Skip to content

Commit 6853bbf

Browse files
committed
Sync latest outcome printer.
1 parent 2e6f623 commit 6853bbf

File tree

5 files changed

+113
-23
lines changed

5 files changed

+113
-23
lines changed

analysis/vendor/res_outcome_printer/res_core.ml

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ let jsxAttr = (Location.mknoloc "JSX", Parsetree.PStr [])
161161
let uncurryAttr = (Location.mknoloc "bs", Parsetree.PStr [])
162162
let ternaryAttr = (Location.mknoloc "ns.ternary", Parsetree.PStr [])
163163
let ifLetAttr = (Location.mknoloc "ns.iflet", Parsetree.PStr [])
164+
let optionalAttr = (Location.mknoloc "ns.optional", Parsetree.PStr [])
165+
166+
let makeExpressionOptional ~optional (e : Parsetree.expression) =
167+
if optional then {e with pexp_attributes = optionalAttr :: e.pexp_attributes}
168+
else e
169+
let makePatternOptional ~optional (p : Parsetree.pattern) =
170+
if optional then {p with ppat_attributes = optionalAttr :: p.ppat_attributes}
171+
else p
172+
164173
let suppressFragileMatchWarningAttr =
165174
( Location.mknoloc "warning",
166175
Parsetree.PStr
@@ -1032,7 +1041,11 @@ let rec parsePattern ?(alias = true) ?(or_ = true) p =
10321041
| _ ->
10331042
Parser.expect Rparen p;
10341043
let loc = mkLoc startPos p.prevEndPos in
1035-
{pat with ppat_loc = loc}))
1044+
{
1045+
pat with
1046+
ppat_loc = loc;
1047+
ppat_attributes = attrs @ pat.Parsetree.ppat_attributes;
1048+
}))
10361049
| Lbracket -> parseArrayPattern ~attrs p
10371050
| Lbrace -> parseRecordPattern ~attrs p
10381051
| Underscore ->
@@ -1196,6 +1209,13 @@ and parseConstrainedPatternRegion p =
11961209
| token when Grammar.isPatternStart token -> Some (parseConstrainedPattern p)
11971210
| _ -> None
11981211

1212+
and parseOptionalLabel p =
1213+
match p.Parser.token with
1214+
| Question ->
1215+
Parser.next p;
1216+
true
1217+
| _ -> false
1218+
11991219
(* field ::=
12001220
* | longident
12011221
* | longident : pattern
@@ -1206,26 +1226,37 @@ and parseConstrainedPatternRegion p =
12061226
* | field , _
12071227
* | field , _,
12081228
*)
1209-
and parseRecordPatternField p =
1229+
and parseRecordPatternRowField ~attrs p =
12101230
let label = parseValuePath p in
12111231
let pattern =
12121232
match p.Parser.token with
12131233
| Colon ->
12141234
Parser.next p;
1215-
parsePattern p
1235+
let optional = parseOptionalLabel p in
1236+
let pat = parsePattern p in
1237+
makePatternOptional ~optional pat
12161238
| _ ->
1217-
Ast_helper.Pat.var ~loc:label.loc
1239+
Ast_helper.Pat.var ~loc:label.loc ~attrs
12181240
(Location.mkloc (Longident.last label.txt) label.loc)
12191241
in
12201242
(label, pattern)
12211243

