Skip to content

Commit

Permalink
Add test code for cases with parameterizing rule in rhs for inline
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed May 17, 2024
1 parent 56b1f45 commit c638e08
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
42 changes: 42 additions & 0 deletions spec/fixtures/inlining/inline_parameterizing_rhs.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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> NUM
%type <i> expression

%rule %inline op : '+' { + }
| option('-') '-' { - }
;

%%

expression : NUM
| expression op expression { $$ = $1 $2 $3; }
;

%%

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[])
{
}
85 changes: 85 additions & 0 deletions spec/lrama/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2387,6 +2387,91 @@
end
end

context 'when inline have rhs include parameterizing rule' do
let(:path) { "inlining/inline_parameterizing_rhs.y" }

it "expands inlining rules" do
expect(grammar.nterms.sort_by(&:number)).to match_symbols([
Sym.new(id: T::Ident.new(s_value: "$accept"), alias_name: nil, number: 6, tag: nil, term: false, token_id: 0, nullable: false),
Sym.new(id: T::Ident.new(s_value: "expression"), alias_name: nil, number: 7, tag: T::Tag.new(s_value: "<i>"), term: false, token_id: 1, nullable: false),
Sym.new(id: T::Ident.new(s_value: "option_'-'"), alias_name: nil, number: 8, tag: nil, term: false, token_id: 2, 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!("expression"),
grammar.find_symbol_by_s_value!("YYEOF"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("YYEOF"),
lineno: 24,
),
Rule.new(
id: 1,
lhs: grammar.find_symbol_by_s_value!("expression"),
rhs: [
grammar.find_symbol_by_s_value!("NUM"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("NUM"),
lineno: 24,
),
Rule.new(
id: 2,
lhs: grammar.find_symbol_by_s_value!("expression"),
rhs: [
grammar.find_symbol_by_s_value!("expression"),
grammar.find_symbol_by_s_value!("'+'"),
grammar.find_symbol_by_s_value!("expression"),
],
token_code: T::UserCode.new(s_value: " $$ = $1 + $3; "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("'+'"),
lineno: 25,
),
Rule.new(
id: 3,
lhs: grammar.find_symbol_by_s_value!("option_'-'"),
rhs: [],
token_code: nil,
nullable: true,
precedence_sym: nil,
lineno: 25,
),
Rule.new(
id: 4,
lhs: grammar.find_symbol_by_s_value!("option_'-'"),
rhs: [
grammar.find_symbol_by_s_value!("'-'"),
],
token_code: nil,
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("'-'"),
lineno: 25,
),
Rule.new(
id: 5,
lhs: grammar.find_symbol_by_s_value!("expression"),
rhs: [
grammar.find_symbol_by_s_value!("expression"),
grammar.find_symbol_by_s_value!("option_'-'"),
grammar.find_symbol_by_s_value!("'-'"),
grammar.find_symbol_by_s_value!("expression"),
],
token_code: T::UserCode.new(s_value: " $$ = $1 - $4; "),
nullable: false,
precedence_sym: grammar.find_symbol_by_s_value!("'-'"),
lineno: 25,
),
])
end
end

context 'when inline with parameterizing rule' do
let(:path) { "inlining/with_parameterizing.y" }

Expand Down

0 comments on commit c638e08

Please sign in to comment.