Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 570f07e

Browse files
committed
Make scanner use char directly instead of converting back and forth between char and int
1 parent 57553f6 commit 570f07e

File tree

7 files changed

+152
-286
lines changed

7 files changed

+152
-286
lines changed

src/res_character_codes.ml

Lines changed: 19 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,18 @@
1-
let eof = -1
1+
let eof = Char.unsafe_chr (-1)
22

3-
let space = 0x0020
4-
let newline = 0x0A (* \n *) [@@live]
5-
let lineFeed = 0x0A (* \n *)
6-
let carriageReturn = 0x0D (* \r *)
7-
let lineSeparator = 0x2028
8-
let paragraphSeparator = 0x2029
3+
let lineSeparator = Char.unsafe_chr 0x2028
4+
let paragraphSeparator = Char.unsafe_chr 0x2029
95

10-
let tab = 0x09
6+
let isUpperCase ch = 'A' <= ch && ch <= 'Z' [@@inline]
7+
let isLowerCase ch = 'a' <= ch && ch <= 'z' [@@inline]
118

12-
let bang = 0x21
13-
let dot = 0x2E
14-
let colon = 0x3A
15-
let comma = 0x2C
16-
let backtick = 0x60
17-
(* let question = 0x3F *)
18-
let semicolon = 0x3B
19-
let underscore = 0x5F
20-
let singleQuote = 0x27
21-
let doubleQuote = 0x22
22-
let equal = 0x3D
23-
let bar = 0x7C
24-
let tilde = 0x7E
25-
let question = 0x3F
26-
let ampersand = 0x26
27-
let at = 0x40
28-
let dollar = 0x24
29-
let percent = 0x25
9+
let isDigit ch = '0' <= ch && ch <= '9' [@@inline]
3010

31-
let lparen = 0x28
32-
let rparen = 0x29
33-
let lbracket = 0x5B
34-
let rbracket = 0x5D
35-
let lbrace = 0x7B
36-
let rbrace = 0x7D
11+
let isLetter ch = isUpperCase ch || isLowerCase ch
3712

38-
let forwardslash = 0x2F (* / *)
39-
let backslash = 0x5C (* \ *)
40-
41-
let greaterThan = 0x3E
42-
let hash = 0x23
43-
let lessThan = 0x3C
44-
45-
let minus = 0x2D
46-
let plus = 0x2B
47-
let asterisk = 0x2A
48-
49-
let _0 = 0x30
50-
let _1 = 0x31 [@@live]
51-
let _2 = 0x32 [@@live]
52-
let _3 = 0x33 [@@live]
53-
let _4 = 0x34 [@@live]
54-
let _5 = 0x35 [@@live]
55-
let _6 = 0x36 [@@live]
56-
let _7 = 0x37 [@@live]
57-
let _8 = 0x38 [@@live]
58-
let _9 = 0x39
59-
60-
module Lower = struct
61-
let a = 0x61
62-
let b = 0x62
63-
let c = 0x63 [@@live]
64-
let d = 0x64 [@@live]
65-
let e = 0x65
66-
let f = 0x66
67-
let g = 0x67
68-
let h = 0x68 [@@live]
69-
let i = 0x69 [@@live]
70-
let j = 0x6A [@@live]
71-
let k = 0x6B [@@live]
72-
let l = 0x6C [@@live]
73-
let m = 0x6D [@@live]
74-
let n = 0x6E
75-
let o = 0x6F
76-
let p = 0x70
77-
let q = 0x71 [@@live]
78-
let r = 0x72
79-
let s = 0x73 [@@live]
80-
let t = 0x74
81-
let u = 0x75 [@@live]
82-
let v = 0x76 [@@live]
83-
let w = 0x77 [@@live]
84-
let x = 0x78
85-
let y = 0x79 [@@live]
86-
let z = 0x7A
87-
end
88-
89-
module Upper = struct
90-
let a = 0x41
91-
(* let b = 0x42 *)
92-
let c = 0x43 [@@live]
93-
let d = 0x44 [@@live]
94-
let e = 0x45 [@@live]
95-
let f = 0x46 [@@live]
96-
let g = 0x47
97-
let h = 0x48 [@@live]
98-
let i = 0x49 [@@live]
99-
let j = 0x4A [@@live]
100-
let k = 0x4B [@@live]
101-
let l = 0x4C [@@live]
102-
let m = 0x4D [@@live]
103-
let b = 0x4E [@@live]
104-
let o = 0x4F [@@live]
105-
let p = 0x50 [@@live]
106-
let q = 0x51 [@@live]
107-
let r = 0x52 [@@live]
108-
let s = 0x53 [@@live]
109-
let t = 0x54 [@@live]
110-
let u = 0x55 [@@live]
111-
let v = 0x56 [@@live]
112-
let w = 0x57 [@@live]
113-
let x = 0x58 [@@live]
114-
let y = 0x59 [@@live]
115-
let z = 0x5a
116-
end
117-
118-
(* returns lower-case ch, ch should be ascii *)
119-
let lower ch =
120-
(* if ch >= Lower.a && ch <= Lower.z then ch else ch + 32 *)
121-
32 lor ch
122-
123-
let isLetter ch =
124-
Lower.a <= ch && ch <= Lower.z ||
125-
Upper.a <= ch && ch <= Upper.z
126-
127-
let isUpperCase ch =
128-
Upper.a <= ch && ch <= Upper.z
129-
130-
let isDigit ch = _0 <= ch && ch <= _9
131-
132-
let isHex ch =
133-
(_0 <= ch && ch <= _9) ||
134-
(Lower.a <= (lower ch) && (lower ch) <= Lower.f)
13+
let isHex = function
14+
| '0'..'9' | 'a'..'f' | 'A'..'F' -> true
15+
| _ -> false
13516

