-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathres_token.ml
270 lines (261 loc) · 5.36 KB
/
res_token.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
module Comment = Res_comment
type t =
| Await
| Open
| True
| False
| Codepoint of {c: int; original: string}
| Int of {i: string; suffix: char option}
| Float of {f: string; suffix: char option}
| String of string
| Lident of string
| Uident of string
| As
| Dot
| DotDot
| DotDotDot
| Bang
| Semicolon
| Let
| And
| Rec
| Underscore
| SingleQuote
| Equal
| EqualEqual
| EqualEqualEqual
| Bar
| Lparen
| Rparen
| Lbracket
| Rbracket
| Lbrace
| Rbrace
| Colon
| Comma
| Eof
| Exception
| Backslash [@live]
| Forwardslash
| ForwardslashDot
| Regex of string * string
| Asterisk
| AsteriskDot
| Exponentiation
| Minus
| MinusDot
| Plus
| PlusDot
| PlusPlus
| PlusEqual
| ColonGreaterThan
| GreaterThan
| LessThan
| LessThanSlash
| Hash
| HashEqual
| Assert
| Tilde
| Question
| If
| Else
| For
| In
| While
| Switch
| When
| EqualGreater
| MinusGreater
| External
| Typ
| Private
| Mutable
| Constraint
| Include
| Module
| Of
| Land
| Lor
| Band (* Bitwise and: & *)
| Caret
| BangEqual
| BangEqualEqual
| LessEqual
| GreaterEqual
| ColonEqual
| At
| AtAt
| Percent
| PercentPercent
| Comment of Comment.t
| List
| Dict
| TemplateTail of string * Lexing.position
| TemplatePart of string * Lexing.position
| Backtick
| BarGreater
| Try
| DocComment of Location.t * string
| ModuleComment of Location.t * string
let precedence = function
| HashEqual | ColonEqual -> 1
| Lor -> 2
| Land -> 3
| Bar -> 4
| Caret -> 5
| Equal | EqualEqual | EqualEqualEqual | LessThan | GreaterThan | BangEqual
| BangEqualEqual | LessEqual | GreaterEqual | BarGreater ->
6
| Plus | PlusDot | Minus | MinusDot | PlusPlus -> 7
| Asterisk | AsteriskDot | Forwardslash | ForwardslashDot | Percent -> 8
| Exponentiation -> 9
| MinusGreater -> 10
| Dot -> 11
| _ -> 0
let to_string = function
| Await -> "await"
| Open -> "open"
| True -> "true"
| False -> "false"
| Codepoint {original} -> "codepoint '" ^ original ^ "'"
| String s -> "string \"" ^ s ^ "\""
| Lident str -> str
| Uident str -> str
| Dot -> "."
| DotDot -> ".."
| DotDotDot -> "..."
| Int {i} -> "int " ^ i
| Float {f} -> "Float: " ^ f
| Bang -> "!"
| Semicolon -> ";"
| Let -> "let"
| And -> "and"
| Rec -> "rec"
| Underscore -> "_"
| SingleQuote -> "'"
| Equal -> "="
| EqualEqual -> "=="
| EqualEqualEqual -> "==="
| Eof -> "eof"
| Bar -> "|"
| As -> "as"
| Lparen -> "("
| Rparen -> ")"
| Lbracket -> "["
| Rbracket -> "]"
| Lbrace -> "{"
| Rbrace -> "}"
| ColonGreaterThan -> ":>"
| Colon -> ":"
| Comma -> ","
| Minus -> "-"
| MinusDot -> "-."
| Plus -> "+"
| PlusDot -> "+."
| PlusPlus -> "++"
| PlusEqual -> "+="
| Backslash -> "\\"
| Regex (pattern, flags) -> "regex: /" ^ pattern ^ "/" ^ flags
| Forwardslash -> "/"
| ForwardslashDot -> "/."
| Exception -> "exception"
| Hash -> "#"
| HashEqual -> "#="
| GreaterThan -> ">"
| LessThan -> "<"
| LessThanSlash -> "</"
| Asterisk -> "*"
| AsteriskDot -> "*."
| Exponentiation -> "**"
| Assert -> "assert"
| Tilde -> "tilde"
| Question -> "?"
| If -> "if"
| Else -> "else"
| For -> "for"
| In -> "in"
| While -> "while"
| Switch -> "switch"
| When -> "when"
| EqualGreater -> "=>"
| MinusGreater -> "->"
| External -> "external"
| Typ -> "type"
| Private -> "private"
| Constraint -> "constraint"
| Mutable -> "mutable"
| Include -> "include"
| Module -> "module"
| Of -> "of"
| Lor -> "||"
| Band -> "&"
| Caret -> "^"
| Land -> "&&"
| BangEqual -> "!="
| BangEqualEqual -> "!=="
| GreaterEqual -> ">="
| LessEqual -> "<="
| ColonEqual -> ":="
| At -> "@"
| AtAt -> "@@"
| Percent -> "%"
| PercentPercent -> "%%"
| Comment c -> "Comment" ^ Comment.to_string c
| List -> "list{"
| Dict -> "dict{"
| TemplatePart (text, _) -> text ^ "${"
| TemplateTail (text, _) -> "TemplateTail(" ^ text ^ ")"
| Backtick -> "`"
| BarGreater -> "|>"
| Try -> "try"
| DocComment (_loc, s) -> "DocComment " ^ s
| ModuleComment (_loc, s) -> "ModuleComment " ^ s
let keyword_table = function
| "and" -> And
| "as" -> As
| "assert" -> Assert
| "await" -> Await
| "constraint" -> Constraint
| "else" -> Else
| "exception" -> Exception
| "external" -> External
| "false" -> False
| "for" -> For
| "if" -> If
| "in" -> In
| "include" -> Include
| "let" -> Let
| "list{" -> List
| "dict{" -> Dict
| "module" -> Module
| "mutable" -> Mutable
| "of" -> Of
| "open" -> Open
| "private" -> Private
| "rec" -> Rec
| "switch" -> Switch
| "true" -> True
| "try" -> Try
| "type" -> Typ
| "when" -> When
| "while" -> While
| _ -> raise Not_found
[@@raises Not_found]
let is_keyword = function
| Await | And | As | Assert | Constraint | Else | Exception | External | False
| For | If | In | Include | Land | Let | List | Lor | Module | Mutable | Of
| Open | Private | Rec | Switch | True | Try | Typ | When | While | Dict ->
true
| _ -> false
let lookup_keyword str =
try keyword_table str
with Not_found -> (
match str.[0] [@doesNotRaise] with
| 'A' .. 'Z' -> Uident str
| _ -> Lident str)
let is_keyword_txt str =
try
let _ = keyword_table str in
true
with Not_found -> false
let catch = Lident "catch"