Skip to content

Commit 4d1c419

Browse files
committed
move back to previous strategy
Since infix operators are weird constraints, scanner shouldn't touch it. Therefore, the printer still needs to distinguish whether the given ident is infix-like or not.
1 parent cb7b5aa commit 4d1c419

File tree

7 files changed

+83
-64
lines changed

7 files changed

+83
-64
lines changed

jscomp/ext/ext_ident.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ let [@inline] no_escape (c : char) =
134134

135135
let is_exotic name =
136136
(* Exotic idents should always wrapped by \"..." *)
137-
match String.unsafe_get name 0 with
138-
| '\\' -> true
139-
| _ -> false
137+
let len = String.length name in
138+
len >= 3
139+
&& String.unsafe_get name 0 = '\\'
140+
&& String.unsafe_get name 1 = '\"'
141+
&& String.unsafe_get name (len - 1) = '\"'
140142

141143
let wrap_exotic name = "\\\"" ^ name ^ "\""
142144

jscomp/stdlib-406/release.ninja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ o stdlib-406/pervasivesU.cmj : cc_cmi stdlib-406/pervasivesU.res | stdlib-406/pe
6868
o stdlib-406/pervasivesU.cmi : cc stdlib-406/pervasivesU.resi | stdlib-406/pervasives.cmj $bsc others
6969
o stdlib-406/queue.cmj : cc_cmi stdlib-406/queue.res | stdlib-406/queue.cmi $bsc others
7070
o stdlib-406/queue.cmi : cc stdlib-406/queue.resi | stdlib-406/pervasives.cmj $bsc others
71-
o stdlib-406/random.cmj : cc_cmi stdlib-406/random.res | stdlib-406/array.cmj stdlib-406/char.cmj stdlib-406/digest.cmj stdlib-406/int32.cmj stdlib-406/int64.cmj stdlib-406/random.cmi $bsc others
71+
o stdlib-406/random.cmj : cc_cmi stdlib-406/random.res | stdlib-406/array.cmj stdlib-406/char.cmj stdlib-406/digest.cmj stdlib-406/int32.cmj stdlib-406/int64.cmj stdlib-406/random.cmi stdlib-406/string.cmj $bsc others
7272
o stdlib-406/random.cmi : cc stdlib-406/random.resi | stdlib-406/int32.cmi stdlib-406/int64.cmi stdlib-406/pervasives.cmj $bsc others
7373
o stdlib-406/set.cmj : cc_cmi stdlib-406/set.res | stdlib-406/list.cmj stdlib-406/set.cmi $bsc others
7474
o stdlib-406/set.cmi : cc stdlib-406/set.resi | stdlib-406/pervasives.cmj $bsc others

jscomp/syntax/src/res_ast_debugger.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module SexpAst = struct
5454
| [] -> [Sexp.list []]
5555
| items -> List.map f items
5656

57-
let string txt = Sexp.atom ("\"" ^ txt ^ "\"")
57+
let string txt = Sexp.atom ("\"" ^ Ext_ident.unwrap_exotic txt ^ "\"")
5858

5959
let char c = Sexp.atom ("'" ^ Char.escaped c ^ "'")
6060

