Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Nov 14, 2023
1 parent b56756e commit 5cf1d18
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 61 deletions.
24 changes: 21 additions & 3 deletions lib/lrama/grammar/parameterizing_rules/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,30 @@ def initialize(token, rule_counter, lhs, user_code, precedence_sym, line)
end

def build
if RULES.key?(@key)
RULES[@key].new(@token, @rule_counter, @lhs, @user_code, @precedence_sym, @line).build
else
validate_key!
rules = inside_rules
unless rules.empty?
@token.args.first.s_value = rules.last.lhs.s_value
end
rules << RULES[@key].new(@token, @rule_counter, @lhs, @user_code, @precedence_sym, @line).build
rules.flatten
end

private

def validate_key!
unless RULES.key?(@key)
raise "Parameterizing rule does not exist. `#{@key}`"
end
end

def inside_rules
if @token.args.any? { |arg| arg.is_a?(Lrama::Lexer::Token::Parameterizing) }
ParameterizingRules::Builder.new(@token.args.first, @rule_counter, @lhs, @user_code, @precedence_sym, @line).build
else
[]
end
end
end
end
end
Expand Down
99 changes: 48 additions & 51 deletions lib/lrama/parser.rb

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

6 changes: 1 addition & 5 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,6 @@ rule
token = val[1]
builder = val[0]
builder.add_rhs(token)
token.args.each {|arg|
builder.add_rhs(arg)
}
result = builder
}
| rhs "{"
Expand Down Expand Up @@ -366,7 +363,6 @@ rule
result = builder
}


parameterizing_rules: IDENTIFIER parameterizing_suffix
{
token = Lrama::Lexer::Token::Parameterizing.new(s_value: val[1], location: @lexer.location, args: [val[0]])
Expand All @@ -379,7 +375,7 @@ rule
token = Lrama::Lexer::Token::Parameterizing.new(s_value: val[0].s_value, location: @lexer.location, args: [val[2]])
result = token
}
| IDENTIFIER "(" symbol "," parameterizing_rule ")"
| IDENTIFIER "(" symbol "," symbol ")"
{
token = Lrama::Lexer::Token::Parameterizing.new(s_value: val[0].s_value, location: @lexer.location, args: [val[2], val[4]])
builder = val[0]
Expand Down
3 changes: 1 addition & 2 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,7 @@
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: "program"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 1, nullable: true),
Sym.new(id: T::Ident.new(s_value: "list_number"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 2, nullable: true),
Sym.new(id: T::Ident.new(s_value: "alias"), alias_name: nil, number: 8, tag: nil, term: false, token_id: 3, nullable: true),
Sym.new(id: T::Ident.new(s_value: "list_number_alias"), alias_name: nil, number: 9, tag: nil, term: false, token_id: 4, nullable: true),
Sym.new(id: T::Ident.new(s_value: "option_list_number"), alias_name: nil, number: 8, tag: nil, term: false, token_id: 3, nullable: true),
])

expect(grammar.rules).to eq([
Expand Down

0 comments on commit 5cf1d18

Please sign in to comment.