12221244
(* TODO: there are better representations than PatField|Underscore ? *)
1223-
and parseRecordPatternItem p =
1245+
and parseRecordPatternRow p =
1246+
let attrs = parseAttributes p in
12241247
match p.Parser.token with
12251248
| DotDotDot ->
12261249
Parser.next p;
1227-
Some (true, PatField (parseRecordPatternField p))
1228-
| Uident _ | Lident _ -> Some (false, PatField (parseRecordPatternField p))
1250+
Some (true, PatField (parseRecordPatternRowField ~attrs p))
1251+
| Uident _ | Lident _ ->
1252+
Some (false, PatField (parseRecordPatternRowField ~attrs p))
1253+
| Question -> (
1254+
Parser.next p;
1255+
match p.token with
1256+
| Uident _ | Lident _ ->
1257+
let lid, pat = parseRecordPatternRowField ~attrs p in
1258+
Some (false, PatField (lid, makePatternOptional ~optional:true pat))
1259+
| _ -> None)
12291260
| Underscore ->
12301261
Parser.next p;
12311262
Some (false, PatUnderscore)
@@ -1236,7 +1267,7 @@ and parseRecordPattern ~attrs p =
12361267
Parser.expect Lbrace p;
12371268
let rawFields =
12381269
parseCommaDelimitedReversedList p ~grammar:PatternRecord ~closing:Rbrace
1239-
~f:parseRecordPatternItem
1270+
~f:parseRecordPatternRow
12401271
in
12411272
Parser.expect Rbrace p;
12421273
let fields, closedFlag =
@@ -2735,6 +2766,10 @@ and parseBracedOrRecordExpr p =
27352766
let loc = mkLoc startPos p.prevEndPos in
27362767
let braces = makeBracesAttr loc in
27372768
{expr with pexp_attributes = braces :: expr.pexp_attributes}))
2769+
| Question ->
2770+
let expr = parseRecordExpr ~startPos [] p in
2771+
Parser.expect Rbrace p;
2772+
expr
27382773
| Uident _ | Lident _ -> (
27392774
let startToken = p.token in
27402775
let valueOrConstructor = parseValueOrConstructor p in
@@ -2756,7 +2791,9 @@ and parseBracedOrRecordExpr p =
27562791
expr
27572792
| Colon -> (
27582793
Parser.next p;
2794+
let optional = parseOptionalLabel p in
27592795
let fieldExpr = parseExpr p in
2796+
let fieldExpr = makeExpressionOptional ~optional fieldExpr in
27602797
match p.token with
27612798
| Rbrace ->
27622799
Parser.next p;
@@ -2893,7 +2930,7 @@ and parseBracedOrRecordExpr p =
28932930
let braces = makeBracesAttr loc in
28942931
{expr with pexp_attributes = braces :: expr.pexp_attributes}
28952932

2896-
and parseRecordRowWithStringKey p =
2933+
and parseRecordExprRowWithStringKey p =
28972934
match p.Parser.token with
28982935
| String s -> (
28992936
let loc = mkLoc p.startPos p.endPos in
@@ -2907,7 +2944,8 @@ and parseRecordRowWithStringKey p =
29072944
| _ -> Some (field, Ast_helper.Exp.ident ~loc:field.loc field))
29082945
| _ -> None
29092946

2910-
and parseRecordRow p =
2947+
and parseRecordExprRow p =
2948+
let attrs = parseAttributes p in
29112949
let () =
29122950
match p.Parser.token with
29132951
| Token.DotDotDot ->
@@ -2922,23 +2960,39 @@ and parseRecordRow p =
29222960
match p.Parser.token with
29232961
| Colon ->
29242962
Parser.next p;
2963+
let optional = parseOptionalLabel p in
29252964
let fieldExpr = parseExpr p in
2965+
let fieldExpr = makeExpressionOptional ~optional fieldExpr in
29262966
Some (field, fieldExpr)
29272967
| _ ->
2928-
let value = Ast_helper.Exp.ident ~loc:field.loc field in
2968+
let value = Ast_helper.Exp.ident ~loc:field.loc ~attrs field in
29292969
let value =
29302970
match startToken with
29312971
| Uident _ -> removeModuleNameFromPunnedFieldValue value
29322972
| _ -> value
29332973
in
29342974
Some (field, value))
2975+
| Question -> (
2976+
Parser.next p;
2977+
match p.Parser.token with
2978+
| Lident _ | Uident _ ->
2979+
let startToken = p.token in
2980+
let field = parseValuePath p in
2981+
let value = Ast_helper.Exp.ident ~loc:field.loc ~attrs field in
2982+
let value =
2983+
match startToken with
2984+
| Uident _ -> removeModuleNameFromPunnedFieldValue value
2985+
| _ -> value
2986+
in
2987+
Some (field, makeExpressionOptional ~optional:true value)
2988+
| _ -> None)
29352989
| _ -> None
29362990

29372991
and parseRecordExprWithStringKeys ~startPos firstRow p =
29382992
let rows =
29392993
firstRow
29402994
:: parseCommaDelimitedRegion ~grammar:Grammar.RecordRowsStringKey
2941-
~closing:Rbrace ~f:parseRecordRowWithStringKey p
2995+
~closing:Rbrace ~f:parseRecordExprRowWithStringKey p
29422996
in
29432997
let loc = mkLoc startPos p.endPos in
29442998
let recordStrExpr =
@@ -2950,7 +3004,7 @@ and parseRecordExprWithStringKeys ~startPos firstRow p =
29503004
and parseRecordExpr ~startPos ?(spread = None) rows p =
29513005
let exprs =
29523006
parseCommaDelimitedRegion ~grammar:Grammar.RecordRows ~closing:Rbrace
2953-
~f:parseRecordRow p
3007+
~f:parseRecordExprRow p
29543008
in
29553009
let rows = List.concat [rows; exprs] in
29563010
let () =
@@ -4224,15 +4278,19 @@ and parseFieldDeclarationRegion p =
42244278
| Lident _ ->
42254279
let lident, loc = parseLident p in
42264280
let name = Location.mkloc lident loc in
4281+
let optional = parseOptionalLabel p in
42274282
let typ =
42284283
match p.Parser.token with
42294284
| Colon ->
42304285
Parser.next p;
42314286
parsePolyTypeExpr p
42324287
| _ ->
4233-
Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} []
4288+
Ast_helper.Typ.constr ~loc:name.loc ~attrs
4289+
{name with txt = Lident name.txt}
4290+
[]
42344291
in
42354292
let loc = mkLoc startPos typ.ptyp_loc.loc_end in
4293+
let attrs = if optional then optionalAttr :: attrs else attrs in
42364294
Some (Ast_helper.Type.field ~attrs ~loc ~mut name typ)
42374295
| _ -> None
42384296

