@@ -2,8 +2,15 @@ module CharacterCodes = Res_character_codes
2
2
module Diagnostics = Res_diagnostics
3
3
module Token = Res_token
4
4
module Comment = Res_comment
5
+
5
6
type mode = Jsx | Diamond
6
7
8
+ (* We hide the implementation detail of the scanner reading character. Our char
9
+ will also contain the special -1 value to indicate end-of-file. This isn't
10
+ ideal; we should clean this up *)
11
+ let hackyEOFChar = Char. unsafe_chr (- 1 )
12
+ type charEncoding = Char .t
13
+
7
14
type t = {
8
15
filename : string ;
9
16
src : bytes ;
@@ -12,7 +19,7 @@ type t = {
12
19
-> endPos : Lexing .position
13
20
-> Diagnostics .category
14
21
-> unit ;
15
- mutable ch : Char .t ; (* current character *)
22
+ mutable ch : charEncoding ; (* current character *)
16
23
mutable offset : int ; (* character offset *)
17
24
mutable rdOffset : int ; (* reading offset (position after current character) *)
18
25
mutable lineOffset : int ; (* current line offset *)
@@ -61,14 +68,14 @@ let next scanner =
61
68
scanner.ch < - ch
62
69
) else (
63
70
scanner.offset < - Bytes. length scanner.src;
64
- scanner.ch < - CharacterCodes. eof
71
+ scanner.ch < - hackyEOFChar
65
72
)
66
73
67
74
let peek scanner =
68
75
if scanner.rdOffset < Bytes. length scanner.src then
69
76
Bytes. unsafe_get scanner.src scanner.rdOffset
70
77
else
71
- CharacterCodes. eof
78
+ hackyEOFChar
72
79
73
80
let make ?(line =1 ) ~filename b =
74
81
let scanner = {
@@ -199,7 +206,7 @@ let scanNumber scanner =
199
206
(Diagnostics. message msg)
200
207
);
201
208
next scanner;
202
- Some ( ch)
209
+ Some ch
203
210
| _ ->
204
211
None
205
212
in
@@ -214,7 +221,7 @@ let scanExoticIdentifier scanner =
214
221
let startPos = position scanner in
215
222
216
223
let rec scan () =
217
- if scanner.ch == CharacterCodes. eof then
224
+ if scanner.ch == hackyEOFChar then
218
225
let endPos = position scanner in
219
226
scanner.err ~start Pos ~end Pos (Diagnostics. message " Did you forget a \" here?" )
220
227
else if scanner.ch == '"' then (
@@ -273,7 +280,7 @@ let scanStringEscapeSequence ~startPos scanner =
273
280
let d = CharacterCodes. digitValue scanner.ch in
274
281
if d > = base then
275
282
let pos = position scanner in
276
- let msg = if scanner.ch == CharacterCodes. eof then
283
+ let msg = if scanner.ch == hackyEOFChar then
277
284
" unclosed escape sequence"
278
285
else " unknown escape sequence"
279
286
in
@@ -295,7 +302,7 @@ let scanString scanner =
295
302
296
303
let startPos = position scanner in
297
304
let rec scan () =
298
- if scanner.ch == CharacterCodes. eof then
305
+ if scanner.ch == hackyEOFChar then
299
306
let endPos = position scanner in
300
307
scanner.err ~start Pos ~end Pos Diagnostics. unclosedString
301
308
else if scanner.ch == '"' then (
@@ -352,7 +359,7 @@ let scanEscape scanner =
352
359
let scanSingleLineComment scanner =
353
360
let startOff = scanner.offset in
354
361
let startPos = position scanner in
355
- while scanner.ch != CharacterCodes. eof && not (CharacterCodes. isLineBreak scanner.ch) do
362
+ while scanner.ch != hackyEOFChar && not (CharacterCodes. isLineBreak scanner.ch) do
356
363
next scanner
357
364
done ;
358
365
let endPos = position scanner in
@@ -371,7 +378,7 @@ let scanMultiLineComment scanner =
371
378
next scanner;
372
379
next scanner;
373
380
if depth > 0 then scan ~depth: (depth - 1 ) () else ()
374
- ) else if scanner.ch == CharacterCodes. eof then (
381
+ ) else if scanner.ch == hackyEOFChar then (
375
382
let endPos = position scanner in
376
383
scanner.err ~start Pos ~end Pos Diagnostics. unclosedComment
377
384
) else if scanner.ch == '/'
@@ -405,7 +412,7 @@ let scanTemplateLiteralToken scanner =
405
412
let startPos = position scanner in
406
413
407
414
let rec scan () =
408
- if scanner.ch == CharacterCodes. eof then (
415
+ if scanner.ch == hackyEOFChar then (
409
416
let endPos = position scanner in
410
417
scanner.err ~start Pos ~end Pos Diagnostics. unclosedTemplate;
411
418
Token. TemplateTail (
@@ -465,7 +472,7 @@ let rec scan scanner =
465
472
)
466
473
else begin
467
474
next scanner;
468
- if ch == CharacterCodes. eof then Token. Eof
475
+ if ch == hackyEOFChar then Token. Eof
469
476
else match ch with
470
477
| '.' ->
471
478
if scanner.ch == '.' then (
@@ -698,21 +705,21 @@ let isBinaryOp src startCnum endCnum =
698
705
in
699
706
let rightOk =
700
707
let c =
701
- if endCnum == Bytes. length src then CharacterCodes. eof
708
+ if endCnum == Bytes. length src then hackyEOFChar
702
709
else endCnum |> (Bytes. get [@ doesNotRaise]) src
703
710
in
704
711
c == ' ' ||
705
712
c == '\t' ||
706
713
CharacterCodes. isLineBreak c ||
707
- c == CharacterCodes. eof
714
+ c == hackyEOFChar
708
715
in
709
716
leftOk && rightOk
710
717
711
718
(* Assume `{` consumed, advances the scanner towards the ends of Reason quoted strings. (for conversion)
712
719
* In {| foo bar |} the scanner will be advanced until after the `|}` *)
713
720
let tryAdvanceQuotedString scanner =
714
721
let rec scanContents tag () =
715
- if scanner.ch == CharacterCodes. eof then (
722
+ if scanner.ch == hackyEOFChar then (
716
723
()
717
724
) else if scanner.ch == '|' then (
718
725
next scanner;
0 commit comments