-
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.
- Loading branch information
Showing
11 changed files
with
876 additions
and
435 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Lrama | ||
class Grammar | ||
class ParameterizingRule < Struct.new(:name, :args, :rhs, keyword_init: true) | ||
def ==(other) | ||
self.class == other.class && | ||
self.name == other.name && | ||
self.args == other.args && | ||
self.rhs == other.rhs | ||
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,38 @@ | ||
require 'lrama/grammar/parameterizing_rules/builder' | ||
|
||
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, build_token, rule_counter, lhs_tag, user_code, precedence_sym, line) | ||
validate_argument_number!(token) | ||
rules = [] | ||
@rhs.each do |rhs| | ||
rules << Rule.new(id: rule_counter.increment, _lhs: build_token, _rhs: [rhs.symbol].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 | ||
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,32 @@ | ||
require 'lrama/grammar/parameterizing_rules/builder' | ||
|
||
module Lrama | ||
class Grammar | ||
class ParameterizingRuleResolver | ||
attr_reader :rules, :tokens | ||
|
||
def initialize | ||
@rules = [] | ||
@tokens = [] | ||
@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 << builder.build_rules(token, build_token, rule_counter, lhs_tag, user_code, precedence_sym, line) | ||
@tokens << build_token | ||
end | ||
@rules.flatten! | ||
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,15 @@ | ||
require 'lrama/grammar/parameterizing_rules/builder' | ||
|
||
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.