forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppPreprocessor.cxx
More file actions
3127 lines (2707 loc) · 77.9 KB
/
cppPreprocessor.cxx
File metadata and controls
3127 lines (2707 loc) · 77.9 KB
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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file cppPreprocessor.cxx
* @author drose
* @date 1999-10-22
*/
#include "cppPreprocessor.h"
#include "cppExpressionParser.h"
#include "cppExpression.h"
#include "cppScope.h"
#include "cppIdentifier.h"
#include "cppTemplateScope.h"
#include "cppTemplateParameterList.h"
#include "cppClassTemplateParameter.h"
#include "cppConstType.h"
#include "cppFunctionGroup.h"
#include "cppFunctionType.h"
#include "cppPointerType.h"
#include "cppParameterList.h"
#include "cppSimpleType.h"
#include "cppGlobals.h"
#include "cppCommentBlock.h"
#include "cppBison.h"
#include "indent.h"
#include "pstrtod.h"
#include "string_utils.h"
#include <assert.h>
#include <ctype.h>
using std::cerr;
using std::string;
// We manage our own visibility counter, in addition to that managed by
// cppBison.y. We do this just so we can define manifests with the correct
// visibility when they are declared. (Asking the parser for the current
// visibility is prone to error, since the parser might be several tokens
// behind the preprocessor.)
static CPPVisibility preprocessor_vis = V_public;
static int
hex_val(int c) {
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return (c - '0');
default:
return (tolower(c) - 'a' + 10);
}
}
static string
trim_blanks(const string &str) {
size_t first, last;
if(str.empty())
return str;
first = 0;
while (first < str.length() && isspace(str[first])) {
first++;
}
last = str.length() - 1;
while (last > first && isspace(str[last])) {
last--;
}
return str.substr(first, last - first + 1);
}
/**
*
*/
CPPPreprocessor::InputFile::
InputFile() {
_in = nullptr;
_ignore_manifest = nullptr;
_line_number = 0;
_col_number = 0;
_next_line_number = 1;
_next_col_number = 1;
_lock_position = false;
}
/**
*
*/
CPPPreprocessor::InputFile::
~InputFile() {
if (_in != nullptr) {
// For some reason--compiler bug in gcc 3.2?--explicitly deleting the
// stream pointer does not call the appropriate global delete function;
// instead apparently calling the system delete function. So we call the
// delete function by hand instead.
#if !defined(USE_MEMORY_NOWRAPPERS) && defined(REDEFINE_GLOBAL_OPERATOR_NEW)
_in->~istream();
(*global_operator_delete)(_in);
#else
delete _in;
#endif
}
}
/**
*
*/
bool CPPPreprocessor::InputFile::
open(const CPPFile &file) {
assert(_in == nullptr);
_file = file;
pifstream *in = new pifstream;
_in = in;
return _file._filename.open_read(*in);
}
/**
*
*/
bool CPPPreprocessor::InputFile::
connect_input(const string &input) {
assert(_in == nullptr);
_input = input;
_in = new std::istringstream(_input);
return !_in->fail();
}
/**
* Fetches a single character from the source file.
*/
int CPPPreprocessor::InputFile::
get() {
assert(_in != nullptr);
if (!_lock_position) {
_line_number = _next_line_number;
_col_number = _next_col_number;
}
int c = _in->get();
// Quietly skip over embedded carriage-return characters. We shouldn't see
// any of these unless there was some DOS-to-Unix file conversion problem.
while (c == '\r') {
c = _in->get();
}
switch (c) {
case EOF:
break;
case '\n':
if (!_lock_position) {
++_next_line_number;
_next_col_number = 1;
}
break;
default:
if (!_lock_position) {
++_next_col_number;
}
}
return c;
}
/**
* Like get(), but does not advance the file pointer.
*/
int CPPPreprocessor::InputFile::
peek() {
assert(_in != nullptr);
int c = _in->peek();
// Quietly skip over embedded carriage-return characters. We shouldn't see
// any of these unless there was some DOS-to-Unix file conversion problem.
while (c == '\r') {
_in->get();
c = _in->peek();
}
return c;
}
/**
*
*/
CPPPreprocessor::
CPPPreprocessor() {
_noangles = false;
_state = S_eof;
_paren_nesting = 0;
_parsing_template_params = false;
_parsing_attribute = false;
_unget = '\0';
_last_c = '\0';
_start_of_line = true;
_last_cpp_comment = false;
_save_comments = true;
_resolve_identifiers = true;
_warning_count = 0;
_error_count = 0;
_error_abort = false;
#ifdef CPP_VERBOSE_LEX
_token_index = 0;
#endif
_verbose = 1;
}
/**
* Sets the verbosity level of the parser. At 0, no warnings will be
* reported; at 1 or higher, expect to get spammed.
*/
void CPPPreprocessor::
set_verbose(int verbose) {
_verbose = verbose;
}
/**
* Returns the verbosity level of the parser.
*/
int CPPPreprocessor::
get_verbose() const {
return _verbose;
}
/**
*
*/
void CPPPreprocessor::
copy_filepos(const CPPPreprocessor &other) {
assert(!_files.empty());
_files.back()._file = other.get_file();
_files.back()._line_number = other.get_line_number();
_files.back()._col_number = other.get_col_number();
}
/**
*
*/
CPPFile CPPPreprocessor::
get_file() const {
if (_files.empty()) {
return CPPFile("");
}
return _files.back()._file;
}
/**
* Returns the line number of the last character returned by get().
*/
int CPPPreprocessor::
get_line_number() const {
if (_files.empty()) {
return 0;
}
return _files.back()._line_number;
}
/**
* Returns the column number of the last character returned by get().
*/
int CPPPreprocessor::
get_col_number() const {
if (_files.empty()) {
return 0;
}
return _files.back()._col_number;
}
/**
*
*/
CPPToken CPPPreprocessor::
get_next_token() {
#ifdef CPP_VERBOSE_LEX
CPPToken tok = get_next_token0();
indent(cerr, _files.size() * 2)
<< _token_index++ << ". " << tok << "\n";
return tok;
}
CPPToken CPPPreprocessor::
get_next_token0() {
#endif
// We make a nested call to internal_get_next_token(), so we can combine
// sequences of identifiers and scoping symbols into a single identifier,
// for yacc's convenience.
CPPToken token(0);
if (!_saved_tokens.empty()) {
token = _saved_tokens.back();
_saved_tokens.pop_back();
} else {
token = internal_get_next_token();
}
YYLTYPE loc = token._lloc;
if (_resolve_identifiers &&
(token._token == SIMPLE_IDENTIFIER || token._token == SCOPE)) {
// We will be returning a scoped identifier, or a scoping. Keep pulling
// off tokens until we reach the end of the scopeidentifier sequence.
string name;
// If we started the ball with an identifier, use it and get the next
// token. Otherwise, we started with :: (global scope), and we indicate
// this with an empty string at the beginning of the scoping sequence.
if (token._token == SIMPLE_IDENTIFIER) {
name = token._lval.str;
token = internal_get_next_token();
}
CPPIdentifier *ident = new CPPIdentifier(name, loc);
YYSTYPE result;
result.u.identifier = ident;
if (token._token == '<') {
// If the next token is an angle bracket and the current identifier
// wants template instantiation, assume the angle bracket begins the
// instantiation and call yacc recursively to parse the template
// parameters.
CPPDeclaration *decl = ident->find_template(current_scope, global_scope);
if (decl != nullptr) {
ident->_names.back().set_templ
(nested_parse_template_instantiation(decl->get_template_scope()));
token = internal_get_next_token();
} else {
error(string("unknown template '") + ident->get_fully_scoped_name() + "'", loc);
}
}
while (token._token == SCOPE) {
loc.last_line = token._lloc.last_line;
loc.last_column = token._lloc.last_column;
name += "::";
token = internal_get_next_token();
string token_prefix;
if (token._token == '~') {
// A scoping operator followed by a tilde can only be the start of a
// scoped destructor name. Make the tilde be part of the name.
name += "~";
token_prefix = "~";
token = internal_get_next_token();
}
if (token._token != SIMPLE_IDENTIFIER) {
// The last useful token was a SCOPE, thus this is a scoping token.
if (token._token == KW_OPERATOR) {
// Unless the last token we came across was the "operator" keyword.
// We make a special case for this, because it's occasionally scoped
// in normal use.
token._lval = result;
_last_token_loc = token._lloc;
return token;
}
_saved_tokens.push_back(token);
_last_token_loc = loc;
return CPPToken(SCOPING, loc, name, result);
}
name += token._lval.str;
ident->_names.push_back(token_prefix + token._lval.str);
loc.last_line = token._lloc.last_line;
loc.last_column = token._lloc.last_column;
ident->_loc.last_line = loc.last_line;
ident->_loc.last_column = loc.last_column;
token = internal_get_next_token();
if (token._token == '<') {
// If the next token is an angle bracket and the current indentifier
// wants template instantiation, assume the angle bracket begins the
// instantiation and call yacc recursively to parse the template
// parameters.
CPPDeclaration *decl =
ident->find_template(current_scope, global_scope);
if (decl != nullptr) {
ident->_names.back().set_templ
(nested_parse_template_instantiation(decl->get_template_scope()));
token = internal_get_next_token();
} else {
error(string("unknown template '") + ident->get_fully_scoped_name() + "'", loc);
}
}
}
// The last useful token was a SIMPLE_IDENTIFIER, thus this is a normal
// scoped identifier.
_saved_tokens.push_back(token);
int token_type = IDENTIFIER;
CPPDeclaration *decl = ident->find_symbol(current_scope, global_scope);
if (decl != nullptr && decl->as_type() != nullptr) {
// We need to see type pack template parameters as a different type of
// identifier to resolve a parser ambiguity.
CPPClassTemplateParameter *ctp = decl->as_class_template_parameter();
if (ctp && ctp->_packed) {
token_type = TYPEPACK_IDENTIFIER;
} else {
token_type = TYPENAME_IDENTIFIER;
}
}
_last_token_loc = loc;
return CPPToken(token_type, loc, name, result);
}
// This is the normal case: just pass through whatever token we got.
_last_token_loc = loc;
return token;
}
/**
*
*/
CPPToken CPPPreprocessor::
peek_next_token() {
CPPToken token(0);
if (!_saved_tokens.empty()) {
token = _saved_tokens.back();
} else {
token = internal_get_next_token();
_saved_tokens.push_back(token);
}
return token;
}
/**
*
*/
void CPPPreprocessor::
warning(const string &message) {
if (_verbose < 2) {
return;
}
int line = get_line_number();
int col = get_col_number();
YYLTYPE loc;
loc.first_line = line;
loc.first_column = col;
loc.last_line = line;
loc.last_column = col;
loc.file = get_file();
warning(message, loc);
}
/**
*
*/
void CPPPreprocessor::
warning(const string &message, const YYLTYPE &loc) {
if (_verbose >= 2) {
if (_verbose >= 3) {
indent(cerr, _files.size() * 2);
}
if (!loc.file.empty()) {
cerr << loc.file << ':';
}
if (loc.first_line) {
cerr << loc.first_line << ':';
if (loc.first_column) {
cerr << loc.first_column << ':';
}
}
cerr << " warning: " << message << "\n";
show_line(loc);
}
_warning_count++;
}
/**
*
*/
void CPPPreprocessor::
error(const string &message) {
int line = get_line_number();
int col = get_col_number();
YYLTYPE loc;
loc.first_line = line;
loc.first_column = col;
loc.last_line = line;
loc.last_column = col;
loc.file = get_file();
error(message, loc);
}
/**
*
*/
void CPPPreprocessor::
error(const string &message, const YYLTYPE &loc) {
if (_state == S_nested || _state == S_end_nested) {
// Don't report or log errors in the nested state. These will be reported
// when the nesting level collapses.
return;
}
if (_verbose >= 1) {
if (_verbose >= 3) {
indent(cerr, _files.size() * 2);
}
if (!loc.file.empty()) {
cerr << loc.file << ':';
}
if (loc.first_line) {
cerr << loc.first_line << ':';
if (loc.first_column) {
cerr << loc.first_column << ':';
}
}
cerr << " error: " << message << "\n";
show_line(loc);
if (_error_abort) {
cerr << "Aborting.\n";
abort();
}
}
_error_count++;
}
/**
* Shows the indicated line, useful for error messages.
*/
void CPPPreprocessor::
show_line(const YYLTYPE &loc) {
if (loc.file._filename.empty()) {
return;
}
int indent_level = 0;
if (_verbose >= 3) {
indent_level = _files.size() * 2;
}
// Seek to the offending line in the file.
std::ifstream stream;
if (loc.file._filename.open_read(stream)) {
int l = 0;
string linestr;
while (l < loc.first_line) {
std::getline(stream, linestr);
++l;
}
// Strip off trailing whitespace.
size_t last = linestr.length();
while (isspace(linestr[--last])) {
linestr = linestr.substr(0, last);
}
indent(cerr, indent_level) << linestr << "\n";
// Point the user at the offending column.
if (loc.first_column) {
int last_column;
if (loc.first_line == loc.last_line && loc.last_column) {
last_column = loc.last_column;
} else {
last_column = linestr.length();
}
indent(cerr, indent_level);
int i = 0;
for (; i < loc.first_column - 1; ++i) {
cerr.put(' ');
}
cerr.put('^');
while (++i < last_column) {
cerr.put('~');
}
cerr << "\n";
}
}
}
/**
*
*/
int CPPPreprocessor::
get_warning_count() const {
return _warning_count;
}
/**
*
*/
int CPPPreprocessor::
get_error_count() const {
return _error_count;
}
/**
* Returns the CPPCommentBlock immediately preceding the indicated line, if
* any. If there is no such comment, returns NULL.
*/
CPPCommentBlock *CPPPreprocessor::
get_comment_before(int line, CPPFile file) {
CPPComments::reverse_iterator ci;
ci = _comments.rbegin();
int wrong_file_count = 0;
while (ci != _comments.rend()) {
CPPCommentBlock *comment = (*ci);
if (comment->_file == file) {
wrong_file_count = 0;
if (comment->_last_line == line || comment->_last_line == line - 1) {
return comment;
}
if (comment->_last_line < line) {
return nullptr;
}
} else {
wrong_file_count++;
if (wrong_file_count > 10) {
return nullptr;
}
}
++ci;
}
return nullptr;
}
/**
* Returns the CPPCommentBlock that starts on the indicated line, if any. If
* there is no such comment, returns NULL.
*/
CPPCommentBlock *CPPPreprocessor::
get_comment_on(int line, CPPFile file) {
CPPComments::reverse_iterator ci;
ci = _comments.rbegin();
while (ci != _comments.rend()) {
CPPCommentBlock *comment = (*ci);
if (comment->_file == file) {
if (comment->_line_number == line) {
return comment;
} else if (comment->_line_number < line) {
return nullptr;
}
}
++ci;
}
return nullptr;
}
/**
*
*/
bool CPPPreprocessor::
init_cpp(const CPPFile &file) {
_state = S_normal;
_saved_tokens.push_back(CPPToken(START_CPP));
_last_c = '\0';
return push_file(file);
}
/**
*
*/
bool CPPPreprocessor::
init_const_expr(const string &expr) {
_state = S_normal;
_saved_tokens.push_back(CPPToken(START_CONST_EXPR));
return push_string(expr, false);
}
/**
*
*/
bool CPPPreprocessor::
init_type(const string &type) {
_state = S_normal;
_saved_tokens.push_back(CPPToken(START_TYPE));
return push_string(type, false);
}
/**
*
*/
bool CPPPreprocessor::
push_file(const CPPFile &file) {
if (_verbose >= 3) {
indent(cerr, _files.size() * 2)
<< "Reading " << file << "\n";
}
assert(_last_c == 0);
_files.push_back(InputFile());
InputFile &infile = _files.back();
if (infile.open(file)) {
// Record the fact that we opened the file for the benefit of user code.
_parsed_files.insert(file);
infile._prev_last_c = _last_c;
_last_c = '\0';
_start_of_line = true;
return true;
}
_files.pop_back();
return false;
}
/**
*
*/
bool CPPPreprocessor::
push_string(const string &input, bool lock_position) {
#ifdef CPP_VERBOSE_LEX
indent(cerr, _files.size() * 2)
<< "Pushing to string \"" << input
<< "\"\nlock_position = " << lock_position << "\n";
#endif
CPPFile first_file = get_file();
int first_line = get_line_number();
int first_col = get_col_number();
_files.push_back(InputFile());
InputFile &infile = _files.back();
if (infile.connect_input(input)) {
if (lock_position) {
infile._file = first_file;
infile._line_number = first_line;
infile._col_number = first_col;
infile._lock_position = true;
}
infile._prev_last_c = _last_c;
_last_c = '\0';
return true;
}
#ifdef CPP_VERBOSE_LEX
indent(cerr, _files.size() * 2)
<< "Unable to read string\n";
#endif
_files.pop_back();
return false;
}
/**
* Given a string, expand all manifests within the string and return the new
* string.
*/
string CPPPreprocessor::
expand_manifests(const string &input_expr, bool expand_undefined,
const YYLTYPE &loc) {
// Get a copy of the expression string we can modify.
string expr = input_expr;
// Repeatedly scan the expr for any manifest names or defined() function.
bool manifest_found;
do {
manifest_found = false;
size_t p = 0;
while (p < expr.size()) {
if (isalpha(expr[p]) || expr[p] == '_') {
size_t q = p;
while (p < expr.size() && (isalnum(expr[p]) || expr[p] == '_')) {
p++;
}
string ident = expr.substr(q, p - q);
// Here's an identifier. Is it "defined"?
if (ident == "defined") {
expand_defined_function(expr, q, p);
} else if (expand_undefined && ident == "__has_include") {
expand_has_include_function(expr, q, p, loc);
} else {
// Is it a manifest?
Manifests::const_iterator mi = _manifests.find(ident);
if (mi != _manifests.end()) {
const CPPManifest *manifest = (*mi).second;
expand_manifest_inline(expr, q, p, manifest);
manifest_found = true;
} else if (ident == "__FILE__") {
// Special case: this is a dynamic definition.
string file = string("\"") + loc.file._filename_as_referenced.get_fullpath() + "\"";
expr = expr.substr(0, q) + file + expr.substr(p);
p = q + file.size();
manifest_found = true;
} else if (ident == "__LINE__") {
// So is this.
string line = format_string(loc.first_line);
expr = expr.substr(0, q) + line + expr.substr(p);
p = q + line.size();
manifest_found = true;
} else if (expand_undefined && ident != "true" && ident != "false") {
// It is not found. Expand it to 0, but only if we are currently
// parsing an #if expression.
expr = expr.substr(0, q) + "0" + expr.substr(p);
p = q + 1;
}
}
} else if (expr[p] == '\'' || expr[p] == '"') {
// Skip the next part until we find a closing quotation mark.
char quote = expr[p];
p++;
while (p < expr.size() && expr[p] != quote) {
if (expr[p] == '\\') {
// This might be an escaped quote. Skip an extra char.
p++;
}
p++;
}
if (p >= expr.size()) {
// Unclosed string.
warning("missing terminating " + string(1, quote) + " character", loc);
}
p++;
} else {
p++;
}
}
// If we expanded any manifests at all that time, then go back through the
// string and look again--we might have a manifest that expands to another
// manifest.
} while (manifest_found);
return expr;
}
/**
* Given a string, expand all manifests within the string and evaluate it as
* an expression. Returns NULL if the string is not a valid expression.
*
* This is an internal support function for CPPPreprocessor; however, there is
* a public variant of this function defined for CPPParser.
*/
CPPExpression *CPPPreprocessor::
parse_expr(const string &input_expr, CPPScope *current_scope,
CPPScope *global_scope, const YYLTYPE &loc) {
string expr = expand_manifests(input_expr, false, loc);
CPPExpressionParser ep(current_scope, global_scope);
ep._verbose = 0;
if (ep.parse_expr(expr, *this)) {
return ep._expr;
} else {
return nullptr;
}
}
/**
*
*/
CPPToken CPPPreprocessor::
internal_get_next_token() {
if (_state == S_eof || _state == S_end_nested) {
return CPPToken::eof();
}
int c = _last_c;
_last_c = '\0';
if (c == '\0' || c == EOF) {
c = get();
}
// Skip any whitespace, comments, and preprocessor directives before the
// token.
c = skip_whitespace(c);
while (c == '#' && _start_of_line && !should_ignore_preprocessor()) {
c = skip_whitespace(process_directive(c));
}
if (c == '\'') {
return get_quoted_char(c);
} else if (c == '"') {
return get_quoted_string(c);
} else if (isalpha(c) || c == '_') {
return get_identifier(c);
} else if (isdigit(c)) {
return get_number(c);
}
if (c == EOF) {
_state = S_eof;
return CPPToken::eof();
}
// Check for a number beginning with a decimal point.
int next_c = peek();
if (c == '.' && isdigit(next_c)) {
return get_number(c);
}
YYLTYPE loc;
loc.file = get_file();
loc.first_line = get_line_number();
loc.first_column = get_col_number();
loc.last_line = loc.first_line;
loc.last_column = loc.first_column;
// Check for two- or three-character tokens.
int di = check_digraph(c);
if (di != 0) {
c = di;
++loc.last_column;
get();
int tri = check_trigraph(di);
if (tri != 0) {
++loc.last_column;
get();
return CPPToken(tri, loc);
}
return CPPToken(di, loc);
}
if (_state == S_nested) {
// If we're running a nested lexer, keep track of the paren levels. When
// we encounter a comma or closing angle bracket at the bottom level, we
// stop.
switch (c) {
case '(':
case '[':
_paren_nesting++;
break;
case ')':
case ']':
_paren_nesting--;
break;
case ',':
if (_paren_nesting <= 0) {
_state = S_end_nested;
return CPPToken(0, loc);
}
break;
case '>':
if (_paren_nesting <= 0) {
_parsing_template_params = false;
_state = S_end_nested;
return CPPToken(0, loc);
}
}
} else if (_parsing_attribute) {
// If we're parsing an attribute, also keep track of the paren nesting.
if (c == '[' || c == '(') {
++_paren_nesting;
} else if (c == ']' || c == ')') {
--_paren_nesting;
}
}