@@ -66,8 +66,7 @@ module SexpAst = struct
6666
let longident l =
6767
let rec loop l =
6868
match l with
69-
| Longident.Lident ident ->
70-
Sexp.list [Sexp.atom "Lident"; string (Ext_ident.unwrap_exotic ident)]
69+
| Longident.Lident ident -> Sexp.list [Sexp.atom "Lident"; string ident]
7170
| Longident.Ldot (lident, txt) ->
7271
Sexp.list [Sexp.atom "Ldot"; loop lident; string txt]
7372
| Longident.Lapply (l1, l2) ->
@@ -602,7 +601,7 @@ module SexpAst = struct
602601
Sexp.list
603602
[
604603
Sexp.atom "Pexp_variant";
605-
string (Ext_ident.unwrap_exotic lbl);
604+
string lbl;
606605
(match exprOpt with
607606
| None -> Sexp.atom "None"
608607
| Some expr -> Sexp.list [Sexp.atom "Some"; expression expr]);
@@ -761,7 +760,7 @@ module SexpAst = struct
761760
Sexp.list
762761
[
763762
Sexp.atom "Ppat_variant";
764-
string (Ext_ident.unwrap_exotic lbl);
763+
string lbl;
765764
(match optPattern with
766765
| None -> Sexp.atom "None"
767766
| Some p -> Sexp.list [Sexp.atom "Some"; pattern p]);
@@ -815,7 +814,7 @@ module SexpAst = struct
815814
Sexp.list
816815
[
817816
Sexp.atom "Rtag";
818-
string (Ext_ident.unwrap_exotic labelLoc.txt);
817+
string labelLoc.txt;
819818
attributes attrs;
820819
Sexp.atom (if truth then "true" else "false");
821820
Sexp.list (mapEmpty ~f:coreType types);

jscomp/syntax/src/res_core.ml

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -384,39 +384,20 @@ let buildLongident words =
384384
| hd :: tl -> List.fold_left (fun p s -> Longident.Ldot (p, s)) (Lident hd) tl
385385

386386
let makeInfixOperator (p : Parser.t) token startPos endPos =
387-
(* FIXME: why this always gonna be true???? *)
388-
let isBinaryOp =
389-
Lexing.(Scanner.isBinaryOp p.scanner.src startPos.pos_cnum endPos.pos_cnum)
390-
in
391387
let stringifiedToken =
392-
if isBinaryOp then
393-
match token with
394-
| Token.Minus ->
395-
print_endline "@@@ binary";
396-
Ext_ident.wrap_exotic "-"
397-
| Token.PlusPlus -> Ext_ident.wrap_exotic "^"
398-
| Token.BangEqual -> Ext_ident.wrap_exotic "<>"
399-
| Token.BangEqualEqual -> Ext_ident.wrap_exotic "!="
400-
| Token.Equal ->
401-
(* TODO: could have a totally different meaning like x->fooSet(y)*)
402-
Parser.err ~startPos ~endPos p
403-
(Diagnostics.message "Did you mean `==` here?");
404-
"="
405-
| Token.EqualEqual -> Ext_ident.wrap_exotic "="
406-
| Token.EqualEqualEqual -> Ext_ident.wrap_exotic "=="
407-
| Token.GreaterThan -> Ext_ident.wrap_exotic ">"
408-
| Token.LessThan -> Ext_ident.wrap_exotic "<"
409-
| token -> Ext_ident.wrap_exotic (Token.toString token)
410-
else
411-
(* unary *)
412-
match token with
413-
| Token.Minus ->
414-
print_endline "@@@ unary";
415-
Ext_ident.wrap_exotic "~-"
416-
| Token.MinusDot -> Ext_ident.wrap_exotic "~-."
417-
| Token.MinusGreater when p.uncurried_config = Legacy -> "|."
418-
| Token.MinusGreater -> "|.u"
419-
| token -> Ext_ident.wrap_exotic (Token.toString token)
388+
if token = Token.MinusGreater then
389+
if p.uncurried_config = Legacy then "|." else "|.u"
390+
else if token = Token.PlusPlus then "^"
391+
else if token = Token.BangEqual then "<>"
392+
else if token = Token.BangEqualEqual then "!="
393+
else if token = Token.Equal then (
394+
(* TODO: could have a totally different meaning like x->fooSet(y)*)
395+
Parser.err ~startPos ~endPos p
396+
(Diagnostics.message "Did you mean `==` here?");
397+
"=")
398+
else if token = Token.EqualEqual then "="
399+
else if token = Token.EqualEqualEqual then "=="
400+
else Token.toString token
420401
in
421402
let loc = mkLoc startPos endPos in
422403
let operator = Location.mkloc (Longident.Lident stringifiedToken) loc in

jscomp/syntax/src/res_printer.ml

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,10 @@ let printLongident = function
376376
| Longident.Lident txt -> Doc.text txt
377377
| lid -> Doc.join ~sep:Doc.dot (printLongidentAux [] lid)
378378

379-
type identifierStyle = ExoticLike | NormalIdent
379+
type polyVarIdentifierStyle = ExoticLike | NormalIdent
380380

381-
let classifyIdentContent txt =
382-
if Ext_ident.is_exotic txt then ExoticLike
383-
else if Token.isKeywordTxt txt then ExoticLike
381+
let classifyPolyVarIdentContent txt =
382+
if Token.isKeywordTxt txt then ExoticLike
384383
else
385384
let len = String.length txt in
386385
let rec loop i =
@@ -406,7 +405,7 @@ let for_all_from s start p =
406405
unsafe_for_all_range s ~start ~finish:(len - 1) p
407406

408407
(* See https://github.com/rescript-lang/rescript-compiler/blob/726cfa534314b586e5b5734471bc2023ad99ebd9/jscomp/ext/ext_string.ml#L510 *)
409-
let isValidNumericPolyvarNumber (x : string) =
408+
let isValidNumericPolyVarNumber (x : string) =
410409
let len = String.length x in
411410
len > 0
412411
&&
@@ -423,12 +422,10 @@ let isValidNumericPolyvarNumber (x : string) =
423422
(* Exotic identifiers in poly-vars have a "lighter" syntax: #"ease-in" *)
424423
let printPolyVarIdent txt =
425424
(* numeric poly-vars don't need quotes: #644 *)
426-
if isValidNumericPolyvarNumber txt then Doc.text txt
425+
if isValidNumericPolyVarNumber txt then Doc.text txt
427426
else
428-
match classifyIdentContent txt with
429-
| ExoticLike ->
430-
Doc.concat
431-
[Doc.text "\""; Doc.text (Ext_ident.unwrap_exotic txt); Doc.text "\""]
427+
match classifyPolyVarIdentContent txt with
428+
| ExoticLike -> Doc.text ("\"" ^ Ext_ident.unwrap_exotic txt ^ "\"")
432429
| NormalIdent -> (
433430
match txt with
434431
| "" -> Doc.concat [Doc.text "\""; Doc.text txt; Doc.text "\""]
@@ -438,6 +435,10 @@ let polyVarIdentToString polyVarIdent =
438435
Doc.concat [Doc.text "#"; printPolyVarIdent polyVarIdent]
439436
|> Doc.toString ~width:80
440437

438+
let printIdentPossiblyInfixOperator txt =
439+
if Res_token.isInfixOperatorTxt txt then Doc.text (Ext_ident.wrap_exotic txt)
440+
else Doc.text txt
441+
441442
let printLident l =
442443
let flatLidOpt lid =
443444
let rec flat accu = function
@@ -448,14 +449,16 @@ let printLident l =
448449
flat [] lid
449450
in
450451
match l with
451-
| Longident.Lident txt -> Doc.text txt
452+
| Longident.Lident txt -> printIdentPossiblyInfixOperator txt
452453
| Longident.Ldot (path, txt) ->
453454
let doc =
454455
match flatLidOpt path with
455456
| Some txts ->
456457
Doc.concat
457458
[
458-
Doc.join ~sep:Doc.dot (List.map Doc.text txts); Doc.dot; Doc.text txt;
459+
Doc.join ~sep:Doc.dot (List.map Doc.text txts);
460+
Doc.dot;
461+
printIdentPossiblyInfixOperator txt;
459462
]
460463
| None -> Doc.text "printLident: Longident.Lapply is not supported"
461464
in
@@ -1053,7 +1056,7 @@ and printValueDescription ~state valueDescription cmtTbl =
10531056
attrs;
10541057
Doc.text header;
10551058
printComments
1056-
(Doc.text valueDescription.pval_name.txt)
1059+
(printIdentPossiblyInfixOperator valueDescription.pval_name.txt)
10571060
cmtTbl valueDescription.pval_name.loc;
10581061
Doc.text ": ";
10591062
printTypExpr ~state valueDescription.pval_type cmtTbl;
@@ -2119,7 +2122,7 @@ and printPattern ~state (p : Parsetree.pattern) cmtTbl =
21192122
let patternWithoutAttributes =
21202123
match p.ppat_desc with
21212124
| Ppat_any -> Doc.text "_"
2122-
| Ppat_var var -> Doc.text var.txt
2125+
| Ppat_var var -> printIdentPossiblyInfixOperator var.txt
21232126
| Ppat_constant c ->
21242127
let templateLiteral =
21252128
ParsetreeViewer.hasTemplateLiteralAttr p.ppat_attributes
@@ -4421,16 +4424,15 @@ and printJsxProp ~state arg cmtTbl =
44214424
* Navabar.createElement -> Navbar
44224425
* Staff.Users.createElement -> Staff.Users *)
44234426
and printJsxName {txt = lident} =
4424-
let printIdent = Doc.text in
44254427
let rec flatten acc lident =
44264428
match lident with
4427-
| Longident.Lident txt -> printIdent txt :: acc
4429+
| Longident.Lident txt -> Doc.text txt :: acc
44284430
| Ldot (lident, "createElement") -> flatten acc lident
4429-
| Ldot (lident, txt) -> flatten (printIdent txt :: acc) lident
4431+
| Ldot (lident, txt) -> flatten (Doc.text txt :: acc) lident
44304432
| _ -> acc
44314433
in
44324434
match lident with
4433-
| Longident.Lident txt -> printIdent txt
4435+
| Longident.Lident txt -> Doc.text txt
44344436
| _ as lident ->
44354437
let segments = flatten [] lident in
44364438
Doc.join ~sep:Doc.dot segments
@@ -4987,7 +4989,7 @@ and printExpFunParameter ~state parameter cmtTbl =
49874989
[
49884990
printAttributes ~state ppat_attributes cmtTbl;
49894991
Doc.text "~";
4990-
Doc.text lbl;
4992+
printIdentPossiblyInfixOperator lbl;
49914993
]
49924994
| ( (Asttypes.Labelled lbl | Optional lbl),
49934995
{
@@ -5000,7 +5002,7 @@ and printExpFunParameter ~state parameter cmtTbl =
50005002
[
50015003
printAttributes ~state ppat_attributes cmtTbl;
50025004
Doc.text "~";
5003-
Doc.text lbl;
5005+
printIdentPossiblyInfixOperator lbl;
50045006
Doc.text ": ";
50055007
printTypExpr ~state typ cmtTbl;
50065008
]
@@ -5009,7 +5011,7 @@ and printExpFunParameter ~state parameter cmtTbl =
50095011
Doc.concat
50105012
[
50115013
Doc.text "~";
5012-
Doc.text lbl;
5014+
printIdentPossiblyInfixOperator lbl;
50135015
Doc.text " as ";
50145016
printPattern ~state pattern cmtTbl;
50155017
]

jscomp/syntax/src/res_scanner.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ let scanExoticIdentifier scanner =
329329
scanner.err ~startPos ~endPos
330330
(Diagnostics.message "A quoted identifier can't be empty string.")
331331
in
332-
Token.Lident ident
332+
if Res_token.isInfixOperatorTxt name then Token.Lident name
333+
else Token.Lident ident
333334

334335
let scanStringEscapeSequence ~startPos scanner =
335336
let scan ~n ~base ~max =

jscomp/syntax/src/res_token.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,38 @@ let isKeywordTxt str =
257257
true
258258
with Not_found -> false
259259

260+
let infixOperatorTable = function
261+
| "==" -> EqualEqual
262+
| "===" -> EqualEqualEqual
263+
| "-" -> Minus
264+
| "-." -> MinusDot
265+
| "+" -> Plus
266+
| "+." -> PlusDot
267+
| "++" -> PlusPlus
268+
| "/" -> Forwardslash
269+
| "/." -> ForwardslashDot
270+
| ">" -> GreaterThan
271+
| "<" -> LessThan
272+
| "*" -> Asterisk
273+
| "*." -> AsteriskDot
274+
| "**" -> Exponentiation
275+
| "||" -> Lor
276+
| "&&" -> Land
277+
| "!=" -> BangEqual
278+
| "!==" -> BangEqualEqual
279+
| ">=" -> GreaterEqual
280+
| "<=" -> LessEqual
281+
| _ -> raise Not_found
282+
[@@raises Not_found]
283+
284+
let isInfixOperatorTxt str =
285+
match str with
286+
| "=" | "<>" | "^" | "~-" | "~-." | ":=" | "|." | "|.u" | "|>" ->
287+
true (* Allow internally aliases to OCaml ones *)
288+
| _ -> (
289+
try
290+
let _ = infixOperatorTable str in
291+
true
292+
with Not_found -> false)
293+
260294
let catch = Lident "catch"

0 commit comments

Comments
 (0)