lib/lrama/output.rb in lrama-0.1.0 vs lib/lrama/output.rb in lrama-0.2.0
- old
+ new
@@ -25,18 +25,18 @@
@grammar = grammar
end
def render
report_duration(:render) do
- erb = ERB.new(File.read(template_file), nil, '-')
+ 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)
@out << tmp
if @header_file_path
- erb = ERB.new(File.read(header_template_file), nil, '-')
+ 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)
if @header_out
@@ -133,10 +133,21 @@
end
str
end
+ # b4_user_initial_action
+ def user_initial_action(comment = "")
+ return "" unless @grammar.initial_action
+
+ <<-STR
+ #{comment}
+ #line #{@grammar.initial_action.line} "#{@grammar_file_path}"
+ #{@grammar.initial_action.translated_code}
+ STR
+ end
+
# b4_user_actions
def user_actions
str = ""
@context.states.rules.each do |rule|
@@ -162,31 +173,111 @@
STR
str
end
+ def omit_braces_and_blanks(param)
+ param[1..-2].strip
+ end
+
# b4_parse_param
def parse_param
- # Omit "{}"
- @grammar.parse_param[1..-2]
+ if @grammar.parse_param
+ omit_braces_and_blanks(@grammar.parse_param)
+ else
+ ""
+ end
end
+ def lex_param
+ if @grammar.lex_param
+ omit_braces_and_blanks(@grammar.lex_param)
+ else
+ ""
+ end
+ end
+
# b4_user_formals
def user_formals
if @grammar.parse_param
", #{parse_param}"
else
""
end
end
+ # b4_user_args
+ def user_args
+ if @grammar.parse_param
+ ", #{parse_param_name}"
+ else
+ ""
+ end
+ end
+
+ def extract_param_name(param)
+ /\A(.)+([a-zA-Z0-9_]+)\z/.match(param)[2]
+ end
+
+ def parse_param_name
+ if @grammar.parse_param
+ extract_param_name(parse_param)
+ else
+ ""
+ end
+ end
+
+ def lex_param_name
+ if @grammar.lex_param
+ extract_param_name(lex_param)
+ else
+ ""
+ end
+ end
+
+ # b4_parse_param_use
+ def parse_param_use(val, loc)
+ str = <<-STR
+ YY_USE (#{val});
+ YY_USE (#{loc});
+ STR
+
+ if @grammar.parse_param
+ str << " YY_USE (#{parse_param_name});"
+ end
+
+ str
+ end
+
+ # b4_yylex_formals
+ def yylex_formals
+ ary = ["&yylval", "&yylloc"]
+
+ if @grammar.lex_param
+ ary << lex_param_name
+ end
+
+ "(#{ary.join(', ')})"
+ end
+
# b4_table_value_equals
def table_value_equals(table, value, literal, symbol)
if literal < table.min || table.max < literal
"0"
else
"((#{value}) == #{symbol})"
end
+ end
+
+ # b4_yyerror_args
+ def yyerror_args
+ ary = ["&yylloc"]
+
+ if @grammar.parse_param
+ ary << parse_param_name
+ end
+
+ "#{ary.join(', ')}"
end
def template_basename
File.basename(template_file)
end