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