-
Notifications
You must be signed in to change notification settings - Fork 125
/
jsLex.mll
478 lines (449 loc) · 11.5 KB
/
jsLex.mll
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
{
(* to know what these tokens correspond to, simply
* look at association list below *)
type token =
| Yield
| With
| While
| Void
| Var
| Typeof
| Try
| True
| TimesEqual
| Times
| Tilda
| Throw
| This
| Switch
| Super
| String of (string)
| Static
| Semic
| Rparen
| Return
| Regexp of (string * string)
| Rcurly
| Rbracket
| Question
| Public
| Protected
| Private
| PlusPlus
| PlusEqual
| Plus
| PercentEqual
| Percent
| Package
| Null
| New
| MinusMinus
| MinusEqual
| Minus
| LtLtEqual
| LtLt
| Lt
| Lparen
| Let
| Le
| Lcurly
| Lbracket
| LT
| Interface
| Integer of (string)
| Instanceof
| In
| Import
| Implements
| If
| Ident of (string)
| GtGtGtEqual
| GtGtGt
| GtGtEqual
| GtGt
| Gt
| Ge
| Function
| For
| Finally
| False
| Extends
| Export
| EqualEqualEqual
| EqualEqual
| Equal
| Enum
| Else
| EOF
| Dot
| Do
| DivEqual
| Div
| Delete
| Default
| Debugger
| Continue
| Const
| Comma
| Colon
| Class
| ChapeauEqual
| Chapeau
| Catch
| Case
| Break
| BarEqual
| BarBar
| Bar
| BangEqualEqual
| BangEqual
| Bang
| AmperEqual
| AmperAmper
| Amper
(* the ecmascript defines two kinds of lexing: for the places in the ast
* where a token starting with / is a regular expression, and the places where
* it is the division or the division-assignment /=
* to avoid having to have some information flow from the parser to the lexer,
* this is done by looking at the previous token (see function lex) *)
let can_have_a_division = ref false
(* mapping keywords to tokens *)
let keywords_list = [
"break", Break;
"case", Case;
"catch", Catch;
"continue", Continue;
"debugger", Debugger;
"default", Default;
"delete", Delete;
"do", Do;
"else", Else;
"finally", Finally;
"for", For;
"function", Function;
"if", If;
"in", In;
"instanceof", Instanceof;
"new", New;
"return", Return;
"switch", Switch;
"this", This;
"throw", Throw;
"try", Try;
"typeof", Typeof;
"var", Var;
"void", Void;
"while", While;
"with", With;
"class", Class;
"const", Const;
"enum", Enum;
"export", Export;
"extends", Extends;
"import", Import;
"super", Super;
"implements", Implements;
"interface", Interface;
"let", Let;
"package", Package;
"private", Private;
"protected", Protected;
"public", Public;
"static", Static;
"yield", Yield;
"null", Null;
"true", True;
"false", False;
]
let keywords = Hashtbl.create 100
let () = List.iter (fun (a,b) -> Hashtbl.add keywords a b) keywords_list
(* using a single buffer to store the result of parsing string literals, regexp literals, etc. *)
let b = Buffer.create 1000
}
let identifier_part = ['a'-'z''A'-'Z''_''$''0'-'9']
let identifier = ['a'-'z''A'-'Z''_''$'] identifier_part*
let hexa = ['0'-'9''a'-'f''A'-'F']
rule main = parse
| ['\t''\012''\013'' ']+ { main lexbuf }
(* beware that we must not throw newlines to be able to implement semicolon
* insertions *)
| ['\n' '\r']+ { LT }
| "//" [^'\n''\r']* { main lexbuf }
(* beware that if a newline appears in a multi line comment
* then we _must_ generate a newline token *)
| "/*" { multiline_comment false lexbuf }
| '/' { if !can_have_a_division then Div else (Buffer.clear b; regexp_body lexbuf) }
| "/=" { if !can_have_a_division then DivEqual else (Buffer.clear b; Buffer.add_char b '='; regexp_body lexbuf) }
| '{' { Lcurly }
| '}' { Rcurly }
| '[' { Lbracket }
| ']' { Rbracket }
| '(' { Lparen }
| ')' { Rparen }
| '.' { Dot }
| ';' { Semic }
| ',' { Comma }
| '<' { Lt }
| '>' { Gt }
| "<=" { Le }
| ">=" { Ge }
| "==" { EqualEqual }
| "!=" { BangEqual }
| "===" { EqualEqualEqual }
| "!==" { BangEqualEqual }
| "+" { Plus }
| "-" { Minus }
| "*" { Times }
| "%" { Percent }
| "++" { PlusPlus }
| "--" { MinusMinus }
| "<<" { LtLt }
| ">>" { GtGt }
| ">>>" { GtGtGt }
| "&" { Amper }
| "|" { Bar }
| "^" { Chapeau }
| "!" { Bang }
| "~" { Tilda }
| "&&" { AmperAmper }
| "||" { BarBar }
| "?" { Question }
| ":" { Colon }
| "=" { Equal }
| "+=" { PlusEqual }
| "-=" { MinusEqual }
| "*=" { TimesEqual }
| "%=" { PercentEqual }
| "<<=" { LtLtEqual }
| ">>=" { GtGtEqual }
| ">>>=" { GtGtGtEqual }
| "&=" { AmperEqual }
| "|=" { BarEqual }
| "^=" { ChapeauEqual }
| identifier as s { try Hashtbl.find keywords s with Not_found -> Ident s }
| ('0' | ['1'-'9'] ['0'-'9']*) '.' ['0'-'9']* (['e''E'] ['-''+']? ['0'-'9']+)?
| '.' ['0'-'9']+ (['e''E'] ['-''+']? ['0'-'9']+)?
| ('0' | ['1'-'9'] ['0'-'9']*) (['e''E'] ['-''+']? ['0'-'9']+)?
| '0' ['x''X'] hexa*
as s { Integer s }
| "'" { Buffer.clear b; string false lexbuf }
| '"' { Buffer.clear b; string true lexbuf }
| eof { EOF }
| _ as c { raise (Stream.Error (Printf.sprintf "unexpected character %C in main lexing" c)) }
(* regular expression are not really parsed, you simply interpret them enough
* so that you can find the end of the regexp
* in particular, escapes are *not* interpreted, and so the string in the regexp
* node and token should not be escaped when printed *)
and regexp_body = parse
| ['\r''\n'] { raise (Stream.Error "Line terminator inside a regexp literal") }
| '\\' _ as s { Buffer.add_string b s; regexp_body lexbuf }
| '[' as c { Buffer.add_char b c; character_class lexbuf; regexp_body lexbuf }
| [^'\\''\r''\n''[' '/']+ as s { Buffer.add_string b s; regexp_body lexbuf }
| '/' { let s = Buffer.contents b in
Buffer.clear b;
regexp_flags s lexbuf }
| _ as c { raise (Stream.Error (Printf.sprintf "unexpected character %C in regexp body" c)) }
| eof { raise (Stream.Error "unterminated regexp body ") }
and character_class = parse
| ']' as c { Buffer.add_char b c }
| '\\' _ as s { Buffer.add_string b s; character_class lexbuf }
| [^'\\' ']']+ as s { Buffer.add_string b s; character_class lexbuf }
| _ as c { raise (Stream.Error (Printf.sprintf "unexpected character %C in character class" c)) }
| eof { raise (Stream.Error "unterminated character class ") }
and regexp_flags s1 = parse
| identifier_part* as s2 { Regexp (s1,s2) }
(* [double] is true when the string is enclosed in double quotes
* and false when it is enclosed in single quotes *)
and string double = parse
| "'" { if double then (Buffer.add_char b '\''; string double lexbuf)
else String (Buffer.contents b) }
| '"' { if double then String (Buffer.contents b)
else (Buffer.add_char b '"'; string double lexbuf) }
| [^'\'' '"' '\\''\n''\r']+ as s { Buffer.add_string b s; string double lexbuf }
| ['\n' '\r'] { raise (Stream.Error "Line terminator inside a single string literal") }
| "\\" (['0'-'7'] ['0'-'7']? ['0'-'7']? as s) { Buffer.add_char b (Char.chr (int_of_string s)); string double lexbuf }
| "\\b" { Buffer.add_char b '\008'; string double lexbuf }
| "\\t" { Buffer.add_char b '\t'; string double lexbuf }
| "\\n" { Buffer.add_char b '\n'; string double lexbuf }
| "\\v" { Buffer.add_char b '\011'; string double lexbuf }
| "\\f" { Buffer.add_char b '\012'; string double lexbuf }
| "\\r" { Buffer.add_char b '\r'; string double lexbuf }
| "\\\\" { Buffer.add_char b '\\'; string double lexbuf }
| "\\" (['"''\''] as c) { Buffer.add_char b c; string double lexbuf }
| "\\u" (hexa hexa hexa hexa as s) { Buffer.add_string b (Scanf.sscanf s "%x" (fun d -> Cactutf.cons d)); string double lexbuf }
| "\\x" (hexa hexa as s) { Buffer.add_string b (Scanf.sscanf s "%x" (fun d -> Cactutf.cons d)); string double lexbuf }
| eof { raise (Stream.Error "unterminated string literal comment") }
| "\\" { Buffer.add_char b '\\'; string double lexbuf }
| _ as c { raise (Stream.Error (Printf.sprintf "unexpected character %C in a string literal" c)) }
(* [newline] is true when a newline has been parsed in the comment *)
and multiline_comment newline = parse
| [^'*''\n''\r']* { multiline_comment newline lexbuf }
| ['\r''\n'] { multiline_comment true lexbuf }
| "*/" { if newline then LT else main lexbuf }
| '*' { multiline_comment newline lexbuf }
| eof { raise (Stream.Error "unterminated multiline comment") }
{
(* this global variable is used to ensure that the lexer never returns
* two consecutive new lines, which is useful in the parser, because
* if you want to look at the first non newline token, you need a lookahead
* of 2 with this (otherwise the lookahead would be unbounded) *)
let just_parsed_a_line_terminator = ref true
(* the main lexing function: called the actual lexer, and updates the global
* state *)
let rec lex lexbuf =
match main lexbuf with
| LT when !just_parsed_a_line_terminator ->
(* INVARIANT: there is never two consecutive LT in the token stream *)
(* can have a division doesn't change *)
(* just_parsed_a_line_terminator is still true *)
lex lexbuf
| LT ->
(* can have a division doesn't change *)
just_parsed_a_line_terminator := true;
LT
(* these symbols cannot be followed by a division *)
| Lbracket
| Lcurly
| Rcurly
| Lparen
| Dot
| Semic
| Comma
| Lt
| Gt
| Le
| Ge
| EqualEqual
| BangEqual
| EqualEqualEqual
| BangEqualEqual
| Plus
| Minus
| Times
| Percent
| LtLt
| GtGt
| GtGtGt
| Amper
| Bar
| Chapeau
| Bang
| Tilda
| AmperAmper
| BarBar
| Question
| Colon
| Equal
| PlusEqual
| MinusEqual
| TimesEqual
| PercentEqual
| LtLtEqual
| GtGtEqual
| GtGtGtEqual
| AmperEqual
| BarEqual
| ChapeauEqual
| Div
| DivEqual
| Break
| Case
| Catch
| Continue
| Debugger
| Default
| Delete
| Do
| Else
| Finally
| For
| Function
| If
| In
| Instanceof
| New
| Return
| Switch
| This
| Throw
| Typeof
| Try
| Var
| Void
| While
| With
| Class
| Const
| Enum
| Export
| Extends
| Import
| Super
| Implements
| Interface
| Let
| Package
| Private
| Protected
| Public
| Static
| Yield
as r ->
just_parsed_a_line_terminator := false;
can_have_a_division := false;
r
(* these symbols can be followed by a division *)
| EOF (* don't care *)
| Rbracket
| PlusPlus
| MinusMinus
| Rparen
| Ident _
| False
| True
| Null
| Regexp _
| String _
| Integer _
as r ->
just_parsed_a_line_terminator := false;
can_have_a_division := true;
r
let init_lexer () =
can_have_a_division := false;
just_parsed_a_line_terminator := true
let stream lexbuf =
Stream.from (
fun _ ->
match lex lexbuf with
| EOF -> None
| t -> Some t
)
let stream_of_file file =
init_lexer ();
try
let ic_ = open_in file in
Gc.finalise close_in ic_; (* garbage collecting the input channel *)
let lexbuf = Lexing.from_channel ic_ in
stream lexbuf, lexbuf
with Sys_error diagnostic ->
Printf.printf "Couldn't open file %s: %s\n%!" file diagnostic;
exit 1
let stream_of_string string =
init_lexer ();
let lexbuf = Lexing.from_string string in
stream lexbuf, lexbuf
}