forked from xtravar/CppNet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Preprocessor.cs
2255 lines (2016 loc) · 67 KB
/
Preprocessor.cs
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
/*
* Anarres C Preprocessor
* Copyright (c) 2007-2008, Shevek
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
namespace CppNet {
/**
* A C Preprocessor.
* The Preprocessor outputs a token stream which does not need
* re-lexing for C or C++. Alternatively, the output text may be
* reconstructed by concatenating the {@link Token#getText() text}
* values of the returned {@link Token Tokens}. (See
* {@link CppReader}, which does this.)
*/
/*
Source file name and line number information is conveyed by lines of the form
# linenum filename flags
These are called linemarkers. They are inserted as needed into
the output (but never within a string or character constant). They
mean that the following line originated in file filename at line
linenum. filename will never contain any non-printing characters;
they are replaced with octal escape sequences.
After the file name comes zero or more flags, which are `1', `2',
`3', or `4'. If there are multiple flags, spaces separate them. Here
is what the flags mean:
`1'
This indicates the start of a new file.
`2'
This indicates returning to a file (after having included another
file).
`3'
This indicates that the following text comes from a system header
file, so certain warnings should be suppressed.
`4'
This indicates that the following text should be treated as being
wrapped in an implicit extern "C" block.
*/
public class Preprocessor : IDisposable {
private class InternalSource : Source {
public override Token token()
{
throw new LexerException("Cannot read from " + getName());
}
internal override String getPath()
{
return "<internal-data>";
}
internal override String getName() {
return "internal data";
}
}
private static readonly Source INTERNAL = new InternalSource();
private static readonly Macro __LINE__ = new Macro(INTERNAL, "__LINE__");
private static readonly Macro __FILE__ = new Macro(INTERNAL, "__FILE__");
private static readonly Macro __COUNTER__ = new Macro(INTERNAL, "__COUNTER__");
private List<Source> inputs;
/* The fundamental engine. */
private Dictionary<String,Macro> macros;
private Stack<State> states;
private Source source;
/* Miscellaneous support. */
private int counter;
/* Support junk to make it work like cpp */
private List<String> quoteincludepath; /* -iquote */
private List<String> sysincludepath; /* -I */
private List<String> frameworkspath;
private Feature features;
private Warning warnings;
private VirtualFileSystem filesystem;
private PreprocessorListener listener;
private List<string> _importedPaths = new List<string>();
public Preprocessor() {
this.inputs = new List<Source>();
this.macros = new Dictionary<String,Macro>();
macros.Add(__LINE__.getName(), __LINE__);
macros.Add(__FILE__.getName(), __FILE__);
macros.Add(__COUNTER__.getName(), __COUNTER__);
this.states = new Stack<State>();
states.Push(new State());
this.source = null;
this.counter = 0;
this.quoteincludepath = new List<String>();
this.sysincludepath = new List<String>();
this.frameworkspath = new List<String>();
this.features = Feature.NONE;
this.warnings = Warning.NONE;
this.filesystem = new JavaFileSystem();
this.listener = null;
EmitExtraLineInfo = true;
}
public Preprocessor(Source initial) :
this() {
addInput(initial);
}
/** Equivalent to
* 'new Preprocessor(new {@link FileLexerSource}(file))'
*/
public Preprocessor(FileInfo file) :
this(new FileLexerSource(file)) {
}
public bool EmitExtraLineInfo { get; set; }
/**
* Sets the VirtualFileSystem used by this Preprocessor.
*/
public void setFileSystem(VirtualFileSystem filesystem) {
this.filesystem = filesystem;
}
/**
* Returns the VirtualFileSystem used by this Preprocessor.
*/
public VirtualFileSystem getFileSystem() {
return filesystem;
}
/**
* Sets the PreprocessorListener which handles events for
* this Preprocessor.
*
* The listener is notified of warnings, errors and source
* changes, amongst other things.
*/
public void setListener(PreprocessorListener listener) {
this.listener = listener;
Source s = source;
while (s != null) {
// s.setListener(listener);
s.init(this);
s = s.getParent();
}
}
/**
* Returns the PreprocessorListener which handles events for
* this Preprocessor.
*/
public PreprocessorListener getListener() {
return listener;
}
/**
* Returns the feature-set for this Preprocessor.
*
* This set may be freely modified by user code.
*/
public Feature getFeatures() {
return features;
}
/**
* Adds a feature to the feature-set of this Preprocessor.
*/
public void addFeature(Feature f) {
features |= f;
}
/**
* Adds features to the feature-set of this Preprocessor.
*/
public void addFeatures(Feature f) {
features |= f;
}
/**
* Returns true if the given feature is in
* the feature-set of this Preprocessor.
*/
public bool getFeature(Feature f) {
return (features & f) != Feature.NONE;
}
/**
* Returns the warning-set for this Preprocessor.
*
* This set may be freely modified by user code.
*/
public Warning getWarnings() {
return warnings;
}
/**
* Adds a warning to the warning-set of this Preprocessor.
*/
public void addWarning(Warning w) {
warnings |= w;
}
/**
* Adds warnings to the warning-set of this Preprocessor.
*/
public void addWarnings(Warning w) {
warnings |= w;
}
/**
* Returns true if the given warning is in
* the warning-set of this Preprocessor.
*/
public bool getWarning(Warning w) {
return (warnings & w) != Warning.NONE;
}
/**
* Adds input for the Preprocessor.
*
* Inputs are processed in the order in which they are added.
*/
public void addInput(Source source) {
source.init(this);
inputs.Add(source);
}
/**
* Adds input for the Preprocessor.
*
* @see #addInput(Source)
*/
public void addInput(FileInfo file) {
addInput(new FileLexerSource(file));
}
/**
* Handles an error.
*
* If a PreprocessorListener is installed, it receives the
* error. Otherwise, an exception is thrown.
*/
protected void error(int line, int column, String msg) {
if (listener != null)
listener.handleError(source, line, column, msg);
else
throw new LexerException("Error at " + line + ":" + column + ": " + msg);
}
/**
* Handles an error.
*
* If a PreprocessorListener is installed, it receives the
* error. Otherwise, an exception is thrown.
*
* @see #error(int, int, String)
*/
protected void error(Token tok, String msg) {
error(tok.getLine(), tok.getColumn(), msg);
}
/**
* Handles a warning.
*
* If a PreprocessorListener is installed, it receives the
* warning. Otherwise, an exception is thrown.
*/
protected void warning(int line, int column, String msg) {
if (warnings.HasFlag(Warning.ERROR))
error(line, column, msg);
else if (listener != null)
listener.handleWarning(source, line, column, msg);
else
throw new LexerException("Warning at " + line + ":" + column + ": " + msg);
}
/**
* Handles a warning.
*
* If a PreprocessorListener is installed, it receives the
* warning. Otherwise, an exception is thrown.
*
* @see #warning(int, int, String)
*/
protected void warning(Token tok, String msg) {
warning(tok.getLine(), tok.getColumn(), msg);
}
/**
* Adds a Macro to this Preprocessor.
*
* The given {@link Macro} object encapsulates both the name
* and the expansion.
*/
public void addMacro(Macro m) {
// System.out.println("Macro " + m);
String name = m.getName();
/* Already handled as a source error in macro(). */
if ("defined" == name)
throw new LexerException("Cannot redefine name 'defined'");
macros[m.getName()] = m;
}
/**
* Defines the given name as a macro.
*
* The String value is lexed into a token stream, which is
* used as the macro expansion.
*/
public void addMacro(String name, String value) {
try {
Macro m = new Macro(name);
StringLexerSource s = new StringLexerSource(value);
for (;;) {
Token tok = s.token();
if(tok.getType() == Token.EOF)
break;
m.addToken(tok);
}
addMacro(m);
}
catch (IOException e) {
throw new LexerException(e);
}
}
/**
* Defines the given name as a macro, with the value <code>1</code>.
*
* This is a convnience method, and is equivalent to
* <code>addMacro(name, "1")</code>.
*/
public void addMacro(String name) {
addMacro(name, "1");
}
/**
* Sets the user include path used by this Preprocessor.
*/
/* Note for future: Create an IncludeHandler? */
public void setQuoteIncludePath(List<String> path) {
this.quoteincludepath = path;
}
/**
* Returns the user include-path of this Preprocessor.
*
* This list may be freely modified by user code.
*/
public List<String> getQuoteIncludePath() {
return quoteincludepath;
}
/**
* Sets the system include path used by this Preprocessor.
*/
/* Note for future: Create an IncludeHandler? */
public void setSystemIncludePath(List<String> path) {
this.sysincludepath = path;
}
/**
* Returns the system include-path of this Preprocessor.
*
* This list may be freely modified by user code.
*/
public List<String> getSystemIncludePath() {
return sysincludepath;
}
/**
* Sets the Objective-C frameworks path used by this Preprocessor.
*/
/* Note for future: Create an IncludeHandler? */
public void setFrameworksPath(List<String> path) {
this.frameworkspath = path;
}
/**
* Returns the Objective-C frameworks path used by this
* Preprocessor.
*
* This list may be freely modified by user code.
*/
public List<String> getFrameworksPath() {
return frameworkspath;
}
/**
* Returns the Map of Macros parsed during the run of this
* Preprocessor.
*/
public Dictionary<String,Macro> getMacros() {
return macros;
}
/**
* Returns the named macro.
*
* While you can modify the returned object, unexpected things
* might happen if you do.
*/
public Macro getMacro(String name) {
Macro retval;
macros.TryGetValue(name, out retval);
return retval;
}
/* States */
private void push_state() {
State top = states.Peek();
states.Push(new State(top));
}
private void pop_state() {
State s = states.Pop();
if (states.Count == 0) {
error(0, 0, "#" + "endif without #" + "if");
states.Push(s);
}
}
private bool isActive() {
State state = states.Peek();
return state.isParentActive() && state.isActive();
}
/* Sources */
/**
* Returns the top Source on the input stack.
*
* @see Source
* @see #push_source(Source,bool)
* @see #pop_source()
*/
public Source getSource() {
return source;
}
/**
* Pushes a Source onto the input stack.
*
* @see #getSource()
* @see #pop_source()
*/
protected void push_source(Source source, bool autopop) {
source.init(this);
source.setParent(this.source, autopop);
// source.setListener(listener);
if (listener != null)
listener.handleSourceChange(this.source, "suspend");
this.source = source;
if (listener != null)
listener.handleSourceChange(this.source, "push");
}
/**
* Pops a Source from the input stack.
*
* @see #getSource()
* @see #push_source(Source,bool)
*/
protected void pop_source() {
if (listener != null)
listener.handleSourceChange(this.source, "pop");
Source s = this.source;
this.source = s.getParent();
/* Always a noop unless called externally. */
s.close();
if (listener != null && this.source != null)
listener.handleSourceChange(this.source, "resume");
}
/* Source tokens */
private Token _source_token;
/* XXX Make this include the Token.NL, and make all cpp directives eat
* their own Token.NL. */
private Token line_token(int line, String name, String extra) {
StringBuilder buf = new StringBuilder();
buf.Append("#line ").Append(line)
.Append(" \"");
/* XXX This call to escape(name) is correct but ugly. */
MacroTokenSource.escape(buf, name);
buf.Append("\"");
if (EmitExtraLineInfo)
buf.Append(extra);
buf.Append("\n");
return new Token(Token.P_LINE, line, 0, buf.ToString(), null);
}
private Token source_token() {
if(_source_token != null) {
Token tok = _source_token;
_source_token = null;
if (getFeature(Feature.DEBUG))
System.Console.Error.WriteLine("Returning unget token " + tok);
return tok;
}
for (;;) {
Source s = getSource();
if (s == null) {
if (inputs.Count == 0)
return new Token(Token.EOF);
Source t = inputs[0];
inputs.RemoveAt(0);
push_source(t, true);
if (getFeature(Feature.LINEMARKERS))
return line_token(t.getLine(), t.getName(), " 1");
continue;
}
Token tok = s.token();
/* XXX Refactor with skipline() */
if(tok.getType() == Token.EOF && s.isAutopop()) {
// System.out.println("Autopop " + s);
pop_source();
Source t = getSource();
if (getFeature(Feature.LINEMARKERS)
&& s.isNumbered()
&& t != null) {
/* We actually want 'did the nested source
* contain a newline token', which isNumbered()
* approximates. This is not perfect, but works. */
return line_token(t.getLine() + 1, t.getName(), " 2");
}
continue;
}
if (getFeature(Feature.DEBUG))
System.Console.Error.WriteLine("Returning fresh token " + tok);
return tok;
}
}
private void source_untoken(Token tok) {
if (this._source_token != null)
throw new InvalidOperationException("Cannot return two tokens");
this._source_token = tok;
}
private bool isWhite(Token tok) {
int type = tok.getType();
return (type == Token.WHITESPACE)
|| (type == Token.CCOMMENT)
|| (type == Token.CPPCOMMENT);
}
private Token source_token_nonwhite() {
Token tok;
do {
tok = source_token();
} while (isWhite(tok));
return tok;
}
/**
* Returns an Token.NL or an Token.EOF token.
*
* The metadata on the token will be correct, which is better
* than generating a new one.
*
* This method can, as of recent patches, return a P_LINE token.
*/
private Token source_skipline(bool white) {
// (new Exception("skipping line")).printStackTrace(System.out);
Source s = getSource();
Token tok = s.skipline(white);
/* XXX Refactor with source_token() */
if (tok.getType() == Token.EOF && s.isAutopop()) {
// System.out.println("Autopop " + s);
pop_source();
Source t = getSource();
if (getFeature(Feature.LINEMARKERS)
&& s.isNumbered()
&& t != null) {
/* We actually want 'did the nested source
* contain a newline token', which isNumbered()
* approximates. This is not perfect, but works. */
return line_token(t.getLine() + 1, t.getName(), " 2");
}
}
return tok;
}
/* processes and expands a macro. */
private bool macro(Macro m, Token orig) {
Token tok;
List<Argument> args;
// System.out.println("pp: expanding " + m);
if (m.isFunctionLike()) {
for (;;) {
tok = source_token();
// System.out.println("pp: open: token is " + tok);
switch (tok.getType()) {
case Token.WHITESPACE: /* XXX Really? */
case Token.CCOMMENT:
case Token.CPPCOMMENT:
case Token.NL:
break; /* continue */
case '(':
goto BREAK_OPEN;
default:
source_untoken(tok);
return false;
}
}
BREAK_OPEN:
// tok = expanded_token_nonwhite();
tok = source_token_nonwhite();
/* We either have, or we should have args.
* This deals elegantly with the case that we have
* one empty arg. */
if (tok.getType() != ')' || m.getArgs() > 0) {
args = new List<Argument>();
Argument arg = new Argument();
int depth = 0;
bool space = false;
ARGS: for (;;) {
// System.out.println("pp: arg: token is " + tok);
switch (tok.getType()) {
case Token.EOF:
error(tok, "EOF in macro args");
return false;
case ',':
if (depth == 0) {
if (m.isVariadic() &&
/* We are building the last arg. */
args.Count == m.getArgs() - 1) {
/* Just add the comma. */
arg.addToken(tok);
}
else {
args.Add(arg);
arg = new Argument();
}
}
else {
arg.addToken(tok);
}
space = false;
break;
case ')':
if (depth == 0) {
args.Add(arg);
goto BREAK_ARGS;
}
else {
depth--;
arg.addToken(tok);
}
space = false;
break;
case '(':
depth++;
arg.addToken(tok);
space = false;
break;
case Token.WHITESPACE:
case Token.CCOMMENT:
case Token.CPPCOMMENT:
/* Avoid duplicating spaces. */
space = true;
break;
default:
/* Do not put space on the beginning of
* an argument token. */
if (space && arg.Count != 0)
arg.addToken(Token.space);
arg.addToken(tok);
space = false;
break;
}
// tok = expanded_token();
tok = source_token();
}
BREAK_ARGS:
if(m.isVariadic() && args.Count < m.getArgs()) {
args.Add(new Argument());
}
/* space may still be true here, thus trailing space
* is stripped from arguments. */
if (args.Count != m.getArgs()) {
error(tok,
"macro " + m.getName() +
" has " + m.getArgs() + " parameters " +
"but given " + args.Count + " args");
/* We could replay the arg tokens, but I
* note that GNU cpp does exactly what we do,
* i.e. output the macro name and chew the args.
*/
return false;
}
/*
for (Argument a : args)
a.expand(this);
*/
for (int i = 0; i < args.Count; i++) {
args[i].expand(this);
}
// System.out.println("Macro " + m + " args " + args);
}
else {
/* nargs == 0 and we (correctly) got () */
args = null;
}
}
else {
/* Macro without args. */
args = null;
}
if (m == __LINE__) {
push_source(new FixedTokenSource(
new Token[] { new Token(Token.INTEGER,
orig.getLine(), orig.getColumn(),
orig.getLine().ToString(),
orig.getLine()) }
), true);
}
else if (m == __FILE__) {
StringBuilder buf = new StringBuilder("\"");
String name = getSource().getName();
if (name == null)
name = "<no file>";
for (int i = 0; i < name.Length; i++) {
char c = name[i];
switch (c) {
case '\\':
buf.Append("\\\\");
break;
case '"':
buf.Append("\\\"");
break;
default:
buf.Append(c);
break;
}
}
buf.Append("\"");
String text = buf.ToString();
push_source(new FixedTokenSource(
new Token[] { new Token(Token.STRING,
orig.getLine(), orig.getColumn(),
text, text) }
), true);
}
else if (m == __COUNTER__) {
/* This could equivalently have been done by adding
* a special Macro subclass which overrides getTokens(). */
int value = this.counter++;
push_source(new FixedTokenSource(
new Token[] { new Token(Token.INTEGER,
orig.getLine(), orig.getColumn(),
value.ToString(),
value) }
), true);
}
else {
push_source(new MacroTokenSource(m, args), true);
}
return true;
}
/**
* Expands an argument.
*/
/* I'd rather this were done lazily, but doing so breaks spec. */
internal List<Token> expand(List<Token> arg) {
List<Token> expansion = new List<Token>();
bool space = false;
push_source(new FixedTokenSource(arg), false);
for (;;) {
Token tok = expanded_token();
switch (tok.getType()) {
case Token.EOF:
goto BREAK_EXPANSION;
case Token.WHITESPACE:
case Token.CCOMMENT:
case Token.CPPCOMMENT:
space = true;
break;
default:
if (space && expansion.Count != 0)
expansion.Add(Token.space);
expansion.Add(tok);
space = false;
break;
}
}
BREAK_EXPANSION:
pop_source();
return expansion;
}
/* processes a #define directive */
private Token define() {
Token tok = source_token_nonwhite();
if (tok.getType() != Token.IDENTIFIER) {
error(tok, "Expected Token.IDENTIFIER");
return source_skipline(false);
}
/* if predefined */
String name = tok.getText();
if ("defined" == name) {
error(tok, "Cannot redefine name 'defined'");
return source_skipline(false);
}
Macro m = new Macro(getSource(), name);
List<String> args;
tok = source_token();
if (tok.getType() == '(') {
tok = source_token_nonwhite();
if (tok.getType() != ')') {
args = new List<String>();
for (;;) {
switch (tok.getType()) {
case Token.IDENTIFIER:
if(m.isVariadic()) {
throw new Exception();
}
args.Add(tok.getText());
break;
case Token.ELLIPSIS:
m.setVariadic(true);
args.Add("__VA_ARGS__");
break;
case Token.NL:
case Token.EOF:
error(tok,
"Unterminated macro parameter list");
return tok;
default:
error(tok,
"error in macro parameters: " +
tok.getText());
return source_skipline(false);
}
tok = source_token_nonwhite();
switch (tok.getType()) {
case ',':
break;
case Token.ELLIPSIS:
tok = source_token_nonwhite();
if (tok.getType() != ')')
error(tok,
"ellipsis must be on last argument");
m.setVariadic(true);
goto BREAK_ARGS;
case ')':
goto BREAK_ARGS;
case Token.NL:
case Token.EOF:
/* Do not skip line. */
error(tok,
"Unterminated macro parameters");
return tok;
default:
error(tok,
"Bad token in macro parameters: " +
tok.getText());
return source_skipline(false);
}
tok = source_token_nonwhite();
}
BREAK_ARGS:;
}
else {
System.Diagnostics.Debug.Assert(tok.getType() == ')', "Expected ')'");
args = new List<string>();
}
m.setArgs(args);
}
else {
/* For searching. */
args = new List<string>();
source_untoken(tok);
}
/* Get an expansion for the macro, using IndexOf. */
bool space = false;
bool paste = false;
int idx;
/* Ensure no space at start. */
tok = source_token_nonwhite();
for (;;) {
switch (tok.getType()) {
case Token.EOF:
goto BREAK_EXPANSION;
case Token.NL:
goto BREAK_EXPANSION;
case Token.CCOMMENT:
case Token.CPPCOMMENT:
/* XXX This is where we implement GNU's cpp -CC. */
// break;
case Token.WHITESPACE:
if (!paste)
space = true;
break;
/* Paste. */
case Token.PASTE:
space = false;
paste = true;
m.addPaste(new Token(Token.M_PASTE,
tok.getLine(), tok.getColumn(),
"#" + "#", null));
break;
/* Stringify. */
case '#':
if (space)
m.addToken(Token.space);
space = false;
Token la = source_token_nonwhite();
if(la.getType() == Token.IDENTIFIER &&
((idx = args.IndexOf(la.getText())) != -1)) {
m.addToken(new Token(Token.M_STRING,
la.getLine(), la.getColumn(),
"#" + la.getText(),
idx));
}
else {
m.addToken(tok);
/* Allow for special processing. */
source_untoken(la);
}
break;
case Token.IDENTIFIER:
if (space)
m.addToken(Token.space);
space = false;
paste = false;
idx = args.IndexOf(tok.getText());
if (idx == -1)
m.addToken(tok);