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

Add error recovery test case for "(1+2" #425

Open
wants to merge 1 commit 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
8 changes: 8 additions & 0 deletions spec/fixtures/integration/error_recovery.l
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ NUMBER [0-9]+
return NUM;
}

[\(] {
return LPAREN;
}

[\)] {
return RPAREN;
}

[+\-\*\/\(\)] {
return yytext[0];
}
Expand Down
18 changes: 15 additions & 3 deletions spec/fixtures/integration/error_recovery.y
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,37 @@ static int yyerror(YYLTYPE *loc, const char *str);
}

%token <val> NUM
%token <val> LPAREN "("
%token <val> RPAREN ")"
%type <val> stmt
%type <val> 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; }
;

%%
Expand Down
7 changes: 6 additions & 1 deletion spec/lrama/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down