Mercurial > projects > ddmd
annotate dmd/Lexer.d @ 34:544b922227c7
update to work with dmd 2.048
author | korDen |
---|---|
date | Sat, 21 Aug 2010 05:46:08 +0400 |
parents | d42cd5917df4 |
children | 0aa7d1437ada |
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 | |
1625 TOK hexStringConstant(Token* t) | |
1626 { | |
1627 assert(false); | |
1628 } | |
1629 | |
1630 version (DMDV2) { | |
1631 TOK delimitedStringConstant(Token* t) | |
1632 { | |
1633 assert(false); | |
1634 } | |
1635 | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1636 /************************************** |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1637 * Lex delimited strings: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1638 * q{ foo(xxx) } // " foo(xxx) " |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1639 * q{foo(} // "foo(" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1640 * q{{foo}"}"} // "{foo}"}"" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1641 * Input: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1642 * p is on the q |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1643 */ |
0 | 1644 TOK tokenStringConstant(Token* t) |
1645 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1646 uint nest = 1; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1647 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1648 ubyte* pstart = ++p; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1649 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1650 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1651 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1652 Token tok; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1653 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1654 scan(&tok); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1655 switch (tok.value) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1656 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1657 case TOKlcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1658 nest++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1659 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1660 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1661 case TOKrcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1662 if (--nest == 0) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1663 goto Ldone; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1664 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1665 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1666 case TOKeof: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1667 goto Lerror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1668 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1669 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1670 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1671 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1672 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1673 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1674 Ldone: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1675 t.len = p - 1 - pstart; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1676 char* tmp = cast(char*)GC.malloc(t.len + 1); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1677 memcpy(tmp, pstart, t.len); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1678 tmp[t.len] = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1679 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1680 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1681 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1682 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1683 Lerror: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1684 error("unterminated token string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1685 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1686 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1687 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1688 return TOKstring; |
0 | 1689 } |
1690 } | |
1691 TOK escapeStringConstant(Token* t, int wide) | |
1692 { | |
1693 uint c; | |
1694 Loc start = loc; | |
1695 | |
1696 p++; | |
1697 stringbuffer.reset(); | |
1698 while (true) | |
1699 { | |
1700 c = *p++; | |
1701 switch (c) | |
1702 { | |
1703 version (TEXTUAL_ASSEMBLY_OUT) { | |
1704 } else { | |
1705 case '\\': | |
1706 switch (*p) | |
1707 { | |
1708 case 'u': | |
1709 case 'U': | |
1710 case '&': | |
1711 c = escapeSequence(); | |
1712 stringbuffer.writeUTF8(c); | |
1713 continue; | |
1714 | |
1715 default: | |
1716 c = escapeSequence(); | |
1717 break; | |
1718 } | |
1719 break; | |
1720 } | |
1721 case '\n': | |
1722 loc.linnum++; | |
1723 break; | |
1724 | |
1725 case '\r': | |
1726 if (*p == '\n') | |
1727 continue; // ignore | |
1728 c = '\n'; // treat EndOfLine as \n character | |
1729 loc.linnum++; | |
1730 break; | |
1731 | |
1732 case '"': | |
1733 t.len = stringbuffer.offset; | |
1734 stringbuffer.writeByte(0); | |
2 | 1735 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 1736 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
1737 t.ustring = tmp; | |
1738 stringPostfix(t); | |
1739 return TOK.TOKstring; | |
1740 | |
1741 case 0: | |
1742 case 0x1A: | |
1743 p--; | |
1744 error("unterminated string constant starting at %s", start.toChars()); | |
1745 t.ustring = "".ptr; | |
1746 t.len = 0; | |
1747 t.postfix = 0; | |
1748 return TOK.TOKstring; | |
1749 | |
1750 default: | |
1751 if (c & 0x80) | |
1752 { | |
1753 p--; | |
1754 c = decodeUTF(); | |
1755 if (c == LS || c == PS) | |
1756 { c = '\n'; | |
1757 loc.linnum++; | |
1758 } | |
1759 p++; | |
1760 stringbuffer.writeUTF8(c); | |
1761 continue; | |
1762 } | |
1763 break; | |
1764 } | |
1765 stringbuffer.writeByte(c); | |
1766 } | |
1767 | |
1768 assert(false); | |
1769 } | |
1770 | |
1771 TOK charConstant(Token* t, int wide) | |
1772 { | |
1773 uint c; | |
1774 TOK tk = TOKcharv; | |
1775 | |
1776 //printf("Lexer.charConstant\n"); | |
1777 p++; | |
1778 c = *p++; | |
1779 switch (c) | |
1780 { | |
1781 version (TEXTUAL_ASSEMBLY_OUT) { | |
1782 } else { | |
1783 case '\\': | |
1784 switch (*p) | |
1785 { | |
1786 case 'u': | |
1787 t.uns64value = escapeSequence(); | |
1788 tk = TOKwcharv; | |
1789 break; | |
1790 | |
1791 case 'U': | |
1792 case '&': | |
1793 t.uns64value = escapeSequence(); | |
1794 tk = TOKdcharv; | |
1795 break; | |
1796 | |
1797 default: | |
1798 t.uns64value = escapeSequence(); | |
1799 break; | |
1800 } | |
1801 break; | |
1802 } | |
1803 case '\n': | |
1804 L1: | |
1805 loc.linnum++; | |
1806 case '\r': | |
1807 case 0: | |
1808 case 0x1A: | |
1809 case '\'': | |
1810 error("unterminated character constant"); | |
1811 return tk; | |
1812 | |
1813 default: | |
1814 if (c & 0x80) | |
1815 { | |
1816 p--; | |
1817 c = decodeUTF(); | |
1818 p++; | |
1819 if (c == LS || c == PS) | |
1820 goto L1; | |
1821 if (c < 0xD800 || (c >= 0xE000 && c < 0xFFFE)) | |
1822 tk = TOKwcharv; | |
1823 else | |
1824 tk = TOKdcharv; | |
1825 } | |
1826 t.uns64value = c; | |
1827 break; | |
1828 } | |
1829 | |
1830 if (*p != '\'') | |
1831 { | |
1832 error("unterminated character constant"); | |
1833 return tk; | |
1834 } | |
1835 p++; | |
1836 return tk; | |
1837 } | |
1838 | |
1839 /*************************************** | |
1840 * Get postfix of string literal. | |
1841 */ | |
1842 void stringPostfix(Token* t) | |
1843 { | |
1844 switch (*p) | |
1845 { | |
1846 case 'c': | |
1847 case 'w': | |
1848 case 'd': | |
1849 t.postfix = *p; | |
1850 p++; | |
1851 break; | |
1852 | |
1853 default: | |
1854 t.postfix = 0; | |
1855 break; | |
1856 } | |
1857 } | |
1858 | |
1859 uint wchar_(uint u) | |
1860 { | |
1861 assert(false); | |
1862 } | |
1863 | |
1864 /************************************** | |
1865 * Read in a number. | |
1866 * If it's an integer, store it in tok.TKutok.Vlong. | |
1867 * integers can be decimal, octal or hex | |
1868 * Handle the suffixes U, UL, LU, L, etc. | |
1869 * If it's double, store it in tok.TKutok.Vdouble. | |
1870 * Returns: | |
1871 * TKnum | |
1872 * TKdouble,... | |
1873 */ | |
1874 | |
1875 TOK number(Token* t) | |
1876 { | |
1877 // We use a state machine to collect numbers | |
1878 enum STATE { STATE_initial, STATE_0, STATE_decimal, STATE_octal, STATE_octale, | |
1879 STATE_hex, STATE_binary, STATE_hex0, STATE_binary0, | |
1880 STATE_hexh, STATE_error }; | |
1881 STATE state; | |
1882 | |
1883 enum FLAGS | |
1884 { | |
1885 FLAGS_undefined = 0, | |
1886 FLAGS_decimal = 1, // decimal | |
1887 FLAGS_unsigned = 2, // u or U suffix | |
1888 FLAGS_long = 4, // l or L suffix | |
1889 }; | |
1890 | |
1891 FLAGS flags = FLAGS.FLAGS_decimal; | |
1892 | |
1893 int i; | |
1894 int base; | |
1895 uint c; | |
1896 ubyte *start; | |
1897 TOK result; | |
1898 | |
1899 //printf("Lexer.number()\n"); | |
1900 state = STATE.STATE_initial; | |
1901 base = 0; | |
1902 stringbuffer.reset(); | |
1903 start = p; | |
1904 while (1) | |
1905 { | |
1906 c = *p; | |
1907 switch (state) | |
1908 { | |
1909 case STATE.STATE_initial: // opening state | |
1910 if (c == '0') | |
1911 state = STATE.STATE_0; | |
1912 else | |
1913 state = STATE.STATE_decimal; | |
1914 break; | |
1915 | |
1916 case STATE.STATE_0: | |
1917 flags = (flags & ~FLAGS.FLAGS_decimal); | |
1918 switch (c) | |
1919 { | |
1920 version (ZEROH) { | |
1921 case 'H': // 0h | |
1922 case 'h': | |
1923 goto hexh; | |
1924 } | |
1925 case 'X': | |
1926 case 'x': | |
1927 state = STATE.STATE_hex0; | |
1928 break; | |
1929 | |
1930 case '.': | |
1931 if (p[1] == '.') // .. is a separate token | |
1932 goto done; | |
1933 case 'i': | |
1934 case 'f': | |
1935 case 'F': | |
1936 goto real_; | |
1937 version (ZEROH) { | |
1938 case 'E': | |
1939 case 'e': | |
1940 goto case_hex; | |
1941 } | |
1942 case 'B': | |
1943 case 'b': | |
1944 state = STATE.STATE_binary0; | |
1945 break; | |
1946 | |
1947 case '0': case '1': case '2': case '3': | |
1948 case '4': case '5': case '6': case '7': | |
1949 state = STATE.STATE_octal; | |
1950 break; | |
1951 | |
1952 version (ZEROH) { | |
1953 case '8': case '9': case 'A': | |
1954 case 'C': case 'D': case 'F': | |
1955 case 'a': case 'c': case 'd': case 'f': | |
1956 case_hex: | |
1957 state = STATE.STATE_hexh; | |
1958 break; | |
1959 } | |
1960 case '_': | |
1961 state = STATE.STATE_octal; | |
1962 p++; | |
1963 continue; | |
1964 | |
1965 case 'L': | |
1966 if (p[1] == 'i') | |
1967 goto real_; | |
1968 goto done; | |
1969 | |
1970 default: | |
1971 goto done; | |
1972 } | |
1973 break; | |
1974 | |
1975 case STATE.STATE_decimal: // reading decimal number | |
1976 if (!isdigit(c)) | |
1977 { | |
1978 version (ZEROH) { | |
1979 if (ishex(c) | |
1980 || c == 'H' || c == 'h' | |
1981 ) | |
1982 goto hexh; | |
1983 } | |
1984 if (c == '_') // ignore embedded _ | |
1985 { p++; | |
1986 continue; | |
1987 } | |
1988 if (c == '.' && p[1] != '.') | |
1989 goto real_; | |
1990 else if (c == 'i' || c == 'f' || c == 'F' || | |
1991 c == 'e' || c == 'E') | |
1992 { | |
1993 real_: // It's a real number. Back up and rescan as a real | |
1994 p = start; | |
1995 return inreal(t); | |
1996 } | |
1997 else if (c == 'L' && p[1] == 'i') | |
1998 goto real_; | |
1999 goto done; | |
2000 } | |
2001 break; | |
2002 | |
2003 case STATE.STATE_hex0: // reading hex number | |
2004 case STATE.STATE_hex: | |
2005 if (! ishex(cast(ubyte)c)) | |
2006 { | |
2007 if (c == '_') // ignore embedded _ | |
2008 { p++; | |
2009 continue; | |
2010 } | |
2011 if (c == '.' && p[1] != '.') | |
2012 goto real_; | |
2013 if (c == 'P' || c == 'p' || c == 'i') | |
2014 goto real_; | |
2015 if (state == STATE.STATE_hex0) | |
2016 error("Hex digit expected, not '%c'", c); | |
2017 goto done; | |
2018 } | |
2019 state = STATE.STATE_hex; | |
2020 break; | |
2021 | |
2022 version (ZEROH) { | |
2023 hexh: | |
2024 state = STATE.STATE_hexh; | |
2025 case STATE.STATE_hexh: // parse numbers like 0FFh | |
2026 if (!ishex(c)) | |
2027 { | |
2028 if (c == 'H' || c == 'h') | |
2029 { | |
2030 p++; | |
2031 base = 16; | |
2032 goto done; | |
2033 } | |
2034 else | |
2035 { | |
2036 // Check for something like 1E3 or 0E24 | |
2037 if (memchr(cast(char*)stringbuffer.data, 'E', stringbuffer.offset) || | |
2038 memchr(cast(char*)stringbuffer.data, 'e', stringbuffer.offset)) | |
2039 goto real_; | |
2040 error("Hex digit expected, not '%c'", c); | |
2041 goto done; | |
2042 } | |
2043 } | |
2044 break; | |
2045 } | |
2046 | |
2047 case STATE.STATE_octal: // reading octal number | |
2048 case STATE.STATE_octale: // reading octal number with non-octal digits | |
2049 if (!isoctal(cast(ubyte)c)) | |
2050 { | |
2051 version (ZEROH) { | |
2052 if (ishex(c) | |
2053 || c == 'H' || c == 'h' | |
2054 ) | |
2055 goto hexh; | |
2056 } | |
2057 if (c == '_') // ignore embedded _ | |
2058 { p++; | |
2059 continue; | |
2060 } | |
2061 if (c == '.' && p[1] != '.') | |
2062 goto real_; | |
2063 if (c == 'i') | |
2064 goto real_; | |
2065 if (isdigit(c)) | |
2066 { | |
2067 state = STATE.STATE_octale; | |
2068 } | |
2069 else | |
2070 goto done; | |
2071 } | |
2072 break; | |
2073 | |
2074 case STATE.STATE_binary0: // starting binary number | |
2075 case STATE.STATE_binary: // reading binary number | |
2076 if (c != '0' && c != '1') | |
2077 { | |
2078 version (ZEROH) { | |
2079 if (ishex(c) | |
2080 || c == 'H' || c == 'h' | |
2081 ) | |
2082 goto hexh; | |
2083 } | |
2084 if (c == '_') // ignore embedded _ | |
2085 { p++; | |
2086 continue; | |
2087 } | |
2088 if (state == STATE.STATE_binary0) | |
2089 { error("binary digit expected"); | |
2090 state = STATE.STATE_error; | |
2091 break; | |
2092 } | |
2093 else | |
2094 goto done; | |
2095 } | |
2096 state = STATE.STATE_binary; | |
2097 break; | |
2098 | |
2099 case STATE.STATE_error: // for error recovery | |
2100 if (!isdigit(c)) // scan until non-digit | |
2101 goto done; | |
2102 break; | |
2103 | |
2104 default: | |
2105 assert(0); | |
2106 } | |
2107 stringbuffer.writeByte(c); | |
2108 p++; | |
2109 } | |
2110 done: | |
2111 stringbuffer.writeByte(0); // terminate string | |
2112 if (state == STATE.STATE_octale) | |
2113 error("Octal digit expected"); | |
2114 | |
2115 ulong n; // unsigned >=64 bit integer type | |
2116 | |
2117 if (stringbuffer.offset == 2 && (state == STATE.STATE_decimal || state == STATE.STATE_0)) | |
2118 n = stringbuffer.data[0] - '0'; | |
2119 else | |
2120 { | |
2121 // Convert string to integer | |
2122 version (__DMC__) { | |
2123 errno = 0; | |
2124 n = strtoull(cast(char*)stringbuffer.data,null,base); | |
2125 if (errno == ERANGE) | |
2126 error("integer overflow"); | |
2127 } else { | |
2128 // Not everybody implements strtoull() | |
2129 char* p = cast(char*)stringbuffer.data; | |
2130 int r = 10, d; | |
2131 | |
2132 if (*p == '0') | |
2133 { | |
2134 if (p[1] == 'x' || p[1] == 'X') | |
2135 p += 2, r = 16; | |
2136 else if (p[1] == 'b' || p[1] == 'B') | |
2137 p += 2, r = 2; | |
2138 else if (isdigit(p[1])) | |
2139 p += 1, r = 8; | |
2140 } | |
2141 | |
2142 n = 0; | |
2143 while (1) | |
2144 { | |
2145 if (*p >= '0' && *p <= '9') | |
2146 d = *p - '0'; | |
2147 else if (*p >= 'a' && *p <= 'z') | |
2148 d = *p - 'a' + 10; | |
2149 else if (*p >= 'A' && *p <= 'Z') | |
2150 d = *p - 'A' + 10; | |
2151 else | |
2152 break; | |
2153 if (d >= r) | |
2154 break; | |
2155 ulong n2 = n * r; | |
2156 //printf("n2 / r = %llx, n = %llx\n", n2/r, n); | |
2157 if (n2 / r != n || n2 + d < n) | |
2158 { | |
2159 error ("integer overflow"); | |
2160 break; | |
2161 } | |
2162 | |
2163 n = n2 + d; | |
2164 p++; | |
2165 } | |
2166 } | |
2167 if (n.sizeof > 8 && | |
2168 n > 0xFFFFFFFFFFFFFFFF) // if n needs more than 64 bits | |
2169 error("integer overflow"); | |
2170 } | |
2171 | |
2172 // Parse trailing 'u', 'U', 'l' or 'L' in any combination | |
2173 while (1) | |
2174 { FLAGS f; | |
2175 | |
2176 switch (*p) | |
2177 { case 'U': | |
2178 case 'u': | |
2179 f = FLAGS.FLAGS_unsigned; | |
2180 goto L1; | |
2181 | |
2182 case 'l': | |
2183 if (1 || !global.params.useDeprecated) | |
2184 error("'l' suffix is deprecated, use 'L' instead"); | |
2185 case 'L': | |
2186 f = FLAGS.FLAGS_long; | |
2187 L1: | |
2188 p++; | |
2189 if (flags & f) | |
2190 error("unrecognized token"); | |
2191 flags = (flags | f); | |
2192 continue; | |
2193 default: | |
2194 break; | |
2195 } | |
2196 break; | |
2197 } | |
2198 | |
2199 switch (flags) | |
2200 { | |
2201 case FLAGS.FLAGS_undefined: | |
2202 /* Octal or Hexadecimal constant. | |
2203 * First that fits: int, uint, long, ulong | |
2204 */ | |
2205 if (n & 0x8000000000000000) | |
2206 result = TOK.TOKuns64v; | |
2207 else if (n & 0xFFFFFFFF00000000) | |
2208 result = TOK.TOKint64v; | |
2209 else if (n & 0x80000000) | |
2210 result = TOK.TOKuns32v; | |
2211 else | |
2212 result = TOK.TOKint32v; | |
2213 break; | |
2214 | |
2215 case FLAGS.FLAGS_decimal: | |
2216 /* First that fits: int, long, long long | |
2217 */ | |
2218 if (n & 0x8000000000000000) | |
2219 { error("signed integer overflow"); | |
2220 result = TOK.TOKuns64v; | |
2221 } | |
2222 else if (n & 0xFFFFFFFF80000000) | |
2223 result = TOK.TOKint64v; | |
2224 else | |
2225 result = TOK.TOKint32v; | |
2226 break; | |
2227 | |
2228 case FLAGS.FLAGS_unsigned: | |
2229 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned: | |
2230 /* First that fits: uint, ulong | |
2231 */ | |
2232 if (n & 0xFFFFFFFF00000000) | |
2233 result = TOK.TOKuns64v; | |
2234 else | |
2235 result = TOK.TOKuns32v; | |
2236 break; | |
2237 | |
2238 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_long: | |
2239 if (n & 0x8000000000000000) | |
2240 { error("signed integer overflow"); | |
2241 result = TOK.TOKuns64v; | |
2242 } | |
2243 else | |
2244 result = TOK.TOKint64v; | |
2245 break; | |
2246 | |
2247 case FLAGS.FLAGS_long: | |
2248 if (n & 0x8000000000000000) | |
2249 result = TOK.TOKuns64v; | |
2250 else | |
2251 result = TOK.TOKint64v; | |
2252 break; | |
2253 | |
2254 case FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2255 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2256 result = TOK.TOKuns64v; | |
2257 break; | |
2258 | |
2259 default: | |
2260 debug { | |
2261 printf("%x\n",flags); | |
2262 } | |
2263 assert(0); | |
2264 } | |
2265 t.uns64value = n; | |
2266 return result; | |
2267 } | |
2268 | |
2269 /************************************** | |
2270 * Read in characters, converting them to real. | |
2271 * Bugs: | |
2272 * Exponent overflow not detected. | |
2273 * Too much requested precision is not detected. | |
2274 */ | |
2275 TOK inreal(Token* t) | |
2276 in | |
2277 { | |
2278 assert(*p == '.' || isdigit(*p)); | |
2279 } | |
2280 out (result) | |
2281 { | |
2282 switch (result) | |
2283 { | |
2284 case TOKfloat32v: | |
2285 case TOKfloat64v: | |
2286 case TOKfloat80v: | |
2287 case TOKimaginary32v: | |
2288 case TOKimaginary64v: | |
2289 case TOKimaginary80v: | |
2290 break; | |
2291 | |
2292 default: | |
2293 assert(0); | |
2294 } | |
2295 } | |
2296 body | |
2297 { | |
2298 int dblstate; | |
2299 uint c; | |
2300 char hex; // is this a hexadecimal-floating-constant? | |
2301 TOK result; | |
2302 | |
2303 //printf("Lexer.inreal()\n"); | |
2304 stringbuffer.reset(); | |
2305 dblstate = 0; | |
2306 hex = 0; | |
2307 Lnext: | |
2308 while (true) | |
2309 { | |
2310 // Get next char from input | |
2311 c = *p++; | |
2312 //printf("dblstate = %d, c = '%c'\n", dblstate, c); | |
2313 while (true) | |
2314 { | |
2315 switch (dblstate) | |
2316 { | |
2317 case 0: // opening state | |
2318 if (c == '0') | |
2319 dblstate = 9; | |
2320 else if (c == '.') | |
2321 dblstate = 3; | |
2322 else | |
2323 dblstate = 1; | |
2324 break; | |
2325 | |
2326 case 9: | |
2327 dblstate = 1; | |
2328 if (c == 'X' || c == 'x') | |
2329 { | |
2330 hex++; | |
2331 break; | |
2332 } | |
2333 case 1: // digits to left of . | |
2334 case 3: // digits to right of . | |
2335 case 7: // continuing exponent digits | |
2336 if (!isdigit(c) && !(hex && isxdigit(c))) | |
2337 { | |
2338 if (c == '_') | |
2339 goto Lnext; // ignore embedded '_' | |
2340 dblstate++; | |
2341 continue; | |
2342 } | |
2343 break; | |
2344 | |
2345 case 2: // no more digits to left of . | |
2346 if (c == '.') | |
2347 { | |
2348 dblstate++; | |
2349 break; | |
2350 } | |
2351 case 4: // no more digits to right of . | |
2352 if ((c == 'E' || c == 'e') || | |
2353 hex && (c == 'P' || c == 'p')) | |
2354 { | |
2355 dblstate = 5; | |
2356 hex = 0; // exponent is always decimal | |
2357 break; | |
2358 } | |
2359 if (hex) | |
2360 error("binary-exponent-part required"); | |
2361 goto done; | |
2362 | |
2363 case 5: // looking immediately to right of E | |
2364 dblstate++; | |
2365 if (c == '-' || c == '+') | |
2366 break; | |
2367 case 6: // 1st exponent digit expected | |
2368 if (!isdigit(c)) | |
2369 error("exponent expected"); | |
2370 dblstate++; | |
2371 break; | |
2372 | |
2373 case 8: // past end of exponent digits | |
2374 goto done; | |
2375 } | |
2376 break; | |
2377 } | |
2378 stringbuffer.writeByte(c); | |
2379 } | |
2380 done: | |
2381 p--; | |
2382 | |
2383 stringbuffer.writeByte(0); | |
2384 | |
2385 version (_WIN32) { /// && __DMC__ | |
2386 char* save = __locale_decpoint; | |
2387 __locale_decpoint = cast(char*)".".ptr; | |
2388 } | |
2389 t.float80value = strtold(cast(char*)stringbuffer.data, null); | |
2390 | |
2391 errno = 0; | |
2392 switch (*p) | |
2393 { | |
2394 case 'F': | |
2395 case 'f': | |
2396 strtof(cast(char*)stringbuffer.data, null); | |
2397 result = TOKfloat32v; | |
2398 p++; | |
2399 break; | |
2400 | |
2401 default: | |
2402 strtod(cast(char*)stringbuffer.data, null); | |
2403 result = TOKfloat64v; | |
2404 break; | |
2405 | |
2406 case 'l': | |
2407 if (!global.params.useDeprecated) | |
2408 error("'l' suffix is deprecated, use 'L' instead"); | |
2409 case 'L': | |
2410 result = TOKfloat80v; | |
2411 p++; | |
2412 break; | |
2413 } | |
2414 if (*p == 'i' || *p == 'I') | |
2415 { | |
2416 if (!global.params.useDeprecated && *p == 'I') | |
2417 error("'I' suffix is deprecated, use 'i' instead"); | |
2418 p++; | |
2419 switch (result) | |
2420 { | |
2421 case TOKfloat32v: | |
2422 result = TOKimaginary32v; | |
2423 break; | |
2424 case TOKfloat64v: | |
2425 result = TOKimaginary64v; | |
2426 break; | |
2427 case TOKfloat80v: | |
2428 result = TOKimaginary80v; | |
2429 break; | |
2430 } | |
2431 } | |
2432 | |
2433 version (_WIN32) { ///&& __DMC__ | |
2434 __locale_decpoint = save; | |
2435 } | |
2436 if (errno == ERANGE) | |
2437 error("number is not representable"); | |
2438 | |
2439 return result; | |
2440 } | |
2441 | |
2442 void error(T...)(string format, T t) | |
2443 { | |
2444 error(this.loc, format, t); | |
2445 } | |
2446 | |
2447 void error(T...)(Loc loc, string format, T t) | |
2448 { | |
2449 if (mod && !global.gag) | |
2450 { | |
2451 string p = loc.toChars(); | |
2452 if (p.length != 0) | |
2453 writef("%s: ", p); | |
2454 | |
2455 writefln(format, t); | |
2456 | |
2457 if (global.errors >= 20) // moderate blizzard of cascading messages | |
2458 fatal(); | |
2459 } | |
2460 | |
2461 global.errors++; | |
2462 } | |
2463 | |
2464 void pragma_() | |
2465 { | |
2466 assert(false); | |
2467 } | |
2468 | |
2469 uint decodeUTF() | |
2470 { | |
2471 assert(false); | |
2472 } | |
2473 | |
2474 void getDocComment(Token* t, uint lineComment) | |
2475 { | |
2476 assert(false); | |
2477 } | |
2478 | |
2479 static bool isValidIdentifier(string p) | |
2480 { | |
2481 if (p.length == 0) { | |
2482 return false; | |
2483 } | |
2484 | |
2485 if (p[0] >= '0' && p[0] <= '9') { // beware of isdigit() on signed chars | |
2486 return false; | |
2487 } | |
2488 | |
2489 size_t idx = 0; | |
2490 while (idx < p.length) | |
2491 { | |
2492 dchar dc; | |
2493 | |
2494 if (utf_decodeChar(p, &idx, &dc) !is null) { | |
2495 return false; | |
2496 } | |
2497 | |
2498 if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_')) { | |
2499 return false; | |
2500 } | |
2501 } | |
2502 | |
2503 return true; | |
2504 } | |
2505 | |
2506 /// TODO: reimplement based on strings | |
2507 static ubyte* combineComments(ubyte* c1, ubyte* c2) | |
2508 { | |
2509 //printf("Lexer.combineComments('%s', '%s')\n", c1, c2); | |
2510 | |
2511 ubyte* c = c2; | |
2512 | |
2513 if (c1) | |
2514 { | |
2515 c = c1; | |
2516 if (c2) | |
2517 { | |
2518 size_t len1 = strlen(cast(char*)c1); | |
2519 size_t len2 = strlen(cast(char*)c2); | |
2520 | |
2 | 2521 c = cast(ubyte*)GC.malloc(len1 + 1 + len2 + 1); |
0 | 2522 memcpy(c, c1, len1); |
2523 if (len1 && c1[len1 - 1] != '\n') | |
2524 { | |
2525 c[len1] = '\n'; | |
2526 len1++; | |
2527 } | |
2528 memcpy(c + len1, c2, len2); | |
2529 c[len1 + len2] = 0; | |
2530 } | |
2531 } | |
2532 | |
2533 return c; | |
2534 } | |
2535 } |