Last active
December 10, 2015 13:28
-
-
Save JoshCheek/4441025 to your computer and use it in GitHub Desktop.
Ripper for syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ripper' | |
Ripper::SexpBuilder.instance_methods.grep(/error/i) # => [:on_alias_error, :on_assign_error, :on_class_name_error, :on_param_error, :on_parse_error] | |
class DemoBuilder < Ripper::SexpBuilder | |
instance_methods.each do |meth_name| | |
next unless meth_name =~ /^on_/ | |
super_meth = instance_method meth_name | |
define_method meth_name do |*args| | |
super_meth.bind(self).call(*args).tap do |result| | |
puts "#{meth_name}(#{args.map(&:inspect).join(', ')})".ljust(50) << " #> #{result.inspect}" | |
end | |
end | |
end | |
end | |
src = %(+) | |
DemoBuilder.new(src).parse | |
# >> on_op("+") #> [:@op, "+", [1, 0]] | |
# >> on_parse_error("syntax error, unexpected $end") #> [:parse_error, "syntax error, unexpected $end"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment