lib/sexp_processor.rb in ParseTree-2.0.2 vs lib/sexp_processor.rb in ParseTree-2.1.0

- old
+ new

@@ -135,17 +135,16 @@ def initialize @default_method = nil @warn_on_default = true @auto_shift_type = false @strict = false - @unsupported = [:alloca, :cfunc, :cref, :evstr, :ifunc, :last, :memo, :newline, :opt_n, :method] # internal nodes that you can't get to + @unsupported = [:alloca, :cfunc, :cref, :ifunc, :last, :memo, :newline, :opt_n, :method] # internal nodes that you can't get to @unsupported_checked = false @debug = {} @expected = Sexp @require_empty = true @exceptions = {} - @process_level = 0 # we do this on an instance basis so we can subclass it for # different processors. @processors = {} @rewriters = {} @@ -168,19 +167,23 @@ raise NotEmptyError, msg end end def rewrite(exp) + type = exp.first + + self.context.unshift type # FIX: first one doubles up because process already unshifted -- look at moving initial rewrite up above exp.map! { |sub| Array === sub ? rewrite(sub) : sub } - type = exp.first begin meth = @rewriters[type] exp = self.send(meth, exp) if meth old_type, type = type, exp.first end until old_type == type + self.context.shift + exp end ## # Default Sexp processor. Invokes process_<type> methods matching @@ -188,12 +191,10 @@ # the initializer. def process(exp) return nil if exp.nil? - @process_level += 1 - unless @unsupported_checked then m = public_methods.grep(/^process_/) { |o| o.sub(/^process_/, '').intern } supported = m - (m - @unsupported) raise UnsupportedNodeError, "#{supported.inspect} shouldn't be in @unsupported" unless supported.empty? @@ -205,11 +206,11 @@ type = exp.first raise "type should be a Symbol, not: #{exp.first.inspect}" unless Symbol === type - @context.unshift type + self.context.unshift type if @debug.has_key? type then str = exp.inspect puts "// DEBUG: #{str}" if str =~ @debug[type] end @@ -218,11 +219,11 @@ exp_orig = exp.deep_clone if $DEBUG or @debug.has_key? type or @exceptions.has_key?(type) raise UnsupportedNodeError, "'#{type}' is not a supported node type" if @unsupported.include? type - exp = self.rewrite(exp) if @process_level == 1 + exp = self.rewrite(exp) if self.context.size == 1 if @debug.has_key? type then str = exp.inspect puts "// DEBUG (rewritten): #{str}" if str =~ @debug[type] end @@ -274,13 +275,11 @@ msg += " in #{exp_orig.inspect} from #{caller.inspect}" if $DEBUG raise UnknownNodeError, msg end end - @process_level -= 1 - - @context.shift + self.context.shift result end def generate # :nodoc: raise NotImplementedError, "not implemented yet" @@ -289,10 +288,10 @@ ## # Raises unless the Sexp type for +list+ matches +typ+ def assert_type(list, typ) raise SexpTypeError, "Expected type #{typ.inspect} in #{list.inspect}" if - list.first != typ + not Array === list or list.first != typ end def error_handler(type, exp=nil) # :nodoc: begin return yield