Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intoroduce parameterizing rules with conditional statement #418

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/lrama/grammar/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def resolve_symbol(symbol)
if symbol.is_a?(Lexer::Token::InstantiateRule)
resolved_args = symbol.args.map { |arg| resolve_symbol(arg) }
Lrama::Lexer::Token::InstantiateRule.new(s_value: symbol.s_value, location: symbol.location, args: resolved_args, lhs_tag: symbol.lhs_tag)
elsif symbol.is_a?(Lexer::Token::ControlSyntax)
resolved = symbol.dup
resolved.condition = @parameter_to_arg[symbol.condition_value]
resolved
else
parameter_to_arg(symbol) || symbol
end
Expand Down
10 changes: 9 additions & 1 deletion lib/lrama/grammar/parameterizing_rule/rhs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ module Lrama
class Grammar
class ParameterizingRule
class Rhs
attr_accessor :symbols, :user_code, :precedence_sym
attr_accessor :symbols, :user_code, :precedence_sym, :if_clause

def initialize
@symbols = []
@user_code = nil
@precedence_sym = nil
@if_clause = nil
end

def skip?(bindings)
return false unless @if_clause

resolved = bindings.resolve_symbol(@if_clause)
resolved.is_a?(Lexer::Token::ControlSyntax) && resolved.if? && resolved.false?
end

def resolve_user_code(bindings)
Expand Down
4 changes: 4 additions & 0 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ def process_rhs
if (created_lhs = @parameterizing_rule_resolver.created_lhs(lhs_s_value))
@replaced_rhs << created_lhs
else
next if parameterizing_rule.rhs_list.all? { |r| r.skip?(bindings) }
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, @parameterizing_rule_resolver, lhs_tag: token.lhs_tag || parameterizing_rule.tag)
rule_builder.lhs = lhs_token
next if r.skip?(bindings)
r.symbols.each { |sym| rule_builder.add_rhs(bindings.resolve_symbol(sym)) }
rule_builder.line = line
rule_builder.precedence_sym = r.precedence_sym
Expand All @@ -168,6 +170,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
3 changes: 3 additions & 0 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class Lexer
%no-stdlib
%inline
%locations
%if
%true
%false
).freeze

def initialize(grammar_file)
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
30 changes: 30 additions & 0 deletions lib/lrama/lexer/token/control_syntax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Lrama
class Lexer
class Token
class ControlSyntax < Token
attr_accessor :condition

def initialize(s_value:, location:, condition: nil)
@condition = condition
super(s_value: s_value, location: location)
end

def if?
s_value == '%if'
end

def true?
!!@condition&.s_value
end

def false?
!true?
end

def condition_value
@condition&.s_value
end
end
end
end
end
868 changes: 482 additions & 386 deletions lib/lrama/parser.rb

Large diffs are not rendered by default.

28 changes: 23 additions & 5 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,16 @@ rule
rule_args: IDENTIFIER { result = [val[0]] }
| rule_args "," IDENTIFIER { result = val[0].append(val[2]) }

rule_rhs_list: rule_rhs
rule_rhs_list: rule_rhs if_clause?
{
builder = val[0]
builder.if_clause = val[1]
result = [builder]
}
| rule_rhs_list "|" rule_rhs
| rule_rhs_list "|" rule_rhs if_clause?
{
builder = val[2]
builder.if_clause = val[3]
result = val[0].append(builder)
}

Expand All @@ -227,7 +229,7 @@ rule
builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[2], location: @lexer.location, args: [val[1]])
result = builder
}
| rule_rhs IDENTIFIER "(" parameterizing_args ")" TAG?
| rule_rhs IDENTIFIER "(" parameterizing_rule_args ")" TAG?
{
builder = val[0]
builder.symbols << Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[3], lhs_tag: val[5])
Expand All @@ -250,6 +252,11 @@ rule
result = builder
}

if_clause: "%if" "(" IDENTIFIER ")"
{
result = Lrama::Lexer::Token::ControlSyntax.new(s_value: val[0], location: @lexer.location, condition: val[2])
}

alias: # empty
| string_as_id { result = val[0].s_value }

Expand Down Expand Up @@ -362,11 +369,22 @@ rule
| "+" { result = "nonempty_list" }
| "*" { result = "list" }

