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