Class SexpProcessor
In: lib/sexp_processor.rb
Parent: Object

SexpProcessor provides a uniform interface to process Sexps.

In order to create your own SexpProcessor subclass you‘ll need to call super in the initialize method, then set any of the Sexp flags you want to be different from the defaults.

SexpProcessor uses a Sexp‘s type to determine which process method to call in the subclass. For Sexp s(:lit, 1) SexpProcessor will call process_lit, if it is defined.

You can also specify a default method to call for any Sexp types without a process_<type> method or use the default processor provided to skip over them.

Here is a simple example:

  class MyProcessor < SexpProcessor
    def initialize
      super
      self.strict = false
    end

    def process_lit(exp)
      val = exp.shift
      return val
    end
  end

Methods

Classes and Modules

Class SexpProcessor::Environment

Constants

VERSION = '3.0.1'

Attributes

auto_shift_type  [RW]  Automatically shifts off the Sexp type before handing the Sexp to process_<type>
context  [R]  Return a stack of contexts. Most recent node is first.
debug  [RW]  A Hash of Sexp types and Regexp.

Print a debug message if the Sexp type matches the Hash key and the Sexp‘s inspect output matches the Regexp.

default_method  [RW]  A default method to call if a process_<type> method is not found for the Sexp type.
env  [R]  A scoped environment to make you happy.
expected  [RW]  Expected result class
require_empty  [RW]  Raise an exception if the Sexp is not empty after processing
strict  [RW]  Raise an exception if no process_<type> method is found for a Sexp.
unsupported  [RW]  An array that specifies node types that are unsupported by this processor. SexpProcessor will raise UnsupportedNodeError if you try to process one of those node types.
warn_on_default  [RW]  Emit a warning when the method in default_method is called.

Public Class methods

Creates a new SexpProcessor. Use super to invoke this initializer from SexpProcessor subclasses, then use the attributes above to customize the functionality of the SexpProcessor

Public Instance methods

Raises unless the Sexp type for list matches typ

Registers an error handler for node

Default Sexp processor. Invokes process_<type> methods matching the Sexp type given. Performs additional checks as specified by the initializer.

A fairly generic processor for a dummy node. Dummy nodes are used when your processor is doing a complicated rewrite that replaces the current sexp with multiple sexps.

Bogus Example:

  def process_something(exp)
    return s(:dummy, process(exp), s(:extra, 42))
  end

Add a scope level to the current env. Eg:

  def process_defn exp
    name = exp.shift
    args = process(exp.shift)
    scope do
      body = process(exp.shift)
      # ...
    end
  end

  env[:x] = 42
  scope do
    env[:x]       # => 42
    env[:y] = 24
  end
  env[:y]         # => nil

[Validate]