lib/lrama/grammar/rule_builder.rb in lrama-0.5.10 vs lib/lrama/grammar/rule_builder.rb in lrama-0.5.11

- old
+ new

@@ -1,21 +1,22 @@ require 'lrama/grammar/parameterizing_rules/builder' module Lrama class Grammar class RuleBuilder - attr_accessor :lhs, :line + attr_accessor :lhs, :lhs_tag, :line attr_reader :rhs, :user_code, :precedence_sym def initialize(rule_counter, midrule_action_counter, position_in_original_rule_rhs = nil, skip_preprocess_references: false) @rule_counter = rule_counter @midrule_action_counter = midrule_action_counter @position_in_original_rule_rhs = position_in_original_rule_rhs @skip_preprocess_references = skip_preprocess_references @lhs = nil @rhs = [] + @lhs_tag = nil @user_code = nil @precedence_sym = nil @line = nil @rule_builders_for_derived_rules = [] end @@ -79,26 +80,20 @@ end def build_rules tokens = @replaced_rhs - # Expand Parameterizing rules - if tokens.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) } - @rules = @parameterizing_rules - @midrule_action_rules = [] - else - rule = Rule.new( - id: @rule_counter.increment, _lhs: lhs, _rhs: tokens, token_code: user_code, - position_in_original_rule_rhs: @position_in_original_rule_rhs, precedence_sym: precedence_sym, lineno: line - ) - @rules = [rule] - @midrule_action_rules = @rule_builders_for_derived_rules.map do |rule_builder| - rule_builder.rules - end.flatten - @midrule_action_rules.each do |r| - r.original_rule = rule - end + rule = Rule.new( + id: @rule_counter.increment, _lhs: lhs, _rhs: tokens, token_code: user_code, + position_in_original_rule_rhs: @position_in_original_rule_rhs, precedence_sym: precedence_sym, lineno: line + ) + @rules = [rule] + @midrule_action_rules = @rule_builders_for_derived_rules.map do |rule_builder| + rule_builder.rules + end.flatten + @midrule_action_rules.each do |r| + r.original_rule = rule end end # rhs is a mixture of variety type of tokens like `Ident`, `Parameterizing`, `UserCode` and so on. # `#process_rhs` replaces some kind of tokens to `Ident` so that all `@replaced_rhs` are `Ident` or `Char`. @@ -113,11 +108,14 @@ when Lrama::Lexer::Token::Char @replaced_rhs << token when Lrama::Lexer::Token::Ident @replaced_rhs << token when Lrama::Lexer::Token::Parameterizing - @parameterizing_rules = ParameterizingRules::Builder.new(token, @rule_counter, lhs, user_code, precedence_sym, line).build - @replaced_rhs << token + parameterizing = ParameterizingRules::Builder.new(token, @rule_counter, @lhs_tag, user_code, precedence_sym, line) + parameterizing.build.each do |r| + @parameterizing_rules << r + end + @replaced_rhs << parameterizing.build_token when Lrama::Lexer::Token::UserCode prefix = token.referred ? "@" : "$@" new_token = Lrama::Lexer::Token::Ident.new(s_value: prefix + @midrule_action_counter.increment.to_s) @replaced_rhs << new_token