diff --git a/spec/fixtures/inlining/inline_parameterizing_rhs.y b/spec/fixtures/inlining/inline_parameterizing_rhs.y new file mode 100644 index 00000000..40ba582d --- /dev/null +++ b/spec/fixtures/inlining/inline_parameterizing_rhs.y @@ -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 NUM +%type 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[]) +{ +} diff --git a/spec/lrama/parser_spec.rb b/spec/lrama/parser_spec.rb index e2990334..40512c87 100644 --- a/spec/lrama/parser_spec.rb +++ b/spec/lrama/parser_spec.rb @@ -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: ""), 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" }