Skip to content

Commit

Permalink
Provide standard library as standard.y
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Jan 12, 2024
1 parent 6cc1b54 commit 2ca1f91
Show file tree
Hide file tree
Showing 33 changed files with 839 additions and 1,008 deletions.
22 changes: 15 additions & 7 deletions lib/lrama/grammar/parameterizing_rule/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def add_parameterizing_rule(rule)
@rules << rule
end

def defined?(token)
!select_rules(token).empty?
end

def find(token)
select_rules(token).last
end
Expand All @@ -28,9 +24,21 @@ def created_lhs(lhs_s_value)
private

def select_rules(token)
@rules.select do |rule|
rule.name == token.rule_name &&
rule.required_parameters_count == token.args_count
rules = select_rules_by_name(token.rule_name)
rules = rules.select { |rule| rule.required_parameters_count == token.args_count }
if rules.empty?
raise "Invalid number of arguments. `#{token.rule_name}`"
else
rules
end
end

def select_rules_by_name(rule_name)
rules = @rules.select { |rule| rule.name == rule_name }
if rules.empty?
raise "Parameterizing rule does not exist. `#{rule_name}`"
else
rules
end
end
end
Expand Down
60 changes: 0 additions & 60 deletions lib/lrama/grammar/parameterizing_rules/builder.rb

This file was deleted.

36 changes: 0 additions & 36 deletions lib/lrama/grammar/parameterizing_rules/builder/base.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/lrama/grammar/parameterizing_rules/builder/list.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/lrama/grammar/parameterizing_rules/builder/nonempty_list.rb

This file was deleted.

28 changes: 0 additions & 28 deletions lib/lrama/grammar/parameterizing_rules/builder/option.rb

This file was deleted.

39 changes: 0 additions & 39 deletions lib/lrama/grammar/parameterizing_rules/builder/separated_list.rb

This file was deleted.

This file was deleted.

54 changes: 22 additions & 32 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'lrama/grammar/parameterizing_rules/builder'

module Lrama
class Grammar
class RuleBuilder
Expand Down Expand Up @@ -59,7 +57,7 @@ def setup_rules(parameterizing_rule_resolver)
end

def rules
@parameterizing_rules + @old_parameterizing_rules + @midrule_action_rules + @rules
@parameterizing_rules + @midrule_action_rules + @rules
end

private
Expand Down Expand Up @@ -97,7 +95,6 @@ def process_rhs(parameterizing_rule_resolver)
return if @replaced_rhs

@replaced_rhs = []
@old_parameterizing_rules = []

rhs.each_with_index do |token, i|
case token
Expand All @@ -106,35 +103,28 @@ def process_rhs(parameterizing_rule_resolver)
when Lrama::Lexer::Token::Ident
@replaced_rhs << token
when Lrama::Lexer::Token::InstantiateRule
if parameterizing_rule_resolver.defined?(token)
parameterizing_rule = parameterizing_rule_resolver.find(token)
raise "Unexpected token. #{token}" unless parameterizing_rule

bindings = Binding.new(parameterizing_rule, token.args)
lhs_s_value = lhs_s_value(token, bindings)
if (created_lhs = parameterizing_rule_resolver.created_lhs(lhs_s_value))
@replaced_rhs << created_lhs
else
lhs_token = Lrama::Lexer::Token::Ident.new(s_value: lhs_s_value, location: token.location)
@replaced_rhs << lhs_token
parameterizing_rule_resolver.created_lhs_list << lhs_token
parameterizing_rule.rhs_list.each do |r|
rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, i, lhs_tag: token.lhs_tag, skip_preprocess_references: true)
rule_builder.lhs = lhs_token
r.symbols.each { |sym| rule_builder.add_rhs(bindings.resolve_symbol(sym)) }
rule_builder.line = line
rule_builder.user_code = r.user_code
rule_builder.precedence_sym = r.precedence_sym
rule_builder.complete_input
rule_builder.setup_rules(parameterizing_rule_resolver)
@rule_builders_for_parameterizing_rules << rule_builder
end
end
parameterizing_rule = parameterizing_rule_resolver.find(token)
raise "Unexpected token. #{token}" unless parameterizing_rule

bindings = Binding.new(parameterizing_rule, token.args)
lhs_s_value = lhs_s_value(token, bindings)
if (created_lhs = parameterizing_rule_resolver.created_lhs(lhs_s_value))
@replaced_rhs << created_lhs
else
# TODO: Delete when the standard library will defined as a grammar file.
parameterizing_rule = ParameterizingRules::Builder.new(token, @rule_counter, token.lhs_tag, user_code, precedence_sym, line)
@old_parameterizing_rules = @old_parameterizing_rules + parameterizing_rule.build
@replaced_rhs << parameterizing_rule.build_token
lhs_token = Lrama::Lexer::Token::Ident.new(s_value: lhs_s_value, location: token.location)
@replaced_rhs << lhs_token
parameterizing_rule_resolver.created_lhs_list << lhs_token
parameterizing_rule.rhs_list.each do |r|
rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, i, lhs_tag: token.lhs_tag, skip_preprocess_references: true)
rule_builder.lhs = lhs_token
r.symbols.each { |sym| rule_builder.add_rhs(bindings.resolve_symbol(sym)) }
rule_builder.line = line
rule_builder.user_code = r.user_code
rule_builder.precedence_sym = r.precedence_sym
rule_builder.complete_input
rule_builder.setup_rules(parameterizing_rule_resolver)
@rule_builders_for_parameterizing_rules << rule_builder
end
end
when Lrama::Lexer::Token::UserCode
prefix = token.referred ? "@" : "$@"
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Lexer
%empty
%code
%rule
%include-stdlib
)

def initialize(grammar_file)
Expand Down
Loading

0 comments on commit 2ca1f91

Please sign in to comment.