Skip to content

Commit 0a5a97c

Browse files
authored
fix(compiler): Properly handle parsing return of negative number (#1611)
1 parent 4091e0e commit 0a5a97c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

compiler/src/parsing/parser.mly

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ module Grain_parsing = struct end
6363
%left INFIX_110 DASH
6464
%left INFIX_120 STAR SLASH
6565

66-
%right SEMI EOL COMMA DOT COLON LPAREN RETURN
66+
%right SEMI EOL COMMA DOT COLON LPAREN
6767

6868
%nonassoc _if
6969
%nonassoc ELSE
@@ -531,7 +531,8 @@ stmt_expr:
531531
| THROW expr { Exp.apply ~loc:(to_loc $loc) (mkid_expr $loc($1) [mkstr $loc($1) "throw"]) [$2] }
532532
| ASSERT expr { Exp.apply ~loc:(to_loc $loc) (mkid_expr $loc($1) [mkstr $loc($1) "assert"]) [$2] }
533533
| FAIL expr { Exp.apply ~loc:(to_loc $loc) (mkid_expr $loc($1) [mkstr $loc($1) "fail"]) [$2] }
534-
| RETURN ioption(expr) { Exp.return ~loc:(to_loc $loc) $2 }
534+
// allow DASH to cause a shift instead of the usual reduction of the left side for subtraction
535+
| RETURN ioption(expr) %prec _below_infix { Exp.return ~loc:(to_loc $loc) $2 }
535536
| CONTINUE { Exp.continue ~loc:(to_loc $loc) () }
536537
| BREAK { Exp.break ~loc:(to_loc $loc) () }
537538

compiler/test/suites/parsing.re

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,19 @@ describe("parsing", ({test, testSkip}) => {
216216
prog_loc: Location.dummy_loc,
217217
},
218218
);
219+
assertParse(
220+
"regression_issue_1609",
221+
"return -1",
222+
{
223+
statements: [
224+
Top.expr(
225+
Exp.return(
226+
Some(Exp.constant(PConstNumber(PConstNumberInt("-1")))),
227+
),
228+
),
229+
],
230+
comments: [],
231+
prog_loc: Location.dummy_loc,
232+
},
233+
);
219234
});

0 commit comments

Comments
 (0)