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