Mercurial > projects > ddmd
annotate dmd/Lexer.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | 60bb0fe4563e |
children | b7b61140701d |
rev | line source |
---|---|
0 | 1 module dmd.Lexer; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.StringTable; |
5 import dmd.OutBuffer; | |
6 import dmd.Token; | |
7 import dmd.Loc; | |
8 import dmd.Module; | |
9 import dmd.Identifier; | |
10 import dmd.TOK; | |
11 import dmd.Keyword; | |
12 import dmd.StringValue; | |
13 import dmd.Global; | |
14 import dmd.Util; | |
15 import dmd.Id; | |
16 import dmd.Dchar; | |
17 import dmd.Utf; | |
18 | |
19 import std.stdio : writeln; | |
20 | |
4 | 21 import core.memory; |
2 | 22 |
0 | 23 import core.stdc.ctype; |
24 import core.stdc.stdlib; | |
25 import core.stdc.string; | |
26 import core.stdc.stdio; | |
27 import core.stdc.time; | |
28 import core.stdc.errno; | |
29 | |
30 enum LS = 0x2028; // UTF line separator | |
31 enum PS = 0x2029; // UTF paragraph separator | |
32 | |
33 extern (C) extern | |
34 { | |
35 __gshared char* __locale_decpoint; | |
36 } | |
37 | |
38 int isUniAlpha(uint u) | |
39 { | |
40 assert(false); | |
41 } | |
42 | |
43 class Lexer | |
44 { | |
45 static StringTable stringtable; | |
46 static OutBuffer stringbuffer; | |
47 static Token* freelist; | |
48 | |
49 Loc loc; // for error messages | |
50 | |
51 ubyte* base; // pointer to start of buffer | |
52 ubyte* end; // past end of buffer | |
53 ubyte* p; // current character | |
54 Token token; | |
55 Module mod; | |
56 int doDocComment; // collect doc comment information | |
57 int anyToken; // !=0 means seen at least one token | |
58 int commentToken; // !=0 means comments are TOKcomment's | |
59 | |
60 static this() | |
61 { | |
62 stringtable = new StringTable(); | |
63 stringbuffer = new OutBuffer(); | |
64 } | |
65 | |
66 static ~this() | |
67 { | |
34 | 68 //delete stringtable; |
0 | 69 } |
70 | |
71 this(Module mod, ubyte* base, uint begoffset, uint endoffset, int doDocComment, int commentToken) | |
72 { | |
73 loc = Loc(mod, 1); | |
74 | |
75 memset(&token,0,token.sizeof); | |
76 this.base = base; | |
77 this.end = base + endoffset; | |
78 p = base + begoffset; | |
79 this.mod = mod; | |
80 this.doDocComment = doDocComment; | |
81 this.anyToken = 0; | |
82 this.commentToken = commentToken; | |
83 //initKeywords(); | |
84 | |
85 /* If first line starts with '#!', ignore the line | |
86 */ | |
87 | |
88 if (p[0] == '#' && p[1] =='!') | |
89 { | |
90 p += 2; | |
91 while (1) | |
92 { | |
93 ubyte c = *p; | |
94 switch (c) | |
95 { | |
96 case '\n': | |
97 p++; | |
98 break; | |
99 | |
100 case '\r': | |
101 p++; | |
102 if (*p == '\n') | |
103 p++; | |
104 break; | |
105 | |
106 case 0: | |
107 case 0x1A: | |
108 break; | |
109 | |
110 default: | |
111 if (c & 0x80) | |
112 { | |
113 uint u = decodeUTF(); | |
114 if (u == PS || u == LS) | |
115 break; | |
116 } | |
117 p++; | |
118 continue; | |
119 } | |
120 break; | |
121 } | |
122 loc.linnum = 2; | |
123 } | |
124 } | |
125 | |
126 version (DMDV2) { | |
127 static Keyword[] keywords = | |
128 [ | |
129 // { "", TOK }, | |
130 | |
131 { "this", TOK.TOKthis }, | |
132 { "super", TOK.TOKsuper }, | |
133 { "assert", TOK.TOKassert }, | |
134 { "null", TOK.TOKnull }, | |
135 { "true", TOK.TOKtrue }, | |
136 { "false", TOK.TOKfalse }, | |
137 { "cast", TOK.TOKcast }, | |
138 { "new", TOK.TOKnew }, | |
139 { "delete", TOK.TOKdelete }, | |
140 { "throw", TOK.TOKthrow }, | |
141 { "module", TOK.TOKmodule }, | |
142 { "pragma", TOK.TOKpragma }, | |
143 { "typeof", TOK.TOKtypeof }, | |
144 { "typeid", TOK.TOKtypeid }, | |
145 | |
146 { "template", TOK.TOKtemplate }, | |
147 | |
148 { "void", TOK.TOKvoid }, | |
149 { "byte", TOK.TOKint8 }, | |
150 { "ubyte", TOK.TOKuns8 }, | |
151 { "short", TOK.TOKint16 }, | |
152 { "ushort", TOK.TOKuns16 }, | |
153 { "int", TOK.TOKint32 }, | |
154 { "uint", TOK.TOKuns32 }, | |
155 { "long", TOK.TOKint64 }, | |
156 { "ulong", TOK.TOKuns64 }, | |
157 { "cent", TOK.TOKcent, }, | |
158 { "ucent", TOK.TOKucent, }, | |
159 { "float", TOK.TOKfloat32 }, | |
160 { "double", TOK.TOKfloat64 }, | |
161 { "real", TOK.TOKfloat80 }, | |
162 | |
163 { "bool", TOK.TOKbool }, | |
164 { "char", TOK.TOKchar }, | |
165 { "wchar", TOK.TOKwchar }, | |
166 { "dchar", TOK.TOKdchar }, | |
167 | |
168 { "ifloat", TOK.TOKimaginary32 }, | |
169 { "idouble", TOK.TOKimaginary64 }, | |
170 { "ireal", TOK.TOKimaginary80 }, | |
171 | |
172 { "cfloat", TOK.TOKcomplex32 }, | |
173 { "cdouble", TOK.TOKcomplex64 }, | |
174 { "creal", TOK.TOKcomplex80 }, | |
175 | |
176 { "delegate", TOK.TOKdelegate }, | |
177 { "function", TOK.TOKfunction }, | |
178 | |
179 { "is", TOK.TOKis }, | |
180 { "if", TOK.TOKif }, | |
181 { "else", TOK.TOKelse }, | |
182 { "while", TOK.TOKwhile }, | |
183 { "for", TOK.TOKfor }, | |
184 { "do", TOK.TOKdo }, | |
185 { "switch", TOK.TOKswitch }, | |
186 { "case", TOK.TOKcase }, | |
187 { "default", TOK.TOKdefault }, | |
188 { "break", TOK.TOKbreak }, | |
189 { "continue", TOK.TOKcontinue }, | |
190 { "synchronized", TOK.TOKsynchronized }, | |
191 { "return", TOK.TOKreturn }, | |
192 { "goto", TOK.TOKgoto }, | |
193 { "try", TOK.TOKtry }, | |
194 { "catch", TOK.TOKcatch }, | |
195 { "finally", TOK.TOKfinally }, | |
196 { "with", TOK.TOKwith }, | |
197 { "asm", TOK.TOKasm }, | |
198 { "foreach", TOK.TOKforeach }, | |
199 { "foreach_reverse", TOK.TOKforeach_reverse }, | |
200 { "scope", TOK.TOKscope }, | |
201 | |
202 { "struct", TOK.TOKstruct }, | |
203 { "class", TOK.TOKclass }, | |
204 { "interface", TOK.TOKinterface }, | |
205 { "union", TOK.TOKunion }, | |
206 { "enum", TOK.TOKenum }, | |
207 { "import", TOK.TOKimport }, | |
208 { "mixin", TOK.TOKmixin }, | |
209 { "static", TOK.TOKstatic }, | |
210 { "final", TOK.TOKfinal }, | |
211 { "const", TOK.TOKconst }, | |
212 { "typedef", TOK.TOKtypedef }, | |
213 { "alias", TOK.TOKalias }, | |
214 { "override", TOK.TOKoverride }, | |
215 { "abstract", TOK.TOKabstract }, | |
216 { "volatile", TOK.TOKvolatile }, | |
217 { "debug", TOK.TOKdebug }, | |
218 { "deprecated", TOK.TOKdeprecated }, | |
219 { "in", TOK.TOKin }, | |
220 { "out", TOK.TOKout }, | |
221 { "inout", TOK.TOKinout }, | |
222 { "lazy", TOK.TOKlazy }, | |
223 { "auto", TOK.TOKauto }, | |
224 | |
225 { "align", TOK.TOKalign }, | |
226 { "extern", TOK.TOKextern }, | |
227 { "private", TOK.TOKprivate }, | |
228 { "package", TOK.TOKpackage }, | |
229 { "protected", TOK.TOKprotected }, | |
230 { "public", TOK.TOKpublic }, | |
231 { "export", TOK.TOKexport }, | |
232 | |
233 { "body", TOK.TOKbody }, | |
234 { "invariant", TOK.TOKinvariant }, | |
235 { "unittest", TOK.TOKunittest }, | |
236 { "version", TOK.TOKversion }, | |
237 //{ "manifest", TOK.TOKmanifest }, | |
238 | |
239 // Added after 1.0 | |
240 { "ref", TOK.TOKref }, | |
241 { "macro", TOK.TOKmacro }, | |
242 { "pure", TOK.TOKpure }, | |
243 { "nothrow", TOK.TOKnothrow }, | |
244 { "__thread", TOK.TOKtls }, | |
245 { "__gshared", TOK.TOKgshared }, | |
246 { "__traits", TOK.TOKtraits }, | |
247 { "__overloadset", TOK.TOKoverloadset }, | |
248 { "__FILE__", TOK.TOKfile }, | |
249 { "__LINE__", TOK.TOKline }, | |
250 { "shared", TOK.TOKshared }, | |
251 { "immutable", TOK.TOKimmutable }, | |
252 ]; | |
253 } else { | |
254 static Keyword[] keywords = | |
255 [ | |
256 // { "", TOK }, | |
257 | |
258 { "this", TOK.TOKthis }, | |
259 { "super", TOK.TOKsuper }, | |
260 { "assert", TOK.TOKassert }, | |
261 { "null", TOK.TOKnull }, | |
262 { "true", TOK.TOKtrue }, | |
263 { "false", TOK.TOKfalse }, | |
264 { "cast", TOK.TOKcast }, | |
265 { "new", TOK.TOKnew }, | |
266 { "delete", TOK.TOKdelete }, | |
267 { "throw", TOK.TOKthrow }, | |
268 { "module", TOK.TOKmodule }, | |
269 { "pragma", TOK.TOKpragma }, | |
270 { "typeof", TOK.TOKtypeof }, | |
271 { "typeid", TOK.TOKtypeid }, | |
272 | |
273 { "template", TOK.TOKtemplate }, | |
274 | |
275 { "void", TOK.TOKvoid }, | |
276 { "byte", TOK.TOKint8 }, | |
277 { "ubyte", TOK.TOKuns8 }, | |
278 { "short", TOK.TOKint16 }, | |
279 { "ushort", TOK.TOKuns16 }, | |
280 { "int", TOK.TOKint32 }, | |
281 { "uint", TOK.TOKuns32 }, | |
282 { "long", TOK.TOKint64 }, | |
283 { "ulong", TOK.TOKuns64 }, | |
284 { "cent", TOK.TOKcent, }, | |
285 { "ucent", TOK.TOKucent, }, | |
286 { "float", TOK.TOKfloat32 }, | |
287 { "double", TOK.TOKfloat64 }, | |
288 { "real", TOK.TOKfloat80 }, | |
289 | |
290 { "bool", TOK.TOKbool }, | |
291 { "char", TOK.TOKchar }, | |
292 { "wchar", TOK.TOKwchar }, | |
293 { "dchar", TOK.TOKdchar }, | |
294 | |
295 { "ifloat", TOK.TOKimaginary32 }, | |
296 { "idouble", TOK.TOKimaginary64 }, | |
297 { "ireal", TOK.TOKimaginary80 }, | |
298 | |
299 { "cfloat", TOK.TOKcomplex32 }, | |
300 { "cdouble", TOK.TOKcomplex64 }, | |
301 { "creal", TOK.TOKcomplex80 }, | |
302 | |
303 { "delegate", TOK.TOKdelegate }, | |
304 { "function", TOK.TOKfunction }, | |
305 | |
306 { "is", TOK.TOKis }, | |
307 { "if", TOK.TOKif }, | |
308 { "else", TOK.TOKelse }, | |
309 { "while", TOK.TOKwhile }, | |
310 { "for", TOK.TOKfor }, | |
311 { "do", TOK.TOKdo }, | |
312 { "switch", TOK.TOKswitch }, | |
313 { "case", TOK.TOKcase }, | |
314 { "default", TOK.TOKdefault }, | |
315 { "break", TOK.TOKbreak }, | |
316 { "continue", TOK.TOKcontinue }, | |
317 { "synchronized", TOK.TOKsynchronized }, | |
318 { "return", TOK.TOKreturn }, | |
319 { "goto", TOK.TOKgoto }, | |
320 { "try", TOK.TOKtry }, | |
321 { "catch", TOK.TOKcatch }, | |
322 { "finally", TOK.TOKfinally }, | |
323 { "with", TOK.TOKwith }, | |
324 { "asm", TOK.TOKasm }, | |
325 { "foreach", TOK.TOKforeach }, | |
326 { "foreach_reverse", TOK.TOKforeach_reverse }, | |
327 { "scope", TOK.TOKscope }, | |
328 | |
329 { "struct", TOK.TOKstruct }, | |
330 { "class", TOK.TOKclass }, | |
331 { "interface", TOK.TOKinterface }, | |
332 { "union", TOK.TOKunion }, | |
333 { "enum", TOK.TOKenum }, | |
334 { "import", TOK.TOKimport }, | |
335 { "mixin", TOK.TOKmixin }, | |
336 { "static", TOK.TOKstatic }, | |
337 { "final", TOK.TOKfinal }, | |
338 { "const", TOK.TOKconst }, | |
339 { "typedef", TOK.TOKtypedef }, | |
340 { "alias", TOK.TOKalias }, | |
341 { "override", TOK.TOKoverride }, | |
342 { "abstract", TOK.TOKabstract }, | |
343 { "volatile", TOK.TOKvolatile }, | |
344 { "debug", TOK.TOKdebug }, | |
345 { "deprecated", TOK.TOKdeprecated }, | |
346 { "in", TOK.TOKin }, | |
347 { "out", TOK.TOKout }, | |
348 { "inout", TOK.TOKinout }, | |
349 { "lazy", TOK.TOKlazy }, | |
350 { "auto", TOK.TOKauto }, | |
351 | |
352 { "align", TOK.TOKalign }, | |
353 { "extern", TOK.TOKextern }, | |
354 { "private", TOK.TOKprivate }, | |
355 { "package", TOK.TOKpackage }, | |
356 { "protected", TOK.TOKprotected }, | |
357 { "public", TOK.TOKpublic }, | |
358 { "export", TOK.TOKexport }, | |
359 | |
360 { "body", TOK.TOKbody }, | |
361 { "invariant", TOK.TOKinvariant }, | |
362 { "unittest", TOK.TOKunittest }, | |
363 { "version", TOK.TOKversion }, | |
364 //{ "manifest", TOK.TOKmanifest }, | |
365 | |
366 // Added after 1.0 | |
367 { "ref", TOK.TOKref }, | |
368 { "macro", TOK.TOKmacro }, | |
369 ]; | |
370 } | |
371 | |
372 static ubyte cmtable[256]; | |
373 enum CMoctal = 0x1; | |
374 enum CMhex = 0x2; | |
375 enum CMidchar = 0x4; | |
376 | |
377 ubyte isoctal (ubyte c) { return cmtable[c] & CMoctal; } | |
378 ubyte ishex (ubyte c) { return cmtable[c] & CMhex; } | |
379 ubyte isidchar(ubyte c) { return cmtable[c] & CMidchar; } | |
380 | |
381 static void cmtable_init() | |
382 { | |
383 for (uint c = 0; c < cmtable.length; c++) | |
384 { | |
385 if ('0' <= c && c <= '7') | |
386 cmtable[c] |= CMoctal; | |
387 if (isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) | |
388 cmtable[c] |= CMhex; | |
389 if (isalnum(c) || c == '_') | |
390 cmtable[c] |= CMidchar; | |
391 } | |
392 } | |
393 | |
394 static void initKeywords() | |
395 { | |
396 uint nkeywords = keywords.length; | |
397 | |
398 if (global.params.Dversion == 1) | |
399 nkeywords -= 2; | |
400 | |
401 cmtable_init(); | |
402 | |
403 for (uint u = 0; u < nkeywords; u++) | |
404 { | |
34 | 405 //printf("keyword[%d] = '%.*s'\n",u, keywords[u].name); |
0 | 406 string s = keywords[u].name; |
407 TOK v = keywords[u].value; | |
408 StringValue* sv = stringtable.insert(s); | |
409 sv.ptrvalue = cast(void*) new Identifier(sv.lstring.string_, v); | |
410 | |
411 //printf("tochars[%d] = '%s'\n",v, s); | |
412 Token.tochars[v] = s; | |
413 } | |
414 | |
415 Token.tochars[TOK.TOKeof] = "EOF"; | |
416 Token.tochars[TOK.TOKlcurly] = "{"; | |
417 Token.tochars[TOK.TOKrcurly] = "}"; | |
418 Token.tochars[TOK.TOKlparen] = "("; | |
419 Token.tochars[TOK.TOKrparen] = ")"; | |
420 Token.tochars[TOK.TOKlbracket] = "["; | |
421 Token.tochars[TOK.TOKrbracket] = "]"; | |
422 Token.tochars[TOK.TOKsemicolon] = ";"; | |
423 Token.tochars[TOK.TOKcolon] = ":"; | |
424 Token.tochars[TOK.TOKcomma] = ","; | |
425 Token.tochars[TOK.TOKdot] = "."; | |
426 Token.tochars[TOK.TOKxor] = "^"; | |
427 Token.tochars[TOK.TOKxorass] = "^="; | |
428 Token.tochars[TOK.TOKassign] = "="; | |
429 Token.tochars[TOK.TOKconstruct] = "="; | |
430 version (DMDV2) { | |
431 Token.tochars[TOK.TOKblit] = "="; | |
432 } | |
433 Token.tochars[TOK.TOKlt] = "<"; | |
434 Token.tochars[TOK.TOKgt] = ">"; | |
435 Token.tochars[TOK.TOKle] = "<="; | |
436 Token.tochars[TOK.TOKge] = ">="; | |
437 Token.tochars[TOK.TOKequal] = "=="; | |
438 Token.tochars[TOK.TOKnotequal] = "!="; | |
439 Token.tochars[TOK.TOKnotidentity] = "!is"; | |
440 Token.tochars[TOK.TOKtobool] = "!!"; | |
441 | |
442 Token.tochars[TOK.TOKunord] = "!<>="; | |
443 Token.tochars[TOK.TOKue] = "!<>"; | |
444 Token.tochars[TOK.TOKlg] = "<>"; | |
445 Token.tochars[TOK.TOKleg] = "<>="; | |
446 Token.tochars[TOK.TOKule] = "!>"; | |
447 Token.tochars[TOK.TOKul] = "!>="; | |
448 Token.tochars[TOK.TOKuge] = "!<"; | |
449 Token.tochars[TOK.TOKug] = "!<="; | |
450 | |
451 Token.tochars[TOK.TOKnot] = "!"; | |
452 Token.tochars[TOK.TOKtobool] = "!!"; | |
453 Token.tochars[TOK.TOKshl] = "<<"; | |
454 Token.tochars[TOK.TOKshr] = ">>"; | |
455 Token.tochars[TOK.TOKushr] = ">>>"; | |
456 Token.tochars[TOK.TOKadd] = "+"; | |
457 Token.tochars[TOK.TOKmin] = "-"; | |
458 Token.tochars[TOK.TOKmul] = "*"; | |
459 Token.tochars[TOK.TOKdiv] = "/"; | |
460 Token.tochars[TOK.TOKmod] = "%"; | |
461 Token.tochars[TOK.TOKslice] = ".."; | |
462 Token.tochars[TOK.TOKdotdotdot] = "..."; | |
463 Token.tochars[TOK.TOKand] = "&"; | |
464 Token.tochars[TOK.TOKandand] = "&&"; | |
465 Token.tochars[TOK.TOKor] = "|"; | |
466 Token.tochars[TOK.TOKoror] = "||"; | |
467 Token.tochars[TOK.TOKarray] = "[]"; | |
468 Token.tochars[TOK.TOKindex] = "[i]"; | |
469 Token.tochars[TOK.TOKaddress] = "&"; | |
470 Token.tochars[TOK.TOKstar] = "*"; | |
471 Token.tochars[TOK.TOKtilde] = "~"; | |
472 Token.tochars[TOK.TOKdollar] = "$"; | |
473 Token.tochars[TOK.TOKcast] = "cast"; | |
474 Token.tochars[TOK.TOKplusplus] = "++"; | |
475 Token.tochars[TOK.TOKminusminus] = "--"; | |
476 Token.tochars[TOK.TOKtype] = "type"; | |
477 Token.tochars[TOK.TOKquestion] = "?"; | |
478 Token.tochars[TOK.TOKneg] = "-"; | |
479 Token.tochars[TOK.TOKuadd] = "+"; | |
480 Token.tochars[TOK.TOKvar] = "var"; | |
481 Token.tochars[TOK.TOKaddass] = "+="; | |
482 Token.tochars[TOK.TOKminass] = "-="; | |
483 Token.tochars[TOK.TOKmulass] = "*="; | |
484 Token.tochars[TOK.TOKdivass] = "/="; | |
485 Token.tochars[TOK.TOKmodass] = "%="; | |
486 Token.tochars[TOK.TOKshlass] = "<<="; | |
487 Token.tochars[TOK.TOKshrass] = ">>="; | |
488 Token.tochars[TOK.TOKushrass] = ">>>="; | |
489 Token.tochars[TOK.TOKandass] = "&="; | |
490 Token.tochars[TOK.TOKorass] = "|="; | |
491 Token.tochars[TOK.TOKcatass] = "~="; | |
492 Token.tochars[TOK.TOKcat] = "~"; | |
493 Token.tochars[TOK.TOKcall] = "call"; | |
494 Token.tochars[TOK.TOKidentity] = "is"; | |
495 Token.tochars[TOK.TOKnotidentity] = "!is"; | |
496 | |
497 Token.tochars[TOK.TOKorass] = "|="; | |
498 Token.tochars[TOK.TOKidentifier] = "identifier"; | |
499 Token.tochars[TOK.TOKat] = "@"; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
500 Token.tochars[TOK.TOKpow] = "^^"; |
135 | 501 Token.tochars[TOK.TOKpowass] = "^^="; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
502 |
0 | 503 // For debugging |
73 | 504 Token.tochars[TOKerror] = "error"; |
0 | 505 Token.tochars[TOK.TOKdotexp] = "dotexp"; |
506 Token.tochars[TOK.TOKdotti] = "dotti"; | |
507 Token.tochars[TOK.TOKdotvar] = "dotvar"; | |
508 Token.tochars[TOK.TOKdottype] = "dottype"; | |
509 Token.tochars[TOK.TOKsymoff] = "symoff"; | |
510 Token.tochars[TOK.TOKarraylength] = "arraylength"; | |
511 Token.tochars[TOK.TOKarrayliteral] = "arrayliteral"; | |
512 Token.tochars[TOK.TOKassocarrayliteral] = "assocarrayliteral"; | |
513 Token.tochars[TOK.TOKstructliteral] = "structliteral"; | |
514 Token.tochars[TOK.TOKstring] = "string"; | |
515 Token.tochars[TOK.TOKdsymbol] = "symbol"; | |
516 Token.tochars[TOK.TOKtuple] = "tuple"; | |
517 Token.tochars[TOK.TOKdeclaration] = "declaration"; | |
518 Token.tochars[TOK.TOKdottd] = "dottd"; | |
519 Token.tochars[TOK.TOKon_scope_exit] = "scope(exit)"; | |
520 Token.tochars[TOK.TOKon_scope_success] = "scope(success)"; | |
521 Token.tochars[TOK.TOKon_scope_failure] = "scope(failure)"; | |
522 } | |
523 | |
524 static Identifier idPool(string s) | |
525 { | |
526 StringValue* sv = stringtable.update(s); | |
527 Identifier id = cast(Identifier) sv.ptrvalue; | |
528 if (id is null) | |
529 { | |
530 id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
531 sv.ptrvalue = cast(void*)id; | |
532 } | |
533 | |
534 return id; | |
535 } | |
536 | |
537 static Identifier uniqueId(string s) | |
538 { | |
539 static int num; | |
540 return uniqueId(s, ++num); | |
541 } | |
542 | |
543 /********************************************* | |
544 * Create a unique identifier using the prefix s. | |
545 */ | |
546 static Identifier uniqueId(string s, int num) | |
547 { | |
548 char buffer[32]; | |
549 size_t slen = s.length; | |
550 | |
551 assert(slen + num.sizeof * 3 + 1 <= buffer.sizeof); | |
552 int len = sprintf(buffer.ptr, "%.*s%d", s, num); | |
553 | |
554 return idPool(buffer[0..len].idup); | |
555 } | |
556 | |
557 TOK nextToken() | |
558 { | |
559 Token *t; | |
560 | |
561 if (token.next) | |
562 { | |
563 t = token.next; | |
564 memcpy(&token, t, Token.sizeof); | |
565 t.next = freelist; | |
566 freelist = t; | |
567 } | |
568 else | |
569 { | |
570 scan(&token); | |
571 } | |
572 | |
573 //token.print(); | |
574 return token.value; | |
575 } | |
576 | |
577 /*********************** | |
578 * Look ahead at next token's value. | |
579 */ | |
580 TOK peekNext() | |
581 { | |
582 return peek(&token).value; | |
583 } | |
584 | |
585 TOK peekNext2() | |
586 { | |
587 assert(false); | |
588 } | |
589 | |
590 void scan(Token* t) | |
591 { | |
592 uint lastLine = loc.linnum; | |
593 uint linnum; | |
594 | |
595 t.blockComment = null; | |
596 t.lineComment = null; | |
597 while (1) | |
598 { | |
599 t.ptr = p; | |
600 //printf("p = %p, *p = '%c'\n",p,*p); | |
601 switch (*p) | |
602 { | |
603 case 0: | |
604 case 0x1A: | |
605 t.value = TOK.TOKeof; // end of file | |
606 return; | |
607 | |
608 case ' ': | |
609 case '\t': | |
610 case '\v': | |
611 case '\f': | |
612 p++; | |
613 continue; // skip white space | |
614 | |
615 case '\r': | |
616 p++; | |
617 if (*p != '\n') // if CR stands by itself | |
618 loc.linnum++; | |
619 continue; // skip white space | |
620 | |
621 case '\n': | |
622 p++; | |
623 loc.linnum++; | |
624 continue; // skip white space | |
625 | |
626 case '0': case '1': case '2': case '3': case '4': | |
627 case '5': case '6': case '7': case '8': case '9': | |
628 t.value = number(t); | |
629 return; | |
630 | |
631 version (CSTRINGS) { | |
632 case '\'': | |
633 t.value = charConstant(t, 0); | |
634 return; | |
635 | |
636 case '"': | |
637 t.value = stringConstant(t,0); | |
638 return; | |
639 | |
640 case 'l': | |
641 case 'L': | |
642 if (p[1] == '\'') | |
643 { | |
644 p++; | |
645 t.value = charConstant(t, 1); | |
646 return; | |
647 } | |
648 else if (p[1] == '"') | |
649 { | |
650 p++; | |
651 t.value = stringConstant(t, 1); | |
652 return; | |
653 } | |
654 } else { | |
655 case '\'': | |
656 t.value = charConstant(t,0); | |
657 return; | |
658 | |
659 case 'r': | |
660 if (p[1] != '"') | |
661 goto case_ident; | |
662 p++; | |
663 case '`': | |
664 t.value = wysiwygStringConstant(t, *p); | |
665 return; | |
666 | |
667 case 'x': | |
668 if (p[1] != '"') | |
669 goto case_ident; | |
670 p++; | |
671 t.value = hexStringConstant(t); | |
672 return; | |
673 | |
674 version (DMDV2) { | |
675 case 'q': | |
676 if (p[1] == '"') | |
677 { | |
678 p++; | |
679 t.value = delimitedStringConstant(t); | |
680 return; | |
681 } | |
682 else if (p[1] == '{') | |
683 { | |
684 p++; | |
685 t.value = tokenStringConstant(t); | |
686 return; | |
687 } | |
688 else | |
689 goto case_ident; | |
690 } | |
691 | |
692 case '"': | |
693 t.value = escapeStringConstant(t,0); | |
694 return; | |
695 version (TEXTUAL_ASSEMBLY_OUT) { | |
696 } else { | |
697 case '\\': // escaped string literal | |
698 { uint c; | |
699 ubyte* pstart = p; | |
700 | |
701 stringbuffer.reset(); | |
702 do | |
703 { | |
704 p++; | |
705 switch (*p) | |
706 { | |
707 case 'u': | |
708 case 'U': | |
709 case '&': | |
710 c = escapeSequence(); | |
711 stringbuffer.writeUTF8(c); | |
712 break; | |
713 | |
714 default: | |
715 c = escapeSequence(); | |
716 stringbuffer.writeByte(c); | |
717 break; | |
718 } | |
719 } while (*p == '\\'); | |
720 t.len = stringbuffer.offset; | |
721 stringbuffer.writeByte(0); | |
2 | 722 char* cc = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 723 memcpy(cc, stringbuffer.data, stringbuffer.offset); |
724 t.ustring = cc; | |
725 t.postfix = 0; | |
726 t.value = TOK.TOKstring; | |
727 if (!global.params.useDeprecated) | |
728 error("Escape String literal %.*s is deprecated, use double quoted string literal \"%.*s\" instead", p - pstart, pstart, p - pstart, pstart); | |
729 return; | |
730 } | |
731 } | |
732 case 'l': | |
733 case 'L': | |
734 } | |
735 case 'a': case 'b': case 'c': case 'd': case 'e': | |
736 case 'f': case 'g': case 'h': case 'i': case 'j': | |
737 case 'k': case 'm': case 'n': case 'o': | |
738 version (DMDV2) { | |
739 case 'p': /*case 'q': case 'r':*/ case 's': case 't': | |
740 } else { | |
741 case 'p': case 'q': /*case 'r':*/ case 's': case 't': | |
742 } | |
743 case 'u': case 'v': case 'w': /*case 'x':*/ case 'y': | |
744 case 'z': | |
745 case 'A': case 'B': case 'C': case 'D': case 'E': | |
746 case 'F': case 'G': case 'H': case 'I': case 'J': | |
747 case 'K': case 'M': case 'N': case 'O': | |
748 case 'P': case 'Q': case 'R': case 'S': case 'T': | |
749 case 'U': case 'V': case 'W': case 'X': case 'Y': | |
750 case 'Z': | |
751 case '_': | |
752 case_ident: | |
135 | 753 { |
754 ubyte c; | |
0 | 755 |
135 | 756 while (1) |
757 { | |
758 c = *++p; | |
759 if (isidchar(c)) | |
760 continue; | |
761 else if (c & 0x80) | |
762 { | |
763 ubyte *s = p; | |
764 uint u = decodeUTF(); | |
765 if (isUniAlpha(u)) | |
766 continue; | |
767 error("char 0x%04x not allowed in identifier", u); | |
768 p = s; | |
769 } | |
770 break; | |
771 } | |
772 | |
773 StringValue *sv = stringtable.update((cast(immutable(char)*)t.ptr)[0.. p - t.ptr]); | |
774 Identifier id = cast(Identifier) sv.ptrvalue; | |
775 | |
0 | 776 if (id is null) |
777 { id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
778 sv.ptrvalue = cast(void*)id; | |
779 } | |
780 t.ident = id; | |
781 t.value = cast(TOK) id.value; | |
782 anyToken = 1; | |
783 if (*t.ptr == '_') // if special identifier token | |
784 { | |
785 static char date[11+1]; | |
786 static char time[8+1]; | |
787 static char timestamp[24+1]; | |
788 | |
789 if (!date[0]) // lazy evaluation | |
790 { time_t tm; | |
791 char *p; | |
792 | |
793 .time(&tm); | |
794 p = ctime(&tm); | |
795 assert(p); | |
796 sprintf(date.ptr, "%.6s %.4s", p + 4, p + 20); | |
797 sprintf(time.ptr, "%.8s", p + 11); | |
798 sprintf(timestamp.ptr, "%.24s", p); | |
799 } | |
800 | |
801 ///version (DMDV1) { | |
802 /// if (mod && id == Id.FILE) | |
803 /// { | |
804 /// t.ustring = cast(ubyte*)(loc.filename ? loc.filename : mod.ident.toChars()); | |
805 /// goto Lstr; | |
806 /// } | |
807 /// else if (mod && id == Id.LINE) | |
808 /// { | |
809 /// t.value = TOK.TOKint64v; | |
810 /// t.uns64value = loc.linnum; | |
811 /// } | |
812 /// else | |
813 ///} | |
814 if (id == Id.DATE) | |
815 { | |
816 t.ustring = date.ptr; | |
817 goto Lstr; | |
818 } | |
819 else if (id == Id.TIME) | |
820 { | |
821 t.ustring = time.ptr; | |
822 goto Lstr; | |
823 } | |
824 else if (id == Id.VENDOR) | |
825 { | |
826 t.ustring = "Digital Mars D".ptr; | |
827 goto Lstr; | |
828 } | |
829 else if (id == Id.TIMESTAMP) | |
830 { | |
831 t.ustring = timestamp.ptr; | |
832 Lstr: | |
833 t.value = TOK.TOKstring; | |
834 Llen: | |
835 t.postfix = 0; | |
836 t.len = strlen(cast(char*)t.ustring); | |
837 } | |
838 else if (id == Id.VERSIONX) | |
839 { | |
840 uint major = 0; | |
841 uint minor = 0; | |
842 | |
843 foreach (char cc; global.version_[1..$]) | |
844 { | |
845 if (isdigit(cc)) | |
846 minor = minor * 10 + cc - '0'; | |
847 else if (cc == '.') | |
848 { | |
849 major = minor; | |
850 minor = 0; | |
851 } | |
852 else | |
853 break; | |
854 } | |
855 t.value = TOK.TOKint64v; | |
856 t.uns64value = major * 1000 + minor; | |
857 } | |
858 ///version (DMDV2) { | |
859 else if (id == Id.EOFX) | |
860 { | |
861 t.value = TOK.TOKeof; | |
862 // Advance scanner to end of file | |
863 while (!(*p == 0 || *p == 0x1A)) | |
864 p++; | |
865 } | |
866 ///} | |
867 } | |
868 //printf("t.value = %d\n",t.value); | |
869 return; | |
870 } | |
871 | |
872 case '/': | |
873 p++; | |
874 switch (*p) | |
875 { | |
876 case '=': | |
877 p++; | |
878 t.value = TOK.TOKdivass; | |
879 return; | |
880 | |
881 case '*': | |
882 p++; | |
883 linnum = loc.linnum; | |
884 while (1) | |
885 { | |
886 while (1) | |
887 { | |
888 ubyte c = *p; | |
889 switch (c) | |
890 { | |
891 case '/': | |
892 break; | |
893 | |
894 case '\n': | |
895 loc.linnum++; | |
896 p++; | |
897 continue; | |
898 | |
899 case '\r': | |
900 p++; | |
901 if (*p != '\n') | |
902 loc.linnum++; | |
903 continue; | |
904 | |
905 case 0: | |
906 case 0x1A: | |
907 error("unterminated /* */ comment"); | |
908 p = end; | |
909 t.value = TOK.TOKeof; | |
910 return; | |
911 | |
912 default: | |
913 if (c & 0x80) | |
914 { uint u = decodeUTF(); | |
915 if (u == PS || u == LS) | |
916 loc.linnum++; | |
917 } | |
918 p++; | |
919 continue; | |
920 } | |
921 break; | |
922 } | |
923 p++; | |
924 if (p[-2] == '*' && p - 3 != t.ptr) | |
925 break; | |
926 } | |
927 if (commentToken) | |
928 { | |
929 t.value = TOK.TOKcomment; | |
930 return; | |
931 } | |
932 else if (doDocComment && t.ptr[2] == '*' && p - 4 != t.ptr) | |
933 { // if /** but not /**/ | |
934 getDocComment(t, lastLine == linnum); | |
935 } | |
936 continue; | |
937 | |
938 case '/': // do // style comments | |
939 linnum = loc.linnum; | |
940 while (1) | |
941 { ubyte c = *++p; | |
942 switch (c) | |
943 { | |
944 case '\n': | |
945 break; | |
946 | |
947 case '\r': | |
948 if (p[1] == '\n') | |
949 p++; | |
950 break; | |
951 | |
952 case 0: | |
953 case 0x1A: | |
954 if (commentToken) | |
955 { | |
956 p = end; | |
957 t.value = TOK.TOKcomment; | |
958 return; | |
959 } | |
960 if (doDocComment && t.ptr[2] == '/') | |
961 getDocComment(t, lastLine == linnum); | |
962 p = end; | |
963 t.value = TOK.TOKeof; | |
964 return; | |
965 | |
966 default: | |
967 if (c & 0x80) | |
968 { uint u = decodeUTF(); | |
969 if (u == PS || u == LS) | |
970 break; | |
971 } | |
972 continue; | |
973 } | |
974 break; | |
975 } | |
976 | |
977 if (commentToken) | |
978 { | |
979 p++; | |
980 loc.linnum++; | |
981 t.value = TOK.TOKcomment; | |
982 return; | |
983 } | |
984 if (doDocComment && t.ptr[2] == '/') | |
985 getDocComment(t, lastLine == linnum); | |
986 | |
987 p++; | |
988 loc.linnum++; | |
989 continue; | |
990 | |
991 case '+': | |
992 { | |
993 int nest; | |
994 | |
995 linnum = loc.linnum; | |
996 p++; | |
997 nest = 1; | |
998 while (1) | |
999 { ubyte c = *p; | |
1000 switch (c) | |
1001 { | |
1002 case '/': | |
1003 p++; | |
1004 if (*p == '+') | |
1005 { | |
1006 p++; | |
1007 nest++; | |
1008 } | |
1009 continue; | |
1010 | |
1011 case '+': | |
1012 p++; | |
1013 if (*p == '/') | |
1014 { | |
1015 p++; | |
1016 if (--nest == 0) | |
1017 break; | |
1018 } | |
1019 continue; | |
1020 | |
1021 case '\r': | |
1022 p++; | |
1023 if (*p != '\n') | |
1024 loc.linnum++; | |
1025 continue; | |
1026 | |
1027 case '\n': | |
1028 loc.linnum++; | |
1029 p++; | |
1030 continue; | |
1031 | |
1032 case 0: | |
1033 case 0x1A: | |
1034 error("unterminated /+ +/ comment"); | |
1035 p = end; | |
1036 t.value = TOK.TOKeof; | |
1037 return; | |
1038 | |
1039 default: | |
1040 if (c & 0x80) | |
1041 { uint u = decodeUTF(); | |
1042 if (u == PS || u == LS) | |
1043 loc.linnum++; | |
1044 } | |
1045 p++; | |
1046 continue; | |
1047 } | |
1048 break; | |
1049 } | |
1050 if (commentToken) | |
1051 { | |
1052 t.value = TOK.TOKcomment; | |
1053 return; | |
1054 } | |
1055 if (doDocComment && t.ptr[2] == '+' && p - 4 != t.ptr) | |
1056 { // if /++ but not /++/ | |
1057 getDocComment(t, lastLine == linnum); | |
1058 } | |
1059 continue; | |
1060 } | |
1061 | |
1062 default: | |
1063 break; /// | |
1064 } | |
1065 t.value = TOK.TOKdiv; | |
1066 return; | |
1067 | |
1068 case '.': | |
1069 p++; | |
1070 if (isdigit(*p)) | |
1071 { /* Note that we don't allow ._1 and ._ as being | |
1072 * valid floating point numbers. | |
1073 */ | |
1074 p--; | |
1075 t.value = inreal(t); | |
1076 } | |
1077 else if (p[0] == '.') | |
1078 { | |
1079 if (p[1] == '.') | |
1080 { p += 2; | |
1081 t.value = TOK.TOKdotdotdot; | |
1082 } | |
1083 else | |
1084 { p++; | |
1085 t.value = TOK.TOKslice; | |
1086 } | |
1087 } | |
1088 else | |
1089 t.value = TOK.TOKdot; | |
1090 return; | |
1091 | |
1092 case '&': | |
1093 p++; | |
1094 if (*p == '=') | |
1095 { p++; | |
1096 t.value = TOK.TOKandass; | |
1097 } | |
1098 else if (*p == '&') | |
1099 { p++; | |
1100 t.value = TOK.TOKandand; | |
1101 } | |
1102 else | |
1103 t.value = TOK.TOKand; | |
1104 return; | |
1105 | |
1106 case '|': | |
1107 p++; | |
1108 if (*p == '=') | |
1109 { p++; | |
1110 t.value = TOK.TOKorass; | |
1111 } | |
1112 else if (*p == '|') | |
1113 { p++; | |
1114 t.value = TOK.TOKoror; | |
1115 } | |
1116 else | |
1117 t.value = TOK.TOKor; | |
1118 return; | |
1119 | |
1120 case '-': | |
1121 p++; | |
1122 if (*p == '=') | |
1123 { p++; | |
1124 t.value = TOK.TOKminass; | |
1125 } | |
1126 /// #if 0 | |
1127 /// else if (*p == '>') | |
1128 /// { p++; | |
1129 /// t.value = TOK.TOKarrow; | |
1130 /// } | |
1131 /// #endif | |
1132 else if (*p == '-') | |
1133 { p++; | |
1134 t.value = TOK.TOKminusminus; | |
1135 } | |
1136 else | |
1137 t.value = TOK.TOKmin; | |
1138 return; | |
1139 | |
1140 case '+': | |
1141 p++; | |
1142 if (*p == '=') | |
1143 { p++; | |
1144 t.value = TOK.TOKaddass; | |
1145 } | |
1146 else if (*p == '+') | |
1147 { p++; | |
1148 t.value = TOK.TOKplusplus; | |
1149 } | |
1150 else | |
1151 t.value = TOK.TOKadd; | |
1152 return; | |
1153 | |
1154 case '<': | |
1155 p++; | |
1156 if (*p == '=') | |
1157 { p++; | |
1158 t.value = TOK.TOKle; // <= | |
1159 } | |
1160 else if (*p == '<') | |
1161 { p++; | |
1162 if (*p == '=') | |
1163 { p++; | |
1164 t.value = TOK.TOKshlass; // <<= | |
1165 } | |
1166 else | |
1167 t.value = TOK.TOKshl; // << | |
1168 } | |
1169 else if (*p == '>') | |
1170 { p++; | |
1171 if (*p == '=') | |
1172 { p++; | |
1173 t.value = TOK.TOKleg; // <>= | |
1174 } | |
1175 else | |
1176 t.value = TOK.TOKlg; // <> | |
1177 } | |
1178 else | |
1179 t.value = TOK.TOKlt; // < | |
1180 return; | |
1181 | |
1182 case '>': | |
1183 p++; | |
1184 if (*p == '=') | |
1185 { p++; | |
1186 t.value = TOK.TOKge; // >= | |
1187 } | |
1188 else if (*p == '>') | |
1189 { p++; | |
1190 if (*p == '=') | |
1191 { p++; | |
1192 t.value = TOK.TOKshrass; // >>= | |
1193 } | |
1194 else if (*p == '>') | |
1195 { p++; | |
1196 if (*p == '=') | |
1197 { p++; | |
1198 t.value = TOK.TOKushrass; // >>>= | |
1199 } | |
1200 else | |
1201 t.value = TOK.TOKushr; // >>> | |
1202 } | |
1203 else | |
1204 t.value = TOK.TOKshr; // >> | |
1205 } | |
1206 else | |
1207 t.value = TOK.TOKgt; // > | |
1208 return; | |
1209 | |
1210 case '!': | |
1211 p++; | |
1212 if (*p == '=') | |
1213 { p++; | |
1214 if (*p == '=' && global.params.Dversion == 1) | |
1215 { p++; | |
1216 t.value = TOK.TOKnotidentity; // !== | |
1217 } | |
1218 else | |
1219 t.value = TOK.TOKnotequal; // != | |
1220 } | |
1221 else if (*p == '<') | |
1222 { p++; | |
1223 if (*p == '>') | |
1224 { p++; | |
1225 if (*p == '=') | |
1226 { p++; | |
1227 t.value = TOK.TOKunord; // !<>= | |
1228 } | |
1229 else | |
1230 t.value = TOK.TOKue; // !<> | |
1231 } | |
1232 else if (*p == '=') | |
1233 { p++; | |
1234 t.value = TOK.TOKug; // !<= | |
1235 } | |
1236 else | |
1237 t.value = TOK.TOKuge; // !< | |
1238 } | |
1239 else if (*p == '>') | |
1240 { p++; | |
1241 if (*p == '=') | |
1242 { p++; | |
1243 t.value = TOK.TOKul; // !>= | |
1244 } | |
1245 else | |
1246 t.value = TOK.TOKule; // !> | |
1247 } | |
1248 else | |
1249 t.value = TOK.TOKnot; // ! | |
1250 return; | |
1251 | |
1252 case '=': | |
1253 p++; | |
1254 if (*p == '=') | |
1255 { p++; | |
1256 if (*p == '=' && global.params.Dversion == 1) | |
1257 { p++; | |
1258 t.value = TOK.TOKidentity; // === | |
1259 } | |
1260 else | |
1261 t.value = TOK.TOKequal; // == | |
1262 } | |
1263 else | |
1264 t.value = TOK.TOKassign; // = | |
1265 return; | |
1266 | |
1267 case '~': | |
1268 p++; | |
1269 if (*p == '=') | |
1270 { p++; | |
1271 t.value = TOK.TOKcatass; // ~= | |
1272 } | |
1273 else | |
1274 t.value = TOK.TOKtilde; // ~ | |
1275 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1276 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1277 version(DMDV2) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1278 case '^': |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1279 p++; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1280 if (*p == '^') |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1281 { p++; |
135 | 1282 if (*p == '=') |
1283 { p++; | |
1284 t.value = TOKpowass; // ^^= | |
1285 } | |
1286 else | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1287 t.value = TOKpow; // ^^ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1288 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1289 else if (*p == '=') |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1290 { p++; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1291 t.value = TOKxorass; // ^= |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1292 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1293 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1294 t.value = TOKxor; // ^ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1295 return; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1296 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1297 |
0 | 1298 /* |
1299 #define SINGLE(c,tok) case c: p++; t.value = tok; return; | |
1300 | |
1301 SINGLE('(', TOKlparen) | |
1302 SINGLE(')', TOKrparen) | |
1303 SINGLE('[', TOKlbracket) | |
1304 SINGLE(']', TOKrbracket) | |
1305 SINGLE('{', TOKlcurly) | |
1306 SINGLE('}', TOKrcurly) | |
1307 SINGLE('?', TOKquestion) | |
1308 SINGLE(',', TOKcomma) | |
1309 SINGLE(';', TOKsemicolon) | |
1310 SINGLE(':', TOKcolon) | |
1311 SINGLE('$', TOKdollar) | |
1312 SINGLE('@', TOKat) | |
1313 | |
1314 #undef SINGLE | |
1315 | |
1316 #define DOUBLE(c1,tok1,c2,tok2) \ | |
1317 case c1: \ | |
1318 p++; \ | |
1319 if (*p == c2) \ | |
1320 { p++; \ | |
1321 t.value = tok2; \ | |
1322 } \ | |
1323 else \ | |
1324 t.value = tok1; \ | |
1325 return; | |
1326 | |
1327 DOUBLE('*', TOKmul, '=', TOKmulass) | |
1328 DOUBLE('%', TOKmod, '=', TOKmodass) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1329 #if DMDV1 |
0 | 1330 DOUBLE('^', TOKxor, '=', TOKxorass) |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1331 #endif |
0 | 1332 #undef DOUBLE |
1333 */ | |
1334 | |
1335 case '(': p++; t.value = TOK.TOKlparen; return; | |
1336 case ')': p++; t.value = TOK.TOKrparen; return; | |
1337 case '[': p++; t.value = TOK.TOKlbracket; return; | |
1338 case ']': p++; t.value = TOK.TOKrbracket; return; | |
1339 case '{': p++; t.value = TOK.TOKlcurly; return; | |
1340 case '}': p++; t.value = TOK.TOKrcurly; return; | |
1341 case '?': p++; t.value = TOK.TOKquestion; return; | |
1342 case ',': p++; t.value = TOK.TOKcomma; return; | |
1343 case ';': p++; t.value = TOK.TOKsemicolon; return; | |
1344 case ':': p++; t.value = TOK.TOKcolon; return; | |
1345 case '$': p++; t.value = TOK.TOKdollar; return; | |
1346 case '@': p++; t.value = TOK.TOKat; return; | |
1347 | |
1348 case '*': | |
1349 p++; | |
1350 if (*p == '=') { | |
1351 p++; | |
1352 t.value = TOK.TOKmulass; | |
1353 } else { | |
1354 t.value = TOK.TOKmul; | |
1355 } | |
1356 return; | |
1357 | |
1358 case '%': | |
1359 p++; | |
1360 if (*p == '=') { | |
1361 p++; | |
1362 t.value = TOK.TOKmodass; | |
1363 } else { | |
1364 t.value = TOK.TOKmod; | |
1365 } | |
1366 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1367 version(DMDV1) { |
0 | 1368 case '^': |
1369 p++; | |
1370 if (*p == '=') { | |
1371 p++; | |
1372 t.value = TOK.TOKxorass; | |
1373 } else { | |
1374 t.value = TOK.TOKxor; | |
1375 } | |
1376 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1377 } |
0 | 1378 case '#': |
1379 p++; | |
1380 pragma_(); | |
1381 continue; | |
1382 | |
1383 default: | |
135 | 1384 { uint c = *p; |
0 | 1385 |
1386 if (c & 0x80) | |
135 | 1387 { c = decodeUTF(); |
0 | 1388 |
1389 // Check for start of unicode identifier | |
135 | 1390 if (isUniAlpha(c)) |
0 | 1391 goto case_ident; |
1392 | |
135 | 1393 if (c == PS || c == LS) |
0 | 1394 { |
1395 loc.linnum++; | |
1396 p++; | |
1397 continue; | |
1398 } | |
1399 } | |
135 | 1400 if (c < 0x80 && isprint(c)) |
0 | 1401 error("unsupported char '%c'", c); |
1402 else | |
1403 error("unsupported char 0x%02x", c); | |
1404 p++; | |
1405 continue; | |
1406 } | |
1407 } | |
1408 } | |
1409 } | |
1410 | |
1411 Token* peek(Token* ct) | |
1412 { | |
1413 Token* t; | |
1414 | |
1415 if (ct.next) | |
1416 t = ct.next; | |
1417 else | |
1418 { | |
1419 t = new Token(); | |
1420 scan(t); | |
1421 t.next = null; | |
1422 ct.next = t; | |
1423 } | |
1424 return t; | |
1425 } | |
1426 | |
1427 Token* peekPastParen(Token* tk) | |
1428 { | |
1429 //printf("peekPastParen()\n"); | |
1430 int parens = 1; | |
1431 int curlynest = 0; | |
1432 while (1) | |
1433 { | |
1434 tk = peek(tk); | |
1435 //tk.print(); | |
1436 switch (tk.value) | |
1437 { | |
1438 case TOK.TOKlparen: | |
1439 parens++; | |
1440 continue; | |
1441 | |
1442 case TOK.TOKrparen: | |
1443 --parens; | |
1444 if (parens) | |
1445 continue; | |
1446 tk = peek(tk); | |
1447 break; | |
1448 | |
1449 case TOK.TOKlcurly: | |
1450 curlynest++; | |
1451 continue; | |
1452 | |
1453 case TOK.TOKrcurly: | |
1454 if (--curlynest >= 0) | |
1455 continue; | |
1456 break; | |
1457 | |
1458 case TOK.TOKsemicolon: | |
1459 if (curlynest) | |
1460 continue; | |
1461 break; | |
1462 | |
1463 case TOK.TOKeof: | |
1464 break; | |
1465 | |
1466 default: | |
1467 continue; | |
1468 } | |
1469 return tk; | |
1470 } | |
1471 } | |
1472 | |
1473 /******************************************* | |
1474 * Parse escape sequence. | |
1475 */ | |
1476 uint escapeSequence() | |
1477 { | |
1478 uint c = *p; | |
1479 | |
1480 version (TEXTUAL_ASSEMBLY_OUT) { | |
1481 return c; | |
1482 } | |
1483 int n; | |
1484 int ndigits; | |
1485 | |
1486 switch (c) | |
1487 { | |
1488 case '\'': | |
1489 case '"': | |
1490 case '?': | |
1491 case '\\': | |
1492 Lconsume: | |
1493 p++; | |
1494 break; | |
1495 | |
1496 case 'a': c = 7; goto Lconsume; | |
1497 case 'b': c = 8; goto Lconsume; | |
1498 case 'f': c = 12; goto Lconsume; | |
1499 case 'n': c = 10; goto Lconsume; | |
1500 case 'r': c = 13; goto Lconsume; | |
1501 case 't': c = 9; goto Lconsume; | |
1502 case 'v': c = 11; goto Lconsume; | |
1503 | |
1504 case 'u': | |
1505 ndigits = 4; | |
1506 goto Lhex; | |
1507 case 'U': | |
1508 ndigits = 8; | |
1509 goto Lhex; | |
1510 case 'x': | |
1511 ndigits = 2; | |
1512 Lhex: | |
1513 p++; | |
1514 c = *p; | |
1515 if (ishex(cast(ubyte)c)) | |
1516 { | |
1517 uint v; | |
1518 | |
1519 n = 0; | |
1520 v = 0; | |
1521 while (1) | |
1522 { | |
1523 if (isdigit(c)) | |
1524 c -= '0'; | |
1525 else if (islower(c)) | |
1526 c -= 'a' - 10; | |
1527 else | |
1528 c -= 'A' - 10; | |
1529 v = v * 16 + c; | |
1530 c = *++p; | |
1531 if (++n == ndigits) | |
1532 break; | |
1533 if (!ishex(cast(ubyte)c)) | |
1534 { error("escape hex sequence has %d hex digits instead of %d", n, ndigits); | |
1535 break; | |
1536 } | |
1537 } | |
1538 if (ndigits != 2 && !utf_isValidDchar(v)) | |
1539 { error("invalid UTF character \\U%08x", v); | |
1540 v = '?'; // recover with valid UTF character | |
1541 } | |
1542 c = v; | |
1543 } | |
1544 else | |
1545 error("undefined escape hex sequence \\%c\n",c); | |
1546 break; | |
1547 | |
1548 case '&': // named character entity | |
1549 for (ubyte* idstart = ++p; true; p++) | |
1550 { | |
1551 switch (*p) | |
1552 { | |
1553 case ';': | |
1554 c = HtmlNamedEntity(idstart, p - idstart); | |
1555 if (c == ~0) | |
1556 { | |
1557 error("unnamed character entity &%s;", idstart[0..(p - idstart)]); | |
1558 c = ' '; | |
1559 } | |
1560 p++; | |
1561 break; | |
1562 | |
1563 default: | |
1564 if (isalpha(*p) || | |
1565 (p != idstart + 1 && isdigit(*p))) | |
1566 continue; | |
1567 error("unterminated named entity"); | |
1568 break; | |
1569 } | |
1570 break; | |
1571 } | |
1572 break; | |
1573 | |
1574 case 0: | |
1575 case 0x1A: // end of file | |
1576 c = '\\'; | |
1577 break; | |
1578 | |
1579 default: | |
1580 if (isoctal(cast(ubyte)c)) | |
1581 { | |
1582 uint v; | |
1583 | |
1584 n = 0; | |
1585 v = 0; | |
1586 do | |
1587 { | |
1588 v = v * 8 + (c - '0'); | |
1589 c = *++p; | |
1590 } while (++n < 3 && isoctal(cast(ubyte)c)); | |
1591 c = v; | |
1592 if (c > 0xFF) | |
1593 error("0%03o is larger than a byte", c); | |
1594 } | |
1595 else | |
1596 error("undefined escape sequence \\%c\n",c); | |
1597 break; | |
1598 } | |
1599 return c; | |
1600 } | |
1601 | |
1602 TOK wysiwygStringConstant(Token* t, int tc) | |
1603 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1604 uint c; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1605 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1606 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1607 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1608 stringbuffer.reset(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1609 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1610 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1611 c = *p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1612 switch (c) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1613 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1614 case '\n': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1615 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1616 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1617 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1618 case '\r': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1619 if (*p == '\n') |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1620 continue; // ignore |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1621 c = '\n'; // treat EndOfLine as \n character |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1622 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1623 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1624 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1625 case 0: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1626 case 0x1A: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1627 error("unterminated string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1628 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1629 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1630 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1631 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1632 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1633 case '"': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1634 case '`': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1635 if (c == tc) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1636 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1637 t.len = stringbuffer.offset; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1638 stringbuffer.writeByte(0); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1639 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1640 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1641 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1642 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1643 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1644 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1645 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1646 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1647 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1648 if (c & 0x80) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1649 { p--; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1650 uint u = decodeUTF(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1651 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1652 if (u == PS || u == LS) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1653 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1654 stringbuffer.writeUTF8(u); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1655 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1656 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1657 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1658 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1659 stringbuffer.writeByte(c); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1660 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1661 |
0 | 1662 assert(false); |
1663 } | |
1664 | |
51 | 1665 /************************************** |
1666 * Lex hex strings: | |
1667 * x"0A ae 34FE BD" | |
1668 */ | |
0 | 1669 TOK hexStringConstant(Token* t) |
1670 { | |
51 | 1671 uint c; |
1672 Loc start = loc; | |
1673 uint n = 0; | |
1674 uint v; | |
1675 | |
1676 p++; | |
1677 stringbuffer.reset(); | |
1678 while (1) | |
1679 { | |
1680 c = *p++; | |
1681 switch (c) | |
1682 { | |
1683 case ' ': | |
1684 case '\t': | |
1685 case '\v': | |
1686 case '\f': | |
1687 continue; // skip white space | |
1688 | |
1689 case '\r': | |
1690 if (*p == '\n') | |
1691 continue; // ignore | |
1692 // Treat isolated '\r' as if it were a '\n' | |
1693 case '\n': | |
1694 loc.linnum++; | |
1695 continue; | |
1696 | |
1697 case 0: | |
1698 case 0x1A: | |
1699 error("unterminated string constant starting at %s", start.toChars()); | |
1700 t.ustring = "".ptr; | |
1701 t.len = 0; | |
1702 t.postfix = 0; | |
1703 return TOKstring; | |
1704 | |
1705 case '"': | |
1706 if (n & 1) | |
1707 { | |
1708 error("odd number (%d) of hex characters in hex string", n); | |
1709 stringbuffer.writeByte(v); | |
1710 } | |
1711 t.len = stringbuffer.offset; | |
1712 stringbuffer.writeByte(0); | |
1713 void* mem = malloc(stringbuffer.offset); | |
1714 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
1715 t.ustring = cast(const(char)*)mem; | |
1716 stringPostfix(t); | |
1717 return TOKstring; | |
1718 | |
1719 default: | |
1720 if (c >= '0' && c <= '9') | |
1721 c -= '0'; | |
1722 else if (c >= 'a' && c <= 'f') | |
1723 c -= 'a' - 10; | |
1724 else if (c >= 'A' && c <= 'F') | |
1725 c -= 'A' - 10; | |
1726 else if (c & 0x80) | |
1727 { p--; | |
1728 uint u = decodeUTF(); | |
1729 p++; | |
1730 if (u == PS || u == LS) | |
1731 loc.linnum++; | |
1732 else | |
135 | 1733 error("non-hex character \\u%04x", u); |
51 | 1734 } |
1735 else | |
1736 error("non-hex character '%c'", c); | |
1737 if (n & 1) | |
1738 { v = (v << 4) | c; | |
1739 stringbuffer.writeByte(v); | |
1740 } | |
1741 else | |
1742 v = c; | |
1743 n++; | |
1744 break; | |
1745 } | |
1746 } | |
0 | 1747 } |
1748 | |
1749 version (DMDV2) { | |
51 | 1750 /************************************** |
1751 * Lex delimited strings: | |
1752 * q"(foo(xxx))" // "foo(xxx)" | |
1753 * q"[foo(]" // "foo(" | |
1754 * q"/foo]/" // "foo]" | |
1755 * q"HERE | |
1756 * foo | |
1757 * HERE" // "foo\n" | |
1758 * Input: | |
1759 * p is on the " | |
1760 */ | |
0 | 1761 TOK delimitedStringConstant(Token* t) |
1762 { | |
51 | 1763 uint c; |
1764 Loc start = loc; | |
1765 uint delimleft = 0; | |
1766 uint delimright = 0; | |
1767 uint nest = 1; | |
1768 uint nestcount; | |
1769 Identifier hereid = null; | |
1770 uint blankrol = 0; | |
1771 uint startline = 0; | |
1772 | |
1773 p++; | |
1774 stringbuffer.reset(); | |
1775 while (1) | |
1776 { | |
1777 c = *p++; | |
1778 //printf("c = '%c'\n", c); | |
1779 switch (c) | |
1780 { | |
1781 case '\n': | |
1782 Lnextline: | |
1783 loc.linnum++; | |
1784 startline = 1; | |
1785 if (blankrol) | |
1786 { blankrol = 0; | |
1787 continue; | |
1788 } | |
1789 if (hereid) | |
1790 { | |
1791 stringbuffer.writeUTF8(c); | |
1792 continue; | |
1793 } | |
1794 break; | |
1795 | |
1796 case '\r': | |
1797 if (*p == '\n') | |
1798 continue; // ignore | |
1799 c = '\n'; // treat EndOfLine as \n character | |
1800 goto Lnextline; | |
1801 | |
1802 case 0: | |
1803 case 0x1A: | |
1804 goto Lerror; | |
1805 | |
1806 default: | |
1807 if (c & 0x80) | |
1808 { p--; | |
1809 c = decodeUTF(); | |
1810 p++; | |
1811 if (c == PS || c == LS) | |
1812 goto Lnextline; | |
1813 } | |
1814 break; | |
1815 } | |
1816 if (delimleft == 0) | |
1817 { | |
1818 delimleft = c; | |
1819 nest = 1; | |
1820 nestcount = 1; | |
1821 if (c == '(') | |
1822 delimright = ')'; | |
1823 else if (c == '{') | |
1824 delimright = '}'; | |
1825 else if (c == '[') | |
1826 delimright = ']'; | |
1827 else if (c == '<') | |
1828 delimright = '>'; | |
1829 else if (isalpha(c) || c == '_' || (c >= 0x80 && isUniAlpha(c))) | |
1830 { | |
1831 // Start of identifier; must be a heredoc | |
1832 Token t2; | |
1833 p--; | |
1834 scan(&t2); // read in heredoc identifier | |
1835 if (t2.value != TOKidentifier) | |
1836 { | |
1837 error("identifier expected for heredoc, not %s", t2.toChars()); | |
1838 delimright = c; | |
1839 } | |
1840 else | |
1841 { | |
1842 hereid = t2.ident; | |
1843 //printf("hereid = '%s'\n", hereid.toChars()); | |
1844 blankrol = 1; | |
1845 } | |
1846 nest = 0; | |
1847 } | |
1848 else | |
1849 { | |
1850 delimright = c; | |
1851 nest = 0; | |
1852 if (isspace(c)) | |
1853 error("delimiter cannot be whitespace"); | |
1854 } | |
1855 } | |
1856 else | |
1857 { | |
1858 if (blankrol) | |
1859 { | |
1860 error("heredoc rest of line should be blank"); | |
1861 blankrol = 0; | |
1862 continue; | |
1863 } | |
1864 if (nest == 1) | |
1865 { | |
1866 if (c == delimleft) | |
1867 nestcount++; | |
1868 else if (c == delimright) | |
1869 { nestcount--; | |
1870 if (nestcount == 0) | |
1871 goto Ldone; | |
1872 } | |
1873 } | |
1874 else if (c == delimright) | |
1875 goto Ldone; | |
1876 if (startline && isalpha(c) && hereid) | |
1877 { | |
1878 Token t2; | |
1879 ubyte* psave = p; | |
1880 p--; | |
1881 scan(&t2); // read in possible heredoc identifier | |
1882 //printf("endid = '%s'\n", t2.ident.toChars()); | |
1883 if (t2.value == TOKidentifier && t2.ident.equals(hereid)) | |
1884 { | |
1885 /* should check that rest of line is blank | |
1886 */ | |
1887 goto Ldone; | |
1888 } | |
1889 p = psave; | |
1890 } | |
1891 stringbuffer.writeUTF8(c); | |
1892 startline = 0; | |
1893 } | |
1894 } | |
1895 | |
1896 Ldone: | |
1897 if (*p == '"') | |
1898 p++; | |
1899 else | |
1900 error("delimited string must end in %c\"", delimright); | |
1901 t.len = stringbuffer.offset; | |
1902 stringbuffer.writeByte(0); | |
1903 void* mem = malloc(stringbuffer.offset); | |
1904 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
1905 t.ustring = cast(const(char)*)mem; | |
1906 stringPostfix(t); | |
1907 return TOKstring; | |
1908 | |
1909 Lerror: | |
1910 error("unterminated string constant starting at %s", start.toChars()); | |
1911 t.ustring = "".ptr; | |
1912 t.len = 0; | |
1913 t.postfix = 0; | |
1914 return TOKstring; | |
0 | 1915 } |
1916 | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1917 /************************************** |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1918 * Lex delimited strings: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1919 * q{ foo(xxx) } // " foo(xxx) " |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1920 * q{foo(} // "foo(" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1921 * q{{foo}"}"} // "{foo}"}"" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1922 * Input: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1923 * p is on the q |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1924 */ |
0 | 1925 TOK tokenStringConstant(Token* t) |
1926 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1927 uint nest = 1; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1928 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1929 ubyte* pstart = ++p; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1930 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1931 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1932 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1933 Token tok; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1934 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1935 scan(&tok); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1936 switch (tok.value) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1937 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1938 case TOKlcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1939 nest++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1940 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1941 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1942 case TOKrcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1943 if (--nest == 0) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1944 goto Ldone; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1945 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1946 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1947 case TOKeof: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1948 goto Lerror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1949 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1950 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1951 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1952 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1953 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1954 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1955 Ldone: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1956 t.len = p - 1 - pstart; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1957 char* tmp = cast(char*)GC.malloc(t.len + 1); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1958 memcpy(tmp, pstart, t.len); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1959 tmp[t.len] = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1960 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1961 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1962 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1963 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1964 Lerror: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1965 error("unterminated token string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1966 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1967 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1968 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1969 return TOKstring; |
0 | 1970 } |
1971 } | |
1972 TOK escapeStringConstant(Token* t, int wide) | |
1973 { | |
1974 uint c; | |
1975 Loc start = loc; | |
1976 | |
1977 p++; | |
1978 stringbuffer.reset(); | |
1979 while (true) | |
1980 { | |
1981 c = *p++; | |
1982 switch (c) | |
1983 { | |
1984 version (TEXTUAL_ASSEMBLY_OUT) { | |
1985 } else { | |
1986 case '\\': | |
1987 switch (*p) | |
1988 { | |
1989 case 'u': | |
1990 case 'U': | |
1991 case '&': | |
1992 c = escapeSequence(); | |
1993 stringbuffer.writeUTF8(c); | |
1994 continue; | |
1995 | |
1996 default: | |
1997 c = escapeSequence(); | |
1998 break; | |
1999 } | |
2000 break; | |
2001 } | |
2002 case '\n': | |
2003 loc.linnum++; | |
2004 break; | |
2005 | |
2006 case '\r': | |
2007 if (*p == '\n') | |
2008 continue; // ignore | |
2009 c = '\n'; // treat EndOfLine as \n character | |
2010 loc.linnum++; | |
2011 break; | |
2012 | |
2013 case '"': | |
2014 t.len = stringbuffer.offset; | |
2015 stringbuffer.writeByte(0); | |
2 | 2016 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 2017 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
2018 t.ustring = tmp; | |
2019 stringPostfix(t); | |
2020 return TOK.TOKstring; | |
2021 | |
2022 case 0: | |
2023 case 0x1A: | |
2024 p--; | |
2025 error("unterminated string constant starting at %s", start.toChars()); | |
2026 t.ustring = "".ptr; | |
2027 t.len = 0; | |
2028 t.postfix = 0; | |
2029 return TOK.TOKstring; | |
2030 | |
2031 default: | |
2032 if (c & 0x80) | |
2033 { | |
2034 p--; | |
2035 c = decodeUTF(); | |
2036 if (c == LS || c == PS) | |
2037 { c = '\n'; | |
2038 loc.linnum++; | |
2039 } | |
2040 p++; | |
2041 stringbuffer.writeUTF8(c); | |
2042 continue; | |
2043 } | |
2044 break; | |
2045 } | |
2046 stringbuffer.writeByte(c); | |
2047 } | |
2048 | |
2049 assert(false); | |
2050 } | |
2051 | |
2052 TOK charConstant(Token* t, int wide) | |
2053 { | |
2054 uint c; | |
2055 TOK tk = TOKcharv; | |
2056 | |
2057 //printf("Lexer.charConstant\n"); | |
2058 p++; | |
2059 c = *p++; | |
2060 switch (c) | |
2061 { | |
2062 version (TEXTUAL_ASSEMBLY_OUT) { | |
2063 } else { | |
2064 case '\\': | |
2065 switch (*p) | |
2066 { | |
2067 case 'u': | |
2068 t.uns64value = escapeSequence(); | |
2069 tk = TOKwcharv; | |
2070 break; | |
2071 | |
2072 case 'U': | |
2073 case '&': | |
2074 t.uns64value = escapeSequence(); | |
2075 tk = TOKdcharv; | |
2076 break; | |
2077 | |
2078 default: | |
2079 t.uns64value = escapeSequence(); | |
2080 break; | |
2081 } | |
2082 break; | |
2083 } | |
2084 case '\n': | |
2085 L1: | |
2086 loc.linnum++; | |
2087 case '\r': | |
2088 case 0: | |
2089 case 0x1A: | |
2090 case '\'': | |
2091 error("unterminated character constant"); | |
2092 return tk; | |
2093 | |
2094 default: | |
2095 if (c & 0x80) | |
2096 { | |
2097 p--; | |
2098 c = decodeUTF(); | |
2099 p++; | |
2100 if (c == LS || c == PS) | |
2101 goto L1; | |
2102 if (c < 0xD800 || (c >= 0xE000 && c < 0xFFFE)) | |
2103 tk = TOKwcharv; | |
2104 else | |
2105 tk = TOKdcharv; | |
2106 } | |
2107 t.uns64value = c; | |
2108 break; | |
2109 } | |
2110 | |
2111 if (*p != '\'') | |
2112 { | |
2113 error("unterminated character constant"); | |
2114 return tk; | |
2115 } | |
2116 p++; | |
2117 return tk; | |
2118 } | |
2119 | |
2120 /*************************************** | |
2121 * Get postfix of string literal. | |
2122 */ | |
2123 void stringPostfix(Token* t) | |
2124 { | |
2125 switch (*p) | |
2126 { | |
2127 case 'c': | |
2128 case 'w': | |
2129 case 'd': | |
2130 t.postfix = *p; | |
2131 p++; | |
2132 break; | |
2133 | |
2134 default: | |
2135 t.postfix = 0; | |
2136 break; | |
2137 } | |
2138 } | |
2139 | |
2140 uint wchar_(uint u) | |
2141 { | |
2142 assert(false); | |
2143 } | |
2144 | |
2145 /************************************** | |
2146 * Read in a number. | |
2147 * If it's an integer, store it in tok.TKutok.Vlong. | |
2148 * integers can be decimal, octal or hex | |
2149 * Handle the suffixes U, UL, LU, L, etc. | |
2150 * If it's double, store it in tok.TKutok.Vdouble. | |
2151 * Returns: | |
2152 * TKnum | |
2153 * TKdouble,... | |
2154 */ | |
2155 | |
2156 TOK number(Token* t) | |
2157 { | |
2158 // We use a state machine to collect numbers | |
2159 enum STATE { STATE_initial, STATE_0, STATE_decimal, STATE_octal, STATE_octale, | |
2160 STATE_hex, STATE_binary, STATE_hex0, STATE_binary0, | |
2161 STATE_hexh, STATE_error }; | |
2162 STATE state; | |
2163 | |
2164 enum FLAGS | |
2165 { | |
2166 FLAGS_undefined = 0, | |
2167 FLAGS_decimal = 1, // decimal | |
2168 FLAGS_unsigned = 2, // u or U suffix | |
2169 FLAGS_long = 4, // l or L suffix | |
2170 }; | |
2171 | |
2172 FLAGS flags = FLAGS.FLAGS_decimal; | |
2173 | |
2174 int i; | |
2175 int base; | |
2176 uint c; | |
2177 ubyte *start; | |
2178 TOK result; | |
2179 | |
2180 //printf("Lexer.number()\n"); | |
2181 state = STATE.STATE_initial; | |
2182 base = 0; | |
2183 stringbuffer.reset(); | |
2184 start = p; | |
2185 while (1) | |
2186 { | |
2187 c = *p; | |
2188 switch (state) | |
2189 { | |
2190 case STATE.STATE_initial: // opening state | |
2191 if (c == '0') | |
2192 state = STATE.STATE_0; | |
2193 else | |
2194 state = STATE.STATE_decimal; | |
2195 break; | |
2196 | |
2197 case STATE.STATE_0: | |
2198 flags = (flags & ~FLAGS.FLAGS_decimal); | |
2199 switch (c) | |
2200 { | |
2201 version (ZEROH) { | |
2202 case 'H': // 0h | |
2203 case 'h': | |
2204 goto hexh; | |
2205 } | |
2206 case 'X': | |
2207 case 'x': | |
2208 state = STATE.STATE_hex0; | |
2209 break; | |
2210 | |
2211 case '.': | |
2212 if (p[1] == '.') // .. is a separate token | |
2213 goto done; | |
2214 case 'i': | |
2215 case 'f': | |
2216 case 'F': | |
2217 goto real_; | |
2218 version (ZEROH) { | |
2219 case 'E': | |
2220 case 'e': | |
2221 goto case_hex; | |
2222 } | |
2223 case 'B': | |
2224 case 'b': | |
2225 state = STATE.STATE_binary0; | |
2226 break; | |
2227 | |
2228 case '0': case '1': case '2': case '3': | |
2229 case '4': case '5': case '6': case '7': | |
2230 state = STATE.STATE_octal; | |
2231 break; | |
2232 | |
2233 version (ZEROH) { | |
2234 case '8': case '9': case 'A': | |
2235 case 'C': case 'D': case 'F': | |
2236 case 'a': case 'c': case 'd': case 'f': | |
2237 case_hex: | |
2238 state = STATE.STATE_hexh; | |
2239 break; | |
2240 } | |
2241 case '_': | |
2242 state = STATE.STATE_octal; | |
2243 p++; | |
2244 continue; | |
2245 | |
2246 case 'L': | |
2247 if (p[1] == 'i') | |
2248 goto real_; | |
2249 goto done; | |
2250 | |
2251 default: | |
2252 goto done; | |
2253 } | |
2254 break; | |
2255 | |
2256 case STATE.STATE_decimal: // reading decimal number | |
2257 if (!isdigit(c)) | |
2258 { | |
2259 version (ZEROH) { | |
2260 if (ishex(c) | |
2261 || c == 'H' || c == 'h' | |
2262 ) | |
2263 goto hexh; | |
2264 } | |
2265 if (c == '_') // ignore embedded _ | |
2266 { p++; | |
2267 continue; | |
2268 } | |
2269 if (c == '.' && p[1] != '.') | |
2270 goto real_; | |
2271 else if (c == 'i' || c == 'f' || c == 'F' || | |
2272 c == 'e' || c == 'E') | |
2273 { | |
2274 real_: // It's a real number. Back up and rescan as a real | |
2275 p = start; | |
2276 return inreal(t); | |
2277 } | |
2278 else if (c == 'L' && p[1] == 'i') | |
2279 goto real_; | |
2280 goto done; | |
2281 } | |
2282 break; | |
2283 | |
2284 case STATE.STATE_hex0: // reading hex number | |
2285 case STATE.STATE_hex: | |
2286 if (! ishex(cast(ubyte)c)) | |
2287 { | |
2288 if (c == '_') // ignore embedded _ | |
2289 { p++; | |
2290 continue; | |
2291 } | |
2292 if (c == '.' && p[1] != '.') | |
2293 goto real_; | |
2294 if (c == 'P' || c == 'p' || c == 'i') | |
2295 goto real_; | |
2296 if (state == STATE.STATE_hex0) | |
2297 error("Hex digit expected, not '%c'", c); | |
2298 goto done; | |
2299 } | |
2300 state = STATE.STATE_hex; | |
2301 break; | |
2302 | |
2303 version (ZEROH) { | |
2304 hexh: | |
2305 state = STATE.STATE_hexh; | |
2306 case STATE.STATE_hexh: // parse numbers like 0FFh | |
2307 if (!ishex(c)) | |
2308 { | |
2309 if (c == 'H' || c == 'h') | |
2310 { | |
2311 p++; | |
2312 base = 16; | |
2313 goto done; | |
2314 } | |
2315 else | |
2316 { | |
2317 // Check for something like 1E3 or 0E24 | |
2318 if (memchr(cast(char*)stringbuffer.data, 'E', stringbuffer.offset) || | |
2319 memchr(cast(char*)stringbuffer.data, 'e', stringbuffer.offset)) | |
2320 goto real_; | |
2321 error("Hex digit expected, not '%c'", c); | |
2322 goto done; | |
2323 } | |
2324 } | |
2325 break; | |
2326 } | |
2327 | |
2328 case STATE.STATE_octal: // reading octal number | |
2329 case STATE.STATE_octale: // reading octal number with non-octal digits | |
2330 if (!isoctal(cast(ubyte)c)) | |
2331 { | |
2332 version (ZEROH) { | |
2333 if (ishex(c) | |
2334 || c == 'H' || c == 'h' | |
2335 ) | |
2336 goto hexh; | |
2337 } | |
2338 if (c == '_') // ignore embedded _ | |
2339 { p++; | |
2340 continue; | |
2341 } | |
2342 if (c == '.' && p[1] != '.') | |
2343 goto real_; | |
2344 if (c == 'i') | |
2345 goto real_; | |
2346 if (isdigit(c)) | |
2347 { | |
2348 state = STATE.STATE_octale; | |
2349 } | |
2350 else | |
2351 goto done; | |
2352 } | |
2353 break; | |
2354 | |
2355 case STATE.STATE_binary0: // starting binary number | |
2356 case STATE.STATE_binary: // reading binary number | |
2357 if (c != '0' && c != '1') | |
2358 { | |
2359 version (ZEROH) { | |
2360 if (ishex(c) | |
2361 || c == 'H' || c == 'h' | |
2362 ) | |
2363 goto hexh; | |
2364 } | |
2365 if (c == '_') // ignore embedded _ | |
2366 { p++; | |
2367 continue; | |
2368 } | |
2369 if (state == STATE.STATE_binary0) | |
2370 { error("binary digit expected"); | |
2371 state = STATE.STATE_error; | |
2372 break; | |
2373 } | |
2374 else | |
2375 goto done; | |
2376 } | |
2377 state = STATE.STATE_binary; | |
2378 break; | |
2379 | |
2380 case STATE.STATE_error: // for error recovery | |
2381 if (!isdigit(c)) // scan until non-digit | |
2382 goto done; | |
2383 break; | |
2384 | |
2385 default: | |
2386 assert(0); | |
2387 } | |
2388 stringbuffer.writeByte(c); | |
2389 p++; | |
2390 } | |
2391 done: | |
2392 stringbuffer.writeByte(0); // terminate string | |
2393 if (state == STATE.STATE_octale) | |
2394 error("Octal digit expected"); | |
2395 | |
2396 ulong n; // unsigned >=64 bit integer type | |
2397 | |
2398 if (stringbuffer.offset == 2 && (state == STATE.STATE_decimal || state == STATE.STATE_0)) | |
2399 n = stringbuffer.data[0] - '0'; | |
2400 else | |
2401 { | |
2402 // Convert string to integer | |
2403 version (__DMC__) { | |
2404 errno = 0; | |
2405 n = strtoull(cast(char*)stringbuffer.data,null,base); | |
2406 if (errno == ERANGE) | |
2407 error("integer overflow"); | |
2408 } else { | |
2409 // Not everybody implements strtoull() | |
2410 char* p = cast(char*)stringbuffer.data; | |
2411 int r = 10, d; | |
2412 | |
2413 if (*p == '0') | |
2414 { | |
2415 if (p[1] == 'x' || p[1] == 'X') | |
2416 p += 2, r = 16; | |
2417 else if (p[1] == 'b' || p[1] == 'B') | |
2418 p += 2, r = 2; | |
2419 else if (isdigit(p[1])) | |
2420 p += 1, r = 8; | |
2421 } | |
2422 | |
2423 n = 0; | |
2424 while (1) | |
2425 { | |
2426 if (*p >= '0' && *p <= '9') | |
2427 d = *p - '0'; | |
2428 else if (*p >= 'a' && *p <= 'z') | |
2429 d = *p - 'a' + 10; | |
2430 else if (*p >= 'A' && *p <= 'Z') | |
2431 d = *p - 'A' + 10; | |
2432 else | |
2433 break; | |
2434 if (d >= r) | |
2435 break; | |
2436 ulong n2 = n * r; | |
2437 //printf("n2 / r = %llx, n = %llx\n", n2/r, n); | |
2438 if (n2 / r != n || n2 + d < n) | |
2439 { | |
2440 error ("integer overflow"); | |
2441 break; | |
2442 } | |
2443 | |
2444 n = n2 + d; | |
2445 p++; | |
2446 } | |
2447 } | |
2448 if (n.sizeof > 8 && | |
2449 n > 0xFFFFFFFFFFFFFFFF) // if n needs more than 64 bits | |
2450 error("integer overflow"); | |
2451 } | |
2452 | |
2453 // Parse trailing 'u', 'U', 'l' or 'L' in any combination | |
2454 while (1) | |
2455 { FLAGS f; | |
2456 | |
2457 switch (*p) | |
2458 { case 'U': | |
2459 case 'u': | |
2460 f = FLAGS.FLAGS_unsigned; | |
2461 goto L1; | |
2462 | |
2463 case 'l': | |
2464 if (1 || !global.params.useDeprecated) | |
2465 error("'l' suffix is deprecated, use 'L' instead"); | |
2466 case 'L': | |
2467 f = FLAGS.FLAGS_long; | |
2468 L1: | |
2469 p++; | |
2470 if (flags & f) | |
2471 error("unrecognized token"); | |
2472 flags = (flags | f); | |
2473 continue; | |
2474 default: | |
2475 break; | |
2476 } | |
2477 break; | |
2478 } | |
2479 | |
2480 switch (flags) | |
2481 { | |
2482 case FLAGS.FLAGS_undefined: | |
2483 /* Octal or Hexadecimal constant. | |
2484 * First that fits: int, uint, long, ulong | |
2485 */ | |
2486 if (n & 0x8000000000000000) | |
2487 result = TOK.TOKuns64v; | |
2488 else if (n & 0xFFFFFFFF00000000) | |
2489 result = TOK.TOKint64v; | |
2490 else if (n & 0x80000000) | |
2491 result = TOK.TOKuns32v; | |
2492 else | |
2493 result = TOK.TOKint32v; | |
2494 break; | |
2495 | |
2496 case FLAGS.FLAGS_decimal: | |
2497 /* First that fits: int, long, long long | |
2498 */ | |
2499 if (n & 0x8000000000000000) | |
2500 { error("signed integer overflow"); | |
2501 result = TOK.TOKuns64v; | |
2502 } | |
2503 else if (n & 0xFFFFFFFF80000000) | |
2504 result = TOK.TOKint64v; | |
2505 else | |
2506 result = TOK.TOKint32v; | |
2507 break; | |
2508 | |
2509 case FLAGS.FLAGS_unsigned: | |
2510 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned: | |
2511 /* First that fits: uint, ulong | |
2512 */ | |
2513 if (n & 0xFFFFFFFF00000000) | |
2514 result = TOK.TOKuns64v; | |
2515 else | |
2516 result = TOK.TOKuns32v; | |
2517 break; | |
2518 | |
2519 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_long: | |
2520 if (n & 0x8000000000000000) | |
2521 { error("signed integer overflow"); | |
2522 result = TOK.TOKuns64v; | |
2523 } | |
2524 else | |
2525 result = TOK.TOKint64v; | |
2526 break; | |
2527 | |
2528 case FLAGS.FLAGS_long: | |
2529 if (n & 0x8000000000000000) | |
2530 result = TOK.TOKuns64v; | |
2531 else | |
2532 result = TOK.TOKint64v; | |
2533 break; | |
2534 | |
2535 case FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2536 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2537 result = TOK.TOKuns64v; | |
2538 break; | |
2539 | |
2540 default: | |
2541 debug { | |
2542 printf("%x\n",flags); | |
2543 } | |
2544 assert(0); | |
2545 } | |
2546 t.uns64value = n; | |
2547 return result; | |
2548 } | |
2549 | |
2550 /************************************** | |
2551 * Read in characters, converting them to real. | |
2552 * Bugs: | |
2553 * Exponent overflow not detected. | |
2554 * Too much requested precision is not detected. | |
2555 */ | |
2556 TOK inreal(Token* t) | |
2557 in | |
2558 { | |
2559 assert(*p == '.' || isdigit(*p)); | |
2560 } | |
2561 out (result) | |
2562 { | |
2563 switch (result) | |
2564 { | |
2565 case TOKfloat32v: | |
2566 case TOKfloat64v: | |
2567 case TOKfloat80v: | |
2568 case TOKimaginary32v: | |
2569 case TOKimaginary64v: | |
2570 case TOKimaginary80v: | |
2571 break; | |
2572 | |
2573 default: | |
2574 assert(0); | |
2575 } | |
2576 } | |
2577 body | |
2578 { | |
2579 int dblstate; | |
2580 uint c; | |
2581 char hex; // is this a hexadecimal-floating-constant? | |
2582 TOK result; | |
2583 | |
2584 //printf("Lexer.inreal()\n"); | |
2585 stringbuffer.reset(); | |
2586 dblstate = 0; | |
2587 hex = 0; | |
2588 Lnext: | |
2589 while (true) | |
2590 { | |
2591 // Get next char from input | |
2592 c = *p++; | |
2593 //printf("dblstate = %d, c = '%c'\n", dblstate, c); | |
2594 while (true) | |
2595 { | |
2596 switch (dblstate) | |
2597 { | |
2598 case 0: // opening state | |
2599 if (c == '0') | |
2600 dblstate = 9; | |
2601 else if (c == '.') | |
2602 dblstate = 3; | |
2603 else | |
2604 dblstate = 1; | |
2605 break; | |
2606 | |
2607 case 9: | |
2608 dblstate = 1; | |
2609 if (c == 'X' || c == 'x') | |
2610 { | |
2611 hex++; | |
2612 break; | |
2613 } | |
2614 case 1: // digits to left of . | |
2615 case 3: // digits to right of . | |
2616 case 7: // continuing exponent digits | |
2617 if (!isdigit(c) && !(hex && isxdigit(c))) | |
2618 { | |
2619 if (c == '_') | |
2620 goto Lnext; // ignore embedded '_' | |
2621 dblstate++; | |
2622 continue; | |
2623 } | |
2624 break; | |
2625 | |
2626 case 2: // no more digits to left of . | |
2627 if (c == '.') | |
2628 { | |
2629 dblstate++; | |
2630 break; | |
2631 } | |
2632 case 4: // no more digits to right of . | |
2633 if ((c == 'E' || c == 'e') || | |
2634 hex && (c == 'P' || c == 'p')) | |
2635 { | |
2636 dblstate = 5; | |
2637 hex = 0; // exponent is always decimal | |
2638 break; | |
2639 } | |
2640 if (hex) | |
2641 error("binary-exponent-part required"); | |
2642 goto done; | |
2643 | |
2644 case 5: // looking immediately to right of E | |
2645 dblstate++; | |
2646 if (c == '-' || c == '+') | |
2647 break; | |
2648 case 6: // 1st exponent digit expected | |
2649 if (!isdigit(c)) | |
2650 error("exponent expected"); | |
2651 dblstate++; | |
2652 break; | |
2653 | |
2654 case 8: // past end of exponent digits | |
2655 goto done; | |
79 | 2656 |
2657 default: | |
2658 assert(0, "inreal.dblstate has unexpected value"); | |
0 | 2659 } |
2660 break; | |
2661 } | |
2662 stringbuffer.writeByte(c); | |
2663 } | |
2664 done: | |
2665 p--; | |
2666 | |
2667 stringbuffer.writeByte(0); | |
2668 | |
114 | 2669 version (Windows) { /// && __DMC__ |
0 | 2670 char* save = __locale_decpoint; |
2671 __locale_decpoint = cast(char*)".".ptr; | |
2672 } | |
2673 t.float80value = strtold(cast(char*)stringbuffer.data, null); | |
2674 | |
2675 errno = 0; | |
2676 switch (*p) | |
2677 { | |
2678 case 'F': | |
2679 case 'f': | |
2680 strtof(cast(char*)stringbuffer.data, null); | |
2681 result = TOKfloat32v; | |
2682 p++; | |
2683 break; | |
2684 | |
2685 default: | |
2686 strtod(cast(char*)stringbuffer.data, null); | |
2687 result = TOKfloat64v; | |
2688 break; | |
2689 | |
2690 case 'l': | |
2691 if (!global.params.useDeprecated) | |
2692 error("'l' suffix is deprecated, use 'L' instead"); | |
2693 case 'L': | |
2694 result = TOKfloat80v; | |
2695 p++; | |
2696 break; | |
2697 } | |
2698 if (*p == 'i' || *p == 'I') | |
2699 { | |
2700 if (!global.params.useDeprecated && *p == 'I') | |
2701 error("'I' suffix is deprecated, use 'i' instead"); | |
2702 p++; | |
2703 switch (result) | |
2704 { | |
2705 case TOKfloat32v: | |
2706 result = TOKimaginary32v; | |
2707 break; | |
2708 case TOKfloat64v: | |
2709 result = TOKimaginary64v; | |
2710 break; | |
2711 case TOKfloat80v: | |
2712 result = TOKimaginary80v; | |
2713 break; | |
2714 } | |
2715 } | |
2716 | |
114 | 2717 version (Windows) { ///&& __DMC__ |
0 | 2718 __locale_decpoint = save; |
2719 } | |
2720 if (errno == ERANGE) | |
2721 error("number is not representable"); | |
2722 | |
2723 return result; | |
2724 } | |
2725 | |
2726 void error(T...)(string format, T t) | |
2727 { | |
2728 error(this.loc, format, t); | |
2729 } | |
2730 | |
2731 void error(T...)(Loc loc, string format, T t) | |
2732 { | |
2733 if (mod && !global.gag) | |
2734 { | |
2735 string p = loc.toChars(); | |
2736 if (p.length != 0) | |
2737 writef("%s: ", p); | |
2738 | |
2739 writefln(format, t); | |
2740 | |
2741 if (global.errors >= 20) // moderate blizzard of cascading messages | |
2742 fatal(); | |
2743 } | |
2744 | |
2745 global.errors++; | |
2746 } | |
2747 | |
2748 void pragma_() | |
2749 { | |
2750 assert(false); | |
2751 } | |
2752 | |
49 | 2753 /******************************************** |
2754 * Decode UTF character. | |
2755 * Issue error messages for invalid sequences. | |
2756 * Return decoded character, advance p to last character in UTF sequence. | |
2757 */ | |
0 | 2758 uint decodeUTF() |
2759 { | |
49 | 2760 dchar u; |
2761 ubyte c; | |
2762 ubyte* s = p; | |
2763 size_t len; | |
2764 size_t idx; | |
2765 string msg; | |
2766 | |
2767 c = *s; | |
2768 assert(c & 0x80); | |
2769 | |
2770 // Check length of remaining string up to 6 UTF-8 characters | |
2771 for (len = 1; len < 6 && s[len]; len++) { | |
2772 ; | |
2773 } | |
2774 | |
2775 idx = 0; | |
2776 msg = utf_decodeChar(cast(string)s[0..len], &idx, &u); | |
2777 p += idx - 1; | |
2778 if (msg) | |
2779 { | |
2780 error("%s", msg); | |
2781 } | |
2782 return u; | |
0 | 2783 } |
2784 | |
2785 void getDocComment(Token* t, uint lineComment) | |
2786 { | |
2787 assert(false); | |
2788 } | |
2789 | |
2790 static bool isValidIdentifier(string p) | |
2791 { | |
2792 if (p.length == 0) { | |
2793 return false; | |
2794 } | |
2795 | |
2796 if (p[0] >= '0' && p[0] <= '9') { // beware of isdigit() on signed chars | |
2797 return false; | |
2798 } | |
2799 | |
2800 size_t idx = 0; | |
2801 while (idx < p.length) | |
2802 { | |
2803 dchar dc; | |
2804 | |
2805 if (utf_decodeChar(p, &idx, &dc) !is null) { | |
2806 return false; | |
2807 } | |
2808 | |
2809 if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_')) { | |
2810 return false; | |
2811 } | |
2812 } | |
2813 | |
2814 return true; | |
2815 } | |
2816 | |
79 | 2817 /// TODO: use normal string append when GC works |
2818 static string combineComments(const(char)[] c1, const(char)[] c2) | |
0 | 2819 { |
79 | 2820 //writef("Lexer.combineComments('%s', '%s')\n", c1, c2); |
0 | 2821 |
79 | 2822 char[] c = cast(char[]) c2; |
2823 | |
2824 if (c1 !is null) | |
0 | 2825 { |
79 | 2826 c = cast(char[]) c1; |
2827 if (c2 !is null) | |
0 | 2828 { |
79 | 2829 c = cast(char[]) (GC.malloc(c1.length + 1 + c2.length)[0 .. c1.length + 1 + c2.length]); |
2830 size_t len1 = c1.length; | |
2831 c[0..len1] = c1[]; | |
2832 c[len1++] = '\n'; | |
2833 c[len1 .. len1 + c2.length] = c2[]; | |
0 | 2834 } |
2835 } | |
79 | 2836 |
2837 return cast(string)c; | |
0 | 2838 } |
2839 } |