Skip to content

Commit

Permalink
parse ... in variants
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Jun 29, 2023
1 parent 60cf75a commit 37abef7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions jscomp/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4730,6 +4730,12 @@ and parseTypeConstructorDeclaration ~startPos p =
Parser.leaveBreadcrumb p Grammar.ConstructorDeclaration;
let attrs = parseAttributes p in
match p.Parser.token with
| DotDotDot ->
Parser.next p;
let name = Location.mkloc "..." (mkLoc startPos p.prevEndPos) in
let typ = parsePolyTypeExpr p in
let loc = mkLoc startPos typ.ptyp_loc.loc_end in
Ast_helper.Type.constructor ~loc ~attrs ~args:(Pcstr_tuple [typ]) name
| Uident uident ->
let uidentLoc = mkLoc p.startPos p.endPos in
Parser.next p;
Expand Down
11 changes: 6 additions & 5 deletions jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1442,16 +1442,17 @@ and printConstructorDeclarations ~state ~privateFlag
and printConstructorDeclaration2 ~state i
(cd : Parsetree.constructor_declaration) cmtTbl =
let attrs = printAttributes ~state cd.pcd_attributes cmtTbl in
let isDotDotDot = cd.pcd_name.txt = "..." in
let bar =
if i > 0 || cd.pcd_attributes <> [] then Doc.text "| "
if i > 0 || cd.pcd_attributes <> [] || isDotDotDot then Doc.text "| "
else Doc.ifBreaks (Doc.text "| ") Doc.nil
in
let constrName =
let doc = Doc.text cd.pcd_name.txt in
printComments doc cmtTbl cd.pcd_name.loc
in
let constrArgs =
printConstructorArguments ~state ~indent:true cd.pcd_args cmtTbl
printConstructorArguments ~isDotDotDot ~state ~indent:true cd.pcd_args cmtTbl
in
let gadt =
match cd.pcd_res with
Expand All @@ -1473,15 +1474,15 @@ and printConstructorDeclaration2 ~state i
]);
]

and printConstructorArguments ~state ~indent
and printConstructorArguments ?(isDotDotDot = false) ~state ~indent
(cdArgs : Parsetree.constructor_arguments) cmtTbl =
match cdArgs with
| Pcstr_tuple [] -> Doc.nil
| Pcstr_tuple types ->
let args =
Doc.concat
[
Doc.lparen;
(if isDotDotDot then Doc.nil else Doc.lparen);
Doc.indent
(Doc.concat
[
Expand All @@ -1494,7 +1495,7 @@ and printConstructorArguments ~state ~indent
]);
Doc.trailingComma;
Doc.softLine;
Doc.rparen;
(if isDotDotDot then Doc.nil else Doc.rparen);
]
in
Doc.group (if indent then Doc.indent args else args)
Expand Down
7 changes: 7 additions & 0 deletions jscomp/syntax/tests/printer/typeDef/expected/variant.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ type color =
| @rgb Red
| @rgb Blue
| @rgb Green

// Spreads
module S = {
type s = Five | Six
}
type aa = One | Two
type bb = | ...aa | Three | Four | ...S.s
7 changes: 7 additions & 0 deletions jscomp/syntax/tests/printer/typeDef/variant.res
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ type color =
| @rgb Red
| @rgb Blue
| @rgb Green

// Spreads
module S = {
type s = Five | Six
}
type aa = One | Two
type bb = | ...aa | Three | Four | ...S.s

0 comments on commit 37abef7

Please sign in to comment.