Skip to content

Commit

Permalink
Merge pull request evanphx#24 from dudleyf/ast-helpers-module
Browse files Browse the repository at this point in the history
Output the generated AST helpers in a module so they can be reused.
  • Loading branch information
evanphx committed Apr 19, 2012
2 parents 39046ef + f776af7 commit 2fbf077
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
9 changes: 6 additions & 3 deletions lib/kpeg/code_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,11 +83,14 @@ def handle_ast(code)


if output_node if output_node
code << " end\n" code << " end\n"
code << " module #{root}Construction\n"
methods.each do |short, name, attrs| methods.each do |short, name, attrs|
code << " def #{short}(#{attrs.join(', ')})\n" code << " def #{short}(#{attrs.join(', ')})\n"
code << " #{root}::#{name}.new(#{attrs.join(', ')})\n" code << " #{root}::#{name}.new(#{attrs.join(', ')})\n"
code << " end\n" code << " end\n"
end end
code << " end\n"
code << " include #{root}Construction\n"
end end
end end


Expand Down
26 changes: 16 additions & 10 deletions test/test_kpeg_code_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1661,15 +1661,18 @@ def initialize()
end end
end end
end end
def bracket(receiver, argument) module ASTConstruction
AST::BracketOperator.new(receiver, argument) def bracket(receiver, argument)
end AST::BracketOperator.new(receiver, argument)
def simple() end
AST::Simple.new() def simple()
end AST::Simple.new()
def simple2() end
AST::Simple2.new() def simple2()
AST::Simple2.new()
end
end end
include ASTConstruction
# root = . # root = .
def _root def _root
Expand Down Expand Up @@ -1715,9 +1718,12 @@ def initialize(receiver, argument)
attr_reader :argument attr_reader :argument
end end
end end
def bracket(receiver, argument) module MegaASTConstruction
MegaAST::BracketOperator.new(receiver, argument) def bracket(receiver, argument)
MegaAST::BracketOperator.new(receiver, argument)
end
end end
include MegaASTConstruction
# root = . # root = .
def _root def _root
Expand Down

0 comments on commit 2fbf077

Please sign in to comment.