diff --git a/lib/lrama/command.rb b/lib/lrama/command.rb index 213b7acb..eff31e46 100644 --- a/lib/lrama/command.rb +++ b/lib/lrama/command.rb @@ -8,7 +8,7 @@ def run(argv) warning = Lrama::Warning.new text = options.y.read options.y.close if options.y != STDIN - grammar = Lrama::NewParser.new(text).parse + grammar = Lrama::NewParser.new(text, options.header_file).parse states = Lrama::States.new(grammar, warning, trace_state: (options.trace_opts[:automaton] || options.trace_opts[:closure])) states.compute context = Lrama::Context.new(states) diff --git a/lib/lrama/new_parser.rb b/lib/lrama/new_parser.rb index a18e127a..af28ed21 100644 --- a/lib/lrama/new_parser.rb +++ b/lib/lrama/new_parser.rb @@ -10,8 +10,9 @@ class NewParser < Racc::Parser module_eval(<<'...end parser.y/module_eval...', 'parser.y', 121) -def initialize(text) +def initialize(text, header_file) @text = text + @header_path = header_file ? header_file.sub("./", "") : nil @lineno = [] @column = [] end @@ -474,7 +475,11 @@ def _reduce_5(val, _values, result) module_eval(<<'.,.,', 'parser.y', 7) def _reduce_6(val, _values, result) - @grammar.prologue = val[2] + if @header_path + @grammar.prologue = "\n#include \"#{@header_path}\"\n" + val[2] + else + @grammar.prologue = val[2] + end result end .,.,