lib/sexp_processor.rb in ParseTree-1.3.6 vs lib/sexp_processor.rb in ParseTree-1.3.7

- old
+ new

@@ -191,28 +191,38 @@ def s(*args) Sexp.new(*args) end ## +# SexpProcessor base exception class. + +class SexpProcessorError < StandardError; end + +## # Raised by SexpProcessor if it sees a node type listed in its # unsupported list. -class UnsupportedNodeError < SyntaxError; end +class UnsupportedNodeError < SexpProcessorError; end ## # Raised by SexpProcessor if it is in strict mode and sees a node for # which there is no processor available. -class UnknownNodeError < SyntaxError; end +class UnknownNodeError < SexpProcessorError; end ## # Raised by SexpProcessor if a processor did not process every node in # a sexp and @require_empty is true. -class NotEmptyError < SyntaxError; end +class NotEmptyError < SexpProcessorError; end ## +# Raised if assert_type encounters an unexpected sexp type. + +class SexpTypeError < SexpProcessorError; end + +## # 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. @@ -258,11 +268,11 @@ attr_accessor :auto_shift_type ## # An array that specifies node types that are unsupported by this - # processor. SexpProcesor will raise UnsupportedNodeError if you try + # processor. SexpProcessor will raise UnsupportedNodeError if you try # to process one of those node types. attr_accessor :unsupported ## @@ -387,11 +397,11 @@ result = error_handler(type, exp_orig) do self.send(meth, exp) end - raise TypeError, "Result must be a #{@expected}, was #{result.class}:#{result.inspect}" unless @expected === result + raise SexpTypeError, "Result must be a #{@expected}, was #{result.class}:#{result.inspect}" unless @expected === result if @require_empty and not exp.empty? then msg = "exp not empty after #{self.class}.#{meth} on #{exp.inspect}" msg += " from #{exp_orig.inspect}" if $DEBUG raise NotEmptyError, msg @@ -429,39 +439,39 @@ end result end def generate # :nodoc: - raise "not implemented yet" + raise NotImplementedError, "not implemented yet" end ## # Raises unless the Sexp type for +list+ matches +typ+ def assert_type(list, typ) - raise TypeError, "Expected type #{typ.inspect} in #{list.inspect}" if + raise SexpTypeError, "Expected type #{typ.inspect} in #{list.inspect}" if list.first != typ end def error_handler(type, exp=nil) # :nodoc: begin return yield - rescue Exception => err + rescue StandardError => err if @exceptions.has_key? type then return @exceptions[type].call(self, exp, err) else - puts "#{err.class} Exception thrown while processing #{type} for sexp #{exp.inspect} #{caller.inspect}" if $DEBUG + $stderr.puts "#{err.class} Exception thrown while processing #{type} for sexp #{exp.inspect} #{caller.inspect}" if $DEBUG raise end end end private :error_handler ## # Registers an error handler for +node+ - def on(node, &block) - @exceptions[node] = block + def on_error_in(node_type, &block) + @exceptions[node_type] = block end ## # A fairly generic processor for a dummy node. Dummy nodes are used # when your processor is doing a complicated rewrite that replaces