Skip to content

Commit

Permalink
Change to use RuleBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Nov 6, 2023
1 parent 0ba4c68 commit 1398220
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,15 +465,15 @@ def expand_parameterizing_rules(lhs, rhs, code, precedence_sym, lineno)
@rules << Rule.new(id: @rules.count, lhs: list_token, rhs: [], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: list_token, rhs: [list_token, token], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
elsif rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.separated_nonempty_list? }
separated_list_token = Lrama::Lexer::Token::Ident.new(s_value: "separated_list_#{rhs[0].s_value}")
add_term(id: separated_list_token)
separated_list_token = Lrama::Lexer::Token::Ident.new(s_value: "separated_nonempty_list_#{rhs[0].s_value}")
add_nterm(id: separated_list_token)
add_term(id: rhs[2])
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: [separated_list_token], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: separated_list_token, rhs: [token], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: separated_list_token, rhs: [separated_list_token, rhs[2], token], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
elsif rhs.any? {|r| r.is_a?(Lrama::Lexer::Token::Parameterizing) && r.separated_list? }
separated_list_token = Lrama::Lexer::Token::Ident.new(s_value: "separated_list_#{rhs[0].s_value}")
add_term(id: separated_list_token)
add_nterm(id: separated_list_token)
add_term(id: rhs[2])
@rules << Rule.new(id: @rules.count, lhs: lhs, rhs: [separated_list_token], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
@rules << Rule.new(id: @rules.count, lhs: separated_list_token, rhs: [], token_code: code, precedence_sym: precedence_sym, lineno: lineno)
Expand Down
27 changes: 15 additions & 12 deletions lib/lrama/parser.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ rule
| parameterizing_separated_prefix symbol "," rhs ")"
{
token = Lrama::Lexer::Token::Parameterizing.new(s_value: val[0].chop)
result = val[3].append(token).append(val[1])
builder = val[3]
builder.add_rhs(token)
builder.add_rhs(val[1])
result = builder
}
| rhs "{"
{
Expand Down
28 changes: 15 additions & 13 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,9 @@
grammar = Lrama::Parser.new(y, path).parse

expect(grammar.nterms.sort_by(&:number)).to eq([
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 5, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "separated_nonempty_list_number"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 2, nullable: false),
])

expect(grammar.rules).to eq([
Expand All @@ -853,16 +854,16 @@
id: 1,
lhs: grammar.find_symbol_by_s_value!("program"),
rhs: [
grammar.find_symbol_by_s_value!("separated_list_number"),
grammar.find_symbol_by_s_value!("separated_nonempty_list_number"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("separated_list_number"),
precedence_sym: nil,
lineno: 20,
),
Rule.new(
id: 2,
lhs: grammar.find_symbol_by_s_value!("separated_list_number"),
lhs: grammar.find_symbol_by_s_value!("separated_nonempty_list_number"),
rhs: [
grammar.find_symbol_by_s_value!("number"),
],
Expand All @@ -873,10 +874,10 @@
),
Rule.new(
id: 3,
lhs: grammar.find_symbol_by_s_value!("separated_list_number"),
lhs: grammar.find_symbol_by_s_value!("separated_nonempty_list_number"),
rhs: [
grammar.find_symbol_by_s_value!("separated_list_number"),
grammar.find_symbol_by_number!(5),
grammar.find_symbol_by_s_value!("separated_nonempty_list_number"),
grammar.find_symbol_by_number!(4),
grammar.find_symbol_by_s_value!("number"),
],
token_code: nil,
Expand All @@ -893,8 +894,9 @@
grammar = Lrama::Parser.new(y, path).parse

expect(grammar.nterms.sort_by(&:number)).to eq([
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 5, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "separated_list_number"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 1, nullable: true),
Sym.new(id: T::Ident.new(s_value: "program"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 2, nullable: true),
])

expect(grammar.rules).to eq([
Expand All @@ -917,8 +919,8 @@
grammar.find_symbol_by_s_value!("separated_list_number"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("separated_list_number"),
nullable: true,
precedence_sym: nil,
lineno: 20,
),
Rule.new(
Expand Down Expand Up @@ -946,7 +948,7 @@
lhs: grammar.find_symbol_by_s_value!("separated_list_number"),
rhs: [
grammar.find_symbol_by_s_value!("separated_list_number"),
grammar.find_symbol_by_number!(5),
grammar.find_symbol_by_number!(4),
grammar.find_symbol_by_s_value!("number"),
],
token_code: nil,
Expand Down

0 comments on commit 1398220

Please sign in to comment.