diff --git a/lib/lrama/grammar/binding.rb b/lib/lrama/grammar/binding.rb index 5e6e7c59..718c3699 100644 --- a/lib/lrama/grammar/binding.rb +++ b/lib/lrama/grammar/binding.rb @@ -17,6 +17,10 @@ def resolve_symbol(symbol) if symbol.is_a?(Lexer::Token::InstantiateRule) resolved_args = symbol.args.map { |arg| resolve_symbol(arg) } Lrama::Lexer::Token::InstantiateRule.new(s_value: symbol.s_value, location: symbol.location, args: resolved_args, lhs_tag: symbol.lhs_tag) + elsif symbol.is_a?(Lexer::Token::ControlSyntax) + resolved = symbol.dup + resolved.condition = @parameter_to_arg[symbol.condition_value] + resolved else parameter_to_arg(symbol) || symbol end diff --git a/lib/lrama/grammar/parameterizing_rule/rhs.rb b/lib/lrama/grammar/parameterizing_rule/rhs.rb index d574841d..a5c6ef35 100644 --- a/lib/lrama/grammar/parameterizing_rule/rhs.rb +++ b/lib/lrama/grammar/parameterizing_rule/rhs.rb @@ -4,12 +4,20 @@ module Lrama class Grammar class ParameterizingRule class Rhs - attr_accessor :symbols, :user_code, :precedence_sym + attr_accessor :symbols, :user_code, :precedence_sym, :if_clause def initialize @symbols = [] @user_code = nil @precedence_sym = nil + @if_clause = nil + end + + def skip?(bindings) + return false unless @if_clause + + resolved = bindings.resolve_symbol(@if_clause) + resolved.is_a?(Lexer::Token::ControlSyntax) && resolved.if? && resolved.false? end def resolve_user_code(bindings) diff --git a/lib/lrama/grammar/rule_builder.rb b/lib/lrama/grammar/rule_builder.rb index f6df22b6..e2784e38 100644 --- a/lib/lrama/grammar/rule_builder.rb +++ b/lib/lrama/grammar/rule_builder.rb @@ -140,12 +140,14 @@ def process_rhs if (created_lhs = @parameterizing_rule_resolver.created_lhs(lhs_s_value)) @replaced_rhs << created_lhs else + next if parameterizing_rule.rhs_list.all? { |r| r.skip?(bindings) } lhs_token = Lrama::Lexer::Token::Ident.new(s_value: lhs_s_value, location: token.location) @replaced_rhs << lhs_token @parameterizing_rule_resolver.created_lhs_list << lhs_token parameterizing_rule.rhs_list.each do |r| rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, @parameterizing_rule_resolver, lhs_tag: token.lhs_tag || parameterizing_rule.tag) rule_builder.lhs = lhs_token + next if r.skip?(bindings) r.symbols.each { |sym| rule_builder.add_rhs(bindings.resolve_symbol(sym)) } rule_builder.line = line rule_builder.precedence_sym = r.precedence_sym @@ -168,6 +170,8 @@ def process_rhs rule_builder.setup_rules @rule_builders_for_derived_rules << rule_builder + when Lrama::Lexer::Token::ControlSyntax + # NOP else raise "Unexpected token. #{token}" end diff --git a/lib/lrama/lexer.rb b/lib/lrama/lexer.rb index 0a8f9433..82f4e765 100644 --- a/lib/lrama/lexer.rb +++ b/lib/lrama/lexer.rb @@ -41,6 +41,9 @@ class Lexer %no-stdlib %inline %locations + %if + %true + %false ).freeze def initialize(grammar_file) diff --git a/lib/lrama/lexer/token.rb b/lib/lrama/lexer/token.rb index 45a097f6..ed78ec62 100644 --- a/lib/lrama/lexer/token.rb +++ b/lib/lrama/lexer/token.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'token/char' +require_relative 'token/control_syntax' require_relative 'token/ident' require_relative 'token/instantiate_rule' require_relative 'token/tag' diff --git a/lib/lrama/lexer/token/control_syntax.rb b/lib/lrama/lexer/token/control_syntax.rb new file mode 100644 index 00000000..16ff6c3a --- /dev/null +++ b/lib/lrama/lexer/token/control_syntax.rb @@ -0,0 +1,30 @@ +module Lrama + class Lexer + class Token + class ControlSyntax < Token + attr_accessor :condition + + def initialize(s_value:, location:, condition: nil) + @condition = condition + super(s_value: s_value, location: location) + end + + def if? + s_value == '%if' + end + + def true? + !!@condition&.s_value + end + + def false? + !true? + end + + def condition_value + @condition&.s_value + end + end + end + end +end diff --git a/lib/lrama/parser.rb b/lib/lrama/parser.rb index 6a35dba2..1507395b 100644 --- a/lib/lrama/parser.rb +++ b/lib/lrama/parser.rb @@ -654,7 +654,7 @@ def token_to_str(t) module Lrama class Parser < Racc::Parser -module_eval(<<'...end parser.y/module_eval...', 'parser.y', 417) +module_eval(<<'...end parser.y/module_eval...', 'parser.y', 435) include Lrama::Report::Duration @@ -728,297 +728,322 @@ def raise_parse_error(error_message, location) ##### State transition tables begin ### racc_action_table = [ - 94, 48, 95, 166, 48, 77, 172, 48, 77, 166, - 48, 77, 172, 48, 77, 47, 6, 85, 69, 48, - 48, 47, 47, 77, 74, 81, 48, 48, 47, 47, - 40, 81, 81, 48, 41, 47, 92, 48, 81, 47, - 44, 77, 103, 168, 169, 45, 175, 169, 96, 168, - 169, 52, 175, 169, 86, 20, 24, 25, 26, 27, - 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, - 38, 92, 48, 4, 47, 5, 77, 103, 48, 48, - 47, 47, 77, 103, 116, 48, 4, 47, 5, 77, + 94, 6, 95, 48, 85, 201, 48, 77, 211, 48, + 77, 211, 48, 77, 47, 48, 77, 211, 48, 77, + 47, 48, 77, 47, 40, 148, 69, 48, 149, 168, + 48, 77, 175, 48, 77, 168, 48, 77, 175, -118, + 77, 86, 4, 41, 5, 183, 184, 185, 4, 44, + 5, 96, 206, 207, 173, 206, 207, 149, 206, 207, + 45, 206, 207, 92, 206, 207, 52, 206, 207, 170, + 171, 172, 178, 52, 172, 170, 171, 172, 178, 92, + 172, 52, 20, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 20, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 38, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 55, 20, 24, 25, 26, 27, 28, + 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 43, 20, 24, 25, 26, 27, 28, 29, 30, 31, 32, - 33, 34, 35, 36, 37, 38, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 52, 20, 24, 25, 26, - 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, - 37, 38, 11, 12, 13, 14, 15, 16, 17, 18, - 19, 43, 20, 24, 25, 26, 27, 28, 29, 30, - 31, 32, 33, 34, 35, 36, 37, 38, 48, 48, - 47, 47, 77, 103, 48, 48, 47, 47, 77, 77, - 48, 48, 47, 47, 77, 77, 48, 48, 196, 196, - 77, 77, 48, 48, 47, 196, 77, 77, 148, 170, - 52, 149, 149, 180, 181, 182, 131, 180, 181, 182, - 131, 203, 208, 215, 204, 204, 204, 48, 48, 47, - 47, 48, 48, 47, 47, 48, 48, 47, 47, 180, - 181, 182, 119, 120, 55, 52, 52, 52, 52, 52, - 61, 62, 63, 64, 65, 87, 52, 52, 106, 109, - 111, 118, 125, 126, 128, 131, 132, 77, 140, 141, - 142, 143, 145, 146, 153, 140, 155, 153, 159, 160, - 161, 163, 164, 171, 176, 153, 183, 131, 187, 153, - 189, 131, 153, 198, 153, 131, 161, 164, 205, 164, - 161, 161, 213, 131, 161 ] + 33, 34, 35, 36, 37, 38, 48, 48, 47, 47, + 77, 74, 81, 48, 48, 47, 47, 52, 81, 81, + 48, 48, 47, 47, 48, 81, 47, 52, 77, 103, + 48, 48, 47, 47, 77, 103, 48, 48, 47, 47, + 77, 103, 116, 48, 48, 47, 47, 77, 103, 183, + 184, 185, 119, 120, 131, 183, 184, 185, 52, 48, + 131, 47, 48, 77, 47, 48, 77, 47, 48, 77, + 47, 48, 77, 47, 223, 77, 235, 224, 236, 224, + 48, 224, 47, 48, 48, 47, 47, 48, 52, 47, + 183, 184, 185, 52, 61, 62, 63, 64, 65, 87, + 52, 52, 106, 109, 111, 118, 125, 126, 128, 131, + 132, 77, 140, 141, 142, 143, 145, 146, 153, 140, + 155, 153, 159, 160, 161, 163, 164, 174, 179, 153, + 186, 131, 189, 191, 153, 193, 131, 153, 208, 209, + 153, 131, 161, 164, 216, 217, 219, 220, 164, 222, + 161, 161, -116, 231, 131, -118, 161, -117 ] racc_action_check = [ - 46, 151, 46, 151, 157, 151, 157, 177, 157, 177, - 188, 177, 188, 32, 188, 32, 1, 38, 32, 33, - 34, 33, 34, 33, 33, 34, 35, 36, 35, 36, - 5, 35, 36, 37, 6, 37, 44, 58, 37, 58, - 9, 58, 58, 151, 151, 11, 157, 157, 46, 177, - 177, 13, 188, 188, 38, 44, 44, 44, 44, 44, - 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, - 44, 88, 59, 0, 59, 0, 59, 59, 60, 79, - 60, 79, 60, 60, 79, 72, 2, 72, 2, 72, + 46, 1, 46, 186, 38, 186, 193, 186, 193, 217, + 193, 217, 219, 217, 219, 222, 219, 222, 224, 222, + 224, 32, 224, 32, 5, 139, 32, 151, 139, 151, + 157, 151, 157, 180, 157, 180, 192, 180, 192, 202, + 192, 38, 0, 6, 0, 202, 202, 202, 2, 9, + 2, 46, 186, 186, 154, 193, 193, 154, 217, 217, + 11, 219, 219, 44, 222, 222, 13, 224, 224, 151, + 151, 151, 157, 14, 157, 180, 180, 180, 192, 88, + 192, 15, 44, 44, 44, 44, 44, 44, 44, 44, + 44, 44, 44, 44, 44, 44, 44, 44, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, - 88, 88, 88, 88, 88, 88, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 14, 3, 3, 3, 3, + 88, 88, 88, 88, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 16, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 100, 12, - 100, 12, 100, 100, 74, 111, 74, 111, 74, 111, - 168, 175, 168, 175, 168, 175, 183, 189, 183, 189, - 183, 189, 204, 205, 204, 205, 204, 205, 139, 154, - 15, 139, 154, 165, 165, 165, 165, 173, 173, 173, - 173, 195, 200, 212, 195, 200, 212, 67, 69, 67, - 69, 81, 106, 81, 106, 114, 116, 114, 116, 197, - 197, 197, 86, 86, 16, 17, 20, 24, 25, 26, - 27, 28, 29, 30, 31, 39, 50, 55, 66, 70, - 71, 85, 89, 90, 91, 92, 98, 110, 118, 119, - 120, 121, 130, 131, 141, 142, 144, 145, 146, 147, - 148, 149, 150, 156, 162, 164, 166, 167, 170, 171, - 172, 174, 176, 186, 187, 191, 192, 194, 196, 199, - 201, 203, 207, 208, 214 ] + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 33, 34, 33, 34, + 33, 33, 34, 35, 36, 35, 36, 17, 35, 36, + 37, 12, 37, 12, 58, 37, 58, 20, 58, 58, + 59, 67, 59, 67, 59, 59, 60, 79, 60, 79, + 60, 60, 79, 100, 69, 100, 69, 100, 100, 167, + 167, 167, 86, 86, 167, 176, 176, 176, 24, 72, + 176, 72, 74, 72, 74, 111, 74, 111, 170, 111, + 170, 178, 170, 178, 212, 178, 228, 212, 232, 228, + 81, 232, 81, 106, 114, 106, 114, 116, 25, 116, + 213, 213, 213, 26, 27, 28, 29, 30, 31, 39, + 50, 55, 66, 70, 71, 85, 89, 90, 91, 92, + 98, 110, 118, 119, 120, 121, 130, 131, 141, 142, + 144, 145, 146, 147, 148, 149, 150, 156, 162, 164, + 168, 169, 171, 173, 174, 175, 177, 179, 189, 190, + 191, 195, 196, 198, 200, 201, 203, 208, 210, 211, + 214, 216, 218, 221, 223, 229, 233, 235 ] racc_action_pointer = [ - 63, 16, 76, 93, nil, 23, 34, nil, 119, 31, - nil, 39, 156, 5, 69, 144, 219, 179, nil, nil, - 180, nil, nil, nil, 181, 182, 183, 225, 226, 227, - 228, 229, 10, 16, 17, 23, 24, 30, 12, 233, - nil, nil, nil, nil, 32, nil, -5, nil, nil, nil, - 190, nil, nil, nil, nil, 191, nil, nil, 34, 69, - 75, nil, nil, nil, nil, nil, 230, 204, nil, 205, - 233, 232, 82, nil, 161, nil, nil, nil, nil, 76, - nil, 208, nil, nil, nil, 202, 218, nil, 67, 233, - 221, 222, 194, nil, nil, nil, nil, nil, 244, nil, - 155, nil, nil, nil, nil, nil, 209, nil, nil, nil, - 240, 162, nil, nil, 212, nil, 213, nil, 243, 208, - 211, 240, nil, nil, nil, nil, nil, nil, nil, nil, - 211, 248, nil, nil, nil, nil, nil, nil, nil, 148, - nil, 200, 250, nil, 254, 203, 206, 212, 252, 256, - 218, -2, nil, nil, 149, nil, 219, 1, nil, nil, - nil, nil, 223, nil, 211, 145, 227, 216, 167, nil, - 227, 215, 231, 149, 220, 168, 218, 4, nil, nil, - nil, nil, nil, 173, nil, nil, 271, 220, 7, 174, - nil, 224, 268, nil, 233, 161, 239, 171, nil, 235, - 162, 272, nil, 273, 179, 180, nil, 235, 232, nil, - nil, nil, 163, nil, 276, nil, nil ] + 32, 1, 38, 101, nil, 17, 43, nil, 127, 40, + nil, 54, 178, 19, 26, 34, 118, 130, nil, nil, + 140, nil, nil, nil, 171, 201, 206, 249, 250, 251, + 252, 253, 18, 163, 164, 170, 171, 177, -1, 257, + nil, nil, nil, nil, 59, nil, -5, nil, nil, nil, + 213, nil, nil, nil, nil, 214, nil, nil, 181, 187, + 193, nil, nil, nil, nil, nil, 254, 188, nil, 201, + 257, 256, 216, nil, 219, nil, nil, nil, nil, 194, + nil, 237, nil, nil, nil, 226, 208, nil, 75, 257, + 245, 246, 215, nil, nil, nil, nil, nil, 268, nil, + 200, nil, nil, nil, nil, nil, 240, nil, nil, nil, + 264, 222, nil, nil, 241, nil, 244, nil, 267, 232, + 235, 264, nil, nil, nil, nil, nil, nil, nil, nil, + 235, 272, nil, nil, nil, nil, nil, nil, nil, -15, + nil, 221, 274, nil, 278, 224, 227, 235, 276, 280, + 242, 24, nil, nil, 14, nil, 243, 27, nil, nil, + nil, nil, 247, nil, 232, nil, nil, 160, 251, 237, + 225, 253, nil, 252, 237, 256, 166, 242, 228, 240, + 30, nil, nil, nil, nil, nil, 0, nil, nil, 293, + 297, 243, 33, 3, nil, 247, 294, nil, 259, nil, + 264, 266, -4, 263, nil, nil, nil, nil, 267, nil, + 264, 270, 194, 201, 302, nil, 303, 6, 269, 9, + nil, 265, 12, 260, 15, nil, nil, nil, 196, 272, + nil, nil, 198, 308, nil, 274, nil, nil ] racc_action_default = [ - -1, -127, -1, -3, -10, -127, -127, -2, -3, -127, - -16, -127, -127, -127, -127, -127, -127, -127, -24, -25, - -127, -30, -31, -32, -127, -127, -127, -127, -127, -127, - -127, -127, -127, -127, -127, -127, -127, -127, -127, -127, - -13, 217, -4, -26, -127, -17, -118, -89, -90, -117, - -14, -19, -81, -20, -21, -127, -23, -29, -127, -127, - -127, -36, -37, -38, -39, -40, -41, -47, -49, -127, - -52, -42, -74, -76, -127, -79, -80, -126, -43, -84, - -86, -127, -44, -45, -46, -127, -127, -11, -5, -7, - -91, -127, -64, -18, -119, -120, -121, -15, -127, -22, - -27, -33, -122, -123, -34, -35, -127, -48, -50, -53, - -72, -127, -75, -77, -84, -85, -127, -87, -127, -127, - -127, -127, -6, -8, -9, -115, -92, -93, -94, -65, - -127, -127, -82, -28, -51, -54, -73, -78, -88, -127, - -60, -124, -127, -12, -127, -124, -127, -127, -55, -127, - -58, -62, -66, -125, -127, -116, -95, -96, -98, -114, - -83, -56, -127, -61, -124, -64, -89, -64, -127, -111, - -127, -124, -89, -64, -64, -127, -124, -63, -67, -68, - -104, -105, -106, -127, -70, -71, -127, -124, -97, -127, - -99, -64, -55, -103, -57, -127, -89, -107, -112, -59, - -127, -55, -102, -55, -127, -127, -109, -127, -64, -100, - -69, -108, -127, -113, -55, -110, -101 ] + -1, -138, -1, -3, -10, -138, -138, -2, -3, -138, + -16, -138, -138, -138, -138, -138, -138, -138, -24, -25, + -138, -30, -31, -32, -138, -138, -138, -138, -138, -138, + -138, -138, -138, -138, -138, -138, -138, -138, -138, -138, + -13, 238, -4, -26, -138, -17, -129, -92, -93, -128, + -14, -19, -84, -20, -21, -138, -23, -29, -138, -138, + -138, -36, -37, -38, -39, -40, -41, -47, -49, -138, + -52, -42, -77, -79, -138, -82, -83, -137, -43, -87, + -89, -138, -44, -45, -46, -138, -138, -11, -5, -7, + -94, -138, -66, -18, -130, -131, -132, -15, -138, -22, + -27, -33, -133, -134, -34, -35, -138, -48, -50, -53, + -75, -138, -78, -80, -87, -88, -138, -90, -138, -138, + -138, -138, -6, -8, -9, -126, -95, -96, -97, -67, + -138, -138, -85, -28, -51, -54, -76, -81, -91, -138, + -60, -135, -138, -12, -138, -135, -138, -138, -55, -138, + -58, -62, -68, -136, -138, -127, -98, -99, -101, -125, + -86, -56, -138, -61, -135, -63, -64, -66, -92, -66, + -138, -138, -122, -138, -135, -92, -66, -66, -138, -135, + -62, -69, -70, -107, -108, -109, -138, -72, -73, -138, + -138, -135, -100, -138, -102, -66, -55, -106, -57, -65, + -138, -92, -110, -138, -114, -119, -120, -121, -138, -123, + -59, -92, -138, -118, -55, -105, -55, -138, -112, -138, + -74, -138, -138, -66, -138, -116, -103, -71, -138, -111, + -115, -124, -138, -55, -118, -113, -117, -104 ] racc_goto_table = [ - 102, 102, 102, 49, 130, 73, 68, 89, 162, 91, - 150, 139, 101, 104, 105, 1, 9, 174, 51, 53, - 54, 42, 115, 70, 117, 79, 79, 79, 79, 56, - 123, 3, 57, 7, 124, 154, 58, 59, 60, 179, - 158, 107, 102, 108, 112, 194, 113, 191, 174, 157, - 39, 122, 202, 91, 133, 97, 199, 115, 70, 138, - 70, 209, 195, 210, 121, 46, 158, 99, 200, 93, - 114, 206, 114, 66, 216, 188, 71, 178, 135, 184, - 134, 110, 177, 137, 212, 190, 192, 78, 82, 83, - 84, 136, 98, 165, 147, 127, 156, 70, 186, 173, - 207, 144, nil, 201, nil, 114, nil, 114, nil, nil, - 185, nil, nil, nil, nil, nil, nil, 193, nil, 165, - 214, nil, nil, nil, nil, 197, nil, nil, nil, nil, - 173, 197, nil, nil, nil, nil, nil, nil, nil, nil, - nil, nil, nil, nil, nil, nil, 211, 197 ] + 102, 102, 102, 49, 68, 130, 73, 162, 182, 89, + 1, 150, 91, 51, 53, 54, 177, 195, 123, 166, + 101, 104, 105, 70, 124, 79, 79, 79, 79, 56, + 39, 115, 57, 117, 158, 157, 58, 59, 60, 107, + 121, 108, 102, 218, 3, 112, 7, 113, 199, 198, + 97, 177, 203, 122, 225, 215, 91, 139, 70, 212, + 70, 210, 133, 158, 192, 46, 115, 99, 138, 9, + 114, 230, 114, 226, 42, 227, 230, 93, 134, 66, + 181, 154, 187, 228, 137, 71, 135, 110, 232, 194, + 196, 180, 237, 167, 200, 136, 98, 70, 147, 176, + 78, 82, 83, 84, 127, 114, 156, 114, 214, 190, + 221, 144, 188, nil, nil, nil, nil, nil, nil, nil, + 197, nil, 167, nil, nil, nil, nil, nil, 202, nil, + nil, nil, nil, nil, 176, 213, 233, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, nil, 213, + nil, 229, nil, nil, 213, nil, 234 ] racc_goto_check = [ - 35, 35, 35, 27, 40, 42, 26, 8, 32, 12, - 31, 30, 21, 21, 21, 1, 7, 39, 16, 16, - 16, 7, 45, 27, 45, 27, 27, 27, 27, 15, - 5, 6, 15, 6, 9, 30, 15, 15, 15, 37, - 34, 26, 35, 26, 42, 31, 42, 37, 39, 49, - 10, 8, 32, 12, 21, 16, 31, 45, 27, 45, - 27, 32, 38, 32, 11, 13, 34, 15, 38, 14, - 27, 37, 27, 22, 32, 49, 23, 40, 28, 40, - 26, 29, 33, 42, 38, 40, 40, 24, 24, 24, - 24, 41, 43, 35, 44, 47, 48, 27, 50, 35, - 51, 52, nil, 40, nil, 27, nil, 27, nil, nil, - 35, nil, nil, nil, nil, nil, nil, 35, nil, 35, - 40, nil, nil, nil, nil, 35, nil, nil, nil, nil, - 35, 35, nil, nil, nil, nil, nil, nil, nil, nil, - nil, nil, nil, nil, nil, nil, 35, 35 ] + 37, 37, 37, 27, 26, 42, 44, 32, 39, 8, + 1, 31, 12, 16, 16, 16, 41, 39, 5, 35, + 21, 21, 21, 27, 9, 27, 27, 27, 27, 15, + 10, 47, 15, 47, 36, 51, 15, 15, 15, 26, + 11, 26, 37, 39, 6, 44, 6, 44, 35, 31, + 16, 41, 52, 8, 39, 32, 12, 30, 27, 52, + 27, 31, 21, 36, 51, 13, 47, 15, 47, 7, + 27, 53, 27, 32, 7, 32, 53, 14, 26, 22, + 42, 30, 42, 52, 44, 23, 28, 29, 52, 42, + 42, 33, 32, 37, 40, 43, 45, 27, 46, 37, + 24, 24, 24, 24, 49, 27, 50, 27, 42, 55, + 56, 57, 37, nil, nil, nil, nil, nil, nil, nil, + 37, nil, 37, nil, nil, nil, nil, nil, 37, nil, + nil, nil, nil, nil, 37, 37, 42, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, + nil, nil, nil, nil, nil, nil, nil, nil, nil, 37, + nil, 37, nil, nil, 37, nil, 37 ] racc_goto_pointer = [ - nil, 15, nil, nil, nil, -59, 31, 13, -37, -55, - 46, -23, -35, 53, 23, 12, 5, nil, nil, nil, - nil, -46, 41, 43, 53, nil, -26, -9, -32, 11, - -107, -131, -140, -82, -105, -58, nil, -126, -121, -140, - -88, -19, -28, 40, -38, -57, nil, 5, -49, -96, - -71, -98, -24 ] + nil, 10, nil, nil, nil, -71, 44, 66, -35, -65, + 26, -47, -32, 53, 31, 12, 0, nil, nil, nil, + nil, -38, 47, 52, 66, nil, -28, -9, -24, 17, + -61, -130, -141, -73, nil, -132, -111, -58, nil, -159, + -92, -141, -87, -15, -27, 44, -34, -48, nil, 14, + -39, -110, -134, -148, nil, -63, -99, -14 ] racc_goto_default = [ nil, nil, 2, 8, 88, nil, nil, nil, nil, nil, nil, nil, 10, nil, nil, 50, nil, 21, 22, 23, 100, nil, nil, nil, nil, 67, nil, 75, nil, nil, - nil, nil, nil, 151, 152, 72, 129, nil, nil, 167, - nil, 76, nil, nil, nil, 80, 90, nil, nil, nil, - nil, nil, nil ] + nil, nil, nil, 151, 165, nil, 152, 72, 129, nil, + nil, 169, nil, 76, nil, nil, nil, 80, 90, nil, + nil, nil, nil, 204, 205, nil, nil, nil ] racc_reduce_table = [ 0, 0, :racc_error, - 0, 61, :_reduce_1, - 2, 61, :_reduce_2, - 0, 62, :_reduce_3, - 2, 62, :_reduce_4, - 1, 63, :_reduce_5, - 2, 63, :_reduce_6, - 0, 64, :_reduce_none, - 1, 64, :_reduce_none, - 5, 56, :_reduce_none, - 0, 65, :_reduce_10, - 0, 66, :_reduce_11, - 5, 57, :_reduce_12, - 2, 57, :_reduce_none, - 1, 71, :_reduce_14, - 2, 71, :_reduce_15, - 1, 58, :_reduce_none, - 2, 58, :_reduce_17, - 3, 58, :_reduce_none, - 2, 58, :_reduce_none, - 2, 58, :_reduce_20, - 2, 58, :_reduce_21, - 3, 58, :_reduce_22, - 2, 58, :_reduce_23, - 1, 58, :_reduce_24, - 1, 58, :_reduce_25, - 2, 58, :_reduce_none, - 1, 76, :_reduce_27, - 2, 76, :_reduce_28, - 2, 67, :_reduce_29, - 1, 67, :_reduce_none, - 1, 67, :_reduce_none, + 0, 64, :_reduce_1, + 2, 64, :_reduce_2, + 0, 65, :_reduce_3, + 2, 65, :_reduce_4, + 1, 66, :_reduce_5, + 2, 66, :_reduce_6, + 0, 67, :_reduce_none, 1, 67, :_reduce_none, - 3, 67, :_reduce_33, - 3, 67, :_reduce_34, - 3, 67, :_reduce_35, - 2, 67, :_reduce_36, - 2, 67, :_reduce_37, - 2, 67, :_reduce_38, - 2, 67, :_reduce_39, - 2, 67, :_reduce_40, - 2, 72, :_reduce_none, - 2, 72, :_reduce_42, - 2, 72, :_reduce_43, - 2, 72, :_reduce_44, - 2, 72, :_reduce_45, - 2, 72, :_reduce_46, - 1, 81, :_reduce_47, - 2, 81, :_reduce_48, - 1, 77, :_reduce_49, - 2, 77, :_reduce_50, - 3, 77, :_reduce_51, - 0, 84, :_reduce_none, - 1, 84, :_reduce_none, - 3, 80, :_reduce_54, + 5, 59, :_reduce_none, + 0, 68, :_reduce_10, + 0, 69, :_reduce_11, + 5, 60, :_reduce_12, + 2, 60, :_reduce_none, + 1, 74, :_reduce_14, + 2, 74, :_reduce_15, + 1, 61, :_reduce_none, + 2, 61, :_reduce_17, + 3, 61, :_reduce_none, + 2, 61, :_reduce_none, + 2, 61, :_reduce_20, + 2, 61, :_reduce_21, + 3, 61, :_reduce_22, + 2, 61, :_reduce_23, + 1, 61, :_reduce_24, + 1, 61, :_reduce_25, + 2, 61, :_reduce_none, + 1, 79, :_reduce_27, + 2, 79, :_reduce_28, + 2, 70, :_reduce_29, + 1, 70, :_reduce_none, + 1, 70, :_reduce_none, + 1, 70, :_reduce_none, + 3, 70, :_reduce_33, + 3, 70, :_reduce_34, + 3, 70, :_reduce_35, + 2, 70, :_reduce_36, + 2, 70, :_reduce_37, + 2, 70, :_reduce_38, + 2, 70, :_reduce_39, + 2, 70, :_reduce_40, + 2, 75, :_reduce_none, + 2, 75, :_reduce_42, + 2, 75, :_reduce_43, + 2, 75, :_reduce_44, + 2, 75, :_reduce_45, + 2, 75, :_reduce_46, + 1, 84, :_reduce_47, + 2, 84, :_reduce_48, + 1, 80, :_reduce_49, + 2, 80, :_reduce_50, + 3, 80, :_reduce_51, 0, 87, :_reduce_none, 1, 87, :_reduce_none, - 8, 73, :_reduce_57, - 5, 74, :_reduce_58, - 8, 74, :_reduce_59, - 1, 85, :_reduce_60, - 3, 85, :_reduce_61, - 1, 86, :_reduce_62, - 3, 86, :_reduce_63, - 0, 95, :_reduce_none, - 1, 95, :_reduce_none, - 1, 88, :_reduce_66, - 3, 88, :_reduce_67, - 3, 88, :_reduce_68, - 6, 88, :_reduce_69, - 3, 88, :_reduce_70, - 3, 88, :_reduce_71, - 0, 83, :_reduce_none, - 1, 83, :_reduce_73, - 1, 97, :_reduce_74, - 2, 97, :_reduce_75, - 1, 78, :_reduce_76, - 2, 78, :_reduce_77, - 3, 78, :_reduce_78, - 1, 90, :_reduce_none, + 3, 83, :_reduce_54, + 0, 90, :_reduce_none, 1, 90, :_reduce_none, - 0, 98, :_reduce_81, - 0, 99, :_reduce_82, - 5, 70, :_reduce_83, - 1, 100, :_reduce_84, - 2, 100, :_reduce_85, - 1, 79, :_reduce_86, - 2, 79, :_reduce_87, - 3, 79, :_reduce_88, + 8, 76, :_reduce_57, + 5, 77, :_reduce_58, + 8, 77, :_reduce_59, + 1, 88, :_reduce_60, + 3, 88, :_reduce_61, + 0, 93, :_reduce_none, + 1, 93, :_reduce_none, + 2, 89, :_reduce_64, + 4, 89, :_reduce_65, + 0, 100, :_reduce_none, + 1, 100, :_reduce_none, + 1, 91, :_reduce_68, + 3, 91, :_reduce_69, + 3, 91, :_reduce_70, + 6, 91, :_reduce_71, + 3, 91, :_reduce_72, + 3, 91, :_reduce_73, + 4, 92, :_reduce_74, + 0, 86, :_reduce_none, + 1, 86, :_reduce_76, + 1, 102, :_reduce_77, + 2, 102, :_reduce_78, + 1, 81, :_reduce_79, + 2, 81, :_reduce_80, + 3, 81, :_reduce_81, + 1, 95, :_reduce_none, + 1, 95, :_reduce_none, + 0, 103, :_reduce_84, + 0, 104, :_reduce_85, + 5, 73, :_reduce_86, + 1, 105, :_reduce_87, + 2, 105, :_reduce_88, 1, 82, :_reduce_89, - 1, 82, :_reduce_90, - 0, 102, :_reduce_none, - 1, 102, :_reduce_none, - 2, 59, :_reduce_none, - 2, 59, :_reduce_none, - 4, 101, :_reduce_95, - 1, 103, :_reduce_96, - 3, 103, :_reduce_97, - 1, 104, :_reduce_98, - 3, 104, :_reduce_99, - 5, 104, :_reduce_100, - 7, 104, :_reduce_101, - 4, 104, :_reduce_102, - 3, 104, :_reduce_103, - 1, 92, :_reduce_104, - 1, 92, :_reduce_105, - 1, 92, :_reduce_106, - 1, 93, :_reduce_107, - 3, 93, :_reduce_108, - 2, 93, :_reduce_109, - 4, 93, :_reduce_110, - 0, 105, :_reduce_111, - 0, 106, :_reduce_112, - 5, 94, :_reduce_113, - 3, 91, :_reduce_114, - 0, 107, :_reduce_115, - 3, 60, :_reduce_116, - 1, 68, :_reduce_none, - 0, 69, :_reduce_none, - 1, 69, :_reduce_none, - 1, 69, :_reduce_none, - 1, 69, :_reduce_none, - 1, 75, :_reduce_none, - 1, 75, :_reduce_none, - 0, 89, :_reduce_none, - 1, 89, :_reduce_none, - 1, 96, :_reduce_126 ] - -racc_reduce_n = 127 - -racc_shift_n = 217 + 2, 82, :_reduce_90, + 3, 82, :_reduce_91, + 1, 85, :_reduce_92, + 1, 85, :_reduce_93, + 0, 107, :_reduce_none, + 1, 107, :_reduce_none, + 2, 62, :_reduce_none, + 2, 62, :_reduce_none, + 4, 106, :_reduce_98, + 1, 108, :_reduce_99, + 3, 108, :_reduce_100, + 1, 109, :_reduce_101, + 3, 109, :_reduce_102, + 5, 109, :_reduce_103, + 7, 109, :_reduce_104, + 4, 109, :_reduce_105, + 3, 109, :_reduce_106, + 1, 97, :_reduce_107, + 1, 97, :_reduce_108, + 1, 97, :_reduce_109, + 1, 98, :_reduce_110, + 3, 98, :_reduce_111, + 2, 98, :_reduce_112, + 4, 98, :_reduce_113, + 1, 110, :_reduce_114, + 3, 110, :_reduce_115, + 2, 110, :_reduce_116, + 4, 110, :_reduce_117, + 1, 111, :_reduce_none, + 1, 111, :_reduce_none, + 1, 112, :_reduce_120, + 1, 112, :_reduce_121, + 0, 113, :_reduce_122, + 0, 114, :_reduce_123, + 5, 99, :_reduce_124, + 3, 96, :_reduce_125, + 0, 115, :_reduce_126, + 3, 63, :_reduce_127, + 1, 71, :_reduce_none, + 0, 72, :_reduce_none, + 1, 72, :_reduce_none, + 1, 72, :_reduce_none, + 1, 72, :_reduce_none, + 1, 78, :_reduce_none, + 1, 78, :_reduce_none, + 0, 94, :_reduce_none, + 1, 94, :_reduce_none, + 1, 101, :_reduce_137 ] + +racc_reduce_n = 138 + +racc_shift_n = 238 racc_token_table = { false => 0, @@ -1067,17 +1092,20 @@ def raise_parse_error(error_message, location) "," => 43, "|" => 44, "%prec" => 45, - "{" => 46, - "}" => 47, - "?" => 48, - "+" => 49, - "*" => 50, - "[" => 51, - "]" => 52, - "{...}" => 53, - "%empty" => 54 } - -racc_nt_base = 55 + "%if" => 46, + "{" => 47, + "}" => 48, + "?" => 49, + "+" => 50, + "*" => 51, + "%true" => 52, + "%false" => 53, + "[" => 54, + "]" => 55, + "{...}" => 56, + "%empty" => 57 } + +racc_nt_base = 58 racc_use_result_var = true @@ -1145,11 +1173,14 @@ def raise_parse_error(error_message, location) "\",\"", "\"|\"", "\"%prec\"", + "\"%if\"", "\"{\"", "\"}\"", "\"?\"", "\"+\"", "\"*\"", + "\"%true\"", + "\"%false\"", "\"[\"", "\"]\"", "\"{...}\"", @@ -1188,11 +1219,13 @@ def raise_parse_error(error_message, location) "rule_rhs_list", "\"-option@TAG\"", "rule_rhs", + "if_clause", + "\"-option@if_clause\"", "empty", "symbol", "named_ref", "parameterizing_suffix", - "parameterizing_args", + "parameterizing_rule_args", "midrule_action", "\"-option@named_ref\"", "string_as_id", @@ -1204,6 +1237,9 @@ def raise_parse_error(error_message, location) "\"-option@;\"", "rhs_list", "rhs", + "parameterizing_args", + "symbol_or_bool", + "bool", "@5", "@6", "@7" ] @@ -1646,30 +1682,36 @@ def _reduce_61(val, _values, result) end .,., +# reduce 62 omitted + +# reduce 63 omitted + module_eval(<<'.,.,', 'parser.y', 201) - def _reduce_62(val, _values, result) + def _reduce_64(val, _values, result) builder = val[0] + builder.if_clause = val[1] result = [builder] result end .,., -module_eval(<<'.,.,', 'parser.y', 206) - def _reduce_63(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 207) + def _reduce_65(val, _values, result) builder = val[2] + builder.if_clause = val[3] result = val[0].append(builder) result end .,., -# reduce 64 omitted +# reduce 66 omitted -# reduce 65 omitted +# reduce 67 omitted -module_eval(<<'.,.,', 'parser.y', 212) - def _reduce_66(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 214) + def _reduce_68(val, _values, result) reset_precs result = Grammar::ParameterizingRule::Rhs.new @@ -1677,8 +1719,8 @@ def _reduce_66(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 217) - def _reduce_67(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 219) + def _reduce_69(val, _values, result) token = val[1] token.alias_name = val[2] builder = val[0] @@ -1689,8 +1731,8 @@ def _reduce_67(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 225) - def _reduce_68(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 227) + def _reduce_70(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 @@ -1699,8 +1741,8 @@ def _reduce_68(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 231) - def _reduce_69(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 233) + def _reduce_71(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], lhs_tag: val[5]) result = builder @@ -1709,8 +1751,8 @@ def _reduce_69(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 237) - def _reduce_70(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 239) + def _reduce_72(val, _values, result) user_code = val[1] user_code.alias_name = val[2] builder = val[0] @@ -1721,8 +1763,8 @@ def _reduce_70(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 245) - def _reduce_71(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 247) + def _reduce_73(val, _values, result) sym = @grammar.find_symbol_by_id!(val[2]) @prec_seen = true builder = val[0] @@ -1733,137 +1775,145 @@ def _reduce_71(val, _values, result) end .,., -# reduce 72 omitted +module_eval(<<'.,.,', 'parser.y', 256) + def _reduce_74(val, _values, result) + result = Lrama::Lexer::Token::ControlSyntax.new(s_value: val[0], location: @lexer.location, condition: val[2]) -module_eval(<<'.,.,', 'parser.y', 253) - def _reduce_73(val, _values, result) - result = val[0].s_value result end .,., +# reduce 75 omitted + module_eval(<<'.,.,', 'parser.y', 260) - def _reduce_74(val, _values, result) + def _reduce_76(val, _values, result) + result = val[0].s_value + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 267) + def _reduce_77(val, _values, result) result = val[1] ? val[1].unshift(val[0]) : val result end .,., -module_eval(<<'.,.,', 'parser.y', 260) - def _reduce_75(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 267) + def _reduce_78(val, _values, result) result = val[1] ? val[1].unshift(val[0]) : val result end .,., -module_eval(<<'.,.,', 'parser.y', 255) - def _reduce_76(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 262) + def _reduce_79(val, _values, result) result = [{tag: nil, tokens: val[0]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 256) - def _reduce_77(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 263) + def _reduce_80(val, _values, result) result = [{tag: val[0], tokens: val[1]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 257) - def _reduce_78(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 264) + def _reduce_81(val, _values, result) result = val[0].append({tag: val[1], tokens: val[2]}) result end .,., -# reduce 79 omitted +# reduce 82 omitted -# reduce 80 omitted +# reduce 83 omitted -module_eval(<<'.,.,', 'parser.y', 263) - def _reduce_81(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 270) + def _reduce_84(val, _values, result) begin_c_declaration("}") result end .,., -module_eval(<<'.,.,', 'parser.y', 267) - def _reduce_82(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 274) + def _reduce_85(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 271) - def _reduce_83(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 278) + def _reduce_86(val, _values, result) result = val[2] result end .,., -module_eval(<<'.,.,', 'parser.y', 279) - def _reduce_84(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 286) + def _reduce_87(val, _values, result) result = val[1] ? val[1].unshift(val[0]) : val result end .,., -module_eval(<<'.,.,', 'parser.y', 279) - def _reduce_85(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 286) + def _reduce_88(val, _values, result) result = val[1] ? val[1].unshift(val[0]) : val result end .,., -module_eval(<<'.,.,', 'parser.y', 274) - def _reduce_86(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 281) + def _reduce_89(val, _values, result) result = [{tag: nil, tokens: val[0]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 275) - def _reduce_87(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 282) + def _reduce_90(val, _values, result) result = [{tag: val[0], tokens: val[1]}] result end .,., -module_eval(<<'.,.,', 'parser.y', 276) - def _reduce_88(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 283) + def _reduce_91(val, _values, result) result = val[0].append({tag: val[1], tokens: val[2]}) result end .,., -module_eval(<<'.,.,', 'parser.y', 278) - def _reduce_89(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 285) + def _reduce_92(val, _values, result) on_action_error("ident after %prec", val[0]) if @prec_seen result end .,., -module_eval(<<'.,.,', 'parser.y', 279) - def _reduce_90(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 286) + def _reduce_93(val, _values, result) on_action_error("char after %prec", val[0]) if @prec_seen result end .,., -# reduce 91 omitted +# reduce 94 omitted -# reduce 92 omitted +# reduce 95 omitted -# reduce 93 omitted +# reduce 96 omitted -# reduce 94 omitted +# reduce 97 omitted -module_eval(<<'.,.,', 'parser.y', 287) - def _reduce_95(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 294) + def _reduce_98(val, _values, result) lhs = val[0] lhs.alias_name = val[1] val[3].each do |builder| @@ -1876,8 +1926,8 @@ def _reduce_95(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 298) - def _reduce_96(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 305) + def _reduce_99(val, _values, result) builder = val[0] if !builder.line builder.line = @lexer.line - 1 @@ -1888,8 +1938,8 @@ def _reduce_96(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 306) - def _reduce_97(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 313) + def _reduce_100(val, _values, result) builder = val[2] if !builder.line builder.line = @lexer.line - 1 @@ -1900,8 +1950,8 @@ def _reduce_97(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 315) - def _reduce_98(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 322) + def _reduce_101(val, _values, result) reset_precs result = @grammar.create_rule_builder(@rule_counter, @midrule_action_counter) @@ -1909,8 +1959,8 @@ def _reduce_98(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 320) - def _reduce_99(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 327) + def _reduce_102(val, _values, result) token = val[1] token.alias_name = val[2] builder = val[0] @@ -1921,8 +1971,8 @@ def _reduce_99(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 328) - def _reduce_100(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 335) + def _reduce_103(val, _values, result) token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], alias_name: val[3], location: @lexer.location, args: [val[1]], lhs_tag: val[4]) builder = val[0] builder.add_rhs(token) @@ -1933,8 +1983,8 @@ def _reduce_100(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 336) - def _reduce_101(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 343) + def _reduce_104(val, _values, result) token = Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, alias_name: val[5], location: @lexer.location, args: val[3], lhs_tag: val[6]) builder = val[0] builder.add_rhs(token) @@ -1945,8 +1995,8 @@ def _reduce_101(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 344) - def _reduce_102(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 351) + def _reduce_105(val, _values, result) user_code = val[1] user_code.alias_name = val[2] user_code.tag = val[3] @@ -1958,8 +2008,8 @@ def _reduce_102(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 353) - def _reduce_103(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 360) + def _reduce_106(val, _values, result) sym = @grammar.find_symbol_by_id!(val[2]) @prec_seen = true builder = val[0] @@ -1970,57 +2020,103 @@ def _reduce_103(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 360) - def _reduce_104(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 367) + def _reduce_107(val, _values, result) result = "option" result end .,., -module_eval(<<'.,.,', 'parser.y', 361) - def _reduce_105(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 368) + def _reduce_108(val, _values, result) result = "nonempty_list" result end .,., -module_eval(<<'.,.,', 'parser.y', 362) - def _reduce_106(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 369) + def _reduce_109(val, _values, result) result = "list" result end .,., -module_eval(<<'.,.,', 'parser.y', 364) - def _reduce_107(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 371) + def _reduce_110(val, _values, result) result = [val[0]] result end .,., -module_eval(<<'.,.,', 'parser.y', 365) - def _reduce_108(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 372) + def _reduce_111(val, _values, result) result = val[0].append(val[2]) result end .,., -module_eval(<<'.,.,', 'parser.y', 366) - def _reduce_109(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 373) + def _reduce_112(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', 367) - def _reduce_110(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 374) + def _reduce_113(val, _values, result) result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] result end .,., -module_eval(<<'.,.,', 'parser.y', 371) - def _reduce_111(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 376) + def _reduce_114(val, _values, result) + result = [val[0]] + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 377) + def _reduce_115(val, _values, result) + result = val[0].append(val[2]) + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 378) + def _reduce_116(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', 379) + def _reduce_117(val, _values, result) + result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] + result + end +.,., + +# reduce 118 omitted + +# reduce 119 omitted + +module_eval(<<'.,.,', 'parser.y', 384) + def _reduce_120(val, _values, result) + result = Lrama::Lexer::Token::Ident.new(s_value: true) + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 385) + def _reduce_121(val, _values, result) + result = Lrama::Lexer::Token::Ident.new(s_value: false) + result + end +.,., + +module_eval(<<'.,.,', 'parser.y', 389) + def _reduce_122(val, _values, result) if @prec_seen on_action_error("multiple User_code after %prec", val[0]) if @code_after_prec @code_after_prec = true @@ -2031,31 +2127,31 @@ def _reduce_111(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 379) - def _reduce_112(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 397) + def _reduce_123(val, _values, result) end_c_declaration result end .,., -module_eval(<<'.,.,', 'parser.y', 383) - def _reduce_113(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 401) + def _reduce_124(val, _values, result) result = val[2] result end .,., -module_eval(<<'.,.,', 'parser.y', 386) - def _reduce_114(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 404) + def _reduce_125(val, _values, result) result = val[1].s_value result end .,., -module_eval(<<'.,.,', 'parser.y', 390) - def _reduce_115(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 408) + def _reduce_126(val, _values, result) begin_c_declaration('\Z') @grammar.epilogue_first_lineno = @lexer.line + 1 @@ -2063,8 +2159,8 @@ def _reduce_115(val, _values, result) end .,., -module_eval(<<'.,.,', 'parser.y', 395) - def _reduce_116(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 413) + def _reduce_127(val, _values, result) end_c_declaration @grammar.epilogue = val[2].s_value @@ -2072,26 +2168,26 @@ def _reduce_116(val, _values, result) end .,., -# reduce 117 omitted +# reduce 128 omitted -# reduce 118 omitted +# reduce 129 omitted -# reduce 119 omitted +# reduce 130 omitted -# reduce 120 omitted +# reduce 131 omitted -# reduce 121 omitted +# reduce 132 omitted -# reduce 122 omitted +# reduce 133 omitted -# reduce 123 omitted +# reduce 134 omitted -# reduce 124 omitted +# reduce 135 omitted -# reduce 125 omitted +# reduce 136 omitted -module_eval(<<'.,.,', 'parser.y', 412) - def _reduce_126(val, _values, result) +module_eval(<<'.,.,', 'parser.y', 430) + def _reduce_137(val, _values, result) result = Lrama::Lexer::Token::Ident.new(s_value: val[0]) result end diff --git a/parser.y b/parser.y index 52603a72..75351b9c 100644 --- a/parser.y +++ b/parser.y @@ -197,14 +197,16 @@ rule rule_args: IDENTIFIER { result = [val[0]] } | rule_args "," IDENTIFIER { result = val[0].append(val[2]) } - rule_rhs_list: rule_rhs + rule_rhs_list: rule_rhs if_clause? { builder = val[0] + builder.if_clause = val[1] result = [builder] } - | rule_rhs_list "|" rule_rhs + | rule_rhs_list "|" rule_rhs if_clause? { builder = val[2] + builder.if_clause = val[3] result = val[0].append(builder) } @@ -227,7 +229,7 @@ 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 ")" TAG? + | rule_rhs IDENTIFIER "(" parameterizing_rule_args ")" TAG? { builder = val[0] builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5]) @@ -250,6 +252,11 @@ rule result = builder } + if_clause: "%if" "(" IDENTIFIER ")" + { + result = Lrama::Lexer::Token::ControlSyntax.new(s_value: val[0], location: @lexer.location, condition: val[2]) + } + alias: # empty | string_as_id { result = val[0].s_value } @@ -362,11 +369,22 @@ rule | "+" { result = "nonempty_list" } | "*" { result = "list" } - parameterizing_args: symbol { result = [val[0]] } - | parameterizing_args ',' symbol { result = val[0].append(val[2]) } + parameterizing_rule_args: symbol { result = [val[0]] } + | parameterizing_args ',' symbol { result = val[0].append(val[2]) } + | symbol parameterizing_suffix { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[0])] } + | IDENTIFIER "(" parameterizing_args ")" { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] } + + parameterizing_args: symbol_or_bool { result = [val[0]] } + | parameterizing_args ',' symbol_or_bool { result = val[0].append(val[2]) } | symbol parameterizing_suffix { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[0])] } | IDENTIFIER "(" parameterizing_args ")" { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] } + symbol_or_bool: symbol + | bool + + bool: "%true" { result = Lrama::Lexer::Token::Ident.new(s_value: true) } + | "%false" { result = Lrama::Lexer::Token::Ident.new(s_value: false) } + midrule_action: "{" { if @prec_seen diff --git a/sig/lrama/grammar/parameterizing_rule/rhs.rbs b/sig/lrama/grammar/parameterizing_rule/rhs.rbs index 844557c9..bc207d52 100644 --- a/sig/lrama/grammar/parameterizing_rule/rhs.rbs +++ b/sig/lrama/grammar/parameterizing_rule/rhs.rbs @@ -7,6 +7,7 @@ module Lrama attr_reader precedence_sym: Grammar::Symbol? def initialize: () -> void + def skip?: (Grammar::Binding bindings) -> bool def resolve_user_code: (Grammar::Binding bindings) -> Lexer::Token::UserCode? end end diff --git a/sig/lrama/lexer/token/control_syntax.rbs b/sig/lrama/lexer/token/control_syntax.rbs new file mode 100644 index 00000000..c6f06114 --- /dev/null +++ b/sig/lrama/lexer/token/control_syntax.rbs @@ -0,0 +1,15 @@ +module Lrama + class Lexer + class Token + class ControlSyntax < Token + attr_accessor condition: Lexer::Token::Ident? + + def initialize: (s_value: String, location: Location, ?condition: Lexer::Token::Ident?) -> void + def if?: () -> bool + def true?: () -> bool + def false?: () -> bool + def condition_value: () -> String? + end + end + end +end diff --git a/spec/fixtures/parameterizing_rules/user_defined/if.y b/spec/fixtures/parameterizing_rules/user_defined/if.y new file mode 100644 index 00000000..ffacc836 --- /dev/null +++ b/spec/fixtures/parameterizing_rules/user_defined/if.y @@ -0,0 +1,43 @@ +/* + * 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 defined_rule(X, condition): /* empty */ + | X { $$ = $1; } %if(condition) + ; + +%% + +r_true : defined_rule(number, %true) + ; + +r_false : defined_rule(number, %false) + ; + +%% + +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 8b14ee5f..3eb2110d 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -2334,6 +2334,86 @@ ]) end end + + context "when if" do + let(:path) { "parameterizing_rules/user_defined/if.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: "defined_rule_number_true"), alias_name: nil, number: 5, tag: nil, term: false, token_id: 1, nullable: true), + Sym.new(id: T::Ident.new(s_value: "r_true"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 2, nullable: true), + Sym.new(id: T::Ident.new(s_value: "defined_rule_number_false"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 3, nullable: true), + Sym.new(id: T::Ident.new(s_value: "r_false"), 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!("r_true"), + grammar.find_symbol_by_s_value!("YYEOF"), + ], + token_code: nil, + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"), + lineno: 23, + ), + Rule.new( + id: 1, + lhs: grammar.find_symbol_by_s_value!("defined_rule_number_true"), + rhs: [], + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 23, + ), + Rule.new( + id: 2, + lhs: grammar.find_symbol_by_s_value!("defined_rule_number_true"), + rhs: [ + grammar.find_symbol_by_s_value!("number"), + ], + token_code: T::UserCode.new(s_value: " $$ = $1; "), + nullable: false, + precedence_sym: grammar.find_symbol_by_s_value!("number"), + lineno: 23, + ), + Rule.new( + id: 3, + lhs: grammar.find_symbol_by_s_value!("r_true"), + rhs: [ + grammar.find_symbol_by_s_value!("defined_rule_number_true"), + ], + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 23, + ), + Rule.new( + id: 4, + lhs: grammar.find_symbol_by_s_value!("defined_rule_number_false"), + rhs: [], + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 26, + ), + Rule.new( + id: 5, + lhs: grammar.find_symbol_by_s_value!("r_false"), + rhs: [ + grammar.find_symbol_by_s_value!("defined_rule_number_false") + ], + token_code: nil, + nullable: true, + precedence_sym: nil, + lineno: 26, + ), + ]) + end + end end context 'when error case' do