Skip to content
Browse files

Output the generated AST helpers in a module so they can be reused.

  • Loading branch information...
1 parent 39046ef commit ef98f1b3f2f746b627a6831a8cf346e5d51fb375 @dudleyf committed
Showing with 22 additions and 13 deletions.
  1. +6 −3 lib/kpeg/code_generator.rb
  2. +16 −10 test/test_kpeg_code_generator.rb
View
9 lib/kpeg/code_generator.rb
@@ -83,11 +83,14 @@ def handle_ast(code)
if output_node
code << " end\n"
+ code << " module #{root}Helpers\n"
methods.each do |short, name, attrs|
- code << " def #{short}(#{attrs.join(', ')})\n"
- code << " #{root}::#{name}.new(#{attrs.join(', ')})\n"
- code << " end\n"
+ code << " def #{short}(#{attrs.join(', ')})\n"
+ code << " #{root}::#{name}.new(#{attrs.join(', ')})\n"
+ code << " end\n"
end
+ code << " end\n"
+ code << " include #{root}Helpers\n"
end
end
View
26 test/test_kpeg_code_generator.rb
@@ -1661,15 +1661,18 @@ def initialize()
end
end
end
- def bracket(receiver, argument)
- AST::BracketOperator.new(receiver, argument)
- end
- def simple()
- AST::Simple.new()
- end
- def simple2()
- AST::Simple2.new()
+ module ASTHelpers
+ def bracket(receiver, argument)
+ AST::BracketOperator.new(receiver, argument)
+ end
+ def simple()
+ AST::Simple.new()
+ end
+ def simple2()
+ AST::Simple2.new()
+ end
end
+ include ASTHelpers
# root = .
def _root
@@ -1715,9 +1718,12 @@ def initialize(receiver, argument)
attr_reader :argument
end
end
- def bracket(receiver, argument)
- MegaAST::BracketOperator.new(receiver, argument)
+ module MegaASTHelpers
+ def bracket(receiver, argument)
+ MegaAST::BracketOperator.new(receiver, argument)
+ end
end
+ include MegaASTHelpers
# root = .
def _root

0 comments on commit ef98f1b

Please sign in to comment.
Something went wrong with that request. Please try again.