lib/lrama/output.rb in lrama-0.3.0 vs lib/lrama/output.rb in lrama-0.4.0
- old
+ new
@@ -23,23 +23,34 @@
@header_file_path = header_file_path
@context = context
@grammar = grammar
end
+ if ERB.instance_method(:initialize).parameters.last.first == :key
+ def self.erb(input)
+ ERB.new(input, trim_mode: '-')
+ end
+ else
+ 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)
+ end
+
def render
report_duration(:render) do
- erb = ERB.new(File.read(template_file), trim_mode: '-')
- erb.filename = template_file
- tmp = erb.result_with_hash(context: @context, output: self)
- tmp = replace_special_variables(tmp, @output_file_path)
+ tmp = eval_template(template_file, @output_file_path)
@out << tmp
if @header_file_path
- erb = ERB.new(File.read(header_template_file), trim_mode: '-')
- erb.filename = header_template_file
- tmp = erb.result_with_hash(context: @context, output: self)
- tmp = replace_special_variables(tmp, @header_file_path)
+ tmp = eval_template(header_template_file, @header_file_path)
if @header_out
@header_out << tmp
else
File.open(@header_file_path, "w+") do |f|
@@ -139,10 +150,10 @@
def user_initial_action(comment = "")
return "" unless @grammar.initial_action
<<-STR
#{comment}
- #line #{@grammar.initial_action.line} "#{@grammar_file_path}"
+#line #{@grammar.initial_action.line} "#{@grammar_file_path}"
#{@grammar.initial_action.translated_code}
STR
end
# b4_user_actions