Skip to content

Commit 1565c16

Browse files
authored
fix(compiler): Fix precedence of >> operator (#1515)
1 parent e21f2b1 commit 1565c16

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

compiler/src/parsing/parser.messages

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6969,14 +6969,21 @@ program: BIGINT RCARET RCARET EOL WHEN
69696969
## In state 3, spurious reduction of production nonempty_list(eol) -> EOL
69706970
## In state 5, spurious reduction of production eols -> nonempty_list(eol)
69716971
##
6972-
program: LPAREN INFIX_100 WHILE
6972+
program: BIGINT RCARET RCARET RPAREN
69736973
##
6974-
## Ends in an error in state: 150.
6974+
## Ends in an error in state: 65.
69756975
##
6976-
## id_str -> lparen special_op . rparen [ WHEN TYPE THICKARROW STAR SLASH SEMI RPAREN RECORD RCARET RBRACK RBRACE PIPE LPAREN LET LCARET LBRACK INFIX_ASSIGNMENT_10 INFIX_90 INFIX_80 INFIX_70 INFIX_60 INFIX_50 INFIX_40 INFIX_30 INFIX_120 INFIX_110 INFIX_100 IMPORT GETS FROM EXPORT EQUAL EOL EOF ENUM ELSE DOT DASH COMMA COLON AT AS ]
6976+
## binop_expr -> non_stmt_expr rcaret_rcaret_op . non_stmt_expr [ THICKARROW STAR SLASH SEMI RPAREN RCARET RBRACK RBRACE PIPE LCARET INFIX_90 INFIX_80 INFIX_70 INFIX_60 INFIX_50 INFIX_40 INFIX_30 INFIX_120 INFIX_110 INFIX_100 EOL EOF ELSE DASH COMMA COLON ]
6977+
## binop_expr -> non_stmt_expr rcaret_rcaret_op . eols non_stmt_expr [ THICKARROW STAR SLASH SEMI RPAREN RCARET RBRACK RBRACE PIPE LCARET INFIX_90 INFIX_80 INFIX_70 INFIX_60 INFIX_50 INFIX_40 INFIX_30 INFIX_120 INFIX_110 INFIX_100 EOL EOF ELSE DASH COMMA COLON ]
69776978
##
69786979
## The known suffix of the stack is as follows:
6979-
## lparen special_op
6980+
## non_stmt_expr rcaret_rcaret_op
6981+
##
6982+
## WARNING: This example involves spurious reductions.
6983+
## This implies that, although the LR(1) items shown above provide an
6984+
## accurate view of the past (what has been recognized so far), they
6985+
## may provide an INCOMPLETE view of the future (what was expected next).
6986+
## In state 70, spurious reduction of production rcaret_rcaret_op -> lnonempty_list_inner(RCARET) RCARET
69806987
##
69816988
program: LPAREN RCARET WHILE
69826989
##

compiler/src/parsing/parser.mly

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ annotated_expr:
232232

233233
binop_expr:
234234
| non_stmt_expr infix_op opt_eols non_stmt_expr { Exp.binop ~loc:(to_loc $loc) (mkid_expr $loc($2) [mkstr $loc($2) $2]) [$1; $4] }
235+
| non_stmt_expr rcaret_rcaret_op opt_eols non_stmt_expr %prec INFIX_100 { Exp.binop ~loc:(to_loc $loc) (mkid_expr $loc($2) [mkstr $loc($2) $2]) [$1; $4] }
235236

236237
ellipsis_prefix(X):
237238
| ELLIPSIS X {$2}
@@ -385,6 +386,9 @@ paren_expr:
385386
app_expr:
386387
| left_accessor_expr lparen lseparated_list(comma, expr) comma? rparen { Exp.apply ~loc:(to_loc $loc) $1 $3 }
387388

389+
rcaret_rcaret_op:
390+
| lnonempty_list(RCARET) RCARET { (String.init (1 + List.length $1) (fun _ -> '>')) }
391+
388392
// These are all inlined to carry over their precedence.
389393
%inline infix_op:
390394
| INFIX_30
@@ -402,7 +406,7 @@ app_expr:
402406
| DASH { "-" }
403407
| PIPE { "|" }
404408
| LCARET { "<" }
405-
| llist(RCARET) RCARET { (String.init (1 + List.length $1) (fun _ -> '>')) }
409+
| RCARET { ">" }
406410

407411
%inline prefix_op:
408412
| PREFIX_150 {$1}
@@ -413,7 +417,7 @@ primitive_:
413417
| FAIL { "fail" }
414418

415419
special_op:
416-
| infix_op | prefix_op {$1}
420+
| infix_op | rcaret_rcaret_op | prefix_op {$1}
417421

418422
%inline special_id:
419423
| lparen special_op rparen { mkstr $loc($2) $2 }

compiler/test/suites/parsing.re

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,34 @@ describe("parsing", ({test, testSkip}) => {
186186
prog_loc: Location.dummy_loc,
187187
},
188188
);
189+
assertParse(
190+
"regression_issue_1473",
191+
"a << b >> c",
192+
{
193+
statements: [
194+
Top.expr(
195+
Exp.apply(
196+
Exp.ident(
197+
Location.mknoloc(
198+
Identifier.IdentName(Location.mknoloc(">>")),
199+
),
200+
),
201+
[
202+
Exp.apply(
203+
Exp.ident(
204+
Location.mknoloc(
205+
Identifier.IdentName(Location.mknoloc("<<")),
206+
),
207+
),
208+
[a, b],
209+
),
210+
c,
211+
],
212+
),
213+
),
214+
],
215+
comments: [],
216+
prog_loc: Location.dummy_loc,
217+
},
218+
);
189219
});

0 commit comments

Comments
 (0)