Skip to content

Commit

Permalink
Change only support for postfix if format
Browse files Browse the repository at this point in the history
```
%rule defined_rule(X, condition): X { $$ = $1; } %if(condition)
                                ;
```
  • Loading branch information
ydah committed Jun 9, 2024
1 parent 11ef2d0 commit 12580af
Show file tree
Hide file tree
Showing 12 changed files with 457 additions and 540 deletions.
7 changes: 1 addition & 6 deletions lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Grammar
:after_shift, :before_reduce, :after_reduce, :after_shift_error_token, :after_pop_stack,
:symbols_resolver, :types,
:rules, :rule_builders,
:sym_to_rules, :no_stdlib, :if_count
:sym_to_rules, :no_stdlib

def_delegators "@symbols_resolver", :symbols, :nterms, :terms, :add_nterm, :add_term,
:find_symbol_by_number!, :find_symbol_by_id!, :token_to_symbol,
Expand Down Expand Up @@ -60,7 +60,6 @@ def initialize(rule_counter)
@accept_symbol = nil
@aux = Auxiliary.new
@no_stdlib = false
@if_count = 0

append_special_symbols
end
Expand Down Expand Up @@ -174,10 +173,6 @@ def find_rules_by_symbol(sym)
@sym_to_rules[sym.number]
end

def initialize_if_count
@if_count = 0
end

private

def compute_nullable
Expand Down
19 changes: 0 additions & 19 deletions lib/lrama/grammar/parameterizing_rule/rhs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@ def skip?(bindings)
last_sym.is_a?(Lexer::Token::ControlSyntax) && last_sym.if? && last_sym.false?
end

def resolve_symbols(bindings)
is_skip = []
@symbols.map do |sym|
resolved = bindings.resolve_symbol(sym)
if resolved.is_a?(Lexer::Token::ControlSyntax)
if resolved.if?
is_skip.push(resolved.false?)
elsif resolved.endif?
is_skip.pop
else
raise "Unexpected control syntax: #{resolved.condition_value}"
end
nil
else
resolved unless is_skip.last
end
end.compact
end

def resolve_user_code(bindings)
return unless user_code

Expand Down
6 changes: 3 additions & 3 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ def process_rhs
rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, @parameterizing_rule_resolver, lhs_tag: token.lhs_tag || parameterizing_rule.tag)
rule_builder.lhs = lhs_token
next if r.skip?(bindings)
r.resolve_symbols(bindings).each do |sym|
rule_builder.add_rhs(sym)
end
r.symbols.each { |sym| rule_builder.add_rhs(bindings.resolve_symbol(sym)) }
rule_builder.line = line
rule_builder.precedence_sym = r.precedence_sym
rule_builder.user_code = r.resolve_user_code(bindings)
Expand All @@ -176,6 +174,8 @@ def process_rhs
rule_builder.setup_rules

@rule_builders_for_derived_rules << rule_builder
when Lrama::Lexer::Token::ControlSyntax
# NOP
else
raise "Unexpected token. #{token}"
end
Expand Down
1 change: 0 additions & 1 deletion lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Lexer
%no-stdlib
%inline
%if
%endif
%true
%false
)
Expand Down
1 change: 1 addition & 0 deletions lib/lrama/lexer/token.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'token/char'
require_relative 'token/control_syntax'
require_relative 'token/ident'
require_relative 'token/instantiate_rule'
require_relative 'token/tag'
Expand Down
4 changes: 0 additions & 4 deletions lib/lrama/lexer/token/control_syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def if?
s_value == '%if'
end

def endif?
s_value == '%endif'
end

def true?
!!@condition&.s_value
end
Expand Down
905 changes: 443 additions & 462 deletions lib/lrama/parser.rb

Large diffs are not rendered by default.

10 changes: 0 additions & 10 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ rule
{
rule = Grammar::ParameterizingRule::Rule.new(val[2].s_value, [], val[4], is_inline: true)
@grammar.add_parameterizing_rule(rule)
@grammar.initialize_if_count
}
| "%rule" "%inline" IDENTIFIER "(" rule_args ")" ":" rule_rhs_list
{
Expand Down Expand Up @@ -327,15 +326,6 @@ rule
{
builder = val[0]
builder.symbols << Lrama::Lexer::Token::ControlSyntax.new(s_value: val[1], location: @lexer.location, condition: val[3])
@grammar.if_count += 1
result = builder
}
| rule_rhs "%endif"
{
on_action_error("no %if before %endif", val[0]) if @grammar.if_count == 0
builder = val[0]
builder.symbols << Lrama::Lexer::Token::ControlSyntax.new(s_value: val[1], location: @lexer.location)
@grammar.if_count -= 1
result = builder
}

Expand Down
1 change: 0 additions & 1 deletion sig/lrama/grammar/parameterizing_rule/rhs.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module Lrama

def initialize: () -> void
def skip?: (Grammar::Binding bindings) -> bool
def resolve_symbols: (Grammar::Binding bindings) -> Array[untyped]
def resolve_user_code: (Grammar::Binding bindings) -> Lexer::Token::UserCode?
end
end
Expand Down
1 change: 0 additions & 1 deletion sig/lrama/lexer/token/control_syntax.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Lrama

def initialize: (s_value: String, location: Location, ?condition: Lexer::Token::Ident?) -> void
def if?: () -> bool
def endif?: () -> bool
def true?: () -> bool
def false?: () -> bool
def condition_value: () -> String?
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/parameterizing_rules/user_defined/if.y
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ static int yyerror(YYLTYPE *loc, const char *str);

%rule defined_rule(X, condition): /* empty */
| X { $$ = $1; } %if(condition)
| %if(condition) X %endif X { $$ = $1; }
;

%%
Expand Down
41 changes: 9 additions & 32 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2304,7 +2304,7 @@
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"),
lineno: 24,
lineno: 23,
),
Rule.new(
id: 1,
Expand All @@ -2313,7 +2313,7 @@
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 24,
lineno: 23,
),
Rule.new(
id: 2,
Expand All @@ -2324,61 +2324,38 @@
token_code: T::UserCode.new(s_value: " $$ = $1; "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("number"),
lineno: 24,
lineno: 23,
),
Rule.new(
id: 3,
lhs: grammar.find_symbol_by_s_value!("defined_rule_number_true"),
rhs: [
grammar.find_symbol_by_s_value!("number"),
grammar.find_symbol_by_s_value!("number"),
],
token_code: T::UserCode.new(s_value: " $$ = $1; "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("number"),
lineno: 24,
),
Rule.new(
id: 4,
lhs: grammar.find_symbol_by_s_value!("r_true"),
rhs: [
grammar.find_symbol_by_s_value!("defined_rule_number_true"),
],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 24,
lineno: 23,
),
Rule.new(
id: 5,
id: 4,
lhs: grammar.find_symbol_by_s_value!("defined_rule_number_false"),
rhs: [],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 27,
),
Rule.new(
id: 6,
lhs: grammar.find_symbol_by_s_value!("defined_rule_number_false"),
rhs: [
grammar.find_symbol_by_s_value!("number"),
],
token_code: T::UserCode.new(s_value: " $$ = $1; "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("number"),
lineno: 27,
lineno: 26,
),
Rule.new(
id: 7,
id: 5,
lhs: grammar.find_symbol_by_s_value!("r_false"),
rhs: [
grammar.find_symbol_by_s_value!("defined_rule_number_false"),
grammar.find_symbol_by_s_value!("defined_rule_number_false")
],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 27,
lineno: 26,
),
])
end
Expand Down

0 comments on commit 12580af

Please sign in to comment.