Skip to content

Commit

Permalink
Merge setup_references into numberize_references
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Nov 12, 2023
1 parent adc5b44 commit c19f571
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def freeze_rhs

def preprocess_references
numberize_references
setup_references
end

def build_rules
Expand Down Expand Up @@ -137,7 +136,8 @@ def process_rhs
end

def numberize_references
(rhs + [user_code]).compact.each do |token|
# Bison n'th component is 1-origin
(rhs + [user_code]).compact.each.with_index(1) do |token, i|
next unless token.is_a?(Lrama::Lexer::Token::UserCode)

token.references.each do |ref|
Expand All @@ -154,26 +154,15 @@ def numberize_references
ref.index = referring_symbol[1] + 1
end
end
end
end
end

def setup_references
# Bison n'th component is 1-origin
(rhs + [user_code]).compact.each.with_index(1) do |token, i|
if token.is_a?(Lrama::Lexer::Token::UserCode)
token.references.each do |ref|
next if ref.type == :at
# $$, $n, @$, @n can be used in any actions

if ref.name == "$"
# TODO: Should be postponed after middle actions are extracted?
elsif ref.index
raise "Can not refer following component. #{ref.index} >= #{i}. #{token}" if ref.index >= i
rhs[ref.index - 1].referred = true
else
raise "[BUG] Unreachable #{token}."
end
# TODO: Need to check index of @ too?
next if ref.type == :at

if ref.index
# TODO: Prohibit $0 even so Bison allows it?
# See: https://www.gnu.org/software/bison/manual/html_node/Actions.html
raise "Can not refer following component. #{ref.index} >= #{i}. #{token}" if ref.index >= i
rhs[ref.index - 1].referred = true
end
end
end
Expand Down

0 comments on commit c19f571

Please sign in to comment.