diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-12-03 00:24:38 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-12-03 00:24:38 +0000 |
commit | 87e1dd2982dd472de557f62468d77211c8a71bff (patch) | |
tree | 1eb0da500430c977a9d0f8222c641faa41c5d80d | |
parent | b56e266d6450e21575edf3af355c9723faa0ba97 (diff) | |
download | ruby-87e1dd2982dd472de557f62468d77211c8a71bff.tar.gz |
Add RubyVM::AST#pretty_print
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66140 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | lib/pp.rb | 30 | ||||
-rw-r--r-- | test/test_pp.rb | 9 |
2 files changed, 39 insertions, 0 deletions
@@ -514,6 +514,36 @@ class MatchData # :nodoc: end end +class RubyVM::AbstractSyntaxTree::Node + def pretty_print_children(q, names = []) + children.zip(names) do |c, n| + if n + q.breakable + q.text "#{n}:" + end + q.group(2) do + q.breakable + q.pp c + end + end + end + + def pretty_print(q) + q.group(1, "(#{type.sub(/\ANODE_/,'')}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") { + case type + when "NODE_SCOPE" + pretty_print_children(q, %w"tbl args body") + when "NODE_ARGS" + pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block]) + when "NODE_DEFN" + pretty_print_children(q, %w[mid body]) + else + pretty_print_children(q) + end + } + end +end + class Object < BasicObject # :nodoc: include PP::ObjectMixin end diff --git a/test/test_pp.rb b/test/test_pp.rb index 5f8216dcce..01a5a409b4 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -194,4 +194,13 @@ class PPFileStatTest < Test::Unit::TestCase end end +class PPAbstractSyntaxTree < Test::Unit::TestCase + AST = RubyVM::AbstractSyntaxTree + def test_literal + ast = AST.parse("1") + expected = "(SCOPE@1:0-1:1 tbl: [] args: nil body: (LIT@1:0-1:1 1))" + assert_equal(expected, PP.singleline_pp(ast, ''.dup)) + end +end + end |