forked from HaxeFoundation/haxe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
executable file
·418 lines (376 loc) · 12.9 KB
/
lexer.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
(*
* Copyright (C)2005-2012 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*)
{
open Lexing
open Ast
type error_msg =
| Invalid_character of char
| Unterminated_string
| Unterminated_regexp
| Unclosed_comment
| Unclosed_code
| Invalid_escape
| Invalid_option
exception Error of error_msg * pos
let error_msg = function
| Invalid_character c when int_of_char c > 32 && int_of_char c < 128 -> Printf.sprintf "Invalid character '%c'" c
| Invalid_character c -> Printf.sprintf "Invalid character 0x%.2X" (int_of_char c)
| Unterminated_string -> "Unterminated string"
| Unterminated_regexp -> "Unterminated regular expression"
| Unclosed_comment -> "Unclosed comment"
| Unclosed_code -> "Unclosed code string"
| Invalid_escape -> "Invalid escape sequence"
| Invalid_option -> "Invalid regular expression option"
type lexer_file = {
lfile : string;
mutable lline : int;
mutable lmaxline : int;
mutable llines : (int * int) list;
mutable lalines : (int * int) array;
mutable lstrings : int list;
}
let make_file file =
{
lfile = file;
lline = 1;
lmaxline = 1;
llines = [0,1];
lalines = [|0,1|];
lstrings = [];
}
let cur = ref (make_file "")
let all_files = Hashtbl.create 0
let buf = Buffer.create 100
let error e pos =
raise (Error (e,{ pmin = pos; pmax = pos; pfile = !cur.lfile }))
let keywords =
let h = Hashtbl.create 3 in
List.iter (fun k -> Hashtbl.add h (s_keyword k) k)
[Function;Class;Static;Var;If;Else;While;Do;For;
Break;Return;Continue;Extends;Implements;Import;
Switch;Case;Default;Public;Private;Try;Untyped;
Catch;New;This;Throw;Extern;Enum;In;Interface;
Cast;Override;Dynamic;Typedef;Package;
Inline;Using;Null;True;False;Abstract;Macro];
h
let init file =
let f = make_file file in
cur := f;
Hashtbl.replace all_files file f
let save() =
!cur
let restore c =
cur := c
let newline lexbuf =
let cur = !cur in
cur.lline <- cur.lline + 1;
cur.llines <- (lexeme_end lexbuf,cur.lline) :: cur.llines
let fmt_pos p =
p.pmin + (p.pmax - p.pmin) * 1000000
let add_fmt_string p =
let file = (try
Hashtbl.find all_files p.pfile
with Not_found ->
let f = make_file p.pfile in
Hashtbl.replace all_files p.pfile f;
f
) in
file.lstrings <- (fmt_pos p) :: file.lstrings
let fast_add_fmt_string p =
let cur = !cur in
cur.lstrings <- (fmt_pos p) :: cur.lstrings
let is_fmt_string p =
try
let file = Hashtbl.find all_files p.pfile in
List.mem (fmt_pos p) file.lstrings
with Not_found ->
false
let remove_fmt_string p =
try
let file = Hashtbl.find all_files p.pfile in
file.lstrings <- List.filter ((<>) (fmt_pos p)) file.lstrings
with Not_found ->
()
let find_line p f =
(* rebuild cache if we have a new line *)
if f.lmaxline <> f.lline then begin
f.lmaxline <- f.lline;
f.lalines <- Array.of_list (List.rev f.llines);
end;
let rec loop min max =
let med = (min + max) lsr 1 in
let lp, line = Array.unsafe_get f.lalines med in
if med = min then
line, p - lp
else if lp > p then
loop min med
else
loop med max
in
loop 0 (Array.length f.lalines)
(* resolve a position within a non-haxe file by counting newlines *)
let resolve_pos file =
let ch = open_in_bin file in
let f = make_file file in
let rec loop p =
let inc i () =
f.lline <- f.lline + 1;
f.llines <- (p + i,f.lline) :: f.llines;
i
in
let i = match input_char ch with
| '\n' -> inc 1
| '\r' ->
ignore(input_char ch);
inc 2
| _ -> fun () -> 1
in
loop (p + i())
in
try
loop 0
with End_of_file ->
close_in ch;
f
let find_file file =
try Hashtbl.find all_files file with Not_found -> try resolve_pos file with Sys_error _ -> make_file file
let find_pos p =
find_line p.pmin (find_file p.pfile)
let get_error_line p =
let l, _ = find_pos p in
l
let get_error_pos printer p =
if p.pmin = -1 then
"(unknown)"
else
let file = find_file p.pfile in
let l1, p1 = find_line p.pmin file in
let l2, p2 = find_line p.pmax file in
if l1 = l2 then begin
let s = (if p1 = p2 then Printf.sprintf " %d" p1 else Printf.sprintf "s %d-%d" p1 p2) in
Printf.sprintf "%s character%s" (printer p.pfile l1) s
end else
Printf.sprintf "%s lines %d-%d" (printer p.pfile l1) l1 l2
let reset() = Buffer.reset buf
let contents() = Buffer.contents buf
let store lexbuf = Buffer.add_string buf (lexeme lexbuf)
let add c = Buffer.add_string buf c
let mk_tok t pmin pmax =
t , { pfile = !cur.lfile; pmin = pmin; pmax = pmax }
let mk lexbuf t =
mk_tok t (lexeme_start lexbuf) (lexeme_end lexbuf)
let mk_ident lexbuf =
let s = lexeme lexbuf in
mk lexbuf (try Kwd (Hashtbl.find keywords s) with Not_found -> Const (Ident s))
let invalid_char lexbuf =
error (Invalid_character (lexeme_char lexbuf 0)) (lexeme_start lexbuf)
}
let ident = ('_'* ['a'-'z'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']* | '_'+ | '_'+ ['0'-'9'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']* )
let idtype = '_'* ['A'-'Z'] ['_' 'a'-'z' 'A'-'Z' '0'-'9']*
rule skip_header = parse
| "\239\187\191" { skip_header lexbuf }
| "#!" [^'\n' '\r']* { skip_header lexbuf }
| "" | eof { }
and token = parse
| eof { mk lexbuf Eof }
| [' ' '\t']+ { token lexbuf }
| "\r\n" { newline lexbuf; token lexbuf }
| '\n' | '\r' { newline lexbuf; token lexbuf }
| "0x" ['0'-'9' 'a'-'f' 'A'-'F']+ { mk lexbuf (Const (Int (lexeme lexbuf))) }
| ['0'-'9']+ { mk lexbuf (Const (Int (lexeme lexbuf))) }
| ['0'-'9']+ '.' ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
| '.' ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
| ['0'-'9']+ ['e' 'E'] ['+' '-']? ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
| ['0'-'9']+ '.' ['0'-'9']* ['e' 'E'] ['+' '-']? ['0'-'9']+ { mk lexbuf (Const (Float (lexeme lexbuf))) }
| ['0'-'9']+ "..." {
let s = lexeme lexbuf in
mk lexbuf (IntInterval (String.sub s 0 (String.length s - 3)))
}
| "//" [^'\n' '\r']* {
let s = lexeme lexbuf in
mk lexbuf (CommentLine (String.sub s 2 ((String.length s)-2)))
}
| "++" { mk lexbuf (Unop Increment) }
| "--" { mk lexbuf (Unop Decrement) }
| "~" { mk lexbuf (Unop NegBits) }
| "%=" { mk lexbuf (Binop (OpAssignOp OpMod)) }
| "&=" { mk lexbuf (Binop (OpAssignOp OpAnd)) }
| "|=" { mk lexbuf (Binop (OpAssignOp OpOr)) }
| "^=" { mk lexbuf (Binop (OpAssignOp OpXor)) }
| "+=" { mk lexbuf (Binop (OpAssignOp OpAdd)) }
| "-=" { mk lexbuf (Binop (OpAssignOp OpSub)) }
| "*=" { mk lexbuf (Binop (OpAssignOp OpMult)) }
| "/=" { mk lexbuf (Binop (OpAssignOp OpDiv)) }
| "<<=" { mk lexbuf (Binop (OpAssignOp OpShl)) }
(*//| ">>=" { mk lexbuf (Binop (OpAssignOp OpShr)) } *)
(*//| ">>>=" { mk lexbuf (Binop (OpAssignOp OpUShr)) } *)
| "==" { mk lexbuf (Binop OpEq) }
| "!=" { mk lexbuf (Binop OpNotEq) }
| "<=" { mk lexbuf (Binop OpLte) }
(*//| ">=" { mk lexbuf (Binop OpGte) }*)
| "&&" { mk lexbuf (Binop OpBoolAnd) }
| "||" { mk lexbuf (Binop OpBoolOr) }
| "<<" { mk lexbuf (Binop OpShl) }
| "->" { mk lexbuf Arrow }
| "..." { mk lexbuf (Binop OpInterval) }
| "=>" { mk lexbuf (Binop OpArrow)}
| "!" { mk lexbuf (Unop Not) }
| "<" { mk lexbuf (Binop OpLt) }
| ">" { mk lexbuf (Binop OpGt) }
| ";" { mk lexbuf Semicolon }
| ":" { mk lexbuf DblDot }
| "," { mk lexbuf Comma }
| "." { mk lexbuf Dot }
| "%" { mk lexbuf (Binop OpMod) }
| "&" { mk lexbuf (Binop OpAnd) }
| "|" { mk lexbuf (Binop OpOr) }
| "^" { mk lexbuf (Binop OpXor) }
| "+" { mk lexbuf (Binop OpAdd) }
| "*" { mk lexbuf (Binop OpMult) }
| "/" { mk lexbuf (Binop OpDiv) }
| "-" { mk lexbuf (Binop OpSub) }
| "=" { mk lexbuf (Binop OpAssign) }
| "[" { mk lexbuf BkOpen }
| "]" { mk lexbuf BkClose }
| "{" { mk lexbuf BrOpen }
| "}" { mk lexbuf BrClose }
| "(" { mk lexbuf POpen }
| ")" { mk lexbuf PClose }
| "?" { mk lexbuf Question }
| "@" { mk lexbuf At }
| "/*" {
reset();
let pmin = lexeme_start lexbuf in
let pmax = (try comment lexbuf with Exit -> error Unclosed_comment pmin) in
mk_tok (Comment (contents())) pmin pmax;
}
| '"' {
reset();
let pmin = lexeme_start lexbuf in
let pmax = (try string lexbuf with Exit -> error Unterminated_string pmin) in
let str = (try unescape (contents()) with Exit -> error Invalid_escape pmin) in
mk_tok (Const (String str)) pmin pmax;
}
| "'" {
reset();
let pmin = lexeme_start lexbuf in
let pmax = (try string2 lexbuf with Exit -> error Unterminated_string pmin) in
let str = (try unescape (contents()) with Exit -> error Invalid_escape pmin) in
let t = mk_tok (Const (String str)) pmin pmax in
fast_add_fmt_string (snd t);
t
}
| "~/" {
reset();
let pmin = lexeme_start lexbuf in
let options, pmax = (try regexp lexbuf with Exit -> error Unterminated_regexp pmin) in
let str = contents() in
mk_tok (Const (Regexp (str,options))) pmin pmax;
}
| '#' ident {
let v = lexeme lexbuf in
let v = String.sub v 1 (String.length v - 1) in
mk lexbuf (Sharp v)
}
| '$' ['_' 'a'-'z' 'A'-'Z' '0'-'9']* {
let v = lexeme lexbuf in
let v = String.sub v 1 (String.length v - 1) in
mk lexbuf (Dollar v)
}
| ident { mk_ident lexbuf }
| idtype { mk lexbuf (Const (Ident (lexeme lexbuf))) }
| _ { invalid_char lexbuf }
and comment = parse
| eof { raise Exit }
| '\n' | '\r' | "\r\n" { newline lexbuf; store lexbuf; comment lexbuf }
| "*/" { lexeme_end lexbuf }
| '*' { store lexbuf; comment lexbuf }
| [^'*' '\n' '\r']+ { store lexbuf; comment lexbuf }
and string = parse
| eof { raise Exit }
| '\n' | '\r' | "\r\n" { newline lexbuf; store lexbuf; string lexbuf }
| "\\\"" { store lexbuf; string lexbuf }
| "\\\\" { store lexbuf; string lexbuf }
| '\\' { store lexbuf; string lexbuf }
| '"' { lexeme_end lexbuf }
| [^'"' '\\' '\r' '\n']+ { store lexbuf; string lexbuf }
and string2 = parse
| eof { raise Exit }
| '\n' | '\r' | "\r\n" { newline lexbuf; store lexbuf; string2 lexbuf }
| '\\' { store lexbuf; string2 lexbuf }
| "\\\\" { store lexbuf; string2 lexbuf }
| "\\'" { store lexbuf; string2 lexbuf }
| "'" { lexeme_end lexbuf }
| "$$" | "\\$" | '$' { store lexbuf; string2 lexbuf }
| "${" {
let pmin = lexeme_start lexbuf in
store lexbuf;
(try code_string lexbuf with Exit -> error Unclosed_code pmin);
string2 lexbuf;
}
| [^'\'' '\\' '\r' '\n' '$']+ { store lexbuf; string2 lexbuf }
and code_string = parse
| eof { raise Exit }
| '\n' | '\r' | "\r\n" { newline lexbuf; store lexbuf; code_string lexbuf }
| '{' | '/' { store lexbuf; code_string lexbuf }
| '}' { store lexbuf; (* stop *) }
| '"' {
add "\"";
let pmin = lexeme_start lexbuf in
(try ignore(string lexbuf) with Exit -> error Unterminated_string pmin);
add "\"";
code_string lexbuf;
}
| "'" {
add "'";
let pmin = lexeme_start lexbuf in
let pmax = (try string2 lexbuf with Exit -> error Unterminated_string pmin) in
add "'";
fast_add_fmt_string { pfile = !cur.lfile; pmin = pmin; pmax = pmax };
code_string lexbuf;
}
| "/*" {
let pmin = lexeme_start lexbuf in
(try ignore(comment lexbuf) with Exit -> error Unclosed_comment pmin);
code_string lexbuf;
}
| "//" [^'\n' '\r']* { store lexbuf; code_string lexbuf; }
| [^'/' '"' '\'' '{' '}' '\n' '\r']+ { store lexbuf; code_string lexbuf; }
and regexp = parse
| eof | '\n' | '\r' { raise Exit }
| '\\' '/' { add "/"; regexp lexbuf }
| '\\' 'r' { add "\r"; regexp lexbuf }
| '\\' 'n' { add "\n"; regexp lexbuf }
| '\\' 't' { add "\t"; regexp lexbuf }
| '\\' ['\\' '$' '.' '*' '+' '^' '|' '{' '}' '[' ']' '(' ')' '?' '-' '0'-'9'] { add (lexeme lexbuf); regexp lexbuf }
| '\\' ['w' 'W' 'b' 'B' 's' 'S' 'd' 'D' 'x'] { add (lexeme lexbuf); regexp lexbuf }
| '\\' ['u' 'U'] ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] ['0'-'9' 'a'-'f' 'A'-'F'] { add (lexeme lexbuf); regexp lexbuf }
| '\\' [^ '\\'] { error (Invalid_character (lexeme lexbuf).[1]) (lexeme_end lexbuf - 1) }
| '/' { regexp_options lexbuf, lexeme_end lexbuf }
| [^ '\\' '/' '\r' '\n']+ { store lexbuf; regexp lexbuf }
and regexp_options = parse
| 'g' | 'i' | 'm' | 's' | 'u' {
let l = lexeme lexbuf in
l ^ regexp_options lexbuf
}
| ['a' - 'z'] { error Invalid_option (lexeme_start lexbuf) }
| "" { "" }