Skip to content

Commit 9ed093b

Browse files
authored
feat(compiler): Support pattern aliases (#1174)
* feat(compiler): Support pattern aliases * snapshots * Convert snapshot test to runnable test
1 parent 5f20868 commit 9ed093b

File tree

7 files changed

+705
-1
lines changed

7 files changed

+705
-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) }

0 commit comments

Comments
 (0)