Skip to content

Commit 65e64bf

Browse files
committed
upgrade syntax to 1680ae5
1 parent 37f9c07 commit 65e64bf

File tree

2 files changed

+136
-51
lines changed

2 files changed

+136
-51
lines changed

lib/4.06.1/whole_compiler.ml

Lines changed: 135 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -415154,13 +415154,13 @@ let shouldInlineRhsBinaryExpr rhs = match rhs.pexp_desc with
415154415154

415155415155
let filterPrinteableAttributes attrs =
415156415156
List.filter (fun attr -> match attr with
415157-
| ({Location.txt="bs" | "ns.ternary" | "ns.iflet"}, _) -> false
415157+
| ({Location.txt="bs" | "ns.ternary" | "ns.iflet" | "JSX"}, _) -> false
415158415158
| _ -> true
415159415159
) attrs
415160415160

415161415161
let partitionPrinteableAttributes attrs =
415162415162
List.partition (fun attr -> match attr with
415163-
| ({Location.txt="bs" | "ns.ternary" | "ns.iflet"}, _) -> false
415163+
| ({Location.txt="bs" | "ns.ternary" | "ns.iflet" | "JSX"}, _) -> false
415164415164
| _ -> true
415165415165
) attrs
415166415166

@@ -416933,7 +416933,11 @@ and walkExprArgument (_argLabel, expr) t comments =
416933416933
| Ppat_construct (constr, Some pat) ->
416934416934
let (leading, trailing) = partitionLeadingTrailing comments constr.loc in
416935416935
attach t.leading constr.loc leading;
416936-
let (leading, inside, trailing) = partitionByLoc trailing pat.ppat_loc in
416936+
let (afterConstructor, rest) =
416937+
partitionAdjacentTrailing constr.loc trailing
416938+
in
416939+
attach t.trailing constr.loc afterConstructor;
416940+
let (leading, inside, trailing) = partitionByLoc rest pat.ppat_loc in
416937416941
attach t.leading pat.ppat_loc leading;
416938416942
walkPattern pat t inside;
416939416943
attach t.trailing pat.ppat_loc trailing
@@ -420405,15 +420409,41 @@ let printIdentLike ?allowUident txt =
420405420409
]
420406420410
| NormalIdent -> Doc.text txt
420407420411

