Skip to content

Commit 1960ac5

Browse files
committed
Sync latest parser files from rescript-lang/rescript#6073
This fixes highlighting, and other things, in presence of unicode strings.
1 parent d618e4c commit 1960ac5

9 files changed

+80
-33
lines changed

analysis/vendor/res_syntax/res_core.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,7 @@ and parseAttributesAndBinding (p : Parser.t) =
24692469
let err = p.scanner.err in
24702470
let ch = p.scanner.ch in
24712471
let offset = p.scanner.offset in
2472+
let offset16 = p.scanner.offset16 in
24722473
let lineOffset = p.scanner.lineOffset in
24732474
let lnum = p.scanner.lnum in
24742475
let mode = p.scanner.mode in
@@ -2490,6 +2491,7 @@ and parseAttributesAndBinding (p : Parser.t) =
24902491
p.scanner.err <- err;
24912492
p.scanner.ch <- ch;
24922493
p.scanner.offset <- offset;
2494+
p.scanner.offset16 <- offset16;
24932495
p.scanner.lineOffset <- lineOffset;
24942496
p.scanner.lnum <- lnum;
24952497
p.scanner.mode <- mode;

analysis/vendor/res_syntax/res_driver.ml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,31 @@ let printEngine =
131131
print_string (Res_printer.printInterface ~width signature ~comments));
132132
}
133133

134-
let parse_implementation sourcefile =
134+
let parse_implementation ?(ignoreParseErrors = false) sourcefile =
135135
Location.input_name := sourcefile;
136136
let parseResult =
137137
parsingEngine.parseImplementation ~forPrinter:false ~filename:sourcefile
138138
in
139139
if parseResult.invalid then (
140140
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
141-
exit 1);
141+
if not ignoreParseErrors then exit 1);
142142
parseResult.parsetree
143143
[@@raises exit]
144144

145-
let parse_interface sourcefile =
145+
let parse_interface ?(ignoreParseErrors = false) sourcefile =
146146
Location.input_name := sourcefile;
147147
let parseResult =
148148
parsingEngine.parseInterface ~forPrinter:false ~filename:sourcefile
149149
in
150150
if parseResult.invalid then (
151151
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
152-
exit 1);
152+
if not ignoreParseErrors then exit 1);
153153
parseResult.parsetree
154154
[@@raises exit]
155+
156+
(* suppress unused optional arg *)
157+
let _ =
158+
fun s ->
159+
( parse_implementation ~ignoreParseErrors:false s,
160+
parse_interface ~ignoreParseErrors:false s )
161+
[@@raises exit]

analysis/vendor/res_syntax/res_driver.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ val parsingEngine : Res_diagnostics.t list parsingEngine
5353
val printEngine : printEngine
5454

5555
(* ReScript implementation parsing compatible with ocaml pparse driver. Used by the compiler. *)
56-
val parse_implementation : string -> Parsetree.structure
56+
val parse_implementation :
57+
?ignoreParseErrors:bool -> string -> Parsetree.structure
5758
[@@live] [@@raises Location.Error]
5859

5960
(* ReScript interface parsing compatible with ocaml pparse driver. Used by the compiler *)
60-
val parse_interface : string -> Parsetree.signature
61+
val parse_interface : ?ignoreParseErrors:bool -> string -> Parsetree.signature
6162
[@@live] [@@raises Location.Error]

analysis/vendor/res_syntax/res_multi_printer.ml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
let defaultPrintWidth = 100
22