analysis/vendor/res_outcome_printer/res_outcome_printer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ and printRecordDeclRowDoc (name, mut, opt, arg) =
526526
Doc.group
527527
(Doc.concat
528528
[
529-
(if opt then Doc.text "@optional " else Doc.nil);
529+
(if opt then Doc.text "?" else Doc.nil);
530530
(if mut then Doc.text "mutable " else Doc.nil);
531531
printIdentLike ~allowUident:false name;
532532
Doc.text ": ";

analysis/vendor/res_outcome_printer/res_parsetree_viewer.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ let filterParsingAttrs attrs =
169169
| ( {
170170
Location.txt =
171171
( "ns.ternary" | "ns.braces" | "res.template" | "bs" | "ns.iflet"
172-
| "ns.namedArgLoc" );
172+
| "ns.namedArgLoc" | "ns.optional" );
173173
},
174174
_ ) ->
175175
false
@@ -304,6 +304,12 @@ let isIfLetExpr expr =
304304
true
305305
| _ -> false
306306

307+
let rec hasOptionalAttribute attrs =
308+
match attrs with
309+
| [] -> false
310+
| ({Location.txt = "ns.optional"}, _) :: _ -> true
311+
| _ :: attrs -> hasOptionalAttribute attrs
312+
307313
let hasAttributes attrs =
308314
List.exists
309315
(fun attr ->

analysis/vendor/res_outcome_printer/res_parsetree_viewer.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ val filterFragileMatchAttributes : Parsetree.attributes -> Parsetree.attributes
8383

8484
val isJsxExpression : Parsetree.expression -> bool
8585
val hasJsxAttribute : Parsetree.attributes -> bool
86+
val hasOptionalAttribute : Parsetree.attributes -> bool
8687

8788
val shouldIndentBinaryExpr : Parsetree.expression -> bool
8889
val shouldInlineRhsBinaryExpr : Parsetree.expression -> bool

analysis/vendor/res_outcome_printer/res_printer.ml

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,10 @@ let printConstant ?(templateLiteral = false) c =
523523
in
524524
Doc.text ("'" ^ str ^ "'")
525525

526+
let printOptionalLabel attrs =
527+
if Res_parsetree_viewer.hasOptionalAttribute attrs then Doc.text "?"
528+
else Doc.nil
529+
526530
let rec printStructure (s : Parsetree.structure) t =
527531
match s with
528532
| [] -> printCommentsInside t Location.none
@@ -1426,10 +1430,16 @@ and printLabelDeclaration (ld : Parsetree.label_declaration) cmtTbl =
14261430
let doc = printIdentLike ld.pld_name.txt in
14271431
printComments doc cmtTbl ld.pld_name.loc
14281432
in
1433+
let optional = printOptionalLabel ld.pld_attributes in
14291434
Doc.group
14301435
(Doc.concat
14311436
[
1432-
attrs; mutableFlag; name; Doc.text ": "; printTypExpr ld.pld_type cmtTbl;
1437+
attrs;
1438+
mutableFlag;
1439+
name;
1440+
optional;
1441+
Doc.text ": ";
1442+
printTypExpr ld.pld_type cmtTbl;
14331443
])
14341444

14351445
and printTypExpr (typExpr : Parsetree.core_type) cmtTbl =
@@ -2342,16 +2352,24 @@ and printPatternRecordRow row cmtTbl =
23422352
match row with
23432353
(* punned {x}*)
23442354
| ( ({Location.txt = Longident.Lident ident} as longident),
2345-
{Parsetree.ppat_desc = Ppat_var {txt; _}} )
2355+
{Parsetree.ppat_desc = Ppat_var {txt; _}; ppat_attributes} )
23462356
when ident = txt ->
2347-
printLidentPath longident cmtTbl
2357+
Doc.concat
2358+
[
2359+
printOptionalLabel ppat_attributes;
2360+
printAttributes ppat_attributes cmtTbl;
2361+
printLidentPath longident cmtTbl;
2362+
]
23482363
| longident, pattern ->
23492364
let locForComments =
23502365
{longident.loc with loc_end = pattern.Parsetree.ppat_loc.loc_end}
23512366
in
23522367
let rhsDoc =
23532368
let doc = printPattern pattern cmtTbl in
2354-
if Parens.patternRecordRowRhs pattern then addParens doc else doc
2369+
let doc =
2370+
if Parens.patternRecordRowRhs pattern then addParens doc else doc
2371+
in
2372+
Doc.concat [printOptionalLabel pattern.ppat_attributes; doc]
23552373
in
23562374
let doc =
23572375
Doc.group
@@ -2716,7 +2734,8 @@ and printExpression (e : Parsetree.expression) cmtTbl =
27162734
Doc.join
27172735
~sep:(Doc.concat [Doc.text ","; Doc.line])
27182736
(List.map
2719-
(fun row -> printRecordRow row cmtTbl punningAllowed)
2737+
(fun row ->
2738+
printExpressionRecordRow row cmtTbl punningAllowed)
27202739
rows);
27212740
]);
27222741
Doc.trailingComma;
@@ -4638,20 +4657,26 @@ and printDirectionFlag flag =
46384657
| Asttypes.Downto -> Doc.text " downto "
46394658
| Asttypes.Upto -> Doc.text " to "
46404659

4641-
and printRecordRow (lbl, expr) cmtTbl punningAllowed =
4660+
and printExpressionRecordRow (lbl, expr) cmtTbl punningAllowed =
46424661
let cmtLoc = {lbl.loc with loc_end = expr.pexp_loc.loc_end} in
46434662
let doc =
46444663
Doc.group
46454664
(match expr.pexp_desc with
46464665
| Pexp_ident {txt = Lident key; loc = _keyLoc}
46474666
when punningAllowed && Longident.last lbl.txt = key ->
46484667
(* print punned field *)
4649-
printLidentPath lbl cmtTbl
4668+
Doc.concat
4669+
[
4670+
printAttributes expr.pexp_attributes cmtTbl;
4671+
printOptionalLabel expr.pexp_attributes;
4672+
printLidentPath lbl cmtTbl;
4673+
]
46504674
| _ ->
46514675
Doc.concat
46524676
[
46534677
printLidentPath lbl cmtTbl;
46544678
Doc.text ": ";
4679+
printOptionalLabel expr.pexp_attributes;
46554680
(let doc = printExpressionWithComments expr cmtTbl in
46564681
match Parens.expr expr with
46574682
| Parens.Parenthesized -> addParens doc

0 commit comments

Comments
 (0)