Mercurial > projects > ldc
comparison dmd2/lexer.c @ 1452:638d16625da2
LDC 2 compiles again.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sat, 30 May 2009 17:23:32 +0100 |
parents | 356e65836fb5 |
children | a1666b613c15 |
comparison
equal
deleted
inserted
replaced
1423:42bd767ec5a4 | 1452:638d16625da2 |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | 2 // Compiler implementation of the D programming language |
3 // Copyright (c) 1999-2008 by Digital Mars | 3 // Copyright (c) 1999-2009 by Digital Mars |
4 // All Rights Reserved | 4 // All Rights Reserved |
5 // written by Walter Bright | 5 // written by Walter Bright |
6 // http://www.digitalmars.com | 6 // http://www.digitalmars.com |
7 // License for redistribution is by either the Artistic License | 7 // License for redistribution is by either the Artistic License |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | 8 // in artistic.txt, or the GNU General Public License in gnu.txt. |
9 // See the included readme.txt for details. | 9 // See the included readme.txt for details. |
10 | |
11 #if IN_LLVM | |
12 #include <cmath> | |
13 #endif | |
10 | 14 |
11 /* Lexical Analyzer */ | 15 /* Lexical Analyzer */ |
12 | 16 |
13 #include <stdio.h> | 17 #include <stdio.h> |
14 #include <string.h> | 18 #include <string.h> |
16 #include <stdarg.h> | 20 #include <stdarg.h> |
17 #include <errno.h> | 21 #include <errno.h> |
18 #include <wchar.h> | 22 #include <wchar.h> |
19 #include <stdlib.h> | 23 #include <stdlib.h> |
20 #include <assert.h> | 24 #include <assert.h> |
21 #include <sys/time.h> | 25 #include <time.h> // for time() and ctime() |
22 #include <math.h> | 26 |
23 | 27 #include "rmem.h" |
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 | 28 |
44 #include "stringtable.h" | 29 #include "stringtable.h" |
45 | 30 |
46 #include "lexer.h" | 31 #include "lexer.h" |
47 #include "utf.h" | 32 #include "utf.h" |
117 | 102 |
118 p = buffer; | 103 p = buffer; |
119 switch (value) | 104 switch (value) |
120 { | 105 { |
121 case TOKint32v: | 106 case TOKint32v: |
122 #if IN_GCC | |
123 sprintf(buffer,"%d",(d_int32)int64value); | 107 sprintf(buffer,"%d",(d_int32)int64value); |
124 #else | |
125 sprintf(buffer,"%d",int32value); | |
126 #endif | |
127 break; | 108 break; |
128 | 109 |
129 case TOKuns32v: | 110 case TOKuns32v: |
130 case TOKcharv: | 111 case TOKcharv: |
131 case TOKwcharv: | 112 case TOKwcharv: |
132 case TOKdcharv: | 113 case TOKdcharv: |
133 #if IN_GCC | |
134 sprintf(buffer,"%uU",(d_uns32)uns64value); | 114 sprintf(buffer,"%uU",(d_uns32)uns64value); |
135 #else | |
136 sprintf(buffer,"%uU",uns32value); | |
137 #endif | |
138 break; | 115 break; |
139 | 116 |
140 case TOKint64v: | 117 case TOKint64v: |
141 sprintf(buffer,"%lldL",int64value); | 118 sprintf(buffer,"%jdL",int64value); |
142 break; | 119 break; |
143 | 120 |
144 case TOKuns64v: | 121 case TOKuns64v: |
145 sprintf(buffer,"%lluUL",uns64value); | 122 sprintf(buffer,"%juUL",uns64value); |
146 break; | 123 break; |
147 | 124 |
148 #if IN_GCC | 125 #if IN_GCC |
149 case TOKfloat32v: | 126 case TOKfloat32v: |
150 case TOKfloat64v: | 127 case TOKfloat64v: |
401 TOK Lexer::peekNext() | 378 TOK Lexer::peekNext() |
402 { | 379 { |
403 return peek(&token)->value; | 380 return peek(&token)->value; |
404 } | 381 } |
405 | 382 |
383 /*********************** | |
384 * Look 2 tokens ahead at value. | |
385 */ | |
386 | |
387 TOK Lexer::peekNext2() | |
388 { | |
389 Token *t = peek(&token); | |
390 return peek(t)->value; | |
391 } | |
392 | |
406 /********************************* | 393 /********************************* |
407 * tk is on the opening (. | 394 * tk is on the opening (. |
408 * Look ahead and return token that is past the closing ). | 395 * Look ahead and return token that is past the closing ). |
409 */ | 396 */ |
410 | 397 |
597 #endif | 584 #endif |
598 | 585 |
599 case '"': | 586 case '"': |
600 t->value = escapeStringConstant(t,0); | 587 t->value = escapeStringConstant(t,0); |
601 return; | 588 return; |
602 | 589 #if ! TEXTUAL_ASSEMBLY_OUT |
603 case '\\': // escaped string literal | 590 case '\\': // escaped string literal |
604 { unsigned c; | 591 { unsigned c; |
592 unsigned char *pstart = p; | |
605 | 593 |
606 stringbuffer.reset(); | 594 stringbuffer.reset(); |
607 do | 595 do |
608 { | 596 { |
609 p++; | 597 p++; |
626 stringbuffer.writeByte(0); | 614 stringbuffer.writeByte(0); |
627 t->ustring = (unsigned char *)mem.malloc(stringbuffer.offset); | 615 t->ustring = (unsigned char *)mem.malloc(stringbuffer.offset); |
628 memcpy(t->ustring, stringbuffer.data, stringbuffer.offset); | 616 memcpy(t->ustring, stringbuffer.data, stringbuffer.offset); |
629 t->postfix = 0; | 617 t->postfix = 0; |
630 t->value = TOKstring; | 618 t->value = TOKstring; |
619 if (!global.params.useDeprecated) | |
620 error("Escape String literal %.*s is deprecated, use double quoted string literal \"%.*s\" instead", p - pstart, pstart, p - pstart, pstart); | |
631 return; | 621 return; |
632 } | 622 } |
633 | 623 #endif |
634 case 'l': | 624 case 'l': |
635 case 'L': | 625 case 'L': |
636 #endif | 626 #endif |
637 case 'a': case 'b': case 'c': case 'd': case 'e': | 627 case 'a': case 'b': case 'c': case 'd': case 'e': |
638 case 'f': case 'g': case 'h': case 'i': case 'j': | 628 case 'f': case 'g': case 'h': case 'i': case 'j': |
689 | 679 |
690 #if DMDV1 | 680 #if DMDV1 |
691 if (mod && id == Id::FILE) | 681 if (mod && id == Id::FILE) |
692 { | 682 { |
693 t->ustring = (unsigned char *)(loc.filename ? loc.filename : mod->ident->toChars()); | 683 t->ustring = (unsigned char *)(loc.filename ? loc.filename : mod->ident->toChars()); |
694 goto Lstring; | 684 goto Lstr; |
695 } | 685 } |
696 else if (mod && id == Id::LINE) | 686 else if (mod && id == Id::LINE) |
697 { | 687 { |
698 t->value = TOKint64v; | 688 t->value = TOKint64v; |
699 t->uns64value = loc.linnum; | 689 t->uns64value = loc.linnum; |
701 else | 691 else |
702 #endif | 692 #endif |
703 if (id == Id::DATE) | 693 if (id == Id::DATE) |
704 { | 694 { |
705 t->ustring = (unsigned char *)date; | 695 t->ustring = (unsigned char *)date; |
706 goto Lstring; | 696 goto Lstr; |
707 } | 697 } |
708 else if (id == Id::TIME) | 698 else if (id == Id::TIME) |
709 { | 699 { |
710 t->ustring = (unsigned char *)time; | 700 t->ustring = (unsigned char *)time; |
711 goto Lstring; | 701 goto Lstr; |
712 } | 702 } |
713 else if (id == Id::VENDOR) | 703 else if (id == Id::VENDOR) |
714 { | 704 { |
715 t->ustring = (unsigned char *)"LDC"; | 705 t->ustring = (unsigned char *)"LDC"; |
716 goto Lstring; | 706 goto Lstr; |
717 } | 707 } |
718 else if (id == Id::TIMESTAMP) | 708 else if (id == Id::TIMESTAMP) |
719 { | 709 { |
720 t->ustring = (unsigned char *)timestamp; | 710 t->ustring = (unsigned char *)timestamp; |
721 Lstring: | 711 Lstr: |
722 t->value = TOKstring; | 712 t->value = TOKstring; |
723 Llen: | 713 Llen: |
724 t->postfix = 0; | 714 t->postfix = 0; |
725 t->len = strlen((char *)t->ustring); | 715 t->len = strlen((char *)t->ustring); |
726 } | 716 } |
1226 /******************************************* | 1216 /******************************************* |
1227 * Parse escape sequence. | 1217 * Parse escape sequence. |
1228 */ | 1218 */ |
1229 | 1219 |
1230 unsigned Lexer::escapeSequence() | 1220 unsigned Lexer::escapeSequence() |
1231 { unsigned c; | 1221 { unsigned c = *p; |
1222 | |
1223 #ifdef TEXTUAL_ASSEMBLY_OUT | |
1224 return c; | |
1225 #endif | |
1232 int n; | 1226 int n; |
1233 int ndigits; | 1227 int ndigits; |
1234 | 1228 |
1235 c = *p; | |
1236 switch (c) | 1229 switch (c) |
1237 { | 1230 { |
1238 case '\'': | 1231 case '\'': |
1239 case '"': | 1232 case '"': |
1240 case '?': | 1233 case '?': |
1585 nest = 0; | 1578 nest = 0; |
1586 } | 1579 } |
1587 else | 1580 else |
1588 { delimright = c; | 1581 { delimright = c; |
1589 nest = 0; | 1582 nest = 0; |
1583 if (isspace(c)) | |
1584 error("delimiter cannot be whitespace"); | |
1590 } | 1585 } |
1591 } | 1586 } |
1592 else | 1587 else |
1593 { | 1588 { |
1594 if (blankrol) | 1589 if (blankrol) |
1606 goto Ldone; | 1601 goto Ldone; |
1607 } | 1602 } |
1608 } | 1603 } |
1609 else if (c == delimright) | 1604 else if (c == delimright) |
1610 goto Ldone; | 1605 goto Ldone; |
1611 if (startline && isalpha(c)) | 1606 if (startline && isalpha(c) && hereid) |
1612 { Token t; | 1607 { Token t; |
1613 unsigned char *psave = p; | 1608 unsigned char *psave = p; |
1614 p--; | 1609 p--; |
1615 scan(&t); // read in possible heredoc identifier | 1610 scan(&t); // read in possible heredoc identifier |
1616 //printf("endid = '%s'\n", t.ident->toChars()); | 1611 //printf("endid = '%s'\n", t.ident->toChars()); |
1715 while (1) | 1710 while (1) |
1716 { | 1711 { |
1717 c = *p++; | 1712 c = *p++; |
1718 switch (c) | 1713 switch (c) |
1719 { | 1714 { |
1715 #if !( TEXTUAL_ASSEMBLY_OUT ) | |
1720 case '\\': | 1716 case '\\': |
1721 switch (*p) | 1717 switch (*p) |
1722 { | 1718 { |
1723 case 'u': | 1719 case 'u': |
1724 case 'U': | 1720 case 'U': |
1730 default: | 1726 default: |
1731 c = escapeSequence(); | 1727 c = escapeSequence(); |
1732 break; | 1728 break; |
1733 } | 1729 } |
1734 break; | 1730 break; |
1735 | 1731 #endif |
1736 case '\n': | 1732 case '\n': |
1737 loc.linnum++; | 1733 loc.linnum++; |
1738 break; | 1734 break; |
1739 | 1735 |
1740 case '\r': | 1736 case '\r': |
1791 //printf("Lexer::charConstant\n"); | 1787 //printf("Lexer::charConstant\n"); |
1792 p++; | 1788 p++; |
1793 c = *p++; | 1789 c = *p++; |
1794 switch (c) | 1790 switch (c) |
1795 { | 1791 { |
1792 #if ! TEXTUAL_ASSEMBLY_OUT | |
1796 case '\\': | 1793 case '\\': |
1797 switch (*p) | 1794 switch (*p) |
1798 { | 1795 { |
1799 case 'u': | 1796 case 'u': |
1800 t->uns64value = escapeSequence(); | 1797 t->uns64value = escapeSequence(); |
1810 default: | 1807 default: |
1811 t->uns64value = escapeSequence(); | 1808 t->uns64value = escapeSequence(); |
1812 break; | 1809 break; |
1813 } | 1810 } |
1814 break; | 1811 break; |
1815 | 1812 #endif |
1816 case '\n': | 1813 case '\n': |
1817 L1: | 1814 L1: |
1818 loc.linnum++; | 1815 loc.linnum++; |
1819 case '\r': | 1816 case '\r': |
1820 case 0: | 1817 case 0: |
2959 { "macro", TOKmacro }, | 2956 { "macro", TOKmacro }, |
2960 #if DMDV2 | 2957 #if DMDV2 |
2961 { "pure", TOKpure }, | 2958 { "pure", TOKpure }, |
2962 { "nothrow", TOKnothrow }, | 2959 { "nothrow", TOKnothrow }, |
2963 { "__thread", TOKtls }, | 2960 { "__thread", TOKtls }, |
2961 { "__gshared", TOKgshared }, | |
2964 { "__traits", TOKtraits }, | 2962 { "__traits", TOKtraits }, |
2965 { "__overloadset", TOKoverloadset }, | 2963 { "__overloadset", TOKoverloadset }, |
2966 { "__FILE__", TOKfile }, | 2964 { "__FILE__", TOKfile }, |
2967 { "__LINE__", TOKline }, | 2965 { "__LINE__", TOKline }, |
2968 { "shared", TOKshared }, | 2966 { "shared", TOKshared }, |
3093 Token::tochars[TOKdotexp] = "dotexp"; | 3091 Token::tochars[TOKdotexp] = "dotexp"; |
3094 Token::tochars[TOKdotti] = "dotti"; | 3092 Token::tochars[TOKdotti] = "dotti"; |
3095 Token::tochars[TOKdotvar] = "dotvar"; | 3093 Token::tochars[TOKdotvar] = "dotvar"; |
3096 Token::tochars[TOKdottype] = "dottype"; | 3094 Token::tochars[TOKdottype] = "dottype"; |
3097 Token::tochars[TOKsymoff] = "symoff"; | 3095 Token::tochars[TOKsymoff] = "symoff"; |
3098 Token::tochars[TOKtypedot] = "typedot"; | |
3099 Token::tochars[TOKarraylength] = "arraylength"; | 3096 Token::tochars[TOKarraylength] = "arraylength"; |
3100 Token::tochars[TOKarrayliteral] = "arrayliteral"; | 3097 Token::tochars[TOKarrayliteral] = "arrayliteral"; |
3101 Token::tochars[TOKassocarrayliteral] = "assocarrayliteral"; | 3098 Token::tochars[TOKassocarrayliteral] = "assocarrayliteral"; |
3102 Token::tochars[TOKstructliteral] = "structliteral"; | 3099 Token::tochars[TOKstructliteral] = "structliteral"; |
3103 Token::tochars[TOKstring] = "string"; | 3100 Token::tochars[TOKstring] = "string"; |
3104 Token::tochars[TOKdsymbol] = "symbol"; | 3101 Token::tochars[TOKdsymbol] = "symbol"; |
3105 Token::tochars[TOKtuple] = "tuple"; | 3102 Token::tochars[TOKtuple] = "tuple"; |
3106 Token::tochars[TOKdeclaration] = "declaration"; | 3103 Token::tochars[TOKdeclaration] = "declaration"; |
3107 Token::tochars[TOKdottd] = "dottd"; | 3104 Token::tochars[TOKdottd] = "dottd"; |
3108 Token::tochars[TOKon_scope_exit] = "scope(exit)"; | 3105 Token::tochars[TOKon_scope_exit] = "scope(exit)"; |
3109 } | 3106 Token::tochars[TOKon_scope_success] = "scope(success)"; |
3107 Token::tochars[TOKon_scope_failure] = "scope(failure)"; | |
3108 } |