Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite lhs #247

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/lrama/grammar/parameterizing_rules/builder/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/lrama/grammar/parameterizing_rules/builder/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/grammar/rule.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion sig/lrama/grammar/rule.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lrama/grammar/rule_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down