13617
(*
13718
// ES5 7.3:
@@ -146,15 +27,16 @@ let isHex ch =
14627
// breaking characters are treated as white space but not as line terminators.
14728
*)
14829
let isLineBreak ch =
149-
ch == lineFeed
150-
|| ch == carriageReturn
30+
ch == '\n'
31+
|| ch == '\r'
15132
|| ch == lineSeparator
15233
|| ch == paragraphSeparator
15334

15435
let digitValue ch =
155-
if _0 <= ch && ch <= _9 then
156-
ch - 48
157-
else if Lower.a <= (lower ch) && (lower ch) <= Lower.f then
158-
(lower ch) - Lower.a + 10
159-
else
160-
16 (* larger than any legal value *)
36+
match ch with
37+
| '0'..'9' -> (Char.code ch) - 48
38+
| 'a'..'f' ->
39+
(Char.code ch) - (Char.code 'a') + 10
40+
| 'A'..'F' ->
41+
(Char.code ch) + 32 - (Char.code 'a') + 10
42+
| _ -> 16 (* larger than any legal value *)

src/res_core.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ let parseTemplateStringLiteral s =
861861
| '`' as c ->
862862
Buffer.add_char b c;
863863
loop (i + 2)
864-
| c when Res_character_codes.isLineBreak (Char.code c) ->
864+
| c when Res_character_codes.isLineBreak c ->
865865
loop (i + 2)
866866
| c ->
867867
Buffer.add_char b '\\';

src/res_diagnostics.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type category =
1010
| UnclosedString
1111
| UnclosedTemplate
1212
| UnclosedComment
13-
| UnknownUchar of int
13+
| UnknownUchar of Char.t
1414

1515
type t = {
1616
startPos: Lexing.position;
@@ -65,7 +65,7 @@ let explain t =
6565
"This comment seems to be missing a closing `*/`"
6666
| UnknownUchar uchar ->
6767
begin match uchar with
68-
| 94 (* ^ *) ->
68+
| '^' ->
6969
"Hmm, not sure what I should do here with this character.\nIf you're trying to deref an expression, use `foo.contents` instead."
7070
| _ ->
7171
"Hmm, I have no idea what this character means…"

src/res_diagnostics.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ val lident: Token.t -> category
1717
val unclosedString: category
1818
val unclosedTemplate: category
1919
val unclosedComment: category
20-
val unknownUchar: int -> category
20+
val unknownUchar: Char.t -> category
2121
val message: string -> category
2222

2323
val make:

0 commit comments

Comments
 (0)