Skip to content

Commit

Permalink
Merge pull request #184 from ydah/extract-methods
Browse files Browse the repository at this point in the history
Extract Parameterizing method
  • Loading branch information
yui-knk authored Nov 4, 2023
2 parents dc1ad3c + 64603eb commit c4a9cfe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,19 +583,19 @@ def normalize_rules

def expand_parameterizing_rules(lhs, rhs, code, precedence_sym, lineno)
token = Lrama::Lexer::Token::Ident.new(s_value: rhs[0].s_value)
if rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.s_value == "?" }
if rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.option? }
option_token = Lrama::Lexer::Token::Ident.new(s_value: "option_#{rhs[0].s_value}")
add_term(id: option_token)
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: [option_token], code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: option_token, rhs: [], code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: option_token, rhs: [token], code: code, precedence_sym: precedence_sym, lineno: lineno)
elsif rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.s_value == "+" }
elsif rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.nonempty_list? }
nonempty_list_token = Lrama::Lexer::Token::Ident.new(s_value: "nonempty_list_#{rhs[0].s_value}")
add_term(id: nonempty_list_token)
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: [nonempty_list_token], code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: nonempty_list_token, rhs: [token], code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: nonempty_list_token, rhs: [nonempty_list_token, token], code: code, precedence_sym: precedence_sym, lineno: lineno)
elsif rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.s_value == "*" }
elsif rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.list? }
list_token = Lrama::Lexer::Token::Ident.new(s_value: "list_#{rhs[0].s_value}")
add_term(id: list_token)
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: [list_token], code: code, precedence_sym: precedence_sym, lineno: lineno)
Expand Down
11 changes: 11 additions & 0 deletions lib/lrama/lexer/token/parameterizing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ module Lrama
class Lexer
class Token
class Parameterizing < Token
def option?
self.s_value == "?"
end

def nonempty_list?
self.s_value == "+"
end

def list?
self.s_value == "*"
end
end
end
end
Expand Down

0 comments on commit c4a9cfe

Please sign in to comment.