Mercurial > projects > ddmd
annotate dmd/Lexer.d @ 169:e7769d53e750
Moves static variables from Module to Global
author | korDen |
---|---|
date | Thu, 30 Sep 2010 13:29:54 +0400 |
parents | ceed63f310fb |
children | af724d3510d7 |
rev | line source |
---|---|
0 | 1 module dmd.Lexer; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.StringTable; |
5 import dmd.OutBuffer; | |
6 import dmd.Token; | |
7 import dmd.Loc; | |
8 import dmd.Module; | |
9 import dmd.Identifier; | |
10 import dmd.TOK; | |
11 import dmd.Keyword; | |
12 import dmd.StringValue; | |
13 import dmd.Global; | |
14 import dmd.Util; | |
15 import dmd.Id; | |
16 import dmd.Dchar; | |
17 import dmd.Utf; | |
18 | |
19 import std.stdio : writeln; | |
20 | |
4 | 21 import core.memory; |
2 | 22 |
0 | 23 import core.stdc.ctype; |
24 import core.stdc.stdlib; | |
25 import core.stdc.string; | |
26 import core.stdc.errno; | |
27 | |
28 enum LS = 0x2028; // UTF line separator | |
29 enum PS = 0x2029; // UTF paragraph separator | |
30 | |
31 extern (C) extern | |
32 { | |
33 __gshared char* __locale_decpoint; | |
34 } | |
35 | |
162 | 36 bool isUniAlpha(uint u) |
0 | 37 { |
168
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
38 enum ushort table[][2] = |
162 | 39 [ |
40 [ 0x00AA, 0x00AA ], | |
41 [ 0x00B5, 0x00B5 ], | |
42 [ 0x00B7, 0x00B7 ], | |
43 [ 0x00BA, 0x00BA ], | |
44 [ 0x00C0, 0x00D6 ], | |
45 [ 0x00D8, 0x00F6 ], | |
46 [ 0x00F8, 0x01F5 ], | |
47 [ 0x01FA, 0x0217 ], | |
48 [ 0x0250, 0x02A8 ], | |
49 [ 0x02B0, 0x02B8 ], | |
50 [ 0x02BB, 0x02BB ], | |
51 [ 0x02BD, 0x02C1 ], | |
52 [ 0x02D0, 0x02D1 ], | |
53 [ 0x02E0, 0x02E4 ], | |
54 [ 0x037A, 0x037A ], | |
55 [ 0x0386, 0x0386 ], | |
56 [ 0x0388, 0x038A ], | |
57 [ 0x038C, 0x038C ], | |
58 [ 0x038E, 0x03A1 ], | |
59 [ 0x03A3, 0x03CE ], | |
60 [ 0x03D0, 0x03D6 ], | |
61 [ 0x03DA, 0x03DA ], | |
62 [ 0x03DC, 0x03DC ], | |
63 [ 0x03DE, 0x03DE ], | |
64 [ 0x03E0, 0x03E0 ], | |
65 [ 0x03E2, 0x03F3 ], | |
66 [ 0x0401, 0x040C ], | |
67 [ 0x040E, 0x044F ], | |
68 [ 0x0451, 0x045C ], | |
69 [ 0x045E, 0x0481 ], | |
70 [ 0x0490, 0x04C4 ], | |
71 [ 0x04C7, 0x04C8 ], | |
72 [ 0x04CB, 0x04CC ], | |
73 [ 0x04D0, 0x04EB ], | |
74 [ 0x04EE, 0x04F5 ], | |
75 [ 0x04F8, 0x04F9 ], | |
76 [ 0x0531, 0x0556 ], | |
77 [ 0x0559, 0x0559 ], | |
78 [ 0x0561, 0x0587 ], | |
79 [ 0x05B0, 0x05B9 ], | |
80 [ 0x05BB, 0x05BD ], | |
81 [ 0x05BF, 0x05BF ], | |
82 [ 0x05C1, 0x05C2 ], | |
83 [ 0x05D0, 0x05EA ], | |
84 [ 0x05F0, 0x05F2 ], | |
85 [ 0x0621, 0x063A ], | |
86 [ 0x0640, 0x0652 ], | |
87 [ 0x0660, 0x0669 ], | |
88 [ 0x0670, 0x06B7 ], | |
89 [ 0x06BA, 0x06BE ], | |
90 [ 0x06C0, 0x06CE ], | |
91 [ 0x06D0, 0x06DC ], | |
92 [ 0x06E5, 0x06E8 ], | |
93 [ 0x06EA, 0x06ED ], | |
94 [ 0x06F0, 0x06F9 ], | |
95 [ 0x0901, 0x0903 ], | |
96 [ 0x0905, 0x0939 ], | |
97 [ 0x093D, 0x093D ], | |
98 [ 0x093E, 0x094D ], | |
99 [ 0x0950, 0x0952 ], | |
100 [ 0x0958, 0x0963 ], | |
101 [ 0x0966, 0x096F ], | |
102 [ 0x0981, 0x0983 ], | |
103 [ 0x0985, 0x098C ], | |
104 [ 0x098F, 0x0990 ], | |
105 [ 0x0993, 0x09A8 ], | |
106 [ 0x09AA, 0x09B0 ], | |
107 [ 0x09B2, 0x09B2 ], | |
108 [ 0x09B6, 0x09B9 ], | |
109 [ 0x09BE, 0x09C4 ], | |
110 [ 0x09C7, 0x09C8 ], | |
111 [ 0x09CB, 0x09CD ], | |
112 [ 0x09DC, 0x09DD ], | |
113 [ 0x09DF, 0x09E3 ], | |
114 [ 0x09E6, 0x09EF ], | |
115 [ 0x09F0, 0x09F1 ], | |
116 [ 0x0A02, 0x0A02 ], | |
117 [ 0x0A05, 0x0A0A ], | |
118 [ 0x0A0F, 0x0A10 ], | |
119 [ 0x0A13, 0x0A28 ], | |
120 [ 0x0A2A, 0x0A30 ], | |
121 [ 0x0A32, 0x0A33 ], | |
122 [ 0x0A35, 0x0A36 ], | |
123 [ 0x0A38, 0x0A39 ], | |
124 [ 0x0A3E, 0x0A42 ], | |
125 [ 0x0A47, 0x0A48 ], | |
126 [ 0x0A4B, 0x0A4D ], | |
127 [ 0x0A59, 0x0A5C ], | |
128 [ 0x0A5E, 0x0A5E ], | |
129 [ 0x0A66, 0x0A6F ], | |
130 [ 0x0A74, 0x0A74 ], | |
131 [ 0x0A81, 0x0A83 ], | |
132 [ 0x0A85, 0x0A8B ], | |
133 [ 0x0A8D, 0x0A8D ], | |
134 [ 0x0A8F, 0x0A91 ], | |
135 [ 0x0A93, 0x0AA8 ], | |
136 [ 0x0AAA, 0x0AB0 ], | |
137 [ 0x0AB2, 0x0AB3 ], | |
138 [ 0x0AB5, 0x0AB9 ], | |
139 [ 0x0ABD, 0x0AC5 ], | |
140 [ 0x0AC7, 0x0AC9 ], | |
141 [ 0x0ACB, 0x0ACD ], | |
142 [ 0x0AD0, 0x0AD0 ], | |
143 [ 0x0AE0, 0x0AE0 ], | |
144 [ 0x0AE6, 0x0AEF ], | |
145 [ 0x0B01, 0x0B03 ], | |
146 [ 0x0B05, 0x0B0C ], | |
147 [ 0x0B0F, 0x0B10 ], | |
148 [ 0x0B13, 0x0B28 ], | |
149 [ 0x0B2A, 0x0B30 ], | |
150 [ 0x0B32, 0x0B33 ], | |
151 [ 0x0B36, 0x0B39 ], | |
152 [ 0x0B3D, 0x0B3D ], | |
153 [ 0x0B3E, 0x0B43 ], | |
154 [ 0x0B47, 0x0B48 ], | |
155 [ 0x0B4B, 0x0B4D ], | |
156 [ 0x0B5C, 0x0B5D ], | |
157 [ 0x0B5F, 0x0B61 ], | |
158 [ 0x0B66, 0x0B6F ], | |
159 [ 0x0B82, 0x0B83 ], | |
160 [ 0x0B85, 0x0B8A ], | |
161 [ 0x0B8E, 0x0B90 ], | |
162 [ 0x0B92, 0x0B95 ], | |
163 [ 0x0B99, 0x0B9A ], | |
164 [ 0x0B9C, 0x0B9C ], | |
165 [ 0x0B9E, 0x0B9F ], | |
166 [ 0x0BA3, 0x0BA4 ], | |
167 [ 0x0BA8, 0x0BAA ], | |
168 [ 0x0BAE, 0x0BB5 ], | |
169 [ 0x0BB7, 0x0BB9 ], | |
170 [ 0x0BBE, 0x0BC2 ], | |
171 [ 0x0BC6, 0x0BC8 ], | |
172 [ 0x0BCA, 0x0BCD ], | |
173 [ 0x0BE7, 0x0BEF ], | |
174 [ 0x0C01, 0x0C03 ], | |
175 [ 0x0C05, 0x0C0C ], | |
176 [ 0x0C0E, 0x0C10 ], | |
177 [ 0x0C12, 0x0C28 ], | |
178 [ 0x0C2A, 0x0C33 ], | |
179 [ 0x0C35, 0x0C39 ], | |
180 [ 0x0C3E, 0x0C44 ], | |
181 [ 0x0C46, 0x0C48 ], | |
182 [ 0x0C4A, 0x0C4D ], | |
183 [ 0x0C60, 0x0C61 ], | |
184 [ 0x0C66, 0x0C6F ], | |
185 [ 0x0C82, 0x0C83 ], | |
186 [ 0x0C85, 0x0C8C ], | |
187 [ 0x0C8E, 0x0C90 ], | |
188 [ 0x0C92, 0x0CA8 ], | |
189 [ 0x0CAA, 0x0CB3 ], | |
190 [ 0x0CB5, 0x0CB9 ], | |
191 [ 0x0CBE, 0x0CC4 ], | |
192 [ 0x0CC6, 0x0CC8 ], | |
193 [ 0x0CCA, 0x0CCD ], | |
194 [ 0x0CDE, 0x0CDE ], | |
195 [ 0x0CE0, 0x0CE1 ], | |
196 [ 0x0CE6, 0x0CEF ], | |
197 [ 0x0D02, 0x0D03 ], | |
198 [ 0x0D05, 0x0D0C ], | |
199 [ 0x0D0E, 0x0D10 ], | |
200 [ 0x0D12, 0x0D28 ], | |
201 [ 0x0D2A, 0x0D39 ], | |
202 [ 0x0D3E, 0x0D43 ], | |
203 [ 0x0D46, 0x0D48 ], | |
204 [ 0x0D4A, 0x0D4D ], | |
205 [ 0x0D60, 0x0D61 ], | |
206 [ 0x0D66, 0x0D6F ], | |
207 [ 0x0E01, 0x0E3A ], | |
208 [ 0x0E40, 0x0E5B ], | |
209 // { 0x0E50, 0x0E59 }, | |
210 [ 0x0E81, 0x0E82 ], | |
211 [ 0x0E84, 0x0E84 ], | |
212 [ 0x0E87, 0x0E88 ], | |
213 [ 0x0E8A, 0x0E8A ], | |
214 [ 0x0E8D, 0x0E8D ], | |
215 [ 0x0E94, 0x0E97 ], | |
216 [ 0x0E99, 0x0E9F ], | |
217 [ 0x0EA1, 0x0EA3 ], | |
218 [ 0x0EA5, 0x0EA5 ], | |
219 [ 0x0EA7, 0x0EA7 ], | |
220 [ 0x0EAA, 0x0EAB ], | |
221 [ 0x0EAD, 0x0EAE ], | |
222 [ 0x0EB0, 0x0EB9 ], | |
223 [ 0x0EBB, 0x0EBD ], | |
224 [ 0x0EC0, 0x0EC4 ], | |
225 [ 0x0EC6, 0x0EC6 ], | |
226 [ 0x0EC8, 0x0ECD ], | |
227 [ 0x0ED0, 0x0ED9 ], | |
228 [ 0x0EDC, 0x0EDD ], | |
229 [ 0x0F00, 0x0F00 ], | |
230 [ 0x0F18, 0x0F19 ], | |
231 [ 0x0F20, 0x0F33 ], | |
232 [ 0x0F35, 0x0F35 ], | |
233 [ 0x0F37, 0x0F37 ], | |
234 [ 0x0F39, 0x0F39 ], | |
235 [ 0x0F3E, 0x0F47 ], | |
236 [ 0x0F49, 0x0F69 ], | |
237 [ 0x0F71, 0x0F84 ], | |
238 [ 0x0F86, 0x0F8B ], | |
239 [ 0x0F90, 0x0F95 ], | |
240 [ 0x0F97, 0x0F97 ], | |
241 [ 0x0F99, 0x0FAD ], | |
242 [ 0x0FB1, 0x0FB7 ], | |
243 [ 0x0FB9, 0x0FB9 ], | |
244 [ 0x10A0, 0x10C5 ], | |
245 [ 0x10D0, 0x10F6 ], | |
246 [ 0x1E00, 0x1E9B ], | |
247 [ 0x1EA0, 0x1EF9 ], | |
248 [ 0x1F00, 0x1F15 ], | |
249 [ 0x1F18, 0x1F1D ], | |
250 [ 0x1F20, 0x1F45 ], | |
251 [ 0x1F48, 0x1F4D ], | |
252 [ 0x1F50, 0x1F57 ], | |
253 [ 0x1F59, 0x1F59 ], | |
254 [ 0x1F5B, 0x1F5B ], | |
255 [ 0x1F5D, 0x1F5D ], | |
256 [ 0x1F5F, 0x1F7D ], | |
257 [ 0x1F80, 0x1FB4 ], | |
258 [ 0x1FB6, 0x1FBC ], | |
259 [ 0x1FBE, 0x1FBE ], | |
260 [ 0x1FC2, 0x1FC4 ], | |
261 [ 0x1FC6, 0x1FCC ], | |
262 [ 0x1FD0, 0x1FD3 ], | |
263 [ 0x1FD6, 0x1FDB ], | |
264 [ 0x1FE0, 0x1FEC ], | |
265 [ 0x1FF2, 0x1FF4 ], | |
266 [ 0x1FF6, 0x1FFC ], | |
267 [ 0x203F, 0x2040 ], | |
268 [ 0x207F, 0x207F ], | |
269 [ 0x2102, 0x2102 ], | |
270 [ 0x2107, 0x2107 ], | |
271 [ 0x210A, 0x2113 ], | |
272 [ 0x2115, 0x2115 ], | |
273 [ 0x2118, 0x211D ], | |
274 [ 0x2124, 0x2124 ], | |
275 [ 0x2126, 0x2126 ], | |
276 [ 0x2128, 0x2128 ], | |
277 [ 0x212A, 0x2131 ], | |
278 [ 0x2133, 0x2138 ], | |
279 [ 0x2160, 0x2182 ], | |
280 [ 0x3005, 0x3007 ], | |
281 [ 0x3021, 0x3029 ], | |
282 [ 0x3041, 0x3093 ], | |
283 [ 0x309B, 0x309C ], | |
284 [ 0x30A1, 0x30F6 ], | |
285 [ 0x30FB, 0x30FC ], | |
286 [ 0x3105, 0x312C ], | |
287 [ 0x4E00, 0x9FA5 ], | |
288 [ 0xAC00, 0xD7A3 ], | |
289 ]; | |
290 | |
291 debug { | |
292 for (int i = 0; i < table.length; i++) | |
293 { | |
294 //printf("%x\n", table[i][0]); | |
295 assert(table[i][0] <= table[i][1]); | |
296 if (i < table.length - 1) | |
297 assert(table[i][1] < table[i + 1][0]); | |
298 } | |
299 } | |
300 | |
301 if (u > 0xD7A3) | |
302 goto Lisnot; | |
303 | |
304 // Binary search | |
305 int mid; | |
306 int low; | |
307 int high; | |
308 | |
309 low = 0; | |
310 high = table.length - 1; | |
311 while (low <= high) | |
312 { | |
313 mid = (low + high) >> 1; | |
314 if (u < table[mid][0]) | |
315 high = mid - 1; | |
316 else if (u > table[mid][1]) | |
317 low = mid + 1; | |
318 else | |
319 goto Lis; | |
320 } | |
321 | |
322 Lisnot: | |
323 debug { | |
324 for (int i = 0; i < table.length; i++) | |
325 { | |
326 assert(u < table[i][0] || u > table[i][1]); | |
327 } | |
328 } | |
329 return false; | |
330 | |
331 Lis: | |
332 debug { | |
333 for (int i = 0; i < table.length; i++) | |
334 { | |
335 if (u >= table[i][0] && u <= table[i][1]) | |
336 return 1; | |
337 } | |
338 assert(0); // should have been in table | |
339 } | |
340 return true; | |
0 | 341 } |
342 | |
343 class Lexer | |
344 { | |
345 Loc loc; // for error messages | |
346 | |
347 ubyte* base; // pointer to start of buffer | |
348 ubyte* end; // past end of buffer | |
349 ubyte* p; // current character | |
350 Token token; | |
351 Module mod; | |
352 int doDocComment; // collect doc comment information | |
353 int anyToken; // !=0 means seen at least one token | |
354 int commentToken; // !=0 means comments are TOKcomment's | |
355 | |
356 this(Module mod, ubyte* base, uint begoffset, uint endoffset, int doDocComment, int commentToken) | |
357 { | |
358 loc = Loc(mod, 1); | |
359 | |
360 memset(&token,0,token.sizeof); | |
361 this.base = base; | |
362 this.end = base + endoffset; | |
363 p = base + begoffset; | |
364 this.mod = mod; | |
365 this.doDocComment = doDocComment; | |
366 this.anyToken = 0; | |
367 this.commentToken = commentToken; | |
368 //initKeywords(); | |
369 | |
370 /* If first line starts with '#!', ignore the line | |
371 */ | |
372 | |
373 if (p[0] == '#' && p[1] =='!') | |
374 { | |
375 p += 2; | |
376 while (1) | |
377 { | |
378 ubyte c = *p; | |
379 switch (c) | |
380 { | |
381 case '\n': | |
382 p++; | |
383 break; | |
384 | |
385 case '\r': | |
386 p++; | |
387 if (*p == '\n') | |
388 p++; | |
389 break; | |
390 | |
391 case 0: | |
392 case 0x1A: | |
393 break; | |
394 | |
395 default: | |
396 if (c & 0x80) | |
397 { | |
398 uint u = decodeUTF(); | |
399 if (u == PS || u == LS) | |
400 break; | |
401 } | |
402 p++; | |
403 continue; | |
404 } | |
405 break; | |
406 } | |
407 loc.linnum = 2; | |
408 } | |
409 } | |
410 | |
411 version (DMDV2) { | |
169 | 412 enum Keyword[] keywords = |
0 | 413 [ |
414 // { "", TOK }, | |
415 | |
416 { "this", TOK.TOKthis }, | |
417 { "super", TOK.TOKsuper }, | |
418 { "assert", TOK.TOKassert }, | |
419 { "null", TOK.TOKnull }, | |
420 { "true", TOK.TOKtrue }, | |
421 { "false", TOK.TOKfalse }, | |
422 { "cast", TOK.TOKcast }, | |
423 { "new", TOK.TOKnew }, | |
424 { "delete", TOK.TOKdelete }, | |
425 { "throw", TOK.TOKthrow }, | |
426 { "module", TOK.TOKmodule }, | |
427 { "pragma", TOK.TOKpragma }, | |
428 { "typeof", TOK.TOKtypeof }, | |
429 { "typeid", TOK.TOKtypeid }, | |
430 | |
431 { "template", TOK.TOKtemplate }, | |
432 | |
433 { "void", TOK.TOKvoid }, | |
434 { "byte", TOK.TOKint8 }, | |
435 { "ubyte", TOK.TOKuns8 }, | |
436 { "short", TOK.TOKint16 }, | |
437 { "ushort", TOK.TOKuns16 }, | |
438 { "int", TOK.TOKint32 }, | |
439 { "uint", TOK.TOKuns32 }, | |
440 { "long", TOK.TOKint64 }, | |
441 { "ulong", TOK.TOKuns64 }, | |
442 { "cent", TOK.TOKcent, }, | |
443 { "ucent", TOK.TOKucent, }, | |
444 { "float", TOK.TOKfloat32 }, | |
445 { "double", TOK.TOKfloat64 }, | |
446 { "real", TOK.TOKfloat80 }, | |
447 | |
448 { "bool", TOK.TOKbool }, | |
449 { "char", TOK.TOKchar }, | |
450 { "wchar", TOK.TOKwchar }, | |
451 { "dchar", TOK.TOKdchar }, | |
452 | |
453 { "ifloat", TOK.TOKimaginary32 }, | |
454 { "idouble", TOK.TOKimaginary64 }, | |
455 { "ireal", TOK.TOKimaginary80 }, | |
456 | |
457 { "cfloat", TOK.TOKcomplex32 }, | |
458 { "cdouble", TOK.TOKcomplex64 }, | |
459 { "creal", TOK.TOKcomplex80 }, | |
460 | |
461 { "delegate", TOK.TOKdelegate }, | |
462 { "function", TOK.TOKfunction }, | |
463 | |
464 { "is", TOK.TOKis }, | |
465 { "if", TOK.TOKif }, | |
466 { "else", TOK.TOKelse }, | |
467 { "while", TOK.TOKwhile }, | |
468 { "for", TOK.TOKfor }, | |
469 { "do", TOK.TOKdo }, | |
470 { "switch", TOK.TOKswitch }, | |
471 { "case", TOK.TOKcase }, | |
472 { "default", TOK.TOKdefault }, | |
473 { "break", TOK.TOKbreak }, | |
474 { "continue", TOK.TOKcontinue }, | |
475 { "synchronized", TOK.TOKsynchronized }, | |
476 { "return", TOK.TOKreturn }, | |
477 { "goto", TOK.TOKgoto }, | |
478 { "try", TOK.TOKtry }, | |
479 { "catch", TOK.TOKcatch }, | |
480 { "finally", TOK.TOKfinally }, | |
481 { "with", TOK.TOKwith }, | |
482 { "asm", TOK.TOKasm }, | |
483 { "foreach", TOK.TOKforeach }, | |
484 { "foreach_reverse", TOK.TOKforeach_reverse }, | |
485 { "scope", TOK.TOKscope }, | |
486 | |
487 { "struct", TOK.TOKstruct }, | |
488 { "class", TOK.TOKclass }, | |
489 { "interface", TOK.TOKinterface }, | |
490 { "union", TOK.TOKunion }, | |
491 { "enum", TOK.TOKenum }, | |
492 { "import", TOK.TOKimport }, | |
493 { "mixin", TOK.TOKmixin }, | |
494 { "static", TOK.TOKstatic }, | |
495 { "final", TOK.TOKfinal }, | |
496 { "const", TOK.TOKconst }, | |
497 { "typedef", TOK.TOKtypedef }, | |
498 { "alias", TOK.TOKalias }, | |
499 { "override", TOK.TOKoverride }, | |
500 { "abstract", TOK.TOKabstract }, | |
501 { "volatile", TOK.TOKvolatile }, | |
502 { "debug", TOK.TOKdebug }, | |
503 { "deprecated", TOK.TOKdeprecated }, | |
504 { "in", TOK.TOKin }, | |
505 { "out", TOK.TOKout }, | |
506 { "inout", TOK.TOKinout }, | |
507 { "lazy", TOK.TOKlazy }, | |
508 { "auto", TOK.TOKauto }, | |
509 | |
510 { "align", TOK.TOKalign }, | |
511 { "extern", TOK.TOKextern }, | |
512 { "private", TOK.TOKprivate }, | |
513 { "package", TOK.TOKpackage }, | |
514 { "protected", TOK.TOKprotected }, | |
515 { "public", TOK.TOKpublic }, | |
516 { "export", TOK.TOKexport }, | |
517 | |
518 { "body", TOK.TOKbody }, | |
519 { "invariant", TOK.TOKinvariant }, | |
520 { "unittest", TOK.TOKunittest }, | |
521 { "version", TOK.TOKversion }, | |
522 //{ "manifest", TOK.TOKmanifest }, | |
523 | |
524 // Added after 1.0 | |
525 { "ref", TOK.TOKref }, | |
526 { "macro", TOK.TOKmacro }, | |
527 { "pure", TOK.TOKpure }, | |
528 { "nothrow", TOK.TOKnothrow }, | |
529 { "__thread", TOK.TOKtls }, | |
530 { "__gshared", TOK.TOKgshared }, | |
531 { "__traits", TOK.TOKtraits }, | |
532 { "__overloadset", TOK.TOKoverloadset }, | |
533 { "__FILE__", TOK.TOKfile }, | |
534 { "__LINE__", TOK.TOKline }, | |
535 { "shared", TOK.TOKshared }, | |
536 { "immutable", TOK.TOKimmutable }, | |
537 ]; | |
538 } else { | |
169 | 539 enum Keyword[] keywords = |
0 | 540 [ |
541 // { "", TOK }, | |
542 | |
543 { "this", TOK.TOKthis }, | |
544 { "super", TOK.TOKsuper }, | |
545 { "assert", TOK.TOKassert }, | |
546 { "null", TOK.TOKnull }, | |
547 { "true", TOK.TOKtrue }, | |
548 { "false", TOK.TOKfalse }, | |
549 { "cast", TOK.TOKcast }, | |
550 { "new", TOK.TOKnew }, | |
551 { "delete", TOK.TOKdelete }, | |
552 { "throw", TOK.TOKthrow }, | |
553 { "module", TOK.TOKmodule }, | |
554 { "pragma", TOK.TOKpragma }, | |
555 { "typeof", TOK.TOKtypeof }, | |
556 { "typeid", TOK.TOKtypeid }, | |
557 | |
558 { "template", TOK.TOKtemplate }, | |
559 | |
560 { "void", TOK.TOKvoid }, | |
561 { "byte", TOK.TOKint8 }, | |
562 { "ubyte", TOK.TOKuns8 }, | |
563 { "short", TOK.TOKint16 }, | |
564 { "ushort", TOK.TOKuns16 }, | |
565 { "int", TOK.TOKint32 }, | |
566 { "uint", TOK.TOKuns32 }, | |
567 { "long", TOK.TOKint64 }, | |
568 { "ulong", TOK.TOKuns64 }, | |
569 { "cent", TOK.TOKcent, }, | |
570 { "ucent", TOK.TOKucent, }, | |
571 { "float", TOK.TOKfloat32 }, | |
572 { "double", TOK.TOKfloat64 }, | |
573 { "real", TOK.TOKfloat80 }, | |
574 | |
575 { "bool", TOK.TOKbool }, | |
576 { "char", TOK.TOKchar }, | |
577 { "wchar", TOK.TOKwchar }, | |
578 { "dchar", TOK.TOKdchar }, | |
579 | |
580 { "ifloat", TOK.TOKimaginary32 }, | |
581 { "idouble", TOK.TOKimaginary64 }, | |
582 { "ireal", TOK.TOKimaginary80 }, | |
583 | |
584 { "cfloat", TOK.TOKcomplex32 }, | |
585 { "cdouble", TOK.TOKcomplex64 }, | |
586 { "creal", TOK.TOKcomplex80 }, | |
587 | |
588 { "delegate", TOK.TOKdelegate }, | |
589 { "function", TOK.TOKfunction }, | |
590 | |
591 { "is", TOK.TOKis }, | |
592 { "if", TOK.TOKif }, | |
593 { "else", TOK.TOKelse }, | |
594 { "while", TOK.TOKwhile }, | |
595 { "for", TOK.TOKfor }, | |
596 { "do", TOK.TOKdo }, | |
597 { "switch", TOK.TOKswitch }, | |
598 { "case", TOK.TOKcase }, | |
599 { "default", TOK.TOKdefault }, | |
600 { "break", TOK.TOKbreak }, | |
601 { "continue", TOK.TOKcontinue }, | |
602 { "synchronized", TOK.TOKsynchronized }, | |
603 { "return", TOK.TOKreturn }, | |
604 { "goto", TOK.TOKgoto }, | |
605 { "try", TOK.TOKtry }, | |
606 { "catch", TOK.TOKcatch }, | |
607 { "finally", TOK.TOKfinally }, | |
608 { "with", TOK.TOKwith }, | |
609 { "asm", TOK.TOKasm }, | |
610 { "foreach", TOK.TOKforeach }, | |
611 { "foreach_reverse", TOK.TOKforeach_reverse }, | |
612 { "scope", TOK.TOKscope }, | |
613 | |
614 { "struct", TOK.TOKstruct }, | |
615 { "class", TOK.TOKclass }, | |
616 { "interface", TOK.TOKinterface }, | |
617 { "union", TOK.TOKunion }, | |
618 { "enum", TOK.TOKenum }, | |
619 { "import", TOK.TOKimport }, | |
620 { "mixin", TOK.TOKmixin }, | |
621 { "static", TOK.TOKstatic }, | |
622 { "final", TOK.TOKfinal }, | |
623 { "const", TOK.TOKconst }, | |
624 { "typedef", TOK.TOKtypedef }, | |
625 { "alias", TOK.TOKalias }, | |
626 { "override", TOK.TOKoverride }, | |
627 { "abstract", TOK.TOKabstract }, | |
628 { "volatile", TOK.TOKvolatile }, | |
629 { "debug", TOK.TOKdebug }, | |
630 { "deprecated", TOK.TOKdeprecated }, | |
631 { "in", TOK.TOKin }, | |
632 { "out", TOK.TOKout }, | |
633 { "inout", TOK.TOKinout }, | |
634 { "lazy", TOK.TOKlazy }, | |
635 { "auto", TOK.TOKauto }, | |
636 | |
637 { "align", TOK.TOKalign }, | |
638 { "extern", TOK.TOKextern }, | |
639 { "private", TOK.TOKprivate }, | |
640 { "package", TOK.TOKpackage }, | |
641 { "protected", TOK.TOKprotected }, | |
642 { "public", TOK.TOKpublic }, | |
643 { "export", TOK.TOKexport }, | |
644 | |
645 { "body", TOK.TOKbody }, | |
646 { "invariant", TOK.TOKinvariant }, | |
647 { "unittest", TOK.TOKunittest }, | |
648 { "version", TOK.TOKversion }, | |
649 //{ "manifest", TOK.TOKmanifest }, | |
650 | |
651 // Added after 1.0 | |
652 { "ref", TOK.TOKref }, | |
653 { "macro", TOK.TOKmacro }, | |
654 ]; | |
655 } | |
656 | |
657 static ubyte cmtable[256]; | |
658 enum CMoctal = 0x1; | |
659 enum CMhex = 0x2; | |
660 enum CMidchar = 0x4; | |
661 | |
662 ubyte isoctal (ubyte c) { return cmtable[c] & CMoctal; } | |
663 ubyte ishex (ubyte c) { return cmtable[c] & CMhex; } | |
664 ubyte isidchar(ubyte c) { return cmtable[c] & CMidchar; } | |
665 | |
666 static void cmtable_init() | |
667 { | |
668 for (uint c = 0; c < cmtable.length; c++) | |
669 { | |
670 if ('0' <= c && c <= '7') | |
671 cmtable[c] |= CMoctal; | |
672 if (isdigit(c) || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) | |
673 cmtable[c] |= CMhex; | |
674 if (isalnum(c) || c == '_') | |
675 cmtable[c] |= CMidchar; | |
676 } | |
677 } | |
168
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
678 |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
679 static StringTable stringtable() |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
680 { |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
681 return global.stringtable; |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
682 } |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
683 |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
684 static OutBuffer stringbuffer() |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
685 { |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
686 return global.stringbuffer; |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
687 } |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
688 |
0 | 689 static void initKeywords() |
690 { | |
691 uint nkeywords = keywords.length; | |
692 | |
693 if (global.params.Dversion == 1) | |
694 nkeywords -= 2; | |
695 | |
696 cmtable_init(); | |
697 | |
698 for (uint u = 0; u < nkeywords; u++) | |
699 { | |
34 | 700 //printf("keyword[%d] = '%.*s'\n",u, keywords[u].name); |
0 | 701 string s = keywords[u].name; |
702 TOK v = keywords[u].value; | |
703 StringValue* sv = stringtable.insert(s); | |
704 sv.ptrvalue = cast(void*) new Identifier(sv.lstring.string_, v); | |
705 | |
706 //printf("tochars[%d] = '%s'\n",v, s); | |
707 Token.tochars[v] = s; | |
708 } | |
709 | |
710 Token.tochars[TOK.TOKeof] = "EOF"; | |
711 Token.tochars[TOK.TOKlcurly] = "{"; | |
712 Token.tochars[TOK.TOKrcurly] = "}"; | |
713 Token.tochars[TOK.TOKlparen] = "("; | |
714 Token.tochars[TOK.TOKrparen] = ")"; | |
715 Token.tochars[TOK.TOKlbracket] = "["; | |
716 Token.tochars[TOK.TOKrbracket] = "]"; | |
717 Token.tochars[TOK.TOKsemicolon] = ";"; | |
718 Token.tochars[TOK.TOKcolon] = ":"; | |
719 Token.tochars[TOK.TOKcomma] = ","; | |
720 Token.tochars[TOK.TOKdot] = "."; | |
721 Token.tochars[TOK.TOKxor] = "^"; | |
722 Token.tochars[TOK.TOKxorass] = "^="; | |
723 Token.tochars[TOK.TOKassign] = "="; | |
724 Token.tochars[TOK.TOKconstruct] = "="; | |
725 version (DMDV2) { | |
726 Token.tochars[TOK.TOKblit] = "="; | |
727 } | |
728 Token.tochars[TOK.TOKlt] = "<"; | |
729 Token.tochars[TOK.TOKgt] = ">"; | |
730 Token.tochars[TOK.TOKle] = "<="; | |
731 Token.tochars[TOK.TOKge] = ">="; | |
732 Token.tochars[TOK.TOKequal] = "=="; | |
733 Token.tochars[TOK.TOKnotequal] = "!="; | |
734 Token.tochars[TOK.TOKnotidentity] = "!is"; | |
735 Token.tochars[TOK.TOKtobool] = "!!"; | |
736 | |
737 Token.tochars[TOK.TOKunord] = "!<>="; | |
738 Token.tochars[TOK.TOKue] = "!<>"; | |
739 Token.tochars[TOK.TOKlg] = "<>"; | |
740 Token.tochars[TOK.TOKleg] = "<>="; | |
741 Token.tochars[TOK.TOKule] = "!>"; | |
742 Token.tochars[TOK.TOKul] = "!>="; | |
743 Token.tochars[TOK.TOKuge] = "!<"; | |
744 Token.tochars[TOK.TOKug] = "!<="; | |
745 | |
746 Token.tochars[TOK.TOKnot] = "!"; | |
747 Token.tochars[TOK.TOKtobool] = "!!"; | |
748 Token.tochars[TOK.TOKshl] = "<<"; | |
749 Token.tochars[TOK.TOKshr] = ">>"; | |
750 Token.tochars[TOK.TOKushr] = ">>>"; | |
751 Token.tochars[TOK.TOKadd] = "+"; | |
752 Token.tochars[TOK.TOKmin] = "-"; | |
753 Token.tochars[TOK.TOKmul] = "*"; | |
754 Token.tochars[TOK.TOKdiv] = "/"; | |
755 Token.tochars[TOK.TOKmod] = "%"; | |
756 Token.tochars[TOK.TOKslice] = ".."; | |
757 Token.tochars[TOK.TOKdotdotdot] = "..."; | |
758 Token.tochars[TOK.TOKand] = "&"; | |
759 Token.tochars[TOK.TOKandand] = "&&"; | |
760 Token.tochars[TOK.TOKor] = "|"; | |
761 Token.tochars[TOK.TOKoror] = "||"; | |
762 Token.tochars[TOK.TOKarray] = "[]"; | |
763 Token.tochars[TOK.TOKindex] = "[i]"; | |
764 Token.tochars[TOK.TOKaddress] = "&"; | |
765 Token.tochars[TOK.TOKstar] = "*"; | |
766 Token.tochars[TOK.TOKtilde] = "~"; | |
767 Token.tochars[TOK.TOKdollar] = "$"; | |
768 Token.tochars[TOK.TOKcast] = "cast"; | |
769 Token.tochars[TOK.TOKplusplus] = "++"; | |
770 Token.tochars[TOK.TOKminusminus] = "--"; | |
771 Token.tochars[TOK.TOKtype] = "type"; | |
772 Token.tochars[TOK.TOKquestion] = "?"; | |
773 Token.tochars[TOK.TOKneg] = "-"; | |
774 Token.tochars[TOK.TOKuadd] = "+"; | |
775 Token.tochars[TOK.TOKvar] = "var"; | |
776 Token.tochars[TOK.TOKaddass] = "+="; | |
777 Token.tochars[TOK.TOKminass] = "-="; | |
778 Token.tochars[TOK.TOKmulass] = "*="; | |
779 Token.tochars[TOK.TOKdivass] = "/="; | |
780 Token.tochars[TOK.TOKmodass] = "%="; | |
781 Token.tochars[TOK.TOKshlass] = "<<="; | |
782 Token.tochars[TOK.TOKshrass] = ">>="; | |
783 Token.tochars[TOK.TOKushrass] = ">>>="; | |
784 Token.tochars[TOK.TOKandass] = "&="; | |
785 Token.tochars[TOK.TOKorass] = "|="; | |
786 Token.tochars[TOK.TOKcatass] = "~="; | |
787 Token.tochars[TOK.TOKcat] = "~"; | |
788 Token.tochars[TOK.TOKcall] = "call"; | |
789 Token.tochars[TOK.TOKidentity] = "is"; | |
790 Token.tochars[TOK.TOKnotidentity] = "!is"; | |
791 | |
792 Token.tochars[TOK.TOKorass] = "|="; | |
793 Token.tochars[TOK.TOKidentifier] = "identifier"; | |
794 Token.tochars[TOK.TOKat] = "@"; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
795 Token.tochars[TOK.TOKpow] = "^^"; |
135 | 796 Token.tochars[TOK.TOKpowass] = "^^="; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
797 |
0 | 798 // For debugging |
73 | 799 Token.tochars[TOKerror] = "error"; |
0 | 800 Token.tochars[TOK.TOKdotexp] = "dotexp"; |
801 Token.tochars[TOK.TOKdotti] = "dotti"; | |
802 Token.tochars[TOK.TOKdotvar] = "dotvar"; | |
803 Token.tochars[TOK.TOKdottype] = "dottype"; | |
804 Token.tochars[TOK.TOKsymoff] = "symoff"; | |
805 Token.tochars[TOK.TOKarraylength] = "arraylength"; | |
806 Token.tochars[TOK.TOKarrayliteral] = "arrayliteral"; | |
807 Token.tochars[TOK.TOKassocarrayliteral] = "assocarrayliteral"; | |
808 Token.tochars[TOK.TOKstructliteral] = "structliteral"; | |
809 Token.tochars[TOK.TOKstring] = "string"; | |
810 Token.tochars[TOK.TOKdsymbol] = "symbol"; | |
811 Token.tochars[TOK.TOKtuple] = "tuple"; | |
812 Token.tochars[TOK.TOKdeclaration] = "declaration"; | |
813 Token.tochars[TOK.TOKdottd] = "dottd"; | |
814 Token.tochars[TOK.TOKon_scope_exit] = "scope(exit)"; | |
815 Token.tochars[TOK.TOKon_scope_success] = "scope(success)"; | |
816 Token.tochars[TOK.TOKon_scope_failure] = "scope(failure)"; | |
817 } | |
818 | |
819 static Identifier idPool(string s) | |
820 { | |
821 StringValue* sv = stringtable.update(s); | |
822 Identifier id = cast(Identifier) sv.ptrvalue; | |
823 if (id is null) | |
824 { | |
825 id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
826 sv.ptrvalue = cast(void*)id; | |
827 } | |
828 | |
829 return id; | |
830 } | |
831 | |
832 static Identifier uniqueId(string s) | |
833 { | |
168
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
834 return uniqueId(s, ++global.num); |
0 | 835 } |
836 | |
837 /********************************************* | |
838 * Create a unique identifier using the prefix s. | |
839 */ | |
840 static Identifier uniqueId(string s, int num) | |
841 { | |
842 char buffer[32]; | |
843 size_t slen = s.length; | |
844 | |
845 assert(slen + num.sizeof * 3 + 1 <= buffer.sizeof); | |
846 int len = sprintf(buffer.ptr, "%.*s%d", s, num); | |
847 | |
848 return idPool(buffer[0..len].idup); | |
849 } | |
850 | |
851 TOK nextToken() | |
852 { | |
853 Token *t; | |
854 | |
855 if (token.next) | |
856 { | |
857 t = token.next; | |
858 memcpy(&token, t, Token.sizeof); | |
168
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
859 t.next = global.freelist; |
ceed63f310fb
stringtable, stringbuffer and freelist moved to Global
korDen
parents:
163
diff
changeset
|
860 global.freelist = t; |
0 | 861 } |
862 else | |
863 { | |
864 scan(&token); | |
865 } | |
866 | |
867 //token.print(); | |
868 return token.value; | |
869 } | |
870 | |
871 /*********************** | |
872 * Look ahead at next token's value. | |
873 */ | |
874 TOK peekNext() | |
875 { | |
876 return peek(&token).value; | |
877 } | |
878 | |
163 | 879 /*********************** |
880 * Look 2 tokens ahead at value. | |
881 */ | |
0 | 882 TOK peekNext2() |
883 { | |
163 | 884 Token* t = peek(&token); |
885 return peek(t).value; | |
0 | 886 } |
887 | |
888 void scan(Token* t) | |
889 { | |
890 uint lastLine = loc.linnum; | |
891 uint linnum; | |
892 | |
893 t.blockComment = null; | |
894 t.lineComment = null; | |
895 while (1) | |
896 { | |
897 t.ptr = p; | |
898 //printf("p = %p, *p = '%c'\n",p,*p); | |
899 switch (*p) | |
900 { | |
901 case 0: | |
902 case 0x1A: | |
903 t.value = TOK.TOKeof; // end of file | |
904 return; | |
905 | |
906 case ' ': | |
907 case '\t': | |
908 case '\v': | |
909 case '\f': | |
910 p++; | |
911 continue; // skip white space | |
912 | |
913 case '\r': | |
914 p++; | |
915 if (*p != '\n') // if CR stands by itself | |
916 loc.linnum++; | |
917 continue; // skip white space | |
918 | |
919 case '\n': | |
920 p++; | |
921 loc.linnum++; | |
922 continue; // skip white space | |
923 | |
924 case '0': case '1': case '2': case '3': case '4': | |
925 case '5': case '6': case '7': case '8': case '9': | |
926 t.value = number(t); | |
927 return; | |
928 | |
929 version (CSTRINGS) { | |
930 case '\'': | |
931 t.value = charConstant(t, 0); | |
932 return; | |
933 | |
934 case '"': | |
935 t.value = stringConstant(t,0); | |
936 return; | |
937 | |
938 case 'l': | |
939 case 'L': | |
940 if (p[1] == '\'') | |
941 { | |
942 p++; | |
943 t.value = charConstant(t, 1); | |
944 return; | |
945 } | |
946 else if (p[1] == '"') | |
947 { | |
948 p++; | |
949 t.value = stringConstant(t, 1); | |
950 return; | |
951 } | |
952 } else { | |
953 case '\'': | |
954 t.value = charConstant(t,0); | |
955 return; | |
956 | |
957 case 'r': | |
958 if (p[1] != '"') | |
959 goto case_ident; | |
960 p++; | |
961 case '`': | |
962 t.value = wysiwygStringConstant(t, *p); | |
963 return; | |
964 | |
965 case 'x': | |
966 if (p[1] != '"') | |
967 goto case_ident; | |
968 p++; | |
969 t.value = hexStringConstant(t); | |
970 return; | |
971 | |
972 version (DMDV2) { | |
973 case 'q': | |
974 if (p[1] == '"') | |
975 { | |
976 p++; | |
977 t.value = delimitedStringConstant(t); | |
978 return; | |
979 } | |
980 else if (p[1] == '{') | |
981 { | |
982 p++; | |
983 t.value = tokenStringConstant(t); | |
984 return; | |
985 } | |
986 else | |
987 goto case_ident; | |
988 } | |
989 | |
990 case '"': | |
991 t.value = escapeStringConstant(t,0); | |
992 return; | |
993 version (TEXTUAL_ASSEMBLY_OUT) { | |
994 } else { | |
995 case '\\': // escaped string literal | |
996 { uint c; | |
997 ubyte* pstart = p; | |
998 | |
999 stringbuffer.reset(); | |
1000 do | |
1001 { | |
1002 p++; | |
1003 switch (*p) | |
1004 { | |
1005 case 'u': | |
1006 case 'U': | |
1007 case '&': | |
1008 c = escapeSequence(); | |
1009 stringbuffer.writeUTF8(c); | |
1010 break; | |
1011 | |
1012 default: | |
1013 c = escapeSequence(); | |
1014 stringbuffer.writeByte(c); | |
1015 break; | |
1016 } | |
1017 } while (*p == '\\'); | |
1018 t.len = stringbuffer.offset; | |
1019 stringbuffer.writeByte(0); | |
2 | 1020 char* cc = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 1021 memcpy(cc, stringbuffer.data, stringbuffer.offset); |
1022 t.ustring = cc; | |
1023 t.postfix = 0; | |
1024 t.value = TOK.TOKstring; | |
1025 if (!global.params.useDeprecated) | |
1026 error("Escape String literal %.*s is deprecated, use double quoted string literal \"%.*s\" instead", p - pstart, pstart, p - pstart, pstart); | |
1027 return; | |
1028 } | |
1029 } | |
1030 case 'l': | |
1031 case 'L': | |
1032 } | |
1033 case 'a': case 'b': case 'c': case 'd': case 'e': | |
1034 case 'f': case 'g': case 'h': case 'i': case 'j': | |
1035 case 'k': case 'm': case 'n': case 'o': | |
1036 version (DMDV2) { | |
1037 case 'p': /*case 'q': case 'r':*/ case 's': case 't': | |
1038 } else { | |
1039 case 'p': case 'q': /*case 'r':*/ case 's': case 't': | |
1040 } | |
1041 case 'u': case 'v': case 'w': /*case 'x':*/ case 'y': | |
1042 case 'z': | |
1043 case 'A': case 'B': case 'C': case 'D': case 'E': | |
1044 case 'F': case 'G': case 'H': case 'I': case 'J': | |
1045 case 'K': case 'M': case 'N': case 'O': | |
1046 case 'P': case 'Q': case 'R': case 'S': case 'T': | |
1047 case 'U': case 'V': case 'W': case 'X': case 'Y': | |
1048 case 'Z': | |
1049 case '_': | |
1050 case_ident: | |
135 | 1051 { |
1052 ubyte c; | |
0 | 1053 |
135 | 1054 while (1) |
1055 { | |
1056 c = *++p; | |
1057 if (isidchar(c)) | |
1058 continue; | |
1059 else if (c & 0x80) | |
1060 { | |
1061 ubyte *s = p; | |
1062 uint u = decodeUTF(); | |
1063 if (isUniAlpha(u)) | |
1064 continue; | |
1065 error("char 0x%04x not allowed in identifier", u); | |
1066 p = s; | |
1067 } | |
1068 break; | |
1069 } | |
1070 | |
1071 StringValue *sv = stringtable.update((cast(immutable(char)*)t.ptr)[0.. p - t.ptr]); | |
1072 Identifier id = cast(Identifier) sv.ptrvalue; | |
1073 | |
0 | 1074 if (id is null) |
1075 { id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
1076 sv.ptrvalue = cast(void*)id; | |
1077 } | |
1078 t.ident = id; | |
1079 t.value = cast(TOK) id.value; | |
1080 anyToken = 1; | |
1081 if (*t.ptr == '_') // if special identifier token | |
1082 { | |
1083 ///version (DMDV1) { | |
1084 /// if (mod && id == Id.FILE) | |
1085 /// { | |
1086 /// t.ustring = cast(ubyte*)(loc.filename ? loc.filename : mod.ident.toChars()); | |
1087 /// goto Lstr; | |
1088 /// } | |
1089 /// else if (mod && id == Id.LINE) | |
1090 /// { | |
1091 /// t.value = TOK.TOKint64v; | |
1092 /// t.uns64value = loc.linnum; | |
1093 /// } | |
1094 /// else | |
1095 ///} | |
1096 if (id == Id.DATE) | |
1097 { | |
169 | 1098 t.ustring = global.date.ptr; |
0 | 1099 goto Lstr; |
1100 } | |
1101 else if (id == Id.TIME) | |
1102 { | |
169 | 1103 t.ustring = global.time.ptr; |
0 | 1104 goto Lstr; |
1105 } | |
1106 else if (id == Id.VENDOR) | |
1107 { | |
1108 t.ustring = "Digital Mars D".ptr; | |
1109 goto Lstr; | |
1110 } | |
1111 else if (id == Id.TIMESTAMP) | |
1112 { | |
169 | 1113 t.ustring = global.timestamp.ptr; |
0 | 1114 Lstr: |
1115 t.value = TOK.TOKstring; | |
1116 Llen: | |
1117 t.postfix = 0; | |
1118 t.len = strlen(cast(char*)t.ustring); | |
1119 } | |
1120 else if (id == Id.VERSIONX) | |
1121 { | |
1122 uint major = 0; | |
1123 uint minor = 0; | |
1124 | |
1125 foreach (char cc; global.version_[1..$]) | |
1126 { | |
1127 if (isdigit(cc)) | |
1128 minor = minor * 10 + cc - '0'; | |
1129 else if (cc == '.') | |
1130 { | |
1131 major = minor; | |
1132 minor = 0; | |
1133 } | |
1134 else | |
1135 break; | |
1136 } | |
1137 t.value = TOK.TOKint64v; | |
1138 t.uns64value = major * 1000 + minor; | |
1139 } | |
1140 ///version (DMDV2) { | |
1141 else if (id == Id.EOFX) | |
1142 { | |
1143 t.value = TOK.TOKeof; | |
1144 // Advance scanner to end of file | |
1145 while (!(*p == 0 || *p == 0x1A)) | |
1146 p++; | |
1147 } | |
1148 ///} | |
1149 } | |
1150 //printf("t.value = %d\n",t.value); | |
1151 return; | |
1152 } | |
1153 | |
1154 case '/': | |
1155 p++; | |
1156 switch (*p) | |
1157 { | |
1158 case '=': | |
1159 p++; | |
1160 t.value = TOK.TOKdivass; | |
1161 return; | |
1162 | |
1163 case '*': | |
1164 p++; | |
1165 linnum = loc.linnum; | |
1166 while (1) | |
1167 { | |
1168 while (1) | |
1169 { | |
1170 ubyte c = *p; | |
1171 switch (c) | |
1172 { | |
1173 case '/': | |
1174 break; | |
1175 | |
1176 case '\n': | |
1177 loc.linnum++; | |
1178 p++; | |
1179 continue; | |
1180 | |
1181 case '\r': | |
1182 p++; | |
1183 if (*p != '\n') | |
1184 loc.linnum++; | |
1185 continue; | |
1186 | |
1187 case 0: | |
1188 case 0x1A: | |
1189 error("unterminated /* */ comment"); | |
1190 p = end; | |
1191 t.value = TOK.TOKeof; | |
1192 return; | |
1193 | |
1194 default: | |
1195 if (c & 0x80) | |
1196 { uint u = decodeUTF(); | |
1197 if (u == PS || u == LS) | |
1198 loc.linnum++; | |
1199 } | |
1200 p++; | |
1201 continue; | |
1202 } | |
1203 break; | |
1204 } | |
1205 p++; | |
1206 if (p[-2] == '*' && p - 3 != t.ptr) | |
1207 break; | |
1208 } | |
1209 if (commentToken) | |
1210 { | |
1211 t.value = TOK.TOKcomment; | |
1212 return; | |
1213 } | |
1214 else if (doDocComment && t.ptr[2] == '*' && p - 4 != t.ptr) | |
1215 { // if /** but not /**/ | |
1216 getDocComment(t, lastLine == linnum); | |
1217 } | |
1218 continue; | |
1219 | |
1220 case '/': // do // style comments | |
1221 linnum = loc.linnum; | |
1222 while (1) | |
1223 { ubyte c = *++p; | |
1224 switch (c) | |
1225 { | |
1226 case '\n': | |
1227 break; | |
1228 | |
1229 case '\r': | |
1230 if (p[1] == '\n') | |
1231 p++; | |
1232 break; | |
1233 | |
1234 case 0: | |
1235 case 0x1A: | |
1236 if (commentToken) | |
1237 { | |
1238 p = end; | |
1239 t.value = TOK.TOKcomment; | |
1240 return; | |
1241 } | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
1242 if (doDocComment && t.ptr[2] == '/' || t.ptr[2] == '!') // '///' or '//!' |
0 | 1243 getDocComment(t, lastLine == linnum); |
1244 p = end; | |
1245 t.value = TOK.TOKeof; | |
1246 return; | |
1247 | |
1248 default: | |
1249 if (c & 0x80) | |
1250 { uint u = decodeUTF(); | |
1251 if (u == PS || u == LS) | |
1252 break; | |
1253 } | |
1254 continue; | |
1255 } | |
1256 break; | |
1257 } | |
1258 | |
1259 if (commentToken) | |
1260 { | |
1261 p++; | |
1262 loc.linnum++; | |
1263 t.value = TOK.TOKcomment; | |
1264 return; | |
1265 } | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
1266 if (doDocComment && t.ptr[2] == '/' || t.ptr[2] == '!') // '///' or '//!' |
0 | 1267 getDocComment(t, lastLine == linnum); |
1268 | |
1269 p++; | |
1270 loc.linnum++; | |
1271 continue; | |
1272 | |
1273 case '+': | |
1274 { | |
1275 int nest; | |
1276 | |
1277 linnum = loc.linnum; | |
1278 p++; | |
1279 nest = 1; | |
1280 while (1) | |
1281 { ubyte c = *p; | |
1282 switch (c) | |
1283 { | |
1284 case '/': | |
1285 p++; | |
1286 if (*p == '+') | |
1287 { | |
1288 p++; | |
1289 nest++; | |
1290 } | |
1291 continue; | |
1292 | |
1293 case '+': | |
1294 p++; | |
1295 if (*p == '/') | |
1296 { | |
1297 p++; | |
1298 if (--nest == 0) | |
1299 break; | |
1300 } | |
1301 continue; | |
1302 | |
1303 case '\r': | |
1304 p++; | |
1305 if (*p != '\n') | |
1306 loc.linnum++; | |
1307 continue; | |
1308 | |
1309 case '\n': | |
1310 loc.linnum++; | |
1311 p++; | |
1312 continue; | |
1313 | |
1314 case 0: | |
1315 case 0x1A: | |
1316 error("unterminated /+ +/ comment"); | |
1317 p = end; | |
1318 t.value = TOK.TOKeof; | |
1319 return; | |
1320 | |
1321 default: | |
1322 if (c & 0x80) | |
1323 { uint u = decodeUTF(); | |
1324 if (u == PS || u == LS) | |
1325 loc.linnum++; | |
1326 } | |
1327 p++; | |
1328 continue; | |
1329 } | |
1330 break; | |
1331 } | |
1332 if (commentToken) | |
1333 { | |
1334 t.value = TOK.TOKcomment; | |
1335 return; | |
1336 } | |
1337 if (doDocComment && t.ptr[2] == '+' && p - 4 != t.ptr) | |
1338 { // if /++ but not /++/ | |
1339 getDocComment(t, lastLine == linnum); | |
1340 } | |
1341 continue; | |
1342 } | |
1343 | |
1344 default: | |
1345 break; /// | |
1346 } | |
1347 t.value = TOK.TOKdiv; | |
1348 return; | |
1349 | |
1350 case '.': | |
1351 p++; | |
1352 if (isdigit(*p)) | |
1353 { /* Note that we don't allow ._1 and ._ as being | |
1354 * valid floating point numbers. | |
1355 */ | |
1356 p--; | |
1357 t.value = inreal(t); | |
1358 } | |
1359 else if (p[0] == '.') | |
1360 { | |
1361 if (p[1] == '.') | |
1362 { p += 2; | |
1363 t.value = TOK.TOKdotdotdot; | |
1364 } | |
1365 else | |
1366 { p++; | |
1367 t.value = TOK.TOKslice; | |
1368 } | |
1369 } | |
1370 else | |
1371 t.value = TOK.TOKdot; | |
1372 return; | |
1373 | |
1374 case '&': | |
1375 p++; | |
1376 if (*p == '=') | |
1377 { p++; | |
1378 t.value = TOK.TOKandass; | |
1379 } | |
1380 else if (*p == '&') | |
1381 { p++; | |
1382 t.value = TOK.TOKandand; | |
1383 } | |
1384 else | |
1385 t.value = TOK.TOKand; | |
1386 return; | |
1387 | |
1388 case '|': | |
1389 p++; | |
1390 if (*p == '=') | |
1391 { p++; | |
1392 t.value = TOK.TOKorass; | |
1393 } | |
1394 else if (*p == '|') | |
1395 { p++; | |
1396 t.value = TOK.TOKoror; | |
1397 } | |
1398 else | |
1399 t.value = TOK.TOKor; | |
1400 return; | |
1401 | |
1402 case '-': | |
1403 p++; | |
1404 if (*p == '=') | |
1405 { p++; | |
1406 t.value = TOK.TOKminass; | |
1407 } | |
1408 /// #if 0 | |
1409 /// else if (*p == '>') | |
1410 /// { p++; | |
1411 /// t.value = TOK.TOKarrow; | |
1412 /// } | |
1413 /// #endif | |
1414 else if (*p == '-') | |
1415 { p++; | |
1416 t.value = TOK.TOKminusminus; | |
1417 } | |
1418 else | |
1419 t.value = TOK.TOKmin; | |
1420 return; | |
1421 | |
1422 case '+': | |
1423 p++; | |
1424 if (*p == '=') | |
1425 { p++; | |
1426 t.value = TOK.TOKaddass; | |
1427 } | |
1428 else if (*p == '+') | |
1429 { p++; | |
1430 t.value = TOK.TOKplusplus; | |
1431 } | |
1432 else | |
1433 t.value = TOK.TOKadd; | |
1434 return; | |
1435 | |
1436 case '<': | |
1437 p++; | |
1438 if (*p == '=') | |
1439 { p++; | |
1440 t.value = TOK.TOKle; // <= | |
1441 } | |
1442 else if (*p == '<') | |
1443 { p++; | |
1444 if (*p == '=') | |
1445 { p++; | |
1446 t.value = TOK.TOKshlass; // <<= | |
1447 } | |
1448 else | |
1449 t.value = TOK.TOKshl; // << | |
1450 } | |
1451 else if (*p == '>') | |
1452 { p++; | |
1453 if (*p == '=') | |
1454 { p++; | |
1455 t.value = TOK.TOKleg; // <>= | |
1456 } | |
1457 else | |
1458 t.value = TOK.TOKlg; // <> | |
1459 } | |
1460 else | |
1461 t.value = TOK.TOKlt; // < | |
1462 return; | |
1463 | |
1464 case '>': | |
1465 p++; | |
1466 if (*p == '=') | |
1467 { p++; | |
1468 t.value = TOK.TOKge; // >= | |
1469 } | |
1470 else if (*p == '>') | |
1471 { p++; | |
1472 if (*p == '=') | |
1473 { p++; | |
1474 t.value = TOK.TOKshrass; // >>= | |
1475 } | |
1476 else if (*p == '>') | |
1477 { p++; | |
1478 if (*p == '=') | |
1479 { p++; | |
1480 t.value = TOK.TOKushrass; // >>>= | |
1481 } | |
1482 else | |
1483 t.value = TOK.TOKushr; // >>> | |
1484 } | |
1485 else | |
1486 t.value = TOK.TOKshr; // >> | |
1487 } | |
1488 else | |
1489 t.value = TOK.TOKgt; // > | |
1490 return; | |
1491 | |
1492 case '!': | |
1493 p++; | |
1494 if (*p == '=') | |
1495 { p++; | |
1496 if (*p == '=' && global.params.Dversion == 1) | |
1497 { p++; | |
1498 t.value = TOK.TOKnotidentity; // !== | |
1499 } | |
1500 else | |
1501 t.value = TOK.TOKnotequal; // != | |
1502 } | |
1503 else if (*p == '<') | |
1504 { p++; | |
1505 if (*p == '>') | |
1506 { p++; | |
1507 if (*p == '=') | |
1508 { p++; | |
1509 t.value = TOK.TOKunord; // !<>= | |
1510 } | |
1511 else | |
1512 t.value = TOK.TOKue; // !<> | |
1513 } | |
1514 else if (*p == '=') | |
1515 { p++; | |
1516 t.value = TOK.TOKug; // !<= | |
1517 } | |
1518 else | |
1519 t.value = TOK.TOKuge; // !< | |
1520 } | |
1521 else if (*p == '>') | |
1522 { p++; | |
1523 if (*p == '=') | |
1524 { p++; | |
1525 t.value = TOK.TOKul; // !>= | |
1526 } | |
1527 else | |
1528 t.value = TOK.TOKule; // !> | |
1529 } | |
1530 else | |
1531 t.value = TOK.TOKnot; // ! | |
1532 return; | |
1533 | |
1534 case '=': | |
1535 p++; | |
1536 if (*p == '=') | |
1537 { p++; | |
1538 if (*p == '=' && global.params.Dversion == 1) | |
1539 { p++; | |
1540 t.value = TOK.TOKidentity; // === | |
1541 } | |
1542 else | |
1543 t.value = TOK.TOKequal; // == | |
1544 } | |
1545 else | |
1546 t.value = TOK.TOKassign; // = | |
1547 return; | |
1548 | |
1549 case '~': | |
1550 p++; | |
1551 if (*p == '=') | |
1552 { p++; | |
1553 t.value = TOK.TOKcatass; // ~= | |
1554 } | |
1555 else | |
1556 t.value = TOK.TOKtilde; // ~ | |
1557 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1558 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1559 version(DMDV2) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1560 case '^': |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1561 p++; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1562 if (*p == '^') |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1563 { p++; |
135 | 1564 if (*p == '=') |
1565 { p++; | |
1566 t.value = TOKpowass; // ^^= | |
1567 } | |
1568 else | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1569 t.value = TOKpow; // ^^ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1570 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1571 else if (*p == '=') |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1572 { p++; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1573 t.value = TOKxorass; // ^= |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1574 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1575 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1576 t.value = TOKxor; // ^ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1577 return; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1578 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1579 |
0 | 1580 /* |
1581 #define SINGLE(c,tok) case c: p++; t.value = tok; return; | |
1582 | |
1583 SINGLE('(', TOKlparen) | |
1584 SINGLE(')', TOKrparen) | |
1585 SINGLE('[', TOKlbracket) | |
1586 SINGLE(']', TOKrbracket) | |
1587 SINGLE('{', TOKlcurly) | |
1588 SINGLE('}', TOKrcurly) | |
1589 SINGLE('?', TOKquestion) | |
1590 SINGLE(',', TOKcomma) | |
1591 SINGLE(';', TOKsemicolon) | |
1592 SINGLE(':', TOKcolon) | |
1593 SINGLE('$', TOKdollar) | |
1594 SINGLE('@', TOKat) | |
1595 | |
1596 #undef SINGLE | |
1597 | |
1598 #define DOUBLE(c1,tok1,c2,tok2) \ | |
1599 case c1: \ | |
1600 p++; \ | |
1601 if (*p == c2) \ | |
1602 { p++; \ | |
1603 t.value = tok2; \ | |
1604 } \ | |
1605 else \ | |
1606 t.value = tok1; \ | |
1607 return; | |
1608 | |
1609 DOUBLE('*', TOKmul, '=', TOKmulass) | |
1610 DOUBLE('%', TOKmod, '=', TOKmodass) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1611 #if DMDV1 |
0 | 1612 DOUBLE('^', TOKxor, '=', TOKxorass) |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1613 #endif |
0 | 1614 #undef DOUBLE |
1615 */ | |
1616 | |
1617 case '(': p++; t.value = TOK.TOKlparen; return; | |
1618 case ')': p++; t.value = TOK.TOKrparen; return; | |
1619 case '[': p++; t.value = TOK.TOKlbracket; return; | |
1620 case ']': p++; t.value = TOK.TOKrbracket; return; | |
1621 case '{': p++; t.value = TOK.TOKlcurly; return; | |
1622 case '}': p++; t.value = TOK.TOKrcurly; return; | |
1623 case '?': p++; t.value = TOK.TOKquestion; return; | |
1624 case ',': p++; t.value = TOK.TOKcomma; return; | |
1625 case ';': p++; t.value = TOK.TOKsemicolon; return; | |
1626 case ':': p++; t.value = TOK.TOKcolon; return; | |
1627 case '$': p++; t.value = TOK.TOKdollar; return; | |
1628 case '@': p++; t.value = TOK.TOKat; return; | |
1629 | |
1630 case '*': | |
1631 p++; | |
1632 if (*p == '=') { | |
1633 p++; | |
1634 t.value = TOK.TOKmulass; | |
1635 } else { | |
1636 t.value = TOK.TOKmul; | |
1637 } | |
1638 return; | |
1639 | |
1640 case '%': | |
1641 p++; | |
1642 if (*p == '=') { | |
1643 p++; | |
1644 t.value = TOK.TOKmodass; | |
1645 } else { | |
1646 t.value = TOK.TOKmod; | |
1647 } | |
1648 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1649 version(DMDV1) { |
0 | 1650 case '^': |
1651 p++; | |
1652 if (*p == '=') { | |
1653 p++; | |
1654 t.value = TOK.TOKxorass; | |
1655 } else { | |
1656 t.value = TOK.TOKxor; | |
1657 } | |
1658 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1659 } |
0 | 1660 case '#': |
1661 p++; | |
1662 pragma_(); | |
1663 continue; | |
1664 | |
1665 default: | |
135 | 1666 { uint c = *p; |
0 | 1667 |
1668 if (c & 0x80) | |
135 | 1669 { c = decodeUTF(); |
0 | 1670 |
1671 // Check for start of unicode identifier | |
135 | 1672 if (isUniAlpha(c)) |
0 | 1673 goto case_ident; |
1674 | |
135 | 1675 if (c == PS || c == LS) |
0 | 1676 { |
1677 loc.linnum++; | |
1678 p++; | |
1679 continue; | |
1680 } | |
1681 } | |
135 | 1682 if (c < 0x80 && isprint(c)) |
0 | 1683 error("unsupported char '%c'", c); |
1684 else | |
1685 error("unsupported char 0x%02x", c); | |
1686 p++; | |
1687 continue; | |
1688 } | |
1689 } | |
1690 } | |
1691 } | |
1692 | |
1693 Token* peek(Token* ct) | |
1694 { | |
1695 Token* t; | |
1696 | |
1697 if (ct.next) | |
1698 t = ct.next; | |
1699 else | |
1700 { | |
1701 t = new Token(); | |
1702 scan(t); | |
1703 t.next = null; | |
1704 ct.next = t; | |
1705 } | |
1706 return t; | |
1707 } | |
1708 | |
1709 Token* peekPastParen(Token* tk) | |
1710 { | |
1711 //printf("peekPastParen()\n"); | |
1712 int parens = 1; | |
1713 int curlynest = 0; | |
1714 while (1) | |
1715 { | |
1716 tk = peek(tk); | |
1717 //tk.print(); | |
1718 switch (tk.value) | |
1719 { | |
1720 case TOK.TOKlparen: | |
1721 parens++; | |
1722 continue; | |
1723 | |
1724 case TOK.TOKrparen: | |
1725 --parens; | |
1726 if (parens) | |
1727 continue; | |
1728 tk = peek(tk); | |
1729 break; | |
1730 | |
1731 case TOK.TOKlcurly: | |
1732 curlynest++; | |
1733 continue; | |
1734 | |
1735 case TOK.TOKrcurly: | |
1736 if (--curlynest >= 0) | |
1737 continue; | |
1738 break; | |
1739 | |
1740 case TOK.TOKsemicolon: | |
1741 if (curlynest) | |
1742 continue; | |
1743 break; | |
1744 | |
1745 case TOK.TOKeof: | |
1746 break; | |
1747 | |
1748 default: | |
1749 continue; | |
1750 } | |
1751 return tk; | |
1752 } | |
1753 } | |
1754 | |
1755 /******************************************* | |
1756 * Parse escape sequence. | |
1757 */ | |
1758 uint escapeSequence() | |
1759 { | |
1760 uint c = *p; | |
1761 | |
1762 version (TEXTUAL_ASSEMBLY_OUT) { | |
1763 return c; | |
1764 } | |
1765 int n; | |
1766 int ndigits; | |
1767 | |
1768 switch (c) | |
1769 { | |
1770 case '\'': | |
1771 case '"': | |
1772 case '?': | |
1773 case '\\': | |
1774 Lconsume: | |
1775 p++; | |
1776 break; | |
1777 | |
1778 case 'a': c = 7; goto Lconsume; | |
1779 case 'b': c = 8; goto Lconsume; | |
1780 case 'f': c = 12; goto Lconsume; | |
1781 case 'n': c = 10; goto Lconsume; | |
1782 case 'r': c = 13; goto Lconsume; | |
1783 case 't': c = 9; goto Lconsume; | |
1784 case 'v': c = 11; goto Lconsume; | |
1785 | |
1786 case 'u': | |
1787 ndigits = 4; | |
1788 goto Lhex; | |
1789 case 'U': | |
1790 ndigits = 8; | |
1791 goto Lhex; | |
1792 case 'x': | |
1793 ndigits = 2; | |
1794 Lhex: | |
1795 p++; | |
1796 c = *p; | |
1797 if (ishex(cast(ubyte)c)) | |
1798 { | |
1799 uint v; | |
1800 | |
1801 n = 0; | |
1802 v = 0; | |
1803 while (1) | |
1804 { | |
1805 if (isdigit(c)) | |
1806 c -= '0'; | |
1807 else if (islower(c)) | |
1808 c -= 'a' - 10; | |
1809 else | |
1810 c -= 'A' - 10; | |
1811 v = v * 16 + c; | |
1812 c = *++p; | |
1813 if (++n == ndigits) | |
1814 break; | |
1815 if (!ishex(cast(ubyte)c)) | |
1816 { error("escape hex sequence has %d hex digits instead of %d", n, ndigits); | |
1817 break; | |
1818 } | |
1819 } | |
1820 if (ndigits != 2 && !utf_isValidDchar(v)) | |
1821 { error("invalid UTF character \\U%08x", v); | |
1822 v = '?'; // recover with valid UTF character | |
1823 } | |
1824 c = v; | |
1825 } | |
1826 else | |
1827 error("undefined escape hex sequence \\%c\n",c); | |
1828 break; | |
1829 | |
1830 case '&': // named character entity | |
1831 for (ubyte* idstart = ++p; true; p++) | |
1832 { | |
1833 switch (*p) | |
1834 { | |
1835 case ';': | |
1836 c = HtmlNamedEntity(idstart, p - idstart); | |
1837 if (c == ~0) | |
1838 { | |
1839 error("unnamed character entity &%s;", idstart[0..(p - idstart)]); | |
1840 c = ' '; | |
1841 } | |
1842 p++; | |
1843 break; | |
1844 | |
1845 default: | |
1846 if (isalpha(*p) || | |
1847 (p != idstart + 1 && isdigit(*p))) | |
1848 continue; | |
1849 error("unterminated named entity"); | |
1850 break; | |
1851 } | |
1852 break; | |
1853 } | |
1854 break; | |
1855 | |
1856 case 0: | |
1857 case 0x1A: // end of file | |
1858 c = '\\'; | |
1859 break; | |
1860 | |
1861 default: | |
1862 if (isoctal(cast(ubyte)c)) | |
1863 { | |
1864 uint v; | |
1865 | |
1866 n = 0; | |
1867 v = 0; | |
1868 do | |
1869 { | |
1870 v = v * 8 + (c - '0'); | |
1871 c = *++p; | |
1872 } while (++n < 3 && isoctal(cast(ubyte)c)); | |
1873 c = v; | |
1874 if (c > 0xFF) | |
1875 error("0%03o is larger than a byte", c); | |
1876 } | |
1877 else | |
1878 error("undefined escape sequence \\%c\n",c); | |
1879 break; | |
1880 } | |
1881 return c; | |
1882 } | |
1883 | |
1884 TOK wysiwygStringConstant(Token* t, int tc) | |
1885 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1886 uint c; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1887 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1888 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1889 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1890 stringbuffer.reset(); |
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 c = *p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1894 switch (c) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1895 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1896 case '\n': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1897 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1898 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1899 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1900 case '\r': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1901 if (*p == '\n') |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1902 continue; // ignore |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1903 c = '\n'; // treat EndOfLine as \n character |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1904 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1905 break; |
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 0: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1908 case 0x1A: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1909 error("unterminated string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1910 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1911 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1912 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1913 return TOKstring; |
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 case '"': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1916 case '`': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1917 if (c == tc) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1918 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1919 t.len = stringbuffer.offset; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1920 stringbuffer.writeByte(0); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1921 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1922 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1923 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1924 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1925 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1926 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1927 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1928 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1929 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1930 if (c & 0x80) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1931 { p--; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1932 uint u = decodeUTF(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1933 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1934 if (u == PS || u == LS) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1935 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1936 stringbuffer.writeUTF8(u); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1937 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1938 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1939 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1940 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1941 stringbuffer.writeByte(c); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1942 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1943 |
0 | 1944 assert(false); |
1945 } | |
1946 | |
51 | 1947 /************************************** |
1948 * Lex hex strings: | |
1949 * x"0A ae 34FE BD" | |
1950 */ | |
0 | 1951 TOK hexStringConstant(Token* t) |
1952 { | |
51 | 1953 uint c; |
1954 Loc start = loc; | |
1955 uint n = 0; | |
1956 uint v; | |
1957 | |
1958 p++; | |
1959 stringbuffer.reset(); | |
1960 while (1) | |
1961 { | |
1962 c = *p++; | |
1963 switch (c) | |
1964 { | |
1965 case ' ': | |
1966 case '\t': | |
1967 case '\v': | |
1968 case '\f': | |
1969 continue; // skip white space | |
1970 | |
1971 case '\r': | |
1972 if (*p == '\n') | |
1973 continue; // ignore | |
1974 // Treat isolated '\r' as if it were a '\n' | |
1975 case '\n': | |
1976 loc.linnum++; | |
1977 continue; | |
1978 | |
1979 case 0: | |
1980 case 0x1A: | |
1981 error("unterminated string constant starting at %s", start.toChars()); | |
1982 t.ustring = "".ptr; | |
1983 t.len = 0; | |
1984 t.postfix = 0; | |
1985 return TOKstring; | |
1986 | |
1987 case '"': | |
1988 if (n & 1) | |
1989 { | |
1990 error("odd number (%d) of hex characters in hex string", n); | |
1991 stringbuffer.writeByte(v); | |
1992 } | |
1993 t.len = stringbuffer.offset; | |
1994 stringbuffer.writeByte(0); | |
1995 void* mem = malloc(stringbuffer.offset); | |
1996 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
1997 t.ustring = cast(const(char)*)mem; | |
1998 stringPostfix(t); | |
1999 return TOKstring; | |
2000 | |
2001 default: | |
2002 if (c >= '0' && c <= '9') | |
2003 c -= '0'; | |
2004 else if (c >= 'a' && c <= 'f') | |
2005 c -= 'a' - 10; | |
2006 else if (c >= 'A' && c <= 'F') | |
2007 c -= 'A' - 10; | |
2008 else if (c & 0x80) | |
2009 { p--; | |
2010 uint u = decodeUTF(); | |
2011 p++; | |
2012 if (u == PS || u == LS) | |
2013 loc.linnum++; | |
2014 else | |
135 | 2015 error("non-hex character \\u%04x", u); |
51 | 2016 } |
2017 else | |
2018 error("non-hex character '%c'", c); | |
2019 if (n & 1) | |
2020 { v = (v << 4) | c; | |
2021 stringbuffer.writeByte(v); | |
2022 } | |
2023 else | |
2024 v = c; | |
2025 n++; | |
2026 break; | |
2027 } | |
2028 } | |
0 | 2029 } |
2030 | |
2031 version (DMDV2) { | |
51 | 2032 /************************************** |
2033 * Lex delimited strings: | |
2034 * q"(foo(xxx))" // "foo(xxx)" | |
2035 * q"[foo(]" // "foo(" | |
2036 * q"/foo]/" // "foo]" | |
2037 * q"HERE | |
2038 * foo | |
2039 * HERE" // "foo\n" | |
2040 * Input: | |
2041 * p is on the " | |
2042 */ | |
0 | 2043 TOK delimitedStringConstant(Token* t) |
2044 { | |
51 | 2045 uint c; |
2046 Loc start = loc; | |
2047 uint delimleft = 0; | |
2048 uint delimright = 0; | |
2049 uint nest = 1; | |
2050 uint nestcount; | |
2051 Identifier hereid = null; | |
2052 uint blankrol = 0; | |
2053 uint startline = 0; | |
2054 | |
2055 p++; | |
2056 stringbuffer.reset(); | |
2057 while (1) | |
2058 { | |
2059 c = *p++; | |
2060 //printf("c = '%c'\n", c); | |
2061 switch (c) | |
2062 { | |
2063 case '\n': | |
2064 Lnextline: | |
2065 loc.linnum++; | |
2066 startline = 1; | |
2067 if (blankrol) | |
2068 { blankrol = 0; | |
2069 continue; | |
2070 } | |
2071 if (hereid) | |
2072 { | |
2073 stringbuffer.writeUTF8(c); | |
2074 continue; | |
2075 } | |
2076 break; | |
2077 | |
2078 case '\r': | |
2079 if (*p == '\n') | |
2080 continue; // ignore | |
2081 c = '\n'; // treat EndOfLine as \n character | |
2082 goto Lnextline; | |
2083 | |
2084 case 0: | |
2085 case 0x1A: | |
2086 goto Lerror; | |
2087 | |
2088 default: | |
2089 if (c & 0x80) | |
2090 { p--; | |
2091 c = decodeUTF(); | |
2092 p++; | |
2093 if (c == PS || c == LS) | |
2094 goto Lnextline; | |
2095 } | |
2096 break; | |
2097 } | |
2098 if (delimleft == 0) | |
2099 { | |
2100 delimleft = c; | |
2101 nest = 1; | |
2102 nestcount = 1; | |
2103 if (c == '(') | |
2104 delimright = ')'; | |
2105 else if (c == '{') | |
2106 delimright = '}'; | |
2107 else if (c == '[') | |
2108 delimright = ']'; | |
2109 else if (c == '<') | |
2110 delimright = '>'; | |
2111 else if (isalpha(c) || c == '_' || (c >= 0x80 && isUniAlpha(c))) | |
2112 { | |
2113 // Start of identifier; must be a heredoc | |
2114 Token t2; | |
2115 p--; | |
2116 scan(&t2); // read in heredoc identifier | |
2117 if (t2.value != TOKidentifier) | |
2118 { | |
2119 error("identifier expected for heredoc, not %s", t2.toChars()); | |
2120 delimright = c; | |
2121 } | |
2122 else | |
2123 { | |
2124 hereid = t2.ident; | |
2125 //printf("hereid = '%s'\n", hereid.toChars()); | |
2126 blankrol = 1; | |
2127 } | |
2128 nest = 0; | |
2129 } | |
2130 else | |
2131 { | |
2132 delimright = c; | |
2133 nest = 0; | |
2134 if (isspace(c)) | |
2135 error("delimiter cannot be whitespace"); | |
2136 } | |
2137 } | |
2138 else | |
2139 { | |
2140 if (blankrol) | |
2141 { | |
2142 error("heredoc rest of line should be blank"); | |
2143 blankrol = 0; | |
2144 continue; | |
2145 } | |
2146 if (nest == 1) | |
2147 { | |
2148 if (c == delimleft) | |
2149 nestcount++; | |
2150 else if (c == delimright) | |
2151 { nestcount--; | |
2152 if (nestcount == 0) | |
2153 goto Ldone; | |
2154 } | |
2155 } | |
2156 else if (c == delimright) | |
2157 goto Ldone; | |
2158 if (startline && isalpha(c) && hereid) | |
2159 { | |
2160 Token t2; | |
2161 ubyte* psave = p; | |
2162 p--; | |
2163 scan(&t2); // read in possible heredoc identifier | |
2164 //printf("endid = '%s'\n", t2.ident.toChars()); | |
2165 if (t2.value == TOKidentifier && t2.ident.equals(hereid)) | |
2166 { | |
2167 /* should check that rest of line is blank | |
2168 */ | |
2169 goto Ldone; | |
2170 } | |
2171 p = psave; | |
2172 } | |
2173 stringbuffer.writeUTF8(c); | |
2174 startline = 0; | |
2175 } | |
2176 } | |
2177 | |
2178 Ldone: | |
2179 if (*p == '"') | |
2180 p++; | |
2181 else | |
2182 error("delimited string must end in %c\"", delimright); | |
2183 t.len = stringbuffer.offset; | |
2184 stringbuffer.writeByte(0); | |
2185 void* mem = malloc(stringbuffer.offset); | |
2186 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
2187 t.ustring = cast(const(char)*)mem; | |
2188 stringPostfix(t); | |
2189 return TOKstring; | |
2190 | |
2191 Lerror: | |
2192 error("unterminated string constant starting at %s", start.toChars()); | |
2193 t.ustring = "".ptr; | |
2194 t.len = 0; | |
2195 t.postfix = 0; | |
2196 return TOKstring; | |
0 | 2197 } |
2198 | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2199 /************************************** |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2200 * Lex delimited strings: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2201 * q{ foo(xxx) } // " foo(xxx) " |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2202 * q{foo(} // "foo(" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2203 * q{{foo}"}"} // "{foo}"}"" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2204 * Input: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2205 * p is on the q |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2206 */ |
0 | 2207 TOK tokenStringConstant(Token* t) |
2208 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2209 uint nest = 1; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2210 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2211 ubyte* pstart = ++p; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2212 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2213 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2214 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2215 Token tok; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2216 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2217 scan(&tok); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2218 switch (tok.value) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2219 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2220 case TOKlcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2221 nest++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2222 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2223 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2224 case TOKrcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2225 if (--nest == 0) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2226 goto Ldone; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2227 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2228 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2229 case TOKeof: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2230 goto Lerror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2231 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2232 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2233 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2234 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2235 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2236 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2237 Ldone: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2238 t.len = p - 1 - pstart; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2239 char* tmp = cast(char*)GC.malloc(t.len + 1); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2240 memcpy(tmp, pstart, t.len); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2241 tmp[t.len] = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2242 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2243 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2244 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2245 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2246 Lerror: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2247 error("unterminated token string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2248 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2249 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2250 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2251 return TOKstring; |
0 | 2252 } |
2253 } | |
2254 TOK escapeStringConstant(Token* t, int wide) | |
2255 { | |
2256 uint c; | |
2257 Loc start = loc; | |
2258 | |
2259 p++; | |
2260 stringbuffer.reset(); | |
2261 while (true) | |
2262 { | |
2263 c = *p++; | |
2264 switch (c) | |
2265 { | |
2266 version (TEXTUAL_ASSEMBLY_OUT) { | |
2267 } else { | |
2268 case '\\': | |
2269 switch (*p) | |
2270 { | |
2271 case 'u': | |
2272 case 'U': | |
2273 case '&': | |
2274 c = escapeSequence(); | |
2275 stringbuffer.writeUTF8(c); | |
2276 continue; | |
2277 | |
2278 default: | |
2279 c = escapeSequence(); | |
2280 break; | |
2281 } | |
2282 break; | |
2283 } | |
2284 case '\n': | |
2285 loc.linnum++; | |
2286 break; | |
2287 | |
2288 case '\r': | |
2289 if (*p == '\n') | |
2290 continue; // ignore | |
2291 c = '\n'; // treat EndOfLine as \n character | |
2292 loc.linnum++; | |
2293 break; | |
2294 | |
2295 case '"': | |
2296 t.len = stringbuffer.offset; | |
2297 stringbuffer.writeByte(0); | |
2 | 2298 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 2299 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
2300 t.ustring = tmp; | |
2301 stringPostfix(t); | |
2302 return TOK.TOKstring; | |
2303 | |
2304 case 0: | |
2305 case 0x1A: | |
2306 p--; | |
2307 error("unterminated string constant starting at %s", start.toChars()); | |
2308 t.ustring = "".ptr; | |
2309 t.len = 0; | |
2310 t.postfix = 0; | |
2311 return TOK.TOKstring; | |
2312 | |
2313 default: | |
2314 if (c & 0x80) | |
2315 { | |
2316 p--; | |
2317 c = decodeUTF(); | |
2318 if (c == LS || c == PS) | |
2319 { c = '\n'; | |
2320 loc.linnum++; | |
2321 } | |
2322 p++; | |
2323 stringbuffer.writeUTF8(c); | |
2324 continue; | |
2325 } | |
2326 break; | |
2327 } | |
2328 stringbuffer.writeByte(c); | |
2329 } | |
2330 | |
2331 assert(false); | |
2332 } | |
2333 | |
2334 TOK charConstant(Token* t, int wide) | |
2335 { | |
2336 uint c; | |
2337 TOK tk = TOKcharv; | |
2338 | |
2339 //printf("Lexer.charConstant\n"); | |
2340 p++; | |
2341 c = *p++; | |
2342 switch (c) | |
2343 { | |
2344 version (TEXTUAL_ASSEMBLY_OUT) { | |
2345 } else { | |
2346 case '\\': | |
2347 switch (*p) | |
2348 { | |
2349 case 'u': | |
2350 t.uns64value = escapeSequence(); | |
2351 tk = TOKwcharv; | |
2352 break; | |
2353 | |
2354 case 'U': | |
2355 case '&': | |
2356 t.uns64value = escapeSequence(); | |
2357 tk = TOKdcharv; | |
2358 break; | |
2359 | |
2360 default: | |
2361 t.uns64value = escapeSequence(); | |
2362 break; | |
2363 } | |
2364 break; | |
2365 } | |
2366 case '\n': | |
2367 L1: | |
2368 loc.linnum++; | |
2369 case '\r': | |
2370 case 0: | |
2371 case 0x1A: | |
2372 case '\'': | |
2373 error("unterminated character constant"); | |
2374 return tk; | |
2375 | |
2376 default: | |
2377 if (c & 0x80) | |
2378 { | |
2379 p--; | |
2380 c = decodeUTF(); | |
2381 p++; | |
2382 if (c == LS || c == PS) | |
2383 goto L1; | |
2384 if (c < 0xD800 || (c >= 0xE000 && c < 0xFFFE)) | |
2385 tk = TOKwcharv; | |
2386 else | |
2387 tk = TOKdcharv; | |
2388 } | |
2389 t.uns64value = c; | |
2390 break; | |
2391 } | |
2392 | |
2393 if (*p != '\'') | |
2394 { | |
2395 error("unterminated character constant"); | |
2396 return tk; | |
2397 } | |
2398 p++; | |
2399 return tk; | |
2400 } | |
2401 | |
2402 /*************************************** | |
2403 * Get postfix of string literal. | |
2404 */ | |
2405 void stringPostfix(Token* t) | |
2406 { | |
2407 switch (*p) | |
2408 { | |
2409 case 'c': | |
2410 case 'w': | |
2411 case 'd': | |
2412 t.postfix = *p; | |
2413 p++; | |
2414 break; | |
2415 | |
2416 default: | |
2417 t.postfix = 0; | |
2418 break; | |
2419 } | |
2420 } | |
2421 | |
2422 uint wchar_(uint u) | |
2423 { | |
2424 assert(false); | |
2425 } | |
2426 | |
2427 /************************************** | |
2428 * Read in a number. | |
2429 * If it's an integer, store it in tok.TKutok.Vlong. | |
2430 * integers can be decimal, octal or hex | |
2431 * Handle the suffixes U, UL, LU, L, etc. | |
2432 * If it's double, store it in tok.TKutok.Vdouble. | |
2433 * Returns: | |
2434 * TKnum | |
2435 * TKdouble,... | |
2436 */ | |
2437 | |
2438 TOK number(Token* t) | |
2439 { | |
2440 // We use a state machine to collect numbers | |
2441 enum STATE { STATE_initial, STATE_0, STATE_decimal, STATE_octal, STATE_octale, | |
2442 STATE_hex, STATE_binary, STATE_hex0, STATE_binary0, | |
2443 STATE_hexh, STATE_error }; | |
2444 STATE state; | |
2445 | |
2446 enum FLAGS | |
2447 { | |
2448 FLAGS_undefined = 0, | |
2449 FLAGS_decimal = 1, // decimal | |
2450 FLAGS_unsigned = 2, // u or U suffix | |
2451 FLAGS_long = 4, // l or L suffix | |
2452 }; | |
2453 | |
2454 FLAGS flags = FLAGS.FLAGS_decimal; | |
2455 | |
2456 int i; | |
2457 int base; | |
2458 uint c; | |
2459 ubyte *start; | |
2460 TOK result; | |
2461 | |
2462 //printf("Lexer.number()\n"); | |
2463 state = STATE.STATE_initial; | |
2464 base = 0; | |
2465 stringbuffer.reset(); | |
2466 start = p; | |
2467 while (1) | |
2468 { | |
2469 c = *p; | |
2470 switch (state) | |
2471 { | |
2472 case STATE.STATE_initial: // opening state | |
2473 if (c == '0') | |
2474 state = STATE.STATE_0; | |
2475 else | |
2476 state = STATE.STATE_decimal; | |
2477 break; | |
2478 | |
2479 case STATE.STATE_0: | |
2480 flags = (flags & ~FLAGS.FLAGS_decimal); | |
2481 switch (c) | |
2482 { | |
2483 version (ZEROH) { | |
2484 case 'H': // 0h | |
2485 case 'h': | |
2486 goto hexh; | |
2487 } | |
2488 case 'X': | |
2489 case 'x': | |
2490 state = STATE.STATE_hex0; | |
2491 break; | |
2492 | |
2493 case '.': | |
2494 if (p[1] == '.') // .. is a separate token | |
2495 goto done; | |
2496 case 'i': | |
2497 case 'f': | |
2498 case 'F': | |
2499 goto real_; | |
2500 version (ZEROH) { | |
2501 case 'E': | |
2502 case 'e': | |
2503 goto case_hex; | |
2504 } | |
2505 case 'B': | |
2506 case 'b': | |
2507 state = STATE.STATE_binary0; | |
2508 break; | |
2509 | |
2510 case '0': case '1': case '2': case '3': | |
2511 case '4': case '5': case '6': case '7': | |
2512 state = STATE.STATE_octal; | |
2513 break; | |
2514 | |
2515 version (ZEROH) { | |
2516 case '8': case '9': case 'A': | |
2517 case 'C': case 'D': case 'F': | |
2518 case 'a': case 'c': case 'd': case 'f': | |
2519 case_hex: | |
2520 state = STATE.STATE_hexh; | |
2521 break; | |
2522 } | |
2523 case '_': | |
2524 state = STATE.STATE_octal; | |
2525 p++; | |
2526 continue; | |
2527 | |
2528 case 'L': | |
2529 if (p[1] == 'i') | |
2530 goto real_; | |
2531 goto done; | |
2532 | |
2533 default: | |
2534 goto done; | |
2535 } | |
2536 break; | |
2537 | |
2538 case STATE.STATE_decimal: // reading decimal number | |
2539 if (!isdigit(c)) | |
2540 { | |
2541 version (ZEROH) { | |
2542 if (ishex(c) | |
2543 || c == 'H' || c == 'h' | |
2544 ) | |
2545 goto hexh; | |
2546 } | |
2547 if (c == '_') // ignore embedded _ | |
2548 { p++; | |
2549 continue; | |
2550 } | |
2551 if (c == '.' && p[1] != '.') | |
2552 goto real_; | |
2553 else if (c == 'i' || c == 'f' || c == 'F' || | |
2554 c == 'e' || c == 'E') | |
2555 { | |
2556 real_: // It's a real number. Back up and rescan as a real | |
2557 p = start; | |
2558 return inreal(t); | |
2559 } | |
2560 else if (c == 'L' && p[1] == 'i') | |
2561 goto real_; | |
2562 goto done; | |
2563 } | |
2564 break; | |
2565 | |
2566 case STATE.STATE_hex0: // reading hex number | |
2567 case STATE.STATE_hex: | |
2568 if (! ishex(cast(ubyte)c)) | |
2569 { | |
2570 if (c == '_') // ignore embedded _ | |
2571 { p++; | |
2572 continue; | |
2573 } | |
2574 if (c == '.' && p[1] != '.') | |
2575 goto real_; | |
2576 if (c == 'P' || c == 'p' || c == 'i') | |
2577 goto real_; | |
2578 if (state == STATE.STATE_hex0) | |
2579 error("Hex digit expected, not '%c'", c); | |
2580 goto done; | |
2581 } | |
2582 state = STATE.STATE_hex; | |
2583 break; | |
2584 | |
2585 version (ZEROH) { | |
2586 hexh: | |
2587 state = STATE.STATE_hexh; | |
2588 case STATE.STATE_hexh: // parse numbers like 0FFh | |
2589 if (!ishex(c)) | |
2590 { | |
2591 if (c == 'H' || c == 'h') | |
2592 { | |
2593 p++; | |
2594 base = 16; | |
2595 goto done; | |
2596 } | |
2597 else | |
2598 { | |
2599 // Check for something like 1E3 or 0E24 | |
2600 if (memchr(cast(char*)stringbuffer.data, 'E', stringbuffer.offset) || | |
2601 memchr(cast(char*)stringbuffer.data, 'e', stringbuffer.offset)) | |
2602 goto real_; | |
2603 error("Hex digit expected, not '%c'", c); | |
2604 goto done; | |
2605 } | |
2606 } | |
2607 break; | |
2608 } | |
2609 | |
2610 case STATE.STATE_octal: // reading octal number | |
2611 case STATE.STATE_octale: // reading octal number with non-octal digits | |
2612 if (!isoctal(cast(ubyte)c)) | |
2613 { | |
2614 version (ZEROH) { | |
2615 if (ishex(c) | |
2616 || c == 'H' || c == 'h' | |
2617 ) | |
2618 goto hexh; | |
2619 } | |
2620 if (c == '_') // ignore embedded _ | |
2621 { p++; | |
2622 continue; | |
2623 } | |
2624 if (c == '.' && p[1] != '.') | |
2625 goto real_; | |
2626 if (c == 'i') | |
2627 goto real_; | |
2628 if (isdigit(c)) | |
2629 { | |
2630 state = STATE.STATE_octale; | |
2631 } | |
2632 else | |
2633 goto done; | |
2634 } | |
2635 break; | |
2636 | |
2637 case STATE.STATE_binary0: // starting binary number | |
2638 case STATE.STATE_binary: // reading binary number | |
2639 if (c != '0' && c != '1') | |
2640 { | |
2641 version (ZEROH) { | |
2642 if (ishex(c) | |
2643 || c == 'H' || c == 'h' | |
2644 ) | |
2645 goto hexh; | |
2646 } | |
2647 if (c == '_') // ignore embedded _ | |
2648 { p++; | |
2649 continue; | |
2650 } | |
2651 if (state == STATE.STATE_binary0) | |
2652 { error("binary digit expected"); | |
2653 state = STATE.STATE_error; | |
2654 break; | |
2655 } | |
2656 else | |
2657 goto done; | |
2658 } | |
2659 state = STATE.STATE_binary; | |
2660 break; | |
2661 | |
2662 case STATE.STATE_error: // for error recovery | |
2663 if (!isdigit(c)) // scan until non-digit | |
2664 goto done; | |
2665 break; | |
2666 | |
2667 default: | |
2668 assert(0); | |
2669 } | |
2670 stringbuffer.writeByte(c); | |
2671 p++; | |
2672 } | |
2673 done: | |
2674 stringbuffer.writeByte(0); // terminate string | |
2675 if (state == STATE.STATE_octale) | |
2676 error("Octal digit expected"); | |
2677 | |
2678 ulong n; // unsigned >=64 bit integer type | |
2679 | |
2680 if (stringbuffer.offset == 2 && (state == STATE.STATE_decimal || state == STATE.STATE_0)) | |
2681 n = stringbuffer.data[0] - '0'; | |
2682 else | |
2683 { | |
2684 // Convert string to integer | |
2685 version (__DMC__) { | |
2686 errno = 0; | |
2687 n = strtoull(cast(char*)stringbuffer.data,null,base); | |
2688 if (errno == ERANGE) | |
2689 error("integer overflow"); | |
2690 } else { | |
2691 // Not everybody implements strtoull() | |
2692 char* p = cast(char*)stringbuffer.data; | |
2693 int r = 10, d; | |
2694 | |
2695 if (*p == '0') | |
2696 { | |
2697 if (p[1] == 'x' || p[1] == 'X') | |
2698 p += 2, r = 16; | |
2699 else if (p[1] == 'b' || p[1] == 'B') | |
2700 p += 2, r = 2; | |
2701 else if (isdigit(p[1])) | |
2702 p += 1, r = 8; | |
2703 } | |
2704 | |
2705 n = 0; | |
2706 while (1) | |
2707 { | |
2708 if (*p >= '0' && *p <= '9') | |
2709 d = *p - '0'; | |
2710 else if (*p >= 'a' && *p <= 'z') | |
2711 d = *p - 'a' + 10; | |
2712 else if (*p >= 'A' && *p <= 'Z') | |
2713 d = *p - 'A' + 10; | |
2714 else | |
2715 break; | |
2716 if (d >= r) | |
2717 break; | |
2718 ulong n2 = n * r; | |
2719 //printf("n2 / r = %llx, n = %llx\n", n2/r, n); | |
2720 if (n2 / r != n || n2 + d < n) | |
2721 { | |
2722 error ("integer overflow"); | |
2723 break; | |
2724 } | |
2725 | |
2726 n = n2 + d; | |
2727 p++; | |
2728 } | |
2729 } | |
2730 if (n.sizeof > 8 && | |
2731 n > 0xFFFFFFFFFFFFFFFF) // if n needs more than 64 bits | |
2732 error("integer overflow"); | |
2733 } | |
2734 | |
2735 // Parse trailing 'u', 'U', 'l' or 'L' in any combination | |
2736 while (1) | |
2737 { FLAGS f; | |
2738 | |
2739 switch (*p) | |
2740 { case 'U': | |
2741 case 'u': | |
2742 f = FLAGS.FLAGS_unsigned; | |
2743 goto L1; | |
2744 | |
2745 case 'l': | |
2746 if (1 || !global.params.useDeprecated) | |
2747 error("'l' suffix is deprecated, use 'L' instead"); | |
2748 case 'L': | |
2749 f = FLAGS.FLAGS_long; | |
2750 L1: | |
2751 p++; | |
2752 if (flags & f) | |
2753 error("unrecognized token"); | |
2754 flags = (flags | f); | |
2755 continue; | |
2756 default: | |
2757 break; | |
2758 } | |
2759 break; | |
2760 } | |
2761 | |
2762 switch (flags) | |
2763 { | |
2764 case FLAGS.FLAGS_undefined: | |
2765 /* Octal or Hexadecimal constant. | |
2766 * First that fits: int, uint, long, ulong | |
2767 */ | |
2768 if (n & 0x8000000000000000) | |
2769 result = TOK.TOKuns64v; | |
2770 else if (n & 0xFFFFFFFF00000000) | |
2771 result = TOK.TOKint64v; | |
2772 else if (n & 0x80000000) | |
2773 result = TOK.TOKuns32v; | |
2774 else | |
2775 result = TOK.TOKint32v; | |
2776 break; | |
2777 | |
2778 case FLAGS.FLAGS_decimal: | |
2779 /* First that fits: int, long, long long | |
2780 */ | |
2781 if (n & 0x8000000000000000) | |
2782 { error("signed integer overflow"); | |
2783 result = TOK.TOKuns64v; | |
2784 } | |
2785 else if (n & 0xFFFFFFFF80000000) | |
2786 result = TOK.TOKint64v; | |
2787 else | |
2788 result = TOK.TOKint32v; | |
2789 break; | |
2790 | |
2791 case FLAGS.FLAGS_unsigned: | |
2792 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned: | |
2793 /* First that fits: uint, ulong | |
2794 */ | |
2795 if (n & 0xFFFFFFFF00000000) | |
2796 result = TOK.TOKuns64v; | |
2797 else | |
2798 result = TOK.TOKuns32v; | |
2799 break; | |
2800 | |
2801 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_long: | |
2802 if (n & 0x8000000000000000) | |
2803 { error("signed integer overflow"); | |
2804 result = TOK.TOKuns64v; | |
2805 } | |
2806 else | |
2807 result = TOK.TOKint64v; | |
2808 break; | |
2809 | |
2810 case FLAGS.FLAGS_long: | |
2811 if (n & 0x8000000000000000) | |
2812 result = TOK.TOKuns64v; | |
2813 else | |
2814 result = TOK.TOKint64v; | |
2815 break; | |
2816 | |
2817 case FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2818 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2819 result = TOK.TOKuns64v; | |
2820 break; | |
2821 | |
2822 default: | |
2823 debug { | |
2824 printf("%x\n",flags); | |
2825 } | |
2826 assert(0); | |
2827 } | |
2828 t.uns64value = n; | |
2829 return result; | |
2830 } | |
2831 | |
2832 /************************************** | |
2833 * Read in characters, converting them to real. | |
2834 * Bugs: | |
2835 * Exponent overflow not detected. | |
2836 * Too much requested precision is not detected. | |
2837 */ | |
2838 TOK inreal(Token* t) | |
2839 in | |
2840 { | |
2841 assert(*p == '.' || isdigit(*p)); | |
2842 } | |
2843 out (result) | |
2844 { | |
2845 switch (result) | |
2846 { | |
2847 case TOKfloat32v: | |
2848 case TOKfloat64v: | |
2849 case TOKfloat80v: | |
2850 case TOKimaginary32v: | |
2851 case TOKimaginary64v: | |
2852 case TOKimaginary80v: | |
2853 break; | |
2854 | |
2855 default: | |
2856 assert(0); | |
2857 } | |
2858 } | |
2859 body | |
2860 { | |
2861 int dblstate; | |
2862 uint c; | |
2863 char hex; // is this a hexadecimal-floating-constant? | |
2864 TOK result; | |
2865 | |
2866 //printf("Lexer.inreal()\n"); | |
2867 stringbuffer.reset(); | |
2868 dblstate = 0; | |
2869 hex = 0; | |
2870 Lnext: | |
2871 while (true) | |
2872 { | |
2873 // Get next char from input | |
2874 c = *p++; | |
2875 //printf("dblstate = %d, c = '%c'\n", dblstate, c); | |
2876 while (true) | |
2877 { | |
2878 switch (dblstate) | |
2879 { | |
2880 case 0: // opening state | |
2881 if (c == '0') | |
2882 dblstate = 9; | |
2883 else if (c == '.') | |
2884 dblstate = 3; | |
2885 else | |
2886 dblstate = 1; | |
2887 break; | |
2888 | |
2889 case 9: | |
2890 dblstate = 1; | |
2891 if (c == 'X' || c == 'x') | |
2892 { | |
2893 hex++; | |
2894 break; | |
2895 } | |
2896 case 1: // digits to left of . | |
2897 case 3: // digits to right of . | |
2898 case 7: // continuing exponent digits | |
2899 if (!isdigit(c) && !(hex && isxdigit(c))) | |
2900 { | |
2901 if (c == '_') | |
2902 goto Lnext; // ignore embedded '_' | |
2903 dblstate++; | |
2904 continue; | |
2905 } | |
2906 break; | |
2907 | |
2908 case 2: // no more digits to left of . | |
2909 if (c == '.') | |
2910 { | |
2911 dblstate++; | |
2912 break; | |
2913 } | |
2914 case 4: // no more digits to right of . | |
2915 if ((c == 'E' || c == 'e') || | |
2916 hex && (c == 'P' || c == 'p')) | |
2917 { | |
2918 dblstate = 5; | |
2919 hex = 0; // exponent is always decimal | |
2920 break; | |
2921 } | |
2922 if (hex) | |
2923 error("binary-exponent-part required"); | |
2924 goto done; | |
2925 | |
2926 case 5: // looking immediately to right of E | |
2927 dblstate++; | |
2928 if (c == '-' || c == '+') | |
2929 break; | |
2930 case 6: // 1st exponent digit expected | |
2931 if (!isdigit(c)) | |
2932 error("exponent expected"); | |
2933 dblstate++; | |
2934 break; | |
2935 | |
2936 case 8: // past end of exponent digits | |
2937 goto done; | |
79 | 2938 |
2939 default: | |
2940 assert(0, "inreal.dblstate has unexpected value"); | |
0 | 2941 } |
2942 break; | |
2943 } | |
2944 stringbuffer.writeByte(c); | |
2945 } | |
2946 done: | |
2947 p--; | |
2948 | |
2949 stringbuffer.writeByte(0); | |
2950 | |
114 | 2951 version (Windows) { /// && __DMC__ |
0 | 2952 char* save = __locale_decpoint; |
2953 __locale_decpoint = cast(char*)".".ptr; | |
2954 } | |
2955 t.float80value = strtold(cast(char*)stringbuffer.data, null); | |
2956 | |
2957 errno = 0; | |
2958 switch (*p) | |
2959 { | |
2960 case 'F': | |
2961 case 'f': | |
2962 strtof(cast(char*)stringbuffer.data, null); | |
2963 result = TOKfloat32v; | |
2964 p++; | |
2965 break; | |
2966 | |
2967 default: | |
2968 strtod(cast(char*)stringbuffer.data, null); | |
2969 result = TOKfloat64v; | |
2970 break; | |
2971 | |
2972 case 'l': | |
2973 if (!global.params.useDeprecated) | |
2974 error("'l' suffix is deprecated, use 'L' instead"); | |
2975 case 'L': | |
2976 result = TOKfloat80v; | |
2977 p++; | |
2978 break; | |
2979 } | |
2980 if (*p == 'i' || *p == 'I') | |
2981 { | |
2982 if (!global.params.useDeprecated && *p == 'I') | |
2983 error("'I' suffix is deprecated, use 'i' instead"); | |
2984 p++; | |
2985 switch (result) | |
2986 { | |
2987 case TOKfloat32v: | |
2988 result = TOKimaginary32v; | |
2989 break; | |
2990 case TOKfloat64v: | |
2991 result = TOKimaginary64v; | |
2992 break; | |
2993 case TOKfloat80v: | |
2994 result = TOKimaginary80v; | |
2995 break; | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
2996 default: |
0 | 2997 } |
2998 } | |
2999 | |
114 | 3000 version (Windows) { ///&& __DMC__ |
0 | 3001 __locale_decpoint = save; |
3002 } | |
3003 if (errno == ERANGE) | |
3004 error("number is not representable"); | |
3005 | |
3006 return result; | |
3007 } | |
3008 | |
3009 void error(T...)(string format, T t) | |
3010 { | |
3011 error(this.loc, format, t); | |
3012 } | |
3013 | |
3014 void error(T...)(Loc loc, string format, T t) | |
3015 { | |
3016 if (mod && !global.gag) | |
3017 { | |
3018 string p = loc.toChars(); | |
3019 if (p.length != 0) | |
3020 writef("%s: ", p); | |
3021 | |
3022 writefln(format, t); | |
3023 | |
3024 if (global.errors >= 20) // moderate blizzard of cascading messages | |
3025 fatal(); | |
3026 } | |
3027 | |
3028 global.errors++; | |
3029 } | |
3030 | |
162 | 3031 /********************************************* |
3032 * Do pragma. | |
3033 * Currently, the only pragma supported is: | |
3034 * #line linnum [filespec] | |
3035 */ | |
0 | 3036 void pragma_() |
3037 { | |
162 | 3038 Token tok; |
3039 int linnum; | |
3040 string filespec = null; | |
3041 Loc loc = this.loc; | |
3042 | |
3043 scan(&tok); | |
3044 if (tok.value != TOKidentifier || tok.ident != Id.line) | |
3045 goto Lerr; | |
3046 | |
3047 scan(&tok); | |
3048 if (tok.value == TOKint32v || tok.value == TOKint64v) | |
3049 linnum = cast(int)(tok.uns64value - 1); /// | |
3050 else | |
3051 goto Lerr; | |
3052 | |
3053 while (1) | |
3054 { | |
3055 switch (*p) | |
3056 { | |
3057 case 0: | |
3058 case 0x1A: | |
3059 case '\n': | |
3060 Lnewline: | |
3061 this.loc.linnum = linnum; | |
3062 if (filespec != null) | |
3063 this.loc.filename = filespec; | |
3064 return; | |
3065 | |
3066 case '\r': | |
3067 p++; | |
3068 if (*p != '\n') | |
3069 { p--; | |
3070 goto Lnewline; | |
3071 } | |
3072 continue; | |
3073 | |
3074 case ' ': | |
3075 case '\t': | |
3076 case '\v': | |
3077 case '\f': | |
3078 p++; | |
3079 continue; // skip white space | |
3080 | |
3081 case '_': | |
3082 if (mod && memcmp(p, "__FILE__".ptr, 8) == 0) | |
3083 { | |
3084 p += 8; | |
3085 filespec = (loc.filename ? loc.filename : mod.ident.toChars()); | |
3086 } | |
3087 continue; | |
3088 | |
3089 case '"': | |
3090 if (filespec) | |
3091 goto Lerr; | |
3092 stringbuffer.reset(); | |
3093 p++; | |
3094 while (1) | |
3095 { | |
3096 uint c; | |
3097 | |
3098 c = *p; | |
3099 switch (c) | |
3100 { | |
3101 case '\n': | |
3102 case '\r': | |
3103 case 0: | |
3104 case 0x1A: | |
3105 goto Lerr; | |
3106 | |
3107 case '"': | |
3108 stringbuffer.writeByte(0); | |
3109 filespec = stringbuffer.extractString(); /// | |
3110 p++; | |
3111 break; | |
3112 | |
3113 default: | |
3114 if (c & 0x80) | |
3115 { | |
3116 uint u = decodeUTF(); | |
3117 if (u == PS || u == LS) | |
3118 goto Lerr; | |
3119 } | |
3120 stringbuffer.writeByte(c); | |
3121 p++; | |
3122 continue; | |
3123 } | |
3124 break; | |
3125 } | |
3126 continue; | |
3127 | |
3128 default: | |
3129 if (*p & 0x80) | |
3130 { | |
3131 uint u = decodeUTF(); | |
3132 if (u == PS || u == LS) | |
3133 goto Lnewline; | |
3134 } | |
3135 goto Lerr; | |
3136 } | |
3137 } | |
3138 | |
3139 Lerr: | |
3140 error(loc, "#line integer [\"filespec\"]\\n expected"); | |
0 | 3141 } |
3142 | |
49 | 3143 /******************************************** |
3144 * Decode UTF character. | |
3145 * Issue error messages for invalid sequences. | |
3146 * Return decoded character, advance p to last character in UTF sequence. | |
3147 */ | |
0 | 3148 uint decodeUTF() |
3149 { | |
49 | 3150 dchar u; |
3151 ubyte c; | |
3152 ubyte* s = p; | |
3153 size_t len; | |
3154 size_t idx; | |
3155 string msg; | |
3156 | |
3157 c = *s; | |
3158 assert(c & 0x80); | |
3159 | |
3160 // Check length of remaining string up to 6 UTF-8 characters | |
3161 for (len = 1; len < 6 && s[len]; len++) { | |
3162 ; | |
3163 } | |
3164 | |
3165 idx = 0; | |
3166 msg = utf_decodeChar(cast(string)s[0..len], &idx, &u); | |
3167 p += idx - 1; | |
3168 if (msg) | |
3169 { | |
3170 error("%s", msg); | |
3171 } | |
3172 return u; | |
0 | 3173 } |
3174 | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3175 /*************************************************** |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3176 * Parse doc comment embedded between t.ptr and p. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3177 * Remove trailing blanks and tabs from lines. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3178 * Replace all newlines with \n. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3179 * Remove leading comment character from each line. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3180 * Decide if it's a lineComment or a blockComment. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3181 * Append to previous one for this token. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3182 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3183 void getDocComment(Token* t, uint lineComment) |
0 | 3184 { |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3185 /* ct tells us which kind of comment it is: '!', '/', '*', or '+' |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3186 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3187 ubyte ct = t.ptr[2]; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3188 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3189 /* Start of comment text skips over / * *, / + +, or / / / |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3190 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3191 ubyte* q = t.ptr + 3; // start of comment text |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3192 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3193 ubyte* qend = p; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3194 if (ct == '*' || ct == '+') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3195 qend -= 2; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3196 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3197 /* Scan over initial row of ****'s or ++++'s or ////'s |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3198 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3199 for (; q < qend; q++) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3200 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3201 if (*q != ct) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3202 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3203 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3204 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3205 /* Remove trailing row of ****'s or ++++'s |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3206 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3207 if (ct != '/' && ct != '!') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3208 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3209 for (; q < qend; qend--) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3210 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3211 if (qend[-1] != ct) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3212 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3213 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3214 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3215 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3216 /* Comment is now [q .. qend]. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3217 * Canonicalize it into buf[]. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3218 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3219 OutBuffer buf = new OutBuffer; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3220 int linestart = 0; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3221 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3222 for (; q < qend; q++) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3223 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3224 ubyte c = *q; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3225 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3226 switch (c) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3227 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3228 case '*': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3229 case '+': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3230 if (linestart && c == ct) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3231 { linestart = 0; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3232 /* Trim preceding whitespace up to preceding \n |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3233 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3234 while (buf.offset && (buf.data[buf.offset - 1] == ' ' || buf.data[buf.offset - 1] == '\t')) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3235 buf.offset--; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3236 continue; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3237 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3238 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3239 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3240 case ' ': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3241 case '\t': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3242 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3243 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3244 case '\r': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3245 if (q[1] == '\n') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3246 continue; // skip the \r |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3247 goto Lnewline; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3248 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3249 default: |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3250 if (c == 226) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3251 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3252 // If LS or PS |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3253 if (q[1] == 128 && |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3254 (q[2] == 168 || q[2] == 169)) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3255 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3256 q += 2; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3257 goto Lnewline; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3258 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3259 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3260 linestart = 0; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3261 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3262 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3263 Lnewline: |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3264 c = '\n'; // replace all newlines with \n |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3265 case '\n': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3266 linestart = 1; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3267 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3268 /* Trim trailing whitespace |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3269 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3270 while (buf.offset && (buf.data[buf.offset - 1] == ' ' || buf.data[buf.offset - 1] == '\t')) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3271 buf.offset--; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3272 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3273 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3274 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3275 buf.writeByte(c); |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3276 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3277 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3278 // Always end with a newline |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3279 if (!buf.offset || buf.data[buf.offset - 1] != '\n') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3280 buf.writeByte('\n'); |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3281 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3282 buf.writeByte(0); |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3283 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3284 // It's a line comment if the start of the doc comment comes |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3285 // after other non-whitespace on the same line. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3286 string* dc = (lineComment && anyToken) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3287 ? &t.lineComment |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3288 : &t.blockComment; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3289 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3290 // Combine with previous doc comment, if any |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3291 if (*dc) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3292 *dc = combineComments(*dc, cast(string) buf.data[0 .. buf.size]); // TODO: utf decode etc? |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3293 else |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3294 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3295 auto bufsize = buf.size; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3296 *dc = cast(string) buf.extractData()[0..bufsize]; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3297 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3298 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3299 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3300 /******************************************** |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3301 * Combine two document comments into one, |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3302 * separated by a newline. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3303 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3304 static string combineComments(string c1, string c2) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3305 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3306 //printf("Lexer::combineComments('%s', '%s')\n", c1, c2); |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3307 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3308 string c = c2; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3309 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3310 if (c1) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3311 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3312 c = c1; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3313 if (c2) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3314 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3315 size_t len1 = c1.length; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3316 size_t len2 = c2.length; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3317 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3318 c = c1.idup; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3319 if (len1 && c1[$-1] != '\n') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3320 c ~= '\n'; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3321 c ~= c2; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3322 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3323 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3324 return c; |
0 | 3325 } |
3326 | |
3327 static bool isValidIdentifier(string p) | |
3328 { | |
3329 if (p.length == 0) { | |
3330 return false; | |
3331 } | |
3332 | |
3333 if (p[0] >= '0' && p[0] <= '9') { // beware of isdigit() on signed chars | |
3334 return false; | |
3335 } | |
3336 | |
3337 size_t idx = 0; | |
3338 while (idx < p.length) | |
3339 { | |
3340 dchar dc; | |
3341 | |
3342 if (utf_decodeChar(p, &idx, &dc) !is null) { | |
3343 return false; | |
3344 } | |
3345 | |
3346 if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_')) { | |
3347 return false; | |
3348 } | |
3349 } | |
3350 | |
3351 return true; | |
3352 } | |
3353 | |
79 | 3354 /// TODO: use normal string append when GC works |
3355 static string combineComments(const(char)[] c1, const(char)[] c2) | |
0 | 3356 { |
79 | 3357 //writef("Lexer.combineComments('%s', '%s')\n", c1, c2); |
0 | 3358 |
79 | 3359 char[] c = cast(char[]) c2; |
3360 | |
3361 if (c1 !is null) | |
0 | 3362 { |
79 | 3363 c = cast(char[]) c1; |
3364 if (c2 !is null) | |
0 | 3365 { |
79 | 3366 c = cast(char[]) (GC.malloc(c1.length + 1 + c2.length)[0 .. c1.length + 1 + c2.length]); |
3367 size_t len1 = c1.length; | |
3368 c[0..len1] = c1[]; | |
3369 c[len1++] = '\n'; | |
3370 c[len1 .. len1 + c2.length] = c2[]; | |
0 | 3371 } |
3372 } | |
79 | 3373 |
3374 return cast(string)c; | |
0 | 3375 } |
3376 } |