-
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.
Support user define parameterizing rules
- Loading branch information
Showing
16 changed files
with
919 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,47 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRuleBuilder | ||
attr_reader :name, :args, :rhs, :term | ||
|
||
def initialize(name, args, rhs) | ||
@name = name | ||
@args = args | ||
@rhs = rhs | ||
@term = nil | ||
@required_args_count = args.count | ||
end | ||
|
||
def build_rules(token, build_token, rule_counter, lhs_tag, user_code, precedence_sym, line) | ||
validate_argument_number!(token) | ||
rules = [] | ||
@rhs.each do |rhs| | ||
@term = rhs_term(token, rhs) | ||
rules << Rule.new(id: rule_counter.increment, _lhs: build_token, _rhs: [@term].compact, lhs_tag: lhs_tag, token_code: rhs.user_code, precedence_sym: rhs.precedence_sym, lineno: line) | ||
end | ||
rules | ||
end | ||
|
||
def build_token(token) | ||
validate_argument_number!(token) | ||
Lrama::Lexer::Token::Ident.new(s_value: "#{name}_#{token.args.first&.s_value}") | ||
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 rhs_term(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,31 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRuleResolver | ||
attr_reader :rules, :tokens, :term | ||
|
||
def initialize | ||
@rules = [] | ||
@tokens = [] | ||
@term = nil | ||
@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, user_code, precedence_sym, line) | ||
@parameterizing_rule_builders.each do |builder| | ||
build_token = builder.build_token(token) | ||
@rules = @rules + builder.build_rules(token, build_token, rule_counter, lhs_tag, user_code, precedence_sym, line) | ||
@tokens << build_token | ||
@term = builder.term | ||
end | ||
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.