-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Inlining #369
Merged
Merged
Introduce Inlining #369
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6f2cd64
Define an Inlining grammar
ydah 3fc4d1a
Prepare the BNF for the inlining example
ydah e8253b6
Eliminate parse errors
ydah 4fd8a61
Fix grammar file
ydah c7a804f
Merge semantic actions and expand inline
ydah 9fe0c58
Fix steep check error
ydah 0cf305a
Change `%inline` to an optional attribute of`% rule`
ydah b08572c
Fix steep check failed for parameterizing rule resolver and rule builder
ydah bba8995
Remove unnecessary if statement in find_rule method
ydah 6928bc8
Refactor method to select not inline rules in Resolver.rb
ydah 7f07d1c
Fix failure to narrow results in select_not_inline_rules
ydah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,13 @@ def initialize(rule_counter, midrule_action_counter, position_in_original_rule_r | |
@user_code = nil | ||
@precedence_sym = nil | ||
@line = nil | ||
@rules = [] | ||
@rule_builders_for_parameterizing_rules = [] | ||
@rule_builders_for_derived_rules = [] | ||
@rule_builders_for_inline_rules = [] | ||
@parameterizing_rules = [] | ||
@inline_rules = [] | ||
@midrule_action_rules = [] | ||
end | ||
|
||
def add_rhs(rhs) | ||
|
@@ -52,12 +57,16 @@ def complete_input | |
|
||
def setup_rules(parameterizing_rule_resolver) | ||
preprocess_references unless @skip_preprocess_references | ||
process_rhs(parameterizing_rule_resolver) | ||
if rhs.any? { |token| parameterizing_rule_resolver.find_inline(token) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [note] In the future, we need to change this condition otherwise lrama can not handle production rules whose RHS include both parameterizing rules and inline rules. |
||
resolve_inline(parameterizing_rule_resolver) | ||
else | ||
process_rhs(parameterizing_rule_resolver) | ||
end | ||
build_rules | ||
end | ||
|
||
def rules | ||
@parameterizing_rules + @midrule_action_rules + @rules | ||
@parameterizing_rules + @inline_rules + @midrule_action_rules + @rules | ||
end | ||
|
||
private | ||
|
@@ -73,19 +82,25 @@ def preprocess_references | |
def build_rules | ||
tokens = @replaced_rhs | ||
|
||
rule = Rule.new( | ||
id: @rule_counter.increment, _lhs: lhs, _rhs: tokens, lhs_tag: lhs_tag, token_code: user_code, | ||
position_in_original_rule_rhs: @position_in_original_rule_rhs, precedence_sym: precedence_sym, lineno: line | ||
) | ||
@rules = [rule] | ||
@parameterizing_rules = @rule_builders_for_parameterizing_rules.map do |rule_builder| | ||
rule_builder.rules | ||
end.flatten | ||
@midrule_action_rules = @rule_builders_for_derived_rules.map do |rule_builder| | ||
rule_builder.rules | ||
end.flatten | ||
@midrule_action_rules.each do |r| | ||
r.original_rule = rule | ||
if tokens | ||
rule = Rule.new( | ||
id: @rule_counter.increment, _lhs: lhs, _rhs: tokens, lhs_tag: lhs_tag, token_code: user_code, | ||
position_in_original_rule_rhs: @position_in_original_rule_rhs, precedence_sym: precedence_sym, lineno: line | ||
) | ||
@rules = [rule] | ||
@parameterizing_rules = @rule_builders_for_parameterizing_rules.map do |rule_builder| | ||
rule_builder.rules | ||
end.flatten | ||
@midrule_action_rules = @rule_builders_for_derived_rules.map do |rule_builder| | ||
rule_builder.rules | ||
end.flatten | ||
@midrule_action_rules.each do |r| | ||
r.original_rule = rule | ||
end | ||
else | ||
@inline_rules = @rule_builders_for_inline_rules.map do |rule_builder| | ||
rule_builder.rules | ||
end.flatten | ||
end | ||
end | ||
|
||
|
@@ -103,7 +118,7 @@ def process_rhs(parameterizing_rule_resolver) | |
when Lrama::Lexer::Token::Ident | ||
@replaced_rhs << token | ||
when Lrama::Lexer::Token::InstantiateRule | ||
parameterizing_rule = parameterizing_rule_resolver.find(token) | ||
parameterizing_rule = parameterizing_rule_resolver.find_rule(token) | ||
raise "Unexpected token. #{token}" unless parameterizing_rule | ||
|
||
bindings = Binding.new(parameterizing_rule, token.args) | ||
|
@@ -157,6 +172,41 @@ def lhs_s_value(token, bindings) | |
"#{token.rule_name}_#{s_values.join('_')}" | ||
end | ||
|
||
def resolve_inline(parameterizing_rule_resolver) | ||
rhs.each_with_index do |token, i| | ||
if inline_rule = parameterizing_rule_resolver.find_inline(token) | ||
inline_rule.rhs_list.each_with_index do |inline_rhs| | ||
rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, lhs_tag: lhs_tag, skip_preprocess_references: true) | ||
resolve_inline_rhs(rule_builder, inline_rhs, i) | ||
rule_builder.lhs = lhs | ||
rule_builder.line = line | ||
rule_builder.user_code = replace_inline_user_code(inline_rhs, i) | ||
rule_builder.complete_input | ||
rule_builder.setup_rules(parameterizing_rule_resolver) | ||
@rule_builders_for_inline_rules << rule_builder | ||
end | ||
end | ||
end | ||
end | ||
|
||
def resolve_inline_rhs(rule_builder, inline_rhs, index) | ||
rhs.each_with_index do |token, i| | ||
if index == i | ||
inline_rhs.symbols.each { |sym| rule_builder.add_rhs(sym) } | ||
else | ||
rule_builder.add_rhs(token) | ||
end | ||
end | ||
end | ||
|
||
def replace_inline_user_code(inline_rhs, index) | ||
return user_code if inline_rhs.user_code.nil? | ||
return user_code if user_code.nil? | ||
|
||
code = user_code.s_value.gsub(/\$#{index + 1}/, inline_rhs.user_code.s_value) | ||
Lrama::Lexer::Token::UserCode.new(s_value: code, location: user_code.location) | ||
end | ||
|
||
def numberize_references | ||
# Bison n'th component is 1-origin | ||
(rhs + [user_code]).compact.each.with_index(1) do |token, i| | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ class Lexer | |
%code | ||
%rule | ||
%no-stdlib | ||
%inline | ||
) | ||
|
||
def initialize(grammar_file) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the result o line 31 is not used by line 32 and overwrote by line 32. Is something wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😨 My apologies... I updated it.