diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index fd8c4006..973f2df1 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -668,7 +668,7 @@ def token_to_str(t) module Lrama class Parser < Racc::Parser -module_eval(<<'...end parser.y/module_eval...', 'parser.y', 392) +module_eval(<<'...end parser.y/module_eval...', 'parser.y', 351) include Lrama::Report::Duration @@ -713,6 +713,11 @@ def reset_precs @prec_seen = false @code_after_prec = false end + +def begin_c_declaration + @lexer.status = :initial + @lexer.end_symbol = nil +end ...end parser.y/module_eval... ##### State transition tables begin ### @@ -1138,16 +1143,14 @@ def _reduce_4(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 16) +module_eval(<<'.,.,', 'parser.y', 14) def _reduce_5(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 21) +module_eval(<<'.,.,', 'parser.y', 17) def _reduce_6(val, _values, result) @grammar.prologue = val[2].s_value @@ -1157,7 +1160,7 @@ def _reduce_6(val, _values, result) # reduce 7 omitted -module_eval(<<'.,.,', 'parser.y', 25) +module_eval(<<'.,.,', 'parser.y', 21) def _reduce_8(val, _values, result) result = "" result @@ -1168,7 +1171,7 @@ def _reduce_8(val, _values, result) # reduce 10 omitted -module_eval(<<'.,.,', 'parser.y', 29) +module_eval(<<'.,.,', 'parser.y', 25) def _reduce_11(val, _values, result) @grammar.expect = val[1] result @@ -1181,7 +1184,7 @@ def _reduce_11(val, _values, result) # reduce 14 omitted -module_eval(<<'.,.,', 'parser.y', 35) +module_eval(<<'.,.,', 'parser.y', 31) def _reduce_15(val, _values, result) val[1].each {|token| token.references = [] @@ -1192,7 +1195,7 @@ def _reduce_15(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 42) +module_eval(<<'.,.,', 'parser.y', 38) def _reduce_16(val, _values, result) val[1].each {|token| token.references = [] @@ -1203,7 +1206,7 @@ def _reduce_16(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 49) +module_eval(<<'.,.,', 'parser.y', 45) def _reduce_17(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1212,16 +1215,14 @@ def _reduce_17(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 54) +module_eval(<<'.,.,', 'parser.y', 48) def _reduce_18(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 59) +module_eval(<<'.,.,', 'parser.y', 51) def _reduce_19(val, _values, result) @grammar.initial_action = @grammar.build_code(:initial_action, val[3]) @@ -1231,7 +1232,7 @@ def _reduce_19(val, _values, result) # reduce 20 omitted -module_eval(<<'.,.,', 'parser.y', 65) +module_eval(<<'.,.,', 'parser.y', 57) def _reduce_21(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1240,16 +1241,14 @@ def _reduce_21(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 70) +module_eval(<<'.,.,', 'parser.y', 60) def _reduce_22(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 75) +module_eval(<<'.,.,', 'parser.y', 63) def _reduce_23(val, _values, result) @grammar.set_union(@grammar.build_code(:union, val[3]), val[3].line) @@ -1259,7 +1258,7 @@ def _reduce_23(val, _values, result) # reduce 24 omitted -module_eval(<<'.,.,', 'parser.y', 80) +module_eval(<<'.,.,', 'parser.y', 68) def _reduce_25(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1268,18 +1267,16 @@ def _reduce_25(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 85) +module_eval(<<'.,.,', 'parser.y', 71) def _reduce_26(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., # reduce 27 omitted -module_eval(<<'.,.,', 'parser.y', 91) +module_eval(<<'.,.,', 'parser.y', 75) def _reduce_28(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1288,16 +1285,14 @@ def _reduce_28(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 96) +module_eval(<<'.,.,', 'parser.y', 78) def _reduce_29(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 101) +module_eval(<<'.,.,', 'parser.y', 81) def _reduce_30(val, _values, result) @grammar.add_printer(ident_or_tags: val[6], code: @grammar.build_code(:printer, val[3]), lineno: val[3].line) @@ -1305,7 +1300,7 @@ def _reduce_30(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 105) +module_eval(<<'.,.,', 'parser.y', 85) def _reduce_31(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1314,16 +1309,14 @@ def _reduce_31(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 110) +module_eval(<<'.,.,', 'parser.y', 88) def _reduce_32(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 115) +module_eval(<<'.,.,', 'parser.y', 91) def _reduce_33(val, _values, result) @grammar.add_error_token(ident_or_tags: val[6], code: @grammar.build_code(:error_token, val[3]), lineno: val[3].line) @@ -1333,7 +1326,7 @@ def _reduce_33(val, _values, result) # reduce 34 omitted -module_eval(<<'.,.,', 'parser.y', 121) +module_eval(<<'.,.,', 'parser.y', 97) def _reduce_35(val, _values, result) val[1].each {|hash| hash[:tokens].each {|id| @@ -1345,7 +1338,7 @@ def _reduce_35(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 129) +module_eval(<<'.,.,', 'parser.y', 105) def _reduce_36(val, _values, result) val[1].each {|hash| hash[:tokens].each {|id| @@ -1359,7 +1352,7 @@ def _reduce_36(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 139) +module_eval(<<'.,.,', 'parser.y', 115) def _reduce_37(val, _values, result) val[1].each {|hash| hash[:tokens].each {|id| @@ -1373,7 +1366,7 @@ def _reduce_37(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 149) +module_eval(<<'.,.,', 'parser.y', 125) def _reduce_38(val, _values, result) val[1].each {|hash| hash[:tokens].each {|id| @@ -1387,7 +1380,7 @@ def _reduce_38(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 159) +module_eval(<<'.,.,', 'parser.y', 135) def _reduce_39(val, _values, result) val[1].each {|hash| hash[:tokens].each {|id| @@ -1401,7 +1394,7 @@ def _reduce_39(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 170) +module_eval(<<'.,.,', 'parser.y', 146) def _reduce_40(val, _values, result) val[0].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: nil, replace: true) @@ -1411,7 +1404,7 @@ def _reduce_40(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 176) +module_eval(<<'.,.,', 'parser.y', 152) def _reduce_41(val, _values, result) val[1].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: val[0], replace: true) @@ -1421,7 +1414,7 @@ def _reduce_41(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 182) +module_eval(<<'.,.,', 'parser.y', 158) def _reduce_42(val, _values, result) val[2].each {|token_declaration| @grammar.add_term(id: token_declaration[0], alias_name: token_declaration[2], token_id: token_declaration[1], tag: val[1], replace: true) @@ -1431,21 +1424,21 @@ def _reduce_42(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 187) +module_eval(<<'.,.,', 'parser.y', 163) def _reduce_43(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 188) +module_eval(<<'.,.,', 'parser.y', 164) def _reduce_44(val, _values, result) result = val[0].append(val[1]) result end .,., -module_eval(<<'.,.,', 'parser.y', 190) +module_eval(<<'.,.,', 'parser.y', 166) def _reduce_45(val, _values, result) result = val result @@ -1460,7 +1453,7 @@ def _reduce_45(val, _values, result) # reduce 49 omitted -module_eval(<<'.,.,', 'parser.y', 200) +module_eval(<<'.,.,', 'parser.y', 176) def _reduce_50(val, _values, result) result = [{tag: nil, tokens: val[0]}] @@ -1468,7 +1461,7 @@ def _reduce_50(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 204) +module_eval(<<'.,.,', 'parser.y', 180) def _reduce_51(val, _values, result) result = [{tag: val[0], tokens: val[1]}] @@ -1476,7 +1469,7 @@ def _reduce_51(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 208) +module_eval(<<'.,.,', 'parser.y', 184) def _reduce_52(val, _values, result) result = val[0].append({tag: val[1], tokens: val[2]}) @@ -1484,14 +1477,14 @@ def _reduce_52(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 211) +module_eval(<<'.,.,', 'parser.y', 187) def _reduce_53(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 212) +module_eval(<<'.,.,', 'parser.y', 188) def _reduce_54(val, _values, result) result = val[0].append(val[1]) result @@ -1502,7 +1495,7 @@ def _reduce_54(val, _values, result) # reduce 56 omitted -module_eval(<<'.,.,', 'parser.y', 219) +module_eval(<<'.,.,', 'parser.y', 195) def _reduce_57(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1511,16 +1504,14 @@ def _reduce_57(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 224) +module_eval(<<'.,.,', 'parser.y', 198) def _reduce_58(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 229) +module_eval(<<'.,.,', 'parser.y', 201) def _reduce_59(val, _values, result) result = val[0].append(val[3]) @@ -1528,7 +1519,7 @@ def _reduce_59(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 233) +module_eval(<<'.,.,', 'parser.y', 205) def _reduce_60(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '}' @@ -1537,16 +1528,14 @@ def _reduce_60(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 238) +module_eval(<<'.,.,', 'parser.y', 208) def _reduce_61(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 243) +module_eval(<<'.,.,', 'parser.y', 211) def _reduce_62(val, _values, result) result = [val[2]] @@ -1554,7 +1543,7 @@ def _reduce_62(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 248) +module_eval(<<'.,.,', 'parser.y', 216) def _reduce_63(val, _values, result) result = [{tag: nil, tokens: val[0]}] @@ -1562,7 +1551,7 @@ def _reduce_63(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 252) +module_eval(<<'.,.,', 'parser.y', 220) def _reduce_64(val, _values, result) result = [{tag: val[0], tokens: val[1]}] @@ -1570,7 +1559,7 @@ def _reduce_64(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 256) +module_eval(<<'.,.,', 'parser.y', 224) def _reduce_65(val, _values, result) result = val[0].append({tag: nil, tokens: val[1]}) @@ -1578,14 +1567,14 @@ def _reduce_65(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 259) +module_eval(<<'.,.,', 'parser.y', 227) def _reduce_66(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 260) +module_eval(<<'.,.,', 'parser.y', 228) def _reduce_67(val, _values, result) result = val[0].append(val[1]) result @@ -1594,14 +1583,14 @@ def _reduce_67(val, _values, result) # reduce 68 omitted -module_eval(<<'.,.,', 'parser.y', 264) +module_eval(<<'.,.,', 'parser.y', 232) def _reduce_69(val, _values, result) raise "Ident after %prec" if @prec_seen result end .,., -module_eval(<<'.,.,', 'parser.y', 265) +module_eval(<<'.,.,', 'parser.y', 233) def _reduce_70(val, _values, result) raise "Char after %prec" if @prec_seen result @@ -1616,7 +1605,7 @@ def _reduce_70(val, _values, result) # reduce 74 omitted -module_eval(<<'.,.,', 'parser.y', 275) +module_eval(<<'.,.,', 'parser.y', 243) def _reduce_75(val, _values, result) lhs = val[0] lhs.alias = val[1] @@ -1628,7 +1617,7 @@ def _reduce_75(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 284) +module_eval(<<'.,.,', 'parser.y', 252) def _reduce_76(val, _values, result) result = [{rhs: val[0], lineno: val[0].first&.line || @lexer.line - 1}] @@ -1636,7 +1625,7 @@ def _reduce_76(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 288) +module_eval(<<'.,.,', 'parser.y', 256) def _reduce_77(val, _values, result) result = val[0].append({rhs: val[2], lineno: val[2].first&.line || @lexer.line - 1}) @@ -1646,7 +1635,7 @@ def _reduce_77(val, _values, result) # reduce 78 omitted -module_eval(<<'.,.,', 'parser.y', 294) +module_eval(<<'.,.,', 'parser.y', 262) def _reduce_79(val, _values, result) reset_precs result = [] @@ -1655,7 +1644,7 @@ def _reduce_79(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 299) +module_eval(<<'.,.,', 'parser.y', 267) def _reduce_80(val, _values, result) reset_precs result = [] @@ -1664,7 +1653,7 @@ def _reduce_80(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 304) +module_eval(<<'.,.,', 'parser.y', 272) def _reduce_81(val, _values, result) token = val[1] token.alias = val[2] @@ -1674,7 +1663,7 @@ def _reduce_81(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 310) +module_eval(<<'.,.,', 'parser.y', 278) def _reduce_82(val, _values, result) if @prec_seen raise "Multiple User_code after %prec" if @code_after_prec @@ -1687,16 +1676,14 @@ def _reduce_82(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 319) +module_eval(<<'.,.,', 'parser.y', 285) def _reduce_83(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 324) +module_eval(<<'.,.,', 'parser.y', 288) def _reduce_84(val, _values, result) token = val[3] token.alias = val[6] @@ -1706,7 +1693,7 @@ def _reduce_84(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 330) +module_eval(<<'.,.,', 'parser.y', 294) def _reduce_85(val, _values, result) if @prec_seen raise "Multiple User_code after %prec" if @code_after_prec @@ -1719,16 +1706,14 @@ def _reduce_85(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 339) +module_eval(<<'.,.,', 'parser.y', 301) def _reduce_86(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil - + begin_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 344) +module_eval(<<'.,.,', 'parser.y', 304) def _reduce_87(val, _values, result) token = val[2] token.alias = val[5] @@ -1738,7 +1723,7 @@ def _reduce_87(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 350) +module_eval(<<'.,.,', 'parser.y', 310) def _reduce_88(val, _values, result) sym = @grammar.find_symbol_by_id!(val[2]) result = val[0].append(sym) @@ -1750,7 +1735,7 @@ def _reduce_88(val, _values, result) # reduce 89 omitted -module_eval(<<'.,.,', 'parser.y', 356) +module_eval(<<'.,.,', 'parser.y', 316) def _reduce_90(val, _values, result) result = val[1].s_value result @@ -1761,7 +1746,7 @@ def _reduce_90(val, _values, result) # reduce 92 omitted -module_eval(<<'.,.,', 'parser.y', 363) +module_eval(<<'.,.,', 'parser.y', 323) def _reduce_93(val, _values, result) @lexer.status = :c_declaration @lexer.end_symbol = '\Z' @@ -1771,10 +1756,9 @@ def _reduce_93(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 369) +module_eval(<<'.,.,', 'parser.y', 329) def _reduce_94(val, _values, result) - @lexer.status = :initial - @lexer.end_symbol = nil + begin_c_declaration @grammar.epilogue = val[2].s_value result @@ -1791,14 +1775,14 @@ def _reduce_94(val, _values, result) # reduce 99 omitted -module_eval(<<'.,.,', 'parser.y', 381) +module_eval(<<'.,.,', 'parser.y', 340) def _reduce_100(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 382) +module_eval(<<'.,.,', 'parser.y', 341) def _reduce_101(val, _values, result) result = val[0].append(val[1]) result @@ -1809,7 +1793,7 @@ def _reduce_101(val, _values, result) # reduce 103 omitted -module_eval(<<'.,.,', 'parser.y', 387) +module_eval(<<'.,.,', 'parser.y', 346) def _reduce_104(val, _values, result) result = Lrama::Lexer::Token.new(type: Lrama::Lexer::Token::Ident, s_value: val[0]) result diff --git a/parser.y b/parser.y index fa5a3a5c..931510fd 100644 --- a/parser.y +++ b/parser.y @@ -12,11 +12,7 @@ rule @lexer.end_symbol = '%}' @grammar.prologue_first_lineno = @lexer.line } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "%}" { @grammar.prologue = val[2].s_value @@ -50,11 +46,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" { @grammar.initial_action = @grammar.build_code(:initial_action, val[3]) @@ -66,11 +58,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" { @grammar.set_union(@grammar.build_code(:union, val[3]), val[3].line) @@ -81,22 +69,14 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } - "}" generic_symlist + C_DECLARATION { begin_c_declaration } + "}" generic_symlist | "%printer" "{" { @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" generic_symlist { @grammar.add_printer(ident_or_tags: val[6], code: @grammar.build_code(:printer, val[3]), lineno: val[3].line) @@ -106,11 +86,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" generic_symlist { @grammar.add_error_token(ident_or_tags: val[6], code: @grammar.build_code(:error_token, val[3]), lineno: val[3].line) @@ -220,11 +196,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" { result = val[0].append(val[3]) @@ -234,11 +206,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" { result = [val[2]] @@ -315,11 +283,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" named_ref_opt { token = val[3] @@ -335,11 +299,7 @@ rule @lexer.status = :c_declaration @lexer.end_symbol = '}' } - C_DECLARATION - { - @lexer.status = :initial - @lexer.end_symbol = nil - } + C_DECLARATION { begin_c_declaration } "}" named_ref_opt { token = val[2] @@ -367,8 +327,7 @@ rule } C_DECLARATION { - @lexer.status = :initial - @lexer.end_symbol = nil + begin_c_declaration @grammar.epilogue = val[2].s_value } @@ -433,3 +392,8 @@ def reset_precs @prec_seen = false @code_after_prec = false end + +def begin_c_declaration + @lexer.status = :initial + @lexer.end_symbol = nil +end