Mercurial > projects > ddmd
annotate dmd/Lexer.d @ 162:438eaa11eed4
updated build script to use dmd2.039
some missing methods implemented
author | korDen |
---|---|
date | Tue, 21 Sep 2010 14:59:56 +0400 |
parents | b7b61140701d |
children | fe932c1a9563 |
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 | |
887 TOK peekNext2() | |
888 { | |
889 assert(false); | |
890 } | |
891 | |
892 void scan(Token* t) | |
893 { | |
894 uint lastLine = loc.linnum; | |
895 uint linnum; | |
896 | |
897 t.blockComment = null; | |
898 t.lineComment = null; | |
899 while (1) | |
900 { | |
901 t.ptr = p; | |
902 //printf("p = %p, *p = '%c'\n",p,*p); | |
903 switch (*p) | |
904 { | |
905 case 0: | |
906 case 0x1A: | |
907 t.value = TOK.TOKeof; // end of file | |
908 return; | |
909 | |
910 case ' ': | |
911 case '\t': | |
912 case '\v': | |
913 case '\f': | |
914 p++; | |
915 continue; // skip white space | |
916 | |
917 case '\r': | |
918 p++; | |
919 if (*p != '\n') // if CR stands by itself | |
920 loc.linnum++; | |
921 continue; // skip white space | |
922 | |
923 case '\n': | |
924 p++; | |
925 loc.linnum++; | |
926 continue; // skip white space | |
927 | |
928 case '0': case '1': case '2': case '3': case '4': | |
929 case '5': case '6': case '7': case '8': case '9': | |
930 t.value = number(t); | |
931 return; | |
932 | |
933 version (CSTRINGS) { | |
934 case '\'': | |
935 t.value = charConstant(t, 0); | |
936 return; | |
937 | |
938 case '"': | |
939 t.value = stringConstant(t,0); | |
940 return; | |
941 | |
942 case 'l': | |
943 case 'L': | |
944 if (p[1] == '\'') | |
945 { | |
946 p++; | |
947 t.value = charConstant(t, 1); | |
948 return; | |
949 } | |
950 else if (p[1] == '"') | |
951 { | |
952 p++; | |
953 t.value = stringConstant(t, 1); | |
954 return; | |
955 } | |
956 } else { | |
957 case '\'': | |
958 t.value = charConstant(t,0); | |
959 return; | |
960 | |
961 case 'r': | |
962 if (p[1] != '"') | |
963 goto case_ident; | |
964 p++; | |
965 case '`': | |
966 t.value = wysiwygStringConstant(t, *p); | |
967 return; | |
968 | |
969 case 'x': | |
970 if (p[1] != '"') | |
971 goto case_ident; | |
972 p++; | |
973 t.value = hexStringConstant(t); | |
974 return; | |
975 | |
976 version (DMDV2) { | |
977 case 'q': | |
978 if (p[1] == '"') | |
979 { | |
980 p++; | |
981 t.value = delimitedStringConstant(t); | |
982 return; | |
983 } | |
984 else if (p[1] == '{') | |
985 { | |
986 p++; | |
987 t.value = tokenStringConstant(t); | |
988 return; | |
989 } | |
990 else | |
991 goto case_ident; | |
992 } | |
993 | |
994 case '"': | |
995 t.value = escapeStringConstant(t,0); | |
996 return; | |
997 version (TEXTUAL_ASSEMBLY_OUT) { | |
998 } else { | |
999 case '\\': // escaped string literal | |
1000 { uint c; | |
1001 ubyte* pstart = p; | |
1002 | |
1003 stringbuffer.reset(); | |
1004 do | |
1005 { | |
1006 p++; | |
1007 switch (*p) | |
1008 { | |
1009 case 'u': | |
1010 case 'U': | |
1011 case '&': | |
1012 c = escapeSequence(); | |
1013 stringbuffer.writeUTF8(c); | |
1014 break; | |
1015 | |
1016 default: | |
1017 c = escapeSequence(); | |
1018 stringbuffer.writeByte(c); | |
1019 break; | |
1020 } | |
1021 } while (*p == '\\'); | |
1022 t.len = stringbuffer.offset; | |
1023 stringbuffer.writeByte(0); | |
2 | 1024 char* cc = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 1025 memcpy(cc, stringbuffer.data, stringbuffer.offset); |
1026 t.ustring = cc; | |
1027 t.postfix = 0; | |
1028 t.value = TOK.TOKstring; | |
1029 if (!global.params.useDeprecated) | |
1030 error("Escape String literal %.*s is deprecated, use double quoted string literal \"%.*s\" instead", p - pstart, pstart, p - pstart, pstart); | |
1031 return; | |
1032 } | |
1033 } | |
1034 case 'l': | |
1035 case 'L': | |
1036 } | |
1037 case 'a': case 'b': case 'c': case 'd': case 'e': | |
1038 case 'f': case 'g': case 'h': case 'i': case 'j': | |
1039 case 'k': case 'm': case 'n': case 'o': | |
1040 version (DMDV2) { | |
1041 case 'p': /*case 'q': case 'r':*/ case 's': case 't': | |
1042 } else { | |
1043 case 'p': case 'q': /*case 'r':*/ case 's': case 't': | |
1044 } | |
1045 case 'u': case 'v': case 'w': /*case 'x':*/ case 'y': | |
1046 case 'z': | |
1047 case 'A': case 'B': case 'C': case 'D': case 'E': | |
1048 case 'F': case 'G': case 'H': case 'I': case 'J': | |
1049 case 'K': case 'M': case 'N': case 'O': | |
1050 case 'P': case 'Q': case 'R': case 'S': case 'T': | |
1051 case 'U': case 'V': case 'W': case 'X': case 'Y': | |
1052 case 'Z': | |
1053 case '_': | |
1054 case_ident: | |
135 | 1055 { |
1056 ubyte c; | |
0 | 1057 |
135 | 1058 while (1) |
1059 { | |
1060 c = *++p; | |
1061 if (isidchar(c)) | |
1062 continue; | |
1063 else if (c & 0x80) | |
1064 { | |
1065 ubyte *s = p; | |
1066 uint u = decodeUTF(); | |
1067 if (isUniAlpha(u)) | |
1068 continue; | |
1069 error("char 0x%04x not allowed in identifier", u); | |
1070 p = s; | |
1071 } | |
1072 break; | |
1073 } | |
1074 | |
1075 StringValue *sv = stringtable.update((cast(immutable(char)*)t.ptr)[0.. p - t.ptr]); | |
1076 Identifier id = cast(Identifier) sv.ptrvalue; | |
1077 | |
0 | 1078 if (id is null) |
1079 { id = new Identifier(sv.lstring.string_, TOK.TOKidentifier); | |
1080 sv.ptrvalue = cast(void*)id; | |
1081 } | |
1082 t.ident = id; | |
1083 t.value = cast(TOK) id.value; | |
1084 anyToken = 1; | |
1085 if (*t.ptr == '_') // if special identifier token | |
1086 { | |
1087 static char date[11+1]; | |
1088 static char time[8+1]; | |
1089 static char timestamp[24+1]; | |
1090 | |
1091 if (!date[0]) // lazy evaluation | |
1092 { time_t tm; | |
1093 char *p; | |
1094 | |
1095 .time(&tm); | |
1096 p = ctime(&tm); | |
1097 assert(p); | |
1098 sprintf(date.ptr, "%.6s %.4s", p + 4, p + 20); | |
1099 sprintf(time.ptr, "%.8s", p + 11); | |
1100 sprintf(timestamp.ptr, "%.24s", p); | |
1101 } | |
1102 | |
1103 ///version (DMDV1) { | |
1104 /// if (mod && id == Id.FILE) | |
1105 /// { | |
1106 /// t.ustring = cast(ubyte*)(loc.filename ? loc.filename : mod.ident.toChars()); | |
1107 /// goto Lstr; | |
1108 /// } | |
1109 /// else if (mod && id == Id.LINE) | |
1110 /// { | |
1111 /// t.value = TOK.TOKint64v; | |
1112 /// t.uns64value = loc.linnum; | |
1113 /// } | |
1114 /// else | |
1115 ///} | |
1116 if (id == Id.DATE) | |
1117 { | |
1118 t.ustring = date.ptr; | |
1119 goto Lstr; | |
1120 } | |
1121 else if (id == Id.TIME) | |
1122 { | |
1123 t.ustring = time.ptr; | |
1124 goto Lstr; | |
1125 } | |
1126 else if (id == Id.VENDOR) | |
1127 { | |
1128 t.ustring = "Digital Mars D".ptr; | |
1129 goto Lstr; | |
1130 } | |
1131 else if (id == Id.TIMESTAMP) | |
1132 { | |
1133 t.ustring = timestamp.ptr; | |
1134 Lstr: | |
1135 t.value = TOK.TOKstring; | |
1136 Llen: | |
1137 t.postfix = 0; | |
1138 t.len = strlen(cast(char*)t.ustring); | |
1139 } | |
1140 else if (id == Id.VERSIONX) | |
1141 { | |
1142 uint major = 0; | |
1143 uint minor = 0; | |
1144 | |
1145 foreach (char cc; global.version_[1..$]) | |
1146 { | |
1147 if (isdigit(cc)) | |
1148 minor = minor * 10 + cc - '0'; | |
1149 else if (cc == '.') | |
1150 { | |
1151 major = minor; | |
1152 minor = 0; | |
1153 } | |
1154 else | |
1155 break; | |
1156 } | |
1157 t.value = TOK.TOKint64v; | |
1158 t.uns64value = major * 1000 + minor; | |
1159 } | |
1160 ///version (DMDV2) { | |
1161 else if (id == Id.EOFX) | |
1162 { | |
1163 t.value = TOK.TOKeof; | |
1164 // Advance scanner to end of file | |
1165 while (!(*p == 0 || *p == 0x1A)) | |
1166 p++; | |
1167 } | |
1168 ///} | |
1169 } | |
1170 //printf("t.value = %d\n",t.value); | |
1171 return; | |
1172 } | |
1173 | |
1174 case '/': | |
1175 p++; | |
1176 switch (*p) | |
1177 { | |
1178 case '=': | |
1179 p++; | |
1180 t.value = TOK.TOKdivass; | |
1181 return; | |
1182 | |
1183 case '*': | |
1184 p++; | |
1185 linnum = loc.linnum; | |
1186 while (1) | |
1187 { | |
1188 while (1) | |
1189 { | |
1190 ubyte c = *p; | |
1191 switch (c) | |
1192 { | |
1193 case '/': | |
1194 break; | |
1195 | |
1196 case '\n': | |
1197 loc.linnum++; | |
1198 p++; | |
1199 continue; | |
1200 | |
1201 case '\r': | |
1202 p++; | |
1203 if (*p != '\n') | |
1204 loc.linnum++; | |
1205 continue; | |
1206 | |
1207 case 0: | |
1208 case 0x1A: | |
1209 error("unterminated /* */ comment"); | |
1210 p = end; | |
1211 t.value = TOK.TOKeof; | |
1212 return; | |
1213 | |
1214 default: | |
1215 if (c & 0x80) | |
1216 { uint u = decodeUTF(); | |
1217 if (u == PS || u == LS) | |
1218 loc.linnum++; | |
1219 } | |
1220 p++; | |
1221 continue; | |
1222 } | |
1223 break; | |
1224 } | |
1225 p++; | |
1226 if (p[-2] == '*' && p - 3 != t.ptr) | |
1227 break; | |
1228 } | |
1229 if (commentToken) | |
1230 { | |
1231 t.value = TOK.TOKcomment; | |
1232 return; | |
1233 } | |
1234 else if (doDocComment && t.ptr[2] == '*' && p - 4 != t.ptr) | |
1235 { // if /** but not /**/ | |
1236 getDocComment(t, lastLine == linnum); | |
1237 } | |
1238 continue; | |
1239 | |
1240 case '/': // do // style comments | |
1241 linnum = loc.linnum; | |
1242 while (1) | |
1243 { ubyte c = *++p; | |
1244 switch (c) | |
1245 { | |
1246 case '\n': | |
1247 break; | |
1248 | |
1249 case '\r': | |
1250 if (p[1] == '\n') | |
1251 p++; | |
1252 break; | |
1253 | |
1254 case 0: | |
1255 case 0x1A: | |
1256 if (commentToken) | |
1257 { | |
1258 p = end; | |
1259 t.value = TOK.TOKcomment; | |
1260 return; | |
1261 } | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
1262 if (doDocComment && t.ptr[2] == '/' || t.ptr[2] == '!') // '///' or '//!' |
0 | 1263 getDocComment(t, lastLine == linnum); |
1264 p = end; | |
1265 t.value = TOK.TOKeof; | |
1266 return; | |
1267 | |
1268 default: | |
1269 if (c & 0x80) | |
1270 { uint u = decodeUTF(); | |
1271 if (u == PS || u == LS) | |
1272 break; | |
1273 } | |
1274 continue; | |
1275 } | |
1276 break; | |
1277 } | |
1278 | |
1279 if (commentToken) | |
1280 { | |
1281 p++; | |
1282 loc.linnum++; | |
1283 t.value = TOK.TOKcomment; | |
1284 return; | |
1285 } | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
1286 if (doDocComment && t.ptr[2] == '/' || t.ptr[2] == '!') // '///' or '//!' |
0 | 1287 getDocComment(t, lastLine == linnum); |
1288 | |
1289 p++; | |
1290 loc.linnum++; | |
1291 continue; | |
1292 | |
1293 case '+': | |
1294 { | |
1295 int nest; | |
1296 | |
1297 linnum = loc.linnum; | |
1298 p++; | |
1299 nest = 1; | |
1300 while (1) | |
1301 { ubyte c = *p; | |
1302 switch (c) | |
1303 { | |
1304 case '/': | |
1305 p++; | |
1306 if (*p == '+') | |
1307 { | |
1308 p++; | |
1309 nest++; | |
1310 } | |
1311 continue; | |
1312 | |
1313 case '+': | |
1314 p++; | |
1315 if (*p == '/') | |
1316 { | |
1317 p++; | |
1318 if (--nest == 0) | |
1319 break; | |
1320 } | |
1321 continue; | |
1322 | |
1323 case '\r': | |
1324 p++; | |
1325 if (*p != '\n') | |
1326 loc.linnum++; | |
1327 continue; | |
1328 | |
1329 case '\n': | |
1330 loc.linnum++; | |
1331 p++; | |
1332 continue; | |
1333 | |
1334 case 0: | |
1335 case 0x1A: | |
1336 error("unterminated /+ +/ comment"); | |
1337 p = end; | |
1338 t.value = TOK.TOKeof; | |
1339 return; | |
1340 | |
1341 default: | |
1342 if (c & 0x80) | |
1343 { uint u = decodeUTF(); | |
1344 if (u == PS || u == LS) | |
1345 loc.linnum++; | |
1346 } | |
1347 p++; | |
1348 continue; | |
1349 } | |
1350 break; | |
1351 } | |
1352 if (commentToken) | |
1353 { | |
1354 t.value = TOK.TOKcomment; | |
1355 return; | |
1356 } | |
1357 if (doDocComment && t.ptr[2] == '+' && p - 4 != t.ptr) | |
1358 { // if /++ but not /++/ | |
1359 getDocComment(t, lastLine == linnum); | |
1360 } | |
1361 continue; | |
1362 } | |
1363 | |
1364 default: | |
1365 break; /// | |
1366 } | |
1367 t.value = TOK.TOKdiv; | |
1368 return; | |
1369 | |
1370 case '.': | |
1371 p++; | |
1372 if (isdigit(*p)) | |
1373 { /* Note that we don't allow ._1 and ._ as being | |
1374 * valid floating point numbers. | |
1375 */ | |
1376 p--; | |
1377 t.value = inreal(t); | |
1378 } | |
1379 else if (p[0] == '.') | |
1380 { | |
1381 if (p[1] == '.') | |
1382 { p += 2; | |
1383 t.value = TOK.TOKdotdotdot; | |
1384 } | |
1385 else | |
1386 { p++; | |
1387 t.value = TOK.TOKslice; | |
1388 } | |
1389 } | |
1390 else | |
1391 t.value = TOK.TOKdot; | |
1392 return; | |
1393 | |
1394 case '&': | |
1395 p++; | |
1396 if (*p == '=') | |
1397 { p++; | |
1398 t.value = TOK.TOKandass; | |
1399 } | |
1400 else if (*p == '&') | |
1401 { p++; | |
1402 t.value = TOK.TOKandand; | |
1403 } | |
1404 else | |
1405 t.value = TOK.TOKand; | |
1406 return; | |
1407 | |
1408 case '|': | |
1409 p++; | |
1410 if (*p == '=') | |
1411 { p++; | |
1412 t.value = TOK.TOKorass; | |
1413 } | |
1414 else if (*p == '|') | |
1415 { p++; | |
1416 t.value = TOK.TOKoror; | |
1417 } | |
1418 else | |
1419 t.value = TOK.TOKor; | |
1420 return; | |
1421 | |
1422 case '-': | |
1423 p++; | |
1424 if (*p == '=') | |
1425 { p++; | |
1426 t.value = TOK.TOKminass; | |
1427 } | |
1428 /// #if 0 | |
1429 /// else if (*p == '>') | |
1430 /// { p++; | |
1431 /// t.value = TOK.TOKarrow; | |
1432 /// } | |
1433 /// #endif | |
1434 else if (*p == '-') | |
1435 { p++; | |
1436 t.value = TOK.TOKminusminus; | |
1437 } | |
1438 else | |
1439 t.value = TOK.TOKmin; | |
1440 return; | |
1441 | |
1442 case '+': | |
1443 p++; | |
1444 if (*p == '=') | |
1445 { p++; | |
1446 t.value = TOK.TOKaddass; | |
1447 } | |
1448 else if (*p == '+') | |
1449 { p++; | |
1450 t.value = TOK.TOKplusplus; | |
1451 } | |
1452 else | |
1453 t.value = TOK.TOKadd; | |
1454 return; | |
1455 | |
1456 case '<': | |
1457 p++; | |
1458 if (*p == '=') | |
1459 { p++; | |
1460 t.value = TOK.TOKle; // <= | |
1461 } | |
1462 else if (*p == '<') | |
1463 { p++; | |
1464 if (*p == '=') | |
1465 { p++; | |
1466 t.value = TOK.TOKshlass; // <<= | |
1467 } | |
1468 else | |
1469 t.value = TOK.TOKshl; // << | |
1470 } | |
1471 else if (*p == '>') | |
1472 { p++; | |
1473 if (*p == '=') | |
1474 { p++; | |
1475 t.value = TOK.TOKleg; // <>= | |
1476 } | |
1477 else | |
1478 t.value = TOK.TOKlg; // <> | |
1479 } | |
1480 else | |
1481 t.value = TOK.TOKlt; // < | |
1482 return; | |
1483 | |
1484 case '>': | |
1485 p++; | |
1486 if (*p == '=') | |
1487 { p++; | |
1488 t.value = TOK.TOKge; // >= | |
1489 } | |
1490 else if (*p == '>') | |
1491 { p++; | |
1492 if (*p == '=') | |
1493 { p++; | |
1494 t.value = TOK.TOKshrass; // >>= | |
1495 } | |
1496 else if (*p == '>') | |
1497 { p++; | |
1498 if (*p == '=') | |
1499 { p++; | |
1500 t.value = TOK.TOKushrass; // >>>= | |
1501 } | |
1502 else | |
1503 t.value = TOK.TOKushr; // >>> | |
1504 } | |
1505 else | |
1506 t.value = TOK.TOKshr; // >> | |
1507 } | |
1508 else | |
1509 t.value = TOK.TOKgt; // > | |
1510 return; | |
1511 | |
1512 case '!': | |
1513 p++; | |
1514 if (*p == '=') | |
1515 { p++; | |
1516 if (*p == '=' && global.params.Dversion == 1) | |
1517 { p++; | |
1518 t.value = TOK.TOKnotidentity; // !== | |
1519 } | |
1520 else | |
1521 t.value = TOK.TOKnotequal; // != | |
1522 } | |
1523 else if (*p == '<') | |
1524 { p++; | |
1525 if (*p == '>') | |
1526 { p++; | |
1527 if (*p == '=') | |
1528 { p++; | |
1529 t.value = TOK.TOKunord; // !<>= | |
1530 } | |
1531 else | |
1532 t.value = TOK.TOKue; // !<> | |
1533 } | |
1534 else if (*p == '=') | |
1535 { p++; | |
1536 t.value = TOK.TOKug; // !<= | |
1537 } | |
1538 else | |
1539 t.value = TOK.TOKuge; // !< | |
1540 } | |
1541 else if (*p == '>') | |
1542 { p++; | |
1543 if (*p == '=') | |
1544 { p++; | |
1545 t.value = TOK.TOKul; // !>= | |
1546 } | |
1547 else | |
1548 t.value = TOK.TOKule; // !> | |
1549 } | |
1550 else | |
1551 t.value = TOK.TOKnot; // ! | |
1552 return; | |
1553 | |
1554 case '=': | |
1555 p++; | |
1556 if (*p == '=') | |
1557 { p++; | |
1558 if (*p == '=' && global.params.Dversion == 1) | |
1559 { p++; | |
1560 t.value = TOK.TOKidentity; // === | |
1561 } | |
1562 else | |
1563 t.value = TOK.TOKequal; // == | |
1564 } | |
1565 else | |
1566 t.value = TOK.TOKassign; // = | |
1567 return; | |
1568 | |
1569 case '~': | |
1570 p++; | |
1571 if (*p == '=') | |
1572 { p++; | |
1573 t.value = TOK.TOKcatass; // ~= | |
1574 } | |
1575 else | |
1576 t.value = TOK.TOKtilde; // ~ | |
1577 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1578 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1579 version(DMDV2) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1580 case '^': |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1581 p++; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1582 if (*p == '^') |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1583 { p++; |
135 | 1584 if (*p == '=') |
1585 { p++; | |
1586 t.value = TOKpowass; // ^^= | |
1587 } | |
1588 else | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1589 t.value = TOKpow; // ^^ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1590 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1591 else if (*p == '=') |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1592 { p++; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1593 t.value = TOKxorass; // ^= |
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 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1596 t.value = TOKxor; // ^ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1597 return; |
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 |
0 | 1600 /* |
1601 #define SINGLE(c,tok) case c: p++; t.value = tok; return; | |
1602 | |
1603 SINGLE('(', TOKlparen) | |
1604 SINGLE(')', TOKrparen) | |
1605 SINGLE('[', TOKlbracket) | |
1606 SINGLE(']', TOKrbracket) | |
1607 SINGLE('{', TOKlcurly) | |
1608 SINGLE('}', TOKrcurly) | |
1609 SINGLE('?', TOKquestion) | |
1610 SINGLE(',', TOKcomma) | |
1611 SINGLE(';', TOKsemicolon) | |
1612 SINGLE(':', TOKcolon) | |
1613 SINGLE('$', TOKdollar) | |
1614 SINGLE('@', TOKat) | |
1615 | |
1616 #undef SINGLE | |
1617 | |
1618 #define DOUBLE(c1,tok1,c2,tok2) \ | |
1619 case c1: \ | |
1620 p++; \ | |
1621 if (*p == c2) \ | |
1622 { p++; \ | |
1623 t.value = tok2; \ | |
1624 } \ | |
1625 else \ | |
1626 t.value = tok1; \ | |
1627 return; | |
1628 | |
1629 DOUBLE('*', TOKmul, '=', TOKmulass) | |
1630 DOUBLE('%', TOKmod, '=', TOKmodass) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1631 #if DMDV1 |
0 | 1632 DOUBLE('^', TOKxor, '=', TOKxorass) |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1633 #endif |
0 | 1634 #undef DOUBLE |
1635 */ | |
1636 | |
1637 case '(': p++; t.value = TOK.TOKlparen; return; | |
1638 case ')': p++; t.value = TOK.TOKrparen; return; | |
1639 case '[': p++; t.value = TOK.TOKlbracket; return; | |
1640 case ']': p++; t.value = TOK.TOKrbracket; return; | |
1641 case '{': p++; t.value = TOK.TOKlcurly; return; | |
1642 case '}': p++; t.value = TOK.TOKrcurly; return; | |
1643 case '?': p++; t.value = TOK.TOKquestion; return; | |
1644 case ',': p++; t.value = TOK.TOKcomma; return; | |
1645 case ';': p++; t.value = TOK.TOKsemicolon; return; | |
1646 case ':': p++; t.value = TOK.TOKcolon; return; | |
1647 case '$': p++; t.value = TOK.TOKdollar; return; | |
1648 case '@': p++; t.value = TOK.TOKat; return; | |
1649 | |
1650 case '*': | |
1651 p++; | |
1652 if (*p == '=') { | |
1653 p++; | |
1654 t.value = TOK.TOKmulass; | |
1655 } else { | |
1656 t.value = TOK.TOKmul; | |
1657 } | |
1658 return; | |
1659 | |
1660 case '%': | |
1661 p++; | |
1662 if (*p == '=') { | |
1663 p++; | |
1664 t.value = TOK.TOKmodass; | |
1665 } else { | |
1666 t.value = TOK.TOKmod; | |
1667 } | |
1668 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1669 version(DMDV1) { |
0 | 1670 case '^': |
1671 p++; | |
1672 if (*p == '=') { | |
1673 p++; | |
1674 t.value = TOK.TOKxorass; | |
1675 } else { | |
1676 t.value = TOK.TOKxor; | |
1677 } | |
1678 return; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1679 } |
0 | 1680 case '#': |
1681 p++; | |
1682 pragma_(); | |
1683 continue; | |
1684 | |
1685 default: | |
135 | 1686 { uint c = *p; |
0 | 1687 |
1688 if (c & 0x80) | |
135 | 1689 { c = decodeUTF(); |
0 | 1690 |
1691 // Check for start of unicode identifier | |
135 | 1692 if (isUniAlpha(c)) |
0 | 1693 goto case_ident; |
1694 | |
135 | 1695 if (c == PS || c == LS) |
0 | 1696 { |
1697 loc.linnum++; | |
1698 p++; | |
1699 continue; | |
1700 } | |
1701 } | |
135 | 1702 if (c < 0x80 && isprint(c)) |
0 | 1703 error("unsupported char '%c'", c); |
1704 else | |
1705 error("unsupported char 0x%02x", c); | |
1706 p++; | |
1707 continue; | |
1708 } | |
1709 } | |
1710 } | |
1711 } | |
1712 | |
1713 Token* peek(Token* ct) | |
1714 { | |
1715 Token* t; | |
1716 | |
1717 if (ct.next) | |
1718 t = ct.next; | |
1719 else | |
1720 { | |
1721 t = new Token(); | |
1722 scan(t); | |
1723 t.next = null; | |
1724 ct.next = t; | |
1725 } | |
1726 return t; | |
1727 } | |
1728 | |
1729 Token* peekPastParen(Token* tk) | |
1730 { | |
1731 //printf("peekPastParen()\n"); | |
1732 int parens = 1; | |
1733 int curlynest = 0; | |
1734 while (1) | |
1735 { | |
1736 tk = peek(tk); | |
1737 //tk.print(); | |
1738 switch (tk.value) | |
1739 { | |
1740 case TOK.TOKlparen: | |
1741 parens++; | |
1742 continue; | |
1743 | |
1744 case TOK.TOKrparen: | |
1745 --parens; | |
1746 if (parens) | |
1747 continue; | |
1748 tk = peek(tk); | |
1749 break; | |
1750 | |
1751 case TOK.TOKlcurly: | |
1752 curlynest++; | |
1753 continue; | |
1754 | |
1755 case TOK.TOKrcurly: | |
1756 if (--curlynest >= 0) | |
1757 continue; | |
1758 break; | |
1759 | |
1760 case TOK.TOKsemicolon: | |
1761 if (curlynest) | |
1762 continue; | |
1763 break; | |
1764 | |
1765 case TOK.TOKeof: | |
1766 break; | |
1767 | |
1768 default: | |
1769 continue; | |
1770 } | |
1771 return tk; | |
1772 } | |
1773 } | |
1774 | |
1775 /******************************************* | |
1776 * Parse escape sequence. | |
1777 */ | |
1778 uint escapeSequence() | |
1779 { | |
1780 uint c = *p; | |
1781 | |
1782 version (TEXTUAL_ASSEMBLY_OUT) { | |
1783 return c; | |
1784 } | |
1785 int n; | |
1786 int ndigits; | |
1787 | |
1788 switch (c) | |
1789 { | |
1790 case '\'': | |
1791 case '"': | |
1792 case '?': | |
1793 case '\\': | |
1794 Lconsume: | |
1795 p++; | |
1796 break; | |
1797 | |
1798 case 'a': c = 7; goto Lconsume; | |
1799 case 'b': c = 8; goto Lconsume; | |
1800 case 'f': c = 12; goto Lconsume; | |
1801 case 'n': c = 10; goto Lconsume; | |
1802 case 'r': c = 13; goto Lconsume; | |
1803 case 't': c = 9; goto Lconsume; | |
1804 case 'v': c = 11; goto Lconsume; | |
1805 | |
1806 case 'u': | |
1807 ndigits = 4; | |
1808 goto Lhex; | |
1809 case 'U': | |
1810 ndigits = 8; | |
1811 goto Lhex; | |
1812 case 'x': | |
1813 ndigits = 2; | |
1814 Lhex: | |
1815 p++; | |
1816 c = *p; | |
1817 if (ishex(cast(ubyte)c)) | |
1818 { | |
1819 uint v; | |
1820 | |
1821 n = 0; | |
1822 v = 0; | |
1823 while (1) | |
1824 { | |
1825 if (isdigit(c)) | |
1826 c -= '0'; | |
1827 else if (islower(c)) | |
1828 c -= 'a' - 10; | |
1829 else | |
1830 c -= 'A' - 10; | |
1831 v = v * 16 + c; | |
1832 c = *++p; | |
1833 if (++n == ndigits) | |
1834 break; | |
1835 if (!ishex(cast(ubyte)c)) | |
1836 { error("escape hex sequence has %d hex digits instead of %d", n, ndigits); | |
1837 break; | |
1838 } | |
1839 } | |
1840 if (ndigits != 2 && !utf_isValidDchar(v)) | |
1841 { error("invalid UTF character \\U%08x", v); | |
1842 v = '?'; // recover with valid UTF character | |
1843 } | |
1844 c = v; | |
1845 } | |
1846 else | |
1847 error("undefined escape hex sequence \\%c\n",c); | |
1848 break; | |
1849 | |
1850 case '&': // named character entity | |
1851 for (ubyte* idstart = ++p; true; p++) | |
1852 { | |
1853 switch (*p) | |
1854 { | |
1855 case ';': | |
1856 c = HtmlNamedEntity(idstart, p - idstart); | |
1857 if (c == ~0) | |
1858 { | |
1859 error("unnamed character entity &%s;", idstart[0..(p - idstart)]); | |
1860 c = ' '; | |
1861 } | |
1862 p++; | |
1863 break; | |
1864 | |
1865 default: | |
1866 if (isalpha(*p) || | |
1867 (p != idstart + 1 && isdigit(*p))) | |
1868 continue; | |
1869 error("unterminated named entity"); | |
1870 break; | |
1871 } | |
1872 break; | |
1873 } | |
1874 break; | |
1875 | |
1876 case 0: | |
1877 case 0x1A: // end of file | |
1878 c = '\\'; | |
1879 break; | |
1880 | |
1881 default: | |
1882 if (isoctal(cast(ubyte)c)) | |
1883 { | |
1884 uint v; | |
1885 | |
1886 n = 0; | |
1887 v = 0; | |
1888 do | |
1889 { | |
1890 v = v * 8 + (c - '0'); | |
1891 c = *++p; | |
1892 } while (++n < 3 && isoctal(cast(ubyte)c)); | |
1893 c = v; | |
1894 if (c > 0xFF) | |
1895 error("0%03o is larger than a byte", c); | |
1896 } | |
1897 else | |
1898 error("undefined escape sequence \\%c\n",c); | |
1899 break; | |
1900 } | |
1901 return c; | |
1902 } | |
1903 | |
1904 TOK wysiwygStringConstant(Token* t, int tc) | |
1905 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1906 uint c; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1907 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1908 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1909 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1910 stringbuffer.reset(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1911 while (true) |
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 c = *p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1914 switch (c) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1915 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1916 case '\n': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1917 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1918 break; |
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 '\r': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1921 if (*p == '\n') |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1922 continue; // ignore |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1923 c = '\n'; // treat EndOfLine as \n character |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1924 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1925 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1926 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1927 case 0: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1928 case 0x1A: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1929 error("unterminated string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1930 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1931 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1932 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1933 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1934 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1935 case '"': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1936 case '`': |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1937 if (c == tc) |
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 t.len = stringbuffer.offset; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1940 stringbuffer.writeByte(0); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1941 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1942 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1943 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1944 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1945 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1946 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1947 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1948 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1949 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1950 if (c & 0x80) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1951 { p--; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1952 uint u = decodeUTF(); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1953 p++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1954 if (u == PS || u == LS) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1955 loc.linnum++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1956 stringbuffer.writeUTF8(u); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1957 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1958 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1959 break; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1960 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
1961 stringbuffer.writeByte(c); |
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 |
0 | 1964 assert(false); |
1965 } | |
1966 | |
51 | 1967 /************************************** |
1968 * Lex hex strings: | |
1969 * x"0A ae 34FE BD" | |
1970 */ | |
0 | 1971 TOK hexStringConstant(Token* t) |
1972 { | |
51 | 1973 uint c; |
1974 Loc start = loc; | |
1975 uint n = 0; | |
1976 uint v; | |
1977 | |
1978 p++; | |
1979 stringbuffer.reset(); | |
1980 while (1) | |
1981 { | |
1982 c = *p++; | |
1983 switch (c) | |
1984 { | |
1985 case ' ': | |
1986 case '\t': | |
1987 case '\v': | |
1988 case '\f': | |
1989 continue; // skip white space | |
1990 | |
1991 case '\r': | |
1992 if (*p == '\n') | |
1993 continue; // ignore | |
1994 // Treat isolated '\r' as if it were a '\n' | |
1995 case '\n': | |
1996 loc.linnum++; | |
1997 continue; | |
1998 | |
1999 case 0: | |
2000 case 0x1A: | |
2001 error("unterminated string constant starting at %s", start.toChars()); | |
2002 t.ustring = "".ptr; | |
2003 t.len = 0; | |
2004 t.postfix = 0; | |
2005 return TOKstring; | |
2006 | |
2007 case '"': | |
2008 if (n & 1) | |
2009 { | |
2010 error("odd number (%d) of hex characters in hex string", n); | |
2011 stringbuffer.writeByte(v); | |
2012 } | |
2013 t.len = stringbuffer.offset; | |
2014 stringbuffer.writeByte(0); | |
2015 void* mem = malloc(stringbuffer.offset); | |
2016 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
2017 t.ustring = cast(const(char)*)mem; | |
2018 stringPostfix(t); | |
2019 return TOKstring; | |
2020 | |
2021 default: | |
2022 if (c >= '0' && c <= '9') | |
2023 c -= '0'; | |
2024 else if (c >= 'a' && c <= 'f') | |
2025 c -= 'a' - 10; | |
2026 else if (c >= 'A' && c <= 'F') | |
2027 c -= 'A' - 10; | |
2028 else if (c & 0x80) | |
2029 { p--; | |
2030 uint u = decodeUTF(); | |
2031 p++; | |
2032 if (u == PS || u == LS) | |
2033 loc.linnum++; | |
2034 else | |
135 | 2035 error("non-hex character \\u%04x", u); |
51 | 2036 } |
2037 else | |
2038 error("non-hex character '%c'", c); | |
2039 if (n & 1) | |
2040 { v = (v << 4) | c; | |
2041 stringbuffer.writeByte(v); | |
2042 } | |
2043 else | |
2044 v = c; | |
2045 n++; | |
2046 break; | |
2047 } | |
2048 } | |
0 | 2049 } |
2050 | |
2051 version (DMDV2) { | |
51 | 2052 /************************************** |
2053 * Lex delimited strings: | |
2054 * q"(foo(xxx))" // "foo(xxx)" | |
2055 * q"[foo(]" // "foo(" | |
2056 * q"/foo]/" // "foo]" | |
2057 * q"HERE | |
2058 * foo | |
2059 * HERE" // "foo\n" | |
2060 * Input: | |
2061 * p is on the " | |
2062 */ | |
0 | 2063 TOK delimitedStringConstant(Token* t) |
2064 { | |
51 | 2065 uint c; |
2066 Loc start = loc; | |
2067 uint delimleft = 0; | |
2068 uint delimright = 0; | |
2069 uint nest = 1; | |
2070 uint nestcount; | |
2071 Identifier hereid = null; | |
2072 uint blankrol = 0; | |
2073 uint startline = 0; | |
2074 | |
2075 p++; | |
2076 stringbuffer.reset(); | |
2077 while (1) | |
2078 { | |
2079 c = *p++; | |
2080 //printf("c = '%c'\n", c); | |
2081 switch (c) | |
2082 { | |
2083 case '\n': | |
2084 Lnextline: | |
2085 loc.linnum++; | |
2086 startline = 1; | |
2087 if (blankrol) | |
2088 { blankrol = 0; | |
2089 continue; | |
2090 } | |
2091 if (hereid) | |
2092 { | |
2093 stringbuffer.writeUTF8(c); | |
2094 continue; | |
2095 } | |
2096 break; | |
2097 | |
2098 case '\r': | |
2099 if (*p == '\n') | |
2100 continue; // ignore | |
2101 c = '\n'; // treat EndOfLine as \n character | |
2102 goto Lnextline; | |
2103 | |
2104 case 0: | |
2105 case 0x1A: | |
2106 goto Lerror; | |
2107 | |
2108 default: | |
2109 if (c & 0x80) | |
2110 { p--; | |
2111 c = decodeUTF(); | |
2112 p++; | |
2113 if (c == PS || c == LS) | |
2114 goto Lnextline; | |
2115 } | |
2116 break; | |
2117 } | |
2118 if (delimleft == 0) | |
2119 { | |
2120 delimleft = c; | |
2121 nest = 1; | |
2122 nestcount = 1; | |
2123 if (c == '(') | |
2124 delimright = ')'; | |
2125 else if (c == '{') | |
2126 delimright = '}'; | |
2127 else if (c == '[') | |
2128 delimright = ']'; | |
2129 else if (c == '<') | |
2130 delimright = '>'; | |
2131 else if (isalpha(c) || c == '_' || (c >= 0x80 && isUniAlpha(c))) | |
2132 { | |
2133 // Start of identifier; must be a heredoc | |
2134 Token t2; | |
2135 p--; | |
2136 scan(&t2); // read in heredoc identifier | |
2137 if (t2.value != TOKidentifier) | |
2138 { | |
2139 error("identifier expected for heredoc, not %s", t2.toChars()); | |
2140 delimright = c; | |
2141 } | |
2142 else | |
2143 { | |
2144 hereid = t2.ident; | |
2145 //printf("hereid = '%s'\n", hereid.toChars()); | |
2146 blankrol = 1; | |
2147 } | |
2148 nest = 0; | |
2149 } | |
2150 else | |
2151 { | |
2152 delimright = c; | |
2153 nest = 0; | |
2154 if (isspace(c)) | |
2155 error("delimiter cannot be whitespace"); | |
2156 } | |
2157 } | |
2158 else | |
2159 { | |
2160 if (blankrol) | |
2161 { | |
2162 error("heredoc rest of line should be blank"); | |
2163 blankrol = 0; | |
2164 continue; | |
2165 } | |
2166 if (nest == 1) | |
2167 { | |
2168 if (c == delimleft) | |
2169 nestcount++; | |
2170 else if (c == delimright) | |
2171 { nestcount--; | |
2172 if (nestcount == 0) | |
2173 goto Ldone; | |
2174 } | |
2175 } | |
2176 else if (c == delimright) | |
2177 goto Ldone; | |
2178 if (startline && isalpha(c) && hereid) | |
2179 { | |
2180 Token t2; | |
2181 ubyte* psave = p; | |
2182 p--; | |
2183 scan(&t2); // read in possible heredoc identifier | |
2184 //printf("endid = '%s'\n", t2.ident.toChars()); | |
2185 if (t2.value == TOKidentifier && t2.ident.equals(hereid)) | |
2186 { | |
2187 /* should check that rest of line is blank | |
2188 */ | |
2189 goto Ldone; | |
2190 } | |
2191 p = psave; | |
2192 } | |
2193 stringbuffer.writeUTF8(c); | |
2194 startline = 0; | |
2195 } | |
2196 } | |
2197 | |
2198 Ldone: | |
2199 if (*p == '"') | |
2200 p++; | |
2201 else | |
2202 error("delimited string must end in %c\"", delimright); | |
2203 t.len = stringbuffer.offset; | |
2204 stringbuffer.writeByte(0); | |
2205 void* mem = malloc(stringbuffer.offset); | |
2206 memcpy(mem, stringbuffer.data, stringbuffer.offset); | |
2207 t.ustring = cast(const(char)*)mem; | |
2208 stringPostfix(t); | |
2209 return TOKstring; | |
2210 | |
2211 Lerror: | |
2212 error("unterminated string constant starting at %s", start.toChars()); | |
2213 t.ustring = "".ptr; | |
2214 t.len = 0; | |
2215 t.postfix = 0; | |
2216 return TOKstring; | |
0 | 2217 } |
2218 | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2219 /************************************** |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2220 * Lex delimited strings: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2221 * q{ foo(xxx) } // " foo(xxx) " |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2222 * q{foo(} // "foo(" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2223 * q{{foo}"}"} // "{foo}"}"" |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2224 * Input: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2225 * p is on the q |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2226 */ |
0 | 2227 TOK tokenStringConstant(Token* t) |
2228 { | |
8
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2229 uint nest = 1; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2230 Loc start = loc; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2231 ubyte* pstart = ++p; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2232 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2233 while (true) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2234 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2235 Token tok; |
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 scan(&tok); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2238 switch (tok.value) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2239 { |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2240 case TOKlcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2241 nest++; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2242 continue; |
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 TOKrcurly: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2245 if (--nest == 0) |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2246 goto Ldone; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2247 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2248 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2249 case TOKeof: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2250 goto Lerror; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2251 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2252 default: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2253 continue; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2254 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2255 } |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2256 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2257 Ldone: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2258 t.len = p - 1 - pstart; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2259 char* tmp = cast(char*)GC.malloc(t.len + 1); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2260 memcpy(tmp, pstart, t.len); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2261 tmp[t.len] = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2262 t.ustring = tmp; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2263 stringPostfix(t); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2264 return TOKstring; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2265 |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2266 Lerror: |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2267 error("unterminated token string constant starting at %s", start.toChars()); |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2268 t.ustring = "".ptr; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2269 t.len = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2270 t.postfix = 0; |
d42cd5917df4
wysiwyg strings, alias this, templates, TypeSlice implementation
dkoroskin <>
parents:
4
diff
changeset
|
2271 return TOKstring; |
0 | 2272 } |
2273 } | |
2274 TOK escapeStringConstant(Token* t, int wide) | |
2275 { | |
2276 uint c; | |
2277 Loc start = loc; | |
2278 | |
2279 p++; | |
2280 stringbuffer.reset(); | |
2281 while (true) | |
2282 { | |
2283 c = *p++; | |
2284 switch (c) | |
2285 { | |
2286 version (TEXTUAL_ASSEMBLY_OUT) { | |
2287 } else { | |
2288 case '\\': | |
2289 switch (*p) | |
2290 { | |
2291 case 'u': | |
2292 case 'U': | |
2293 case '&': | |
2294 c = escapeSequence(); | |
2295 stringbuffer.writeUTF8(c); | |
2296 continue; | |
2297 | |
2298 default: | |
2299 c = escapeSequence(); | |
2300 break; | |
2301 } | |
2302 break; | |
2303 } | |
2304 case '\n': | |
2305 loc.linnum++; | |
2306 break; | |
2307 | |
2308 case '\r': | |
2309 if (*p == '\n') | |
2310 continue; // ignore | |
2311 c = '\n'; // treat EndOfLine as \n character | |
2312 loc.linnum++; | |
2313 break; | |
2314 | |
2315 case '"': | |
2316 t.len = stringbuffer.offset; | |
2317 stringbuffer.writeByte(0); | |
2 | 2318 char* tmp = cast(char*)GC.malloc(stringbuffer.offset); |
0 | 2319 memcpy(tmp, stringbuffer.data, stringbuffer.offset); |
2320 t.ustring = tmp; | |
2321 stringPostfix(t); | |
2322 return TOK.TOKstring; | |
2323 | |
2324 case 0: | |
2325 case 0x1A: | |
2326 p--; | |
2327 error("unterminated string constant starting at %s", start.toChars()); | |
2328 t.ustring = "".ptr; | |
2329 t.len = 0; | |
2330 t.postfix = 0; | |
2331 return TOK.TOKstring; | |
2332 | |
2333 default: | |
2334 if (c & 0x80) | |
2335 { | |
2336 p--; | |
2337 c = decodeUTF(); | |
2338 if (c == LS || c == PS) | |
2339 { c = '\n'; | |
2340 loc.linnum++; | |
2341 } | |
2342 p++; | |
2343 stringbuffer.writeUTF8(c); | |
2344 continue; | |
2345 } | |
2346 break; | |
2347 } | |
2348 stringbuffer.writeByte(c); | |
2349 } | |
2350 | |
2351 assert(false); | |
2352 } | |
2353 | |
2354 TOK charConstant(Token* t, int wide) | |
2355 { | |
2356 uint c; | |
2357 TOK tk = TOKcharv; | |
2358 | |
2359 //printf("Lexer.charConstant\n"); | |
2360 p++; | |
2361 c = *p++; | |
2362 switch (c) | |
2363 { | |
2364 version (TEXTUAL_ASSEMBLY_OUT) { | |
2365 } else { | |
2366 case '\\': | |
2367 switch (*p) | |
2368 { | |
2369 case 'u': | |
2370 t.uns64value = escapeSequence(); | |
2371 tk = TOKwcharv; | |
2372 break; | |
2373 | |
2374 case 'U': | |
2375 case '&': | |
2376 t.uns64value = escapeSequence(); | |
2377 tk = TOKdcharv; | |
2378 break; | |
2379 | |
2380 default: | |
2381 t.uns64value = escapeSequence(); | |
2382 break; | |
2383 } | |
2384 break; | |
2385 } | |
2386 case '\n': | |
2387 L1: | |
2388 loc.linnum++; | |
2389 case '\r': | |
2390 case 0: | |
2391 case 0x1A: | |
2392 case '\'': | |
2393 error("unterminated character constant"); | |
2394 return tk; | |
2395 | |
2396 default: | |
2397 if (c & 0x80) | |
2398 { | |
2399 p--; | |
2400 c = decodeUTF(); | |
2401 p++; | |
2402 if (c == LS || c == PS) | |
2403 goto L1; | |
2404 if (c < 0xD800 || (c >= 0xE000 && c < 0xFFFE)) | |
2405 tk = TOKwcharv; | |
2406 else | |
2407 tk = TOKdcharv; | |
2408 } | |
2409 t.uns64value = c; | |
2410 break; | |
2411 } | |
2412 | |
2413 if (*p != '\'') | |
2414 { | |
2415 error("unterminated character constant"); | |
2416 return tk; | |
2417 } | |
2418 p++; | |
2419 return tk; | |
2420 } | |
2421 | |
2422 /*************************************** | |
2423 * Get postfix of string literal. | |
2424 */ | |
2425 void stringPostfix(Token* t) | |
2426 { | |
2427 switch (*p) | |
2428 { | |
2429 case 'c': | |
2430 case 'w': | |
2431 case 'd': | |
2432 t.postfix = *p; | |
2433 p++; | |
2434 break; | |
2435 | |
2436 default: | |
2437 t.postfix = 0; | |
2438 break; | |
2439 } | |
2440 } | |
2441 | |
2442 uint wchar_(uint u) | |
2443 { | |
2444 assert(false); | |
2445 } | |
2446 | |
2447 /************************************** | |
2448 * Read in a number. | |
2449 * If it's an integer, store it in tok.TKutok.Vlong. | |
2450 * integers can be decimal, octal or hex | |
2451 * Handle the suffixes U, UL, LU, L, etc. | |
2452 * If it's double, store it in tok.TKutok.Vdouble. | |
2453 * Returns: | |
2454 * TKnum | |
2455 * TKdouble,... | |
2456 */ | |
2457 | |
2458 TOK number(Token* t) | |
2459 { | |
2460 // We use a state machine to collect numbers | |
2461 enum STATE { STATE_initial, STATE_0, STATE_decimal, STATE_octal, STATE_octale, | |
2462 STATE_hex, STATE_binary, STATE_hex0, STATE_binary0, | |
2463 STATE_hexh, STATE_error }; | |
2464 STATE state; | |
2465 | |
2466 enum FLAGS | |
2467 { | |
2468 FLAGS_undefined = 0, | |
2469 FLAGS_decimal = 1, // decimal | |
2470 FLAGS_unsigned = 2, // u or U suffix | |
2471 FLAGS_long = 4, // l or L suffix | |
2472 }; | |
2473 | |
2474 FLAGS flags = FLAGS.FLAGS_decimal; | |
2475 | |
2476 int i; | |
2477 int base; | |
2478 uint c; | |
2479 ubyte *start; | |
2480 TOK result; | |
2481 | |
2482 //printf("Lexer.number()\n"); | |
2483 state = STATE.STATE_initial; | |
2484 base = 0; | |
2485 stringbuffer.reset(); | |
2486 start = p; | |
2487 while (1) | |
2488 { | |
2489 c = *p; | |
2490 switch (state) | |
2491 { | |
2492 case STATE.STATE_initial: // opening state | |
2493 if (c == '0') | |
2494 state = STATE.STATE_0; | |
2495 else | |
2496 state = STATE.STATE_decimal; | |
2497 break; | |
2498 | |
2499 case STATE.STATE_0: | |
2500 flags = (flags & ~FLAGS.FLAGS_decimal); | |
2501 switch (c) | |
2502 { | |
2503 version (ZEROH) { | |
2504 case 'H': // 0h | |
2505 case 'h': | |
2506 goto hexh; | |
2507 } | |
2508 case 'X': | |
2509 case 'x': | |
2510 state = STATE.STATE_hex0; | |
2511 break; | |
2512 | |
2513 case '.': | |
2514 if (p[1] == '.') // .. is a separate token | |
2515 goto done; | |
2516 case 'i': | |
2517 case 'f': | |
2518 case 'F': | |
2519 goto real_; | |
2520 version (ZEROH) { | |
2521 case 'E': | |
2522 case 'e': | |
2523 goto case_hex; | |
2524 } | |
2525 case 'B': | |
2526 case 'b': | |
2527 state = STATE.STATE_binary0; | |
2528 break; | |
2529 | |
2530 case '0': case '1': case '2': case '3': | |
2531 case '4': case '5': case '6': case '7': | |
2532 state = STATE.STATE_octal; | |
2533 break; | |
2534 | |
2535 version (ZEROH) { | |
2536 case '8': case '9': case 'A': | |
2537 case 'C': case 'D': case 'F': | |
2538 case 'a': case 'c': case 'd': case 'f': | |
2539 case_hex: | |
2540 state = STATE.STATE_hexh; | |
2541 break; | |
2542 } | |
2543 case '_': | |
2544 state = STATE.STATE_octal; | |
2545 p++; | |
2546 continue; | |
2547 | |
2548 case 'L': | |
2549 if (p[1] == 'i') | |
2550 goto real_; | |
2551 goto done; | |
2552 | |
2553 default: | |
2554 goto done; | |
2555 } | |
2556 break; | |
2557 | |
2558 case STATE.STATE_decimal: // reading decimal number | |
2559 if (!isdigit(c)) | |
2560 { | |
2561 version (ZEROH) { | |
2562 if (ishex(c) | |
2563 || c == 'H' || c == 'h' | |
2564 ) | |
2565 goto hexh; | |
2566 } | |
2567 if (c == '_') // ignore embedded _ | |
2568 { p++; | |
2569 continue; | |
2570 } | |
2571 if (c == '.' && p[1] != '.') | |
2572 goto real_; | |
2573 else if (c == 'i' || c == 'f' || c == 'F' || | |
2574 c == 'e' || c == 'E') | |
2575 { | |
2576 real_: // It's a real number. Back up and rescan as a real | |
2577 p = start; | |
2578 return inreal(t); | |
2579 } | |
2580 else if (c == 'L' && p[1] == 'i') | |
2581 goto real_; | |
2582 goto done; | |
2583 } | |
2584 break; | |
2585 | |
2586 case STATE.STATE_hex0: // reading hex number | |
2587 case STATE.STATE_hex: | |
2588 if (! ishex(cast(ubyte)c)) | |
2589 { | |
2590 if (c == '_') // ignore embedded _ | |
2591 { p++; | |
2592 continue; | |
2593 } | |
2594 if (c == '.' && p[1] != '.') | |
2595 goto real_; | |
2596 if (c == 'P' || c == 'p' || c == 'i') | |
2597 goto real_; | |
2598 if (state == STATE.STATE_hex0) | |
2599 error("Hex digit expected, not '%c'", c); | |
2600 goto done; | |
2601 } | |
2602 state = STATE.STATE_hex; | |
2603 break; | |
2604 | |
2605 version (ZEROH) { | |
2606 hexh: | |
2607 state = STATE.STATE_hexh; | |
2608 case STATE.STATE_hexh: // parse numbers like 0FFh | |
2609 if (!ishex(c)) | |
2610 { | |
2611 if (c == 'H' || c == 'h') | |
2612 { | |
2613 p++; | |
2614 base = 16; | |
2615 goto done; | |
2616 } | |
2617 else | |
2618 { | |
2619 // Check for something like 1E3 or 0E24 | |
2620 if (memchr(cast(char*)stringbuffer.data, 'E', stringbuffer.offset) || | |
2621 memchr(cast(char*)stringbuffer.data, 'e', stringbuffer.offset)) | |
2622 goto real_; | |
2623 error("Hex digit expected, not '%c'", c); | |
2624 goto done; | |
2625 } | |
2626 } | |
2627 break; | |
2628 } | |
2629 | |
2630 case STATE.STATE_octal: // reading octal number | |
2631 case STATE.STATE_octale: // reading octal number with non-octal digits | |
2632 if (!isoctal(cast(ubyte)c)) | |
2633 { | |
2634 version (ZEROH) { | |
2635 if (ishex(c) | |
2636 || c == 'H' || c == 'h' | |
2637 ) | |
2638 goto hexh; | |
2639 } | |
2640 if (c == '_') // ignore embedded _ | |
2641 { p++; | |
2642 continue; | |
2643 } | |
2644 if (c == '.' && p[1] != '.') | |
2645 goto real_; | |
2646 if (c == 'i') | |
2647 goto real_; | |
2648 if (isdigit(c)) | |
2649 { | |
2650 state = STATE.STATE_octale; | |
2651 } | |
2652 else | |
2653 goto done; | |
2654 } | |
2655 break; | |
2656 | |
2657 case STATE.STATE_binary0: // starting binary number | |
2658 case STATE.STATE_binary: // reading binary number | |
2659 if (c != '0' && c != '1') | |
2660 { | |
2661 version (ZEROH) { | |
2662 if (ishex(c) | |
2663 || c == 'H' || c == 'h' | |
2664 ) | |
2665 goto hexh; | |
2666 } | |
2667 if (c == '_') // ignore embedded _ | |
2668 { p++; | |
2669 continue; | |
2670 } | |
2671 if (state == STATE.STATE_binary0) | |
2672 { error("binary digit expected"); | |
2673 state = STATE.STATE_error; | |
2674 break; | |
2675 } | |
2676 else | |
2677 goto done; | |
2678 } | |
2679 state = STATE.STATE_binary; | |
2680 break; | |
2681 | |
2682 case STATE.STATE_error: // for error recovery | |
2683 if (!isdigit(c)) // scan until non-digit | |
2684 goto done; | |
2685 break; | |
2686 | |
2687 default: | |
2688 assert(0); | |
2689 } | |
2690 stringbuffer.writeByte(c); | |
2691 p++; | |
2692 } | |
2693 done: | |
2694 stringbuffer.writeByte(0); // terminate string | |
2695 if (state == STATE.STATE_octale) | |
2696 error("Octal digit expected"); | |
2697 | |
2698 ulong n; // unsigned >=64 bit integer type | |
2699 | |
2700 if (stringbuffer.offset == 2 && (state == STATE.STATE_decimal || state == STATE.STATE_0)) | |
2701 n = stringbuffer.data[0] - '0'; | |
2702 else | |
2703 { | |
2704 // Convert string to integer | |
2705 version (__DMC__) { | |
2706 errno = 0; | |
2707 n = strtoull(cast(char*)stringbuffer.data,null,base); | |
2708 if (errno == ERANGE) | |
2709 error("integer overflow"); | |
2710 } else { | |
2711 // Not everybody implements strtoull() | |
2712 char* p = cast(char*)stringbuffer.data; | |
2713 int r = 10, d; | |
2714 | |
2715 if (*p == '0') | |
2716 { | |
2717 if (p[1] == 'x' || p[1] == 'X') | |
2718 p += 2, r = 16; | |
2719 else if (p[1] == 'b' || p[1] == 'B') | |
2720 p += 2, r = 2; | |
2721 else if (isdigit(p[1])) | |
2722 p += 1, r = 8; | |
2723 } | |
2724 | |
2725 n = 0; | |
2726 while (1) | |
2727 { | |
2728 if (*p >= '0' && *p <= '9') | |
2729 d = *p - '0'; | |
2730 else if (*p >= 'a' && *p <= 'z') | |
2731 d = *p - 'a' + 10; | |
2732 else if (*p >= 'A' && *p <= 'Z') | |
2733 d = *p - 'A' + 10; | |
2734 else | |
2735 break; | |
2736 if (d >= r) | |
2737 break; | |
2738 ulong n2 = n * r; | |
2739 //printf("n2 / r = %llx, n = %llx\n", n2/r, n); | |
2740 if (n2 / r != n || n2 + d < n) | |
2741 { | |
2742 error ("integer overflow"); | |
2743 break; | |
2744 } | |
2745 | |
2746 n = n2 + d; | |
2747 p++; | |
2748 } | |
2749 } | |
2750 if (n.sizeof > 8 && | |
2751 n > 0xFFFFFFFFFFFFFFFF) // if n needs more than 64 bits | |
2752 error("integer overflow"); | |
2753 } | |
2754 | |
2755 // Parse trailing 'u', 'U', 'l' or 'L' in any combination | |
2756 while (1) | |
2757 { FLAGS f; | |
2758 | |
2759 switch (*p) | |
2760 { case 'U': | |
2761 case 'u': | |
2762 f = FLAGS.FLAGS_unsigned; | |
2763 goto L1; | |
2764 | |
2765 case 'l': | |
2766 if (1 || !global.params.useDeprecated) | |
2767 error("'l' suffix is deprecated, use 'L' instead"); | |
2768 case 'L': | |
2769 f = FLAGS.FLAGS_long; | |
2770 L1: | |
2771 p++; | |
2772 if (flags & f) | |
2773 error("unrecognized token"); | |
2774 flags = (flags | f); | |
2775 continue; | |
2776 default: | |
2777 break; | |
2778 } | |
2779 break; | |
2780 } | |
2781 | |
2782 switch (flags) | |
2783 { | |
2784 case FLAGS.FLAGS_undefined: | |
2785 /* Octal or Hexadecimal constant. | |
2786 * First that fits: int, uint, long, ulong | |
2787 */ | |
2788 if (n & 0x8000000000000000) | |
2789 result = TOK.TOKuns64v; | |
2790 else if (n & 0xFFFFFFFF00000000) | |
2791 result = TOK.TOKint64v; | |
2792 else if (n & 0x80000000) | |
2793 result = TOK.TOKuns32v; | |
2794 else | |
2795 result = TOK.TOKint32v; | |
2796 break; | |
2797 | |
2798 case FLAGS.FLAGS_decimal: | |
2799 /* First that fits: int, long, long long | |
2800 */ | |
2801 if (n & 0x8000000000000000) | |
2802 { error("signed integer overflow"); | |
2803 result = TOK.TOKuns64v; | |
2804 } | |
2805 else if (n & 0xFFFFFFFF80000000) | |
2806 result = TOK.TOKint64v; | |
2807 else | |
2808 result = TOK.TOKint32v; | |
2809 break; | |
2810 | |
2811 case FLAGS.FLAGS_unsigned: | |
2812 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned: | |
2813 /* First that fits: uint, ulong | |
2814 */ | |
2815 if (n & 0xFFFFFFFF00000000) | |
2816 result = TOK.TOKuns64v; | |
2817 else | |
2818 result = TOK.TOKuns32v; | |
2819 break; | |
2820 | |
2821 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_long: | |
2822 if (n & 0x8000000000000000) | |
2823 { error("signed integer overflow"); | |
2824 result = TOK.TOKuns64v; | |
2825 } | |
2826 else | |
2827 result = TOK.TOKint64v; | |
2828 break; | |
2829 | |
2830 case FLAGS.FLAGS_long: | |
2831 if (n & 0x8000000000000000) | |
2832 result = TOK.TOKuns64v; | |
2833 else | |
2834 result = TOK.TOKint64v; | |
2835 break; | |
2836 | |
2837 case FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2838 case FLAGS.FLAGS_decimal | FLAGS.FLAGS_unsigned | FLAGS.FLAGS_long: | |
2839 result = TOK.TOKuns64v; | |
2840 break; | |
2841 | |
2842 default: | |
2843 debug { | |
2844 printf("%x\n",flags); | |
2845 } | |
2846 assert(0); | |
2847 } | |
2848 t.uns64value = n; | |
2849 return result; | |
2850 } | |
2851 | |
2852 /************************************** | |
2853 * Read in characters, converting them to real. | |
2854 * Bugs: | |
2855 * Exponent overflow not detected. | |
2856 * Too much requested precision is not detected. | |
2857 */ | |
2858 TOK inreal(Token* t) | |
2859 in | |
2860 { | |
2861 assert(*p == '.' || isdigit(*p)); | |
2862 } | |
2863 out (result) | |
2864 { | |
2865 switch (result) | |
2866 { | |
2867 case TOKfloat32v: | |
2868 case TOKfloat64v: | |
2869 case TOKfloat80v: | |
2870 case TOKimaginary32v: | |
2871 case TOKimaginary64v: | |
2872 case TOKimaginary80v: | |
2873 break; | |
2874 | |
2875 default: | |
2876 assert(0); | |
2877 } | |
2878 } | |
2879 body | |
2880 { | |
2881 int dblstate; | |
2882 uint c; | |
2883 char hex; // is this a hexadecimal-floating-constant? | |
2884 TOK result; | |
2885 | |
2886 //printf("Lexer.inreal()\n"); | |
2887 stringbuffer.reset(); | |
2888 dblstate = 0; | |
2889 hex = 0; | |
2890 Lnext: | |
2891 while (true) | |
2892 { | |
2893 // Get next char from input | |
2894 c = *p++; | |
2895 //printf("dblstate = %d, c = '%c'\n", dblstate, c); | |
2896 while (true) | |
2897 { | |
2898 switch (dblstate) | |
2899 { | |
2900 case 0: // opening state | |
2901 if (c == '0') | |
2902 dblstate = 9; | |
2903 else if (c == '.') | |
2904 dblstate = 3; | |
2905 else | |
2906 dblstate = 1; | |
2907 break; | |
2908 | |
2909 case 9: | |
2910 dblstate = 1; | |
2911 if (c == 'X' || c == 'x') | |
2912 { | |
2913 hex++; | |
2914 break; | |
2915 } | |
2916 case 1: // digits to left of . | |
2917 case 3: // digits to right of . | |
2918 case 7: // continuing exponent digits | |
2919 if (!isdigit(c) && !(hex && isxdigit(c))) | |
2920 { | |
2921 if (c == '_') | |
2922 goto Lnext; // ignore embedded '_' | |
2923 dblstate++; | |
2924 continue; | |
2925 } | |
2926 break; | |
2927 | |
2928 case 2: // no more digits to left of . | |
2929 if (c == '.') | |
2930 { | |
2931 dblstate++; | |
2932 break; | |
2933 } | |
2934 case 4: // no more digits to right of . | |
2935 if ((c == 'E' || c == 'e') || | |
2936 hex && (c == 'P' || c == 'p')) | |
2937 { | |
2938 dblstate = 5; | |
2939 hex = 0; // exponent is always decimal | |
2940 break; | |
2941 } | |
2942 if (hex) | |
2943 error("binary-exponent-part required"); | |
2944 goto done; | |
2945 | |
2946 case 5: // looking immediately to right of E | |
2947 dblstate++; | |
2948 if (c == '-' || c == '+') | |
2949 break; | |
2950 case 6: // 1st exponent digit expected | |
2951 if (!isdigit(c)) | |
2952 error("exponent expected"); | |
2953 dblstate++; | |
2954 break; | |
2955 | |
2956 case 8: // past end of exponent digits | |
2957 goto done; | |
79 | 2958 |
2959 default: | |
2960 assert(0, "inreal.dblstate has unexpected value"); | |
0 | 2961 } |
2962 break; | |
2963 } | |
2964 stringbuffer.writeByte(c); | |
2965 } | |
2966 done: | |
2967 p--; | |
2968 | |
2969 stringbuffer.writeByte(0); | |
2970 | |
114 | 2971 version (Windows) { /// && __DMC__ |
0 | 2972 char* save = __locale_decpoint; |
2973 __locale_decpoint = cast(char*)".".ptr; | |
2974 } | |
2975 t.float80value = strtold(cast(char*)stringbuffer.data, null); | |
2976 | |
2977 errno = 0; | |
2978 switch (*p) | |
2979 { | |
2980 case 'F': | |
2981 case 'f': | |
2982 strtof(cast(char*)stringbuffer.data, null); | |
2983 result = TOKfloat32v; | |
2984 p++; | |
2985 break; | |
2986 | |
2987 default: | |
2988 strtod(cast(char*)stringbuffer.data, null); | |
2989 result = TOKfloat64v; | |
2990 break; | |
2991 | |
2992 case 'l': | |
2993 if (!global.params.useDeprecated) | |
2994 error("'l' suffix is deprecated, use 'L' instead"); | |
2995 case 'L': | |
2996 result = TOKfloat80v; | |
2997 p++; | |
2998 break; | |
2999 } | |
3000 if (*p == 'i' || *p == 'I') | |
3001 { | |
3002 if (!global.params.useDeprecated && *p == 'I') | |
3003 error("'I' suffix is deprecated, use 'i' instead"); | |
3004 p++; | |
3005 switch (result) | |
3006 { | |
3007 case TOKfloat32v: | |
3008 result = TOKimaginary32v; | |
3009 break; | |
3010 case TOKfloat64v: | |
3011 result = TOKimaginary64v; | |
3012 break; | |
3013 case TOKfloat80v: | |
3014 result = TOKimaginary80v; | |
3015 break; | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3016 default: |
0 | 3017 } |
3018 } | |
3019 | |
114 | 3020 version (Windows) { ///&& __DMC__ |
0 | 3021 __locale_decpoint = save; |
3022 } | |
3023 if (errno == ERANGE) | |
3024 error("number is not representable"); | |
3025 | |
3026 return result; | |
3027 } | |
3028 | |
3029 void error(T...)(string format, T t) | |
3030 { | |
3031 error(this.loc, format, t); | |
3032 } | |
3033 | |
3034 void error(T...)(Loc loc, string format, T t) | |
3035 { | |
3036 if (mod && !global.gag) | |
3037 { | |
3038 string p = loc.toChars(); | |
3039 if (p.length != 0) | |
3040 writef("%s: ", p); | |
3041 | |
3042 writefln(format, t); | |
3043 | |
3044 if (global.errors >= 20) // moderate blizzard of cascading messages | |
3045 fatal(); | |
3046 } | |
3047 | |
3048 global.errors++; | |
3049 } | |
3050 | |
162 | 3051 /********************************************* |
3052 * Do pragma. | |
3053 * Currently, the only pragma supported is: | |
3054 * #line linnum [filespec] | |
3055 */ | |
0 | 3056 void pragma_() |
3057 { | |
162 | 3058 Token tok; |
3059 int linnum; | |
3060 string filespec = null; | |
3061 Loc loc = this.loc; | |
3062 | |
3063 scan(&tok); | |
3064 if (tok.value != TOKidentifier || tok.ident != Id.line) | |
3065 goto Lerr; | |
3066 | |
3067 scan(&tok); | |
3068 if (tok.value == TOKint32v || tok.value == TOKint64v) | |
3069 linnum = cast(int)(tok.uns64value - 1); /// | |
3070 else | |
3071 goto Lerr; | |
3072 | |
3073 while (1) | |
3074 { | |
3075 switch (*p) | |
3076 { | |
3077 case 0: | |
3078 case 0x1A: | |
3079 case '\n': | |
3080 Lnewline: | |
3081 this.loc.linnum = linnum; | |
3082 if (filespec != null) | |
3083 this.loc.filename = filespec; | |
3084 return; | |
3085 | |
3086 case '\r': | |
3087 p++; | |
3088 if (*p != '\n') | |
3089 { p--; | |
3090 goto Lnewline; | |
3091 } | |
3092 continue; | |
3093 | |
3094 case ' ': | |
3095 case '\t': | |
3096 case '\v': | |
3097 case '\f': | |
3098 p++; | |
3099 continue; // skip white space | |
3100 | |
3101 case '_': | |
3102 if (mod && memcmp(p, "__FILE__".ptr, 8) == 0) | |
3103 { | |
3104 p += 8; | |
3105 filespec = (loc.filename ? loc.filename : mod.ident.toChars()); | |
3106 } | |
3107 continue; | |
3108 | |
3109 case '"': | |
3110 if (filespec) | |
3111 goto Lerr; | |
3112 stringbuffer.reset(); | |
3113 p++; | |
3114 while (1) | |
3115 { | |
3116 uint c; | |
3117 | |
3118 c = *p; | |
3119 switch (c) | |
3120 { | |
3121 case '\n': | |
3122 case '\r': | |
3123 case 0: | |
3124 case 0x1A: | |
3125 goto Lerr; | |
3126 | |
3127 case '"': | |
3128 stringbuffer.writeByte(0); | |
3129 filespec = stringbuffer.extractString(); /// | |
3130 p++; | |
3131 break; | |
3132 | |
3133 default: | |
3134 if (c & 0x80) | |
3135 { | |
3136 uint u = decodeUTF(); | |
3137 if (u == PS || u == LS) | |
3138 goto Lerr; | |
3139 } | |
3140 stringbuffer.writeByte(c); | |
3141 p++; | |
3142 continue; | |
3143 } | |
3144 break; | |
3145 } | |
3146 continue; | |
3147 | |
3148 default: | |
3149 if (*p & 0x80) | |
3150 { | |
3151 uint u = decodeUTF(); | |
3152 if (u == PS || u == LS) | |
3153 goto Lnewline; | |
3154 } | |
3155 goto Lerr; | |
3156 } | |
3157 } | |
3158 | |
3159 Lerr: | |
3160 error(loc, "#line integer [\"filespec\"]\\n expected"); | |
0 | 3161 } |
3162 | |
49 | 3163 /******************************************** |
3164 * Decode UTF character. | |
3165 * Issue error messages for invalid sequences. | |
3166 * Return decoded character, advance p to last character in UTF sequence. | |
3167 */ | |
0 | 3168 uint decodeUTF() |
3169 { | |
49 | 3170 dchar u; |
3171 ubyte c; | |
3172 ubyte* s = p; | |
3173 size_t len; | |
3174 size_t idx; | |
3175 string msg; | |
3176 | |
3177 c = *s; | |
3178 assert(c & 0x80); | |
3179 | |
3180 // Check length of remaining string up to 6 UTF-8 characters | |
3181 for (len = 1; len < 6 && s[len]; len++) { | |
3182 ; | |
3183 } | |
3184 | |
3185 idx = 0; | |
3186 msg = utf_decodeChar(cast(string)s[0..len], &idx, &u); | |
3187 p += idx - 1; | |
3188 if (msg) | |
3189 { | |
3190 error("%s", msg); | |
3191 } | |
3192 return u; | |
0 | 3193 } |
3194 | |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3195 /*************************************************** |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3196 * Parse doc comment embedded between t.ptr and p. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3197 * Remove trailing blanks and tabs from lines. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3198 * Replace all newlines with \n. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3199 * Remove leading comment character from each line. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3200 * Decide if it's a lineComment or a blockComment. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3201 * Append to previous one for this token. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3202 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3203 void getDocComment(Token* t, uint lineComment) |
0 | 3204 { |
157
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3205 /* ct tells us which kind of comment it is: '!', '/', '*', or '+' |
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 ubyte ct = t.ptr[2]; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3208 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3209 /* Start of comment text skips over / * *, / + +, 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* q = t.ptr + 3; // start of comment text |
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 ubyte* qend = p; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3214 if (ct == '*' || ct == '+') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3215 qend -= 2; |
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 /* Scan over initial row of ****'s or ++++'s or ////'s |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3218 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3219 for (; q < qend; q++) |
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 if (*q != ct) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3222 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3223 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3224 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3225 /* Remove trailing row of ****'s or ++++'s |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3226 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3227 if (ct != '/' && ct != '!') |
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 for (; q < qend; qend--) |
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 (qend[-1] != ct) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3232 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3233 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3234 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3235 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3236 /* Comment is now [q .. qend]. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3237 * Canonicalize it into buf[]. |
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 OutBuffer buf = new OutBuffer; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3240 int linestart = 0; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3241 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3242 for (; q < qend; q++) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3243 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3244 ubyte c = *q; |
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 switch (c) |
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 case '*': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3249 case '+': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3250 if (linestart && c == ct) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3251 { linestart = 0; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3252 /* Trim preceding whitespace up to preceding \n |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3253 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3254 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
|
3255 buf.offset--; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3256 continue; |
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 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3259 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3260 case ' ': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3261 case '\t': |
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 '\r': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3265 if (q[1] == '\n') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3266 continue; // skip the \r |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3267 goto Lnewline; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3268 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3269 default: |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3270 if (c == 226) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3271 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3272 // If LS or PS |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3273 if (q[1] == 128 && |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3274 (q[2] == 168 || q[2] == 169)) |
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 q += 2; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3277 goto Lnewline; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3278 } |
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 linestart = 0; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3281 break; |
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 Lnewline: |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3284 c = '\n'; // replace all newlines with \n |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3285 case '\n': |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3286 linestart = 1; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3287 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3288 /* Trim trailing whitespace |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3289 */ |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3290 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
|
3291 buf.offset--; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3292 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3293 break; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3294 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3295 buf.writeByte(c); |
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 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3298 // Always end with a newline |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3299 if (!buf.offset || buf.data[buf.offset - 1] != '\n') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3300 buf.writeByte('\n'); |
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 buf.writeByte(0); |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3303 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3304 // 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
|
3305 // after other non-whitespace on the same line. |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3306 string* dc = (lineComment && anyToken) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3307 ? &t.lineComment |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3308 : &t.blockComment; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3309 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3310 // Combine with previous doc comment, if any |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3311 if (*dc) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3312 *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
|
3313 else |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3314 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3315 auto bufsize = buf.size; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3316 *dc = cast(string) buf.extractData()[0..bufsize]; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3317 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3318 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3319 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3320 /******************************************** |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3321 * Combine two document comments into one, |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3322 * separated by a newline. |
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 static string combineComments(string c1, string c2) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3325 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3326 //printf("Lexer::combineComments('%s', '%s')\n", c1, c2); |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3327 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3328 string c = 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 if (c1) |
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 c = c1; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3333 if (c2) |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3334 { |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3335 size_t len1 = c1.length; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3336 size_t len2 = c2.length; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3337 |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3338 c = c1.idup; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3339 if (len1 && c1[$-1] != '\n') |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3340 c ~= '\n'; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3341 c ~= c2; |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3342 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3343 } |
b7b61140701d
* added all missing default cases in switch statements
trass3r
parents:
135
diff
changeset
|
3344 return c; |
0 | 3345 } |
3346 | |
3347 static bool isValidIdentifier(string p) | |
3348 { | |
3349 if (p.length == 0) { | |
3350 return false; | |
3351 } | |
3352 | |
3353 if (p[0] >= '0' && p[0] <= '9') { // beware of isdigit() on signed chars | |
3354 return false; | |
3355 } | |
3356 | |
3357 size_t idx = 0; | |
3358 while (idx < p.length) | |
3359 { | |
3360 dchar dc; | |
3361 | |
3362 if (utf_decodeChar(p, &idx, &dc) !is null) { | |
3363 return false; | |
3364 } | |
3365 | |
3366 if (!((dc >= 0x80 && isUniAlpha(dc)) || isalnum(dc) || dc == '_')) { | |
3367 return false; | |
3368 } | |
3369 } | |
3370 | |
3371 return true; | |
3372 } | |
3373 | |
79 | 3374 /// TODO: use normal string append when GC works |
3375 static string combineComments(const(char)[] c1, const(char)[] c2) | |
0 | 3376 { |
79 | 3377 //writef("Lexer.combineComments('%s', '%s')\n", c1, c2); |
0 | 3378 |
79 | 3379 char[] c = cast(char[]) c2; |
3380 | |
3381 if (c1 !is null) | |
0 | 3382 { |
79 | 3383 c = cast(char[]) c1; |
3384 if (c2 !is null) | |
0 | 3385 { |
79 | 3386 c = cast(char[]) (GC.malloc(c1.length + 1 + c2.length)[0 .. c1.length + 1 + c2.length]); |
3387 size_t len1 = c1.length; | |
3388 c[0..len1] = c1[]; | |
3389 c[len1++] = '\n'; | |
3390 c[len1 .. len1 + c2.length] = c2[]; | |
0 | 3391 } |
3392 } | |
79 | 3393 |
3394 return cast(string)c; | |
0 | 3395 } |
3396 } |