420412+
let rec unsafe_for_all_range s ~start ~finish p =
420413+
start > finish ||
420414+
p (String.unsafe_get s start) &&
420415+
unsafe_for_all_range s ~start:(start + 1) ~finish p
420416+
420417+
let for_all_from s start p =
420418+
let len = String.length s in
420419+
unsafe_for_all_range s ~start ~finish:(len - 1) p
420420+
420421+
(* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
420422+
let isValidNumericPolyvarNumber (x : string) =
420423+
let len = String.length x in
420424+
len > 0 && (
420425+
let a = Char.code (String.unsafe_get x 0) in
420426+
a <= 57 &&
420427+
(if len > 1 then
420428+
a > 48 &&
420429+
for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
420430+
else
420431+
a >= 48 )
420432+
)
420433+
420408420434
(* Exotic identifiers in poly-vars have a "lighter" syntax: #"ease-in" *)
420409420435
let printPolyVarIdent txt =
420410-
match classifyIdentContent ~allowUident:true txt with
420411-
| ExoticIdent -> Doc.concat [
420412-
Doc.text "\"";
420413-
Doc.text txt;
420414-
Doc.text"\""
420415-
]
420416-
| NormalIdent -> Doc.text txt
420436+
(* numeric poly-vars don't need quotes: #644 *)
420437+
if isValidNumericPolyvarNumber txt then
420438+
Doc.text txt
420439+
else
420440+
match classifyIdentContent ~allowUident:true txt with
420441+
| ExoticIdent -> Doc.concat [
420442+
Doc.text "\"";
420443+
Doc.text txt;
420444+
Doc.text"\""
420445+
]
420446+
| NormalIdent -> Doc.text txt
420417420447

420418420448

420419420449
let printLident l = match l with
@@ -422118,7 +422148,7 @@ and printPattern (p : Parsetree.pattern) cmtTbl =
422118422148
])
422119422149
)
422120422150
| Ppat_construct(constrName, constructorArgs) ->
422121-
let constrName = printLongident constrName.txt in
422151+
let constrName = printLongidentLocation constrName cmtTbl in
422122422152
let argsDoc = match constructorArgs with
422123422153
| None -> Doc.nil
422124422154
| Some({ppat_loc; ppat_desc = Ppat_construct ({txt = Longident.Lident "()"}, _)}) ->
@@ -423814,7 +423844,11 @@ and printJsxExpression lident args cmtTbl =
423814423844
let name = printJsxName lident in
423815423845
let (formattedProps, children) = printJsxProps args cmtTbl in
423816423846
(* <div className="test" /> *)
423817-
let isSelfClosing = match children with | [] -> true | _ -> false in
423847+
let isSelfClosing =
423848+
match children with
423849+
| Some ({Parsetree.pexp_desc = Pexp_construct ({txt = Longident.Lident "[]"}, None)}) -> true
423850+
| _ -> false
423851+
in
423818423852
Doc.group (
423819423853
Doc.concat [
423820423854
Doc.group (
@@ -423831,7 +423865,10 @@ and printJsxExpression lident args cmtTbl =
423831423865
Doc.indent (
423832423866
Doc.concat [
423833423867
Doc.line;
423834-
printJsxChildren children cmtTbl;
423868+
(match children with
423869+
| Some childrenExpression -> printJsxChildren childrenExpression cmtTbl
423870+
| None -> Doc.nil
423871+
);
423835423872
]
423836423873
);
423837423874
Doc.line;
@@ -423845,17 +423882,17 @@ and printJsxExpression lident args cmtTbl =
423845423882
and printJsxFragment expr cmtTbl =
423846423883
let opening = Doc.text "<>" in
423847423884
let closing = Doc.text "</>" in
423848-
let (children, _) = ParsetreeViewer.collectListExpressions expr in
423885+
(* let (children, _) = ParsetreeViewer.collectListExpressions expr in *)
423849423886
Doc.group (
423850423887
Doc.concat [
423851423888
opening;
423852-
begin match children with
423853-
| [] -> Doc.nil
423854-
| children ->
423889+
begin match expr.pexp_desc with
423890+
| Pexp_construct ({txt = Longident.Lident "[]"}, None) -> Doc.nil
423891+
| _ ->
423855423892
Doc.indent (
423856423893
Doc.concat [
423857423894
Doc.line;
423858-
printJsxChildren children cmtTbl;
423895+
printJsxChildren expr cmtTbl;
423859423896
]
423860423897
)
423861423898
end;
@@ -423864,29 +423901,46 @@ and printJsxFragment expr cmtTbl =
423864423901
]
423865423902
)
423866423903

423867-
and printJsxChildren (children: Parsetree.expression list) cmtTbl =
423868-
Doc.group (
423869-
Doc.join ~sep:Doc.line (
423870-
List.map (fun (expr : Parsetree.expression) ->
423871-
let leadingLineCommentPresent = hasLeadingLineComment cmtTbl expr.pexp_loc in
423872-
let exprDoc = printExpressionWithComments expr cmtTbl in
423873-
match Parens.jsxChildExpr expr with
423874-
| Parenthesized | Braced _ ->
423875-
(* {(20: int)} make sure that we also protect the expression inside *)
423876-
let innerDoc = if Parens.bracedExpr expr then addParens exprDoc else exprDoc in
423877-
if leadingLineCommentPresent then
423878-
addBraces innerDoc
423879-
else
423880-
Doc.concat [Doc.lbrace; innerDoc; Doc.rbrace]
423881-
| Nothing -> exprDoc
423882-
) children
423904+
and printJsxChildren (childrenExpr : Parsetree.expression) cmtTbl =
423905+
match childrenExpr.pexp_desc with
423906+
| Pexp_construct ({txt = Longident.Lident "::"}, _) ->
423907+
let (children, _) = ParsetreeViewer.collectListExpressions childrenExpr in
423908+
Doc.group (
423909+
Doc.join ~sep:Doc.line (
423910+
List.map (fun (expr : Parsetree.expression) ->
423911+
let leadingLineCommentPresent = hasLeadingLineComment cmtTbl expr.pexp_loc in
423912+
let exprDoc = printExpressionWithComments expr cmtTbl in
423913+
match Parens.jsxChildExpr expr with
423914+
| Parenthesized | Braced _ ->
423915+
(* {(20: int)} make sure that we also protect the expression inside *)
423916+
let innerDoc = if Parens.bracedExpr expr then addParens exprDoc else exprDoc in
423917+
if leadingLineCommentPresent then
423918+
addBraces innerDoc
423919+
else
423920+
Doc.concat [Doc.lbrace; innerDoc; Doc.rbrace]
423921+
| Nothing -> exprDoc
423922+
) children
423923+
)
423883423924
)
423884-
)
423925+
| _ ->
423926+
let leadingLineCommentPresent = hasLeadingLineComment cmtTbl childrenExpr.pexp_loc in
423927+
let exprDoc = printExpressionWithComments childrenExpr cmtTbl in
423928+
Doc.concat [
423929+
Doc.dotdotdot;
423930+
match Parens.jsxChildExpr childrenExpr with
423931+
| Parenthesized | Braced _ ->
423932+
let innerDoc = if Parens.bracedExpr childrenExpr then addParens exprDoc else exprDoc in
423933+
if leadingLineCommentPresent then
423934+
addBraces innerDoc
423935+
else
423936+
Doc.concat [Doc.lbrace; innerDoc; Doc.rbrace]
423937+
| Nothing -> exprDoc
423938+
]
423885423939

423886-
and printJsxProps args cmtTbl =
423940+
and printJsxProps args cmtTbl :(Doc.t * Parsetree.expression option) =
423887423941
let rec loop props args =
423888423942
match args with
423889-
| [] -> (Doc.nil, [])
423943+
| [] -> (Doc.nil, None)
423890423944
| [
423891423945
(Asttypes.Labelled "children", children);
423892423946
(
@@ -423905,8 +423959,7 @@ and printJsxProps args cmtTbl =
423905423959
)
423906423960
]
423907423961
) in
423908-
let (children, _) = ParsetreeViewer.collectListExpressions children in
423909-
(formattedProps, children)
423962+
(formattedProps, Some children)
423910423963
| arg::args ->
423911423964
let propDoc = printJsxProp arg cmtTbl in
423912423965
loop (propDoc::props) args
@@ -425863,6 +425916,9 @@ let parseHashIdent ~startPos p =
425863425916
Parser.next p;
425864425917
let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in
425865425918
(text, mkLoc startPos p.prevEndPos)
425919+
| Int {i} ->
425920+
Parser.next p;
425921+
(i, mkLoc startPos p.prevEndPos)
425866425922
| _ ->
425867425923
parseIdent ~startPos ~msg:ErrorMessages.variantIdent p
425868425924

@@ -426366,6 +426422,9 @@ let rec parsePattern ?(alias=true) ?(or_=true) p =
426366426422
Parser.next p;
426367426423
let text = if p.mode = ParseForTypeChecker then parseStringLiteral text else text in
426368426424
(text, mkLoc startPos p.prevEndPos)
426425+
| Int {i} ->
426426+
Parser.next p;
426427+
(i, mkLoc startPos p.prevEndPos)
426369426428
| _ ->
426370426429
parseIdent ~msg:ErrorMessages.variantIdent ~startPos p
426371426430
in
@@ -426766,7 +426825,7 @@ and parseTernaryExpr leftOperand p =
426766426825
| _ ->
426767426826
leftOperand
426768426827

426769-
and parseEs6ArrowExpression ?parameters p =
426828+
and parseEs6ArrowExpression ?context ?parameters p =
426770426829
let startPos = p.Parser.startPos in
426771426830
Parser.leaveBreadcrumb p Grammar.Es6ArrowExpr;
426772426831
let parameters = match parameters with
@@ -426782,7 +426841,7 @@ and parseEs6ArrowExpression ?parameters p =
426782426841
in
426783426842
Parser.expect EqualGreater p;
426784426843
let body =
426785-
let expr = parseExpr p in
426844+
let expr = parseExpr ?context p in
426786426845
match returnType with
426787426846
| Some typ ->
426788426847
Ast_helper.Exp.constraint_
@@ -427298,7 +427357,7 @@ and parseOperandExpr ~context p =
427298427357
if (context != WhenExpr) &&
427299427358
isEs6ArrowExpression ~inTernary:(context=TernaryTrueBranchExpr) p
427300427359
then
427301-
parseEs6ArrowExpression p
427360+
parseEs6ArrowExpression ~context p
427302427361
else
427303427362
parseUnaryExpr p
427304427363
in
@@ -429852,7 +429911,7 @@ and parseTypeEquationOrConstrDecl p =
429852429911
| _ -> (Some typ, Asttypes.Public, Parsetree.Ptype_abstract)
429853429912
end
429854429913
| _ ->
429855-
let uidentEndPos = p.endPos in
429914+
let uidentEndPos = p.prevEndPos in
429856429915
let (args, res) = parseConstrDeclArgs p in
429857429916
let first = Some (
429858429917
let uidentLoc = mkLoc uidentStartPos uidentEndPos in
@@ -432746,6 +432805,28 @@ end = struct
432746432805
module Doc = Res_doc
432747432806
module Token = Res_token
432748432807

432808+
let rec unsafe_for_all_range s ~start ~finish p =
432809+
start > finish ||
432810+
p (String.unsafe_get s start) &&
432811+
unsafe_for_all_range s ~start:(start + 1) ~finish p
432812+
432813+
let for_all_from s start p =
432814+
let len = String.length s in
432815+
unsafe_for_all_range s ~start ~finish:(len - 1) p
432816+
432817+
(* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
432818+
let isValidNumericPolyvarNumber (x : string) =
432819+
let len = String.length x in
432820+
len > 0 && (
432821+
let a = Char.code (String.unsafe_get x 0) in
432822+
a <= 57 &&
432823+
(if len > 1 then
432824+
a > 48 &&
432825+
for_all_from x 1 (function '0' .. '9' -> true | _ -> false)
432826+
else
432827+
a >= 48 )
432828+
)
432829+
432749432830
(* checks if ident contains "arity", like in "arity1", "arity2", "arity3" etc. *)
432750432831
let isArityIdent ident =
432751432832
if String.length ident >= 6 then
@@ -432793,13 +432874,17 @@ let printIdentLike ~allowUident txt =
432793432874
| NormalIdent -> Doc.text txt
432794432875

432795432876
let printPolyVarIdent txt =
432796-
match classifyIdentContent ~allowUident:true txt with
432797-
| ExoticIdent -> Doc.concat [
432798-
Doc.text "\"";
432799-
Doc.text txt;
432800-
Doc.text"\""
432801-
]
432802-
| NormalIdent -> Doc.text txt
432877+
(* numeric poly-vars don't need quotes: #644 *)
432878+
if isValidNumericPolyvarNumber txt then
432879+
Doc.text txt
432880+
else
432881+
match classifyIdentContent ~allowUident:true txt with
432882+
| ExoticIdent -> Doc.concat [
432883+
Doc.text "\"";
432884+
Doc.text txt;
432885+
Doc.text"\""
432886+
]
432887+
| NormalIdent -> Doc.text txt
432803432888

432804432889
(* ReScript doesn't have parenthesized identifiers.
432805432890
* We don't support custom operators. *)

syntax

Submodule syntax updated 171 files

0 commit comments

Comments
 (0)