From e9d6d433cda427dbfcae3b65a53dd7c681e06304 Mon Sep 17 00:00:00 2001 From: ydah Date: Thu, 18 Apr 2024 23:33:06 +0900 Subject: [PATCH] Support inline with parameterizing rule --- lib/lrama/grammar/rule_builder.rb | 10 +- lib/lrama/parser.rb | 660 ++++++++++--------- parser.y | 5 + sig/lrama/grammar/rule_builder.rbs | 2 +- spec/fixtures/inlining/with_parameterizing.y | 42 ++ spec/lrama/parser_spec.rb | 63 ++ 6 files changed, 455 insertions(+), 327 deletions(-) create mode 100644 spec/fixtures/inlining/with_parameterizing.y diff --git a/lib/lrama/grammar/rule_builder.rb b/lib/lrama/grammar/rule_builder.rb index c16db450..66380d88 100644 --- a/lib/lrama/grammar/rule_builder.rb +++ b/lib/lrama/grammar/rule_builder.rb @@ -74,7 +74,11 @@ def resolve_inline_rules if inline_rule = @parameterizing_rule_resolver.find_inline(token) inline_rule.rhs_list.each do |inline_rhs| rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, @parameterizing_rule_resolver, lhs_tag: lhs_tag) - resolve_inline_rhs(rule_builder, inline_rhs, i) + if token.is_a?(Lexer::Token::InstantiateRule) + resolve_inline_rhs(rule_builder, inline_rhs, i, Binding.new(inline_rule, token.args)) + else + resolve_inline_rhs(rule_builder, inline_rhs, i) + end rule_builder.lhs = lhs rule_builder.line = line rule_builder.precedence_sym = precedence_sym @@ -184,10 +188,10 @@ def lhs_s_value(token, bindings) "#{token.rule_name}_#{s_values.join('_')}" end - def resolve_inline_rhs(rule_builder, inline_rhs, index) + def resolve_inline_rhs(rule_builder, inline_rhs, index, bindings = nil) rhs.each_with_index do |token, i| if index == i - inline_rhs.symbols.each { |sym| rule_builder.add_rhs(sym) } + inline_rhs.symbols.each { |sym| rule_builder.add_rhs(bindings.nil? ? sym : bindings.resolve_symbol(sym)) } else rule_builder.add_rhs(token) end diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 7c190716..1a6dc376 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -658,7 +658,7 @@ def token_to_str(t) module Lrama class Parser < Racc::Parser -module_eval(<<'...end parser.y/module_eval...', 'parser.y', 536) +module_eval(<<'...end parser.y/module_eval...', 'parser.y', 541) include Lrama::Report::Duration @@ -732,178 +732,182 @@ def raise_parse_error(error_message, location) ##### State transition tables begin ### racc_action_table = [ - 98, 51, 99, 163, 88, 79, 51, 51, 179, 163, - 79, 79, 51, 162, 179, 156, 79, 165, 157, 3, - 8, 203, 180, 165, 186, 187, 188, 96, 180, 186, - 187, 188, 41, 48, 51, 161, 50, 47, 51, 70, - 50, 54, 89, 82, 51, 166, 50, 54, 100, 82, - 181, 166, 216, 54, 57, 217, 181, 23, 25, 26, + 98, 51, 99, 166, 88, 79, 51, 51, 182, 166, + 79, 79, 51, 165, 182, 158, 79, 168, 159, 47, + 122, 207, 183, 168, 185, 3, 8, 159, 183, 190, + 191, 192, 41, 48, 51, 164, 50, 47, 51, 70, + 50, 54, 89, 82, 51, 169, 50, 54, 100, 82, + 184, 169, 190, 191, 192, 96, 184, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 47, 6, 51, 7, 50, 93, 79, 75, - 51, 51, 50, 50, 58, 82, 82, 51, 51, 50, + 51, 51, 50, 50, 54, 82, 82, 51, 51, 50, 50, 79, 23, 25, 26, 27, 28, 29, 30, 31, - 32, 33, 34, 35, 36, 37, 38, 9, 51, 59, - 50, 14, 15, 16, 17, 18, 19, 60, 61, 20, + 32, 33, 34, 35, 36, 37, 38, 9, 51, 57, + 50, 14, 15, 16, 17, 18, 19, 58, 59, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 51, 51, - 50, 50, 79, 196, 51, 51, 50, 50, 79, 196, - 51, 51, 50, 50, 79, 196, 51, 51, 50, 50, - 79, 196, 51, 51, 50, 50, 79, 196, 51, 51, - 50, 50, 79, 196, 51, 51, 50, 50, 79, 79, + 50, 50, 79, 200, 51, 51, 50, 50, 79, 200, + 51, 51, 50, 50, 79, 200, 51, 51, 50, 50, + 79, 200, 51, 51, 50, 50, 79, 200, 51, 51, + 50, 50, 79, 200, 51, 51, 50, 50, 79, 79, 51, 51, 50, 50, 79, 79, 51, 51, 50, 50, - 79, 79, 51, 51, 206, 206, 79, 79, 51, 51, - 206, 50, 79, 79, 220, 227, 62, 217, 217, 51, - 51, 50, 50, 51, 63, 50, 186, 187, 188, 64, - 65, 66, 67, 90, 94, 96, 101, 101, 101, 103, - 109, 113, 114, 117, 117, 117, 117, 120, 47, 124, - 125, 127, 129, 130, 131, 132, 133, 136, 140, 141, - 142, 143, 146, 147, 148, 150, 160, 168, 170, 171, - 172, 173, 174, 175, 176, 177, 146, 183, 191, 192, - 160, 160, 96, 210, 211, 177, 214, 215, 210, 225, - 226, 96, 96, 210 ] + 79, 79, 51, 51, 211, 211, 79, 79, 51, 51, + 211, 50, 79, 79, 222, 226, 233, 223, 223, 223, + 51, 51, 50, 50, 51, 60, 50, 190, 191, 192, + 61, 62, 63, 64, 65, 66, 67, 90, 94, 96, + 101, 101, 101, 103, 109, 113, 114, 117, 117, 117, + 117, 120, 125, 126, 128, 130, 131, 132, 133, 134, + 137, 141, 142, 143, 144, 145, 148, 149, 150, 152, + 162, 141, 171, 173, 174, 175, 176, 177, 178, 179, + 180, 148, 187, 195, 196, 162, 162, 96, 210, 215, + 216, 180, 219, 162, 221, 180, 215, 231, 232, 96, + 96, 215 ] racc_action_check = [ - 49, 145, 49, 145, 39, 145, 159, 182, 159, 182, - 159, 182, 200, 144, 200, 139, 200, 145, 139, 1, - 3, 179, 159, 182, 164, 164, 164, 164, 200, 179, - 179, 179, 7, 14, 33, 144, 33, 9, 35, 33, - 35, 16, 39, 35, 36, 145, 36, 17, 49, 36, - 159, 182, 207, 18, 19, 207, 200, 9, 9, 9, + 49, 147, 49, 147, 39, 147, 161, 186, 161, 186, + 161, 186, 204, 146, 204, 140, 204, 147, 140, 89, + 89, 182, 161, 186, 163, 1, 3, 163, 204, 182, + 182, 182, 7, 14, 33, 146, 33, 9, 35, 33, + 35, 16, 39, 35, 36, 147, 36, 17, 49, 36, + 161, 186, 167, 167, 167, 167, 204, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 42, 2, 34, 2, 34, 42, 34, 34, - 37, 38, 37, 38, 20, 37, 38, 74, 15, 74, + 37, 38, 37, 38, 18, 37, 38, 74, 15, 74, 15, 74, 42, 42, 42, 42, 42, 42, 42, 42, - 42, 42, 42, 42, 42, 42, 42, 4, 69, 23, - 69, 4, 4, 4, 4, 4, 4, 25, 26, 4, + 42, 42, 42, 42, 42, 42, 42, 4, 69, 19, + 69, 4, 4, 4, 4, 4, 4, 20, 23, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 172, 70, - 172, 70, 172, 172, 173, 81, 173, 81, 173, 173, - 174, 82, 174, 82, 174, 174, 193, 109, 193, 109, - 193, 193, 197, 111, 197, 111, 197, 197, 198, 117, - 198, 117, 198, 198, 75, 114, 75, 114, 75, 114, - 116, 137, 116, 137, 116, 137, 166, 181, 166, 181, - 166, 181, 183, 203, 183, 203, 183, 203, 215, 217, - 215, 217, 215, 217, 213, 222, 27, 213, 222, 119, - 134, 119, 134, 138, 28, 138, 208, 208, 208, 29, - 30, 31, 32, 40, 45, 46, 53, 55, 56, 57, - 68, 72, 73, 80, 85, 86, 87, 88, 89, 95, - 96, 102, 104, 105, 106, 107, 108, 112, 120, 121, - 122, 123, 124, 125, 126, 128, 141, 149, 151, 152, - 153, 154, 155, 156, 157, 158, 161, 163, 167, 169, - 175, 177, 178, 185, 189, 199, 204, 206, 216, 219, - 221, 225, 226, 228 ] + 4, 4, 4, 4, 4, 4, 4, 4, 175, 70, + 175, 70, 175, 175, 176, 81, 176, 81, 176, 176, + 177, 82, 177, 82, 177, 177, 197, 109, 197, 109, + 197, 197, 201, 111, 201, 111, 201, 201, 202, 117, + 202, 117, 202, 202, 75, 114, 75, 114, 75, 114, + 116, 138, 116, 138, 116, 138, 169, 184, 169, 184, + 169, 184, 187, 207, 187, 207, 187, 207, 221, 223, + 221, 223, 221, 223, 212, 218, 228, 212, 218, 228, + 119, 135, 119, 135, 139, 25, 139, 213, 213, 213, + 26, 27, 28, 29, 30, 31, 32, 40, 45, 46, + 53, 55, 56, 57, 68, 72, 73, 80, 85, 86, + 87, 88, 95, 96, 102, 104, 105, 106, 107, 108, + 112, 120, 121, 122, 123, 124, 125, 126, 127, 129, + 142, 143, 151, 153, 154, 155, 156, 157, 158, 159, + 160, 164, 166, 170, 172, 178, 180, 181, 185, 189, + 193, 203, 208, 210, 211, 220, 222, 225, 227, 231, + 232, 234 ] racc_action_pointer = [ - nil, 19, 63, 20, 98, nil, nil, 25, nil, 33, - nil, nil, nil, nil, 27, 85, 22, 28, 34, 49, - 65, nil, nil, 90, nil, 98, 99, 187, 209, 214, - 215, 216, 217, 31, 71, 35, 41, 77, 78, -1, - 221, nil, 68, nil, nil, 201, 174, nil, nil, -5, - nil, nil, nil, 207, nil, 208, 209, 210, nil, nil, - nil, nil, nil, nil, nil, nil, nil, nil, 222, 105, - 136, nil, 225, 224, 84, 171, nil, nil, nil, nil, - 225, 142, 148, nil, nil, 226, 227, 228, 197, 234, - nil, nil, nil, nil, nil, 197, 235, nil, nil, nil, - nil, nil, 239, nil, 240, 241, 242, 243, 244, 154, - nil, 160, 240, nil, 172, nil, 177, 166, nil, 206, - 243, 207, 239, 249, 206, 201, 252, nil, 253, nil, - nil, nil, nil, nil, 207, nil, nil, 178, 210, -26, - nil, 210, nil, nil, -10, -2, nil, nil, nil, 237, - nil, 238, 239, 240, 241, 242, 221, 259, 220, 3, - nil, 220, nil, 227, -24, nil, 183, 248, nil, 249, - nil, nil, 135, 141, 147, 224, nil, 225, 221, -19, - nil, 184, 4, 189, nil, 265, nil, nil, nil, 272, - nil, nil, nil, 153, nil, nil, nil, 159, 165, 230, - 9, nil, nil, 190, 274, nil, 237, 11, 168, nil, - nil, nil, nil, 163, nil, 195, 270, 196, nil, 259, - nil, 260, 164, nil, nil, 230, 231, nil, 275, nil, - nil ] + nil, 25, 63, 26, 98, nil, nil, 25, nil, 33, + nil, nil, nil, nil, 27, 85, 22, 28, 65, 104, + 98, nil, nil, 99, nil, 196, 201, 202, 217, 218, + 219, 220, 221, 31, 71, 35, 41, 77, 78, -1, + 225, nil, 68, nil, nil, 205, 178, nil, nil, -5, + nil, nil, nil, 211, nil, 212, 213, 214, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, 226, 105, + 136, nil, 229, 228, 84, 171, nil, nil, nil, nil, + 229, 142, 148, nil, nil, 230, 231, 232, 201, 15, + nil, nil, nil, nil, nil, 200, 238, nil, nil, nil, + nil, nil, 242, nil, 243, 244, 245, 246, 247, 154, + nil, 160, 243, nil, 172, nil, 177, 166, nil, 207, + 246, 210, 213, 243, 253, 210, 205, 256, nil, 257, + nil, nil, nil, nil, nil, 208, nil, nil, 178, 211, + -26, nil, 214, 256, nil, nil, -10, -2, nil, nil, + nil, 242, nil, 243, 244, 245, 246, 247, 226, 264, + 225, 3, nil, -17, 225, nil, 232, 4, nil, 183, + 253, nil, 254, nil, nil, 135, 141, 147, 229, nil, + 230, 226, -19, nil, 184, 236, 4, 189, nil, 271, + nil, nil, nil, 278, nil, nil, nil, 153, nil, nil, + nil, 159, 165, 236, 9, nil, nil, 190, 280, nil, + 237, 244, 163, 169, nil, nil, nil, nil, 164, nil, + 240, 195, 278, 196, nil, 267, nil, 268, 165, nil, + nil, 238, 239, nil, 283, nil, nil ] racc_action_default = [ - -2, -138, -8, -138, -138, -3, -4, -138, 231, -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, - -138, -7, -123, -96, -98, -138, -120, -122, -13, -127, - -94, -95, -126, -15, -85, -16, -17, -138, -21, -26, + -2, -139, -8, -139, -139, -3, -4, -139, 237, -139, + -9, -10, -11, -12, -139, -139, -139, -139, -139, -139, + -139, -24, -25, -139, -29, -139, -139, -139, -139, -139, + -139, -139, -139, -139, -139, -139, -139, -139, -139, -139, + -139, -7, -124, -97, -99, -139, -121, -123, -13, -128, + -95, -96, -127, -15, -86, -16, -17, -139, -21, -26, -30, -33, -36, -39, -40, -41, -42, -43, -44, -50, - -138, -53, -71, -45, -75, -138, -78, -80, -81, -135, - -46, -88, -138, -91, -93, -47, -48, -49, -138, -138, - -5, -1, -97, -124, -99, -138, -138, -14, -128, -129, - -130, -82, -138, -18, -138, -138, -138, -138, -138, -138, - -54, -51, -73, -72, -138, -79, -76, -138, -92, -89, - -138, -138, -138, -138, -104, -138, -138, -86, -138, -22, - -27, -31, -34, -37, -52, -55, -74, -77, -90, -138, - -58, -62, -6, -125, -100, -101, -105, -121, -83, -138, - -19, -138, -138, -138, -138, -138, -138, -138, -57, -60, - -63, -104, -103, -94, -120, -109, -138, -138, -87, -138, - -23, -28, -138, -138, -138, -62, -59, -62, -120, -94, - -67, -138, -102, -138, -106, -136, -113, -114, -115, -138, - -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 ] + -139, -53, -72, -45, -76, -139, -79, -81, -82, -136, + -46, -89, -139, -92, -94, -47, -48, -49, -139, -139, + -5, -1, -98, -125, -100, -139, -139, -14, -129, -130, + -131, -83, -139, -18, -139, -139, -139, -139, -139, -139, + -54, -51, -74, -73, -139, -80, -77, -139, -93, -90, + -139, -139, -139, -139, -139, -105, -139, -139, -87, -139, + -22, -27, -31, -34, -37, -52, -55, -75, -78, -91, + -139, -59, -63, -139, -6, -126, -101, -102, -106, -122, + -84, -139, -19, -139, -139, -139, -139, -139, -139, -139, + -57, -61, -64, -139, -105, -104, -95, -121, -110, -139, + -139, -88, -139, -23, -28, -139, -139, -139, -63, -60, + -63, -121, -95, -68, -139, -139, -103, -139, -107, -137, + -114, -115, -116, -139, -113, -85, -20, -32, -132, -134, + -135, -35, -38, -56, -62, -65, -66, -139, -139, -71, + -63, -95, -139, -117, -108, -138, -111, -133, -139, -69, + -58, -139, -137, -139, -119, -139, -67, -139, -139, -109, + -118, -121, -121, -120, -137, -70, -112 ] racc_goto_table = [ - 76, 74, 69, 52, 209, 95, 110, 118, 119, 145, - 158, 1, 185, 2, 4, 212, 43, 42, 207, 212, - 212, 72, 91, 84, 84, 84, 84, 202, 80, 85, - 86, 87, 53, 55, 56, 223, 5, 40, 213, 111, - 115, 76, 116, 138, 199, 118, 182, 230, 110, 92, - 222, 193, 197, 198, 122, 10, 218, 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, - nil, nil, nil, nil, nil, nil, nil, 205, 164, 208, + 76, 52, 69, 74, 110, 95, 160, 118, 119, 147, + 140, 189, 1, 2, 214, 212, 4, 43, 42, 72, + 91, 84, 84, 84, 84, 217, 206, 5, 40, 217, + 217, 123, 10, 163, 11, 218, 53, 55, 56, 111, + 115, 76, 203, 139, 116, 118, 110, 229, 186, 228, + 92, 80, 85, 86, 87, 72, 72, 224, 12, 236, + 197, 201, 202, 13, 49, 118, 97, 84, 84, 129, + 110, 172, 104, 153, 220, 105, 154, 106, 135, 155, + 76, 107, 115, 138, 156, 108, 157, 68, 73, 112, + 136, 121, 204, 208, 227, 72, 127, 72, 170, 102, + 151, 146, 193, 84, 115, 84, 225, 124, nil, nil, + nil, nil, nil, 167, nil, nil, nil, nil, nil, nil, + nil, 72, nil, nil, nil, 84, 188, 181, nil, nil, + nil, nil, nil, nil, nil, 194, nil, nil, nil, nil, + 205, nil, nil, nil, nil, nil, nil, nil, nil, nil, + 209, nil, 167, 213, nil, nil, nil, nil, nil, nil, 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 ] + 181, nil, nil, 213, nil, nil, nil, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, 213, nil, 230, + 234, 235 ] racc_goto_check = [ - 42, 48, 33, 35, 60, 43, 34, 55, 54, 59, - 39, 1, 44, 2, 3, 64, 56, 4, 45, 64, - 64, 35, 5, 35, 35, 35, 35, 44, 32, 32, - 32, 32, 15, 15, 15, 60, 6, 7, 45, 33, - 42, 42, 48, 54, 39, 55, 59, 60, 34, 56, - 45, 21, 21, 21, 8, 9, 44, 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, - 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, + 42, 35, 33, 48, 34, 43, 39, 55, 54, 59, + 38, 44, 1, 2, 60, 45, 3, 56, 4, 35, + 5, 35, 35, 35, 35, 64, 44, 6, 7, 64, + 64, 8, 9, 38, 10, 45, 15, 15, 15, 33, + 42, 42, 39, 54, 48, 55, 34, 60, 59, 45, + 56, 32, 32, 32, 32, 35, 35, 44, 11, 60, + 21, 21, 21, 12, 13, 55, 14, 35, 35, 16, + 34, 17, 18, 19, 39, 22, 23, 24, 33, 25, + 42, 26, 42, 48, 27, 28, 29, 30, 31, 36, + 37, 40, 41, 46, 47, 35, 50, 35, 51, 52, + 53, 58, 61, 35, 42, 35, 62, 63, nil, nil, + nil, nil, nil, 42, nil, nil, nil, nil, nil, nil, + nil, 35, nil, nil, nil, 35, 43, 42, nil, nil, + nil, nil, nil, nil, nil, 42, nil, nil, nil, nil, + 43, nil, nil, nil, nil, nil, nil, nil, nil, nil, + 42, nil, 42, 42, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - nil, 42, nil, 42, 43, 43 ] + 42, nil, nil, 42, nil, nil, nil, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, 42, nil, 42, + 43, 43 ] racc_goto_pointer = [ - nil, 11, 13, 12, 8, -20, 34, 31, -36, 51, - 55, 56, 57, 47, 14, 16, -38, -84, 9, -61, - nil, -121, 13, -57, 14, -56, 15, -55, 17, -50, - 51, 51, -7, -31, -63, -12, 14, -25, -32, -131, - 0, -87, -34, -41, -152, -165, -89, -122, -33, nil, - -8, -54, 41, -31, -74, -74, 7, nil, -26, -115, - -181, -65, -110, 9, -178 ] + nil, 12, 13, 14, 9, -22, 25, 22, -59, 28, + 30, 54, 59, 49, 17, 20, -34, -81, 14, -57, + nil, -115, 16, -55, 17, -53, 20, -49, 23, -48, + 54, 54, 16, -31, -65, -14, 17, -22, -110, -136, + 2, -88, -34, -41, -156, -172, -90, -125, -31, nil, + -5, -52, 45, -28, -74, -74, 8, nil, -24, -116, + -175, -66, -110, 14, -172 ] 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, + 46, 161, 199, nil, nil, nil, nil, nil, nil, 78, nil, nil, nil, nil, 81, 83, nil, 44, nil, nil, - nil, nil, nil, nil, 194 ] + nil, nil, nil, nil, 198 ] racc_reduce_table = [ 0, 0, :racc_error, @@ -964,90 +968,91 @@ def raise_parse_error(error_message, location) 3, 88, :_reduce_55, 7, 65, :_reduce_56, 5, 66, :_reduce_57, - 1, 92, :_reduce_58, - 3, 92, :_reduce_59, - 1, 93, :_reduce_60, - 3, 93, :_reduce_61, - 0, 95, :_reduce_62, - 1, 95, :_reduce_63, - 3, 95, :_reduce_64, + 8, 66, :_reduce_58, + 1, 92, :_reduce_59, + 3, 92, :_reduce_60, + 1, 93, :_reduce_61, + 3, 93, :_reduce_62, + 0, 95, :_reduce_63, + 1, 95, :_reduce_64, 3, 95, :_reduce_65, - 5, 95, :_reduce_66, - 0, 100, :_reduce_67, - 0, 101, :_reduce_68, - 7, 95, :_reduce_69, - 3, 95, :_reduce_70, + 3, 95, :_reduce_66, + 5, 95, :_reduce_67, + 0, 100, :_reduce_68, + 0, 101, :_reduce_69, + 7, 95, :_reduce_70, + 3, 95, :_reduce_71, 0, 90, :_reduce_none, 1, 90, :_reduce_none, 0, 91, :_reduce_none, 1, 91, :_reduce_none, - 1, 85, :_reduce_75, - 2, 85, :_reduce_76, - 3, 85, :_reduce_77, - 1, 102, :_reduce_78, - 2, 102, :_reduce_79, + 1, 85, :_reduce_76, + 2, 85, :_reduce_77, + 3, 85, :_reduce_78, + 1, 102, :_reduce_79, + 2, 102, :_reduce_80, 1, 96, :_reduce_none, 1, 96, :_reduce_none, - 0, 104, :_reduce_82, - 0, 105, :_reduce_83, - 6, 69, :_reduce_84, - 0, 106, :_reduce_85, - 0, 107, :_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, + 0, 104, :_reduce_83, + 0, 105, :_reduce_84, + 6, 69, :_reduce_85, + 0, 106, :_reduce_86, + 0, 107, :_reduce_87, + 5, 69, :_reduce_88, + 1, 86, :_reduce_89, + 2, 86, :_reduce_90, + 3, 86, :_reduce_91, + 1, 108, :_reduce_92, + 2, 108, :_reduce_93, 1, 109, :_reduce_none, - 1, 89, :_reduce_94, 1, 89, :_reduce_95, + 1, 89, :_reduce_96, 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, + 4, 111, :_reduce_101, + 1, 112, :_reduce_102, + 3, 112, :_reduce_103, 2, 112, :_reduce_none, - 0, 113, :_reduce_104, - 1, 113, :_reduce_105, - 3, 113, :_reduce_106, - 4, 113, :_reduce_107, - 6, 113, :_reduce_108, - 0, 115, :_reduce_109, - 0, 116, :_reduce_110, - 8, 113, :_reduce_111, - 3, 113, :_reduce_112, - 1, 98, :_reduce_113, + 0, 113, :_reduce_105, + 1, 113, :_reduce_106, + 3, 113, :_reduce_107, + 4, 113, :_reduce_108, + 6, 113, :_reduce_109, + 0, 115, :_reduce_110, + 0, 116, :_reduce_111, + 8, 113, :_reduce_112, + 3, 113, :_reduce_113, 1, 98, :_reduce_114, 1, 98, :_reduce_115, - 1, 99, :_reduce_116, - 3, 99, :_reduce_117, - 2, 99, :_reduce_118, - 4, 99, :_reduce_119, + 1, 98, :_reduce_116, + 1, 99, :_reduce_117, + 3, 99, :_reduce_118, + 2, 99, :_reduce_119, + 4, 99, :_reduce_120, 0, 97, :_reduce_none, - 3, 97, :_reduce_121, + 3, 97, :_reduce_122, 1, 94, :_reduce_none, 0, 59, :_reduce_none, - 0, 117, :_reduce_124, - 3, 59, :_reduce_125, + 0, 117, :_reduce_125, + 3, 59, :_reduce_126, 1, 67, :_reduce_none, 0, 68, :_reduce_none, 1, 68, :_reduce_none, 1, 68, :_reduce_none, 1, 68, :_reduce_none, - 1, 75, :_reduce_131, - 2, 75, :_reduce_132, + 1, 75, :_reduce_132, + 2, 75, :_reduce_133, 1, 118, :_reduce_none, 1, 118, :_reduce_none, - 1, 103, :_reduce_135, + 1, 103, :_reduce_136, 0, 114, :_reduce_none, 1, 114, :_reduce_none ] -racc_reduce_n = 138 +racc_reduce_n = 139 -racc_shift_n = 231 +racc_shift_n = 237 racc_token_table = { false => 0, @@ -1682,42 +1687,42 @@ def _reduce_57(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 246) +module_eval(<<'.,.,', 'parser.y', 247) def _reduce_58(val, _values, result) - result = [val[0]] + rule = Grammar::ParameterizingRule::Rule.new(val[2].s_value, val[4], val[7], is_inline: true) + @grammar.add_parameterizing_rule(rule) + result end .,., -module_eval(<<'.,.,', 'parser.y', 247) +module_eval(<<'.,.,', 'parser.y', 251) def _reduce_59(val, _values, result) - result = val[0].append(val[2]) + result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 251) +module_eval(<<'.,.,', 'parser.y', 252) def _reduce_60(val, _values, result) - builder = val[0] - result = [builder] - + result = val[0].append(val[2]) result end .,., module_eval(<<'.,.,', 'parser.y', 256) def _reduce_61(val, _values, result) - builder = val[2] - result = val[0].append(builder) + builder = val[0] + result = [builder] result end .,., -module_eval(<<'.,.,', 'parser.y', 262) +module_eval(<<'.,.,', 'parser.y', 261) def _reduce_62(val, _values, result) - reset_precs - result = Grammar::ParameterizingRule::Rhs.new + builder = val[2] + result = val[0].append(builder) result end @@ -1734,6 +1739,15 @@ def _reduce_63(val, _values, result) module_eval(<<'.,.,', 'parser.y', 272) def _reduce_64(val, _values, result) + reset_precs + result = Grammar::ParameterizingRule::Rhs.new + + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 277) + def _reduce_65(val, _values, result) token = val[1] token.alias_name = val[2] builder = val[0] @@ -1744,8 +1758,8 @@ def _reduce_64(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 280) - def _reduce_65(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 285) + def _reduce_66(val, _values, result) builder = val[0] builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]]) result = builder @@ -1754,8 +1768,8 @@ def _reduce_65(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 286) - def _reduce_66(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 291) + def _reduce_67(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]) result = builder @@ -1764,8 +1778,8 @@ def _reduce_66(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 292) - def _reduce_67(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 297) + def _reduce_68(val, _values, result) if @prec_seen on_action_error("multiple User_code after %prec", val[0]) if @code_after_prec @code_after_prec = true @@ -1776,16 +1790,16 @@ def _reduce_67(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 300) - def _reduce_68(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 305) + def _reduce_69(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 304) - def _reduce_69(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 309) + def _reduce_70(val, _values, result) user_code = val[3] user_code.alias_name = val[6] builder = val[0] @@ -1796,8 +1810,8 @@ def _reduce_69(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 312) - def _reduce_70(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 317) + def _reduce_71(val, _values, result) sym = @grammar.find_symbol_by_id!(val[2]) @prec_seen = true builder = val[0] @@ -1808,168 +1822,168 @@ def _reduce_70(val, _values, result) end .,., -# reduce 71 omitted - # reduce 72 omitted # reduce 73 omitted # reduce 74 omitted -module_eval(<<'.,.,', 'parser.y', 327) - def _reduce_75(val, _values, result) +# reduce 75 omitted + +module_eval(<<'.,.,', 'parser.y', 332) + def _reduce_76(val, _values, result) result = [{tag: nil, tokens: val[0]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 331) - def _reduce_76(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 336) + def _reduce_77(val, _values, result) result = [{tag: val[0], tokens: val[1]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 335) - def _reduce_77(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 340) + def _reduce_78(val, _values, result) result = val[0].append({tag: val[1], tokens: val[2]}) result end .,., -module_eval(<<'.,.,', 'parser.y', 338) - def _reduce_78(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 343) + def _reduce_79(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 339) - def _reduce_79(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 344) + def _reduce_80(val, _values, result) result = val[0].append(val[1]) result end .,., -# reduce 80 omitted - # reduce 81 omitted -module_eval(<<'.,.,', 'parser.y', 346) - def _reduce_82(val, _values, result) +# reduce 82 omitted + +module_eval(<<'.,.,', 'parser.y', 351) + def _reduce_83(val, _values, result) begin_c_declaration("}") result end .,., -module_eval(<<'.,.,', 'parser.y', 350) - def _reduce_83(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 355) + def _reduce_84(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 354) - def _reduce_84(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 359) + def _reduce_85(val, _values, result) result = val[0].append(val[3]) result end .,., -module_eval(<<'.,.,', 'parser.y', 358) - def _reduce_85(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 363) + def _reduce_86(val, _values, result) begin_c_declaration("}") result end .,., -module_eval(<<'.,.,', 'parser.y', 362) - def _reduce_86(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 367) + def _reduce_87(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 366) - def _reduce_87(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 371) + def _reduce_88(val, _values, result) result = [val[2]] result end .,., -module_eval(<<'.,.,', 'parser.y', 371) - def _reduce_88(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 376) + def _reduce_89(val, _values, result) result = [{tag: nil, tokens: val[0]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 375) - def _reduce_89(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 380) + def _reduce_90(val, _values, result) result = [{tag: val[0], tokens: val[1]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 379) - def _reduce_90(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 384) + def _reduce_91(val, _values, result) result = val[0].append({tag: val[1], tokens: val[2]}) result end .,., -module_eval(<<'.,.,', 'parser.y', 382) - def _reduce_91(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 387) + def _reduce_92(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 383) - def _reduce_92(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 388) + def _reduce_93(val, _values, result) result = val[0].append(val[1]) result end .,., -# reduce 93 omitted +# reduce 94 omitted -module_eval(<<'.,.,', 'parser.y', 387) - def _reduce_94(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 392) + def _reduce_95(val, _values, result) on_action_error("ident after %prec", val[0]) if @prec_seen result end .,., -module_eval(<<'.,.,', 'parser.y', 388) - def _reduce_95(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 393) + def _reduce_96(val, _values, result) on_action_error("char after %prec", val[0]) if @prec_seen result end .,., -# reduce 96 omitted - # reduce 97 omitted # reduce 98 omitted # reduce 99 omitted -module_eval(<<'.,.,', 'parser.y', 398) - def _reduce_100(val, _values, result) +# reduce 100 omitted + +module_eval(<<'.,.,', 'parser.y', 403) + def _reduce_101(val, _values, result) lhs = val[0] lhs.alias_name = val[1] val[3].each do |builder| @@ -1982,8 +1996,8 @@ def _reduce_100(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 409) - def _reduce_101(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 414) + def _reduce_102(val, _values, result) builder = val[0] if !builder.line builder.line = @lexer.line - 1 @@ -1994,8 +2008,8 @@ def _reduce_101(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 417) - def _reduce_102(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 422) + def _reduce_103(val, _values, result) builder = val[2] if !builder.line builder.line = @lexer.line - 1 @@ -2006,10 +2020,10 @@ def _reduce_102(val, _values, result) end .,., -# reduce 103 omitted +# reduce 104 omitted -module_eval(<<'.,.,', 'parser.y', 427) - def _reduce_104(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 432) + def _reduce_105(val, _values, result) reset_precs result = @grammar.create_rule_builder(@rule_counter, @midrule_action_counter) @@ -2017,8 +2031,8 @@ def _reduce_104(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 432) - def _reduce_105(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 437) + def _reduce_106(val, _values, result) reset_precs result = @grammar.create_rule_builder(@rule_counter, @midrule_action_counter) @@ -2026,8 +2040,8 @@ def _reduce_105(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 437) - def _reduce_106(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 442) + def _reduce_107(val, _values, result) token = val[1] token.alias_name = val[2] builder = val[0] @@ -2038,8 +2052,8 @@ def _reduce_106(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 445) - def _reduce_107(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 450) + def _reduce_108(val, _values, result) token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]], lhs_tag: val[3]) builder = val[0] builder.add_rhs(token) @@ -2050,8 +2064,8 @@ def _reduce_107(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 453) - def _reduce_108(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 458) + def _reduce_109(val, _values, result) token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5]) builder = val[0] builder.add_rhs(token) @@ -2062,8 +2076,8 @@ def _reduce_108(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 461) - def _reduce_109(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 466) + def _reduce_110(val, _values, result) if @prec_seen on_action_error("multiple User_code after %prec", val[0]) if @code_after_prec @code_after_prec = true @@ -2074,16 +2088,16 @@ def _reduce_109(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 469) - def _reduce_110(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 474) + def _reduce_111(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 473) - def _reduce_111(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 478) + def _reduce_112(val, _values, result) user_code = val[3] user_code.alias_name = val[6] user_code.tag = val[7] @@ -2095,8 +2109,8 @@ def _reduce_111(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 482) - def _reduce_112(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 487) + def _reduce_113(val, _values, result) sym = @grammar.find_symbol_by_id!(val[2]) @prec_seen = true builder = val[0] @@ -2107,70 +2121,70 @@ def _reduce_112(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 489) - def _reduce_113(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 494) + def _reduce_114(val, _values, result) result = "option" result end .,., -module_eval(<<'.,.,', 'parser.y', 490) - def _reduce_114(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 495) + def _reduce_115(val, _values, result) result = "nonempty_list" result end .,., -module_eval(<<'.,.,', 'parser.y', 491) - def _reduce_115(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 496) + def _reduce_116(val, _values, result) result = "list" result end .,., -module_eval(<<'.,.,', 'parser.y', 493) - def _reduce_116(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 498) + def _reduce_117(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 494) - def _reduce_117(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 499) + def _reduce_118(val, _values, result) result = val[0].append(val[2]) result end .,., -module_eval(<<'.,.,', 'parser.y', 495) - def _reduce_118(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 500) + def _reduce_119(val, _values, result) result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[0])] result end .,., -module_eval(<<'.,.,', 'parser.y', 496) - def _reduce_119(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 501) + def _reduce_120(val, _values, result) result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] result end .,., -# reduce 120 omitted +# reduce 121 omitted -module_eval(<<'.,.,', 'parser.y', 499) - def _reduce_121(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 504) + def _reduce_122(val, _values, result) result = val[1].s_value result end .,., -# reduce 122 omitted - # reduce 123 omitted -module_eval(<<'.,.,', 'parser.y', 506) - def _reduce_124(val, _values, result) +# reduce 124 omitted + +module_eval(<<'.,.,', 'parser.y', 511) + def _reduce_125(val, _values, result) begin_c_declaration('\Z') @grammar.epilogue_first_lineno = @lexer.line + 1 @@ -2178,8 +2192,8 @@ def _reduce_124(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 511) - def _reduce_125(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 516) + def _reduce_126(val, _values, result) end_c_declaration @grammar.epilogue = val[2].s_value @@ -2187,8 +2201,6 @@ def _reduce_125(val, _values, result) end .,., -# reduce 126 omitted - # reduce 127 omitted # reduce 128 omitted @@ -2197,35 +2209,37 @@ def _reduce_125(val, _values, result) # reduce 130 omitted -module_eval(<<'.,.,', 'parser.y', 522) - def _reduce_131(val, _values, result) +# reduce 131 omitted + +module_eval(<<'.,.,', 'parser.y', 527) + def _reduce_132(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 523) - def _reduce_132(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 528) + def _reduce_133(val, _values, result) result = val[0].append(val[1]) result end .,., -# reduce 133 omitted - # reduce 134 omitted -module_eval(<<'.,.,', 'parser.y', 528) - def _reduce_135(val, _values, result) +# reduce 135 omitted + +module_eval(<<'.,.,', 'parser.y', 533) + def _reduce_136(val, _values, result) result = Lrama::Lexer::Token::Ident.new(s_value: val[0]) result end .,., -# reduce 136 omitted - # reduce 137 omitted +# reduce 138 omitted + def _reduce_none(val, _values, result) val[0] end diff --git a/parser.y b/parser.y index ff8df3d7..547c3ebc 100644 --- a/parser.y +++ b/parser.y @@ -243,6 +243,11 @@ rule rule = Grammar::ParameterizingRule::Rule.new(val[2].s_value, [], val[4], is_inline: true) @grammar.add_parameterizing_rule(rule) } + | "%rule" "%inline" IDENTIFIER "(" rule_args ")" ":" rule_rhs_list + { + rule = Grammar::ParameterizingRule::Rule.new(val[2].s_value, val[4], val[7], is_inline: true) + @grammar.add_parameterizing_rule(rule) + } rule_args: IDENTIFIER { result = [val[0]] } | rule_args "," IDENTIFIER { result = val[0].append(val[2]) } diff --git a/sig/lrama/grammar/rule_builder.rbs b/sig/lrama/grammar/rule_builder.rbs index 5efed93c..2b1ea063 100644 --- a/sig/lrama/grammar/rule_builder.rbs +++ b/sig/lrama/grammar/rule_builder.rbs @@ -37,7 +37,7 @@ module Lrama def process_rhs: () -> void def lhs_s_value: (Lexer::Token::InstantiateRule token, Grammar::Binding bindings) -> String def resolve_inline: () -> void - def resolve_inline_rhs: (RuleBuilder rule_builder, Grammar::ParameterizingRule::Rhs inline_rhs, Integer index) -> void + def resolve_inline_rhs: (RuleBuilder rule_builder, Grammar::ParameterizingRule::Rhs inline_rhs, Integer index, ?Binding bindings) -> void def replace_inline_user_code: (Grammar::ParameterizingRule::Rhs inline_rhs, Integer index) -> Lexer::Token::UserCode? def numberize_references: () -> void def flush_user_code: () -> void diff --git a/spec/fixtures/inlining/with_parameterizing.y b/spec/fixtures/inlining/with_parameterizing.y new file mode 100644 index 00000000..39b743f8 --- /dev/null +++ b/spec/fixtures/inlining/with_parameterizing.y @@ -0,0 +1,42 @@ +/* + * 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 NUM +%type expression + +%rule %inline op(p, m) : p { + } + | m { - } + ; + +%% + +expression : NUM + | expression op('+', '-') expression { $$ = $1 $2 $3; } + ; + +%% + +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 6c3f8ff2..89916861 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -1989,6 +1989,69 @@ end end + context 'when inline with parameterizing rule' do + let(:path) { "inlining/with_parameterizing.y" } + + it "expands inlining rules" do + expect(grammar.nterms.sort_by(&:number)).to match_symbols([ + Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false), + Sym.new(id: T::Ident.new(s_value: "expression"), alias_name: nil, number: 7, tag: T::Tag.new(s_value: ""), term: false, token_id: 1, nullable: false), + ]) + + expect(grammar.rules).to eq([ + Rule.new( + id: 0, + lhs: grammar.find_symbol_by_s_value!("$accept"), + rhs: [ + grammar.find_symbol_by_s_value!("expression"), + grammar.find_symbol_by_s_value!("YYEOF"), + ], + token_code: nil, + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"), + lineno: 24, + ), + Rule.new( + id: 1, + lhs: grammar.find_symbol_by_s_value!("expression"), + rhs: [ + grammar.find_symbol_by_s_value!("NUM"), + ], + token_code: nil, + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("NUM"), + lineno: 24, + ), + Rule.new( + id: 2, + lhs: grammar.find_symbol_by_s_value!("expression"), + rhs: [ + grammar.find_symbol_by_s_value!("expression"), + grammar.find_symbol_by_s_value!("'+'"), + grammar.find_symbol_by_s_value!("expression"), + ], + token_code: T::UserCode.new(s_value: " $$ = $1 + $3; "), + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("'+'"), + lineno: 25, + ), + Rule.new( + id: 3, + lhs: grammar.find_symbol_by_s_value!("expression"), + rhs: [ + grammar.find_symbol_by_s_value!("expression"), + grammar.find_symbol_by_s_value!("'-'"), + grammar.find_symbol_by_s_value!("expression"), + ], + token_code: T::UserCode.new(s_value: " $$ = $1 - $3; "), + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("'-'"), + lineno: 25, + ), + ]) + end + end + context 'when inline with resolve index `@n` style' do let(:path) { "inlining/resolve_index.y" }