33
(* print res files to res syntax *)
4-
let printRes ~isInterface ~filename =
5-
if isInterface then
4+
let printRes ~ignoreParseErrors ~isInterface ~filename =
5+
if isInterface then (
66
let parseResult =
77
Res_driver.parsingEngine.parseInterface ~forPrinter:true ~filename
88
in
99
if parseResult.invalid then (
1010
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
11-
exit 1)
12-
else
13-
Res_printer.printInterface ~width:defaultPrintWidth
14-
~comments:parseResult.comments parseResult.parsetree
11+
if not ignoreParseErrors then exit 1);
12+
Res_printer.printInterface ~width:defaultPrintWidth
13+
~comments:parseResult.comments parseResult.parsetree)
1514
else
1615
let parseResult =
1716
Res_driver.parsingEngine.parseImplementation ~forPrinter:true ~filename
1817
in
1918
if parseResult.invalid then (
2019
Res_diagnostics.printReport parseResult.diagnostics parseResult.source;
21-
exit 1)
22-
else
23-
Res_printer.printImplementation ~width:defaultPrintWidth
24-
~comments:parseResult.comments parseResult.parsetree
20+
if not ignoreParseErrors then exit 1);
21+
Res_printer.printImplementation ~width:defaultPrintWidth
22+
~comments:parseResult.comments parseResult.parsetree
2523
[@@raises exit]
2624

2725
(* print ocaml files to res syntax *)
@@ -42,12 +40,15 @@ let printMl ~isInterface ~filename =
4240
~comments:parseResult.comments parseResult.parsetree
4341

