Class: Lrama::Output
- Inherits:
-
Object
- Object
- Lrama::Output
- Extended by:
- Forwardable
- Includes:
- Report::Duration
- Defined in:
- lib/lrama/output.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#error_recovery ⇒ Object
readonly
Returns the value of attribute error_recovery.
-
#grammar ⇒ Object
readonly
Returns the value of attribute grammar.
-
#grammar_file_path ⇒ Object
readonly
Returns the value of attribute grammar_file_path.
-
#include_header ⇒ Object
readonly
Returns the value of attribute include_header.
Class Method Summary collapse
Instance Method Summary collapse
- #aux ⇒ Object
- #b4_cpp_guard__b4_spec_mapped_header_file ⇒ Object
- #extract_param_name(param) ⇒ Object
-
#initialize(out:, output_file_path:, template_name:, grammar_file_path:, header_out: nil, header_file_path: nil, context:, grammar:, error_recovery: false) ⇒ Output
constructor
A new instance of Output.
- #int_array_to_string(ary) ⇒ Object
-
#int_type_for(ary) ⇒ Object
b4_int_type_for.
- #lex_param ⇒ Object
- #lex_param_name ⇒ Object
- #omit_blanks(param) ⇒ Object
-
#parse_param ⇒ Object
b4_parse_param.
- #parse_param_name ⇒ Object
-
#parse_param_use(val, loc) ⇒ Object
b4_parse_param_use.
- #render ⇒ Object
- #render_partial(file) ⇒ Object
- #spec_mapped_header_file ⇒ Object
- #symbol_actions_for_error_token ⇒ Object
- #symbol_actions_for_printer ⇒ Object
-
#symbol_enum ⇒ Object
b4_symbol_enum.
-
#table_value_equals(table, value, literal, symbol) ⇒ Object
b4_table_value_equals.
- #template_basename ⇒ Object
-
#token_enums ⇒ Object
A part of b4_token_enums.
-
#user_actions ⇒ Object
b4_user_actions.
-
#user_args ⇒ Object
b4_user_args.
-
#user_formals ⇒ Object
b4_user_formals.
-
#user_initial_action(comment = "") ⇒ Object
b4_user_initial_action.
-
#yyerror_args ⇒ Object
b4_yyerror_args.
-
#yylex_formals ⇒ Object
b4_yylex_formals.
- #yyrline ⇒ Object
- #yytname ⇒ Object
- #yytranslate ⇒ Object
- #yytranslate_inverted ⇒ Object
Methods included from Report::Duration
enable, enabled?, #report_duration
Constructor Details
#initialize(out:, output_file_path:, template_name:, grammar_file_path:, header_out: nil, header_file_path: nil, context:, grammar:, error_recovery: false) ⇒ Output
Returns a new instance of Output.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/lrama/output.rb', line 17 def initialize( out:, output_file_path:, template_name:, grammar_file_path:, header_out: nil, header_file_path: nil, context:, grammar:, error_recovery: false ) @out = out @output_file_path = output_file_path @template_name = template_name @grammar_file_path = grammar_file_path @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 |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/lrama/output.rb', line 10 def context @context end |
#error_recovery ⇒ Object (readonly)
Returns the value of attribute error_recovery.
10 11 12 |
# File 'lib/lrama/output.rb', line 10 def error_recovery @error_recovery end |
#grammar ⇒ Object (readonly)
Returns the value of attribute grammar.
10 11 12 |
# File 'lib/lrama/output.rb', line 10 def grammar @grammar end |
#grammar_file_path ⇒ Object (readonly)
Returns the value of attribute grammar_file_path.
10 11 12 |
# File 'lib/lrama/output.rb', line 10 def grammar_file_path @grammar_file_path end |
#include_header ⇒ Object (readonly)
Returns the value of attribute include_header.
10 11 12 |
# File 'lib/lrama/output.rb', line 10 def include_header @include_header end |
Class Method Details
.erb(input) ⇒ Object
35 36 37 |
# File 'lib/lrama/output.rb', line 35 def self.erb(input) ERB.new(input, trim_mode: '-') end |
Instance Method Details
#aux ⇒ Object
320 321 322 |
# File 'lib/lrama/output.rb', line 320 def aux @grammar.aux end |
#b4_cpp_guard__b4_spec_mapped_header_file ⇒ Object
344 345 346 347 348 349 350 |
# File 'lib/lrama/output.rb', line 344 def b4_cpp_guard__b4_spec_mapped_header_file if @header_file_path "YY_YY_" + @header_file_path.gsub(/[^a-zA-Z_0-9]+/, "_").upcase + "_INCLUDED" else "" end end |
#extract_param_name(param) ⇒ Object
251 252 253 |
# File 'lib/lrama/output.rb', line 251 def extract_param_name(param) param[/\b([a-zA-Z0-9_]+)(?=\s*\z)/] end |
#int_array_to_string(ary) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/lrama/output.rb', line 324 def int_array_to_string(ary) last = ary.count - 1 s = ary.each_with_index.each_slice(10).map do |slice| str = " " slice.each do |e, i| str << sprintf("%6d%s", e, (i == last) ? "" : ",") end str end s.join("\n") end |
#int_type_for(ary) ⇒ Object
b4_int_type_for
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/lrama/output.rb', line 117 def int_type_for(ary) min = ary.min max = ary.max case when (-127 <= min && min <= 127) && (-127 <= max && max <= 127) "yytype_int8" when (0 <= min && min <= 255) && (0 <= max && max <= 255) "yytype_uint8" when (-32767 <= min && min <= 32767) && (-32767 <= max && max <= 32767) "yytype_int16" when (0 <= min && min <= 65535) && (0 <= max && max <= 65535) "yytype_uint16" else "int" end end |
#lex_param ⇒ Object
225 226 227 228 229 230 231 |
# File 'lib/lrama/output.rb', line 225 def lex_param if @grammar.lex_param omit_blanks(@grammar.lex_param) else "" end end |
#lex_param_name ⇒ Object
263 264 265 266 267 268 269 |
# File 'lib/lrama/output.rb', line 263 def lex_param_name if @grammar.lex_param extract_param_name(lex_param) else "" end end |
#omit_blanks(param) ⇒ Object
212 213 214 |
# File 'lib/lrama/output.rb', line 212 def omit_blanks(param) param.strip end |
#parse_param ⇒ Object
b4_parse_param
217 218 219 220 221 222 223 |
# File 'lib/lrama/output.rb', line 217 def parse_param if @grammar.parse_param omit_blanks(@grammar.parse_param) else "" end end |
#parse_param_name ⇒ Object
255 256 257 258 259 260 261 |
# File 'lib/lrama/output.rb', line 255 def parse_param_name if @grammar.parse_param extract_param_name(parse_param) else "" end end |
#parse_param_use(val, loc) ⇒ Object
b4_parse_param_use
272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/lrama/output.rb', line 272 def parse_param_use(val, loc) str = " YY_USE (\#{val});\n YY_USE (\#{loc});\n STR\n\n if @grammar.parse_param\n str << \" YY_USE (\#{parse_param_name});\"\n end\n\n str\nend\n" |
#render ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/lrama/output.rb', line 48 def render report_duration(:render) do tmp = eval_template(template_file, @output_file_path) @out << tmp if @header_file_path tmp = eval_template(header_template_file, @header_file_path) if @header_out @header_out << tmp else File.write(@header_file_path, tmp) end end end end |
#render_partial(file) ⇒ Object
44 45 46 |
# File 'lib/lrama/output.rb', line 44 def render_partial(file) render_template(partial_file(file)) end |
#spec_mapped_header_file ⇒ Object
340 341 342 |
# File 'lib/lrama/output.rb', line 340 def spec_mapped_header_file @header_file_path end |
#symbol_actions_for_error_token ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/lrama/output.rb', line 165 def symbol_actions_for_error_token str = "" @grammar.symbols.each do |sym| next unless sym.error_token str << "case \#{sym.enum_name}: /* \#{sym.comment} */\n#line \#{sym.error_token.lineno} \"\#{@grammar_file_path}\"\n {\#{sym.error_token.translated_code(sym.tag)}}\n#line [@oline@] [@ofile@]\n break;\n\n STR\n end\n\n str\nend\n" |
#symbol_actions_for_printer ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/lrama/output.rb', line 135 def symbol_actions_for_printer str = "" @grammar.symbols.each do |sym| next unless sym.printer str << "case \#{sym.enum_name}: /* \#{sym.comment} */\n#line \#{sym.printer.lineno} \"\#{@grammar_file_path}\"\n {\#{sym.printer.translated_code(sym.tag)}}\n#line [@oline@] [@ofile@]\n break;\n\n STR\n end\n\n str\nend\n" |
#symbol_enum ⇒ Object
b4_symbol_enum
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lrama/output.rb', line 83 def symbol_enum str = "" last_sym_number = @context.yysymbol_kind_t.last[1] @context.yysymbol_kind_t.each do |s_value, sym_number, display_name| s = sprintf("%s = %d%s", s_value, sym_number, (sym_number == last_sym_number) ? "" : ",") if display_name str << sprintf(" %-40s /* %s */\n", s, display_name) else str << sprintf(" %s\n", s) end end str end |
#table_value_equals(table, value, literal, symbol) ⇒ Object
b4_table_value_equals
297 298 299 300 301 302 303 |
# File 'lib/lrama/output.rb', line 297 def table_value_equals(table, value, literal, symbol) if literal < table.min || table.max < literal "0" else "((#{value}) == #{symbol})" end end |
#template_basename ⇒ Object
316 317 318 |
# File 'lib/lrama/output.rb', line 316 def template_basename File.basename(template_file) end |
#token_enums ⇒ Object
A part of b4_token_enums
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lrama/output.rb', line 66 def token_enums str = "" @context.yytokentype.each do |s_value, token_id, display_name| s = sprintf("%s = %d%s", s_value, token_id, token_id == yymaxutok ? "" : ",") if display_name str << sprintf(" %-30s /* %s */\n", s, display_name) else str << sprintf(" %s\n", s) end end str end |
#user_actions ⇒ Object
b4_user_actions
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/lrama/output.rb', line 185 def user_actions str = "" @context.states.rules.each do |rule| next unless rule.code code = rule.code spaces = " " * (code.column - 1) str << " case \#{rule.id + 1}: /* \#{rule.as_comment} */\n#line \#{code.line} \"\#{@grammar_file_path}\"\n\#{spaces}{\#{rule.translated_code}}\n#line [@oline@] [@ofile@]\nbreak;\n\n STR\n end\n\n str << <<-STR\n\n#line [@oline@] [@ofile@]\n STR\n\n str\nend\n" |
#user_args ⇒ Object
b4_user_args
243 244 245 246 247 248 249 |
# File 'lib/lrama/output.rb', line 243 def user_args if @grammar.parse_param ", #{parse_param_name}" else "" end end |
#user_formals ⇒ Object
b4_user_formals
234 235 236 237 238 239 240 |
# File 'lib/lrama/output.rb', line 234 def user_formals if @grammar.parse_param ", #{parse_param}" else "" end end |
#user_initial_action(comment = "") ⇒ Object
b4_user_initial_action
155 156 157 158 159 160 161 162 163 |
# File 'lib/lrama/output.rb', line 155 def user_initial_action(comment = "") return "" unless @grammar.initial_action " \#{comment}\n#line \#{@grammar.initial_action.line} \"\#{@grammar_file_path}\"\n {\#{@grammar.initial_action.translated_code}}\n STR\nend\n" |
#yyerror_args ⇒ Object
b4_yyerror_args
306 307 308 309 310 311 312 313 314 |
# File 'lib/lrama/output.rb', line 306 def yyerror_args ary = ["&yylloc"] if @grammar.parse_param ary << parse_param_name end "#{ary.join(', ')}" end |
#yylex_formals ⇒ Object
b4_yylex_formals
286 287 288 289 290 291 292 293 294 |
# File 'lib/lrama/output.rb', line 286 def yylex_formals ary = ["&yylval", "&yylloc"] if @grammar.lex_param ary << lex_param_name end "(#{ary.join(', ')})" end |
#yyrline ⇒ Object
108 109 110 |
# File 'lib/lrama/output.rb', line 108 def yyrline int_array_to_string(@context.yyrline) end |
#yytname ⇒ Object
112 113 114 |
# File 'lib/lrama/output.rb', line 112 def yytname string_array_to_string(@context.yytname) + " YY_NULLPTR" end |
#yytranslate ⇒ Object
100 101 102 |
# File 'lib/lrama/output.rb', line 100 def yytranslate int_array_to_string(@context.yytranslate) end |
#yytranslate_inverted ⇒ Object
104 105 106 |
# File 'lib/lrama/output.rb', line 104 def yytranslate_inverted int_array_to_string(@context.yytranslate_inverted) end |