Skip to content

Commit

Permalink
Implement parameterizing rules for option
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Nov 4, 2023
1 parent a0f462d commit e7ef48f
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 215 deletions.
12 changes: 11 additions & 1 deletion lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,17 @@ def normalize_rules
end

c = code ? Code.new(type: :user_code, token_code: code) : nil
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: rhs2, code: c, precedence_sym: precedence_sym, lineno: lineno)
# Expand Parameterizing rules
if rhs2.any? {|r| r.type == Token::Option }
option_token = Token.new(type: Token::Ident, s_value: "option_#{rhs2[0].s_value}")
token = Token.new(type: Token::Ident, s_value: rhs2[0].s_value)
add_term(id: option_token)
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: [option_token], code: c, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: option_token, rhs: [], code: c, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: option_token, rhs: [token], code: c, precedence_sym: precedence_sym, lineno: lineno)
else
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: rhs2, code: c, precedence_sym: precedence_sym, lineno: lineno)
end

add_nterm(id: lhs)
a.each do |new_token, _|
Expand Down
2 changes: 2 additions & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def lex_token
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/#{PERCENT_TOKENS.join('|')}/)
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/\?/)
return [@scanner.matched, @scanner.matched]
when @scanner.scan(/<\w+>/)
return [:TAG, build_token(type: Token::Tag, s_value: @scanner.matched)]
when @scanner.scan(/'.'/)
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def self.define_type(name)
define_type(:Tag) # <int>
define_type(:Ident) # api.pure, tNUMBER
define_type(:Char) # '+'
define_type(:Option) # actual?
end
end
end
Loading

0 comments on commit e7ef48f

Please sign in to comment.