summaryrefslogtreecommitdiff
path: root/ext/ripper/lib
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-07-26 13:42:49 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-07-26 13:42:49 +0000
commit57c8f59761c99970e97272580acdbec080db6949 (patch)
tree3ce483a592ee0d880bba53382dfe50bf0f6f63a7 /ext/ripper/lib
parentbf966297118d781933cecdad2ae1b0ac88ff0e53 (diff)
* ext/ripper/lib/ripper/sexp.rb: new method Ripper.sexp_raw.
* ext/ripper/lib/ripper/sexp.rb (Ripper.sexp): returns more readable tree. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/ripper/lib')
-rw-r--r--ext/ripper/lib/ripper/sexp.rb50
1 files changed, 45 insertions, 5 deletions
diff --git a/ext/ripper/lib/ripper/sexp.rb b/ext/ripper/lib/ripper/sexp.rb
index a55e5f2..621dd0e 100644
--- a/ext/ripper/lib/ripper/sexp.rb
+++ b/ext/ripper/lib/ripper/sexp.rb
@@ -33,23 +33,63 @@ class Ripper
# nil]]]]
#
def Ripper.sexp(src, filename = '-', lineno = 1)
+ SexpBuilderPP.new(src, filename, lineno).parse
+ end
+
+ def Ripper.sexp_raw(src, filename = '-', lineno = 1)
SexpBuilder.new(src, filename, lineno).parse
end
+ class SexpBuilderPP < ::Ripper #:nodoc:
+ private
+
+ PARSER_EVENTS.each do |event|
+ case event.to_s
+ when /_new\z/
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(*args)
+ []
+ end
+ End
+ when /_add\z/
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(list, item)
+ list.push item
+ list
+ end
+ End
+ else
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(*args)
+ [:#{event}, *args]
+ end
+ End
+ end
+ end
+
+ SCANNER_EVENTS.each do |event|
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(tok)
+ [:@#{event}, tok, [lineno(), column()]]
+ end
+ End
+ end
+ end
+
class SexpBuilder < ::Ripper #:nodoc:
private
PARSER_EVENTS.each do |event|
- module_eval(<<-End)
- def on_#{event}(*list)
- list.unshift :#{event}
- list
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
+ def on_#{event}(*args)
+ args.unshift :#{event}
+ args
end
End
end
SCANNER_EVENTS.each do |event|
- module_eval(<<-End)
+ module_eval(<<-End, __FILE__, __LINE__ + 1)
def on_#{event}(tok)
[:@#{event}, tok, [lineno(), column()]]
end