Skip to content

Commit 73f8379

Browse files
committed
simplify parsing of record rest
1 parent 8ecb9f6 commit 73f8379

1 file changed

Lines changed: 16 additions & 43 deletions

File tree

compiler/syntax/src/res_core.ml

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,13 +1456,14 @@ and parse_record_pattern_row p =
14561456
| DotDotDot -> (
14571457
Parser.next p;
14581458
let start_pos = p.Parser.start_pos in
1459-
match p.Parser.token with
1460-
| Uident _ ->
1461-
(* ...ModulePath.t<'a> as name *)
1462-
let type_path = parse_value_path p in
1463-
let type_loc = type_path.loc in
1464-
let type_args = parse_type_constructor_args ~constr_name:type_path p in
1465-
let core_type = Ast_helper.Typ.constr ~loc:type_loc type_path type_args in
1459+
let has_type_annotation =
1460+
Parser.lookahead p (fun p ->
1461+
ignore (parse_atomic_typ_expr ~attrs:[] p);
1462+
p.token = As)
1463+
in
1464+
if has_type_annotation then (
1465+
(* ...TypeAnnotation<'a> as name *)
1466+
let core_type = parse_atomic_typ_expr ~attrs:[] p in
14661467
Parser.expect As p;
14671468
let name_start = p.start_pos in
14681469
let name =
@@ -1480,48 +1481,20 @@ and parse_record_pattern_row p =
14801481
(Ast_helper.Pat.var ~loc:name.loc name)
14811482
core_type
14821483
in
1483-
Some (false, PatRest rest_pat)
1484-
| Lident ident ->
1485-
Parser.next p;
1486-
if p.Parser.token = As || p.Parser.token = Token.LessThan then (
1487-
(* ...typeName<'a> as name *)
1488-
let type_path =
1489-
Location.mkloc (Longident.Lident ident)
1490-
(mk_loc start_pos p.prev_end_pos)
1491-
in
1492-
let type_loc = type_path.loc in
1493-
let type_args = parse_type_constructor_args ~constr_name:type_path p in
1494-
let core_type =
1495-
Ast_helper.Typ.constr ~loc:type_loc type_path type_args
1496-
in
1497-
Parser.expect As p;
1498-
let name_start = p.start_pos in
1499-
let name =
1500-
match p.token with
1501-
| Lident id ->
1502-
Parser.next p;
1503-
Location.mkloc id (mk_loc name_start p.prev_end_pos)
1504-
| _ ->
1505-
Parser.err p (Diagnostics.unexpected p.token p.breadcrumbs);
1506-
Location.mkloc "_" (mk_loc name_start p.prev_end_pos)
1507-
in
1508-
let rest_loc = mk_loc start_pos p.prev_end_pos in
1509-
let rest_pat =
1510-
Ast_helper.Pat.constraint_ ~loc:rest_loc ~attrs
1511-
(Ast_helper.Pat.var ~loc:name.loc name)
1512-
core_type
1513-
in
1514-
Some (false, PatRest rest_pat))
1515-
else
1484+
Some (false, PatRest rest_pat))
1485+
else
1486+
match p.Parser.token with
1487+
| Lident ident ->
15161488
(* ...name (no type annotation) *)
1489+
Parser.next p;
15171490
let loc = mk_loc start_pos p.prev_end_pos in
15181491
let rest_pat =
15191492
Ast_helper.Pat.var ~loc ~attrs (Location.mkloc ident loc)
15201493
in
15211494
Some (false, PatRest rest_pat)
1522-
| _ ->
1523-
(* Fallback: treat as old-style spread (error) *)
1524-
Some (true, PatField (parse_record_pattern_row_field ~attrs p)))
1495+
| _ ->
1496+
(* Fallback: treat as old-style spread (error) *)
1497+
Some (true, PatField (parse_record_pattern_row_field ~attrs p)))
15251498
| Uident _ | Lident _ ->
15261499
Some (false, PatField (parse_record_pattern_row_field ~attrs p))
15271500
| Question -> (

0 commit comments

Comments
 (0)