1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
|
// lex.cc -- Go frontend lexer.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include "lex.h"
// Manage mapping from keywords to the Keyword codes.
class Keywords
{
public:
// The structure which maps keywords to codes.
struct Mapping
{
// Keyword string.
const char* keystring;
// Keyword code.
Keyword keycode;
};
// Return the parsecode corresponding to KEYSTRING, or
// KEYWORD_INVALID if it is not a keyword.
Keyword
keyword_to_code(const char* keyword, size_t len) const;
// Return the string for a keyword.
const char*
keyword_to_string(Keyword) const;
private:
static const Mapping mapping_[];
static const int count_;
};
// Mapping from keyword string to keyword code. This array must be
// kept in sorted order, and the order must match the Keyword enum.
// Strings are looked up using bsearch.
const Keywords::Mapping
Keywords::mapping_[] =
{
{ NULL, KEYWORD_INVALID },
{ "__asm__", KEYWORD_ASM },
{ "break", KEYWORD_BREAK },
{ "case", KEYWORD_CASE },
{ "chan", KEYWORD_CHAN },
{ "const", KEYWORD_CONST },
{ "continue", KEYWORD_CONTINUE },
{ "default", KEYWORD_DEFAULT },
{ "defer", KEYWORD_DEFER },
{ "else", KEYWORD_ELSE },
{ "fallthrough", KEYWORD_FALLTHROUGH },
{ "for", KEYWORD_FOR },
{ "func", KEYWORD_FUNC },
{ "go", KEYWORD_GO },
{ "goto", KEYWORD_GOTO },
{ "if", KEYWORD_IF },
{ "import", KEYWORD_IMPORT },
{ "interface", KEYWORD_INTERFACE },
{ "map", KEYWORD_MAP },
{ "package", KEYWORD_PACKAGE },
{ "range", KEYWORD_RANGE },
{ "return", KEYWORD_RETURN },
{ "select", KEYWORD_SELECT },
{ "struct", KEYWORD_STRUCT },
{ "switch", KEYWORD_SWITCH },
{ "type", KEYWORD_TYPE },
{ "var", KEYWORD_VAR }
};
// Number of entries in the map.
const int Keywords::count_ =
sizeof(Keywords::mapping_) / sizeof(Keywords::mapping_[0]);
// Comparison function passed to bsearch.
extern "C"
{
struct Keywords_search_key
{
const char* str;
size_t len;
};
static int
keyword_compare(const void* keyv, const void* mapv)
{
const Keywords_search_key* key =
static_cast<const Keywords_search_key*>(keyv);
const Keywords::Mapping* map =
static_cast<const Keywords::Mapping*>(mapv);
if (map->keystring == NULL)
return 1;
int i = strncmp(key->str, map->keystring, key->len);
if (i != 0)
return i;
if (map->keystring[key->len] != '\0')
return -1;
return 0;
}
} // End extern "C".
// Convert a string to a keyword code. Return KEYWORD_INVALID if the
// string is not a keyword.
Keyword
Keywords::keyword_to_code(const char* keyword, size_t len) const
{
Keywords_search_key key;
key.str = keyword;
key.len = len;
void* mapv = bsearch(&key,
this->mapping_,
this->count_,
sizeof(this->mapping_[0]),
keyword_compare);
if (mapv == NULL)
return KEYWORD_INVALID;
Mapping* map = static_cast<Mapping*>(mapv);
return map->keycode;
}
// Convert a keyword code to a string.
const char*
Keywords::keyword_to_string(Keyword code) const
{
gcc_assert(code > KEYWORD_INVALID && code < this->count_);
const Mapping* map = &this->mapping_[code];
gcc_assert(map->keycode == code);
return map->keystring;
}
// There is one instance of the Keywords class.
static Keywords keywords;
// Class Token.
// Make a general token.
Token::Token(Classification classification, source_location location)
: classification_(classification), location_(location)
{
}
// Destroy a token.
Token::~Token()
{
this->clear();
}
// Clear a token--release memory.
void
Token::clear()
{
if (this->classification_ == TOKEN_INTEGER)
mpz_clear(this->u_.integer_value);
else if (this->classification_ == TOKEN_FLOAT
|| this->classification_ == TOKEN_IMAGINARY)
mpfr_clear(this->u_.float_value);
}
// Construct a token.
Token::Token(const Token& tok)
: classification_(tok.classification_), location_(tok.location_)
{
switch (this->classification_)
{
case TOKEN_INVALID:
case TOKEN_EOF:
break;
case TOKEN_KEYWORD:
this->u_.keyword = tok.u_.keyword;
break;
case TOKEN_IDENTIFIER:
case TOKEN_STRING:
this->u_.string_value = tok.u_.string_value;
break;
case TOKEN_OPERATOR:
this->u_.op = tok.u_.op;
break;
case TOKEN_INTEGER:
mpz_init_set(this->u_.integer_value, tok.u_.integer_value);
break;
case TOKEN_FLOAT:
case TOKEN_IMAGINARY:
mpfr_init_set(this->u_.float_value, tok.u_.float_value, GMP_RNDN);
break;
default:
gcc_unreachable();
}
}
// Assign to a token.
Token&
Token::operator=(const Token& tok)
{
this->clear();
this->classification_ = tok.classification_;
this->location_ = tok.location_;
switch (tok.classification_)
{
case TOKEN_INVALID:
case TOKEN_EOF:
break;
case TOKEN_KEYWORD:
this->u_.keyword = tok.u_.keyword;
break;
case TOKEN_IDENTIFIER:
this->u_.identifier_value.name = tok.u_.identifier_value.name;
this->u_.identifier_value.is_exported =
tok.u_.identifier_value.is_exported;
break;
case TOKEN_STRING:
this->u_.string_value = tok.u_.string_value;
break;
case TOKEN_OPERATOR:
this->u_.op = tok.u_.op;
break;
case TOKEN_INTEGER:
mpz_init_set(this->u_.integer_value, tok.u_.integer_value);
break;
case TOKEN_FLOAT:
case TOKEN_IMAGINARY:
mpfr_init_set(this->u_.float_value, tok.u_.float_value, GMP_RNDN);
break;
default:
gcc_unreachable();
}
return *this;
}
// Print the token for debugging.
void
Token::print(FILE* file) const
{
switch (this->classification_)
{
case TOKEN_INVALID:
fprintf(file, "invalid");
break;
case TOKEN_EOF:
fprintf(file, "EOF");
break;
case TOKEN_KEYWORD:
fprintf(file, "keyword %s", keywords.keyword_to_string(this->u_.keyword));
break;
case TOKEN_IDENTIFIER:
fprintf(file, "identifier \"%s\"", this->u_.string_value->c_str());
break;
case TOKEN_STRING:
fprintf(file, "quoted string \"%s\"", this->u_.string_value->c_str());
break;
case TOKEN_INTEGER:
fprintf(file, "integer ");
mpz_out_str(file, 10, this->u_.integer_value);
break;
case TOKEN_FLOAT:
fprintf(file, "float ");
mpfr_out_str(file, 10, 0, this->u_.float_value, GMP_RNDN);
break;
case TOKEN_IMAGINARY:
fprintf(file, "imaginary ");
mpfr_out_str(file, 10, 0, this->u_.float_value, GMP_RNDN);
break;
case TOKEN_OPERATOR:
fprintf(file, "operator ");
switch (this->u_.op)
{
case OPERATOR_INVALID:
fprintf(file, "invalid");
break;
case OPERATOR_OROR:
fprintf(file, "||");
break;
case OPERATOR_ANDAND:
fprintf(file, "&&");
break;
case OPERATOR_EQEQ:
fprintf(file, "==");
break;
case OPERATOR_NOTEQ:
fprintf(file, "!=");
break;
case OPERATOR_LT:
fprintf(file, "<");
break;
case OPERATOR_LE:
fprintf(file, "<=");
break;
case OPERATOR_GT:
fprintf(file, ">");
break;
case OPERATOR_GE:
fprintf(file, ">=");
break;
case OPERATOR_PLUS:
fprintf(file, "+");
break;
case OPERATOR_MINUS:
fprintf(file, "-");
break;
case OPERATOR_OR:
fprintf(file, "|");
break;
case OPERATOR_XOR:
fprintf(file, "^");
break;
case OPERATOR_MULT:
fprintf(file, "*");
break;
case OPERATOR_DIV:
fprintf(file, "/");
break;
case OPERATOR_MOD:
fprintf(file, "%%");
break;
case OPERATOR_LSHIFT:
fprintf(file, "<<");
break;
case OPERATOR_RSHIFT:
fprintf(file, ">>");
break;
case OPERATOR_AND:
fprintf(file, "&");
break;
case OPERATOR_BITCLEAR:
fprintf(file, "&^");
break;
case OPERATOR_NOT:
fprintf(file, "!");
break;
case OPERATOR_CHANOP:
fprintf(file, "<-");
break;
case OPERATOR_EQ:
fprintf(file, "=");
break;
case OPERATOR_PLUSEQ:
fprintf(file, "+=");
break;
case OPERATOR_MINUSEQ:
fprintf(file, "-=");
break;
case OPERATOR_OREQ:
fprintf(file, "|=");
break;
case OPERATOR_XOREQ:
fprintf(file, "^=");
break;
case OPERATOR_MULTEQ:
fprintf(file, "*=");
break;
case OPERATOR_DIVEQ:
fprintf(file, "/=");
break;
case OPERATOR_MODEQ:
fprintf(file, "%%=");
break;
case OPERATOR_LSHIFTEQ:
fprintf(file, "<<=");
break;
case OPERATOR_RSHIFTEQ:
fprintf(file, ">>=");
break;
case OPERATOR_ANDEQ:
fprintf(file, "&=");
break;
case OPERATOR_BITCLEAREQ:
fprintf(file, "&^=");
break;
case OPERATOR_PLUSPLUS:
fprintf(file, "++");
break;
case OPERATOR_MINUSMINUS:
fprintf(file, "--");
break;
case OPERATOR_COLON:
fprintf(file, ":");
break;
case OPERATOR_COLONEQ:
fprintf(file, ":=");
break;
case OPERATOR_SEMICOLON:
fprintf(file, ";");
break;
case OPERATOR_DOT:
fprintf(file, ".");
break;
case OPERATOR_COMMA:
fprintf(file, ",");
break;
case OPERATOR_LPAREN:
fprintf(file, "(");
break;
case OPERATOR_RPAREN:
fprintf(file, ")");
break;
case OPERATOR_LCURLY:
fprintf(file, "{");
break;
case OPERATOR_RCURLY:
fprintf(file, "}");
break;
case OPERATOR_LSQUARE:
fprintf(file, "[");
break;
case OPERATOR_RSQUARE:
fprintf(file, "]");
break;
default:
gcc_unreachable();
}
break;
default:
gcc_unreachable();
}
}
// Class Lex.
Lex::Lex(const char* input_file_name, FILE* input_file)
: input_file_name_(input_file_name), input_file_(input_file),
linebuf_(NULL), linebufsize_(120), linesize_(0), lineoff_(0),
lineno_(0), add_semi_at_eol_(false)
{
this->linebuf_ = new char[this->linebufsize_];
linemap_add(line_table, LC_ENTER, 0, input_file_name, 1);
}
Lex::~Lex()
{
delete[] this->linebuf_;
linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
}
// Read a new line from the file.
ssize_t
Lex::get_line()
{
char* buf = this->linebuf_;
size_t size = this->linebufsize_;
FILE* file = this->input_file_;
size_t cur = 0;
while (true)
{
int c = getc(file);
if (c == EOF)
{
if (cur == 0)
return -1;
break;
}
if (cur + 1 >= size)
{
size_t ns = 2 * size + 1;
if (ns < size || static_cast<ssize_t>(ns) < 0)
error_at(this->location(), "out of memory");
char* nb = new char[ns];
memcpy(nb, buf, cur);
delete[] buf;
buf = nb;
size = ns;
}
buf[cur] = c;
++cur;
if (c == '\n')
break;
}
buf[cur] = '\0';
this->linebuf_ = buf;
this->linebufsize_ = size;
return cur;
}
// See if we need to read a new line. Return true if there is a new
// line, false if we are at EOF.
bool
Lex::require_line()
{
if (this->lineoff_ < this->linesize_)
return true;
ssize_t got = this->get_line();
if (got < 0)
return false;
++this->lineno_;
this->linesize_= got;
this->lineoff_ = 0;
linemap_line_start(line_table, this->lineno_, this->linesize_);
return true;
}
// Get the current location.
source_location
Lex::location() const
{
source_location location;
LINEMAP_POSITION_FOR_COLUMN(location, line_table, this->lineoff_ + 1);
return location;
}
// Get a location slightly before the current one. This is used for
// slightly more efficient handling of operator tokens.
source_location
Lex::earlier_location(int chars) const
{
source_location location;
LINEMAP_POSITION_FOR_COLUMN(location, line_table, this->lineoff_ + 1 - chars);
return location;
}
// Get the next token.
Token
Lex::next_token()
{
while (true)
{
if (!this->require_line())
{
bool add_semi_at_eol = this->add_semi_at_eol_;
this->add_semi_at_eol_ = false;
if (add_semi_at_eol)
return this->make_operator(OPERATOR_SEMICOLON, 1);
return this->make_eof_token();
}
const char* p = this->linebuf_ + this->lineoff_;
const char* pend = this->linebuf_ + this->linesize_;
while (p < pend)
{
unsigned char cc = *p;
switch (cc)
{
case ' ': case '\t': case '\r':
++p;
// Skip whitespace quickly.
while (*p == ' ' || *p == '\t' || *p == '\r')
++p;
break;
case '\n':
{
++p;
bool add_semi_at_eol = this->add_semi_at_eol_;
this->add_semi_at_eol_ = false;
if (add_semi_at_eol)
{
this->lineoff_ = p - this->linebuf_;
return this->make_operator(OPERATOR_SEMICOLON, 1);
}
}
break;
case '/':
if (p[1] == '/')
{
this->lineoff_ = p + 2 - this->linebuf_;
this->skip_cpp_comment();
p = pend;
if (p[-1] == '\n' && this->add_semi_at_eol_)
--p;
}
else if (p[1] == '*')
{
this->lineoff_ = p - this->linebuf_;
source_location location = this->location();
if (!this->skip_c_comment())
return Token::make_invalid_token(location);
p = this->linebuf_ + this->lineoff_;
pend = this->linebuf_ + this->linesize_;
}
else if (p[1] == '=')
{
this->add_semi_at_eol_ = false;
this->lineoff_ = p + 2 - this->linebuf_;
return this->make_operator(OPERATOR_DIVEQ, 2);
}
else
{
this->add_semi_at_eol_ = false;
this->lineoff_ = p + 1 - this->linebuf_;
return this->make_operator(OPERATOR_DIV, 1);
}
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
case 'Y': case 'Z':
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case '_':
this->lineoff_ = p - this->linebuf_;
return this->gather_identifier();
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
this->add_semi_at_eol_ = true;
this->lineoff_ = p - this->linebuf_;
return this->gather_number();
case '\'':
this->add_semi_at_eol_ = true;
this->lineoff_ = p - this->linebuf_;
return this->gather_character();
case '"':
this->add_semi_at_eol_ = true;
this->lineoff_ = p - this->linebuf_;
return this->gather_string();
case '`':
this->add_semi_at_eol_ = true;
this->lineoff_ = p - this->linebuf_;
return this->gather_raw_string();
case '<':
case '>':
case '&':
if (p + 2 < pend)
{
this->add_semi_at_eol_ = false;
Operator op = this->three_character_operator(cc, p[1], p[2]);
if (op != OPERATOR_INVALID)
{
this->lineoff_ = p + 3 - this->linebuf_;
return this->make_operator(op, 3);
}
}
// Fall through.
case '|':
case '=':
case '!':
case '+':
case '-':
case '^':
case '*':
// '/' handled above.
case '%':
case ':':
case ';':
case ',':
case '(': case ')':
case '{': case '}':
case '[': case ']':
{
this->add_semi_at_eol_ = false;
Operator op = this->two_character_operator(cc, p[1]);
int chars;
if (op != OPERATOR_INVALID)
{
++p;
chars = 2;
}
else
{
op = this->one_character_operator(cc);
chars = 1;
}
this->lineoff_ = p + 1 - this->linebuf_;
return this->make_operator(op, chars);
}
case '.':
if (p[1] >= '0' && p[1] <= '9')
{
this->add_semi_at_eol_ = true;
this->lineoff_ = p - this->linebuf_;
return this->gather_number();
}
if (p[1] == '.' && p[2] == '.')
{
this->add_semi_at_eol_ = false;
this->lineoff_ = p + 3 - this->linebuf_;
return this->make_operator(OPERATOR_ELLIPSIS, 3);
}
this->add_semi_at_eol_ = false;
this->lineoff_ = p + 1 - this->linebuf_;
return this->make_operator(OPERATOR_DOT, 1);
default:
{
unsigned int ci;
bool issued_error;
this->lineoff_ = p - this->linebuf_;
this->advance_one_utf8_char(p, &ci, &issued_error);
if (Lex::is_unicode_letter(ci))
return this->gather_identifier();
if (!issued_error)
error_at(this->location(),
"invalid character 0x%x in input file",
ci);
p = pend;
break;
}
}
}
this->lineoff_ = p - this->linebuf_;
}
}
// Fetch one UTF-8 character from a string. Set *VALUE to the value.
// Return the number of bytes read from the string. Returns 0 if the
// string does not point to a valid UTF-8 character.
int
Lex::fetch_char(const char* p, unsigned int* value)
{
unsigned char c = *p;
if (c <= 0x7f)
{
*value = c;
return 1;
}
else if ((c & 0xe0) == 0xc0
&& (p[1] & 0xc0) == 0x80)
{
*value = (((c & 0x1f) << 6)
+ (p[1] & 0x3f));
if (*value <= 0x7f)
{
*value = 0xfffd;
return 0;
}
return 2;
}
else if ((c & 0xf0) == 0xe0
&& (p[1] & 0xc0) == 0x80
&& (p[2] & 0xc0) == 0x80)
{
*value = (((c & 0xf) << 12)
+ ((p[1] & 0x3f) << 6)
+ (p[2] & 0x3f));
if (*value <= 0x7ff)
{
*value = 0xfffd;
return 0;
}
return 3;
}
else if ((c & 0xf8) == 0xf0
&& (p[1] & 0xc0) == 0x80
&& (p[2] & 0xc0) == 0x80
&& (p[3] & 0xc0) == 0x80)
{
*value = (((c & 0x7) << 18)
+ ((p[1] & 0x3f) << 12)
+ ((p[2] & 0x3f) << 6)
+ (p[3] & 0x3f));
if (*value <= 0xffff)
{
*value = 0xfffd;
return 0;
}
return 4;
}
else
{
/* Invalid encoding. Return the Unicode replacement
character. */
*value = 0xfffd;
return 0;
}
}
// Advance one UTF-8 character. Return the pointer beyond the
// character. Set *VALUE to the value. Set *ISSUED_ERROR if an error
// was issued.
const char*
Lex::advance_one_utf8_char(const char* p, unsigned int* value,
bool* issued_error)
{
*issued_error = false;
if (*p == '\0')
{
error_at(this->location(), "invalid NUL byte");
*issued_error = true;
*value = 0;
return p + 1;
}
int adv = Lex::fetch_char(p, value);
if (adv == 0)
{
error_at(this->location(), "invalid UTF-8 encoding");
*issued_error = true;
return p + 1;
}
return p + adv;
}
// Pick up an identifier.
Token
Lex::gather_identifier()
{
const char* pstart = this->linebuf_ + this->lineoff_;
const char* p = pstart;
const char* pend = this->linebuf_ + this->linesize_;
bool is_first = true;
bool is_exported = false;
bool has_non_ascii_char = false;
std::string buf;
while (p < pend)
{
unsigned char cc = *p;
if (cc <= 0x7f)
{
if ((cc < 'A' || cc > 'Z')
&& (cc < 'a' || cc > 'z')
&& cc != '_'
&& (cc < '0' || cc > '9'))
break;
++p;
if (is_first)
{
is_exported = cc >= 'A' && cc <= 'Z';
is_first = false;
}
if (has_non_ascii_char)
buf.push_back(cc);
}
else
{
unsigned int ci;
bool issued_error;
this->lineoff_ = p - this->linebuf_;
const char* pnext = this->advance_one_utf8_char(p, &ci,
&issued_error);
if (!Lex::is_unicode_letter(ci) && !Lex::is_unicode_digit(ci))
{
// There is no valid place for a non-ASCII character
// other than an identifier, so we get better error
// handling behaviour if we swallow this character after
// giving an error.
if (!issued_error)
error_at(this->location(),
"invalid character 0x%x in identifier",
ci);
}
if (is_first)
{
is_exported = Lex::is_unicode_uppercase(ci);
is_first = false;
}
if (!has_non_ascii_char)
{
buf.assign(pstart, p - pstart);
has_non_ascii_char = true;
}
p = pnext;
char ubuf[50];
// This assumes that all assemblers can handle an identifier
// with a '$' character.
snprintf(ubuf, sizeof ubuf, "$U%x$", ci);
buf.append(ubuf);
}
}
source_location location = this->location();
this->add_semi_at_eol_ = true;
this->lineoff_ = p - this->linebuf_;
if (has_non_ascii_char)
return Token::make_identifier_token(buf, is_exported, location);
else
{
Keyword code = keywords.keyword_to_code(pstart, p - pstart);
if (code == KEYWORD_INVALID)
return Token::make_identifier_token(std::string(pstart, p - pstart),
is_exported, location);
else
{
switch (code)
{
case KEYWORD_BREAK:
case KEYWORD_CONTINUE:
case KEYWORD_FALLTHROUGH:
case KEYWORD_RETURN:
break;
default:
this->add_semi_at_eol_ = false;
break;
}
return Token::make_keyword_token(code, location);
}
}
}
// Return whether C is a hex digit.
bool
Lex::is_hex_digit(char c)
{
return ((c >= '0' && c <= '9')
|| (c >= 'A' && c <= 'F')
|| (c >= 'a' && c <= 'f'));
}
// Return whether an exponent could start at P.
bool
Lex::could_be_exponent(const char* p, const char* pend)
{
if (*p != 'e' && *p != 'E')
return false;
++p;
if (p >= pend)
return false;
if (*p == '+' || *p == '-')
{
++p;
if (p >= pend)
return false;
}
return *p >= '0' && *p <= '9';
}
// Pick up a number.
Token
Lex::gather_number()
{
const char* pstart = this->linebuf_ + this->lineoff_;
const char* p = pstart;
const char* pend = this->linebuf_ + this->linesize_;
source_location location = this->location();
bool neg = false;
if (*p == '+')
++p;
else if (*p == '-')
{
++p;
neg = true;
}
const char* pnum = p;
if (*p == '0')
{
int base;
if ((p[1] == 'x' || p[1] == 'X')
&& Lex::is_hex_digit(p[2]))
{
base = 16;
p += 2;
pnum = p;
while (p < pend)
{
if (!Lex::is_hex_digit(*p))
break;
++p;
}
}
else
{
base = 8;
pnum = p;
while (p < pend)
{
if (*p < '0' || *p > '7')
break;
++p;
}
}
if (*p != '.' && *p != 'i' && !Lex::could_be_exponent(p, pend))
{
std::string s(pnum, p - pnum);
mpz_t val;
int r = mpz_init_set_str(val, s.c_str(), base);
gcc_assert(r == 0);
if (neg)
mpz_neg(val, val);
this->lineoff_ = p - this->linebuf_;
Token ret = Token::make_integer_token(val, location);
mpz_clear(val);
return ret;
}
}
while (p < pend)
{
if (*p < '0' || *p > '9')
break;
++p;
}
if (*p != '.' && *p != 'i' && !Lex::could_be_exponent(p, pend))
{
std::string s(pnum, p - pnum);
mpz_t val;
int r = mpz_init_set_str(val, s.c_str(), 10);
gcc_assert(r == 0);
if (neg)
mpz_neg(val, val);
this->lineoff_ = p - this->linebuf_;
Token ret = Token::make_integer_token(val, location);
mpz_clear(val);
return ret;
}
if (*p != 'i')
{
bool dot = *p == '.';
++p;
if (!dot)
{
if (*p == '+' || *p == '-')
++p;
}
while (p < pend)
{
if (*p < '0' || *p > '9')
break;
++p;
}
if (dot && Lex::could_be_exponent(p, pend))
{
++p;
if (*p == '+' || *p == '-')
++p;
while (p < pend)
{
if (*p < '0' || *p > '9')
break;
++p;
}
}
}
std::string s(pnum, p - pnum);
mpfr_t val;
int r = mpfr_init_set_str(val, s.c_str(), 10, GMP_RNDN);
gcc_assert(r == 0);
if (neg)
mpfr_neg(val, val, GMP_RNDN);
bool is_imaginary = *p == 'i';
if (is_imaginary)
++p;
this->lineoff_ = p - this->linebuf_;
if (is_imaginary)
{
Token ret = Token::make_imaginary_token(val, location);
mpfr_clear(val);
return ret;
}
else
{
Token ret = Token::make_float_token(val, location);
mpfr_clear(val);
return ret;
}
}
// Advance one character, possibly escaped. Return the pointer beyond
// the character. Set *VALUE to the character. Set *IS_CHARACTER if
// this is a character (e.g., 'a' or '\u1234') rather than a byte
// value (e.g., '\001').
const char*
Lex::advance_one_char(const char* p, bool is_single_quote, unsigned int* value,
bool* is_character)
{
*value = 0;
*is_character = true;
if (*p != '\\')
{
bool issued_error;
const char* ret = this->advance_one_utf8_char(p, value, &issued_error);
if (is_single_quote
&& (*value == '\'' || *value == '\n')
&& !issued_error)
error_at(this->location(), "invalid character literal");
return ret;
}
else
{
++p;
switch (*p)
{
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
*is_character = false;
if (p[1] >= '0' && p[1] <= '7'
&& p[2] >= '0' && p[2] <= '7')
{
*value = ((Lex::octal_value(p[0]) << 6)
+ (Lex::octal_value(p[1]) << 3)
+ Lex::octal_value(p[2]));
if (*value > 255)
{
error_at(this->location(), "invalid octal constant");
*value = 255;
}
return p + 3;
}
error_at(this->location(), "invalid octal character");
return (p[1] >= '0' && p[1] <= '7'
? p + 2
: p + 1);
case 'x':
case 'X':
*is_character = false;
if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2]))
{
*value = (hex_value(p[1]) << 4) + hex_value(p[2]);
return p + 3;
}
error_at(this->location(), "invalid hex character");
return (Lex::is_hex_digit(p[1])
? p + 2
: p + 1);
case 'a':
*value = '\a';
return p + 1;
case 'b':
*value = '\b';
return p + 1;
case 'f':
*value = '\f';
return p + 1;
case 'n':
*value = '\n';
return p + 1;
case 'r':
*value = '\r';
return p + 1;
case 't':
*value = '\t';
return p + 1;
case 'v':
*value = '\v';
return p + 1;
case '\\':
*value = '\\';
return p + 1;
case '\'':
if (!is_single_quote)
error_at(this->location(), "invalid quoted character");
*value = '\'';
return p + 1;
case '"':
if (is_single_quote)
error_at(this->location(), "invalid quoted character");
*value = '"';
return p + 1;
case 'u':
if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2])
&& Lex::is_hex_digit(p[3]) && Lex::is_hex_digit(p[4]))
{
*value = ((hex_value(p[1]) << 12)
+ (hex_value(p[2]) << 8)
+ (hex_value(p[3]) << 4)
+ hex_value(p[4]));
if (*value >= 0xd800 && *value < 0xe000)
{
error_at(this->location(),
"invalid unicode code point 0x%x",
*value);
// Use the replacement character.
*value = 0xfffd;
}
return p + 5;
}
error_at(this->location(), "invalid little unicode code point");
return p + 1;
case 'U':
if (Lex::is_hex_digit(p[1]) && Lex::is_hex_digit(p[2])
&& Lex::is_hex_digit(p[3]) && Lex::is_hex_digit(p[4])
&& Lex::is_hex_digit(p[5]) && Lex::is_hex_digit(p[6])
&& Lex::is_hex_digit(p[7]) && Lex::is_hex_digit(p[8]))
{
*value = ((hex_value(p[1]) << 28)
+ (hex_value(p[2]) << 24)
+ (hex_value(p[3]) << 20)
+ (hex_value(p[4]) << 16)
+ (hex_value(p[5]) << 12)
+ (hex_value(p[6]) << 8)
+ (hex_value(p[7]) << 4)
+ hex_value(p[8]));
if (*value > 0x10ffff
|| (*value >= 0xd800 && *value < 0xe000))
{
error_at(this->location(), "invalid unicode code point 0x%x",
*value);
// Use the replacement character.
*value = 0xfffd;
}
return p + 9;
}
error_at(this->location(), "invalid big unicode code point");
return p + 1;
default:
error_at(this->location(), "invalid character after %<\\%>");
*value = *p;
return p + 1;
}
}
}
// Append V to STR. IS_CHARACTER is true for a character which should
// be stored in UTF-8, false for a general byte value which should be
// stored directly.
void
Lex::append_char(unsigned int v, bool is_character, std::string* str,
source_location location)
{
char buf[4];
size_t len;
if (v <= 0x7f || !is_character)
{
buf[0] = v;
len = 1;
}
else if (v <= 0x7ff)
{
buf[0] = 0xc0 + (v >> 6);
buf[1] = 0x80 + (v & 0x3f);
len = 2;
}
else
{
if (v > 0x10ffff)
{
warning_at(location, 0,
"unicode code point 0x%x out of range in string", v);
// Turn it into the "replacement character".
v = 0xfffd;
}
if (v <= 0xffff)
{
buf[0] = 0xe0 + (v >> 12);
buf[1] = 0x80 + ((v >> 6) & 0x3f);
buf[2] = 0x80 + (v & 0x3f);
len = 3;
}
else
{
buf[0] = 0xf0 + (v >> 18);
buf[1] = 0x80 + ((v >> 12) & 0x3f);
buf[2] = 0x80 + ((v >> 6) & 0x3f);
buf[3] = 0x80 + (v & 0x3f);
len = 4;
}
}
str->append(buf, len);
}
// Pick up a character literal.
Token
Lex::gather_character()
{
++this->lineoff_;
const char* pstart = this->linebuf_ + this->lineoff_;
const char* p = pstart;
unsigned int value;
bool is_character;
p = this->advance_one_char(p, true, &value, &is_character);
if (*p != '\'')
{
error_at(this->location(), "unterminated character constant");
this->lineoff_ = p - this->linebuf_;
return this->make_invalid_token();
}
mpz_t val;
mpz_init_set_ui(val, value);
source_location location = this->location();
this->lineoff_ = p + 1 - this->linebuf_;
Token ret = Token::make_integer_token(val, location);
mpz_clear(val);
return ret;
}
// Pick up a quoted string.
Token
Lex::gather_string()
{
const char* pstart = this->linebuf_ + this->lineoff_ + 1;
const char* p = pstart;
const char* pend = this->linebuf_ + this->linesize_;
std::string value;
while (*p != '"')
{
source_location loc = this->location();
unsigned int c;
bool is_character;
this->lineoff_ = p - this->linebuf_;
p = this->advance_one_char(p, false, &c, &is_character);
if (p >= pend)
{
error_at(this->location(), "unterminated string");
--p;
break;
}
Lex::append_char(c, is_character, &value, loc);
}
source_location location = this->location();
this->lineoff_ = p + 1 - this->linebuf_;
return Token::make_string_token(value, location);
}
// Pick up a raw string.
Token
Lex::gather_raw_string()
{
const char* p = this->linebuf_ + this->lineoff_ + 1;
const char* pend = this->linebuf_ + this->linesize_;
source_location location = this->location();
std::string value;
while (true)
{
while (p < pend)
{
if (*p == '`')
{
this->lineoff_ = p + 1 - this->linebuf_;
return Token::make_string_token(value, location);
}
source_location loc = this->location();
unsigned int c;
bool issued_error;
this->lineoff_ = p - this->linebuf_;
p = this->advance_one_utf8_char(p, &c, &issued_error);
Lex::append_char(c, true, &value, loc);
}
this->lineoff_ = p - this->linebuf_;
if (!this->require_line())
{
error_at(location, "unterminated raw string");
return Token::make_string_token(value, location);
}
p = this->linebuf_ + this->lineoff_;
pend = this->linebuf_ + this->linesize_;
}
}
// If C1 C2 C3 are a three character operator, return the code.
Operator
Lex::three_character_operator(char c1, char c2, char c3)
{
if (c3 == '=')
{
if (c1 == '<' && c2 == '<')
return OPERATOR_LSHIFTEQ;
else if (c1 == '>' && c2 == '>')
return OPERATOR_RSHIFTEQ;
else if (c1 == '&' && c2 == '^')
return OPERATOR_BITCLEAREQ;
}
return OPERATOR_INVALID;
}
// If C1 C2 are a two character operator, return the code.
Operator
Lex::two_character_operator(char c1, char c2)
{
switch (c1)
{
case '|':
if (c2 == '|')
return OPERATOR_OROR;
else if (c2 == '=')
return OPERATOR_OREQ;
break;
case '&':
if (c2 == '&')
return OPERATOR_ANDAND;
else if (c2 == '^')
return OPERATOR_BITCLEAR;
else if (c2 == '=')
return OPERATOR_ANDEQ;
break;
case '^':
if (c2 == '=')
return OPERATOR_XOREQ;
break;
case '=':
if (c2 == '=')
return OPERATOR_EQEQ;
break;
case '!':
if (c2 == '=')
return OPERATOR_NOTEQ;
break;
case '<':
if (c2 == '=')
return OPERATOR_LE;
else if (c2 == '<')
return OPERATOR_LSHIFT;
else if (c2 == '-')
return OPERATOR_CHANOP;
break;
case '>':
if (c2 == '=')
return OPERATOR_GE;
else if (c2 == '>')
return OPERATOR_RSHIFT;
break;
case '*':
if (c2 == '=')
return OPERATOR_MULTEQ;
break;
case '/':
if (c2 == '=')
return OPERATOR_DIVEQ;
break;
case '%':
if (c2 == '=')
return OPERATOR_MODEQ;
break;
case '+':
if (c2 == '+')
{
this->add_semi_at_eol_ = true;
return OPERATOR_PLUSPLUS;
}
else if (c2 == '=')
return OPERATOR_PLUSEQ;
break;
case '-':
if (c2 == '-')
{
this->add_semi_at_eol_ = true;
return OPERATOR_MINUSMINUS;
}
else if (c2 == '=')
return OPERATOR_MINUSEQ;
break;
case ':':
if (c2 == '=')
return OPERATOR_COLONEQ;
break;
default:
break;
}
return OPERATOR_INVALID;
}
// If character C is an operator, return the code.
Operator
Lex::one_character_operator(char c)
{
switch (c)
{
case '<':
return OPERATOR_LT;
case '>':
return OPERATOR_GT;
case '+':
return OPERATOR_PLUS;
case '-':
return OPERATOR_MINUS;
case '|':
return OPERATOR_OR;
case '^':
return OPERATOR_XOR;
case '*':
return OPERATOR_MULT;
case '/':
return OPERATOR_DIV;
case '%':
return OPERATOR_MOD;
case '&':
return OPERATOR_AND;
case '!':
return OPERATOR_NOT;
case '=':
return OPERATOR_EQ;
case ':':
return OPERATOR_COLON;
case ';':
return OPERATOR_SEMICOLON;
case '.':
return OPERATOR_DOT;
case ',':
return OPERATOR_COMMA;
case '(':
return OPERATOR_LPAREN;
case ')':
this->add_semi_at_eol_ = true;
return OPERATOR_RPAREN;
case '{':
return OPERATOR_LCURLY;
case '}':
this->add_semi_at_eol_ = true;
return OPERATOR_RCURLY;
case '[':
return OPERATOR_LSQUARE;
case ']':
this->add_semi_at_eol_ = true;
return OPERATOR_RSQUARE;
default:
return OPERATOR_INVALID;
}
}
// Skip a C-style comment.
bool
Lex::skip_c_comment()
{
while (true)
{
if (!this->require_line())
{
error_at(this->location(), "unterminated comment");
return false;
}
const char* p = this->linebuf_ + this->lineoff_;
const char* pend = this->linebuf_ + this->linesize_;
while (p < pend)
{
if (p[0] == '*' && p + 1 < pend && p[1] == '/')
{
this->lineoff_ = p + 2 - this->linebuf_;
return true;
}
this->lineoff_ = p - this->linebuf_;
unsigned int c;
bool issued_error;
p = this->advance_one_utf8_char(p, &c, &issued_error);
}
this->lineoff_ = p - this->linebuf_;
}
}
// Skip a C++-style comment.
void
Lex::skip_cpp_comment()
{
const char* p = this->linebuf_ + this->lineoff_;
const char* pend = this->linebuf_ + this->linesize_;
// By convention, a C++ comment at the start of the line of the form
// //line FILE:LINENO
// is interpreted as setting the file name and line number of the
// next source line.
if (this->lineoff_ == 2
&& pend - p > 5
&& memcmp(p, "line ", 5) == 0)
{
p += 5;
while (p < pend && *p == ' ')
++p;
const char* pcolon = static_cast<const char*>(memchr(p, ':', pend - p));
if (pcolon != NULL
&& pcolon[1] >= '0'
&& pcolon[1] <= '9')
{
char* plend;
long lineno = strtol(pcolon + 1, &plend, 10);
if (plend > pcolon + 1
&& (plend == pend
|| *plend < '0'
|| *plend > '9')
&& lineno > 0
&& lineno < 0x7fffffff)
{
unsigned int filelen = pcolon - p;
char* file = new char[filelen + 1];
memcpy(file, p, filelen);
file[filelen] = '\0';
linemap_add(line_table, LC_LEAVE, 0, NULL, 0);
linemap_add(line_table, LC_ENTER, 0, file, lineno);
this->lineno_ = lineno - 1;
p = plend;
}
}
}
while (p < pend)
{
this->lineoff_ = p - this->linebuf_;
unsigned int c;
bool issued_error;
p = this->advance_one_utf8_char(p, &c, &issued_error);
}
}
// The Unicode tables use this struct.
struct Unicode_range
{
// The low end of the range.
unsigned int low;
// The high end of the range.
unsigned int high;
// The stride. This entries represents low, low + stride, low + 2 *
// stride, etc., up to high.
unsigned int stride;
};
// A table of Unicode digits--Unicode code points classified as
// "Digit".
static const Unicode_range unicode_digits[] =
{
{ 0x0030, 0x0039, 1},
{ 0x0660, 0x0669, 1},
{ 0x06f0, 0x06f9, 1},
{ 0x07c0, 0x07c9, 1},
{ 0x0966, 0x096f, 1},
{ 0x09e6, 0x09ef, 1},
{ 0x0a66, 0x0a6f, 1},
{ 0x0ae6, 0x0aef, 1},
{ 0x0b66, 0x0b6f, 1},
{ 0x0be6, 0x0bef, 1},
{ 0x0c66, 0x0c6f, 1},
{ 0x0ce6, 0x0cef, 1},
{ 0x0d66, 0x0d6f, 1},
{ 0x0e50, 0x0e59, 1},
{ 0x0ed0, 0x0ed9, 1},
{ 0x0f20, 0x0f29, 1},
{ 0x1040, 0x1049, 1},
{ 0x17e0, 0x17e9, 1},
{ 0x1810, 0x1819, 1},
{ 0x1946, 0x194f, 1},
{ 0x19d0, 0x19d9, 1},
{ 0x1b50, 0x1b59, 1},
{ 0xff10, 0xff19, 1},
{ 0x104a0, 0x104a9, 1},
{ 0x1d7ce, 0x1d7ff, 1},
};
// A table of Unicode letters--Unicode code points classified as
// "Letter".
static const Unicode_range unicode_letters[] =
{
{ 0x0041, 0x005a, 1},
{ 0x0061, 0x007a, 1},
{ 0x00aa, 0x00b5, 11},
{ 0x00ba, 0x00ba, 1},
{ 0x00c0, 0x00d6, 1},
{ 0x00d8, 0x00f6, 1},
{ 0x00f8, 0x02c1, 1},
{ 0x02c6, 0x02d1, 1},
{ 0x02e0, 0x02e4, 1},
{ 0x02ec, 0x02ee, 2},
{ 0x0370, 0x0374, 1},
{ 0x0376, 0x0377, 1},
{ 0x037a, 0x037d, 1},
{ 0x0386, 0x0386, 1},
{ 0x0388, 0x038a, 1},
{ 0x038c, 0x038c, 1},
{ 0x038e, 0x03a1, 1},
{ 0x03a3, 0x03f5, 1},
{ 0x03f7, 0x0481, 1},
{ 0x048a, 0x0523, 1},
{ 0x0531, 0x0556, 1},
{ 0x0559, 0x0559, 1},
{ 0x0561, 0x0587, 1},
{ 0x05d0, 0x05ea, 1},
{ 0x05f0, 0x05f2, 1},
{ 0x0621, 0x064a, 1},
{ 0x066e, 0x066f, 1},
{ 0x0671, 0x06d3, 1},
{ 0x06d5, 0x06d5, 1},
{ 0x06e5, 0x06e6, 1},
{ 0x06ee, 0x06ef, 1},
{ 0x06fa, 0x06fc, 1},
{ 0x06ff, 0x0710, 17},
{ 0x0712, 0x072f, 1},
{ 0x074d, 0x07a5, 1},
{ 0x07b1, 0x07b1, 1},
{ 0x07ca, 0x07ea, 1},
{ 0x07f4, 0x07f5, 1},
{ 0x07fa, 0x07fa, 1},
{ 0x0904, 0x0939, 1},
{ 0x093d, 0x0950, 19},
{ 0x0958, 0x0961, 1},
{ 0x0971, 0x0972, 1},
{ 0x097b, 0x097f, 1},
{ 0x0985, 0x098c, 1},
{ 0x098f, 0x0990, 1},
{ 0x0993, 0x09a8, 1},
{ 0x09aa, 0x09b0, 1},
{ 0x09b2, 0x09b2, 1},
{ 0x09b6, 0x09b9, 1},
{ 0x09bd, 0x09ce, 17},
{ 0x09dc, 0x09dd, 1},
{ 0x09df, 0x09e1, 1},
{ 0x09f0, 0x09f1, 1},
{ 0x0a05, 0x0a0a, 1},
{ 0x0a0f, 0x0a10, 1},
{ 0x0a13, 0x0a28, 1},
{ 0x0a2a, 0x0a30, 1},
{ 0x0a32, 0x0a33, 1},
{ 0x0a35, 0x0a36, 1},
{ 0x0a38, 0x0a39, 1},
{ 0x0a59, 0x0a5c, 1},
{ 0x0a5e, 0x0a5e, 1},
{ 0x0a72, 0x0a74, 1},
{ 0x0a85, 0x0a8d, 1},
{ 0x0a8f, 0x0a91, 1},
{ 0x0a93, 0x0aa8, 1},
{ 0x0aaa, 0x0ab0, 1},
{ 0x0ab2, 0x0ab3, 1},
{ 0x0ab5, 0x0ab9, 1},
{ 0x0abd, 0x0ad0, 19},
{ 0x0ae0, 0x0ae1, 1},
{ 0x0b05, 0x0b0c, 1},
{ 0x0b0f, 0x0b10, 1},
{ 0x0b13, 0x0b28, 1},
{ 0x0b2a, 0x0b30, 1},
{ 0x0b32, 0x0b33, 1},
{ 0x0b35, 0x0b39, 1},
{ 0x0b3d, 0x0b3d, 1},
{ 0x0b5c, 0x0b5d, 1},
{ 0x0b5f, 0x0b61, 1},
{ 0x0b71, 0x0b83, 18},
{ 0x0b85, 0x0b8a, 1},
{ 0x0b8e, 0x0b90, 1},
{ 0x0b92, 0x0b95, 1},
{ 0x0b99, 0x0b9a, 1},
{ 0x0b9c, 0x0b9c, 1},
{ 0x0b9e, 0x0b9f, 1},
{ 0x0ba3, 0x0ba4, 1},
{ 0x0ba8, 0x0baa, 1},
{ 0x0bae, 0x0bb9, 1},
{ 0x0bd0, 0x0bd0, 1},
{ 0x0c05, 0x0c0c, 1},
{ 0x0c0e, 0x0c10, 1},
{ 0x0c12, 0x0c28, 1},
{ 0x0c2a, 0x0c33, 1},
{ 0x0c35, 0x0c39, 1},
{ 0x0c3d, 0x0c3d, 1},
{ 0x0c58, 0x0c59, 1},
{ 0x0c60, 0x0c61, 1},
{ 0x0c85, 0x0c8c, 1},
{ 0x0c8e, 0x0c90, 1},
{ 0x0c92, 0x0ca8, 1},
{ 0x0caa, 0x0cb3, 1},
{ 0x0cb5, 0x0cb9, 1},
{ 0x0cbd, 0x0cde, 33},
{ 0x0ce0, 0x0ce1, 1},
{ 0x0d05, 0x0d0c, 1},
{ 0x0d0e, 0x0d10, 1},
{ 0x0d12, 0x0d28, 1},
{ 0x0d2a, 0x0d39, 1},
{ 0x0d3d, 0x0d3d, 1},
{ 0x0d60, 0x0d61, 1},
{ 0x0d7a, 0x0d7f, 1},
{ 0x0d85, 0x0d96, 1},
{ 0x0d9a, 0x0db1, 1},
{ 0x0db3, 0x0dbb, 1},
{ 0x0dbd, 0x0dbd, 1},
{ 0x0dc0, 0x0dc6, 1},
{ 0x0e01, 0x0e30, 1},
{ 0x0e32, 0x0e33, 1},
{ 0x0e40, 0x0e46, 1},
{ 0x0e81, 0x0e82, 1},
{ 0x0e84, 0x0e84, 1},
{ 0x0e87, 0x0e88, 1},
{ 0x0e8a, 0x0e8d, 3},
{ 0x0e94, 0x0e97, 1},
{ 0x0e99, 0x0e9f, 1},
{ 0x0ea1, 0x0ea3, 1},
{ 0x0ea5, 0x0ea7, 2},
{ 0x0eaa, 0x0eab, 1},
{ 0x0ead, 0x0eb0, 1},
{ 0x0eb2, 0x0eb3, 1},
{ 0x0ebd, 0x0ebd, 1},
{ 0x0ec0, 0x0ec4, 1},
{ 0x0ec6, 0x0ec6, 1},
{ 0x0edc, 0x0edd, 1},
{ 0x0f00, 0x0f00, 1},
{ 0x0f40, 0x0f47, 1},
{ 0x0f49, 0x0f6c, 1},
{ 0x0f88, 0x0f8b, 1},
{ 0x1000, 0x102a, 1},
{ 0x103f, 0x103f, 1},
{ 0x1050, 0x1055, 1},
{ 0x105a, 0x105d, 1},
{ 0x1061, 0x1061, 1},
{ 0x1065, 0x1066, 1},
{ 0x106e, 0x1070, 1},
{ 0x1075, 0x1081, 1},
{ 0x108e, 0x108e, 1},
{ 0x10a0, 0x10c5, 1},
{ 0x10d0, 0x10fa, 1},
{ 0x10fc, 0x10fc, 1},
{ 0x1100, 0x1159, 1},
{ 0x115f, 0x11a2, 1},
{ 0x11a8, 0x11f9, 1},
{ 0x1200, 0x1248, 1},
{ 0x124a, 0x124d, 1},
{ 0x1250, 0x1256, 1},
{ 0x1258, 0x1258, 1},
{ 0x125a, 0x125d, 1},
{ 0x1260, 0x1288, 1},
{ 0x128a, 0x128d, 1},
{ 0x1290, 0x12b0, 1},
{ 0x12b2, 0x12b5, 1},
{ 0x12b8, 0x12be, 1},
{ 0x12c0, 0x12c0, 1},
{ 0x12c2, 0x12c5, 1},
{ 0x12c8, 0x12d6, 1},
{ 0x12d8, 0x1310, 1},
{ 0x1312, 0x1315, 1},
{ 0x1318, 0x135a, 1},
{ 0x1380, 0x138f, 1},
{ 0x13a0, 0x13f4, 1},
{ 0x1401, 0x166c, 1},
{ 0x166f, 0x1676, 1},
{ 0x1681, 0x169a, 1},
{ 0x16a0, 0x16ea, 1},
{ 0x1700, 0x170c, 1},
{ 0x170e, 0x1711, 1},
{ 0x1720, 0x1731, 1},
{ 0x1740, 0x1751, 1},
{ 0x1760, 0x176c, 1},
{ 0x176e, 0x1770, 1},
{ 0x1780, 0x17b3, 1},
{ 0x17d7, 0x17dc, 5},
{ 0x1820, 0x1877, 1},
{ 0x1880, 0x18a8, 1},
{ 0x18aa, 0x18aa, 1},
{ 0x1900, 0x191c, 1},
{ 0x1950, 0x196d, 1},
{ 0x1970, 0x1974, 1},
{ 0x1980, 0x19a9, 1},
{ 0x19c1, 0x19c7, 1},
{ 0x1a00, 0x1a16, 1},
{ 0x1b05, 0x1b33, 1},
{ 0x1b45, 0x1b4b, 1},
{ 0x1b83, 0x1ba0, 1},
{ 0x1bae, 0x1baf, 1},
{ 0x1c00, 0x1c23, 1},
{ 0x1c4d, 0x1c4f, 1},
{ 0x1c5a, 0x1c7d, 1},
{ 0x1d00, 0x1dbf, 1},
{ 0x1e00, 0x1f15, 1},
{ 0x1f18, 0x1f1d, 1},
{ 0x1f20, 0x1f45, 1},
{ 0x1f48, 0x1f4d, 1},
{ 0x1f50, 0x1f57, 1},
{ 0x1f59, 0x1f5d, 2},
{ 0x1f5f, 0x1f7d, 1},
{ 0x1f80, 0x1fb4, 1},
{ 0x1fb6, 0x1fbc, 1},
{ 0x1fbe, 0x1fbe, 1},
{ 0x1fc2, 0x1fc4, 1},
{ 0x1fc6, 0x1fcc, 1},
{ 0x1fd0, 0x1fd3, 1},
{ 0x1fd6, 0x1fdb, 1},
{ 0x1fe0, 0x1fec, 1},
{ 0x1ff2, 0x1ff4, 1},
{ 0x1ff6, 0x1ffc, 1},
{ 0x2071, 0x207f, 14},
{ 0x2090, 0x2094, 1},
{ 0x2102, 0x2107, 5},
{ 0x210a, 0x2113, 1},
{ 0x2115, 0x2115, 1},
{ 0x2119, 0x211d, 1},
{ 0x2124, 0x2128, 2},
{ 0x212a, 0x212d, 1},
{ 0x212f, 0x2139, 1},
{ 0x213c, 0x213f, 1},
{ 0x2145, 0x2149, 1},
{ 0x214e, 0x214e, 1},
{ 0x2183, 0x2184, 1},
{ 0x2c00, 0x2c2e, 1},
{ 0x2c30, 0x2c5e, 1},
{ 0x2c60, 0x2c6f, 1},
{ 0x2c71, 0x2c7d, 1},
{ 0x2c80, 0x2ce4, 1},
{ 0x2d00, 0x2d25, 1},
{ 0x2d30, 0x2d65, 1},
{ 0x2d6f, 0x2d6f, 1},
{ 0x2d80, 0x2d96, 1},
{ 0x2da0, 0x2da6, 1},
{ 0x2da8, 0x2dae, 1},
{ 0x2db0, 0x2db6, 1},
{ 0x2db8, 0x2dbe, 1},
{ 0x2dc0, 0x2dc6, 1},
{ 0x2dc8, 0x2dce, 1},
{ 0x2dd0, 0x2dd6, 1},
{ 0x2dd8, 0x2dde, 1},
{ 0x2e2f, 0x2e2f, 1},
{ 0x3005, 0x3006, 1},
{ 0x3031, 0x3035, 1},
{ 0x303b, 0x303c, 1},
{ 0x3041, 0x3096, 1},
{ 0x309d, 0x309f, 1},
{ 0x30a1, 0x30fa, 1},
{ 0x30fc, 0x30ff, 1},
{ 0x3105, 0x312d, 1},
{ 0x3131, 0x318e, 1},
{ 0x31a0, 0x31b7, 1},
{ 0x31f0, 0x31ff, 1},
{ 0x3400, 0x4db5, 1},
{ 0x4e00, 0x9fc3, 1},
{ 0xa000, 0xa48c, 1},
{ 0xa500, 0xa60c, 1},
{ 0xa610, 0xa61f, 1},
{ 0xa62a, 0xa62b, 1},
{ 0xa640, 0xa65f, 1},
{ 0xa662, 0xa66e, 1},
{ 0xa67f, 0xa697, 1},
{ 0xa717, 0xa71f, 1},
{ 0xa722, 0xa788, 1},
{ 0xa78b, 0xa78c, 1},
{ 0xa7fb, 0xa801, 1},
{ 0xa803, 0xa805, 1},
{ 0xa807, 0xa80a, 1},
{ 0xa80c, 0xa822, 1},
{ 0xa840, 0xa873, 1},
{ 0xa882, 0xa8b3, 1},
{ 0xa90a, 0xa925, 1},
{ 0xa930, 0xa946, 1},
{ 0xaa00, 0xaa28, 1},
{ 0xaa40, 0xaa42, 1},
{ 0xaa44, 0xaa4b, 1},
{ 0xac00, 0xd7a3, 1},
{ 0xf900, 0xfa2d, 1},
{ 0xfa30, 0xfa6a, 1},
{ 0xfa70, 0xfad9, 1},
{ 0xfb00, 0xfb06, 1},
{ 0xfb13, 0xfb17, 1},
{ 0xfb1d, 0xfb1d, 1},
{ 0xfb1f, 0xfb28, 1},
{ 0xfb2a, 0xfb36, 1},
{ 0xfb38, 0xfb3c, 1},
{ 0xfb3e, 0xfb3e, 1},
{ 0xfb40, 0xfb41, 1},
{ 0xfb43, 0xfb44, 1},
{ 0xfb46, 0xfbb1, 1},
{ 0xfbd3, 0xfd3d, 1},
{ 0xfd50, 0xfd8f, 1},
{ 0xfd92, 0xfdc7, 1},
{ 0xfdf0, 0xfdfb, 1},
{ 0xfe70, 0xfe74, 1},
{ 0xfe76, 0xfefc, 1},
{ 0xff21, 0xff3a, 1},
{ 0xff41, 0xff5a, 1},
{ 0xff66, 0xffbe, 1},
{ 0xffc2, 0xffc7, 1},
{ 0xffca, 0xffcf, 1},
{ 0xffd2, 0xffd7, 1},
{ 0xffda, 0xffdc, 1},
{ 0x10000, 0x1000b, 1},
{ 0x1000d, 0x10026, 1},
{ 0x10028, 0x1003a, 1},
{ 0x1003c, 0x1003d, 1},
{ 0x1003f, 0x1004d, 1},
{ 0x10050, 0x1005d, 1},
{ 0x10080, 0x100fa, 1},
{ 0x10280, 0x1029c, 1},
{ 0x102a0, 0x102d0, 1},
{ 0x10300, 0x1031e, 1},
{ 0x10330, 0x10340, 1},
{ 0x10342, 0x10349, 1},
{ 0x10380, 0x1039d, 1},
{ 0x103a0, 0x103c3, 1},
{ 0x103c8, 0x103cf, 1},
{ 0x10400, 0x1049d, 1},
{ 0x10800, 0x10805, 1},
{ 0x10808, 0x10808, 1},
{ 0x1080a, 0x10835, 1},
{ 0x10837, 0x10838, 1},
{ 0x1083c, 0x1083f, 3},
{ 0x10900, 0x10915, 1},
{ 0x10920, 0x10939, 1},
{ 0x10a00, 0x10a00, 1},
{ 0x10a10, 0x10a13, 1},
{ 0x10a15, 0x10a17, 1},
{ 0x10a19, 0x10a33, 1},
{ 0x12000, 0x1236e, 1},
{ 0x1d400, 0x1d454, 1},
{ 0x1d456, 0x1d49c, 1},
{ 0x1d49e, 0x1d49f, 1},
{ 0x1d4a2, 0x1d4a2, 1},
{ 0x1d4a5, 0x1d4a6, 1},
{ 0x1d4a9, 0x1d4ac, 1},
{ 0x1d4ae, 0x1d4b9, 1},
{ 0x1d4bb, 0x1d4bb, 1},
{ 0x1d4bd, 0x1d4c3, 1},
{ 0x1d4c5, 0x1d505, 1},
{ 0x1d507, 0x1d50a, 1},
{ 0x1d50d, 0x1d514, 1},
{ 0x1d516, 0x1d51c, 1},
{ 0x1d51e, 0x1d539, 1},
{ 0x1d53b, 0x1d53e, 1},
{ 0x1d540, 0x1d544, 1},
{ 0x1d546, 0x1d546, 1},
{ 0x1d54a, 0x1d550, 1},
{ 0x1d552, 0x1d6a5, 1},
{ 0x1d6a8, 0x1d6c0, 1},
{ 0x1d6c2, 0x1d6da, 1},
{ 0x1d6dc, 0x1d6fa, 1},
{ 0x1d6fc, 0x1d714, 1},
{ 0x1d716, 0x1d734, 1},
{ 0x1d736, 0x1d74e, 1},
{ 0x1d750, 0x1d76e, 1},
{ 0x1d770, 0x1d788, 1},
{ 0x1d78a, 0x1d7a8, 1},
{ 0x1d7aa, 0x1d7c2, 1},
{ 0x1d7c4, 0x1d7cb, 1},
{ 0x20000, 0x2a6d6, 1},
{ 0x2f800, 0x2fa1d, 1},
};
// A table of Unicode uppercase letters--Unicode code points
// classified as "Letter, uppercase".
static const Unicode_range unicode_uppercase_letters[] =
{
{ 0x0041, 0x005a, 1},
{ 0x00c0, 0x00d6, 1},
{ 0x00d8, 0x00de, 1},
{ 0x0100, 0x0136, 2},
{ 0x0139, 0x0147, 2},
{ 0x014a, 0x0176, 2},
{ 0x0178, 0x0179, 1},
{ 0x017b, 0x017d, 2},
{ 0x0181, 0x0182, 1},
{ 0x0184, 0x0184, 1},
{ 0x0186, 0x0187, 1},
{ 0x0189, 0x018b, 1},
{ 0x018e, 0x0191, 1},
{ 0x0193, 0x0194, 1},
{ 0x0196, 0x0198, 1},
{ 0x019c, 0x019d, 1},
{ 0x019f, 0x01a0, 1},
{ 0x01a2, 0x01a4, 2},
{ 0x01a6, 0x01a7, 1},
{ 0x01a9, 0x01ac, 3},
{ 0x01ae, 0x01af, 1},
{ 0x01b1, 0x01b3, 1},
{ 0x01b5, 0x01b5, 1},
{ 0x01b7, 0x01b8, 1},
{ 0x01bc, 0x01c4, 8},
{ 0x01c7, 0x01cd, 3},
{ 0x01cf, 0x01db, 2},
{ 0x01de, 0x01ee, 2},
{ 0x01f1, 0x01f4, 3},
{ 0x01f6, 0x01f8, 1},
{ 0x01fa, 0x0232, 2},
{ 0x023a, 0x023b, 1},
{ 0x023d, 0x023e, 1},
{ 0x0241, 0x0241, 1},
{ 0x0243, 0x0246, 1},
{ 0x0248, 0x024e, 2},
{ 0x0370, 0x0372, 2},
{ 0x0376, 0x0386, 16},
{ 0x0388, 0x038a, 1},
{ 0x038c, 0x038c, 1},
{ 0x038e, 0x038f, 1},
{ 0x0391, 0x03a1, 1},
{ 0x03a3, 0x03ab, 1},
{ 0x03cf, 0x03cf, 1},
{ 0x03d2, 0x03d4, 1},
{ 0x03d8, 0x03ee, 2},
{ 0x03f4, 0x03f7, 3},
{ 0x03f9, 0x03fa, 1},
{ 0x03fd, 0x042f, 1},
{ 0x0460, 0x0480, 2},
{ 0x048a, 0x04be, 2},
{ 0x04c0, 0x04c1, 1},
{ 0x04c3, 0x04cd, 2},
{ 0x04d0, 0x0522, 2},
{ 0x0531, 0x0556, 1},
{ 0x10a0, 0x10c5, 1},
{ 0x1e00, 0x1e94, 2},
{ 0x1e9e, 0x1efe, 2},
{ 0x1f08, 0x1f0f, 1},
{ 0x1f18, 0x1f1d, 1},
{ 0x1f28, 0x1f2f, 1},
{ 0x1f38, 0x1f3f, 1},
{ 0x1f48, 0x1f4d, 1},
{ 0x1f59, 0x1f5f, 2},
{ 0x1f68, 0x1f6f, 1},
{ 0x1fb8, 0x1fbb, 1},
{ 0x1fc8, 0x1fcb, 1},
{ 0x1fd8, 0x1fdb, 1},
{ 0x1fe8, 0x1fec, 1},
{ 0x1ff8, 0x1ffb, 1},
{ 0x2102, 0x2107, 5},
{ 0x210b, 0x210d, 1},
{ 0x2110, 0x2112, 1},
{ 0x2115, 0x2115, 1},
{ 0x2119, 0x211d, 1},
{ 0x2124, 0x2128, 2},
{ 0x212a, 0x212d, 1},
{ 0x2130, 0x2133, 1},
{ 0x213e, 0x213f, 1},
{ 0x2145, 0x2183, 62},
{ 0x2c00, 0x2c2e, 1},
{ 0x2c60, 0x2c60, 1},
{ 0x2c62, 0x2c64, 1},
{ 0x2c67, 0x2c6b, 2},
{ 0x2c6d, 0x2c6f, 1},
{ 0x2c72, 0x2c75, 3},
{ 0x2c80, 0x2ce2, 2},
{ 0xa640, 0xa65e, 2},
{ 0xa662, 0xa66c, 2},
{ 0xa680, 0xa696, 2},
{ 0xa722, 0xa72e, 2},
{ 0xa732, 0xa76e, 2},
{ 0xa779, 0xa77b, 2},
{ 0xa77d, 0xa77e, 1},
{ 0xa780, 0xa786, 2},
{ 0xa78b, 0xa78b, 1},
{ 0xff21, 0xff3a, 1},
{ 0x10400, 0x10427, 1},
{ 0x1d400, 0x1d419, 1},
{ 0x1d434, 0x1d44d, 1},
{ 0x1d468, 0x1d481, 1},
{ 0x1d49c, 0x1d49c, 1},
{ 0x1d49e, 0x1d49f, 1},
{ 0x1d4a2, 0x1d4a2, 1},
{ 0x1d4a5, 0x1d4a6, 1},
{ 0x1d4a9, 0x1d4ac, 1},
{ 0x1d4ae, 0x1d4b5, 1},
{ 0x1d4d0, 0x1d4e9, 1},
{ 0x1d504, 0x1d505, 1},
{ 0x1d507, 0x1d50a, 1},
{ 0x1d50d, 0x1d514, 1},
{ 0x1d516, 0x1d51c, 1},
{ 0x1d538, 0x1d539, 1},
{ 0x1d53b, 0x1d53e, 1},
{ 0x1d540, 0x1d544, 1},
{ 0x1d546, 0x1d546, 1},
{ 0x1d54a, 0x1d550, 1},
{ 0x1d56c, 0x1d585, 1},
{ 0x1d5a0, 0x1d5b9, 1},
{ 0x1d5d4, 0x1d5ed, 1},
{ 0x1d608, 0x1d621, 1},
{ 0x1d63c, 0x1d655, 1},
{ 0x1d670, 0x1d689, 1},
{ 0x1d6a8, 0x1d6c0, 1},
{ 0x1d6e2, 0x1d6fa, 1},
{ 0x1d71c, 0x1d734, 1},
{ 0x1d756, 0x1d76e, 1},
{ 0x1d790, 0x1d7a8, 1},
{ 0x1d7ca, 0x1d7ca, 1},
};
// Return true if C is in RANGES.
bool
Lex::is_in_unicode_range(unsigned int c, const Unicode_range* ranges,
size_t range_size)
{
if (c < 0x100)
{
// The common case is a small value, and we know that it will be
// in the first few entries of the table. Do a linear scan
// rather than a binary search.
for (size_t i = 0; i < range_size; ++i)
{
const Unicode_range* p = &ranges[i];
if (c <= p->high)
{
if (c < p->low)
return false;
return (c - p->low) % p->stride == 0;
}
}
return false;
}
else
{
size_t lo = 0;
size_t hi = range_size;
while (lo < hi)
{
size_t mid = lo + (hi - lo) / 2;
const Unicode_range* p = &ranges[mid];
if (c < p->low)
hi = mid;
else if (c > p->high)
lo = mid + 1;
else
return (c - p->low) % p->stride == 0;
}
return false;
}
}
// Return whether C is a Unicode digit--a Unicode code point
// classified as "Digit".
bool
Lex::is_unicode_digit(unsigned int c)
{
return Lex::is_in_unicode_range(c, unicode_digits,
ARRAY_SIZE(unicode_digits));
}
// Return whether C is a Unicode letter--a Unicode code point
// classified as "Letter".
bool
Lex::is_unicode_letter(unsigned int c)
{
return Lex::is_in_unicode_range(c, unicode_letters,
ARRAY_SIZE(unicode_letters));
}
// Return whether C is a Unicode uppercase letter. a Unicode code
// point classified as "Letter, uppercase".
bool
Lex::is_unicode_uppercase(unsigned int c)
{
return Lex::is_in_unicode_range(c, unicode_uppercase_letters,
ARRAY_SIZE(unicode_uppercase_letters));
}
// Return whether the identifier NAME should be exported. NAME is a
// mangled name which includes only ASCII characters.
bool
Lex::is_exported_name(const std::string& name)
{
unsigned char c = name[0];
if (c != '$')
return c >= 'A' && c <= 'Z';
else
{
const char* p = name.data();
size_t len = name.length();
if (len < 2 || p[1] != 'U')
return false;
unsigned int ci = 0;
for (size_t i = 2; i < len && p[i] != '$'; ++i)
{
c = p[i];
if (!hex_p(c))
return false;
ci <<= 4;
ci |= hex_value(c);
}
return Lex::is_unicode_uppercase(ci);
}
}
|