From b9c69debff585e43e8f6fec3437d2511e67255e9 Mon Sep 17 00:00:00 2001 From: ydah Date: Wed, 26 Jun 2024 10:37:23 +0900 Subject: [PATCH] Widen the definable position of parameterizing rules We came to this decision after discussion at RubyKaigi 2024. Definitions are also possible in the following positions: ``` %rule defined_option(X): /* empty */ | X ; %% program : defined_option(number) | defined_list(number) ; %rule defined_list(X): /* empty */ /* <--- here */ | defined_list(X) number ; ``` --- lib/lrama/parser.rb | 368 +++++++++--------- parser.y | 4 +- spec/fixtures/inlining/basic.y | 6 + .../parameterizing_rules/user_defined/basic.y | 5 + spec/lrama/parser_spec.rb | 65 +++- 5 files changed, 260 insertions(+), 188 deletions(-) diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index b75bccad..2d0a8ce7 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -733,31 +733,31 @@ def raise_parse_error(error_message, location) 72, 8, 162, 186, 170, 6, 53, 7, 52, 186, 81, 77, 41, 53, 53, 52, 52, 47, 84, 84, 53, 188, 52, 91, 162, 84, 171, 48, 53, 103, - 52, 187, 171, 84, 53, 50, 52, 187, 23, 25, + 52, 187, 171, 84, 53, 50, 52, 187, 21, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, - 36, 37, 38, 47, 53, 53, 52, 52, 95, 81, - 203, 53, 53, 52, 52, 81, 203, 53, 56, 52, - 225, 81, 203, 226, 23, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 9, - 193, 194, 195, 99, 14, 15, 16, 17, 18, 19, - 47, 125, 20, 21, 22, 23, 25, 26, 27, 28, + 36, 37, 38, 39, 47, 53, 53, 52, 52, 95, + 81, 203, 53, 53, 52, 52, 81, 203, 53, 56, + 52, 225, 81, 203, 226, 21, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, - 39, 53, 53, 52, 52, 81, 203, 53, 53, 52, - 52, 81, 203, 53, 53, 52, 52, 81, 203, 53, - 53, 52, 52, 81, 81, 53, 53, 52, 52, 81, - 81, 53, 53, 52, 52, 81, 81, 53, 53, 52, - 214, 81, 81, 53, 53, 214, 214, 81, 81, 53, - 53, 52, 52, 81, 193, 194, 195, 99, 230, 238, - 56, 226, 226, 53, 53, 52, 52, 53, 53, 52, - 52, 193, 194, 195, 56, 59, 60, 61, 62, 63, - 64, 65, 66, 67, 68, 69, 92, 48, 97, 99, - 104, 104, 104, 106, 112, 115, 117, 120, 120, 120, - 120, 123, 128, 129, 131, 133, 134, 135, 136, 137, - 81, 144, 145, 146, 147, 148, 151, 152, 153, 155, - 165, 144, 167, 173, 175, 176, 177, 178, 179, 180, - 182, 183, 151, 190, 198, 199, 206, 165, 210, 213, - 99, 218, 165, 222, 165, 224, 180, 183, 183, 99, - 235, 180, 237, 180, 99, 99, 180 ] + 39, 9, 193, 194, 195, 99, 12, 13, 14, 15, + 16, 17, 47, 125, 18, 19, 20, 21, 25, 26, + 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, + 37, 38, 39, 53, 53, 52, 52, 81, 203, 53, + 53, 52, 52, 81, 203, 53, 53, 52, 52, 81, + 203, 53, 53, 52, 52, 81, 81, 53, 53, 52, + 52, 81, 81, 53, 53, 52, 52, 81, 81, 53, + 53, 52, 214, 81, 81, 53, 53, 214, 214, 81, + 81, 53, 53, 52, 52, 81, 193, 194, 195, 99, + 230, 238, 56, 226, 226, 53, 53, 52, 52, 53, + 53, 52, 52, 193, 194, 195, 56, 59, 60, 61, + 62, 63, 64, 65, 66, 67, 68, 69, 92, 48, + 97, 99, 104, 104, 104, 106, 112, 115, 117, 120, + 120, 120, 120, 123, 128, 129, 131, 133, 134, 135, + 136, 137, 81, 144, 145, 146, 147, 148, 151, 152, + 153, 155, 165, 144, 167, 173, 175, 176, 177, 178, + 179, 180, 182, 183, 151, 190, 198, 199, 206, 165, + 210, 213, 99, 218, 165, 222, 165, 224, 180, 183, + 183, 99, 235, 180, 237, 180, 99, 99, 180 ] racc_action_check = [ 51, 150, 51, 150, 39, 150, 164, 189, 164, 189, @@ -765,80 +765,80 @@ def raise_parse_error(error_message, location) 33, 3, 143, 164, 189, 2, 34, 2, 34, 207, 34, 34, 7, 35, 36, 35, 36, 9, 35, 36, 37, 166, 37, 39, 166, 37, 150, 10, 38, 51, - 38, 164, 189, 38, 15, 14, 15, 207, 9, 9, + 38, 164, 189, 38, 13, 12, 13, 207, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, - 9, 9, 9, 42, 71, 177, 71, 177, 42, 177, - 177, 178, 72, 178, 72, 178, 178, 179, 16, 179, - 215, 179, 179, 215, 42, 42, 42, 42, 42, 42, - 42, 42, 42, 42, 42, 42, 42, 42, 42, 4, - 169, 169, 169, 169, 4, 4, 4, 4, 4, 4, - 91, 91, 4, 4, 4, 4, 4, 4, 4, 4, + 9, 9, 9, 9, 42, 71, 177, 71, 177, 42, + 177, 177, 178, 72, 178, 72, 178, 178, 179, 14, + 179, 215, 179, 179, 215, 42, 42, 42, 42, 42, + 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, + 42, 4, 169, 169, 169, 169, 4, 4, 4, 4, + 4, 4, 91, 91, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 200, 83, 200, 83, 200, 200, 204, 84, 204, - 84, 204, 204, 205, 112, 205, 112, 205, 205, 76, - 77, 76, 77, 76, 77, 117, 119, 117, 119, 117, - 119, 141, 171, 141, 171, 141, 171, 187, 190, 187, - 190, 187, 190, 210, 224, 210, 224, 210, 224, 226, - 114, 226, 114, 226, 184, 184, 184, 184, 221, 232, - 17, 221, 232, 120, 122, 120, 122, 138, 142, 138, - 142, 216, 216, 216, 18, 19, 20, 23, 25, 26, - 27, 28, 29, 30, 31, 32, 40, 44, 45, 46, - 55, 57, 58, 59, 70, 74, 75, 82, 87, 88, - 89, 90, 98, 99, 105, 107, 108, 109, 110, 111, - 116, 123, 124, 125, 126, 127, 128, 129, 130, 132, - 145, 146, 149, 154, 156, 157, 158, 159, 160, 161, - 162, 163, 167, 168, 172, 174, 181, 183, 185, 188, - 192, 196, 206, 211, 213, 214, 217, 220, 223, 225, - 229, 230, 231, 233, 235, 237, 240 ] + 4, 4, 4, 200, 83, 200, 83, 200, 200, 204, + 84, 204, 84, 204, 204, 205, 112, 205, 112, 205, + 205, 76, 77, 76, 77, 76, 77, 117, 119, 117, + 119, 117, 119, 141, 171, 141, 171, 141, 171, 187, + 190, 187, 190, 187, 190, 210, 224, 210, 224, 210, + 224, 226, 114, 226, 114, 226, 184, 184, 184, 184, + 221, 232, 15, 221, 232, 120, 122, 120, 122, 138, + 142, 138, 142, 216, 216, 216, 16, 17, 18, 21, + 25, 26, 27, 28, 29, 30, 31, 32, 40, 44, + 45, 46, 55, 57, 58, 59, 70, 74, 75, 82, + 87, 88, 89, 90, 98, 99, 105, 107, 108, 109, + 110, 111, 116, 123, 124, 125, 126, 127, 128, 129, + 130, 132, 145, 146, 149, 154, 156, 157, 158, 159, + 160, 161, 162, 163, 167, 168, 172, 174, 181, 183, + 185, 188, 192, 196, 206, 211, 213, 214, 217, 220, + 223, 225, 229, 230, 231, 233, 235, 237, 240 ] racc_action_pointer = [ - nil, 13, 15, 21, 100, nil, nil, 25, nil, 33, - 34, nil, nil, nil, 49, 51, 68, 180, 194, 210, - 196, nil, nil, 197, nil, 198, 199, 200, 216, 217, - 218, 219, 220, 12, 23, 30, 31, 37, 45, -1, - 224, nil, 69, nil, 214, 215, 177, nil, nil, nil, - nil, -5, nil, nil, nil, 210, nil, 211, 212, 213, + nil, 13, 15, 21, 102, nil, nil, 25, nil, 33, + 34, nil, 49, 51, 69, 182, 196, 212, 198, nil, + nil, 199, nil, nil, nil, 200, 201, 202, 218, 219, + 220, 221, 222, 12, 23, 30, 31, 37, 45, -1, + 226, nil, 70, nil, 216, 217, 179, nil, nil, nil, + nil, -5, nil, nil, nil, 212, nil, 213, 214, 215, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, - 226, 71, 79, nil, 229, 228, 156, 157, nil, nil, - nil, nil, 229, 139, 145, nil, nil, 230, 231, 232, - 200, 116, nil, nil, nil, nil, nil, nil, 199, 238, - nil, nil, nil, nil, nil, 242, nil, 243, 244, 245, - 246, 247, 151, nil, 187, nil, 243, 162, nil, 163, - 200, nil, 201, 246, 209, 212, 243, 253, 209, 204, - 256, nil, 257, nil, nil, nil, nil, nil, 204, nil, - nil, 168, 205, -23, nil, 213, 256, nil, nil, 216, - -2, nil, nil, nil, 242, nil, 243, 244, 245, 246, - 247, 261, 265, 225, 3, nil, -1, 225, 232, 61, - nil, 169, 253, nil, 254, nil, nil, 72, 78, 84, - nil, 233, nil, 230, 145, 237, nil, 174, 236, 4, - 175, nil, 228, nil, nil, nil, 279, nil, nil, nil, - 138, nil, nil, nil, 144, 150, 235, 9, nil, nil, - 180, 281, nil, 237, 244, 48, 162, 278, nil, nil, - 241, 156, nil, 242, 181, 237, 186, nil, nil, 269, - 283, 271, 157, 285, nil, 242, nil, 243, nil, nil, - 288, nil, nil ] + 228, 72, 80, nil, 231, 230, 158, 159, nil, nil, + nil, nil, 231, 141, 147, nil, nil, 232, 233, 234, + 202, 118, nil, nil, nil, nil, nil, nil, 201, 240, + nil, nil, nil, nil, nil, 244, nil, 245, 246, 247, + 248, 249, 153, nil, 189, nil, 245, 164, nil, 165, + 202, nil, 203, 248, 211, 214, 245, 255, 211, 206, + 258, nil, 259, nil, nil, nil, nil, nil, 206, nil, + nil, 170, 207, -23, nil, 215, 258, nil, nil, 218, + -2, nil, nil, nil, 244, nil, 245, 246, 247, 248, + 249, 263, 267, 227, 3, nil, -1, 227, 234, 63, + nil, 171, 255, nil, 256, nil, nil, 73, 79, 85, + nil, 235, nil, 232, 147, 239, nil, 176, 238, 4, + 177, nil, 230, nil, nil, nil, 281, nil, nil, nil, + 140, nil, nil, nil, 146, 152, 237, 9, nil, nil, + 182, 283, nil, 239, 246, 49, 164, 280, nil, nil, + 243, 158, nil, 244, 183, 239, 188, nil, nil, 271, + 285, 273, 159, 287, nil, 244, nil, 245, nil, nil, + 290, nil, nil ] racc_action_default = [ -2, -140, -10, -140, -140, -3, -4, -140, 243, -140, - -8, -12, -13, -14, -140, -140, -140, -140, -140, -140, - -140, -26, -27, -140, -31, -140, -140, -140, -140, -140, + -8, -12, -140, -140, -140, -140, -140, -140, -140, -24, + -25, -140, -29, -30, -31, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -140, -7, -127, -101, -8, -140, -124, -126, -9, -11, - -15, -131, -99, -100, -130, -17, -90, -18, -19, -140, - -23, -28, -32, -35, -38, -41, -42, -43, -44, -45, + -13, -131, -99, -100, -130, -15, -90, -16, -17, -140, + -21, -26, -32, -35, -38, -41, -42, -43, -44, -45, -46, -52, -140, -55, -57, -47, -80, -140, -83, -85, -86, -139, -48, -93, -140, -96, -98, -49, -50, -51, -140, -140, -5, -1, -102, -128, -103, -104, -140, -140, - -16, -132, -133, -134, -87, -140, -20, -140, -140, -140, + -14, -132, -133, -134, -87, -140, -18, -140, -140, -140, -140, -140, -140, -56, -53, -58, -78, -140, -84, -81, -140, -97, -94, -140, -140, -140, -140, -140, -108, -140, - -140, -91, -140, -24, -29, -33, -36, -39, -54, -59, + -140, -91, -140, -22, -27, -33, -36, -39, -54, -59, -79, -82, -95, -140, -65, -69, -140, -6, -129, -105, - -106, -109, -125, -88, -140, -21, -140, -140, -140, -140, + -106, -109, -125, -88, -140, -19, -140, -140, -140, -140, -140, -60, -140, -63, -67, -70, -140, -108, -99, -124, - -113, -140, -140, -92, -140, -25, -30, -140, -140, -140, + -113, -140, -140, -92, -140, -23, -28, -140, -140, -140, -61, -140, -66, -69, -124, -99, -74, -140, -140, -107, - -140, -110, -124, -117, -118, -119, -140, -116, -89, -22, + -140, -110, -124, -117, -118, -119, -140, -116, -89, -20, -34, -135, -137, -138, -37, -40, -69, -68, -71, -72, -140, -140, -77, -69, -99, -140, -120, -60, -114, -136, -62, -140, -75, -64, -140, -124, -140, -122, -111, -140, @@ -846,20 +846,20 @@ def raise_parse_error(error_message, location) -60, -76, -115 ] racc_goto_table = [ - 78, 54, 98, 76, 163, 71, 113, 49, 181, 121, - 1, 150, 192, 215, 143, 122, 55, 57, 58, 74, - 43, 86, 86, 86, 86, 219, 2, 209, 4, 219, - 219, 42, 93, 221, 200, 204, 205, 166, 5, 40, - 126, 96, 118, 78, 114, 10, 119, 232, 121, 113, - 189, 142, 11, 94, 12, 13, 51, 74, 74, 227, - 82, 87, 88, 89, 228, 220, 100, 132, 121, 86, - 86, 174, 223, 113, 107, 156, 108, 236, 157, 109, - 239, 158, 110, 78, 138, 118, 141, 242, 159, 111, - 160, 70, 75, 139, 116, 124, 207, 211, 74, 231, - 74, 140, 130, 172, 105, 154, 86, 118, 86, 149, - 196, 229, 127, nil, nil, nil, 169, nil, nil, nil, - nil, nil, nil, nil, 74, 191, nil, nil, 86, nil, - 184, nil, nil, nil, nil, nil, nil, 197, nil, nil, + 78, 76, 98, 54, 163, 122, 192, 71, 181, 113, + 121, 150, 43, 49, 215, 143, 82, 87, 88, 89, + 1, 209, 2, 74, 4, 86, 86, 86, 86, 42, + 93, 219, 5, 40, 221, 219, 219, 126, 166, 10, + 11, 142, 118, 78, 119, 94, 114, 96, 232, 121, + 189, 51, 113, 227, 55, 57, 58, 200, 204, 205, + 100, 74, 74, 132, 228, 220, 174, 107, 156, 121, + 108, 157, 223, 86, 86, 109, 113, 236, 158, 110, + 239, 159, 111, 78, 141, 118, 138, 242, 160, 70, + 75, 139, 116, 124, 207, 211, 231, 140, 130, 172, + 105, 154, 74, 149, 74, 196, 229, 118, 127, nil, + 86, nil, 86, nil, nil, nil, 169, nil, nil, nil, + nil, nil, nil, nil, nil, 191, nil, nil, 74, nil, + 184, nil, 86, nil, nil, nil, nil, 197, nil, nil, 208, nil, nil, nil, nil, nil, nil, nil, 217, nil, nil, nil, nil, 212, nil, 169, 216, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, @@ -868,20 +868,20 @@ def raise_parse_error(error_message, location) 216, 240, 234, 241 ] racc_goto_check = [ - 44, 36, 45, 51, 40, 34, 35, 10, 41, 57, - 1, 61, 46, 47, 39, 56, 16, 16, 16, 36, - 58, 36, 36, 36, 36, 65, 2, 46, 3, 65, - 65, 4, 5, 47, 22, 22, 22, 39, 6, 7, - 8, 10, 44, 44, 34, 9, 51, 47, 57, 35, - 61, 56, 11, 58, 12, 13, 14, 36, 36, 46, - 33, 33, 33, 33, 41, 40, 15, 17, 57, 36, - 36, 18, 40, 35, 19, 20, 23, 41, 24, 25, - 41, 26, 27, 44, 34, 44, 51, 41, 28, 29, - 30, 31, 32, 37, 38, 42, 43, 48, 36, 49, - 36, 50, 52, 53, 54, 55, 36, 44, 36, 60, - 62, 63, 64, nil, nil, nil, 44, nil, nil, nil, - nil, nil, nil, nil, 36, 45, nil, nil, 36, nil, - 44, nil, nil, nil, nil, nil, nil, 44, nil, nil, + 44, 51, 45, 36, 40, 56, 46, 34, 41, 35, + 57, 61, 58, 10, 47, 39, 33, 33, 33, 33, + 1, 46, 2, 36, 3, 36, 36, 36, 36, 4, + 5, 65, 6, 7, 47, 65, 65, 8, 39, 9, + 11, 56, 44, 44, 51, 58, 34, 10, 47, 57, + 61, 12, 35, 46, 14, 14, 14, 22, 22, 22, + 13, 36, 36, 15, 41, 40, 16, 17, 18, 57, + 23, 24, 40, 36, 36, 25, 35, 41, 26, 27, + 41, 28, 29, 44, 51, 44, 34, 41, 30, 31, + 32, 37, 38, 42, 43, 48, 49, 50, 52, 53, + 54, 55, 36, 60, 36, 62, 63, 44, 64, nil, + 36, nil, 36, nil, nil, nil, 44, nil, nil, nil, + nil, nil, nil, nil, nil, 45, nil, nil, 36, nil, + 44, nil, 36, nil, nil, nil, nil, 44, nil, nil, 45, nil, nil, nil, nil, nil, nil, nil, 45, nil, nil, nil, nil, 44, nil, 44, 44, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, @@ -890,18 +890,18 @@ def raise_parse_error(error_message, location) 44, 45, 44, 45 ] racc_goto_pointer = [ - nil, 10, 26, 26, 22, -10, 36, 33, -52, 41, - -3, 48, 50, 51, 41, 15, 0, -39, -84, 14, - -58, nil, -143, 15, -56, 17, -54, 19, -48, 25, - -47, 58, 58, 25, -28, -65, -14, -23, 20, -109, - -141, -153, 4, -87, -34, -44, -157, -177, -89, -123, - -15, -31, -2, -50, 48, -26, -69, -74, 11, nil, - -19, -117, -60, -107, 17, -175 ] + nil, 20, 22, 22, 20, -12, 30, 27, -55, 35, + 3, 36, 38, 9, 40, -43, -89, 7, -65, nil, + nil, nil, -120, 9, -63, 13, -57, 16, -55, 18, + -49, 56, 56, -19, -26, -62, -10, -25, 18, -108, + -141, -153, 2, -89, -34, -44, -163, -176, -91, -126, + -19, -33, -6, -54, 44, -30, -79, -73, 3, nil, + -25, -117, -65, -112, 13, -169 ] racc_goto_default = [ nil, 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, 45, nil, nil, nil, nil, nil, nil, nil, 22, + 23, 24, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, 73, 79, nil, nil, nil, nil, nil, 46, 164, 202, nil, nil, nil, nil, nil, 80, nil, nil, nil, nil, nil, 83, 85, nil, 44, @@ -921,24 +921,24 @@ def raise_parse_error(error_message, location) 0, 58, :_reduce_10, 3, 58, :_reduce_none, 1, 64, :_reduce_none, - 1, 64, :_reduce_none, - 1, 64, :_reduce_none, - 2, 64, :_reduce_15, + 2, 64, :_reduce_13, 3, 64, :_reduce_none, 2, 64, :_reduce_none, - 2, 64, :_reduce_18, - 2, 64, :_reduce_19, - 0, 72, :_reduce_20, - 0, 73, :_reduce_21, - 7, 64, :_reduce_22, - 0, 74, :_reduce_23, - 0, 75, :_reduce_24, - 6, 64, :_reduce_25, - 1, 64, :_reduce_26, - 1, 64, :_reduce_27, - 0, 78, :_reduce_28, - 0, 79, :_reduce_29, - 6, 66, :_reduce_30, + 2, 64, :_reduce_16, + 2, 64, :_reduce_17, + 0, 70, :_reduce_18, + 0, 71, :_reduce_19, + 7, 64, :_reduce_20, + 0, 72, :_reduce_21, + 0, 73, :_reduce_22, + 6, 64, :_reduce_23, + 1, 64, :_reduce_24, + 1, 64, :_reduce_25, + 0, 78, :_reduce_26, + 0, 79, :_reduce_27, + 6, 66, :_reduce_28, + 1, 66, :_reduce_none, + 1, 66, :_reduce_none, 1, 66, :_reduce_none, 0, 80, :_reduce_32, 0, 81, :_reduce_33, @@ -954,12 +954,12 @@ def raise_parse_error(error_message, location) 2, 66, :_reduce_43, 2, 66, :_reduce_44, 2, 66, :_reduce_45, - 2, 76, :_reduce_none, - 2, 76, :_reduce_47, - 2, 76, :_reduce_48, - 2, 76, :_reduce_49, - 2, 76, :_reduce_50, - 2, 76, :_reduce_51, + 2, 74, :_reduce_none, + 2, 74, :_reduce_47, + 2, 74, :_reduce_48, + 2, 74, :_reduce_49, + 2, 74, :_reduce_50, + 2, 74, :_reduce_51, 1, 86, :_reduce_52, 2, 86, :_reduce_53, 3, 86, :_reduce_54, @@ -970,9 +970,9 @@ def raise_parse_error(error_message, location) 3, 90, :_reduce_59, 0, 96, :_reduce_none, 1, 96, :_reduce_none, - 8, 67, :_reduce_62, - 5, 68, :_reduce_63, - 8, 68, :_reduce_64, + 8, 75, :_reduce_62, + 5, 76, :_reduce_63, + 8, 76, :_reduce_64, 1, 94, :_reduce_65, 3, 94, :_reduce_66, 1, 95, :_reduce_67, @@ -997,10 +997,10 @@ def raise_parse_error(error_message, location) 1, 99, :_reduce_none, 0, 107, :_reduce_87, 0, 108, :_reduce_88, - 6, 71, :_reduce_89, + 6, 69, :_reduce_89, 0, 109, :_reduce_90, 0, 110, :_reduce_91, - 5, 71, :_reduce_92, + 5, 69, :_reduce_92, 1, 88, :_reduce_93, 2, 88, :_reduce_94, 3, 88, :_reduce_95, @@ -1038,11 +1038,11 @@ def raise_parse_error(error_message, location) 0, 60, :_reduce_none, 0, 119, :_reduce_128, 3, 60, :_reduce_129, - 1, 69, :_reduce_none, - 0, 70, :_reduce_none, - 1, 70, :_reduce_none, - 1, 70, :_reduce_none, - 1, 70, :_reduce_none, + 1, 67, :_reduce_none, + 0, 68, :_reduce_none, + 1, 68, :_reduce_none, + 1, 68, :_reduce_none, + 1, 68, :_reduce_none, 1, 77, :_reduce_135, 2, 77, :_reduce_136, 1, 120, :_reduce_none, @@ -1199,8 +1199,6 @@ def raise_parse_error(error_message, location) "bison_declaration", "\"-option@;\"", "grammar_declaration", - "rule_declaration", - "inline_declaration", "variable", "value", "params", @@ -1209,6 +1207,8 @@ def raise_parse_error(error_message, location) "@5", "@6", "symbol_declaration", + "rule_declaration", + "inline_declaration", "generic_symlist", "@7", "@8", @@ -1309,23 +1309,19 @@ def _reduce_10(val, _values, result) # reduce 12 omitted -# reduce 13 omitted - -# reduce 14 omitted - -module_eval(<<'.,.,', 'parser.y', 34) - def _reduce_15(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 32) + def _reduce_13(val, _values, result) @grammar.expect = val[1] result end .,., -# reduce 16 omitted +# reduce 14 omitted -# reduce 17 omitted +# reduce 15 omitted -module_eval(<<'.,.,', 'parser.y', 39) - def _reduce_18(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 37) + def _reduce_16(val, _values, result) val[1].each {|token| @grammar.lex_param = Grammar::Code::NoReferenceCode.new(type: :lex_param, token_code: token).token_code.s_value } @@ -1334,8 +1330,8 @@ def _reduce_18(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 45) - def _reduce_19(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 43) + def _reduce_17(val, _values, result) val[1].each {|token| @grammar.parse_param = Grammar::Code::NoReferenceCode.new(type: :parse_param, token_code: token).token_code.s_value } @@ -1344,86 +1340,86 @@ def _reduce_19(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 51) - def _reduce_20(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 49) + def _reduce_18(val, _values, result) begin_c_declaration("}") result end .,., -module_eval(<<'.,.,', 'parser.y', 55) - def _reduce_21(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 53) + def _reduce_19(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 59) - def _reduce_22(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 57) + def _reduce_20(val, _values, result) @grammar.add_percent_code(id: val[1], code: val[4]) result end .,., -module_eval(<<'.,.,', 'parser.y', 63) - def _reduce_23(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 61) + def _reduce_21(val, _values, result) begin_c_declaration("}") result end .,., -module_eval(<<'.,.,', 'parser.y', 67) - def _reduce_24(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 65) + def _reduce_22(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 71) - def _reduce_25(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 69) + def _reduce_23(val, _values, result) @grammar.initial_action = Grammar::Code::InitialActionCode.new(type: :initial_action, token_code: val[3]) result end .,., -module_eval(<<'.,.,', 'parser.y', 73) - def _reduce_26(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 71) + def _reduce_24(val, _values, result) @grammar.no_stdlib = true result end .,., -module_eval(<<'.,.,', 'parser.y', 74) - def _reduce_27(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 72) + def _reduce_25(val, _values, result) @grammar.locations = true result end .,., -module_eval(<<'.,.,', 'parser.y', 78) - def _reduce_28(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 76) + def _reduce_26(val, _values, result) begin_c_declaration("}") result end .,., -module_eval(<<'.,.,', 'parser.y', 82) - def _reduce_29(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 80) + def _reduce_27(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 86) - def _reduce_30(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 84) + def _reduce_28(val, _values, result) @grammar.set_union( Grammar::Code::NoReferenceCode.new(type: :union, token_code: val[3]), val[3].line @@ -1433,6 +1429,10 @@ def _reduce_30(val, _values, result) end .,., +# reduce 29 omitted + +# reduce 30 omitted + # reduce 31 omitted module_eval(<<'.,.,', 'parser.y', 94) diff --git a/parser.y b/parser.y index 6fed0fd7..a6cf19d2 100644 --- a/parser.y +++ b/parser.y @@ -30,8 +30,6 @@ rule | bison_declarations bison_declaration ";"? bison_declaration: grammar_declaration - | rule_declaration - | inline_declaration | "%expect" INTEGER { @grammar.expect = val[1] } | "%define" variable value | "%param" params @@ -90,6 +88,8 @@ rule ) } | symbol_declaration + | rule_declaration + | inline_declaration | "%destructor" "{" { begin_c_declaration("}") diff --git a/spec/fixtures/inlining/basic.y b/spec/fixtures/inlining/basic.y index 9fe55314..1db03da3 100644 --- a/spec/fixtures/inlining/basic.y +++ b/spec/fixtures/inlining/basic.y @@ -25,8 +25,14 @@ static int yyerror(YYLTYPE *loc, const char *str); expression : NUM | expression op expression { $$ = $1 $2 $3; } + | expression other_op expression { $$ = $1 $2 $3; } ; + +%rule %inline other_op : '%' { + 1 + } + | '&' { - 1 - } + ; + %% static int yylex(YYSTYPE *yylval, YYLTYPE *loc) diff --git a/spec/fixtures/parameterizing_rules/user_defined/basic.y b/spec/fixtures/parameterizing_rules/user_defined/basic.y index dc8279ab..fb62f5fe 100644 --- a/spec/fixtures/parameterizing_rules/user_defined/basic.y +++ b/spec/fixtures/parameterizing_rules/user_defined/basic.y @@ -21,8 +21,13 @@ static int yyerror(YYLTYPE *loc, const char *str); %% program : defined_option(number) + | defined_list(number) ; +%rule defined_list(X): /* empty */ + | defined_list(X) number + ; + %% static int yylex(YYSTYPE *yylval, YYLTYPE *loc) diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index 3e8ff6a6..8b14ee5f 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -1430,6 +1430,7 @@ 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: "defined_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: "program"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 2, nullable: true), + Sym.new(id: T::Ident.new(s_value: "defined_list_number"), alias_name: nil, number: 7, tag: T::Tag.new(s_value: ""), term: false, token_id: 3, nullable: true), ]) expect(grammar.rules).to eq([ @@ -1478,6 +1479,40 @@ precedence_sym: nil, lineno: 23, ), + Rule.new( + id: 4, + lhs: grammar.find_symbol_by_s_value!("defined_list_number"), + rhs: [], + lhs_tag: T::Tag.new(s_value: ""), + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 24, + ), + Rule.new( + id: 5, + lhs: grammar.find_symbol_by_s_value!("defined_list_number"), + rhs: [ + grammar.find_symbol_by_s_value!("defined_list_number"), + 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: 24, + ), + Rule.new( + id: 6, + lhs: grammar.find_symbol_by_s_value!("program"), + rhs: [ + grammar.find_symbol_by_s_value!("defined_list_number"), + ], + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 24, + ), ]) end end @@ -2324,8 +2359,8 @@ 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: 8, tag: nil, term: false, token_id: 0, nullable: false), - Sym.new(id: T::Ident.new(s_value: "expression"), alias_name: nil, number: 9, tag: T::Tag.new(s_value: ""), term: false, token_id: 1, nullable: false), + Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 10, tag: nil, term: false, token_id: 0, nullable: false), + Sym.new(id: T::Ident.new(s_value: "expression"), alias_name: nil, number: 11, tag: T::Tag.new(s_value: ""), term: false, token_id: 1, nullable: false), ]) expect(grammar.rules).to eq([ @@ -2404,6 +2439,32 @@ precedence_sym: grammar.find_symbol_by_s_value!("'/'"), lineno: 27, ), + Rule.new( + id: 6, + 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 + 1 + $3; "), + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("'%'"), + lineno: 28, + ), + Rule.new( + id: 7, + 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 - 1 - $3; "), + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("'&'"), + lineno: 28, + ), ]) end end