From b61656d63e0494e204375c2ac5a394ae1de57b3b Mon Sep 17 00:00:00 2001 From: S-H-GAMELINKS Date: Tue, 28 May 2024 22:43:57 +0900 Subject: [PATCH] Add error recovery test case for "(1+2" --- spec/fixtures/integration/error_recovery.l | 8 ++++++++ spec/fixtures/integration/error_recovery.y | 18 +++++++++++++++--- spec/lrama/integration_spec.rb | 7 ++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/spec/fixtures/integration/error_recovery.l b/spec/fixtures/integration/error_recovery.l index 35050f87..a1ac397d 100644 --- a/spec/fixtures/integration/error_recovery.l +++ b/spec/fixtures/integration/error_recovery.l @@ -18,6 +18,14 @@ NUMBER [0-9]+ return NUM; } +[\(] { + return LPAREN; +} + +[\)] { + return RPAREN; +} + [+\-\*\/\(\)] { return yytext[0]; } diff --git a/spec/fixtures/integration/error_recovery.y b/spec/fixtures/integration/error_recovery.y index 55e9f7bc..a95eb24a 100644 --- a/spec/fixtures/integration/error_recovery.y +++ b/spec/fixtures/integration/error_recovery.y @@ -13,25 +13,37 @@ static int yyerror(YYLTYPE *loc, const char *str); } %token NUM +%token LPAREN "(" +%token RPAREN ")" +%type stmt %type expr %left '+' '-' %left '*' '/' %error-token { $$ = 100; -} NUM +} NUM RPAREN %% program : /* empty */ - | expr { printf("=> %d", $1); } + | stmt { printf("=> %d", $1); } + ; +stmt : expr { $$ = $1; } + | LPAREN expr RPAREN + { + if ($3 == 100) { + $$ = $2 + $3; + } else { + $$ = $2; + } + } ; expr : NUM | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; } - | '(' expr ')' { $$ = $2; } ; %% diff --git a/spec/lrama/integration_spec.rb b/spec/lrama/integration_spec.rb index 99af41f3..a1af344b 100644 --- a/spec/lrama/integration_spec.rb +++ b/spec/lrama/integration_spec.rb @@ -246,13 +246,18 @@ def generate_object(grammar_file_path, c_path, obj_path, command_args: []) end end - # TODO: Add test case for "(1+2" describe "error_recovery" do it "returns 101 for '(1+)'" do # (1+) #=> 101 # '100' is complemented test_parser("error_recovery", "(1+)", "=> 101", lrama_command_args: %W[-e]) end + + it "returns 103 for '(1+2'" do + # (1+2 #=> 103 + # '100' is complemented + test_parser("error_recovery", "(1+2", "=> 103", lrama_command_args: %W[-e]) + end end describe "sample files" do