diff --git a/lib/lrama/grammar.rb b/lib/lrama/grammar.rb index b58b4901..9664caeb 100644 --- a/lib/lrama/grammar.rb +++ b/lib/lrama/grammar.rb @@ -373,10 +373,10 @@ def append_special_symbols # def normalize_rules # 1. Add $accept rule to the top of rules - accept = find_symbol_by_s_value!("$accept") - eof = find_symbol_by_number!(0) + accept = @accept_symbol + eof = @eof_symbol lineno = @rule_builders.first ? @rule_builders.first.line : 0 - @rules << Rule.new(id: @rule_counter.increment, lhs: accept, _rhs: [@rule_builders.first.lhs, eof], token_code: nil, lineno: lineno) + @rules << Rule.new(id: @rule_counter.increment, _lhs: accept.id, _rhs: [@rule_builders.first.lhs, eof.id], token_code: nil, lineno: lineno) setup_rules @@ -387,12 +387,12 @@ def normalize_rules end builder.rules.each do |rule| - add_nterm(id: rule.lhs) + add_nterm(id: rule._lhs) @rules << rule end builder.midrule_action_rules.each do |rule| - add_nterm(id: rule.lhs) + add_nterm(id: rule._lhs) end end end @@ -405,8 +405,6 @@ def collect_symbols add_term(id: s) when Lrama::Lexer::Token # skip - when Symbol - # skip else raise "Unknown class: #{s}" end @@ -488,7 +486,7 @@ def fill_symbol_number def replace_token_with_symbol @rules.each do |rule| - rule.lhs = token_to_symbol(rule.lhs) + rule.lhs = token_to_symbol(rule._lhs) if rule._lhs rule.rhs = rule._rhs.map do |t| token_to_symbol(t) @@ -500,8 +498,6 @@ def token_to_symbol(token) case token when Lrama::Lexer::Token find_symbol_by_id!(token) - when Symbol - token else raise "Unknown class: #{token}" end diff --git a/lib/lrama/grammar/parameterizing_rules/builder/list.rb b/lib/lrama/grammar/parameterizing_rules/builder/list.rb index 44ecb12f..f8141604 100644 --- a/lib/lrama/grammar/parameterizing_rules/builder/list.rb +++ b/lib/lrama/grammar/parameterizing_rules/builder/list.rb @@ -8,9 +8,9 @@ def build rules = [] list_token = Lrama::Lexer::Token::Ident.new(s_value: "list_#{@token.s_value}") - rules << Rule.new(id: @rule_counter.increment, lhs: @lhs, _rhs: [list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: list_token, _rhs: [], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: list_token, _rhs: [list_token, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: @lhs, _rhs: [list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: list_token, _rhs: [], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: list_token, _rhs: [list_token, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) rules end end diff --git a/lib/lrama/grammar/parameterizing_rules/builder/nonempty_list.rb b/lib/lrama/grammar/parameterizing_rules/builder/nonempty_list.rb index cbb99af8..142d6c15 100644 --- a/lib/lrama/grammar/parameterizing_rules/builder/nonempty_list.rb +++ b/lib/lrama/grammar/parameterizing_rules/builder/nonempty_list.rb @@ -8,9 +8,9 @@ def build rules = [] nonempty_list_token = Lrama::Lexer::Token::Ident.new(s_value: "nonempty_list_#{@token.s_value}") - rules << Rule.new(id: @rule_counter.increment, lhs: @lhs, _rhs: [nonempty_list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: nonempty_list_token, _rhs: [@token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: nonempty_list_token, _rhs: [nonempty_list_token, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: @lhs, _rhs: [nonempty_list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: nonempty_list_token, _rhs: [@token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: nonempty_list_token, _rhs: [nonempty_list_token, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) rules end end diff --git a/lib/lrama/grammar/parameterizing_rules/builder/option.rb b/lib/lrama/grammar/parameterizing_rules/builder/option.rb index 707e6e8c..f751609e 100644 --- a/lib/lrama/grammar/parameterizing_rules/builder/option.rb +++ b/lib/lrama/grammar/parameterizing_rules/builder/option.rb @@ -8,9 +8,9 @@ def build rules = [] option_token = Lrama::Lexer::Token::Ident.new(s_value: "option_#{@token.s_value}") - rules << Rule.new(id: @rule_counter.increment, lhs: @lhs, _rhs: [option_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: option_token, _rhs: [], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: option_token, _rhs: [@token], token_code: @ser_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: @lhs, _rhs: [option_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: option_token, _rhs: [], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: option_token, _rhs: [@token], token_code: @ser_code, precedence_sym: @precedence_sym, lineno: @line) rules end end diff --git a/lib/lrama/grammar/parameterizing_rules/builder/separated_list.rb b/lib/lrama/grammar/parameterizing_rules/builder/separated_list.rb index fe4e293f..95f81564 100644 --- a/lib/lrama/grammar/parameterizing_rules/builder/separated_list.rb +++ b/lib/lrama/grammar/parameterizing_rules/builder/separated_list.rb @@ -15,10 +15,10 @@ def build rules = [] separated_list_token = Lrama::Lexer::Token::Ident.new(s_value: "separated_list_#{@token.s_value}") - rules << Rule.new(id: @rule_counter.increment, lhs: @lhs, _rhs: [separated_list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: separated_list_token, _rhs: [], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: separated_list_token, _rhs: [@token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: separated_list_token, _rhs: [separated_list_token, @separator, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: @lhs, _rhs: [separated_list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: separated_list_token, _rhs: [], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: separated_list_token, _rhs: [@token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: separated_list_token, _rhs: [separated_list_token, @separator, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) rules end end diff --git a/lib/lrama/grammar/parameterizing_rules/builder/separated_nonempty_list.rb b/lib/lrama/grammar/parameterizing_rules/builder/separated_nonempty_list.rb index 785e75d2..64662180 100644 --- a/lib/lrama/grammar/parameterizing_rules/builder/separated_nonempty_list.rb +++ b/lib/lrama/grammar/parameterizing_rules/builder/separated_nonempty_list.rb @@ -15,9 +15,9 @@ def build rules = [] separated_list_token = Lrama::Lexer::Token::Ident.new(s_value: "separated_nonempty_list_#{@token.s_value}") - rules << Rule.new(id: @rule_counter.increment, lhs: @lhs, _rhs: [separated_list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: separated_list_token, _rhs: [@token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) - rules << Rule.new(id: @rule_counter.increment, lhs: separated_list_token, _rhs: [separated_list_token, @separator, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: @lhs, _rhs: [separated_list_token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: separated_list_token, _rhs: [@token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) + rules << Rule.new(id: @rule_counter.increment, _lhs: separated_list_token, _rhs: [separated_list_token, @separator, @token], token_code: @user_code, precedence_sym: @precedence_sym, lineno: @line) rules end end diff --git a/lib/lrama/grammar/rule.rb b/lib/lrama/grammar/rule.rb index a825d544..13b44c00 100644 --- a/lib/lrama/grammar/rule.rb +++ b/lib/lrama/grammar/rule.rb @@ -1,7 +1,7 @@ module Lrama class Grammar # _rhs holds original RHS element. Use rhs to refer to Symbol. - class Rule < Struct.new(:id, :lhs, :_rhs, :rhs, :token_code, :position_in_original_rule_rhs, :nullable, :precedence_sym, :lineno, keyword_init: true) + class Rule < Struct.new(:id, :_lhs, :lhs, :_rhs, :rhs, :token_code, :position_in_original_rule_rhs, :nullable, :precedence_sym, :lineno, keyword_init: true) attr_accessor :original_rule def ==(other) diff --git a/lib/lrama/grammar/rule_builder.rb b/lib/lrama/grammar/rule_builder.rb index 58078d72..80cd0c2c 100644 --- a/lib/lrama/grammar/rule_builder.rb +++ b/lib/lrama/grammar/rule_builder.rb @@ -87,7 +87,7 @@ def build_rules @midrule_action_rules = [] else rule = Rule.new( - id: @rule_counter.increment, lhs: lhs, _rhs: tokens, token_code: user_code, + 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] diff --git a/sig/lrama/grammar/rule.rbs b/sig/lrama/grammar/rule.rbs index 02edc7a2..8de89c19 100644 --- a/sig/lrama/grammar/rule.rbs +++ b/sig/lrama/grammar/rule.rbs @@ -4,7 +4,7 @@ module Lrama attr_accessor original_rule: Rule def initialize: ( - ?id: untyped, ?lhs: untyped, ?_rhs: untyped, ?rhs: untyped, + ?id: untyped, ?_lhs: untyped, ?lhs: untyped, ?_rhs: untyped, ?rhs: untyped, ?token_code: untyped, ?position_in_original_rule_rhs: untyped, ?nullable: untyped, ?precedence_sym: untyped, ?lineno: untyped ) -> void diff --git a/spec/lrama/grammar/rule_builder_spec.rb b/spec/lrama/grammar/rule_builder_spec.rb index 3634afa5..0f184eca 100644 --- a/spec/lrama/grammar/rule_builder_spec.rb +++ b/spec/lrama/grammar/rule_builder_spec.rb @@ -327,10 +327,10 @@ rules = rule_builder.midrule_action_rules expect(rules.count).to eq 2 - expect(rules[0].lhs.s_value).to eq '@1' + expect(rules[0]._lhs.s_value).to eq '@1' expect(rules[0].token_code.s_value).to eq '$1' expect(rules[0].original_rule).to eq rule - expect(rules[1].lhs.s_value).to eq '$@2' + expect(rules[1]._lhs.s_value).to eq '$@2' expect(rules[1].token_code.s_value).to eq '$2 + $3' expect(rules[1].original_rule).to eq rule end