diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 24db131c..ee4f0346 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -752,7 +752,7 @@ def raise_parse_error(error_message, location) 50, 50, 79, 79, 51, 51, 50, 206, 79, 79, 51, 51, 206, 206, 79, 79, 51, 51, 50, 50, 79, 186, 187, 188, 96, 186, 187, 188, 96, 216, - 220, 227, 217, 217, 217, 51, 51, 50, 50, 186, + 220, 228, 217, 217, 217, 51, 51, 50, 50, 186, 187, 188, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 90, 94, 96, 101, 101, 101, 103, 109, 113, 114, 117, 117, 117, 117, 120, 47, 124, @@ -760,7 +760,7 @@ def raise_parse_error(error_message, location) 142, 143, 146, 147, 148, 150, 160, 168, 170, 171, 172, 173, 174, 175, 176, 177, 146, 183, 191, 192, 160, 160, 203, 210, 211, 177, 214, 215, 210, 225, - 226, 96, 96, 210 ] + 210, 227, 96, 96, 210 ] racc_action_check = [ 49, 145, 49, 145, 39, 145, 159, 182, 159, 182, @@ -791,7 +791,7 @@ def raise_parse_error(error_message, location) 122, 123, 124, 125, 126, 128, 141, 149, 151, 152, 153, 154, 155, 156, 157, 158, 161, 163, 167, 169, 175, 177, 179, 185, 189, 199, 204, 206, 216, 219, - 221, 225, 226, 228 ] + 220, 221, 225, 227, 229 ] racc_action_pointer = [ nil, 20, 22, 26, 98, nil, nil, 39, nil, 33, @@ -816,11 +816,11 @@ def raise_parse_error(error_message, location) nil, nil, nil, 135, nil, nil, nil, 141, 147, 230, 9, nil, nil, 177, 274, nil, 237, 158, 161, nil, nil, nil, nil, 159, nil, 178, 270, 183, nil, 259, - nil, 260, 160, nil, nil, 230, 231, nil, 275, nil, - nil ] + 272, 261, 160, nil, nil, 231, nil, 232, nil, 276, + nil, nil ] racc_action_default = [ - -2, -138, -8, -138, -138, -3, -4, -138, 231, -138, + -2, -138, -8, -138, -138, -3, -4, -138, 232, -138, -9, -10, -11, -12, -138, -138, -138, -138, -138, -138, -138, -24, -25, -138, -29, -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, @@ -842,67 +842,67 @@ def raise_parse_error(error_message, location) -112, -84, -20, -32, -131, -133, -134, -35, -38, -56, -61, -64, -65, -138, -138, -70, -94, -138, -116, -107, -137, -110, -132, -138, -68, -138, -136, -138, -118, -138, - -66, -138, -138, -108, -117, -120, -120, -119, -136, -69, - -111 ] + -136, -138, -138, -108, -117, -120, -66, -120, -119, -136, + -69, -111 ] racc_goto_table = [ - 76, 74, 69, 52, 209, 95, 110, 118, 185, 145, - 158, 207, 1, 2, 119, 212, 4, 43, 42, 212, - 212, 72, 202, 84, 84, 84, 84, 80, 85, 86, - 87, 213, 53, 55, 56, 223, 193, 197, 198, 111, - 115, 76, 116, 222, 199, 118, 182, 230, 110, 138, - 92, 91, 218, 5, 40, 122, 10, 72, 72, 11, - 12, 13, 49, 97, 118, 128, 169, 104, 151, 84, - 84, 110, 105, 152, 106, 153, 107, 154, 134, 108, - 76, 137, 115, 155, 68, 73, 112, 135, 139, 121, - 200, 204, 221, 126, 167, 102, 149, 72, 144, 72, - 189, 219, 123, 115, nil, 84, nil, 84, nil, nil, - nil, 164, nil, nil, nil, nil, nil, nil, nil, nil, - nil, nil, 72, 184, nil, 178, 84, nil, nil, nil, - nil, nil, 190, nil, nil, nil, nil, 201, nil, nil, + 76, 95, 69, 52, 74, 118, 110, 209, 185, 145, + 158, 1, 119, 212, 2, 4, 43, 212, 212, 42, + 91, 72, 202, 84, 84, 84, 84, 80, 85, 86, + 87, 5, 40, 207, 53, 55, 56, 122, 223, 111, + 115, 76, 226, 118, 199, 116, 182, 138, 110, 92, + 10, 231, 218, 213, 193, 197, 198, 72, 72, 11, + 12, 13, 118, 49, 97, 222, 128, 169, 104, 84, + 84, 110, 151, 105, 152, 106, 153, 107, 134, 154, + 76, 108, 115, 155, 137, 68, 73, 112, 135, 139, + 121, 200, 204, 221, 126, 167, 102, 72, 149, 72, + 144, 189, 219, 115, 123, 84, nil, 84, nil, nil, + nil, 164, nil, nil, nil, nil, nil, nil, nil, 184, + nil, nil, 72, nil, nil, 178, 84, nil, nil, nil, + nil, nil, 190, 201, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 205, 164, 208, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 178, nil, nil, 208, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - nil, 208, nil, 224, 228, 229 ] + 229, 208, 230, 224 ] racc_goto_check = [ - 42, 48, 33, 35, 60, 43, 34, 55, 44, 59, - 39, 45, 1, 2, 54, 64, 3, 56, 4, 64, - 64, 35, 44, 35, 35, 35, 35, 32, 32, 32, - 32, 45, 15, 15, 15, 60, 21, 21, 21, 33, - 42, 42, 48, 45, 39, 55, 59, 60, 34, 54, - 56, 5, 44, 6, 7, 8, 9, 35, 35, 10, - 11, 12, 13, 14, 55, 16, 17, 18, 19, 35, - 35, 34, 22, 23, 24, 25, 26, 27, 33, 28, - 42, 48, 42, 29, 30, 31, 36, 37, 38, 40, - 41, 46, 47, 50, 51, 52, 53, 35, 58, 35, - 61, 62, 63, 42, nil, 35, nil, 35, nil, nil, - nil, 42, nil, nil, nil, nil, nil, nil, nil, nil, - nil, nil, 35, 43, nil, 42, 35, nil, nil, nil, - nil, nil, 42, nil, nil, nil, nil, 43, nil, nil, + 42, 43, 33, 35, 49, 56, 34, 46, 44, 60, + 39, 1, 55, 64, 2, 3, 57, 64, 64, 4, + 5, 35, 44, 35, 35, 35, 35, 32, 32, 32, + 32, 6, 7, 45, 15, 15, 15, 8, 46, 33, + 42, 42, 46, 56, 39, 49, 60, 55, 34, 57, + 9, 46, 44, 45, 21, 21, 21, 35, 35, 10, + 11, 12, 56, 13, 14, 45, 16, 17, 18, 35, + 35, 34, 19, 22, 23, 24, 25, 26, 33, 27, + 42, 28, 42, 29, 49, 30, 31, 36, 37, 38, + 40, 41, 47, 48, 51, 52, 53, 35, 54, 35, + 59, 61, 62, 42, 63, 35, nil, 35, nil, nil, + nil, 42, nil, nil, nil, nil, nil, nil, nil, 43, + nil, nil, 35, nil, nil, 42, 35, nil, nil, nil, + nil, nil, 42, 43, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 42, 42, 42, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 42, nil, nil, 42, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - nil, 42, nil, 42, 43, 43 ] + 43, 42, 43, 42 ] racc_goto_pointer = [ - nil, 12, 13, 14, 9, 9, 51, 48, -35, 52, - 55, 56, 57, 47, 14, 16, -38, -84, 9, -61, - nil, -136, 13, -57, 14, -56, 15, -55, 17, -50, - 51, 51, -8, -31, -63, -12, 14, -25, -32, -131, - 0, -87, -34, -41, -156, -172, -89, -122, -33, nil, - -8, -54, 41, -31, -68, -74, 8, nil, -26, -115, - -181, -65, -110, 9, -178 ] + nil, 11, 14, 13, 10, -22, 29, 26, -53, 46, + 55, 56, 57, 48, 15, 18, -37, -83, 10, -57, + nil, -118, 14, -56, 15, -55, 16, -53, 19, -50, + 52, 52, -8, -31, -63, -12, 15, -24, -31, -131, + 1, -86, -34, -45, -156, -150, -178, -88, -121, -30, + nil, -7, -53, 42, -29, -70, -76, 7, nil, -24, + -115, -64, -109, 11, -180 ] racc_goto_default = [ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 45, nil, nil, nil, nil, nil, nil, nil, nil, nil, 24, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 71, 77, nil, nil, nil, nil, - 46, 159, 195, nil, nil, nil, nil, nil, nil, 78, - nil, nil, nil, nil, 81, 83, nil, 44, nil, nil, + 46, 159, 195, nil, nil, nil, nil, nil, nil, nil, + 78, nil, nil, nil, nil, 81, 83, nil, 44, nil, nil, nil, nil, nil, 194 ] racc_reduce_table = [ @@ -972,9 +972,9 @@ def raise_parse_error(error_message, location) 1, 95, :_reduce_63, 3, 95, :_reduce_64, 3, 95, :_reduce_65, - 5, 95, :_reduce_66, - 0, 100, :_reduce_67, - 0, 101, :_reduce_68, + 6, 95, :_reduce_66, + 0, 101, :_reduce_67, + 0, 102, :_reduce_68, 7, 95, :_reduce_69, 3, 95, :_reduce_70, 0, 90, :_reduce_none, @@ -984,41 +984,41 @@ def raise_parse_error(error_message, location) 1, 85, :_reduce_75, 2, 85, :_reduce_76, 3, 85, :_reduce_77, - 1, 102, :_reduce_78, - 2, 102, :_reduce_79, + 1, 103, :_reduce_78, + 2, 103, :_reduce_79, 1, 96, :_reduce_none, 1, 96, :_reduce_none, - 0, 104, :_reduce_82, - 0, 105, :_reduce_83, + 0, 105, :_reduce_82, + 0, 106, :_reduce_83, 6, 69, :_reduce_84, - 0, 106, :_reduce_85, - 0, 107, :_reduce_86, + 0, 107, :_reduce_85, + 0, 108, :_reduce_86, 5, 69, :_reduce_87, 1, 86, :_reduce_88, 2, 86, :_reduce_89, 3, 86, :_reduce_90, - 1, 108, :_reduce_91, - 2, 108, :_reduce_92, - 1, 109, :_reduce_none, + 1, 109, :_reduce_91, + 2, 109, :_reduce_92, + 1, 110, :_reduce_none, 1, 89, :_reduce_94, 1, 89, :_reduce_95, 1, 58, :_reduce_none, 2, 58, :_reduce_none, - 1, 110, :_reduce_none, - 2, 110, :_reduce_none, - 4, 111, :_reduce_100, - 1, 112, :_reduce_101, - 3, 112, :_reduce_102, - 2, 112, :_reduce_none, - 0, 113, :_reduce_104, - 1, 113, :_reduce_105, - 3, 113, :_reduce_106, - 4, 113, :_reduce_107, - 6, 113, :_reduce_108, + 1, 111, :_reduce_none, + 2, 111, :_reduce_none, + 4, 112, :_reduce_100, + 1, 113, :_reduce_101, + 3, 113, :_reduce_102, + 2, 113, :_reduce_none, + 0, 114, :_reduce_104, + 1, 114, :_reduce_105, + 3, 114, :_reduce_106, + 4, 114, :_reduce_107, + 6, 114, :_reduce_108, 0, 115, :_reduce_109, 0, 116, :_reduce_110, - 8, 113, :_reduce_111, - 3, 113, :_reduce_112, + 8, 114, :_reduce_111, + 3, 114, :_reduce_112, 1, 98, :_reduce_113, 1, 98, :_reduce_114, 1, 98, :_reduce_115, @@ -1041,13 +1041,13 @@ def raise_parse_error(error_message, location) 2, 75, :_reduce_132, 1, 118, :_reduce_none, 1, 118, :_reduce_none, - 1, 103, :_reduce_135, - 0, 114, :_reduce_none, - 1, 114, :_reduce_none ] + 1, 104, :_reduce_135, + 0, 100, :_reduce_none, + 1, 100, :_reduce_none ] racc_reduce_n = 138 -racc_shift_n = 231 +racc_shift_n = 232 racc_token_table = { false => 0, @@ -1227,6 +1227,7 @@ def raise_parse_error(error_message, location) "named_ref_opt", "parameterizing_suffix", "parameterizing_args", + "tag_opt", "@15", "@16", "symbol_declaration_list", @@ -1241,7 +1242,6 @@ def raise_parse_error(error_message, location) "rules", "rhs_list", "rhs", - "tag_opt", "@21", "@22", "@23", @@ -1757,7 +1757,7 @@ def _reduce_65(val, _values, result) module_eval(<<'.,.,', 'parser.y', 286) def _reduce_66(val, _values, result) builder = val[0] - builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3]) + builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5]) result = builder result diff --git a/parser.y b/parser.y index f82c4fb7..b13c819f 100644 --- a/parser.y +++ b/parser.y @@ -282,10 +282,10 @@ rule builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]]) result = builder } - | rule_rhs IDENTIFIER "(" parameterizing_args ")" + | rule_rhs IDENTIFIER "(" parameterizing_args ")" tag_opt { builder = val[0] - builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3]) + builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5]) result = builder } | rule_rhs "{" diff --git a/spec/fixtures/parameterizing_rules/user_defined/nested_rules_with_tag.y b/spec/fixtures/parameterizing_rules/user_defined/nested_rules_with_tag.y new file mode 100644 index 00000000..5d640014 --- /dev/null +++ b/spec/fixtures/parameterizing_rules/user_defined/nested_rules_with_tag.y @@ -0,0 +1,48 @@ +/* + * This is comment for this file. + */ + +%{ +// Prologue +static int yylex(YYSTYPE *val, YYLTYPE *loc); +static int yyerror(YYLTYPE *loc, const char *str); +%} + +%union { + int i; +} + +%token number + +%rule nested_nested_option(X): /* empty */ + | X + ; + +%rule nested_option(X): /* empty */ + | nested_nested_option(X) + ; + +%rule option(Y): /* empty */ + | nested_option(Y) + ; + +%% + +program : option(number) + ; + +%% + +static int yylex(YYSTYPE *yylval, YYLTYPE *loc) +{ + return 0; +} + +static int yyerror(YYLTYPE *loc, const char *str) +{ + return 0; +} + +int main(int argc, char *argv[]) +{ +} diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index bce8e3e3..2ffbde8b 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -1714,6 +1714,112 @@ end end + context "when nested rules with tag" do + let(:path) { "parameterizing_rules/user_defined/nested_rules_with_tag.y" } + + it "expands parameterizing rules" do + expect(grammar.nterms.sort_by(&:number)).to match_symbols([ + Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 4, tag: nil, term: false, token_id: 0, nullable: false), + Sym.new(id: T::Ident.new(s_value: "option_number"), alias_name: nil, number: 5, tag: T::Tag.new(s_value: ""), term: false, token_id: 1, nullable: true), + Sym.new(id: T::Ident.new(s_value: "nested_option_number"), alias_name: nil, number: 6, tag: T::Tag.new(s_value: ""), term: false, token_id: 2, nullable: true), + Sym.new(id: T::Ident.new(s_value: "nested_nested_option_number"), alias_name: nil, number: 7, tag: T::Tag.new(s_value: ""), term: false, token_id: 3, nullable: true), + Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 8, tag: nil, term: false, token_id: 4, nullable: true), + ]) + + expect(grammar.rules).to eq([ + Rule.new( + id: 0, + lhs: grammar.find_symbol_by_s_value!("$accept"), + rhs: [ + grammar.find_symbol_by_s_value!("program"), + grammar.find_symbol_by_s_value!("YYEOF"), + ], + token_code: nil, + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"), + lineno: 31, + ), + Rule.new( + id: 1, + lhs: grammar.find_symbol_by_s_value!("option_number"), + rhs: [], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 31, + ), + Rule.new( + id: 2, + lhs: grammar.find_symbol_by_s_value!("nested_option_number"), + rhs: [], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 31, + ), + Rule.new( + id: 3, + lhs: grammar.find_symbol_by_s_value!("nested_nested_option_number"), + rhs: [], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 31, + ), + Rule.new( + id: 4, + lhs: grammar.find_symbol_by_s_value!("nested_nested_option_number"), + rhs: [ + grammar.find_symbol_by_s_value!("number") + ], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("number"), + lineno: 31, + ), + Rule.new( + id: 5, + lhs: grammar.find_symbol_by_s_value!("nested_option_number"), + rhs: [ + grammar.find_symbol_by_s_value!("nested_nested_option_number") + ], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 31, + ), + Rule.new( + id: 6, + lhs: grammar.find_symbol_by_s_value!("option_number"), + rhs: [ + grammar.find_symbol_by_s_value!("nested_option_number") + ], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 31, + ), + Rule.new( + id: 7, + lhs: grammar.find_symbol_by_s_value!("program"), + rhs: [ + grammar.find_symbol_by_s_value!("option_number") + ], + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 31, + ), + ]) + end + end + context "when nested rules with symbols and parameterizing rules suffix" do let(:path) { "parameterizing_rules/user_defined/nested_rules_symbols.y" }