ruby-changes:53920
From: nobu <ko1@a...>
Date: Mon, 3 Dec 2018 09:24:43 +0900 (JST)
Subject: [ruby-changes:53920] nobu:r66140 (trunk): Add RubyVM::AST#pretty_print
nobu 2018-12-03 09:24:38 +0900 (Mon, 03 Dec 2018) New Revision: 66140 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66140 Log: Add RubyVM::AST#pretty_print Modified files: trunk/lib/pp.rb trunk/test/test_pp.rb Index: test/test_pp.rb =================================================================== --- test/test_pp.rb (revision 66139) +++ test/test_pp.rb (revision 66140) @@ -194,4 +194,13 @@ class PPFileStatTest < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/test_pp.rb#L194 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 Index: lib/pp.rb =================================================================== --- lib/pp.rb (revision 66139) +++ lib/pp.rb (revision 66140) @@ -514,6 +514,36 @@ class MatchData # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/pp.rb#L514 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/