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