4442
(* print the given file named input to from "language" to res, general interface exposed by the compiler *)
45-
let print language ~input =
43+
let print ?(ignoreParseErrors = false) language ~input =
4644
let isInterface =
4745
let len = String.length input in
4846
len > 0 && String.unsafe_get input (len - 1) = 'i'
4947
in
5048
match language with
51-
| `res -> printRes ~isInterface ~filename:input
49+
| `res -> printRes ~ignoreParseErrors ~isInterface ~filename:input
5250
| `ml -> printMl ~isInterface ~filename:input
5351
[@@raises exit]
52+
53+
(* suppress unused optional arg *)
54+
let _ = fun s -> print ~ignoreParseErrors:false s [@@raises exit]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(* Interface to print source code from different languages to res.
22
* Takes a filename called "input" and returns the corresponding formatted res syntax *)
3-
val print : [`ml | `res] -> input:string -> string
3+
val print : ?ignoreParseErrors:bool -> [`ml | `res] -> input:string -> string

analysis/vendor/res_syntax/res_parser.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ let lookahead p callback =
159159
let err = p.scanner.err in
160160
let ch = p.scanner.ch in
161161
let offset = p.scanner.offset in
162+
let offset16 = p.scanner.offset16 in
162163
let lineOffset = p.scanner.lineOffset in
163164
let lnum = p.scanner.lnum in
164165
let mode = p.scanner.mode in
@@ -177,6 +178,7 @@ let lookahead p callback =
177178
p.scanner.err <- err;
178179
p.scanner.ch <- ch;
179180
p.scanner.offset <- offset;
181+
p.scanner.offset16 <- offset16;
180182
p.scanner.lineOffset <- lineOffset;
181183
p.scanner.lnum <- lnum;
182184
p.scanner.mode <- mode;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let customLayoutThreshold = 2
2+
3+
type t = {
4+
customLayout: int;
5+
mutable uncurried_config: Res_uncurried.config;
6+
customInfixOperators: (string, string) Hashtbl.t;
7+
}
8+
9+
let init =
10+
{
11+
customLayout = 0;
12+
uncurried_config = Res_uncurried.init;
13+
customInfixOperators = Hashtbl.create 0;
14+
}
15+
16+
let nextCustomLayout t = {t with customLayout = t.customLayout + 1}
17+
18+
let shouldBreakCallback t = t.customLayout > customLayoutThreshold

analysis/vendor/res_syntax/res_scanner.ml

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ type t = {
1919
Diagnostics.category ->
2020
unit;
2121
mutable ch: charEncoding; (* current character *)
22-
mutable offset: int; (* character offset *)
22+
mutable offset: int; (* current byte offset *)
23+
mutable offset16: int;
24+
(* current number of utf16 code units since line start *)
2325
mutable lineOffset: int; (* current line offset *)
2426
mutable lnum: int; (* current line number *)
2527
mutable mode: mode list;
@@ -51,12 +53,11 @@ let position scanner =
5153
(* line number *)
5254
pos_lnum = scanner.lnum;
5355
(* offset of the beginning of the line (number
54-
of characters between the beginning of the scanner and the beginning
56+
of bytes between the beginning of the scanner and the beginning
5557
of the line) *)
5658
pos_bol = scanner.lineOffset;
57-
(* [pos_cnum] is the offset of the position (number of
58-
characters between the beginning of the scanner and the position). *)
59-
pos_cnum = scanner.offset;
59+
(* [pos_cnum - pos_bol] is the number of utf16 code units since line start *)
60+
pos_cnum = scanner.lineOffset + scanner.offset16;
6061
}
6162

6263
(* Small debugging util
@@ -95,19 +96,29 @@ let _printDebug ~startPos ~endPos scanner token =
9596

9697
let next scanner =
9798
let nextOffset = scanner.offset + 1 in
98-
(match scanner.ch with
99-
| '\n' ->
100-
scanner.lineOffset <- nextOffset;
101-
scanner.lnum <- scanner.lnum + 1
99+
let utf16len =
100+
match Ext_utf8.classify scanner.ch with
101+
| Single _ | Invalid -> 1
102+
| Leading (n, _) -> ( (((n + 1) / 2) [@doesNotRaise]))
103+
| Cont _ -> 0
104+
in
105+
let newline =
106+
scanner.ch = '\n'
102107
(* What about CRLF (\r + \n) on windows?
103-
* \r\n will always be terminated by a \n
104-
* -> we can just bump the line count on \n *)
105-
| _ -> ());
108+
\r\n will always be terminated by a \n
109+
-> we can just bump the line count on \n *)
110+
in
111+
if newline then (
112+
scanner.lineOffset <- nextOffset;
113+
scanner.offset16 <- 0;
114+
scanner.lnum <- scanner.lnum + 1)
115+
else scanner.offset16 <- scanner.offset16 + utf16len;
106116
if nextOffset < String.length scanner.src then (
107117
scanner.offset <- nextOffset;
108-
scanner.ch <- String.unsafe_get scanner.src scanner.offset)
118+
scanner.ch <- String.unsafe_get scanner.src nextOffset)
109119
else (
110120
scanner.offset <- String.length scanner.src;
121+
scanner.offset16 <- scanner.offset - scanner.lineOffset;
111122
scanner.ch <- hackyEOFChar)
112123

113124
let next2 scanner =
@@ -141,6 +152,7 @@ let make ~filename src =
141152
err = (fun ~startPos:_ ~endPos:_ _ -> ());
142153
ch = (if src = "" then hackyEOFChar else String.unsafe_get src 0);
143154
offset = 0;
155+
offset16 = 0;
144156
lineOffset = 0;
145157
lnum = 1;
146158
mode = [];
@@ -847,6 +859,7 @@ let rec scan scanner =
847859
| ch, _ ->
848860
next scanner;
849861
let offset = scanner.offset in
862+
let offset16 = scanner.offset16 in
850863
let codepoint, length =
851864
Res_utf8.decodeCodePoint scanner.offset scanner.src
852865
(String.length scanner.src)
@@ -863,6 +876,7 @@ let rec scan scanner =
863876
else (
864877
scanner.ch <- ch;
865878
scanner.offset <- offset;
879+
scanner.offset16 <- offset16;
866880
SingleQuote))
867881
| '!' -> (
868882
match (peek scanner, peek2 scanner) with

analysis/vendor/res_syntax/res_scanner.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ type t = {
1111
Res_diagnostics.category ->
1212
unit;
1313
mutable ch: charEncoding; (* current character *)
14-
mutable offset: int; (* character offset *)
14+
mutable offset: int; (* current byte offset *)
15+
mutable offset16: int;
16+
(* current number of utf16 code units since line start *)
1517
mutable lineOffset: int; (* current line offset *)
1618
mutable lnum: int; (* current line number *)
1719
mutable mode: mode list;

0 commit comments

Comments
 (0)