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