-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlexer.d
1307 lines (1262 loc) · 44.6 KB
/
lexer.d
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
// Written in the D programming language
// Author: Timon Gehr
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
import std.string, utf = std.utf, std.uni;
import std.stdio, std.conv;
import std.algorithm : startsWith, swap;
import std.traits : EnumMembers;
import core.memory;
import util : lowerf, escape, mallocAppender, toEngNum;
mixin("enum TokenType{"~TokenNames()~"}");
template Tok(string type){mixin(TokImpl());}
template TokChars(TokenType type){mixin(TokCharsImpl());}
private immutable {
string[2][] complexTokens =
[["i", "Identifier" ],
["``", "StringLiteral" ],
["``c", "StringLiteralC" ],
["``w", "StringLiteralW" ],
["``d", "StringLiteralD" ],
["' '", "CharacterLiteral" ],
["0", "IntLiteral" ],
["0U", "UintLiteral" ],
["0L", "LongLiteral" ],
["0LU", "UlongLiteral" ],
[".0f", "FloatLiteral" ],
[".0", "DoubleLiteral" ],
[".0L", "RealLiteral" ],
[".0fi", "ImaginaryFloatLiteral" ],
[".0i", "ImaginaryDoubleLiteral" ],
[".0Li", "ImaginaryLiteral" ]];
string[2][] simpleTokens =
[["/", "Divide" ],
["/=", "DivideAssign" ],
[".", "Dot" ],
["..", "DotDot" ],
["...", "DotDotDot" ],
["&", "And" ],
["&=", "AndAssign" ],
["&&", "AndAnd" ],
["|", "Or" ],
["|=", "OrAssign" ],
["||", "OrOr" ],
["-", "Minus" ],
["-=", "MinusAssign" ],
["--", "MinusMinus" ],
["+", "Plus" ],
["+=", "PlusAssign" ],
["++", "PlusPlus" ],
["<", "Less" ],
["<=", "LessEqual" ],
["<<", "LeftShift" ],
["<<=", "LeftShiftAssign" ],
["<>", "LessGreater" ],
["<>=", "LessGreaterEqual" ],
[">", "Greater" ],
[">=", "GreaterEqual" ],
[">>=", "RightShiftAssign" ],
[">>>=", "LogicalRightShiftAssign" ],
[">>", "RightShift" ],
[">>>", "LogicalRightShift" ],
["!", "ExclamationMark" ],
["!=", "NotEqual" ],
["!<>", "NotLessGreater" ],
["!<>=", "Unordered" ],
["!<", "NotLess" ],
["!<=", "NotLessEqual" ],
["!>", "NotGreater" ],
["!>=", "NotGreaterEqual" ],
["(", "LeftParen" ],
[")", "RightParen" ],
["[", "LeftBracket" ],
["]", "RightBracket" ],
["{", "LeftCurly" ],
["}", "RightCurly" ],
["?", "QuestionMark" ],
[",", "Comma" ],
[";", "Semicolon" ],
[":", "Colon" ],
["$", "Dollar" ],
["=", "Assign" ],
["=>", "GoesTo" ],
["==", "Equal" ],
["*", "Star" ],
["*=", "MultiplyAssign" ],
["%", "Modulo" ],
["%=", "ModuloAssign" ],
["^", "Xor" ],
["^=", "XorAssign" ],
["^^", "Pow" ],
["^^=", "PowAssign" ],
["~", "Concat" ],
["~=", "ConcatAssign" ],
["@", "At" ]];
string[2][] specialTokens =
[["", "None", ],
[" ", "Whitespace", ],
["//", "Comment", ],
["///", "DokComment", ],
["\n", "NewLine", ], // TODO: incorporate unicode new line support
["Error", "Error" ],
["__error","ErrorLiteral" ],
["EOF", "Eof" ]];
string[2][] compoundTokens =
[["auto ref", "AutoRef" ],
["!is" , "NotIs" ],
["!in" , "NotIn" ]];
string[] keywords = ["abstract", "alias", "align", "asm", "assert", "auto", "body", "bool", "break", "byte", "case", "cast", "catch", "cdouble", "cent", "cfloat", "char", "class", "const", "continue", "creal", "dchar", "debug", "default", "delegate", "delete", "deprecated", "do", "double", "else", "enum", "export", "extern", "false", "final", "finally", "float", "for", "foreach", "foreach_reverse", "function", "goto", "idouble", "if", "ifloat", "immutable", "import", "in", "inout", "int", "interface", "invariant", "ireal", "is", "lazy", "long", "macro", "mixin", "module", "new", "nothrow", "null", "out", "override", "package", "pragma", "private", "protected", "public", "pure", "real", "ref", "return", "scope", "shared", "short", "static", "struct", "super", "switch", "synchronized", "template", "this", "throw", "true", "try", "typedef", "typeid", "typeof", "ubyte", "ucent", "uint", "ulong", "union", "unittest", "ushort", "version", "void", "volatile", "wchar", "while", "with", /*"__FILE__", "__LINE__",*/ "__gshared", "__thread", "__traits"];
string[2][] tokens = specialTokens ~ complexTokens ~ simpleTokens ~ compoundTokens ~ keywordTokens();
}
private{
auto keywordTokens(){
immutable(string[2])[] r;
foreach(i,kw;keywords) r~=[kw,kw~"_"];
return r;
}
string TokenNames(){
string r;
foreach(t;tokens) r~=lowerf(t[1])~",";
return r;
}
string TokImpl(){
string r="static if(type==\""~tokens[0][0]~"\") alias TokenType."~lowerf(tokens[0][1])~" Tok;";
foreach(t;tokens) r~="else static if(type==\""~t[0]~"\") alias TokenType."~lowerf(t[1])~" Tok;";
r~="else static assert(0,\"unknown Token '\"~type~\"'\");";
return r;
}
string TokCharsImpl(){
string r="static if(type==TokenType."~lowerf(tokens[0][1])~") enum TokChars=\""~tokens[0][0]~"\";";
foreach(t;tokens) r~="else static if(type==TokenType."~lowerf(t[1])~") enum TokChars=\""~t[0]~"\";";
r~="else static assert(0,\"invalid TokenType \"~to!string(type));";
return r;
}
}
string TokenTypeToString(TokenType type){
return tokens[cast(int)type][0];
}
string toString(immutable(Token)[] a){string r;foreach(t;a) r~='['~t.toString()~']'; return r;}
class Source{
immutable{
string name;
string code;
}
this(string name, string code)in{auto c=code;assert(c.length>=4&&!c[$-4]&&!c[$-3]&&!c[$-2]&&!c[$-1]);}body{ // four padding zero bytes required because of UTF{
this.name = name;
this.code = code[0..$-4]; // don't expose the padding zeros
sources ~= this;
}
string getLineOf(string rep)in{assert(this is get(rep));}body{
//if(!code.length) return code;
string before=code.ptr[0..code.length+4][0..rep.ptr-code.ptr];
string after =code.ptr[0..code.length+4][rep.ptr-code.ptr..$];
immutable(char)* start=code.ptr, end=code.ptr+code.length;
foreach_reverse(ref c; before) if(c=='\n'||c=='\r'){start = &c+1; break;}
foreach(ref c; after) if(c=='\n'||c=='\r'){end = &c; break;}
return start[0..end-start];
}
static Source get(string rep){
foreach(x; sources) if(rep.ptr>=x.code.ptr && rep.ptr+rep.length<=x.code.ptr+x.code.length+4) return x;
return null;
}
void dispose(){
foreach(i,x; sources) if(x is this){
swap(sources[i], sources[$-1]);
sources=sources[0..$-1];
sources.assumeSafeAppend();
return;
}
assert(0);
}
private static Source[] sources;
}
struct Location{
string rep; // slice of the code representing the Location
int line; // line number at start of location
// reference to the source the code belongs to
@property Source source()const{
auto src = Source.get(rep);
assert(src, "source for '"~rep~"' not found!");
return src;
}
Location to(const(Location) end)const{// in{assert(end.source is source);}body{
// in{assert(rep.ptr<=end.rep.ptr);}body{ // requiring that is not robust enough
if(rep.ptr>end.rep.ptr+end.rep.length) return cast()this;
return Location(rep.ptr[0..end.rep.ptr-rep.ptr+end.rep.length],line);
}
}
import std.array;
int getColumn(Location loc, int tabsize){
int res=0;
auto l=loc.source.getLineOf(loc.rep);
for(;!l.empty&&l[0]&&l.ptr<loc.rep.ptr; l.popFront()){
if(l.front=='\t') res=res-res%tabsize+tabsize;
else res++;
}
return res+1;
}
struct Token{
TokenType type;
string toString() const{
if(type == Tok!"EOF") return "EOF";
if(loc.rep.length) return loc.rep;
// TODO: remove boilerplate
// TODO: get better representations
switch(type){
case Tok!"i":
return name;
case Tok!"``":
return '"'~escape(str)~'"';
case Tok!"``c":
return '"'~escape(str)~`"c`;
case Tok!"``w":
return '"'~escape(str)~`"w`;
case Tok!"``d":
return '"'~escape(str)~`"d`;
case Tok!"' '":
return '\''~escape(to!string(cast(dchar)int64),false)~'\'';
case Tok!"0":
return to!string(cast(int)int64);
case Tok!"0U":
return to!string(cast(uint)int64)~'U';
case Tok!"0L":
return to!string(cast(long)int64)~'L';
case Tok!"0LU":
return to!string(int64)~"LU";
case Tok!".0f":
return to!string(flt80)~'f';
case Tok!".0":
return to!string(flt80);
case Tok!".0L":
return to!string(flt80)~'L';
case Tok!".0i":
return to!string(flt80)~'i';
case Tok!".0fi":
return to!string(flt80)~"fi";
case Tok!".0Li":
return to!string(flt80)~"Li";
case Tok!"Error":
return "error: "~str;
default:
return tokens[cast(int)type][0];case Tok!"true": return "true";
}
}
union{
string str, name; // string literals, identifiers
ulong int64; // integer literals
real flt80; // float, double, real literals
}
Location loc;
}
template token(string t){enum token=Token(Tok!t);} // unions not yet supported
unittest{
static assert({
foreach(i;simpleTokens){
string s=i[0];
bool found = s.length==1;
foreach(j;simpleTokens) if(j[0] == s[0..$-1]) found = true;
if(!found) return false;
}return true;
}(),"Every non-empty prefix of simpleTokens must be a valid token.");
}
string caseSimpleToken(string prefix="", bool needs = false)pure{
string r;
int c=0,d=0;
foreach(i;simpleTokens){string s=i[0]; if(s.startsWith(prefix)) c++;}
if(c==1) r~=`res[0].type = Tok!"`~prefix~'"'~";\nbreak;\n";
else{
if(needs) r~="switch(*p){\n";
foreach(i;simpleTokens){
string s = i[0]; if(s[0]=='/' || s[0] == '.') continue; // / can be the start of a comment, . could be the start of a float literal
if(s.startsWith(prefix) && s.length==prefix.length+1){
r~=`case '`~s[$-1]~"':\n"~(needs?"p++;\n":"");
r~=caseSimpleToken(s,true);
}
}
if(prefix=="!") r~="default: mixin(lexNotAndNotIsAndNotIn);\nbreak;\n}\nbreak;\n";
else if(needs) r~=`default: res[0].type = Tok!"`~prefix~`"`~";\nbreak;\n}\nbreak;\n";
}
return r;
}
auto lex(Source source){ // pure
return Lexer(source);
}
struct Anchor{
size_t index;
}
struct Lexer{
string code; // Manually allocated!
Token[] buffer;
Token[] errors;
Source source;
size_t n,m; // start and end index in buffer
size_t s,e; // global start and end index
size_t numAnchors; // number of existing anchors for this lexer
size_t firstAnchor; // first local index that has to remain in buffer
int line;
/+invariant(){ // useless because DMD does not test the invariant at the proper time...
assert(e-s == (m-n+buffer.length)%buffer.length); // relation between (s,e) and (n,m)
assert(!(buffer.length&buffer.length-1)); // buffer size is always a power of two
assert(numAnchors||firstAnchor==size_t.max);
}+/
//pure: phobos ...
this(Source src){
source = src;
code = src.code.ptr[0..src.code.length+4]; // rely on 4 padding zero bytes
enum initsize=4096;//685438;//
buffer = new Token[](initsize);//
//buffer = (cast(Token*)malloc(Token.sizeof*initsize))[0..initsize];//
numAnchors=0;
firstAnchor=size_t.max;
line=1;
n=s=0;
e=lexTo(buffer);
m=e&buffer.length-1;
if(src.code.length > int.max) errors~=tokError("no support for sources exceeding 2GB",null),code.length=int.max;
}
@property ref const(Token) front()const{return buffer[n];}
@property bool empty(){return buffer[n].type==Tok!"EOF";}
void popFront(){
assert(!empty,"attempted to popFront empty lexer.");
n = n+1 & buffer.length-1; s++;
if(s<e) return; // if the buffer still contains elements
assert(s==e && n==m);
if(!numAnchors){// no anchors, that is easy, just reuse the whole buffer
buffer[0]=buffer[n-1&buffer.length-1]; // we want at least one token of lookback, for nice error reporting
e+=m=lexTo(buffer[n=1..$]);
m=m+1&buffer.length-1;
return;
}
assert(firstAnchor<buffer.length);
size_t num;
if(m < firstAnchor){ // there is an anchor but still space
num=lexTo(buffer[n..firstAnchor]);
e+=num; m=m+num&buffer.length-1;
return;
}else if(m > firstAnchor){ // ditto
num=lexTo(buffer[m..$]);
if(firstAnchor) num+=lexTo(buffer[0..firstAnchor]);
e+=num; m=m+num&buffer.length-1;
return;
}
auto len=buffer.length;
buffer.length=len<<1; // double buffer size
//buffer=(cast(Token*)realloc(buffer.ptr,(len<<1)*Token.sizeof))[0..len<<1];
n=len+firstAnchor; // move start and firstAnchor
buffer[len..n]=buffer[0..firstAnchor]; // move tokens to respect the new buffer topology
num=0;
if(n<buffer.length){
num=lexTo(buffer[n..$]);
e+=num; m=n+num&buffer.length-1;
}
if(!m&&firstAnchor){
num=lexTo(buffer[0..firstAnchor]);
e+=num; m=num&buffer.length-1;
}
}
Anchor pushAnchor(){
if(!numAnchors) firstAnchor=n;
numAnchors++;
return Anchor(s);
}
void popAnchor(Anchor anchor){
numAnchors--;
if(!numAnchors) firstAnchor=size_t.max;
n=n+anchor.index-s&buffer.length-1;
s=anchor.index;
}
private:
Token tokError(string s, string rep, int l=0){
auto r = token!"Error";
r.str = s;
r.loc = Location(rep, l?l:line);
return r;
}
size_t lexTo(Token[] res)in{assert(res.length);}body{
alias mallocAppender appender;
if(!code.length) return 0;
auto p=code.ptr;
auto s=p; // used if the input has to be sliced
auto sl=line;// ditto
char del; // used if a delimiter of some sort needs to be remembered
size_t len; // used as a temporary that stores the length of the last UTF sequence
size_t num=0;// number of tokens lexed, return value
typeof(p) invCharSeq_l=null;
void invCharSeq(){
if(p>invCharSeq_l+1) errors~=tokError("unsupported character",p[0..1]);
else errors[$-1].loc.rep=errors[$-1].loc.rep.ptr[0..errors[$-1].loc.rep.length+1];
invCharSeq_l=p; p++;
}
// text macros:
enum skipUnicode = q{if(*p<0x80){p++;break;} len=0; try utf.decode(p[0..4],len), p+=len; catch(Exception){invCharSeq();}};
enum skipUnicodeCont = q{if(*p<0x80){p++;continue;} len=0; try utf.decode(p[0..4],len), p+=len; catch(Exception){invCharSeq();}}; // don't break, continue
enum caseNl = q{case '\r': if(p[1]=='\n') p++; goto case; case '\n': line++; p++; continue;};
loop: while(res.length) { // breaks on EOF or buffer full
auto begin=p; // start of a token's representation
auto sline=line;
switch(*p++){
// whitespace
case 0, 0x1A:
res[0].type = Tok!"EOF";
res[0].loc.rep=p[0..1];
res[0].loc.line = line; // TODO: Why is this necessary?
res=res[1..$];
num++;
break loop;
case ' ', '\t', '\v':
continue; // ignore whitespace
case '\r': if(*p=='\n') p++; goto case;
case '\n': line++;
continue;
// ! can be the start of !is or !in tokens
enum lexNotAndNotIsAndNotIn=q{
s = p; int cline = line; // save p and line in case we will NOT find in or is
findnotinnotis: for(;;){
switch(*p){
case 'i': p++;
if(*p == 's') res[0].type = Tok!"!is";
else if(*p == 'n') res[0].type = Tok!"!in";
else goto default;
p++;
// make sure the identifier stops here:
if('a' <= *p && *p <='z') goto default;
if('A' <= *p && *p <='Z') goto default;
if('0' <= *p && *p <='9') goto default;
if(*p&0x80){
try{
auto ch=utf.decode(p[0..4],len);
if(isAlpha(ch)) goto default;
}catch(Exception){} goto default;
}
break findnotinnotis;
case ' ', '\t', '\v':
p++; continue; // ignore whitespace
case '\r': if(*++p=='\n') p++; goto case;
case '\n': line++;
p++; continue;
case 0x80: .. case 0xFF:
len=0;
try{
auto ch=utf.decode(p[0..4],len);
if(isWhite(ch)) p+=len; continue;
}catch(Exception){} goto default;
default:
res[0].type = Tok!"!";
p = s;
line = cline;
break findnotinnotis;
}
}
};
// simple tokens
mixin(caseSimpleToken());
// slash is special
case '/':
switch(*p){
case '=': res[0].type = Tok!"/="; p++;
break;
case '/': p++;
while(((*p!='\n') & (*p!='\r')) & ((*p!=0) & (*p!=0x1A))) mixin(skipUnicodeCont);
continue; // ignore comment
case '*':
s=p++-1;
sl=line;
consumecom2: for(;;){
switch(*p){
mixin(caseNl); // handle newlines
case '*': p++; if(*p=='/'){p++; break consumecom2;} break;
case 0, 0x1A: errors~=tokError("unterminated /* */ comment",s[0..p-s],sl); break consumecom2;
default: mixin(skipUnicode);
}
}
continue; // ignore comment
case '+':
int d=1;
s=p++-1;
sl=line;
consumecom3: while(d){
switch(*p){
mixin(caseNl); // handle newlines
case '+': p++; if(*p=='/') d--, p++; break;
case '/': p++; if(*p=='+') d++, p++; break;
case 0, 0x1A: errors~=tokError("unterminated /+ +/ comment",s[0..p-s],sl); break consumecom3;
default: mixin(skipUnicode);
}
}
continue; // ignore comment
default: res[0].type = Tok!"/"; break;
}
break;
// dot is special
case '.':
if('0' > *p || *p > '9'){
if(*p != '.') res[0].type = Tok!".";
else if(*++p!='.') res[0].type = Tok!"..";
else res[0].type = Tok!"...", p++;
break;
}
goto case;
// numeric literals
case '0': .. case '9':
res[0] = lexNumber(--p);
if(res[0].type == Tok!"Error") errors~=res[0], res[0].type=Tok!"__error";
break;
// character literals
case '\'':
s = p; sl = line;
res[0].type = Tok!"' '";
if(*p=='\\'){
try p++, res[0].int64 = cast(ulong)readEscapeSeq(p);
catch(EscapeSeqException e) if(e.msg) errors~=tokError(e.msg,e.loc); else invCharSeq();
}else{
try{
len=0;
res[0].int64 = utf.decode(p[0..4],len);
p+=len;
}catch(Exception){invCharSeq();}
}
if(*p!='\''){
//while((*p!='\''||(p++,0)) && *p && *p!=0x1A) mixin(skipUnicodeCont);
errors~=tokError("unterminated character constant",(s-1)[0..p-s+1],sl);
}else p++;
break;
// string literals
// WYSIWYG string/AWYSIWYG string
case 'r':
if(*p!='"') goto case 'R';
p++; del='"';
goto skipdel;
case '`':
del = '`'; skipdel:
s = p; sl = line;
readwysiwyg: for(;;){
if(*p==del){p++; break;}
switch(*p){
mixin(caseNl); // handle newlines
case 0, 0x1A:
errors~=tokError("unterminated string literal", (s-1)[0..1],sl);
break readwysiwyg;
default: mixin(skipUnicode);
}
}
res[0].type = Tok!"``";
res[0].str = s[0..p-s-1]; // reference to code
goto lexstringsuffix;
// token string
case 'q':
if(*p=='"') goto delimitedstring;
if(*p!='{') goto case 'Q';
p++; s = p; sl = line;
del = 0;
readtstring: for(int nest=1;;){ // TODO: implement more efficiently
Token tt;
code=code[p-code.ptr..$]; // update code to allow recursion
lexTo((&tt)[0..1]);
p=code.ptr;
switch(tt.type){
case Tok!"EOF":
errors~=tokError("unterminated token string literal", (s-2)[0..1], sl);
break readtstring;
case Tok!"{": nest++; break;
case Tok!"}": nest--; if(!nest) break readtstring; break;
default: break;
}
}
res[0].type = Tok!"``";
res[0].str = s[0..p-s-1]; // reference to code
goto lexstringsuffix;
delimitedstring:
res[0].type = Tok!"``";
s=++p; sl=line;
switch(*p){
case 'a': .. case 'z':
case 'A': .. case 'Z':
for(;;){
switch(*p){
case '\r': if(p[1]=='\n') p++; goto case;
case '\n': line++; break;
case 0, 0x1A: break;
case 'a': .. case 'z':
case 'A': .. case 'Z':
case '0': .. case '9':
p++;
continue;
case 0x80: .. case 0xFF:
len=0;
try{auto ch=utf.decode(p[0..4],len);
if(isAlpha(ch)){p+=len; continue;}
break;
}catch(Exception){invCharSeq(); break;}
default: break;
}
break;
}
if(*p!='\n' && *p!='\r') errors~=tokError("heredoc identifier must be followed by a new line",p[0..1]);
while(((*p!='\n') & (*p!='\r')) & ((*p!=0) & (*p!=0x1A))) mixin(skipUnicodeCont); // mere error handling
auto ident=s[0..p-s];
if(*p=='\r'){line++;p++;if(*p=='\n') p++;}
else if(*p=='\n'){line++;p++;}
s=p;
readheredoc: while((*p!=0) & (*p!=0x1A)){ // always at start of new line here
for(auto ip=ident.ptr, end=ident.ptr+ident.length;;){
if(ip==end) break readheredoc;
switch(*p){
mixin(caseNl);
case 0x80: .. case 0xFF:
len=0;
try{auto ch=utf.decode(p[0..4],len);
if(isAlpha(ch)){
if(p[0..len]!=ip[0..len]) break;
p+=len; ip+=len; continue;
}
break;
}catch(Exception){invCharSeq(); break;}
default:
if(*p!=*ip) break;
p++; ip++; continue;
}
break;
}
while(((*p!='\n') & (*p!='\r')) & ((*p!=0) & (*p!=0x1A))) mixin(skipUnicodeCont);
if(*p=='\r'){line++;p++;if(*p=='\n') p++;}
else if(*p=='\n'){line++;p++;}
}
res[0].str = p>s+ident.length?s[0..p-s-ident.length]:""; // reference to code
if(*p!='"') errors~=tokError("unterminated heredoc string literal",(s-1)[0..1],sl);
else p++;
break;
default:
del=*p; char rdel=del; dchar ddel=0;
switch(del){
case '[': rdel=']'; s=++p; break;
case '(': rdel=')'; s=++p; break;
case '<': rdel='>'; s=++p; break;
case '{': rdel='}'; s=++p; break;
case '\r': if(p[1]=='\n') p++; goto case;
case '\n': line++; p++; goto case;
case ' ','\t','\v':
errors~=tokError("string delimiter cannot be whitespace",p[0..1]);
goto case;
case 0x80: case 0xFF:
s=p;
len=0;
try{
ddel=utf.decode(p[0..4],len);
s=p+=len;
}catch(Exception){invCharSeq();}
break;
default: p++; break;
}
if(ddel){
while((*p!=0) & (*p!=0x1A)){
if(*p=='\r'){line++;p++;if(*p=='\n') p++;}
else if(*p=='\n'){line++;p++;}
else if(*p<0x80){p++; continue;}
try{
auto x=utf.decode(p[0..4],len);
if(ddel==x){
res[0].str = s[0..p-s]; // reference to code
p+=len; break;
}
p+=len;
}catch(Exception){invCharSeq();}
}
}else{
for(int nest=1;(nest!=0) & (*p!=0) & (*p!=0x1A);){
if(*p=='\r'){line++;p++;if(*p=='\n') p++;}
else if(*p=='\n'){line++;p++;}
else if(*p==rdel){nest--; p++;}
else if(*p==del){nest++; p++;}
else if(*p & 0x80){
try{
utf.decode(p[0..4],len);
p+=len;
}catch(Exception){invCharSeq();}
}else p++;
}
res[0].str = s[0..p-s-1]; // reference to code
}
if(*p!='"'){
if(*p) errors~=tokError("expected '\"' to close delimited string literal",p[0..1]);
else errors~=tokError("unterminated delimited string literal",(s-2)[0..1],sl);
}else p++;
break;
}
goto lexstringsuffix;
// Hex string
case 'x':
if(*p!='"') goto case 'X';
auto r=appender!string(); p++; s=p; sl=line;
readhexstring: for(int c=0,ch,d;;p++,c++){
switch(*p){
mixin(caseNl); // handle newlines
case ' ', '\t', '\v': c--; break;
case 0, 0x1A:
errors~=tokError("unterminated hex string literal",(s-1)[0..1],sl);
break readhexstring;
case '0': .. case '9': d=*p-'0'; goto handlexchar;
case 'a': .. case 'f': d=*p-('a'-0xa); goto handlexchar;
case 'A': .. case 'F': d=*p-('A'-0xA); goto handlexchar;
handlexchar:
if(c&1) r.put(cast(char)(ch|d));
else ch=d<<4; break;
case '"':
if(c&1) errors~=tokError(format("found %s character%s when expecting an even number of hex digits",toEngNum(c),c!=1?"s":""),s[0..p-s]);
p++; break readhexstring;
default:
if(*p<128) errors~=tokError(format("found '%s' when expecting hex digit",*p),p[0..1]);
else{
s=p;
len=0;
try{
auto chr = utf.decode(p[0..4],len);
p+=len-1;
if(isWhite(chr)) break; //TODO: newlines
}catch(Exception){invCharSeq();}
errors~=tokError(format("found '%s' when expecting hex digit",s[0..len]),s[0..len]);
}
break;
}
}
res[0].type = Tok!"``";
res[0].str = r.data;
goto lexstringsuffix;
// DQString
case '"':
{
// appender only used if there is an escape sequence, slice otherwise
// TODO: how costly is eager initialization?
auto r=appender!string();
auto start = s = p;
readdqstring: for(;;){
switch(*p){
mixin(caseNl);
case 0, 0x1A:
errors~=tokError("unterminated string literal",(start-1)[0..1]);
break readdqstring;
case '\\':
r.put(s[0..p++-s]);
try r.put(readEscapeSeq(p));
catch(EscapeSeqException e) if(e.msg) errors~=tokError(e.msg,e.loc); else invCharSeq();
s = p;
continue;
case '"': p++; break readdqstring;
default: mixin(skipUnicode);
}
}
res[0].type = Tok!"``";
if(!r.data.length) res[0].str = start[0..p-1-start];
else{ r.put(s[0..p-1-s]); res[0].str = r.data; }
goto lexstringsuffix;
}
lexstringsuffix:
if(*p=='c') res[0].type = Tok!"``c", p++;
else if(*p=='w') res[0].type = Tok!"``w", p++;
else if(*p=='d') res[0].type = Tok!"``d", p++;
break;
// identifiers and keywords
case '_':
case 'a': .. case 'p': /*q, r*/ case 's': .. case 'w': /*x*/ case 'y', 'z':
case 'A': .. case 'Z':
s = p-1;
identifier:
readident: for(;;){
switch(*p){
case '_':
case 'a': .. case 'z':
case 'A': .. case 'Z':
case '0': .. case '9':
p++;
break;
case 0x80: .. case 0xFF:
len=0;
try if(isAlpha(utf.decode(p[0..4],len))) p+=len;
else break readident;
catch(Exception){break readident;} // will be caught in the next iteration
break;
default: break readident;
}
}
res[0].name = s[0..p-s];
res[0].type=isKeyword(res[0].name);
break;
case 0x80: .. case 0xFF:
len=0; p--;
try{auto ch=utf.decode(p[0..4],len);
s=p, p+=len;
if(isAlpha(ch)) goto identifier;
if(!isWhite(ch)) errors~=tokError(format("unsupported character '%s'",ch),s[0..len]);
// else if(isNewLine(ch)) line++; // TODO: implement this everywhere
continue;
}catch(Exception){} goto default;
default:
p--; invCharSeq(); p++;
continue;
}
res[0].loc.rep=begin[0..p-begin];
res[0].loc.line=sline;
res=res[1..$];
num++;
}
code=code[p-code.ptr..$];
return num;
}
/* Lex a number FSM. TDPL p33/35
Returns either a valid literal token or one of the following:
errExp = tokError("exponent expected");
errsOverflow = tokError("signed integer constant exceeds long.max");
errOverflow = tokError("integer constant exceeds ulong.max");
//errRepr = tokError("numerical constant is not representable in [float|double|real]");
errOctDepr = tokError("octal literals are deprecated");
*/
private Token lexNumber(ref immutable(char)* _p){
static assert(real.mant_dig <= 64);
auto p = _p;
enum dlim = ulong.max / 10; // limit for decimal values (prevents overflow)
enum helim = int.max / 10; // ditto for binary exponent (hex floats)
enum elim = int.max / 10; // ditto for exponent
Token tok;
bool leadingzero = 0;
bool isfloat = 0;// true if floating point literal
bool isimag = 0; // true if imaginary floating point literal. as in DMD, this only works for decimals
bool toobig = 0;// true if value exceeds ulong.max
ulong val = 0; // current literal value
real rval = 0.0L;// real value
long exp = 0; // exponent
bool neg = 0; // exponent is negative
int dig = 0; // number of consumed digits
int dot = -1; // position of decimal dot, counted from the left (-1=not encountered yet)
int adjexp = 0; // exponent adjustment due to very long literal
enum : int {DEC, BIN, OCT, HEX}
int base = DEC;
// powers of 2 and 10 for fast computation of rval given the mantissa and exponents. (TODO: Get rid of pw2)
static immutable pw2 = mixin("["~{string r; foreach(i;0..16) r~="0x1p"~to!string(1L<<i)~"L,"; return r;}()~"]");
static immutable pw10= mixin("["~{string r; for(int i=15;i>=0;i--) r~= "1e"~to!string(1L<<i)~"L,"; return r;}()~"]");
static immutable pn10= mixin("["~{string r; for(int i=15;i>=0;i--) r~= "1e-"~to!string(1L<<i)~"L,"; return r;}()~"]");
selectbase: switch(*p){
case '0':
p++;
switch(*p){
case 'x', 'X':
p++;
base = HEX;
while(*p == '0') p++; // eat leading zeros
readhex: for(;dig<16;p++){
switch(*p){
case '0': .. case '9':
val = val << 4 | *p-'0'; dig++;
break;
case 'a': .. case 'f':
val = val << 4 | *p-('a'-0xa); dig++;
break;
case 'A': .. case 'F':
val = val << 4 | *p-('A'-0xA); dig++;
break;
case '.':
if(p[1] != '.' && dot == -1) dot = dig, isfloat=1;
else break readhex; goto case;
case '_': // ignore embedded _
break;
default:
break readhex;
}
}
if(dig == 16 && ('8' <= *p && *p <= '9' || 'a' <= *p && *p <= 'f' || 'A' <=*p && *p <= 'F')){ // round properly
val++;
if(!val) val = 1, adjexp = 16; // cope with overflow
}
consumehex: for(;;p++){
switch(*p){
case '0': .. case '9':
case 'a': .. case 'f':
case 'A': .. case 'F':
dig++; adjexp++;
break;
case '.':
if(p[1] != '.' && dot == -1) dot = dig, isfloat = 1; // break; }
else break consumehex; goto case;
case '_': // ignore embedded _
break;
case 'p', 'P':
isfloat = 1;
p++;
neg = 0;
switch(*p){
case '-': neg = 1; goto case;
case '+': p++; goto default;
default: break;
}
if('0'> *p || *p > '9') goto Lexp;
readhexp: for(;;p++){
switch(*p){
case '0': .. case '9':
exp = (exp << 1) + (exp << 3) + *p -'0';
break;
case '_': // ignore embedded _.
break;
default:
break readhexp;
}
if(exp > helim){p++;break readhexp;}
}
goto default;
default:
break consumehex;
}
}
isfloat |= *p == 'f' || *p == 'F';
if(isfloat){ // compute value of hex floating point literal
if(dot==-1) dot = dig;
if(neg) exp += dig - dot - adjexp << 2L;
else exp -= dig - dot - adjexp << 2L;
if(exp<0) neg = !neg, exp=-exp;
if('0' <= *p && *p <= '9' || exp>=8184 || !val){
p++, rval = neg || !val ? .0L : real.infinity;
while('0' <= *p && *p <= '9') p++;
}else{ // TODO: Could construct value directly in memory
rval = 1.0L;
for(int i=0,j=cast(int)(exp&-1u);i<16;i++,j>>=1) if(j&1) rval*=pw2[i];
if(neg) rval = val / rval;
else rval *= val;
}
}
if(adjexp) val = ulong.max; // for error handling
break selectbase;
case 'b', 'B':
p++;
base = BIN;
readbin: for(;dig<64;p++){
switch(*p){
case '0', '1':
val <<= 1; dig++;
val |= *p-'0'; goto case;
case '_': // ignore embedded _
break;
default:
break readbin;
}
}
break selectbase;
/*case 'o': // non-standard
base = OCT;*/
default: // 0xxx-style octal is deprecated, interpret as decimal and give an error
leadingzero = 1;
break;
}
while(*p == '0') p++; // eat leading zeros of decimal
if(('1' > *p || *p > '9') && *p != '.'){
isfloat |= *p == 'f' || *p == 'F' || *p == 'i' || *p == 'L' && p[1]=='i';
leadingzero=0; break;
}
goto case;
case '1': .. case '9': case '.':
readdec: for(;;p++){
switch(*p){
case '0': .. case '9':
val = (val << 1) + (val << 3) + *p -'0'; dig++;
break;
case '.':
if('0'<=p[1]&&p[1]<='9' && dot == -1) dot = dig, isfloat=1; // break; }
else break readdec; goto case;
case '_': // ignore embedded _
break;
default:
break readdec;
}
if(val >= dlim){
p++;
if(val > dlim) break readdec;
if('0' <= *p && *p <= '5') val = (val << 1) + (val << 3) + *p -'0', dig++, p++;
break readdec;