Mercurial > projects > ddmd
annotate dmd/Lexer.d @ 114:e28b18c23469
added a module dmd.common for commonly used stuff
it currently holds code for consistency checking of predefined versions
also added a VisualD project file
author | Trass3r |
---|---|
date | Wed, 01 Sep 2010 18:21:58 +0200 |
parents | 43073c7c7769 |
children | 60bb0fe4563e |
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] = "@"; | |
500 | |
501 // For debugging | |
73 | 502 Token.tochars[TOKerror] = "error"; |
0 | 503 Token.tochars[TOK.TOKdotexp] = "dotexp"; |
504 Token.tochars[TOK.TOKdotti] = "dotti"; | |
505 Token.tochars[TOK.TOKdotvar] = "dotvar"; | |
506 Token.tochars[TOK.TOKdottype] = "dottype"; | |
507 Token.tochars[TOK.TOKsymoff] = "symoff"; | |
508 Token.tochars[TOK.TOKarraylength] = "arraylength"; | |
509 Token.tochars[TOK.TOKarrayliteral] = "arrayliteral"; | |
510 Token.tochars[TOK.TOKassocarrayliteral] = "assocarrayliteral"; | |
511 Token.tochars[TOK.TOKstructliteral] = "structliteral"; | |
512 Token.tochars[TOK.TOKstring] = "string"; | |
513 Token.tochars[TOK.TOKdsymbol] = "symbol"; | |
514 Token.tochars[TOK.TOKtuple] = "tuple"; | |
515 Token.tochars[TOK.TOKdeclaration] = "declaration"; | |
516 Token.tochars[TOK.TOKdottd] = "dottd"; | |
517 Token.tochars[TOK.TOKon_scope_exit] = "scope(exit)"; | |
518 Token.tochars[TOK.TOKon_scope_success] = "scope(success)"; | |
519 Token.tochars[TOK.TOKon_scope_failure] = "scope(failure)"; | |
520 } | |
521 | |
522 static Identifier idPool(string s) | |
523 { | |
524 StringValue* sv = stringtable.update(s); | |
525 Identifier id = cast(Identifier) sv.ptrvalue; | |
526 if (id is null) | |
527 { | |
528 id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
529 sv.ptrvalue = cast(void*)id; | |
530 } | |
531 | |
532 return id; | |
533 } | |
534 | |
535 static Identifier uniqueId(string s) | |
536 { | |
537 static int num; | |
538 return uniqueId(s, ++num); | |
539 } | |
540 | |
541 /********************************************* | |
542 * Create a unique identifier using the prefix s. | |
543 */ | |
544 static Identifier uniqueId(string s, int num) | |
545 { | |
546 char buffer[32]; | |
547 size_t slen = s.length; | |
548 | |
549 assert(slen + num.sizeof * 3 + 1 <= buffer.sizeof); | |
550 int len = sprintf(buffer.ptr, "%.*s%d", s, num); | |
551 | |
552 return idPool(buffer[0..len].idup); | |
553 } | |
554 | |
555 TOK nextToken() | |
556 { | |
557 Token *t; | |
558 | |
559 if (token.next) | |
560 { | |
561 t = token.next; | |
562 memcpy(&token, t, Token.sizeof); | |
563 t.next = freelist; | |
564 freelist = t; | |
565 } | |
566 else | |
567 { | |
568 scan(&token); | |
569 } | |
570 | |
571 //token.print(); | |
572 return token.value; | |
573 } | |
574 | |
575 /*********************** | |
576 * Look ahead at next token's value. | |
577 */ | |
578 TOK peekNext() | |
579 { | |
580 return peek(&token).value; | |
581 } | |
582 | |
583 TOK peekNext2() | |
584 { | |
585 assert(false); | |
586 } | |
587 | |
588 void scan(Token* t) | |
589 { | |
590 uint lastLine = loc.linnum; | |
591 uint linnum; | |
592 | |
593 t.blockComment = null; | |
594 t.lineComment = null; | |
595 while (1) | |
596 { | |
597 t.ptr = p; | |
598 //printf("p = %p, *p = '%c'\n",p,*p); | |
599 switch (*p) | |
600 { | |
601 case 0: | |
602 case 0x1A: | |
603 t.value = TOK.TOKeof; // end of file | |
604 return; | |
605 | |
606 case ' ': | |
607 case '\t': | |
608 case '\v': | |
609 case '\f': | |
610 p++; | |
611 continue; // skip white space | |
612 | |
613 case '\r': | |
614 p++; | |
615 if (*p != '\n') // if CR stands by itself | |
616 loc.linnum++; | |
617 continue; // skip white space | |
618 | |
619 case '\n': | |
620 p++; | |
621 loc.linnum++; | |
622 continue; // skip white space | |
623 | |
624 case '0': case '1': case '2': case '3': case '4': | |
625 case '5': case '6': case '7': case '8': case '9': | |
626 t.value = number(t); | |
627 return; | |
628 | |
629 version (CSTRINGS) { | |
630 case '\'': | |
631 t.value = charConstant(t, 0); | |
632 return; | |
633 | |
634 case '"': | |
635 t.value = stringConstant(t,0); | |
636 return; | |
637 | |
638 case 'l': | |
639 case 'L': | |
640 if (p[1] == '\'') | |
641 { | |
642 p++; | |
643 t.value = charConstant(t, 1); | |
644 return; | |
645 } | |
646 else if (p[1] == '"') | |
647 { | |
648 p++; | |
649 t.value = stringConstant(t, 1); | |
650 return; | |
651 } | |
652 } else { | |
653 case '\'': | |
654 t.value = charConstant(t,0); | |
655 return; | |
656 | |
657 case 'r': | |
658 if (p[1] != '"') | |
659 goto case_ident; | |
660 p++; | |
661 case '`': | |
662 t.value = wysiwygStringConstant(t, *p); | |
663 return; | |
664 | |
665 case 'x': | |
666 if (p[1] != '"') | |
667 goto case_ident; | |
668 p++; | |
669 t.value = hexStringConstant(t); | |
670 return; | |
671 | |
672 version (DMDV2) { | |
673 case 'q': | |
674 if (p[1] == '"') | |
675 { | |
676 p++; | |
677 t.value = delimitedStringConstant(t); | |
678 return; | |
679 } | |
680 else if (p[1] == '{') | |
681 { | |
682 p++; | |
683 t.value = tokenStringConstant(t); | |
684 return; | |
685 } | |
686 else | |
687 goto case_ident; | |
688 } | |
689 | |
690 case '"': | |
691 t.value = escapeStringConstant(t,0); | |
692 return; | |
693 version (TEXTUAL_ASSEMBLY_OUT) { | |
694 } else { | |
695 case '\\': // escaped string literal | |
696 { uint c; | |
697 ubyte* pstart = p; | |
698 | |
699 stringbuffer.reset(); | |
700 do | |
701 { | |
702 p++; | |
703 switch (*p) | |
704 { | |
705 case 'u': | |
706 case 'U': | |
707 case '&': | |
708 c = escapeSequence(); | |
709 stringbuffer.writeUTF8(c); | |
710 break; | |
711 | |
712 default: | |
713 c = escapeSequence(); | |
714 stringbuffer.writeByte(c); | |
715 break; | |
716 } | |
717 } while (*p == '\\'); | |
718 t.len = stringbuffer.offset; | |
719 stringbuffer.writeByte(0); | |
2 | 720 char* cc = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 721 memcpy(cc, stringbuffer.data, stringbuffer.offset); |
722 t.ustring = cc; | |
723 t.postfix = 0; | |
724 t.value = TOK.TOKstring; | |
725 if (!global.params.useDeprecated) | |
726 error("Escape String literal %.*s is deprecated, use double quoted string literal \"%.*s\" instead", p - pstart, pstart, p - pstart, pstart); | |
727 return; | |
728 } | |
729 } | |
730 case 'l': | |
731 case 'L': | |
732 } | |
733 case 'a': case 'b': case 'c': case 'd': case 'e': | |
734 case 'f': case 'g': case 'h': case 'i': case 'j': | |
735 case 'k': case 'm': case 'n': case 'o': | |
736 version (DMDV2) { | |
737 case 'p': /*case 'q': case 'r':*/ case 's': case 't': | |
738 } else { | |
739 case 'p': case 'q': /*case 'r':*/ case 's': case 't': | |
740 } | |
741 case 'u': case 'v': case 'w': /*case 'x':*/ case 'y': | |
742 case 'z': | |
743 case 'A': case 'B': case 'C': case 'D': case 'E': | |
744 case 'F': case 'G': case 'H': case 'I': case 'J': | |
745 case 'K': case 'M': case 'N': case 'O': | |
746 case 'P': case 'Q': case 'R': case 'S': case 'T': | |
747 case 'U': case 'V': case 'W': case 'X': case 'Y': | |
748 case 'Z': | |
749 case '_': | |
750 case_ident: | |
751 { ubyte c; | |
752 StringValue *sv; | |
753 Identifier id; | |
754 | |
755 do | |
756 { | |
757 c = *++p; | |
758 } while (isidchar(c) || (c & 0x80 && isUniAlpha(decodeUTF()))); | |
759 sv = stringtable.update((cast(immutable(char)*)t.ptr)[0.. p - t.ptr]); /// | |
760 id = cast(Identifier) sv.ptrvalue; | |
761 if (id is null) | |
762 { id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
763 sv.ptrvalue = cast(void*)id; | |
764 } | |
765 t.ident = id; | |
766 t.value = cast(TOK) id.value; | |
767 anyToken = 1; | |
768 if (*t.ptr == '_') // if special identifier token | |
769 { | |
770 static char date[11+1]; | |
771 static char time[8+1]; | |
772 static char timestamp[24+1]; | |
773 | |
774 if (!date[0]) // lazy evaluation | |
775 { time_t tm; | |
776 char *p; | |
777 | |
778 .time(&tm); | |
779 p = ctime(&tm); | |
780 assert(p); | |
781 sprintf(date.ptr, "%.6s %.4s", p + 4, p + 20); | |
782 sprintf(time.ptr, "%.8s", p + 11); | |
783 sprintf(timestamp.ptr, "%.24s", p); | |
784 } | |
785 | |
786 ///version (DMDV1) { | |
787 /// if (mod && id == Id.FILE) | |
788 /// { | |
789 /// t.ustring = cast(ubyte*)(loc.filename ? loc.filename : mod.ident.toChars()); | |
790 /// goto Lstr; | |
791 /// } | |
792 /// else if (mod && id == Id.LINE) | |
793 /// { | |
794 /// t.value = TOK.TOKint64v; | |
795 /// t.uns64value = loc.linnum; | |
796 /// } | |
797 /// else | |
798 ///} | |
799 if (id == Id.DATE) | |
800 { | |
801 t.ustring = date.ptr; | |
802 goto Lstr; | |
803 } | |
804 else if (id == Id.TIME) | |
805 { | |
806 t.ustring = time.ptr; | |
807 goto Lstr; | |
808 } | |
809 else if (id == Id.VENDOR) | |
810 { | |
811 t.ustring = "Digital Mars D".ptr; | |
812 goto Lstr; | |
813 } | |
814 else if (id == Id.TIMESTAMP) | |
815 { | |
816 t.ustring = timestamp.ptr; | |
817 Lstr: | |
818 t.value = TOK.TOKstring; | |
819 Llen: | |
820 t.postfix = 0; | |
821 t.len = strlen(cast(char*)t.ustring); | |
822 } | |
823 else if (id == Id.VERSIONX) | |
824 { | |
825 uint major = 0; | |
826 uint minor = 0; | |
827 | |
828 foreach (char cc; global.version_[1..$]) | |
829 { | |
830 if (isdigit(cc)) | |
831 minor = minor * 10 + cc - '0'; | |
832 else if (cc == '.') | |
833 { | |
834 major = minor; | |
835 minor = 0; | |
836 } | |
837 else | |
838 break; | |
839 } | |
840 t.value = TOK.TOKint64v; | |
841 t.uns64value = major * 1000 + minor; | |
842 } | |
843 ///version (DMDV2) { | |
844 else if (id == Id.EOFX) | |
845 { | |
846 t.value = TOK.TOKeof; | |
847 // Advance scanner to end of file | |
848 while (!(*p == 0 || *p == 0x1A)) | |
849 p++; | |
850 } | |
851 ///} | |
852 } | |
853 //printf("t.value = %d\n",t.value); | |
854 return; | |
855 } | |
856 | |
857 case '/': | |
858 p++; | |
859 switch (*p) | |
860 { | |
861 case '=': | |
862 p++; | |
863 t.value = TOK.TOKdivass; | |
864 return; | |
865 | |
866 case '*': | |
867 p++; | |
868 linnum = loc.linnum; | |
869 while (1) | |
870 { | |
871 while (1) | |
872 { | |
873 ubyte c = *p; | |
874 switch (c) | |
875 { | |
876 case '/': | |
877 break; | |
878 | |
879 case '\n': | |
880 loc.linnum++; | |
881 p++; | |
882 continue; | |
883 | |
884 case '\r': | |
885 p++; | |
886 if (*p != '\n') | |
887 loc.linnum++; | |
888 continue; | |
889 | |
890 case 0: | |
891 case 0x1A: | |
892 error("unterminated /* */ comment"); | |
893 p = end; | |
894 t.value = TOK.TOKeof; | |
895 return; | |
896 | |
897 default: | |
898 if (c & 0x80) | |
899 { uint u = decodeUTF(); | |
900 if (u == PS || u == LS) | |
901 loc.linnum++; | |
902 } | |
903 p++; | |
904 continue; | |
905 } | |
906 break; | |
907 } | |
908 p++; | |
909 if (p[-2] == '*' && p - 3 != t.ptr) | |
910 break; | |
911 } | |
912 if (commentToken) | |
913 { | |
914 t.value = TOK.TOKcomment; | |
915 return; | |
916 } | |
917 else if (doDocComment && t.ptr[2] == '*' && p - 4 != t.ptr) | |
918 { // if /** but not /**/ | |
919 getDocComment(t, lastLine == linnum); | |
920 } | |
921 continue; | |
922 | |
923 case '/': // do // style comments | |
924 linnum = loc.linnum; | |
925 while (1) | |
926 { ubyte c = *++p; | |
927 switch (c) | |
928 { | |
929 case '\n': | |
930 break; | |
931 | |
932 case '\r': | |
933 if (p[1] == '\n') | |
934 p++; | |
935 break; | |
936 | |
937 case 0: | |
938 case 0x1A: | |
939 if (commentToken) | |
940 { | |
941 p = end; | |
942 t.value = TOK.TOKcomment; | |
943 return; | |
944 } | |
945 if (doDocComment && t.ptr[2] == '/') | |
946 getDocComment(t, lastLine == linnum); | |
947 p = end; | |
948 t.value = TOK.TOKeof; | |
949 return; | |
950 | |
951 default: | |
952 if (c & 0x80) | |
953 { uint u = decodeUTF(); | |
954 if (u == PS || u == LS) | |
955 break; | |
956 } | |
957 continue; | |
958 } | |
959 break; | |
960 } | |
961 | |
962 if (commentToken) | |
963 { | |
964 p++; | |
965 loc.linnum++; | |
966 t.value = TOK.TOKcomment; | |
967 return; | |
968 } | |
969 if (doDocComment && t.ptr[2] == '/') | |
970 getDocComment(t, lastLine == linnum); | |
971 | |
972 p++; | |
973 loc.linnum++; | |
974 continue; | |
975 | |
976 case '+': | |
977 { | |
978 int nest; | |
979 | |
980 linnum = loc.linnum; | |
981 p++; | |
982 nest = 1; | |
983 while (1) | |
984 { ubyte c = *p; | |
985 switch (c) | |
986 { | |
987 case '/': | |
988 p++; | |
989 if (*p == '+') | |
990 { | |
991 p++; | |
992 nest++; | |
993 } | |
994 continue; | |
995 | |
996 case '+': | |
997 p++; | |
998 if (*p == '/') | |
999 { | |
1000 p++; | |
1001 if (--nest == 0) | |
1002 break; | |
1003 } | |
1004 continue; | |
1005 | |
1006 case '\r': | |
1007 p++; | |
1008 if (*p != '\n') | |
1009 loc.linnum++; | |
1010 continue; | |
1011 | |
1012 case '\n': | |
1013 loc.linnum++; | |
1014 p++; | |
1015 continue; | |
1016 | |
1017 case 0: | |
1018 case 0x1A: | |
1019 error("unterminated /+ +/ comment"); | |
1020 p = end; | |
1021 t.value = TOK.TOKeof; | |
1022 return; | |
1023 | |
1024 default: | |
1025 if (c & 0x80) | |
1026 { uint u = decodeUTF(); | |
1027 if (u == PS || u == LS) | |
1028 loc.linnum++; | |
1029 } | |
1030 p++; | |
1031 continue; | |
1032 } | |
1033 break; | |
1034 } | |
1035 if (commentToken) | |
1036 { | |
1037 t.value = TOK.TOKcomment; | |
1038 return; | |
1039 } | |
1040 if (doDocComment && t.ptr[2] == '+' && p - 4 != t.ptr) | |
1041 { // if /++ but not /++/ | |
1042 getDocComment(t, lastLine == linnum); | |
1043 } | |
1044 continue; | |
1045 } | |
1046 | |
1047 default: | |
1048 break; /// | |
1049 } | |
1050 t.value = TOK.TOKdiv; | |
1051 return; | |
1052 | |
1053 case '.': | |
1054 p++; | |
1055 if (isdigit(*p)) | |
1056 { /* Note that we don't allow ._1 and ._ as being | |
1057 * valid floating point numbers. | |
1058 */ | |
1059 p--; | |
1060 t.value = inreal(t); | |
1061 } | |
1062 else if (p[0] == '.') | |
1063 { | |
1064 if (p[1] == '.') | |
1065 { p += 2; | |
1066 t.value = TOK.TOKdotdotdot; | |
1067 } | |
1068 else | |
1069 { p++; | |
1070 t.value = TOK.TOKslice; | |
1071 } | |
1072 } | |
1073 else | |
1074 t.value = TOK.TOKdot; | |
1075 return; | |
1076 | |
1077 case '&': | |
1078 p++; | |
1079 if (*p == '=') | |
1080 { p++; | |
1081 t.value = TOK.TOKandass; | |
1082 } | |
1083 else if (*p == '&') | |
1084 { p++; | |
1085 t.value = TOK.TOKandand; | |
1086 } | |
1087 else | |
1088 t.value = TOK.TOKand; | |
1089 return; | |
1090 | |
1091 case '|': | |
1092 p++; | |
1093 if (*p == '=') | |
1094 { p++; | |
1095 t.value = TOK.TOKorass; | |
1096 } | |
1097 else if (*p == '|') | |
1098 { p++; | |
1099 t.value = TOK.TOKoror; | |
1100 } | |
1101 else | |
1102 t.value = TOK.TOKor; | |
1103 return; | |
1104 | |
1105 case '-': | |
1106 p++; | |
1107 if (*p == '=') | |
1108 { p++; | |
1109 t.value = TOK.TOKminass; | |
1110 } | |
1111 /// #if 0 | |
1112 /// else if (*p == '>') | |
1113 /// { p++; | |
1114 /// t.value = TOK.TOKarrow; | |
1115 /// } | |
1116 /// #endif | |
1117 else if (*p == '-') | |
1118 { p++; | |
1119 t.value = TOK.TOKminusminus; | |
1120 } | |
1121 else | |
1122 t.value = TOK.TOKmin; | |
1123 return; | |
1124 | |
1125 case '+': | |
1126 p++; | |
1127 if (*p == '=') | |
1128 { p++; | |
1129 t.value = TOK.TOKaddass; | |
1130 } | |
1131 else if (*p == '+') | |
1132 { p++; | |
1133 t.value = TOK.TOKplusplus; | |
1134 } | |
1135 else | |
1136 t.value = TOK.TOKadd; | |
1137 return; | |
1138 | |
1139 case '<': | |
1140 p++; | |
1141 if (*p == '=') | |
1142 { p++; | |
1143 t.value = TOK.TOKle; // <= | |
1144 } | |
1145 else if (*p == '<') | |
1146 { p++; | |
1147 if (*p == '=') | |
1148 { p++; | |
1149 t.value = TOK.TOKshlass; // <<= | |
1150 } | |
1151 else | |
1152 t.value = TOK.TOKshl; // << | |
1153 } | |
1154 else if (*p == '>') | |
1155 { p++; | |
1156 if (*p == '=') | |
1157 { p++; | |
1158 t.value = TOK.TOKleg; // <>= | |
1159 } | |
1160 else | |
1161 t.value = TOK.TOKlg; // <> | |
1162 } | |
1163 else | |
1164 t.value = TOK.TOKlt; // < | |
1165 return; | |
1166 | |
1167 case '>': | |
1168 p++; | |
1169 if (*p == '=') | |
1170 { p++; | |
1171 t.value = TOK.TOKge; // >= | |
1172 } | |
1173 else if (*p == '>') | |
1174 { p++; | |
1175 if (*p == '=') | |
1176 { p++; | |
1177 t.value = TOK.TOKshrass; // >>= | |
1178 } | |
1179 else if (*p == '>') | |
1180 { p++; | |
1181 if (*p == '=') | |
1182 { p++; | |
1183 t.value = TOK.TOKushrass; // >>>= | |
1184 } | |
1185 else | |
1186 t.value = TOK.TOKushr; // >>> | |
1187 } | |
1188 else | |
1189 t.value = TOK.TOKshr; // >> | |
1190 } | |
1191 else | |
1192 t.value = TOK.TOKgt; // > | |
1193 return; | |
1194 | |
1195 case '!': | |
1196 p++; | |
1197 if (*p == '=') | |
1198 { p++; | |
1199 if (*p == '=' && global.params.Dversion == 1) | |
1200 { p++; | |
1201 t.value = TOK.TOKnotidentity; // !== | |
1202 } | |
1203 else | |
1204 t.value = TOK.TOKnotequal; // != | |
1205 } | |
1206 else if (*p == '<') | |
1207 { p++; | |
1208 if (*p == '>') | |
1209 { p++; | |
1210 if (*p == '=') | |
1211 { p++; | |
1212 t.value = TOK.TOKunord; // !<>= | |
1213 } | |
1214 else | |
1215 t.value = TOK.TOKue; // !<> | |
1216 } | |
1217 else if (*p == '=') | |
1218 { p++; | |
1219 t.value = TOK.TOKug; // !<= | |
1220 } | |
1221 else | |
1222 t.value = TOK.TOKuge; // !< | |
1223 } | |
1224 else if (*p == '>') | |
1225 { p++; | |
1226 if (*p == '=') | |
1227 { p++; | |
1228 t.value = TOK.TOKul; // !>= | |
1229 } | |
1230 else | |
1231 t.value = TOK.TOKule; // !> | |
1232 } | |
1233 else | |
1234 t.value = TOK.TOKnot; // ! | |
1235 return; | |
1236 | |
1237 case '=': | |
1238 p++; | |
1239 if (*p == '=') | |
1240 { p++; | |
1241 if (*p == '=' && global.params.Dversion == 1) | |
1242 { p++; | |
1243 t.value = TOK.TOKidentity; // === | |
1244 } | |
1245 else | |
1246 t.value = TOK.TOKequal; // == | |
1247 } | |
1248 else | |
1249 t.value = TOK.TOKassign; // = | |
1250 return; | |
1251 | |
1252 case '~': | |
1253 p++; | |
1254 if (*p == '=') | |
1255 { p++; | |
1256 t.value = TOK.TOKcatass; // ~= | |
1257 } | |
1258 else | |
1259 t.value = TOK.TOKtilde; // ~ | |
1260 return; | |
1261 /* | |
1262 #define SINGLE(c,tok) case c: p++; t.value = tok; return; | |
1263 | |
1264 SINGLE('(', TOKlparen) | |
1265 SINGLE(')', TOKrparen) | |
1266 SINGLE('[', TOKlbracket) | |
1267 SINGLE(']', TOKrbracket) | |
1268 SINGLE('{', TOKlcurly) | |
1269 SINGLE('}', TOKrcurly) | |
1270 SINGLE('?', TOKquestion) | |
1271 SINGLE(',', TOKcomma) | |
1272 SINGLE(';', TOKsemicolon) | |
1273 SINGLE(':', TOKcolon) | |
1274 SINGLE('$', TOKdollar) | |
1275 SINGLE('@', TOKat) | |
1276 | |
1277 #undef SINGLE | |
1278 | |
1279 #define DOUBLE(c1,tok1,c2,tok2) \ | |
1280 case c1: \ | |
1281 p++; \ | |
1282 if (*p == c2) \ | |
1283 { p++; \ | |
1284 t.value = tok2; \ | |
1285 } \ | |
1286 else \ | |
1287 t.value = tok1; \ | |
1288 return; | |
1289 | |
1290 DOUBLE('*', TOKmul, '=', TOKmulass) | |
1291 DOUBLE('%', TOKmod, '=', TOKmodass) | |
1292 DOUBLE('^', TOKxor, '=', TOKxorass) | |
1293 | |
1294 #undef DOUBLE | |
1295 */ | |
1296 | |
1297 case '(': p++; t.value = TOK.TOKlparen; return; | |
1298 case ')': p++; t.value = TOK.TOKrparen; return; | |
1299 case '[': p++; t.value = TOK.TOKlbracket; return; | |
1300 case ']': p++; t.value = TOK.TOKrbracket; return; | |
1301 case '{': p++; t.value = TOK.TOKlcurly; return; | |
1302 case '}': p++; t.value = TOK.TOKrcurly; return; | |
1303 case '?': p++; t.value = TOK.TOKquestion; return; | |
1304 case ',': p++; t.value = TOK.TOKcomma; return; | |
1305 case ';': p++; t.value = TOK.TOKsemicolon; return; | |
1306 case ':': p++; t.value = TOK.TOKcolon; return; | |
1307 case '$': p++; t.value = TOK.TOKdollar; return; | |
1308 case '@': p++; t.value = TOK.TOKat; return; | |
1309 | |
1310 case '*': | |
1311 p++; | |
1312 if (*p == '=') { | |
1313 p++; | |
1314 t.value = TOK.TOKmulass; | |
1315 } else { | |
1316 t.value = TOK.TOKmul; | |
1317 } | |
1318 return; | |
1319 | |
1320 case '%': | |
1321 p++; | |
1322 if (*p == '=') { | |
1323 p++; | |
1324 t.value = TOK.TOKmodass; | |
1325 } else { | |
1326 t.value = TOK.TOKmod; | |
1327 } | |
1328 return; | |
1329 | |
1330 case '^': | |
1331 p++; | |
1332 if (*p == '=') { | |
1333 p++; | |
1334 t.value = TOK.TOKxorass; | |
1335 } else { | |
1336 t.value = TOK.TOKxor; | |
1337 } | |
1338 return; | |
1339 | |
1340 case '#': | |
1341 p++; | |
1342 pragma_(); | |
1343 continue; | |
1344 | |
1345 default: | |
1346 { ubyte c = *p; | |
1347 | |
1348 if (c & 0x80) | |
1349 { uint u = decodeUTF(); | |
1350 | |
1351 // Check for start of unicode identifier | |
1352 if (isUniAlpha(u)) | |
1353 goto case_ident; | |
1354 | |
1355 if (u == PS || u == LS) | |
1356 { | |
1357 loc.linnum++; | |
1358 p++; | |
1359 continue; | |
1360 } | |
1361 } | |
1362 if (isprint(c)) | |
1363 error("unsupported char '%c'", c); | |
1364 else | |
1365 error("unsupported char 0x%02x", c); | |
1366 p++; | |
1367 continue; | |
1368 } | |
1369 } | |
1370 } | |
1371 } | |
1372 | |
1373 Token* peek(Token* ct) | |
1374 { | |
1375 Token* t; | |
1376 | |
1377 if (ct.next) | |
1378 t = ct.next; | |
1379 else | |
1380 { | |
1381 t = new Token(); | |
1382 scan(t); | |
1383 t.next = null; | |
1384 ct.next = t; | |
1385 } | |
1386 return t; | |
1387 } | |
1388 | |
1389 Token* peekPastParen(Token* tk) | |
1390 { | |
1391 //printf("peekPastParen()\n"); | |
1392 int parens = 1; | |
1393 int curlynest = 0; | |
1394 while (1) | |
1395 { | |
1396 tk = peek(tk); | |
1397 //tk.print(); | |
1398 switch (tk.value) | |
1399 { | |
1400 case TOK.TOKlparen: | |
1401 parens++; | |
1402 continue; | |
1403 | |
1404 case TOK.TOKrparen: | |
1405 --parens; | |
1406 if (parens) | |
1407 continue; | |
1408 tk = peek(tk); | |
1409 break; | |
1410 | |
1411 case TOK.TOKlcurly: | |
1412 curlynest++; | |
1413 continue; | |
1414 | |
1415 case TOK.TOKrcurly: | |
1416 if (--curlynest >= 0) | |
1417 continue; | |
1418 break; | |
1419 | |
1420 case TOK.TOKsemicolon: | |
1421 if (curlynest) | |
1422 continue; | |
1423 break; | |
1424 | |
1425 case TOK.TOKeof: | |
1426 break; | |
1427 | |
1428 default: | |
1429 continue; | |
1430 } | |
1431 return tk; | |
1432 } | |
1433 } | |
1434 | |
1435 /******************************************* | |
1436 * Parse escape sequence. | |
1437 */ | |
1438 uint escapeSequence() | |
1439 { | |
1440 uint c = *p; | |
1441 | |
1442 version (TEXTUAL_ASSEMBLY_OUT) { | |
1443 return c; | |
1444 } | |
1445 int n; | |
1446 int ndigits; | |
1447 | |
1448 switch (c) | |
1449 { | |
1450 case '\'': | |
1451 case '"': | |
1452 case '?': | |
1453 case '\\': | |
1454 Lconsume: | |
1455 p++; | |
1456 break; | |
1457 | |
1458 case 'a': c = 7; goto Lconsume; | |
1459 case 'b': c = 8; goto Lconsume; | |
1460 case 'f': c = 12; goto Lconsume; | |
1461 case 'n': c = 10; goto Lconsume; | |
1462 case 'r': c = 13; goto Lconsume; | |
1463 case 't': c = 9; goto Lconsume; | |
1464 case 'v': c = 11; goto Lconsume; | |
1465 | |
1466 case 'u': | |
1467 ndigits = 4; | |
1468 goto Lhex; | |
1469 case 'U': | |
1470 ndigits = 8; | |
1471 goto Lhex; | |
1472 case 'x': | |
1473 ndigits = 2; | |
1474 Lhex: | |
1475 p++; | |
1476 c = *p; | |
1477 if (ishex(cast(ubyte)c)) | |
1478 { | |
1479 uint v; | |
1480 | |
1481 n = 0; | |
1482 v = 0; | |
1483 while (1) | |
1484 { | |
1485 if (isdigit(c)) | |
1486 c -= '0'; | |
1487 else if (islower(c)) | |
1488 c -= 'a' - 10; | |
1489 else | |
1490 c -= 'A' - 10; | |
1491 v = v * 16 + c; | |
1492 c = *++p; | |
1493 if (++n == ndigits) | |
1494 break; | |
1495 if (!ishex(cast(ubyte)c)) | |
1496 { error("escape hex sequence has %d hex digits instead of %d", n, ndigits); | |
1497 break; | |
1498 } | |
1499 } | |
1500 if (ndigits != 2 && !utf_isValidDchar(v)) | |
1501 { error("invalid UTF character \\U%08x", v); | |
1502 v = '?'; // recover with valid UTF character | |
1503 } | |
1504 c = v; | |
1505 } | |
1506 else | |
1507 error("undefined escape hex sequence \\%c\n",c); | |
1508 break; | |
1509 | |
1510 case '&': // named character entity | |
1511 for (ubyte* idstart = ++p; true; p++) | |
1512 { | |
1513 switch (*p) | |
1514 { | |
1515 case ';': | |
1516 c = HtmlNamedEntity(idstart, p - idstart); | |
1517 if (c == ~0) | |
1518 { | |
1519 error("unnamed character entity &%s;", idstart[0..(p - idstart)]); | |
1520 c = ' '; | |
1521 } | |
1522 p++; | |
1523 break; | |
1524 | |
1525 default: | |
1526 if (isalpha(*p) || | |
1527 (p != idstart + 1 && isdigit(*p))) | |
1528 continue; | |
1529 error("unterminated named entity"); | |
1530 break; | |
1531 } | |
1532 break; | |
1533 } | |
1534 break; | |
1535 | |
1536 case 0: | |
1537 case 0x1A: // end of file | |
1538 c = '\\'; | |
1539 break; | |
1540 | |
1541 default: | |
1542 if (isoctal(cast(ubyte)c)) | |
1543 { | |
1544 uint v; | |
1545 | |
1546 n = 0; | |
1547 v = 0; | |
1548 do | |
1549 { | |
1550 v = v * 8 + (c - '0'); | |
1551 c = *++p; | |
1552 } while (++n < 3 && isoctal(cast(ubyte)c)); | |
1553 c = v; | |
1554 if (c > 0xFF) | |
1555 error("0%03o is larger than a byte", c); | |
1556 } | |
1557 else | |
1558 error("undefined escape sequence \\%c\n",c); | |
1559 break; | |
1560 } | |
1561 return c; | |
1562 } | |
1563 | |
1564 TOK wysiwygStringConstant(Token* t, int tc) | |
1565 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1566 uint c; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1567 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1568 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1569 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1570 stringbuffer.reset(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1571 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1572 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1573 c = *p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1574 switch (c) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1575 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1576 case '\n': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1577 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1578 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1579 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1580 case '\r': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1581 if (*p == '\n') |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1582 continue; // ignore |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1583 c = '\n'; // treat EndOfLine as \n character |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1584 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1585 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1586 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1587 case 0: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1588 case 0x1A: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1589 error("unterminated string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1590 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1591 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1592 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1593 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1594 |
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 case '`': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1597 if (c == tc) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1598 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1599 t.len = stringbuffer.offset; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1600 stringbuffer.writeByte(0); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1601 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1602 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1603 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1604 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1605 return TOKstring; |
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 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1608 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1609 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1610 if (c & 0x80) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1611 { p--; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1612 uint u = decodeUTF(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1613 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1614 if (u == PS || u == LS) |
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 stringbuffer.writeUTF8(u); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1617 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1618 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1619 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1620 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1621 stringbuffer.writeByte(c); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1622 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1623 |
0 | 1624 assert(false); |
1625 } | |
1626 | |
51 | 1627 /************************************** |
1628 * Lex hex strings: | |
1629 * x"0A ae 34FE BD" | |
1630 */ | |
0 | 1631 TOK hexStringConstant(Token* t) |
1632 { | |
51 | 1633 uint c; |
1634 Loc start = loc; | |
1635 uint n = 0; | |
1636 uint v; | |
1637 | |
1638 p++; | |
1639 stringbuffer.reset(); | |
1640 while (1) | |
1641 { | |
1642 c = *p++; | |
1643 switch (c) | |
1644 { | |
1645 case ' ': | |
1646 case '\t': | |
1647 case '\v': | |
1648 case '\f': | |
1649 continue; // skip white space | |
1650 | |
1651 case '\r': | |
1652 if (*p == '\n') | |
1653 continue; // ignore | |
1654 // Treat isolated '\r' as if it were a '\n' | |
1655 case '\n': | |
1656 loc.linnum++; | |
1657 continue; | |
1658 | |
1659 case 0: | |
1660 case 0x1A: | |
1661 error("unterminated string constant starting at %s", start.toChars()); | |
1662 t.ustring = "".ptr; | |
1663 t.len = 0; | |
1664 t.postfix = 0; | |
1665 return TOKstring; | |
1666 | |
1667 case '"': | |
1668 if (n & 1) | |
1669 { | |
1670 error("odd number (%d) of hex characters in hex string", n); | |
1671 stringbuffer.writeByte(v); | |
1672 } | |
1673 t.len = stringbuffer.offset; | |
1674 stringbuffer.writeByte(0); | |
1675 void* mem = malloc(stringbuffer.offset); | |
1676 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
1677 t.ustring = cast(const(char)*)mem; | |
1678 stringPostfix(t); | |
1679 return TOKstring; | |
1680 | |
1681 default: | |
1682 if (c >= '0' && c <= '9') | |
1683 c -= '0'; | |
1684 else if (c >= 'a' && c <= 'f') | |
1685 c -= 'a' - 10; | |
1686 else if (c >= 'A' && c <= 'F') | |
1687 c -= 'A' - 10; | |
1688 else if (c & 0x80) | |
1689 { p--; | |
1690 uint u = decodeUTF(); | |
1691 p++; | |
1692 if (u == PS || u == LS) | |
1693 loc.linnum++; | |
1694 else | |
1695 error("non-hex character \\u%x", u); | |
1696 } | |
1697 else | |
1698 error("non-hex character '%c'", c); | |
1699 if (n & 1) | |
1700 { v = (v << 4) | c; | |
1701 stringbuffer.writeByte(v); | |
1702 } | |
1703 else | |
1704 v = c; | |
1705 n++; | |
1706 break; | |
1707 } | |
1708 } | |
0 | 1709 } |
1710 | |
1711 version (DMDV2) { | |
51 | 1712 /************************************** |
1713 * Lex delimited strings: | |
1714 * q"(foo(xxx))" // "foo(xxx)" | |
1715 * q"[foo(]" // "foo(" | |
1716 * q"/foo]/" // "foo]" | |
1717 * q"HERE | |
1718 * foo | |
1719 * HERE" // "foo\n" | |
1720 * Input: | |
1721 * p is on the " | |
1722 */ | |
0 | 1723 TOK delimitedStringConstant(Token* t) |
1724 { | |
51 | 1725 uint c; |
1726 Loc start = loc; | |
1727 uint delimleft = 0; | |
1728 uint delimright = 0; | |
1729 uint nest = 1; | |
1730 uint nestcount; | |
1731 Identifier hereid = null; | |
1732 uint blankrol = 0; | |
1733 uint startline = 0; | |
1734 | |
1735 p++; | |
1736 stringbuffer.reset(); | |
1737 while (1) | |
1738 { | |
1739 c = *p++; | |
1740 //printf("c = '%c'\n", c); | |
1741 switch (c) | |
1742 { | |
1743 case '\n': | |
1744 Lnextline: | |
1745 loc.linnum++; | |
1746 startline = 1; | |
1747 if (blankrol) | |
1748 { blankrol = 0; | |
1749 continue; | |
1750 } | |
1751 if (hereid) | |
1752 { | |
1753 stringbuffer.writeUTF8(c); | |
1754 continue; | |
1755 } | |
1756 break; | |
1757 | |
1758 case '\r': | |
1759 if (*p == '\n') | |
1760 continue; // ignore | |
1761 c = '\n'; // treat EndOfLine as \n character | |
1762 goto Lnextline; | |
1763 | |
1764 case 0: | |
1765 case 0x1A: | |
1766 goto Lerror; | |
1767 | |
1768 default: | |
1769 if (c & 0x80) | |
1770 { p--; | |
1771 c = decodeUTF(); | |
1772 p++; | |
1773 if (c == PS || c == LS) | |
1774 goto Lnextline; | |
1775 } | |
1776 break; | |
1777 } | |
1778 if (delimleft == 0) | |
1779 { | |
1780 delimleft = c; | |
1781 nest = 1; | |
1782 nestcount = 1; | |
1783 if (c == '(') | |
1784 delimright = ')'; | |
1785 else if (c == '{') | |
1786 delimright = '}'; | |
1787 else if (c == '[') | |
1788 delimright = ']'; | |
1789 else if (c == '<') | |
1790 delimright = '>'; | |
1791 else if (isalpha(c) || c == '_' || (c >= 0x80 && isUniAlpha(c))) | |
1792 { | |
1793 // Start of identifier; must be a heredoc | |
1794 Token t2; | |
1795 p--; | |
1796 scan(&t2); // read in heredoc identifier | |
1797 if (t2.value != TOKidentifier) | |
1798 { | |
1799 error("identifier expected for heredoc, not %s", t2.toChars()); | |
1800 delimright = c; | |
1801 } | |
1802 else | |
1803 { | |
1804 hereid = t2.ident; | |
1805 //printf("hereid = '%s'\n", hereid.toChars()); | |
1806 blankrol = 1; | |
1807 } | |
1808 nest = 0; | |
1809 } | |
1810 else | |
1811 { | |
1812 delimright = c; | |
1813 nest = 0; | |
1814 if (isspace(c)) | |
1815 error("delimiter cannot be whitespace"); | |
1816 } | |
1817 } | |
1818 else | |
1819 { | |
1820 if (blankrol) | |
1821 { | |
1822 error("heredoc rest of line should be blank"); | |
1823 blankrol = 0; | |
1824 continue; | |
1825 } | |
1826 if (nest == 1) | |
1827 { | |
1828 if (c == delimleft) | |
1829 nestcount++; | |
1830 else if (c == delimright) | |
1831 { nestcount--; | |
1832 if (nestcount == 0) | |
1833 goto Ldone; | |
1834 } | |
1835 } | |
1836 else if (c == delimright) | |
1837 goto Ldone; | |
1838 if (startline && isalpha(c) && hereid) | |
1839 { | |
1840 Token t2; | |
1841 ubyte* psave = p; | |
1842 p--; | |
1843 scan(&t2); // read in possible heredoc identifier | |
1844 //printf("endid = '%s'\n", t2.ident.toChars()); | |
1845 if (t2.value == TOKidentifier && t2.ident.equals(hereid)) | |
1846 { | |
1847 /* should check that rest of line is blank | |
1848 */ | |
1849 goto Ldone; | |
1850 } | |
1851 p = psave; | |
1852 } | |
1853 stringbuffer.writeUTF8(c); | |
1854 startline = 0; | |
1855 } | |
1856 } | |
1857 | |
1858 Ldone: | |
1859 if (*p == '"') | |
1860 p++; | |
1861 else | |
1862 error("delimited string must end in %c\"", delimright); | |
1863 t.len = stringbuffer.offset; | |
1864 stringbuffer.writeByte(0); | |
1865 void* mem = malloc(stringbuffer.offset); | |
1866 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
1867 t.ustring = cast(const(char)*)mem; | |
1868 stringPostfix(t); | |
1869 return TOKstring; | |
1870 | |
1871 Lerror: | |
1872 error("unterminated string constant starting at %s", start.toChars()); | |
1873 t.ustring = "".ptr; | |
1874 t.len = 0; | |
1875 t.postfix = 0; | |
1876 return TOKstring; | |
0 | 1877 } |
1878 | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1879 /************************************** |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1880 * Lex delimited strings: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1881 * q{ foo(xxx) } // " foo(xxx) " |
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 * q{{foo}"}"} // "{foo}"}"" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1884 * Input: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1885 * p is on the q |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1886 */ |
0 | 1887 TOK tokenStringConstant(Token* t) |
1888 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1889 uint nest = 1; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1890 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1891 ubyte* pstart = ++p; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1892 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1893 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1894 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1895 Token tok; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1896 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1897 scan(&tok); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1898 switch (tok.value) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1899 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1900 case TOKlcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1901 nest++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1902 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1903 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1904 case TOKrcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1905 if (--nest == 0) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1906 goto Ldone; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1907 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1908 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1909 case TOKeof: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1910 goto Lerror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1911 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1912 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1913 continue; |
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 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1917 Ldone: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1918 t.len = p - 1 - pstart; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1919 char* tmp = cast(char*)GC.malloc(t.len + 1); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1920 memcpy(tmp, pstart, t.len); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1921 tmp[t.len] = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1922 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1923 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1924 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1925 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1926 Lerror: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1927 error("unterminated token string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1928 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1929 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1930 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1931 return TOKstring; |
0 | 1932 } |
1933 } | |
1934 TOK escapeStringConstant(Token* t, int wide) | |
1935 { | |
1936 uint c; | |
1937 Loc start = loc; | |
1938 | |
1939 p++; | |
1940 stringbuffer.reset(); | |
1941 while (true) | |
1942 { | |
1943 c = *p++; | |
1944 switch (c) | |
1945 { | |
1946 version (TEXTUAL_ASSEMBLY_OUT) { | |
1947 } else { | |
1948 case '\\': | |
1949 switch (*p) | |
1950 { | |
1951 case 'u': | |
1952 case 'U': | |
1953 case '&': | |
1954 c = escapeSequence(); | |
1955 stringbuffer.writeUTF8(c); | |
1956 continue; | |
1957 | |
1958 default: | |
1959 c = escapeSequence(); | |
1960 break; | |
1961 } | |
1962 break; | |
1963 } | |
1964 case '\n': | |
1965 loc.linnum++; | |
1966 break; | |
1967 | |
1968 case '\r': | |
1969 if (*p == '\n') | |
1970 continue; // ignore | |
1971 c = '\n'; // treat EndOfLine as \n character | |
1972 loc.linnum++; | |
1973 break; | |
1974 | |
1975 case '"': | |
1976 t.len = stringbuffer.offset; | |
1977 stringbuffer.writeByte(0); | |
2 | 1978 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 1979 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
1980 t.ustring = tmp; | |
1981 stringPostfix(t); | |
1982 return TOK.TOKstring; | |
1983 | |
1984 case 0: | |
1985 case 0x1A: | |
1986 p--; | |
1987 error("unterminated string constant starting at %s", start.toChars()); | |
1988 t.ustring = "".ptr; | |
1989 t.len = 0; | |
1990 t.postfix = 0; | |
1991 return TOK.TOKstring; | |
1992 | |
1993 default: | |
1994 if (c & 0x80) | |
1995 { | |
1996 p--; | |
1997 c = decodeUTF(); | |
1998 if (c == LS || c == PS) | |
1999 { c = '\n'; | |
2000 loc.linnum++; | |
2001 } | |
2002 p++; | |
2003 stringbuffer.writeUTF8(c); | |
2004 continue; | |
2005 } | |
2006 break; | |
2007 } | |
2008 stringbuffer.writeByte(c); | |
2009 } | |
2010 | |
2011 assert(false); | |
2012 } | |
2013 | |
2014 TOK charConstant(Token* t, int wide) | |
2015 { | |
2016 uint c; | |
2017 TOK tk = TOKcharv; | |
2018 | |
2019 //printf("Lexer.charConstant\n"); | |
2020 p++; | |
2021 c = *p++; | |
2022 switch (c) | |
2023 { | |
2024 version (TEXTUAL_ASSEMBLY_OUT) { | |
2025 } else { | |
2026 case '\\': | |
2027 switch (*p) | |
2028 { | |
2029 case 'u': | |
2030 t.uns64value = escapeSequence(); | |
2031 tk = TOKwcharv; | |
2032 break; | |
2033 | |
2034 case 'U': | |
2035 case '&': | |
2036 t.uns64value = escapeSequence(); | |
2037 tk = TOKdcharv; | |
2038 break; | |
2039 | |
2040 default: | |
2041 t.uns64value = escapeSequence(); | |
2042 break; | |
2043 } | |
2044 break; | |
2045 } | |
2046 case '\n': | |
2047 L1: | |
2048 loc.linnum++; | |
2049 case '\r': | |
2050 case 0: | |
2051 case 0x1A: | |
2052 case '\'': | |
2053 error("unterminated character constant"); | |
2054 return tk; | |
2055 | |
2056 default: | |
2057 if (c & 0x80) | |
2058 { | |
2059 p--; | |
2060 c = decodeUTF(); | |
2061 p++; | |
2062 if (c == LS || c == PS) | |
2063 goto L1; | |
2064 if (c < 0xD800 || (c >= 0xE000 && c < 0xFFFE)) | |
2065 tk = TOKwcharv; | |
2066 else | |
2067 tk = TOKdcharv; | |
2068 } | |
2069 t.uns64value = c; | |
2070 break; | |
2071 } | |
2072 | |
2073 if (*p != '\'') | |
2074 { | |
2075 error("unterminated character constant"); | |
2076 return tk; | |
2077 } | |
2078 p++; | |
2079 return tk; | |
2080 } | |
2081 | |
2082 /*************************************** | |
2083 * Get postfix of string literal. | |
2084 */ | |
2085 void stringPostfix(Token* t) | |
2086 { | |
2087 switch (*p) | |
2088 { | |
2089 case 'c': | |
2090 case 'w': | |
2091 case 'd': | |
2092 t.postfix = *p; | |
2093 p++; | |
2094 break; | |
2095 | |
2096 default: | |
2097 t.postfix = 0; | |
2098 break; | |
2099 } | |
2100 } | |
2101 | |
2102 uint wchar_(uint u) | |
2103 { | |
2104 assert(false); | |
2105 } | |
2106 | |
2107 /************************************** | |
2108 * Read in a number. | |
2109 * If it's an integer, store it in tok.TKutok.Vlong. | |
2110 * integers can be decimal, octal or hex | |
2111 * Handle the suffixes U, UL, LU, L, etc. | |
2112 * If it's double, store it in tok.TKutok.Vdouble. | |
2113 * Returns: | |
2114 * TKnum | |
2115 * TKdouble,... | |
2116 */ | |
2117 | |
2118 TOK number(Token* t) | |
2119 { | |
2120 // We use a state machine to collect numbers | |
2121 enum STATE { STATE_initial, STATE_0, STATE_decimal, STATE_octal, STATE_octale, | |
2122 STATE_hex, STATE_binary, STATE_hex0, STATE_binary0, | |
2123 STATE_hexh, STATE_error }; | |
2124 STATE state; | |
2125 | |
2126 enum FLAGS | |
2127 { | |
2128 FLAGS_undefined = 0, | |
2129 FLAGS_decimal = 1, // decimal | |
2130 FLAGS_unsigned = 2, // u or U suffix | |
2131 FLAGS_long = 4, // l or L suffix | |
2132 }; | |
2133 | |
2134 FLAGS flags = FLAGS.FLAGS_decimal; | |
2135 | |
2136 int i; | |
2137 int base; | |
2138 uint c; | |
2139 ubyte *start; | |
2140 TOK result; | |
2141 | |
2142 //printf("Lexer.number()\n"); | |
2143 state = STATE.STATE_initial; | |
2144 base = 0; | |
2145 stringbuffer.reset(); | |
2146 start = p; | |
2147 while (1) | |
2148 { | |
2149 c = *p; | |
2150 switch (state) | |
2151 { | |
2152 case STATE.STATE_initial: // opening state | |
2153 if (c == '0') | |
2154 state = STATE.STATE_0; | |
2155 else | |
2156 state = STATE.STATE_decimal; | |
2157 break; | |
2158 | |
2159 case STATE.STATE_0: | |
2160 flags = (flags & ~FLAGS.FLAGS_decimal); | |
2161 switch (c) | |
2162 { | |
2163 version (ZEROH) { | |
2164 case 'H': // 0h | |
2165 case 'h': | |
2166 goto hexh; | |
2167 } | |
2168 case 'X': | |
2169 case 'x': | |
2170 state = STATE.STATE_hex0; | |
2171 break; | |
2172 | |
2173 case '.': | |
2174 if (p[1] == '.') // .. is a separate token | |
2175 goto done; | |
2176 case 'i': | |
2177 case 'f': | |
2178 case 'F': | |
2179 goto real_; | |
2180 version (ZEROH) { | |
2181 case 'E': | |
2182 case 'e': | |
2183 goto case_hex; | |
2184 } | |
2185 case 'B': | |
2186 case 'b': | |
2187 state = STATE.STATE_binary0; | |
2188 break; | |
2189 | |
2190 case '0': case '1': case '2': case '3': | |
2191 case '4': case '5': case '6': case '7': | |
2192 state = STATE.STATE_octal; | |
2193 break; | |
2194 | |
2195 version (ZEROH) { | |
2196 case '8': case '9': case 'A': | |
2197 case 'C': case 'D': case 'F': | |
2198 case 'a': case 'c': case 'd': case 'f': | |
2199 case_hex: | |
2200 state = STATE.STATE_hexh; | |
2201 break; | |
2202 } | |
2203 case '_': | |
2204 state = STATE.STATE_octal; | |
2205 p++; | |
2206 continue; | |
2207 | |
2208 case 'L': | |
2209 if (p[1] == 'i') | |
2210 goto real_; | |
2211 goto done; | |
2212 | |
2213 default: | |
2214 goto done; | |
2215 } | |
2216 break; | |
2217 | |
2218 case STATE.STATE_decimal: // reading decimal number | |
2219 if (!isdigit(c)) | |
2220 { | |
2221 version (ZEROH) { | |
2222 if (ishex(c) | |
2223 || c == 'H' || c == 'h' | |
2224 ) | |
2225 goto hexh; | |
2226 } | |
2227 if (c == '_') // ignore embedded _ | |
2228 { p++; | |
2229 continue; | |
2230 } | |
2231 if (c == '.' && p[1] != '.') | |
2232 goto real_; | |
2233 else if (c == 'i' || c == 'f' || c == 'F' || | |
2234 c == 'e' || c == 'E') | |
2235 { | |
2236 real_: // It's a real number. Back up and rescan as a real | |
2237 p = start; | |
2238 return inreal(t); | |
2239 } | |
2240 else if (c == 'L' && p[1] == 'i') | |
2241 goto real_; | |
2242 goto done; | |
2243 } | |
2244 break; | |
2245 | |
2246 case STATE.STATE_hex0: // reading hex number | |
2247 case STATE.STATE_hex: | |
2248 if (! ishex(cast(ubyte)c)) | |
2249 { | |
2250 if (c == '_') // ignore embedded _ | |
2251 { p++; | |
2252 continue; | |
2253 } | |
2254 if (c == '.' && p[1] != '.') | |
2255 goto real_; | |
2256 if (c == 'P' || c == 'p' || c == 'i') | |
2257 goto real_; | |
2258 if (state == STATE.STATE_hex0) | |
2259 error("Hex digit expected, not '%c'", c); | |
2260 goto done; | |
2261 } | |
2262 state = STATE.STATE_hex; | |
2263 break; | |
2264 | |
2265 version (ZEROH) { | |
2266 hexh: | |
2267 state = STATE.STATE_hexh; | |
2268 case STATE.STATE_hexh: // parse numbers like 0FFh | |
2269 if (!ishex(c)) | |
2270 { | |
2271 if (c == 'H' || c == 'h') | |
2272 { | |
2273 p++; | |
2274 base = 16; | |
2275 goto done; | |
2276 } | |
2277 else | |
2278 { | |
2279 // Check for something like 1E3 or 0E24 | |
2280 if (memchr(cast(char*)stringbuffer.data, 'E', stringbuffer.offset) || | |
2281 memchr(cast(char*)stringbuffer.data, 'e', stringbuffer.offset)) | |
2282 goto real_; | |
2283 error("Hex digit expected, not '%c'", c); | |
2284 goto done; | |
2285 } | |
2286 } | |
2287 break; | |
2288 } | |
2289 | |
2290 case STATE.STATE_octal: // reading octal number | |
2291 case STATE.STATE_octale: // reading octal number with non-octal digits | |
2292 if (!isoctal(cast(ubyte)c)) | |
2293 { | |
2294 version (ZEROH) { | |
2295 if (ishex(c) | |
2296 || c == 'H' || c == 'h' | |
2297 ) | |
2298 goto hexh; | |
2299 } | |
2300 if (c == '_') // ignore embedded _ | |
2301 { p++; | |
2302 continue; | |
2303 } | |
2304 if (c == '.' && p[1] != '.') | |
2305 goto real_; | |
2306 if (c == 'i') | |
2307 goto real_; | |
2308 if (isdigit(c)) | |
2309 { | |
2310 state = STATE.STATE_octale; | |
2311 } | |
2312 else | |
2313 goto done; | |
2314 } | |
2315 break; | |
2316 | |
2317 case STATE.STATE_binary0: // starting binary number | |
2318 case STATE.STATE_binary: // reading binary number | |
2319 if (c != '0' && c != '1') | |
2320 { | |
2321 version (ZEROH) { | |
2322 if (ishex(c) | |
2323 || c == 'H' || c == 'h' | |
2324 ) | |
2325 goto hexh; | |
2326 } | |
2327 if (c == '_') // ignore embedded _ | |
2328 { p++; | |
2329 continue; | |
2330 } | |
2331 if (state == STATE.STATE_binary0) | |
2332 { error("binary digit expected"); | |
2333 state = STATE.STATE_error; | |
2334 break; | |
2335 } | |
2336 else | |
2337 goto done; | |
2338 } | |
2339 state = STATE.STATE_binary; | |
2340 break; | |
2341 | |
2342 case STATE.STATE_error: // for error recovery | |
2343 if (!isdigit(c)) // scan until non-digit | |
2344 goto done; | |
2345 break; | |
2346 | |
2347 default: | |
2348 assert(0); | |
2349 } | |
2350 stringbuffer.writeByte(c); | |
2351 p++; | |
2352 } | |
2353 done: | |
2354 stringbuffer.writeByte(0); // terminate string | |
2355 if (state == STATE.STATE_octale) | |
2356 error("Octal digit expected"); | |
2357 | |
2358 ulong n; // unsigned >=64 bit integer type | |
2359 | |
2360 if (stringbuffer.offset == 2 && (state == STATE.STATE_decimal || state == STATE.STATE_0)) | |
2361 n = stringbuffer.data[0] - '0'; | |
2362 else | |
2363 { | |
2364 // Convert string to integer | |
2365 version (__DMC__) { | |
2366 errno = 0; | |
2367 n = strtoull(cast(char*)stringbuffer.data,null,base); | |
2368 if (errno == ERANGE) | |
2369 error("integer overflow"); | |
2370 } else { | |
2371 // Not everybody implements strtoull() | |
2372 char* p = cast(char*)stringbuffer.data; | |
2373 int r = 10, d; | |
2374 | |
2375 if (*p == '0') | |
2376 { | |
2377 if (p[1] == 'x' || p[1] == 'X') | |
2378 p += 2, r = 16; | |
2379 else if (p[1] == 'b' || p[1] == 'B') | |
2380 p += 2, r = 2; | |
2381 else if (isdigit(p[1])) | |
2382 p += 1, r = 8; | |
2383 } | |
2384 | |
2385 n = 0; | |
2386 while (1) | |
2387 { | |
2388 if (*p >= '0' && *p <= '9') | |
2389 d = *p - '0'; | |
2390 else if (*p >= 'a' && *p <= 'z') | |
2391 d = *p - 'a' + 10; | |
2392 else if (*p >= 'A' && *p <= 'Z') | |
2393 d = *p - 'A' + 10; | |
2394 else | |
2395 break; | |
2396 if (d >= r) | |
2397 break; | |
2398 ulong n2 = n * r; | |
2399 //printf("n2 / r = %llx, n = %llx\n", n2/r, n); | |
2400 if (n2 / r != n || n2 + d < n) | |
2401 { | |
2402 error ("integer overflow"); | |
2403 break; | |
2404 } | |
2405 | |
2406 n = n2 + d; | |
2407 p++; | |
2408 } | |
2409 } | |
2410 if (n.sizeof > 8 && | |
2411 n > 0xFFFFFFFFFFFFFFFF) // if n needs more than 64 bits | |
2412 error("integer overflow"); | |
2413 } | |
2414 | |
2415 // Parse trailing 'u', 'U', 'l' or 'L' in any combination | |
2416 while (1) | |
2417 { FLAGS f; | |
2418 | |
2419 switch (*p) | |
2420 { case 'U': | |
2421 case 'u': | |
2422 f = FLAGS.FLAGS_unsigned; | |
2423 goto L1; | |
2424 | |
2425 case 'l': | |
2426 if (1 || !global.params.useDeprecated) | |
2427 error("'l' suffix is deprecated, use 'L' instead"); | |
2428 case 'L': | |
2429 f = FLAGS.FLAGS_long; | |
2430 L1: | |
2431 p++; | |
2432 if (flags & f) | |
2433 error("unrecognized token"); | |
2434 flags = (flags | f); | |
2435 continue; | |
2436 default: | |
2437 break; | |
2438 } | |
2439 break; | |
2440 } | |
2441 | |
2442 switch (flags) | |
2443 { | |
2444 case FLAGS.FLAGS_undefined: | |
2445 /* Octal or Hexadecimal constant. | |
2446 * First that fits: int, uint, long, ulong | |
2447 */ | |
2448 if (n & 0x8000000000000000) | |
2449 result = TOK.TOKuns64v; | |
2450 else if (n & 0xFFFFFFFF00000000) | |
2451 result = TOK.TOKint64v; | |
2452 else if (n & 0x80000000) | |
2453 result = TOK.TOKuns32v; | |
2454 else | |
2455 result = TOK.TOKint32v; | |
2456 break; | |
2457 | |
2458 case FLAGS.FLAGS_decimal: | |
2459 /* First that fits: int, long, long long | |
2460 */ | |
2461 if (n & 0x8000000000000000) | |
2462 { error("signed integer overflow"); | |
2463 result = TOK.TOKuns64v; | |
2464 } | |
2465 else if (n & 0xFFFFFFFF80000000) | |
2466 result = TOK.TOKint64v; | |
2467 else | |
2468 result = TOK.TOKint32v; | |
2469 break; | |
2470 | |
2471 case FLAGS.FLAGS_unsigned: | |
2472 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned: | |
2473 /* First that fits: uint, ulong | |
2474 */ | |
2475 if (n & 0xFFFFFFFF00000000) | |
2476 result = TOK.TOKuns64v; | |
2477 else | |
2478 result = TOK.TOKuns32v; | |
2479 break; | |
2480 | |
2481 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_long: | |
2482 if (n & 0x8000000000000000) | |
2483 { error("signed integer overflow"); | |
2484 result = TOK.TOKuns64v; | |
2485 } | |
2486 else | |
2487 result = TOK.TOKint64v; | |
2488 break; | |
2489 | |
2490 case FLAGS.FLAGS_long: | |
2491 if (n & 0x8000000000000000) | |
2492 result = TOK.TOKuns64v; | |
2493 else | |
2494 result = TOK.TOKint64v; | |
2495 break; | |
2496 | |
2497 case FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2498 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2499 result = TOK.TOKuns64v; | |
2500 break; | |
2501 | |
2502 default: | |
2503 debug { | |
2504 printf("%x\n",flags); | |
2505 } | |
2506 assert(0); | |
2507 } | |
2508 t.uns64value = n; | |
2509 return result; | |
2510 } | |
2511 | |
2512 /************************************** | |
2513 * Read in characters, converting them to real. | |
2514 * Bugs: | |
2515 * Exponent overflow not detected. | |
2516 * Too much requested precision is not detected. | |
2517 */ | |
2518 TOK inreal(Token* t) | |
2519 in | |
2520 { | |
2521 assert(*p == '.' || isdigit(*p)); | |
2522 } | |
2523 out (result) | |
2524 { | |
2525 switch (result) | |
2526 { | |
2527 case TOKfloat32v: | |
2528 case TOKfloat64v: | |
2529 case TOKfloat80v: | |
2530 case TOKimaginary32v: | |
2531 case TOKimaginary64v: | |
2532 case TOKimaginary80v: | |
2533 break; | |
2534 | |
2535 default: | |
2536 assert(0); | |
2537 } | |
2538 } | |
2539 body | |
2540 { | |
2541 int dblstate; | |
2542 uint c; | |
2543 char hex; // is this a hexadecimal-floating-constant? | |
2544 TOK result; | |
2545 | |
2546 //printf("Lexer.inreal()\n"); | |
2547 stringbuffer.reset(); | |
2548 dblstate = 0; | |
2549 hex = 0; | |
2550 Lnext: | |
2551 while (true) | |
2552 { | |
2553 // Get next char from input | |
2554 c = *p++; | |
2555 //printf("dblstate = %d, c = '%c'\n", dblstate, c); | |
2556 while (true) | |
2557 { | |
2558 switch (dblstate) | |
2559 { | |
2560 case 0: // opening state | |
2561 if (c == '0') | |
2562 dblstate = 9; | |
2563 else if (c == '.') | |
2564 dblstate = 3; | |
2565 else | |
2566 dblstate = 1; | |
2567 break; | |
2568 | |
2569 case 9: | |
2570 dblstate = 1; | |
2571 if (c == 'X' || c == 'x') | |
2572 { | |
2573 hex++; | |
2574 break; | |
2575 } | |
2576 case 1: // digits to left of . | |
2577 case 3: // digits to right of . | |
2578 case 7: // continuing exponent digits | |
2579 if (!isdigit(c) && !(hex && isxdigit(c))) | |
2580 { | |
2581 if (c == '_') | |
2582 goto Lnext; // ignore embedded '_' | |
2583 dblstate++; | |
2584 continue; | |
2585 } | |
2586 break; | |
2587 | |
2588 case 2: // no more digits to left of . | |
2589 if (c == '.') | |
2590 { | |
2591 dblstate++; | |
2592 break; | |
2593 } | |
2594 case 4: // no more digits to right of . | |
2595 if ((c == 'E' || c == 'e') || | |
2596 hex && (c == 'P' || c == 'p')) | |
2597 { | |
2598 dblstate = 5; | |
2599 hex = 0; // exponent is always decimal | |
2600 break; | |
2601 } | |
2602 if (hex) | |
2603 error("binary-exponent-part required"); | |
2604 goto done; | |
2605 | |
2606 case 5: // looking immediately to right of E | |
2607 dblstate++; | |
2608 if (c == '-' || c == '+') | |
2609 break; | |
2610 case 6: // 1st exponent digit expected | |
2611 if (!isdigit(c)) | |
2612 error("exponent expected"); | |
2613 dblstate++; | |
2614 break; | |
2615 | |
2616 case 8: // past end of exponent digits | |
2617 goto done; | |
79 | 2618 |
2619 default: | |
2620 assert(0, "inreal.dblstate has unexpected value"); | |
0 | 2621 } |
2622 break; | |
2623 } | |
2624 stringbuffer.writeByte(c); | |
2625 } | |
2626 done: | |
2627 p--; | |
2628 | |
2629 stringbuffer.writeByte(0); | |
2630 | |
114 | 2631 version (Windows) { /// && __DMC__ |
0 | 2632 char* save = __locale_decpoint; |
2633 __locale_decpoint = cast(char*)".".ptr; | |
2634 } | |
2635 t.float80value = strtold(cast(char*)stringbuffer.data, null); | |
2636 | |
2637 errno = 0; | |
2638 switch (*p) | |
2639 { | |
2640 case 'F': | |
2641 case 'f': | |
2642 strtof(cast(char*)stringbuffer.data, null); | |
2643 result = TOKfloat32v; | |
2644 p++; | |
2645 break; | |
2646 | |
2647 default: | |
2648 strtod(cast(char*)stringbuffer.data, null); | |
2649 result = TOKfloat64v; | |
2650 break; | |
2651 | |
2652 case 'l': | |
2653 if (!global.params.useDeprecated) | |
2654 error("'l' suffix is deprecated, use 'L' instead"); | |
2655 case 'L': | |
2656 result = TOKfloat80v; | |
2657 p++; | |
2658 break; | |
2659 } | |
2660 if (*p == 'i' || *p == 'I') | |
2661 { | |
2662 if (!global.params.useDeprecated && *p == 'I') | |
2663 error("'I' suffix is deprecated, use 'i' instead"); | |
2664 p++; | |
2665 switch (result) | |
2666 { | |
2667 case TOKfloat32v: | |
2668 result = TOKimaginary32v; | |
2669 break; | |
2670 case TOKfloat64v: | |
2671 result = TOKimaginary64v; | |
2672 break; | |
2673 case TOKfloat80v: | |
2674 result = TOKimaginary80v; | |
2675 break; | |
2676 } | |
2677 } | |
2678 | |
114 | 2679 version (Windows) { ///&& __DMC__ |
0 | 2680 __locale_decpoint = save; |
2681 } | |
2682 if (errno == ERANGE) | |
2683 error("number is not representable"); | |
2684 | |
2685 return result; | |
2686 } | |
2687 | |
2688 void error(T...)(string format, T t) | |
2689 { | |
2690 error(this.loc, format, t); | |
2691 } | |
2692 | |
2693 void error(T...)(Loc loc, string format, T t) | |
2694 { | |
2695 if (mod && !global.gag) | |
2696 { | |
2697 string p = loc.toChars(); | |
2698 if (p.length != 0) | |
2699 writef("%s: ", p); | |
2700 | |
2701 writefln(format, t); | |
2702 | |
2703 if (global.errors >= 20) // moderate blizzard of cascading messages | |
2704 fatal(); | |
2705 } | |
2706 | |
2707 global.errors++; | |
2708 } | |
2709 | |
2710 void pragma_() | |
2711 { | |
2712 assert(false); | |
2713 } | |
2714 | |
49 | 2715 /******************************************** |
2716 * Decode UTF character. | |
2717 * Issue error messages for invalid sequences. | |
2718 * Return decoded character, advance p to last character in UTF sequence. | |
2719 */ | |
0 | 2720 uint decodeUTF() |
2721 { | |
49 | 2722 dchar u; |
2723 ubyte c; | |
2724 ubyte* s = p; | |
2725 size_t len; | |
2726 size_t idx; | |
2727 string msg; | |
2728 | |
2729 c = *s; | |
2730 assert(c & 0x80); | |
2731 | |
2732 // Check length of remaining string up to 6 UTF-8 characters | |
2733 for (len = 1; len < 6 && s[len]; len++) { | |
2734 ; | |
2735 } | |
2736 | |
2737 idx = 0; | |
2738 msg = utf_decodeChar(cast(string)s[0..len], &idx, &u); | |
2739 p += idx - 1; | |
2740 if (msg) | |
2741 { | |
2742 error("%s", msg); | |
2743 } | |
2744 return u; | |
0 | 2745 } |
2746 | |
2747 void getDocComment(Token* t, uint lineComment) | |
2748 { | |
2749 assert(false); | |
2750 } | |
2751 | |
2752 static bool isValidIdentifier(string p) | |
2753 { | |
2754 if (p.length == 0) { | |
2755 return false; | |
2756 } | |
2757 | |
2758 if (p[0] >= '0' && p[0] <= '9') { // beware of isdigit() on signed chars | |
2759 return false; | |
2760 } | |
2761 | |
2762 size_t idx = 0; | |
2763 while (idx < p.length) | |
2764 { | |
2765 dchar dc; | |
2766 | |
2767 if (utf_decodeChar(p, &idx, &dc) !is null) { | |
2768 return false; | |
2769 } | |
2770 | |
2771 if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_')) { | |
2772 return false; | |
2773 } | |
2774 } | |
2775 | |
2776 return true; | |
2777 } | |
2778 | |
79 | 2779 /// TODO: use normal string append when GC works |
2780 static string combineComments(const(char)[] c1, const(char)[] c2) | |
0 | 2781 { |
79 | 2782 //writef("Lexer.combineComments('%s', '%s')\n", c1, c2); |
0 | 2783 |
79 | 2784 char[] c = cast(char[]) c2; |
2785 | |
2786 if (c1 !is null) | |
0 | 2787 { |
79 | 2788 c = cast(char[]) c1; |
2789 if (c2 !is null) | |
0 | 2790 { |
79 | 2791 c = cast(char[]) (GC.malloc(c1.length + 1 + c2.length)[0 .. c1.length + 1 + c2.length]); |
2792 size_t len1 = c1.length; | |
2793 c[0..len1] = c1[]; | |
2794 c[len1++] = '\n'; | |
2795 c[len1 .. len1 + c2.length] = c2[]; | |
0 | 2796 } |
2797 } | |
79 | 2798 |
2799 return cast(string)c; | |
0 | 2800 } |
2801 } |