From 34bdf2919d143380f4108a19d9fcd0d38aaab94c Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Sat, 7 Oct 2023 00:26:36 +0900 Subject: [PATCH] Add NewPerser support --- lib/lrama/command.rb | 2 +- lib/lrama/new_parser.rb | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) 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 .,.,