parameterizing_args: symbol { result = [val[0]] }
| parameterizing_args ',' symbol { result = val[0].append(val[2]) }
parameterizing_rule_args: symbol { result = [val[0]] }
| parameterizing_args ',' symbol { result = val[0].append(val[2]) }
| symbol parameterizing_suffix { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[0])] }
| IDENTIFIER "(" parameterizing_args ")" { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] }

parameterizing_args: symbol_or_bool { result = [val[0]] }
| parameterizing_args ',' symbol_or_bool { result = val[0].append(val[2]) }
| symbol parameterizing_suffix { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[1].s_value, location: @lexer.location, args: val[0])] }
| IDENTIFIER "(" parameterizing_args ")" { result = [Lrama::Lexer::Token::InstantiateRule.new(s_value: val[0].s_value, location: @lexer.location, args: val[2])] }

symbol_or_bool: symbol
| bool

bool: "%true" { result = Lrama::Lexer::Token::Ident.new(s_value: true) }
| "%false" { result = Lrama::Lexer::Token::Ident.new(s_value: false) }

midrule_action: "{"
{
if @prec_seen
Expand Down
1 change: 1 addition & 0 deletions sig/lrama/grammar/parameterizing_rule/rhs.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Lrama
attr_reader precedence_sym: Grammar::Symbol?

def initialize: () -> void
def skip?: (Grammar::Binding bindings) -> bool
def resolve_user_code: (Grammar::Binding bindings) -> Lexer::Token::UserCode?
end
end
Expand Down
15 changes: 15 additions & 0 deletions sig/lrama/lexer/token/control_syntax.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Lrama
class Lexer
class Token
class ControlSyntax < Token
attr_accessor condition: Lexer::Token::Ident?

def initialize: (s_value: String, location: Location, ?condition: Lexer::Token::Ident?) -> void
def if?: () -> bool
def true?: () -> bool
def false?: () -> bool
def condition_value: () -> String?
end
end
end
end
43 changes: 43 additions & 0 deletions spec/fixtures/parameterizing_rules/user_defined/if.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This is comment for this file.
*/

%{
// Prologue
static int yylex(YYSTYPE *val, YYLTYPE *loc);
static int yyerror(YYLTYPE *loc, const char *str);
%}

%union {
int i;
}

%token <i> number

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

%%

r_true : defined_rule(number, %true)
;

r_false : defined_rule(number, %false)
;

%%

static int yylex(YYSTYPE *yylval, YYLTYPE *loc)
{
return 0;
}

static int yyerror(YYLTYPE *loc, const char *str)
{
return 0;
}

int main(int argc, char *argv[])
{
}
80 changes: 80 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,86 @@
])
end
end

context "when if" do
let(:path) { "parameterizing_rules/user_defined/if.y" }

it "expands parameterizing rules" do
expect(grammar.nterms.sort_by(&:number)).to match_symbols([
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 4, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "defined_rule_number_true"), alias_name: nil, number: 5, tag: nil, term: false, token_id: 1, nullable: true),
Sym.new(id: T::Ident.new(s_value: "r_true"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 2, nullable: true),
Sym.new(id: T::Ident.new(s_value: "defined_rule_number_false"), alias_name: nil, number: 7, tag: nil, term: false, token_id: 3, nullable: true),
Sym.new(id: T::Ident.new(s_value: "r_false"), alias_name: nil, number: 8, tag: nil, term: false, token_id: 4, nullable: true),
])

expect(grammar.rules).to eq([
Rule.new(
id: 0,
lhs: grammar.find_symbol_by_s_value!("$accept"),
rhs: [
grammar.find_symbol_by_s_value!("r_true"),
grammar.find_symbol_by_s_value!("YYEOF"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"),
lineno: 23,
),
Rule.new(
id: 1,
lhs: grammar.find_symbol_by_s_value!("defined_rule_number_true"),
rhs: [],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 23,
),
Rule.new(
id: 2,
lhs: grammar.find_symbol_by_s_value!("defined_rule_number_true"),
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: 23,
),
Rule.new(
id: 3,
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: 23,
),
Rule.new(
id: 4,
lhs: grammar.find_symbol_by_s_value!("defined_rule_number_false"),
rhs: [],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 26,
),
Rule.new(
id: 5,
lhs: grammar.find_symbol_by_s_value!("r_false"),
rhs: [
grammar.find_symbol_by_s_value!("defined_rule_number_false")
],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 26,
),
])
end
end
end

context 'when error case' do
Expand Down