summaryrefslogtreecommitdiff
path: root/ext/ripper/lib/ripper/sexp.rb
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-02-01 18:32:40 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-02-01 18:32:40 +0000
commitdee374059771bf9d7e9551c906414b446c719b4a (patch)
treeeff2f975c914af6ed00a7eb41252ef0c5a7bb642 /ext/ripper/lib/ripper/sexp.rb
parent2388b9c79bfd5879da9fd895e7636f8f39a2d2a3 (diff)
* ext/ripper/lib/ripper/tokenizer.rb -> lexer.rb.
* ext/ripper/lib/ripper/lexer.rb: new method Ripper.slice. [experimental] * ext/ripper/lib/ripper/sexp.rb: new file. [experimental] * ext/ripper/lib/ripper.rb: require ripper/lexer and ripper/sexp. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7861 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/ripper/lib/ripper/sexp.rb')
-rw-r--r--ext/ripper/lib/ripper/sexp.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/ext/ripper/lib/ripper/sexp.rb b/ext/ripper/lib/ripper/sexp.rb
new file mode 100644
index 0000000..a55e5f2
--- /dev/null
+++ b/ext/ripper/lib/ripper/sexp.rb
@@ -0,0 +1,60 @@
+#
+# ripper/sexp.rb
+#
+# Copyright (C) 2004,2005 Minero Aoki
+#
+# This program is free software.
+# You can distribute and/or modify this program under the Ruby License.
+# For details of Ruby License, see ruby/COPYING.
+#
+
+require 'ripper/core'
+
+class Ripper
+
+ # [EXPERIMENTAL]
+ # Parses +src+ and create S-exp tree.
+ # This method is for mainly developper use.
+ #
+ # require 'ripper'
+ # require 'pp
+ #
+ # pp Ripper.sexp("def m(a) nil end")
+ # #=> [:program,
+ # [:stmts_add,
+ # [:stmts_new],
+ # [:def,
+ # [:@ident, "m", [1, 4]],
+ # [:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil]],
+ # [:bodystmt,
+ # [:stmts_add, [:stmts_new], [:var_ref, [:@kw, "nil", [1, 9]]]],
+ # nil,
+ # nil,
+ # nil]]]]
+ #
+ def Ripper.sexp(src, filename = '-', lineno = 1)
+ SexpBuilder.new(src, filename, lineno).parse
+ end
+
+ class SexpBuilder < ::Ripper #:nodoc:
+ private
+
+ PARSER_EVENTS.each do |event|
+ module_eval(<<-End)
+ def on_#{event}(*list)
+ list.unshift :#{event}
+ list
+ end
+ End
+ end
+
+ SCANNER_EVENTS.each do |event|
+ module_eval(<<-End)
+ def on_#{event}(tok)
+ [:@#{event}, tok, [lineno(), column()]]
+ end
+ End
+ end
+ end
+
+end