-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlexer.ulex
399 lines (343 loc) · 14.1 KB
/
lexer.ulex
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
%name Lexer;
%charset UTF8;
%defs (
open Token
(* Local tracing machinery *)
val doTrace = ref false
fun trace ss = if (!doTrace) then LogErr.log ("[lex] " :: ss) else ()
fun error ss = LogErr.lexError ss
type lex_result = TOKEN
fun eof _ = Eof
val lineno = ref 1
fun incr_line _ =
lineno := (!lineno + 1);
fun reset_coords _ =
lineno := 1
val filename = ref ""
val line_breaks : int list ref = ref []
val token_count : int ref = ref 0
(*
fun token_list (line_fn : unit -> string) = (* was token_fn : unit -> token *)
let
fun inputN _ = line_fn()
val this_strm = yyInput.mkStream(inputN)
val this_getc = yyInput.getc
in
yyUTF8.getList this_getc this_strm
end
*)
fun token_list (fname, token_fn : unit -> TOKEN) =
let
val t = ref []
fun add tok = t := (tok, {file = !filename, line = !lineno}) :: !t
fun add_lb offset = line_breaks := offset :: !line_breaks
fun stop _ = (token_count := length (!t); rev (!t))
fun step _ =
let
val tok = token_fn ()
in
trace ["lexed ", tokenname (tok,!lineno)];
(*
* The lexbreak tokens represent choice points for the parser. We
* return two thunks to it: one for each lexer start state
* it might wish to resume lexing in.
*)
case tok of
LexBreakDiv _ => (add tok; stop ())
| LexBreakDivAssign _ => (add tok; stop ())
| LexBreakLessThan _ => (add tok; stop ())
| Eof => (add Eof; stop ())
| Eol => (add_lb (length (!t)); step ())
| x => (add x; step ())
end
in
filename := fname;
line_breaks := [];
step ()
end
fun chopTrailing (s:string)
: string =
String.substring (s, 0, ((String.size s) - 1))
fun followsLineBreak (ts) =
let
val offset = length ts
val max_offset = !token_count
fun findBreak lbs =
case lbs of
[] => (trace ["followsLineBreak false"];false)
| _ =>
(trace ["token_count=", Int.toString(max_offset),
" offset=", Int.toString(max_offset-offset),
" break=", Int.toString(hd lbs)];
if (hd lbs) = (max_offset - offset) then (trace ["followsLineBreak true"];true) else findBreak (tl lbs))
in
findBreak (!line_breaks)
end
val (curr_quote : char ref) = ref #"\000"
val (curr_chars : (char list) ref) = ref []
val (found_newline : bool ref) = ref false
);
%states REGEXP REGEXP_CHARSET XML SINGLE_LINE_COMMENT MULTI_LINE_COMMENT STRING;
(* note: number and letter are predefined unicode character classes:
* %let int = [:number:]*;
* %let id = [:letter:]([:letter:] | [:number:])*;
*)
%let whitespace = [:whitespace:]+;
%let identifierStart = [$A-Za-z_];
%let identifierPart = [$A-Za-z_0-9];
%let identifier = ({identifierStart} {identifierPart}*);
%let hexDigit = [0-9A-Fa-f];
%let decimalDigit = [0-9];
%let nonZeroDigit = [1-9];
%let exponentIndicator = [Ee];
%let decimalDigits = ({decimalDigit}+);
%let signedInteger = (("+" | "-")? {decimalDigits});
%let exponentPart = ({exponentIndicator} {signedInteger});
%let decimalIntegerLiteral = ({decimalDigits});
%let decimalLiteral_1 = ({decimalIntegerLiteral} "." {decimalDigits}? {exponentPart}?);
%let decimalLiteral_2 = ("." {decimalDigits} {exponentPart}?);
%let decimalLiteral_3 = ({decimalIntegerLiteral} {exponentPart}?);
%let decimalLiteral = ({decimalLiteral_1} | {decimalLiteral_2} | {decimalLiteral_3});
%let hexIntegerLiteral = ("0" [Xx] {hexDigit}+);
%let explicitIntLiteral = ({hexIntegerLiteral} | {decimalIntegerLiteral}) "i";
%let explicitUIntHexLiteral = ({hexIntegerLiteral}) "u";
%let explicitUIntDecLiteral = ({decimalIntegerLiteral}) "u";
%let explicitDoubleLiteral = {decimalLiteral} "d";
%let explicitDecimalLiteral = {decimalLiteral} "m";
%let charEscape = "\\" (["'\\bfnrtv] | "x" {hexDigit}{2} | [0-7] | [0-7][0-7] | [0-7][0-7][0-7]);
%let regexpFlags = [a-zA-Z]*;
<INITIAL>"\n" => (incr_line(); Eol);
<INITIAL>"-" => (Minus);
<INITIAL>"--" => (MinusMinus);
<INITIAL>"!" => (Not);
<INITIAL>"!=" => (NotEquals);
<INITIAL>"!==" => (StrictNotEquals);
<INITIAL>"%" => (Modulus);
<INITIAL>"%=" => (ModulusAssign);
<INITIAL>"&" => (BitwiseAnd);
<INITIAL>"&&" => (LogicalAnd);
<INITIAL>"&&=" => (LogicalAndAssign);
<INITIAL>"&=" => (BitwiseAndAssign);
<INITIAL>"(" => (LeftParen);
<INITIAL>")" => (RightParen);
<INITIAL>"*" => (Mult);
<INITIAL>"*=" => (MultAssign);
<INITIAL>"," => (Comma);
<INITIAL>"." => (Dot);
<INITIAL>".." => (DoubleDot);
<INITIAL>"..." => (TripleDot);
<INITIAL>".<" => (LeftDotAngle);
<INITIAL>"/" => (LexBreakDiv
{ lex_initial =
fn _ => (Div, {file = !filename, line = !lineno}) :: token_list (!filename, fn _ => continue ()),
lex_regexp =
fn _ =>
(curr_chars := [#"/"];
YYBEGIN REGEXP;
token_list (!filename, fn _ => continue ())) });
<INITIAL>"/=" => (LexBreakDivAssign
{ lex_initial =
fn _ => (DivAssign, {file = !filename, line = !lineno}) :: token_list (!filename, fn _ => continue ()),
lex_regexp =
fn _ =>
(curr_chars := [#"=",#"/"];
YYBEGIN REGEXP;
token_list (!filename, fn _ => continue ())) });
<INITIAL>":" => (Colon);
<INITIAL>"::" => (DoubleColon);
<INITIAL>";" => (SemiColon);
<INITIAL>"?" => (QuestionMark);
<INITIAL>"@" => (At);
<INITIAL>"[" => (LeftBracket);
<INITIAL>"]" => (RightBracket);
<INITIAL>"^" => (BitwiseXor);
<INITIAL>"^=" => (BitwiseXorAssign);
<INITIAL>"{" => (LeftBrace);
<INITIAL>"|" => (BitwiseOr);
<INITIAL>"||" => (LogicalOr);
<INITIAL>"||=" => (LogicalOrAssign);
<INITIAL>"|=" => (BitwiseOrAssign);
<INITIAL>"}" => (RightBrace);
<INITIAL>"~" => (BitwiseNot);
<INITIAL>"+" => (Plus);
<INITIAL>"++" => (PlusPlus);
<INITIAL>"+=" => (PlusAssign);
<INITIAL>"<" => (LexBreakLessThan
{ lex_initial =
fn _ => (LessThan, {file = !filename, line = !lineno}) :: token_list (!filename, fn _ => continue ()),
lex_xml =
fn _ =>
(YYBEGIN XML;
token_list (!filename, fn _ => continue ())) });
<INITIAL>"<<" => (LeftShift);
<INITIAL>"<<=" => (LeftShiftAssign);
<INITIAL>"<=" => (LessThanOrEquals);
<INITIAL>"=" => (Assign);
<INITIAL>"-=" => (MinusAssign);
<INITIAL>"==" => (Equals);
<INITIAL>"===" => (StrictEquals);
<INITIAL>">" => (GreaterThan);
<INITIAL>">=" => (GreaterThanOrEquals);
<INITIAL>">>" => (RightShift);
<INITIAL>">>=" => (RightShiftAssign);
<INITIAL>">>>" => (UnsignedRightShift);
<INITIAL>">>>=" => (UnsignedRightShiftAssign);
<INITIAL>"as" => (As);
<INITIAL>"break" => (Break);
<INITIAL>"case" => (Case);
<INITIAL>"cast" => (Cast);
<INITIAL>"catch" => (Catch);
<INITIAL>"class" => (Class);
<INITIAL>"const" => (Const);
<INITIAL>"continue" => (Continue);
<INITIAL>"default" => (Default);
<INITIAL>"delete" => (Delete);
<INITIAL>"do" => (Do);
<INITIAL>"else" => (Else);
<INITIAL>"enum" => (Enum);
<INITIAL>"extends" => (Extends);
<INITIAL>"false" => (False);
<INITIAL>"finally" => (Finally);
<INITIAL>"for" => (For);
<INITIAL>"function" => (Function);
<INITIAL>"if" => (If);
<INITIAL>"implements" => (Implements);
<INITIAL>"import" => (Import);
<INITIAL>"in" => (In);
<INITIAL>"instanceof" => (InstanceOf);
<INITIAL>"interface" => (Interface);
<INITIAL>"internal" => (Internal);
<INITIAL>"intrinsic" => (Intrinsic);
<INITIAL>"is" => (Is);
<INITIAL>"let" => (Let);
<INITIAL>"new" => (New);
<INITIAL>"null" => (Null);
<INITIAL>"package" => (Package);
<INITIAL>"precision" => (Precision);
<INITIAL>"private" => (Private);
<INITIAL>"protected" => (Protected);
<INITIAL>"public" => (Public);
<INITIAL>"return" => (Return);
<INITIAL>"super" => (Super);
<INITIAL>"switch" => (Switch);
<INITIAL>"this" => (This);
<INITIAL>"throw" => (Throw);
<INITIAL>"to" => (To);
<INITIAL>"true" => (True);
<INITIAL>"try" => (Try);
<INITIAL>"typeof" => (TypeOf);
<INITIAL>"use" => (Use);
<INITIAL>"var" => (Var);
<INITIAL>"void" => (Void);
<INITIAL>"while" => (While);
<INITIAL>"with" => (With);
<INITIAL>"call" => (Call);
<INITIAL>"debugger" => (Debugger);
<INITIAL>"decimal" => (Decimal);
<INITIAL>"double" => (Double);
<INITIAL>"dynamic" => (Dynamic);
<INITIAL>"each" => (Each);
<INITIAL>"final" => (Final);
<INITIAL>"get" => (Get);
<INITIAL>"goto" => (Goto);
<INITIAL>"has" => (Has);
<INITIAL>"include" => (Include);
<INITIAL>"int" => (Int);
<INITIAL>"namespace" => (Namespace);
<INITIAL>"native" => (Native);
<INITIAL>"number" => (Number);
<INITIAL>"override" => (Override);
<INITIAL>"prototype" => (Prototype);
<INITIAL>"rounding" => (Rounding);
<INITIAL>"standard" => (Standard);
<INITIAL>"strict" => (Strict);
<INITIAL>"uint" => (UInt);
<INITIAL>"set" => (Set);
<INITIAL>"static" => (Static);
<INITIAL>"type" => (Type);
<INITIAL>"undefined" => (Undefined);
<INITIAL>"xml" => (Token.Xml);
<INITIAL>"yield" => (Yield);
<INITIAL>{whitespace} => (continue());
<INITIAL>{explicitIntLiteral} => (case Int32.fromString (chopTrailing yytext) of
SOME i => ExplicitIntLiteral i
| NONE => error ["unexpected input in {explicitIntLiteral}: '", yytext, "'"]);
<INITIAL>{explicitUIntDecLiteral} => (case LargeInt.fromString (chopTrailing yytext) of
SOME i => ExplicitUIntLiteral (Word32.fromLargeInt i)
| NONE => error ["unexpected input in {explicitUIntDecLiteral}: '", yytext, "'"]);
<INITIAL>{explicitUIntHexLiteral} => (case Word32.fromString (chopTrailing yytext) of
SOME i => ExplicitUIntLiteral i
| NONE => error ["unexpected input in {explicitUIntHexLiteral}: '", yytext, "'"]);
<INITIAL>{explicitDoubleLiteral} => (case Real64.fromString (chopTrailing yytext) of
SOME i => ExplicitDoubleLiteral i
| NONE => error ["unexpected input in {explicitDoubleLiteral}: '", yytext, "'"]);
<INITIAL>{explicitDecimalLiteral} => (case Decimal.fromStringDefault (chopTrailing yytext) of
SOME i => ExplicitDecimalLiteral i
| NONE => error ["unexpected input in {explicitDecimalLiteral}: '", yytext, "'"]);
<INITIAL>{decimalIntegerLiteral} => (DecimalIntegerLiteral yytext);
<INITIAL>{hexIntegerLiteral} => (HexIntegerLiteral yytext);
<INITIAL>{decimalLiteral} => (DecimalLiteral yytext);
<INITIAL>"//" => (YYBEGIN SINGLE_LINE_COMMENT; continue());
<SINGLE_LINE_COMMENT>"\n" => (YYBEGIN INITIAL; incr_line(); Eol);
<SINGLE_LINE_COMMENT>. => (continue());
<INITIAL>"/*" => (YYBEGIN MULTI_LINE_COMMENT; continue());
<MULTI_LINE_COMMENT>"*/" => (YYBEGIN INITIAL; continue());
<MULTI_LINE_COMMENT>"\n" => (incr_line(); continue());
<MULTI_LINE_COMMENT>. => (continue());
<REGEXP>"/"{regexpFlags} => (let
val x_flag = String.isSubstring "x" yytext;
val re = String.implode(rev (!curr_chars)) ^ yytext
in
if !found_newline andalso (not x_flag)
then error ["Illegal newline in regexp"]
else
(curr_chars := [];
found_newline := false;
YYBEGIN INITIAL;
RegexpLiteral re)
end);
<REGEXP>"[" => (curr_chars := #"[" :: !curr_chars;
YYBEGIN REGEXP_CHARSET;
continue());
<REGEXP>"\n"|"\r" => (found_newline := true; incr_line(); continue());
<REGEXP>"\\\n"|"\\\r" => (incr_line(); continue());
<REGEXP>"\\". => (curr_chars := String.sub(yytext,1) :: #"\\" :: !curr_chars;
continue());
<REGEXP>. => (curr_chars := String.sub(yytext,0) :: !curr_chars;
continue());
<REGEXP_CHARSET>"]" => (curr_chars := #"]" :: !curr_chars;
YYBEGIN REGEXP;
continue());
<REGEXP_CHARSET>"\n"|"\r" => (found_newline := true; incr_line(); continue());
<REGEXP_CHARSET>"\\\n"|"\\\r" => (incr_line(); continue());
<REGEXP_CHARSET>"\\". => (curr_chars := String.sub(yytext,1) :: #"\\" :: !curr_chars;
continue());
<REGEXP_CHARSET>. => (curr_chars := String.sub(yytext,0) :: !curr_chars;
continue());
<INITIAL>"'"|"\"" => (curr_quote := String.sub (yytext,0);
curr_chars := [];
YYBEGIN STRING;
continue());
<STRING>"'"|"\"" => (if
(!curr_quote) = String.sub (yytext,0)
then
let
val str = (String.implode (rev (!curr_chars)))
in
curr_quote := #"\000";
curr_chars := [];
YYBEGIN INITIAL;
StringLiteral str
end
else
(curr_chars := (String.sub (yytext,0)) :: (!curr_chars);
continue()));
<STRING>{charEscape} => ((case Char.fromCString yytext of
NONE => error ["unexpected input in <STRING>{charEscape}: '", yytext, "'"]
| SOME c => curr_chars := c :: (!curr_chars));
continue());
<STRING>"\\". => (curr_chars := (String.sub (yytext,1)) :: (!curr_chars);
continue());
<STRING>. => (curr_chars := (String.sub (yytext,0)) :: (!curr_chars);
continue());
<INITIAL>. => (error ["unexpected input: '", yytext, "'"]);