From 25fadce19d6c7000cab5fab770c212834eb5f464 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 19 Sep 2015 01:12:10 +0300 Subject: [PATCH 1/2] new inspect that outputs ruby code --- lib/ast/node.rb | 28 +++++++++++++++++++++++++++- test/test_ast.rb | 14 ++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/ast/node.rb b/lib/ast/node.rb index 511c70d..43b9787 100644 --- a/lib/ast/node.rb +++ b/lib/ast/node.rb @@ -211,7 +211,33 @@ def to_sexp(indent=0) sexp end - alias :inspect :to_sexp + + # Converts `self` to a s-expression ruby string. + # The code return will recreate the node, using the sexp module s() + # + # @param [Integer] indent Base indentation level. + # @return [String] + def inspect(indent=0) + indented = " " * indent + sexp = "#{indented}s(:#{@type}" + + first_node_child = children.index do |child| + child.is_a?(Node) || child.is_a?(Array) + end || children.count + + children.each_with_index do |child, idx| + sexp << ", " + if child.is_a?(Node) && idx >= first_node_child + sexp << "\n#{child.inspect(indent + 1)}" + else + sexp << " #{child.inspect}" + end + end + + sexp << ")" + + sexp + end # @return [AST::Node] self def to_ast diff --git a/test/test_ast.rb b/test/test_ast.rb index e05fa4d..1c193f0 100644 --- a/test/test_ast.rb +++ b/test/test_ast.rb @@ -64,6 +64,20 @@ class MetaNode < AST::Node ]).to_sexp.should.equal "(a :sym\n (b\n (node 0 1)\n (node 0 1)))" end + it 'should format inspect correctly' do + AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).inspect.should.equal "s(:a, :sym, [1, 2])" + AST::Node.new(:a, [ :sym, + AST::Node.new(:b, [ @node, @node ]) + ]).inspect.should.equal "s(:a, :sym, \n s(:b, \n s(:node, 0, 1), \n s(:node, 0, 1)))" + end + + it 'should recreate inspect output' do + simple_node = AST::Node.new(:a, [ :sym, [ 1, 2 ] ]) + eval(simple_node.inspect).should.equal simple_node + complex_node = s(:a , :sym, s(:b, s(:node, 0, 1), s(:node, 0, 1))) + eval(complex_node.inspect).should.equal complex_node + end + it 'should return self in to_ast' do @node.to_ast.should.be.identical_to @node end From a9bc29377a94d2194e92596aceb9be7c7db4c8f8 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 19 Sep 2015 14:51:15 +0300 Subject: [PATCH 2/2] fix spurious whitespace problem --- lib/ast/node.rb | 5 ++--- test/test_ast.rb | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/ast/node.rb b/lib/ast/node.rb index 43b9787..e89283b 100644 --- a/lib/ast/node.rb +++ b/lib/ast/node.rb @@ -226,11 +226,10 @@ def inspect(indent=0) end || children.count children.each_with_index do |child, idx| - sexp << ", " if child.is_a?(Node) && idx >= first_node_child - sexp << "\n#{child.inspect(indent + 1)}" + sexp << ",\n#{child.inspect(indent + 1)}" else - sexp << " #{child.inspect}" + sexp << ", #{child.inspect}" end end diff --git a/test/test_ast.rb b/test/test_ast.rb index 1c193f0..c90f2e5 100644 --- a/test/test_ast.rb +++ b/test/test_ast.rb @@ -65,10 +65,10 @@ class MetaNode < AST::Node end it 'should format inspect correctly' do - AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).inspect.should.equal "s(:a, :sym, [1, 2])" + AST::Node.new(:a, [ :sym, [ 1, 2 ] ]).inspect.should.equal "s(:a, :sym, [1, 2])" AST::Node.new(:a, [ :sym, AST::Node.new(:b, [ @node, @node ]) - ]).inspect.should.equal "s(:a, :sym, \n s(:b, \n s(:node, 0, 1), \n s(:node, 0, 1)))" + ]).inspect.should.equal "s(:a, :sym,\n s(:b,\n s(:node, 0, 1),\n s(:node, 0, 1)))" end it 'should recreate inspect output' do