Skip to content

Commit 8b34be1

Browse files
committed
sync latest parser
1 parent 3b05c29 commit 8b34be1

File tree

5 files changed

+92
-32
lines changed

5 files changed

+92
-32
lines changed

analysis/vendor/res_syntax/res_core.ml

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,7 @@ and parseOperandExpr ~context p =
21102110
match p.Parser.token with
21112111
| Assert ->
21122112
Parser.next p;
2113-
let expr = parseUnaryExpr p in
2113+
let expr = parseExpr p in
21142114
let loc = mkLoc startPos p.prevEndPos in
21152115
Ast_helper.Exp.assert_ ~loc expr
21162116
| Lident "async"
@@ -3598,6 +3598,17 @@ and parseCallExpr p funExpr =
35983598
parseCommaDelimitedRegion ~grammar:Grammar.ArgumentList ~closing:Rparen
35993599
~f:parseArgument p
36003600
in
3601+
let resPartialAttr =
3602+
let loc = mkLoc startPos p.prevEndPos in
3603+
(Location.mkloc "res.partial" loc, Parsetree.PStr [])
3604+
in
3605+
let isPartial =
3606+
match p.token with
3607+
| DotDotDot when args <> [] ->
3608+
Parser.next p;
3609+
true
3610+
| _ -> false
3611+
in
36013612
Parser.expect Rparen p;
36023613
let args =
36033614
match args with
@@ -3626,7 +3637,8 @@ and parseCallExpr p funExpr =
36263637
} as expr;
36273638
};
36283639
]
3629-
when (not loc.loc_ghost) && p.mode = ParseForTypeChecker ->
3640+
when (not loc.loc_ghost) && p.mode = ParseForTypeChecker && not isPartial
3641+
->
36303642
(* Since there is no syntax space for arity zero vs arity one,
36313643
* we expand
36323644
* `fn(. ())` into
@@ -3670,22 +3682,20 @@ and parseCallExpr p funExpr =
36703682
| [] -> []
36713683
in
36723684
let apply =
3673-
List.fold_left
3674-
(fun callBody group ->
3685+
Ext_list.fold_left args funExpr (fun callBody group ->
36753686
let dotted, args = group in
36763687
let args, wrap = processUnderscoreApplication p args in
36773688
let exp =
36783689
let uncurried =
36793690
p.uncurried_config |> Res_uncurried.fromDotted ~dotted
36803691
in
3681-
if uncurried then
3682-
let attrs = [uncurriedAppAttr] in
3683-
Ast_helper.Exp.apply ~loc ~attrs callBody args
3684-
else Ast_helper.Exp.apply ~loc callBody args
3692+
let attrs = if uncurried then [uncurriedAppAttr] else [] in
3693+
let attrs = if isPartial then resPartialAttr :: attrs else attrs in
3694+
Ast_helper.Exp.apply ~loc ~attrs callBody args
36853695
in
36863696
wrap exp)
3687-
funExpr args
36883697
in
3698+
36893699
Parser.eatBreadcrumb p;
36903700
apply
36913701

@@ -4488,7 +4498,18 @@ and parseFieldDeclarationRegion ?foundObjectField p =
44884498
let loc = mkLoc startPos typ.ptyp_loc.loc_end in
44894499
let attrs = if optional then optionalAttr :: attrs else attrs in
44904500
Some (Ast_helper.Type.field ~attrs ~loc ~mut name typ)
4491-
| _ -> None
4501+
| _ ->
4502+
if attrs <> [] then
4503+
Parser.err ~startPos p
4504+
(Diagnostics.message
4505+
"Attributes and doc comments can only be used at the beginning of a \
4506+
field declaration");
4507+
if mut = Mutable then
4508+
Parser.err ~startPos p
4509+
(Diagnostics.message
4510+
"The `mutable` qualifier can only be used at the beginning of a \
4511+
field declaration");
4512+
None
44924513

44934514
(* record-decl ::=
44944515
* | { field-decl }
@@ -5773,7 +5794,22 @@ and parseFunctorModuleExpr p =
57735794
* | extension
57745795
* | attributes module-expr *)
57755796
and parseModuleExpr p =
5797+
let hasAwait, loc_await =
5798+
let startPos = p.startPos in
5799+
match p.Parser.token with
5800+
| Await ->
5801+
Parser.expect Await p;
5802+
let endPos = p.endPos in
5803+
(true, mkLoc startPos endPos)
5804+
| _ -> (false, mkLoc startPos startPos)
5805+
in
57765806
let attrs = parseAttributes p in
5807+
let attrs =
5808+
if hasAwait then
5809+
(({txt = "res.await"; loc = loc_await}, PStr []) : Parsetree.attribute)
5810+
:: attrs
5811+
else attrs
5812+
in
57775813
let modExpr =
57785814
if isEs6ArrowFunctor p then parseFunctorModuleExpr p
57795815
else parsePrimaryModExpr p
@@ -6490,4 +6526,4 @@ let parseSpecification p : Parsetree.signature =
64906526

64916527
(* module structure on the file level *)
64926528
let parseImplementation p : Parsetree.structure =
6493-
parseRegion p ~grammar:Grammar.Implementation ~f:parseStructureItemRegion
6529+
parseRegion p ~grammar:Grammar.Implementation ~f:parseStructureItemRegion

analysis/vendor/res_syntax/res_grammar.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ let isFunctorArgStart = function
215215
| _ -> false
216216

217217
let isModExprStart = function
218-
| Token.At | Percent | Uident _ | Lbrace | Lparen | Lident "unpack" -> true
218+
| Token.At | Percent | Uident _ | Lbrace | Lparen | Lident "unpack" | Await ->
219+
true
219220
| _ -> false
220221

221222
let isRecordRowStart = function
@@ -299,7 +300,7 @@ let isListTerminator grammar token =
299300
| _, Token.Eof
300301
| ExprList, (Rparen | Forwardslash | Rbracket)
301302
| ListExpr, Rparen
302-
| ArgumentList, Rparen
303+
| ArgumentList, (Rparen | DotDotDot)
303304
| TypExprList, (Rparen | Forwardslash | GreaterThan | Equal)
304305
| ModExprList, Rparen
305306
| ( (PatternList | PatternOcamlList | PatternRecord),

analysis/vendor/res_syntax/res_parsetree_viewer.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ let processUncurriedAppAttribute attrs =
7272
in
7373
process false [] attrs
7474

75+
let processPartialAppAttribute attrs =
76+
let rec process partialApp acc attrs =
77+
match attrs with
78+
| [] -> (partialApp, List.rev acc)
79+
| ({Location.txt = "res.partial"}, _) :: rest -> process true acc rest
80+
| attr :: rest -> process partialApp (attr :: acc) rest
81+
in
82+
process false [] attrs
83+
7584
type functionAttributesInfo = {
7685
async: bool;
7786
bs: bool;

analysis/vendor/res_syntax/res_parsetree_viewer.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ val processBsAttribute : Parsetree.attributes -> bool * Parsetree.attributes
2020
val processUncurriedAppAttribute :
2121
Parsetree.attributes -> bool * Parsetree.attributes
2222

23+
val processPartialAppAttribute :
24+
Parsetree.attributes -> bool * Parsetree.attributes
25+
2326
type functionAttributesInfo = {
2427
async: bool;
2528
bs: bool;

analysis/vendor/res_syntax/res_printer.ml

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@ and printModuleBinding ~state ~isRec moduleBinding cmtTbl i =
710710
Doc.concat [Doc.text ": "; printModType ~state modType cmtTbl] )
711711
| modExpr -> (printModExpr ~state modExpr cmtTbl, Doc.nil)
712712
in
713+
let modExprDoc =
714+
if ParsetreeViewer.hasAwaitAttribute moduleBinding.pmb_expr.pmod_attributes
715+
then Doc.concat [Doc.text "await "; modExprDoc]
716+
else modExprDoc
717+
in
713718
let modName =
714719
let doc = Doc.text moduleBinding.pmb_name.Location.txt in
715720
printComments doc cmtTbl moduleBinding.pmb_name.loc
@@ -3187,14 +3192,8 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
31873192
| Pexp_letexception (_extensionConstructor, _expr) ->
31883193
printExpressionBlock ~state ~braces:true e cmtTbl
31893194
| Pexp_assert expr ->
3190-
let rhs =
3191-
let doc = printExpressionWithComments ~state expr cmtTbl in
3192-
match Parens.lazyOrAssertOrAwaitExprRhs expr with
3193-
| Parens.Parenthesized -> addParens doc
3194-
| Braced braces -> printBraces doc expr braces
3195-
| Nothing -> doc
3196-
in
3197-
Doc.concat [Doc.text "assert "; rhs]
3195+
let expr = printExpressionWithComments ~state expr cmtTbl in
3196+
Doc.concat [Doc.text "assert("; expr; Doc.text ")"]
31983197
| Pexp_lazy expr ->
31993198
let rhs =
32003199
let doc = printExpressionWithComments ~state expr cmtTbl in
@@ -3970,6 +3969,13 @@ and printPexpApply ~state expr cmtTbl =
39703969
let uncurried, attrs =
39713970
ParsetreeViewer.processUncurriedAppAttribute expr.pexp_attributes
39723971
in
3972+
let partial, attrs = ParsetreeViewer.processPartialAppAttribute attrs in
3973+
let args =
3974+
if partial then
3975+
let dummy = Ast_helper.Exp.constant (Ast_helper.Const.int 0) in
3976+
args @ [(Asttypes.Labelled "...", dummy)]
3977+
else args
3978+
in
39733979
let dotted = state.uncurried_config |> Res_uncurried.getDotted ~uncurried in
39743980
let callExprDoc =
39753981
let doc = printExpressionWithComments ~state callExpr cmtTbl in
@@ -4580,7 +4586,7 @@ and printArguments ~state ~dotted
45804586
and printArgument ~state (argLbl, arg) cmtTbl =
45814587
match (argLbl, arg) with
45824588
(* ~a (punned)*)
4583-
| ( Asttypes.Labelled lbl,
4589+
| ( Labelled lbl,
45844590
({
45854591
pexp_desc = Pexp_ident {txt = Longident.Lident name};
45864592
pexp_attributes = [] | [({Location.txt = "res.namedArgLoc"}, _)];
@@ -4594,7 +4600,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
45944600
let doc = Doc.concat [Doc.tilde; printIdentLike lbl] in
45954601
printComments doc cmtTbl loc
45964602
(* ~a: int (punned)*)
4597-
| ( Asttypes.Labelled lbl,
4603+
| ( Labelled lbl,
45984604
{
45994605
pexp_desc =
46004606
Pexp_constraint
@@ -4622,7 +4628,7 @@ and printArgument ~state (argLbl, arg) cmtTbl =
46224628
in
46234629
printComments doc cmtTbl loc
46244630
(* ~a? (optional lbl punned)*)
4625-
| ( Asttypes.Optional lbl,
4631+
| ( Optional lbl,
46264632
{
46274633
pexp_desc = Pexp_ident {txt = Longident.Lident name};
46284634
pexp_attributes = [] | [({Location.txt = "res.namedArgLoc"}, _)];
@@ -4642,27 +4648,32 @@ and printArgument ~state (argLbl, arg) cmtTbl =
46424648
(loc, {expr with pexp_attributes = attrs})
46434649
| _ -> (expr.pexp_loc, expr)
46444650
in
4645-
let printedLbl =
4651+
let printedLbl, dotdotdot =
46464652
match argLbl with
4647-
| Asttypes.Nolabel -> Doc.nil
4648-
| Asttypes.Labelled lbl ->
4653+
| Nolabel -> (Doc.nil, false)
4654+
| Labelled "..." ->
4655+
let doc = Doc.text "..." in
4656+
(printComments doc cmtTbl argLoc, true)
4657+
| Labelled lbl ->
46494658
let doc = Doc.concat [Doc.tilde; printIdentLike lbl; Doc.equal] in
4650-
printComments doc cmtTbl argLoc
4651-
| Asttypes.Optional lbl ->
4659+
(printComments doc cmtTbl argLoc, false)
4660+
| Optional lbl ->
46524661
let doc =
46534662
Doc.concat [Doc.tilde; printIdentLike lbl; Doc.equal; Doc.question]
46544663
in
4655-
printComments doc cmtTbl argLoc
4664+
(printComments doc cmtTbl argLoc, false)
46564665
in
46574666
let printedExpr =
46584667
let doc = printExpressionWithComments ~state expr cmtTbl in
46594668
match Parens.expr expr with
4660-
| Parens.Parenthesized -> addParens doc
4669+
| Parenthesized -> addParens doc
46614670
| Braced braces -> printBraces doc expr braces
46624671
| Nothing -> doc
46634672
in
46644673
let loc = {argLoc with loc_end = expr.pexp_loc.loc_end} in
4665-
let doc = Doc.concat [printedLbl; printedExpr] in
4674+
let doc =
4675+
if dotdotdot then printedLbl else Doc.concat [printedLbl; printedExpr]
4676+
in
46664677
printComments doc cmtTbl loc
46674678

46684679
and printCases ~state (cases : Parsetree.case list) cmtTbl =

0 commit comments

Comments
 (0)