Skip to content

Commit

Permalink
Merge pull request ruby#214 from yui-knk/extract_process_rhs
Browse files Browse the repository at this point in the history
Extract `rhs` replacement logic into `#process_rhs`
  • Loading branch information
yui-knk authored Nov 10, 2023
2 parents 04010c2 + 49c43c0 commit eddf032
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
46 changes: 34 additions & 12 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def initialize(rule_counter, midrule_action_counter)
@user_code = nil
@precedence_sym = nil
@line = nil
@code_to_new_token = {}
end

def add_rhs(rhs)
Expand Down Expand Up @@ -55,20 +54,15 @@ def preprocess_references
end

def midrule_action_rules
@midrule_action_rules ||= rhs.select do |token|
token.is_a?(Lrama::Lexer::Token::UserCode)
end.each_with_index.map do |code, i|
prefix = code.referred ? "@" : "$@"
new_token = Lrama::Lexer::Token::Ident.new(s_value: prefix + @midrule_action_counter.increment.to_s)
@code_to_new_token[code] = new_token
Rule.new(id: @rule_counter.increment, lhs: new_token, rhs: [], token_code: code, lineno: code.line)
end
process_rhs

@midrule_action_rules
end

def rhs_with_new_tokens
rhs.map do |token|
@code_to_new_token[token] || token
end
process_rhs

@replaced_rhs
end

def build_rules
Expand All @@ -84,6 +78,34 @@ def build_rules

private

# 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`.
def process_rhs
return @replaced_rhs if @replaced_rhs

@replaced_rhs = []
@midrule_action_rules = []

rhs.each_with_index do |token|
case token
when Lrama::Lexer::Token::Char
@replaced_rhs << token
when Lrama::Lexer::Token::Ident
@replaced_rhs << token
when Lrama::Lexer::Token::Parameterizing
# TODO: Expand Parameterizing here
@replaced_rhs << 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
@midrule_action_rules << Rule.new(id: @rule_counter.increment, lhs: new_token, rhs: [], token_code: token, lineno: token.line)
else
raise "Unexpected token. #{token}"
end
end
end

def expand_parameterizing_rules
rhs = rhs_with_new_tokens
rules = []
Expand Down
2 changes: 1 addition & 1 deletion sig/lrama/grammar/rule_builder.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module Lrama
attr_accessor extracted_action_number: Integer

@user_code: Lexer::Token::UserCode?
@code_to_new_token: Hash[Lexer::Token, Lexer::Token]

def initialize: (Counter rule_counter, Counter midrule_action_counter) -> void
def add_rhs: (Lexer::Token rhs) -> void
Expand All @@ -22,6 +21,7 @@ module Lrama

private

def process_rhs: () -> void
def expand_parameterizing_rules: () -> Array[Rule]
def numberize_references: () -> void
def setup_references: () -> void
Expand Down

0 comments on commit eddf032

Please sign in to comment.