-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from ydah/support-user-define
Support user define parameterizing rules
- Loading branch information
Showing
18 changed files
with
1,007 additions
and
440 deletions.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRule < Struct.new(:rules, :token, keyword_init: true) | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRuleBuilder | ||
attr_reader :name, :args, :rhs | ||
|
||
def initialize(name, args, rhs) | ||
@name = name | ||
@args = args | ||
@rhs = rhs | ||
@required_args_count = args.count | ||
end | ||
|
||
def build_rules(token, rule_counter, lhs_tag, line) | ||
validate_argument_number!(token) | ||
lhs = lhs_token(token) | ||
rules = [] | ||
@rhs.each do |rhs| | ||
rules << Rule.new(id: rule_counter.increment, _lhs: lhs, _rhs: [rhs_token(token, rhs)].compact, lhs_tag: lhs_tag, token_code: rhs.user_code, precedence_sym: rhs.precedence_sym, lineno: line) | ||
end | ||
ParameterizingRule.new(rules: rules, token: lhs) | ||
end | ||
|
||
private | ||
|
||
def validate_argument_number!(token) | ||
unless @required_args_count == token.args.count | ||
raise "Invalid number of arguments. expect: #{@required_args_count} actual: #{token.args.count}" | ||
end | ||
end | ||
|
||
def lhs_token(token) | ||
Lrama::Lexer::Token::Ident.new(s_value: "#{name}_#{token.args.map(&:s_value).join('_')}") | ||
end | ||
|
||
def rhs_token(token, rhs) | ||
return nil unless rhs.symbol | ||
term = rhs.symbol | ||
@args.each_with_index do |arg, index| | ||
term = token.args[index] if arg.s_value == rhs.symbol.s_value | ||
end | ||
term | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRuleResolver | ||
def initialize | ||
@parameterizing_rule_builders = [] | ||
end | ||
|
||
def add_parameterizing_rule_builder(builder) | ||
@parameterizing_rule_builders << builder | ||
end | ||
|
||
def defined?(name) | ||
@parameterizing_rule_builders.any? { |builder| builder.name == name } | ||
end | ||
|
||
def build_rules(token, rule_counter, lhs_tag, line) | ||
builder = @parameterizing_rule_builders.select { |b| b.name == token.s_value }.last | ||
raise "Unknown parameterizing rule #{token.s_value} at line #{token.line}" unless builder | ||
|
||
builder.build_rules(token, rule_counter, lhs_tag, line) | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRuleRhsBuilder | ||
attr_accessor :symbol, :user_code, :precedence_sym | ||
|
||
def initialize | ||
@symbol = nil | ||
@user_code = nil | ||
@precedence_sym = nil | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ class Lexer | |
%error-token | ||
%empty | ||
%code | ||
%rule | ||
) | ||
|
||
def initialize(text) | ||
|
Oops, something went wrong.