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