Skip to content

Commit def2802

Browse files
committed
feat(compiler): Support pattern aliases
1 parent 808d9ce commit def2802

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
@@ -1247,6 +1247,32 @@ program: EXPORT TYPEID AS WHILE
12471247
## The known suffix of the stack is as follows:
12481248
## AS
12491249
##
1250+
program: LET NUMBER_INT AS WHILE
1251+
##
1252+
## Ends in an error in state: 367.
1253+
##
1254+
## pattern -> pattern AS . id_str [ WHEN THICKARROW RPAREN RBRACK RBRACE PIPE EQUAL EOL COMMA COLON AS ]
1255+
## pattern -> pattern AS . eols id_str [ WHEN THICKARROW RPAREN RBRACK RBRACE PIPE EQUAL EOL COMMA COLON AS ]
1256+
##
1257+
## The known suffix of the stack is as follows:
1258+
## pattern AS
1259+
##
1260+
program: LET NUMBER_INT AS EOL WHILE
1261+
##
1262+
## Ends in an error in state: 369.
1263+
##
1264+
## pattern -> pattern AS eols . id_str [ WHEN THICKARROW RPAREN RBRACK RBRACE PIPE EQUAL EOL COMMA COLON AS ]
1265+
##
1266+
## The known suffix of the stack is as follows:
1267+
## pattern AS eols
1268+
##
1269+
## WARNING: This example involves spurious reductions.
1270+
## This implies that, although the LR(1) items shown above provide an
1271+
## accurate view of the past (what has been recognized so far), they
1272+
## may provide an INCOMPLETE view of the future (what was expected next).
1273+
## In state 3, spurious reduction of production nonempty_list(eol) -> EOL
1274+
## In state 5, spurious reduction of production eols -> nonempty_list(eol)
1275+
##
12501276

12511277
Expected an identifier to use as an alias.
12521278

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
@@ -222,6 +222,18 @@ describe("pattern matching", ({test}) => {
222222
"or_match_3",
223223
"match ([5]) { [a, _] | [_, a, _] | [a] => true, _ => false }",
224224
);
225+
// Aliases
226+
assertSnapshot("alias_match_1", "match (true) { _ as p => p }");
227+
assertSnapshot("alias_match_2", "match (true) { a as b => a && b }");
228+
assertSnapshot("alias_match_3", "match (true) { true | false as p => p }");
229+
assertSnapshot(
230+
"alias_match_4",
231+
"match (Some(5)) { Some(3 | 4 as a) => a, Some(_) | None => 5, _ => 6 }",
232+
);
233+
assertSnapshot(
234+
"alias_match_5",
235+
"match (Some(5)) { Some(3 | 4) as a => a, Some(5) | None as a => a, _ => None }",
236+
);
225237
assertFileRun("mixed_matching", "mixedPatternMatching", "true\n");
226238
assertWarning(
227239
"bool_exhaustiveness1",

0 commit comments

Comments
 (0)