This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
macros.d
1714 lines (1496 loc) · 51.6 KB
/
macros.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
/**
* C preprocessor
* Copyright: 2013 by Digital Mars
* License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright
*/
module macros;
import core.stdc.stdlib;
import core.stdc.string;
import std.algorithm;
import std.array;
import std.ascii;
import std.range;
import std.stdio;
import std.traits;
import context;
import id;
import util;
import number;
import ranges;
import skip;
import textbuf;
import charclass;
bool logging;
// Embedded escape sequence commands
enum ESC : ubyte
{
start = '\x00', // 0 can never appear in legit input, so this is the start
arg1 = '\x01',
concat = '\xFF',
stringize = '\xFE',
space = '\xFD',
brk = '\xFC', // separate adjacent tokens
expand = '\xFB',
}
/************************************
* Transform the macro definition 'text' into a replacement list.
* Embed escape sequence commands for argument substitution, argument stringizing,
* and token concatenation.
* Input:
* objectLike true if object-like macro, false if function-like
* parameters macro parameters
* text Original text; must end with \n
* Returns:
* replacement list with embedded commands for inserting arguments and stringizing
*/
ustring macroReplacementList(R)(ref R text, bool objectLike, ustring[] parameters)
{
alias Unqual!(ElementEncodingType!R) E;
if (text.empty)
return cast(ustring)"";
E[1000] tmpbuf = void;
auto outbuf = Textbuf!(uchar, "mrl")(tmpbuf);
outbuf.put(0);
while (1)
{ E c = cast(E)text.front;
text.popFront();
switch (c)
{
case '\t':
c = ' ';
goto case;
case ' ':
if (outbuf.length == 1 || // no leading whitespace
outbuf.last() == ' ') // collapse adjacent whitespace into one ' '
continue;
break;
case '\n': // reached the end of the input
if (outbuf.last() == ' ')
outbuf.pop(); // no trailing whitespace
if (outbuf.last() == ESC.concat && outbuf[outbuf.length - 2] == ESC.start)
err_fatal("## cannot appear at end of macro text");
//textPrint(outbuf[1 .. outbuf.length]);
return outbuf[1 .. outbuf.length].idup;
case '\r':
continue;
case '"':
/* Skip over character literals and string literals without
* examining their insides
*/
outbuf.put(c);
text = text.skipStringLiteral(outbuf);
continue;
case 'R':
if (cast(E)text.front == '"')
{
text.popFront();
outbuf.put('R');
outbuf.put('"');
text = text.skipRawStringLiteral(outbuf);
continue;
}
goto default;
case '\'':
outbuf.put(c);
text = text.skipCharacterLiteral(outbuf);
continue;
case '/':
if (outbuf.last() == '/')
{ // C++ style comments end the input
text = text.skipCppComment();
outbuf.pop();
goto case '\n';
}
break;
case '*':
if (outbuf.last() == '/')
{ // C style comments are treated as a single ' '
text = text.skipCComment();
outbuf.pop();
goto case '\t';
}
break;
case '#':
if (text.front == '#')
{
text.popFront();
/* The ## token concatenation operator.
* Remove leading and trailing spaces, replace with ESC.start ESC.concat
*/
if (outbuf.last() == ' ')
outbuf.pop();
if (outbuf.length == 1)
err_fatal("## cannot appear at beginning of macro text");
outbuf.put(ESC.start);
outbuf.put(ESC.concat);
text = text.skipWhitespace();
continue;
}
else if (!objectLike)
{
/* The # stringize operator, parameter must immediately follow
*/
text = text.skipWhitespace();
StaticArrayBuffer!(uchar, 1024) id = void;
id.initialize();
text = text.inIdentifier(id);
auto argi = countUntil(parameters, id[]);
if (argi == -1)
err_fatal("# must be followed by parameter");
else
{
outbuf.put(ESC.start);
outbuf.put(ESC.stringize);
outbuf.put(cast(uchar)(argi + 1));
}
continue;
}
break;
default:
if (isIdentifierStart(c))
{
if (parameters.length)
{
StaticArrayBuffer!(uchar, 1024) id = void;
id.initialize();
id.put(c);
text = text.inIdentifier(id);
auto argi = countUntil(parameters, id[]);
if (argi == -1)
{
outbuf.put(id[]);
}
else
{
outbuf.put(ESC.start);
outbuf.put(cast(uchar)(argi + 1));
}
}
else
{
outbuf.put(c);
while (1)
{
E c2 = cast(E)text.front;
if (isAlphaNum(c2) || c2 == '_' || c2 == '$')
{
text.popFront();
outbuf.put(c2);
}
else
break;
}
}
continue;
}
break;
}
outbuf.put(c);
}
assert(0);
}
unittest
{
ustring s;
auto r = cast(ustring)"\n";
s = r.macroReplacementList(true, null);
assert(s == "");
r = cast(ustring)" \t/*hello*/ //\n";
s = r.macroReplacementList(true, null);
assert(s == "");
r = cast(ustring)"# \n";
s = r.macroReplacementList(true, null);
assert(s == "#");
r = cast(ustring)"a ## /**/z\n";
s = r.macroReplacementList(true, null);
assert(s == "a" ~ ESC.start ~ ESC.concat ~ "z");
r = cast(ustring)"x#/**/abc y\n";
s = r.macroReplacementList(false, cast(ustring[])(["abc"]));
//writefln("'%s', %s", s, s.length);
assert(s == "x" ~ ESC.start ~ ESC.stringize ~ ESC.arg1 ~ " y");
r = cast(ustring)"x abc/**/y\n";
s = r.macroReplacementList(false, cast(ustring[])(["abc"]));
assert(s == "x " ~ ESC.start ~ ESC.arg1 ~ " y");
r = cast(ustring)"x \"abc\" R\"a(abc)a\" 'a' \ry\n";
s = r.macroReplacementList(false, cast(ustring[])(["abc","a"]));
assert(s == "x \"abc\" R\"a(abc)a\" 'a' y");
}
/***********************************************
* Remove leading and trailing whitespace (' ' and ESC.brk).
* Leave ESC.space intact.
* Returns:
* modified input
*/
uchar[] trimWhiteSpace(uchar[] text)
{
// Remove leading
size_t fronti;
size_t frontn;
while (fronti < text.length)
{
switch (text[fronti])
{
case ' ':
case ESC.brk:
++fronti;
continue;
case ESC.space:
++frontn;
++fronti;
continue;
default:
break;
}
break;
}
// Remove trailing
size_t backi = text.length;
size_t backn;
while (backi > fronti + 1)
{
switch (text[backi - 1])
{
case ' ':
case ESC.brk:
--backi;
continue;
case ESC.space:
++backn;
--backi;
continue;
default:
break;
}
break;
}
if (frontn)
text[fronti - frontn .. fronti] = ESC.space;
if (backn)
text[backi .. backi + backn] = ESC.space;
//writefln("frontn %d fronti %d backi %d backn %d", frontn, fronti, backn, backi);
return text[fronti - frontn .. backi + backn];
}
unittest
{
uchar[0] a;
auto s = trimWhiteSpace(a);
assert(s == "");
char[1] b = " ";
s = trimWhiteSpace(cast(ubyte[])b);
assert(s == "");
ubyte[6] c = cast(ubyte[])("" ~ ESC.brk ~ " a " ~ ESC.brk ~ " ");
s = trimWhiteSpace(cast(uchar[])c);
assert(s == "a");
ubyte[6] d = cast(ubyte[])("" ~ ESC.space ~ " a " ~ ESC.space ~ " ");
s = trimWhiteSpace(cast(uchar[])d);
assert(s == x"FD 61 FD");
ubyte[1] e = cast(ubyte[])("" ~ ESC.space ~ "");
s = trimWhiteSpace(cast(uchar[])e);
assert(s == x"FD");
ubyte[8] f = cast(ubyte[])("" ~ ESC.space ~ " ab " ~ ESC.space ~ "" ~ ESC.space ~ " ");
s = trimWhiteSpace(cast(uchar[])f);
//writefln("'%s', %s", s, s.length);
assert(s == x"FD 61 62 FD FD");
}
/******************************************
* Remove all ESC.space and ESC.brk markers.
* Remove all leading and trailing spaces.
* All done in-place.
*/
private uchar[] trimEscWhiteSpace(uchar[] text)
{
auto p = text.ptr;
bool leading = true;
foreach (uchar c; text)
{
switch (c)
{
case ESC.space:
case ESC.brk:
continue;
case ' ':
if (leading)
continue;
break;
default:
leading = false;
break;
}
*p++ = c;
}
while (p > text.ptr && p[-1] == ' ')
{
--p;
}
return text[0 .. p - text.ptr];
}
unittest
{
ubyte[11] a = cast(ubyte[])("" ~ ESC.space ~ " a" ~ ESC.brk ~ " " ~ ESC.space ~ "b " ~ ESC.space ~ "" ~ ESC.space ~ " ");
auto s = trimEscWhiteSpace(cast(uchar[])a);
//writefln("'%s', %s", s, s.length);
assert(s == "a b");
}
/*************************************
* Stringize the argument of the # operator per C99 6.10.3.2.2
* Output:
* writes to OutputRange r
*/
private void stringize(R)(ref R outbuf, const(uchar)[] text)
{
// Remove leading spaces
size_t i;
for (; i < text.length; ++i)
{
auto c = text[i];
if (!(c == ' ' || c == ESC.space || c == ESC.brk))
break;
}
text = text[i .. $];
// Remove trailing spaces
for (i = text.length; i; --i)
{
auto c = text[i - 1];
if (!(c == ' ' || c == ESC.space || c == ESC.brk))
break;
}
text = text[0 .. i];
outbuf.put('"');
// Adapter OutputRange to escape certain characters
struct EscString
{
void put(uchar c)
{
switch (c)
{
case '"':
case '?':
case '\\':
outbuf.put('\\');
goto default;
default:
outbuf.put(c);
break;
case ESC.expand:
case ESC.brk:
break; // ignore
}
}
}
EscString es;
while (!text.empty)
{
auto c = cast(uchar)text.front;
text.popFront();
switch (c)
{
case 'R':
if (!text.empty && text.front == '"')
{
outbuf.put('R');
es.put('"');
text.popFront();
text = text.skipRawStringLiteral(es);
}
else
goto default;
break;
case '"':
es.put(c);
text = text.skipStringLiteral(es);
break;
case '\'':
outbuf.put(c);
text = text.skipCharacterLiteral(es);
break;
case '?':
outbuf.put('\\');
goto default;
default:
outbuf.put(c);
break;
case ESC.expand:
case ESC.brk:
break; // ignore
}
}
outbuf.put('"');
}
unittest
{
uchar[10] tmpbuf = void;
auto outbuf = Textbuf!uchar(tmpbuf);
outbuf.initialize();
stringize(outbuf, cast(ustring)" ");
assert(outbuf[] == `""`);
outbuf.initialize();
stringize(outbuf, cast(ustring)(" " ~ ESC.space ~ "" ~ ESC.brk ~ "a" ~ ESC.expand ~ "" ~ ESC.brk ~ "bc" ~ ESC.space ~ "" ~ ESC.brk ~ " "));
assert(outbuf[] == `"abc"`);
outbuf.initialize();
stringize(outbuf, cast(ustring)(`ab?\\x'y'"z"`));
assert(outbuf[] == `"ab\?\\x'y'\"z\""`);
outbuf.initialize();
stringize(outbuf, cast(ustring)(`'\'a\\'b\`));//`
assert(outbuf[] == `"'\\'a\\\\'b\"`);
outbuf.initialize();
stringize(outbuf, cast(ustring)(`"R"x(aa)x""`));
assert(outbuf[] == `"\"R\"x(aa)x\"\""`);
outbuf.initialize();
ubyte[] u = cast(ubyte[])"R\"x(a?\\a)x\"";
stringize(outbuf, cast(ustring)u);
//writefln("'%s', %s", s, s.length);
assert(outbuf[] == `"R\"x(a\?\\a)x\""`);
outbuf.free();
}
/********************************************
* Fix _Pragma arg by destringizing per C99 6.10.9:
* "The string literal is destringized by deleting the L prefix, if
* present, deleting the leading and trailing double-quotes, replacing each
* escape sequence \" by a double-quote, and replacing each escape sequence \\ by
* a single backslash."
*/
void destringizePragmaArg(S, T)(S args, ref T argbuffer)
{
bool slash;
assert(args.length == 1);
auto arg = args[0];
//writefln("destringizePragmaArg('%s')", cast(string)arg);
size_t len = argbuffer.length;
if (arg.length < 2)
goto Lerror;
if (arg[0] == ' ')
arg = arg[1 .. $];
if (arg[0] == 'L')
arg = arg[1 .. $];
if (arg.length < 2)
goto Lerror;
if (arg[0] != '"')
goto Lerror;
arg = arg[1 .. $];
argbuffer.initialize(); // recycle argbuffer, overwriting arg
foreach (i, c; arg)
{
switch (c)
{
case '\\':
slash = !slash;
if (slash)
continue;
break;
case '"':
if (!slash)
{
if (i + 1 == arg.length)
{ }
else if (i + 2 == arg.length && arg[i + 1] == ' ')
{ } // allow trailing space
else
goto Lerror;
args.pop();
args.put(cast(ustring)(argbuffer[]));
return;
}
slash = false;
break;
default:
slash = false;
break;
}
argbuffer.put(c);
}
Lerror:
err_fatal("argument to _Pragma must be a string literal");
}
/********************************************
* Get Ith arg from args.
*/
private ustring null_arg = cast(ustring)"";
ustring getIthArg(ustring[] args, size_t argi)
{
if (args.length < argi)
return null;
ustring a = args[argi - 1];
if (a is null)
a = null_arg; // so we can distinguish a missing arg (null_arg) from an empty arg ("")
return a;
}
/*******************************************
* Build macro expanded text, writing it to buffer.
*/
//debug=macroExpandedText;
void macroExpandedText(Context, R)(Id* m, ustring[] args, ref R buffer)
{
debug (macroExpandedText)
{
writefln("macroExpandedText(m = '%s')", cast(string)m.name);
//write("\ttext = "); textPrint(m.text);
writefln("\ttext = '%s'", cast(string)m.text);
for (size_t i = 1; i <= args.length; ++i)
{
auto a = getIthArg(args, i);
writefln("\t[%d] = '%s'", i, cast(string)a);
}
auto bufferlen = buffer.length;
}
/* Determine if we should elide commas ( ,##__VA_ARGS__ extension)
*/
size_t va_args = 0;
if (m.flags & Id.IDdotdotdot)
{ const margs = m.parameters.length;
/* Only elide commas if there are more arguments than ...
* This is unlike GCC, which also elides comments if there is only a ...
* parameter, unless Standard compliant switches are thrown.
*/
if (margs >= 2)
{
// Only elide commas if __VA_ARGS__ was missing (not blank)
if (getIthArg(args, margs) is null_arg)
va_args = margs;
}
}
/* The expanded version of each element in args[] is stored in expbuf[],
* one after the other.
* This is so that args[] are never expanded more than once, subsequent
* references will use the previously expanded version.
* Slices of previously expanded versions are kept in cache[].
*/
uchar[512] tmpbuf = void;
auto expbuf = Textbuf!(uchar,"met")(tmpbuf);
assert(expbuf.length == 0);
// cache[arg-index] contains the offsets in expbuf where the macro
// argument at the given index was previously expanded.
size_t[2][20] cachetmp = void;
size_t[2][] cache = cachetmp;
if (args.length + 1 > cachetmp.length)
{
auto p = malloc(size_t[2].sizeof * (args.length + 1));
assert(p);
cache = (cast(size_t[2]*)p)[0 .. args.length + 1];
}
for (size_t i = 0; i < args.length + 1; ++i)
{
cache[i][1] = 0; // the check for initialization is only done on the [1] element
}
scope (exit)
{
if (args.length + 1 > cachetmp.length)
{
free(cache.ptr);
}
expbuf.free();
//foreach (arg; args)
//if (arg.ptr) free(cast(void*)arg.ptr);
debug (macroExpandedText)
{
writefln("-macroExpandedText(m = '%s')", cast(string)m.name);
writefln("\t'%s'", cast(string)buffer[bufferlen .. buffer.length]);
}
}
/* ESC.start, ESC.stringize and ESC.concat only appear in text[]
*/
for (size_t q = 0; q < m.text.length; ++q)
{
if (m.text[q] == ESC.start)
{
bool expand = true;
bool trimleft = false;
bool trimright = false;
Lagain2:
auto argi = m.text[++q];
switch (argi)
{
case ESC.start: // ESC.start was 'quoted'
buffer.put(ESC.start);
continue;
case ESC.stringize: // stringize argument
{
const argi2 = m.text[++q];
const arg = getIthArg(args, argi2);
stringize(buffer, arg);
continue;
}
case ESC.concat:
if (q + 1 < m.text.length && m.text[q + 1] == ESC.start)
{
/* Look for special case of:
* ',' ESC.start ESC.concat ESC.start __VA_ARGS__
*/
if (q >= 2 && q + 2 < m.text.length &&
m.text[q + 2] == va_args && m.text[q - 2] == ',')
{
/* Elide the comma that was already in buffer,
* replace it with ESC.brk
*/
buffer.pop();
buffer.put(ESC.brk);
}
expand = false;
trimleft = true;
++q;
goto Lagain2;
}
continue; // ignore
default:
// If followed by ESC.concat, don't expand
if (q + 2 < m.text.length &&
m.text[q + 1] == ESC.start && m.text[q + 2] == ESC.concat)
{ expand = false;
trimright = true;
/* Special case of ESC.start i ESC.start ESC.concat ESC.start j
* Paul Mensonides writes:
* In summary, blue paint (ESC.expand) on either operand of
* ## should be discarded unless the concatenation doesn't
* produce a new identifier--which can only happen (in
* well-defined code) via the concatenation of a
* placemarker. (Concatenation that doesn't produce a
* single preprocessing token produces undefined
* behavior.)
*/
size_t argj;
if (q + 4 < m.text.length &&
m.text[q + 3] == ESC.start &&
(argj = m.text[q + 4]) != ESC.start &&
argj != ESC.stringize &&
argj != ESC.concat)
{
//writeln("\tspecial CAT case");
auto a = getIthArg(args, argi);
while (a.length && (a[a.length - 1] == ' ' || a[a.length - 1] == ESC.space))
a = a[0 .. $ - 1];
auto b = getIthArg(args, argj);
auto bstart = b;
while (b.length && (b[0] == ' ' || b[0] == ESC.space || b[0] == ESC.expand))
b = b[1 .. $];
if (!(b.length && isIdentifierChar(b[0])))
break;
if (!a.length && b.length < bstart.length && b.ptr[-1] == ESC.expand)
{ // Keep the ESC.expand
buffer.put(ESC.expand);
buffer.put(b);
q += 4;
continue;
}
size_t pe = a.length;
while (1)
{
if (!pe)
goto L1;
--pe;
if (a[pe] == ESC.expand)
break;
}
if (!isIdentifierStart(a[pe + 1]))
break;
for (size_t k = pe + 1; k < a.length; ++k)
{
if (!isIdentifierChar(a[k]))
goto L1;
}
//writefln("CAT pe = %s, a.length = %s, b.length = %s", pe, a.length, b.length);
buffer.put(a[0 .. pe]);
buffer.put(a[pe + 1 .. a.length]);
buffer.put(b);
q += 4;
continue;
}
}
break;
}
L1:
auto a = getIthArg(args, argi);
//writefln("\t\targ[%d] = '%s'", argi, cast(string)a);
if (expand)
{
//writefln("\t\tbefore '%s'", cast(string)a);
if (cache[argi][1]) // if already expanded
{
// Reuse cached version
auto t = expbuf[cache[argi][0] .. cache[argi][1]];
if (t.length && isMultiTok(t[0]))
buffer.put(ESC.brk);
buffer.put(t);
if (t.length && isMultiTok(t[t.length - 1]))
buffer.put(ESC.brk);
}
else
{
auto start = expbuf.length;
macroExpand!Context(a, expbuf);
//writefln("\t\tafter '%s'", cast(string)expbuf[]);
auto s = expbuf[start .. expbuf.length];
auto t = trimEscWhiteSpace(s);
//writefln("\t\ttrim '%s'", cast(string)t);
if (t.length && isMultiTok(t[0]))
buffer.put(ESC.brk);
buffer.put(t);
if (t.length && isMultiTok(t[t.length - 1]))
buffer.put(ESC.brk);
auto b = expbuf[];
cache[argi][0] = t.ptr - b.ptr;
cache[argi][1] = t.ptr - b.ptr + t.length;
assert(t.length <= b.length);
}
}
else
{
if (trimleft)
{
while (a.length && (a[0] == ' ' || a[0] == ESC.space || a[0] == ESC.expand))
a = a[1 .. $];
}
if (trimright)
{
while (a.length && (a[a.length - 1] == ' ' || a[a.length - 1] == ESC.space))
a = a[0 .. $ - 1];
}
buffer.put(a);
}
}
else
buffer.put(m.text[q]);
}
}
/*****************************************
* Take string text, fully macro expand it, and write the result to outbuf.
*/
//debug=MacroExpand;
// This table is to speed the switch() lookup in macroExpand()
private enum EXPAND : ubyte { none, doublequote, singlequote, expand, zero, dot, digit, idstart }
private immutable EXPAND[256] expand;
static this()
{
// Initialize lookup table
foreach (uint u; 0 .. 256)
{
EXPAND e;
switch (u)
{
case '"': e = EXPAND.doublequote; break;
case '\'': e = EXPAND.singlequote; break;
case ESC.expand: e = EXPAND.expand; break;
case 0: e = EXPAND.zero; break;
case '.': e = EXPAND.dot; break;
case '0': .. case '9': e = EXPAND.digit; break;
default:
if (isIdentifierStart(cast(ubyte)u))
e = EXPAND.idstart;
break;
}
expand[u] = e;
}
}
void macroExpand(Context, R)(const(uchar)[] text, ref R outbuf)
{
debug (MacroExpand)
writefln("+macroExpand(text = '%s')", cast(string)text);
alias uchar E;
auto ctx = Context.getContext();
ctx.expanded.off();
auto r = ctx.pushContext();
r.push(text);
r.popFront();
Louter:
while (1) //(!r.empty) // r.front returns 0 for end of input
{
auto c = r.front;
final switch (expand[c])
{
case EXPAND.doublequote:
/* Skip over character literals and string literals without
* examining their insides
*/
r.popFront();
if (outbuf.length && outbuf.last() == 'R')
{
outbuf.put(c);
r = r.skipRawStringLiteral(outbuf);
}
else
{
outbuf.put(c);
r = r.skipStringLiteral(outbuf);
}
continue;
case EXPAND.singlequote:
r.popFront();
outbuf.put(c);
r = r.skipCharacterLiteral(outbuf);
continue;
case EXPAND.expand:
r.popFront();
outbuf.put(c);
c = r.front;
if (isIdentifierStart(c))
{
r = r.inIdentifier(outbuf);
continue;
}
r.popFront();
break;
case EXPAND.zero:
goto Ldone;
case EXPAND.dot:
r.popFront();
outbuf.put(c);
if (!r.empty)
{
c = r.front;
switch (c)
{
case '0': .. case '9':
r = r.skipFloat(outbuf, false, true, false);
break;
case '*':
r.popFront();
outbuf.put(c);
break;
case '.':
r.popFront();
outbuf.put(c);
if (!r.empty && r.front == '.')
{
outbuf.put(c);
r.popFront();
continue;
}
break;
default:
break;
}
}
continue;
case EXPAND.digit:
r = r.skipFloat(outbuf, false, false, false);
continue;
case EXPAND.idstart:
{
auto expanded = r.isExpanded();
size_t len = outbuf.length;
r = r.inIdentifier(outbuf);