lib/lrama/output.rb in lrama-0.5.6 vs lib/lrama/output.rb in lrama-0.5.7

- old
+ new

@@ -5,11 +5,11 @@ module Lrama class Output extend Forwardable include Report::Duration - attr_reader :grammar_file_path, :context, :grammar, :error_recovery + attr_reader :grammar_file_path, :context, :grammar, :error_recovery, :include_header def_delegators "@context", :yyfinal, :yylast, :yyntokens, :yynnts, :yynrules, :yynstates, :yymaxutok, :yypact_ninf, :yytable_ninf def_delegators "@grammar", :eof_symbol, :error_symbol, :undef_symbol, :accept_symbol @@ -26,10 +26,11 @@ @header_out = header_out @header_file_path = header_file_path @context = context @grammar = grammar @error_recovery = error_recovery + @include_header = header_file_path ? header_file_path.sub("./", "") : nil end if ERB.instance_method(:initialize).parameters.last.first == :key def self.erb(input) ERB.new(input, trim_mode: '-') @@ -38,15 +39,12 @@ def self.erb(input) ERB.new(input, nil, '-') end end - def eval_template(file, path) - erb = self.class.erb(File.read(file)) - erb.filename = file - tmp = erb.result_with_hash(context: @context, output: self) - replace_special_variables(tmp, path) + def render_partial(file) + render_template(partial_file(file)) end def render report_duration(:render) do tmp = eval_template(template_file, @output_file_path) @@ -141,11 +139,11 @@ next unless sym.printer str << <<-STR case #{sym.enum_name}: /* #{sym.comment} */ #line #{sym.printer.lineno} "#{@grammar_file_path}" - #{sym.printer.translated_code(sym.tag)} + {#{sym.printer.translated_code(sym.tag)}} #line [@oline@] [@ofile@] break; STR end @@ -158,11 +156,11 @@ return "" unless @grammar.initial_action <<-STR #{comment} #line #{@grammar.initial_action.line} "#{@grammar_file_path}" - #{@grammar.initial_action.translated_code} + {#{@grammar.initial_action.translated_code}} STR end def symbol_actions_for_error_token str = "" @@ -171,11 +169,11 @@ next unless sym.error_token str << <<-STR case #{sym.enum_name}: /* #{sym.comment} */ #line #{sym.error_token.lineno} "#{@grammar_file_path}" - #{sym.error_token.translated_code(sym.tag)} + {#{sym.error_token.translated_code(sym.tag)}} #line [@oline@] [@ofile@] break; STR end @@ -188,18 +186,17 @@ str = "" @context.states.rules.each do |rule| next unless rule.code - rule = rule code = rule.code spaces = " " * (code.column - 1) str << <<-STR case #{rule.id + 1}: /* #{rule.as_comment} */ #line #{code.line} "#{@grammar_file_path}" -#{spaces}#{rule.translated_code} +#{spaces}{#{rule.translated_code}} #line [@oline@] [@ofile@] break; STR end @@ -210,26 +207,26 @@ STR str end - def omit_braces_and_blanks(param) - param[1..-2].strip + def omit_blanks(param) + param.strip end # b4_parse_param def parse_param if @grammar.parse_param - omit_braces_and_blanks(@grammar.parse_param) + omit_blanks(@grammar.parse_param) else "" end end def lex_param if @grammar.lex_param - omit_braces_and_blanks(@grammar.lex_param) + omit_blanks(@grammar.lex_param) else "" end end @@ -352,15 +349,30 @@ end end private + def eval_template(file, path) + tmp = render_template(file) + replace_special_variables(tmp, path) + end + + def render_template(file) + erb = self.class.erb(File.read(file)) + erb.filename = file + erb.result_with_hash(context: @context, output: self) + end + def template_file File.join(template_dir, @template_name) end def header_template_file File.join(template_dir, "bison/yacc.h") + end + + def partial_file(file) + File.join(template_dir, file) end def template_dir File.expand_path("../../../template", __FILE__) end