Skip to content

Commit 5a124a0

Browse files
committed
feat(compiler): Support pattern aliases
1 parent 7765b9b commit 5a124a0

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

compiler/src/parsing/parser.messages

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,32 @@ program: EXPORT TYPEID AS WHILE
12451245
## The known suffix of the stack is as follows:
12461246
## AS
12471247
##
1248+
program: LET NUMBER_INT AS WHILE
1249+
##
1250+
## Ends in an error in state: 367.
1251+
##
1252+
## pattern -> pattern AS . id_str [ WHEN THICKARROW RPAREN RBRACK RBRACE PIPE EQUAL EOL COMMA COLON AS ]
1253+
## pattern -> pattern AS . eols id_str [ WHEN THICKARROW RPAREN RBRACK RBRACE PIPE EQUAL EOL COMMA COLON AS ]
1254+
##
1255+
## The known suffix of the stack is as follows:
1256+
## pattern AS
1257+
##
1258+
program: LET NUMBER_INT AS EOL WHILE
1259+
##
1260+
## Ends in an error in state: 369.
1261+
##
1262+
## pattern -> pattern AS eols . id_str [ WHEN THICKARROW RPAREN RBRACK RBRACE PIPE EQUAL EOL COMMA COLON AS ]
1263+
##
1264+
## The known suffix of the stack is as follows:
1265+
## pattern AS eols
1266+
##
1267+
## WARNING: This example involves spurious reductions.
1268+
## This implies that, although the LR(1) items shown above provide an
1269+
## accurate view of the past (what has been recognized so far), they
1270+
## may provide an INCOMPLETE view of the future (what was expected next).
1271+
## In state 3, spurious reduction of production nonempty_list(eol) -> EOL
1272+
## In state 5, spurious reduction of production eols -> nonempty_list(eol)
1273+
##
12481274

12491275
Expected an identifier to use as an alias.
12501276

compiler/src/parsing/parser.mly

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module Grain_parsing = struct end
6060

6161
%nonassoc _below_infix
6262

63+
%left AS
6364
%left PIPEPIPE
6465
%left AMPAMP
6566
%left PIPE
@@ -250,7 +251,8 @@ pattern:
250251
| type_id { Pat.construct ~loc:(to_loc $loc) $1 [] }
251252
| lbrack rbrack { Pat.list ~loc:(to_loc $loc) [] }
252253
| lbrack lseparated_nonempty_list(comma, list_item_pat) comma? rbrack { Pat.list ~loc:(to_loc $loc) $2 }
253-
| pattern pipe_op opt_eols pattern { Pat.or_ ~loc:(to_loc $loc) $1 $4 }
254+
| pattern pipe_op opt_eols pattern %prec PIPE { Pat.or_ ~loc:(to_loc $loc) $1 $4 }
255+
| pattern AS opt_eols id_str { Pat.alias ~loc:(to_loc $loc) $1 $4 }
254256

255257
list_item_pat:
256258
| ELLIPSIS pattern { ListSpread ($2, to_loc $loc) }

compiler/test/suites/pattern_matching.re

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ describe("pattern matching", ({test, testSkip}) => {
225225
"or_match_3",
226226
"match ([5]) { [a, _] | [_, a, _] | [a] => true, _ => false }",
227227
);
228+
// Aliases
229+
assertSnapshot("alias_match_1", "match (true) { _ as p => p }");
230+
assertSnapshot("alias_match_2", "match (true) { a as b => a && b }");
231+
assertSnapshot("alias_match_3", "match (true) { true | false as p => p }");
232+
assertSnapshot(
233+
"alias_match_4",
234+
"match (Some(5)) { Some(3 | 4 as a) => a, Some(_) | None => 5, _ => 6 }",
235+
);
236+
assertSnapshot(
237+
"alias_match_5",
238+
"match (Some(5)) { Some(3 | 4) as a => a, Some(5) | None as a => a, _ => None }",
239+
);
228240
assertFileRun("mixed_matching", "mixedPatternMatching", "true\n");
229241
assertWarning(
230242
"bool_exhaustiveness1",

0 commit comments

Comments
 (0)