forked from hiraditya/cpp2cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RlParser.cpp
808 lines (771 loc) · 22.6 KB
/
RlParser.cpp
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
/**
cpp2cxx is an open source software distributed under terms of the
Apache2.0 licence.
Copyrights remain with the original copyright holders.
Use of this material is by permission and/or license.
Copyright [2012] Aditya Kumar, Andrew Sutton, Bjarne Stroustrup
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.
*/
#include "RlParser.h"
#include "ReplacementList.h"
#include "ExceptionHandler.h"
#include "DemacroficationScheme.h"
#include <algorithm>
#include <sstream>
#if defined(DEBUG_RLPARSER) || defined(DEBUG_RLPARSER2)
#include <iostream>
#endif
RlParser::RlParser(DemacroficationScheme const& demacrofication_scheme,
std::ostream& log_file)
:pDemacroficationScheme(&demacrofication_scheme),
logFile(log_file)
{
}
void RlParser::Parse(ReplacementList& rl)
{
//resetting the categories
//rl_ccat = RlCCat::open;
rl_ccat = RlCCat::closed;
rl_dcat = RlDCat::independent;
rl_idlist.clear();
funArgId.clear();
rl_str_formatted.clear();
rl_ttype.Reset();
auto replacement_list_temp = rl.rl_tokens;
//bool rl_modified = false;
//auto rl_term_tok_id = boost::wave::token_id(replacement_list_temp.back());
//auto rl_term_tok_val = replacement_list_temp.back().get_value();
//std::vector<token_type>::iterator beg = rl.rl_tokens.begin();
//std::vector<token_type>::iterator
if(replacement_list_temp.back() == boost::wave::T_CPPCOMMENT){
//std::cout<<"Last token is a cpp comment";
replacement_list_temp.back().set_token_id(boost::wave::T_NEWLINE);
replacement_list_temp.back().set_value("\n");
//rl_modified = true;
//replacement_list_temp.push_back(token_type(boost::wave::T_NEWLINE));
}
//end = rl.rl_tokens.end();
std::vector<token_type>::iterator beg = replacement_list_temp.begin();
end = replacement_list_temp.end();
try {
funArgId = rl.funArgId;
switch(rl.rl_tokens.size()) {
case 0://some problem while parsing the replacement_list
throw ExceptionHandler(" -note: error while parsing the replacement list");
case 1://only one token means that is newline
rl.rl_ccat = RlCCat::closed;
rl.rl_dcat = RlDCat::independent;
break;
default://more than one token required for parsing the replacement list
Parser(beg,end);
//need to set these values by the parser
//change these rvalues to pointers to save space and time
rl.rl_ccat = rl_ccat;
rl.rl_dcat = rl_dcat;
rl.rl_ttype = rl_ttype;
rl.rl_idlist = rl_idlist;
rl.rl_str_formatted = rl_str_formatted;
break;
}
} catch(ExceptionHandler& e) {
//set the reject type and the demacrofier will take care of it
rl_ccat = RlCCat::open;
rl_ttype.reject_type = true;
logFile<<e.GetMessage()<<"\n";
}
/*if the replacement text was modified?
if(rl_modified){
replacement_list_temp.back().set_token_id(rl_term_tok_id);
replacement_list_temp.back().set_value(rl_term_tok_val);
}*/
}
void RlParser::Parser(std::vector<token_type>::iterator beg,
std::vector<token_type>::iterator term)
{
//boost::wave::token_id id;
it = beg;
end = term;
#ifdef DEBUG_RLPARSER
std::vector<token_type>::iterator temp_it;
temp_it = beg;
std::cout<<"\nInside RL Parser\n";
while(temp_it!=end) {
std::cout<<temp_it->get_value();
temp_it++;
}
std::cout<<"\nDone printing RL Parser\n";
//it = beg;
#endif
ExpressionStatement();
//do not change the order, first CPP comments should be checked
//see the Match function for why...
/* if(Match(boost::wave::T_CPPCOMMENT) || Match(boost::wave::T_NEWLINE)) {
#ifdef DEBUG_RLPARSER
std::cout<<"PARSER: RlCCat::closed\n";
#endif
rl_ccat = RlCCat::closed;
}
*/
//std::cout<<"Out of RL Parser\n";
//if no error then complete
//std::cout<<"expression matched successfully\n";
}
bool RlParser::Match(boost::wave::token_id id)
{
boost::wave::token_id next_id;
std::stringstream id_value;
next_id = *it;
//while ((next_id = *it) == boost::wave::T_SPACE)
// it++;
id_value << it->get_value();
if(id == next_id) {
#ifdef DEBUG_RLPARSER
std::cout<<"\t\tSymbol Matched: "<<id_value.str();
#endif
if(id == boost::wave::T_NEWLINE || id == boost::wave::T_CPPCOMMENT) {
return true;
}
else {
rl_str_formatted += it->get_value().c_str();
FillFormattedRL(*it);
++it;//increment to get to the next token
}
//condStmt.push_back(*it);
id_value.str(std::string());
/// @todo all the space is already consumed so no need of this,
/// but check it first
while (*it == boost::wave::T_SPACE || *it == boost::wave::T_CCOMMENT) {
rl_str_formatted += it->get_value().c_str();
++it;
}
id_value << it->get_value();
//std::cout<<"\nNext_id: "<<id_value.str();
return true;
}
else {
//it--;
//std::cout<<"\nNOMATCH::Expected: "<<id_value.str();
//if there is mismatch the replacement_list is assumed to be open
/// @todo decide open/close carefully
//if(id != boost::wave::T_CPPCOMMENT || id != boost::wave::T_SEMICOLON) {
if(id != boost::wave::T_SEMICOLON) {
rl_ccat = RlCCat::open;
/// @brief also writes a log when there is an unmatching semicolon in a macro.
logFile<<" - note: PARSER_ERROR: RlCCat::open \'"<<id_value.str()<<"\'\n";
#ifdef DEBUG_RLPARSER
std::cout<<"\nPARSER: RlCCat::open \'"<< id_value.str()<<"\'\n";
#endif
}
//throw "Invalid expression";
return false;
}
}
void RlParser::ExpressionStatement()
{
/// expression-statement:
/// expression_opt ;
using namespace boost::wave;
Assignment();
boost::wave::token_id id = boost::wave::token_id(*it);
#ifdef DEBUG_RLPARSER
std::stringstream id_value;
id_value << it->get_value();
std::cout << "\nin ExpressionStatement: "<<id_value.str();
#endif
while(id == T_SEMICOLON) {
Match(id);
Assignment();
id = *it;
rl_ttype.statement_type = true;
}
}
void RlParser::Assignment()
{
using namespace boost::wave;
Expression();
//assignment-- function like only
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value << it->get_value();
#ifdef DEBUG_RLPARSER
std::cout << "\nin Assignment: "<<id_value.str();
#endif
switch(id) {
//comma has the lowest priority
case T_COMMA:
Match(id);
Assignment();
break;
case T_ASSIGN://=
case T_ANDASSIGN://&=
case T_ANDASSIGN_ALT:// and_eq
case T_DIVIDEASSIGN:// /=
case T_MINUSASSIGN:// -=
case T_STARASSIGN:// *=
case T_PERCENTASSIGN:
case T_PLUSASSIGN:
case T_ORASSIGN:
case T_ORASSIGN_ALT:// or_eq
case T_ORASSIGN_TRIGRAPH:
case T_XOR_TRIGRAPH:
case T_XORASSIGN:
case T_XORASSIGN_ALT:
case T_XORASSIGN_TRIGRAPH:
case T_SHIFTLEFTASSIGN: // <<=
case T_SHIFTRIGHTASSIGN:// >>=
Match(id);
Expression();
rl_ttype.assignment_type = true;
break;
default:
break;
}
}
void RlParser::Expression()
{
using namespace boost::wave;
//bool expr_valid = false;
Expression1();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value << it->get_value();
#ifdef DEBUG_RLPARSER
std::cout << "\nin Expression: "<<id_value.str();
#endif
//although the comma has lower priority than the assignment but
//it has been kept here to facilitate simple parsing
while(id == T_AND || id == T_XOR || id == T_OR || id == T_ANDAND ||
id == T_OROR || id == T_OROR_ALT) {
Match(id);
Expression1();
id = *it;
rl_ttype.binary_operator_type = true;
}
}
void RlParser::Expression1()
{
using namespace boost::wave;
Expression2();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value << it->get_value();
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression1: "<<id_value.str();
#endif
while(id == T_EQUAL || id == T_NOTEQUAL || id == T_NOTEQUAL_ALT ||
id == T_LESS || id == T_LESSEQUAL || id == T_GREATER ||
id == T_GREATEREQUAL) {
Match(id);
Expression2();
id = *it;
rl_ttype.relational_operator_type = true;
}
}
void RlParser::Expression2()
{
using namespace boost::wave;
Expression3();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value << it->get_value();
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression2: "<<id_value.str();
#endif
while(id == T_SHIFTLEFT || id == T_SHIFTRIGHT) {
Match(id);
Expression3();
id = *it;
rl_ttype.binary_operator_type = true;
}
}
void RlParser::Expression3()
{
using namespace boost::wave;
Expression4();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value << it->get_value();
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression3: "<<id_value.str();
#endif
while(id == T_PLUS || id == T_MINUS) {
Match(id);
Expression4();
id = *it;
rl_ttype.binary_operator_type = true;
}
}
void RlParser::Expression4()
{
using namespace boost::wave;
Expression5();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value << it->get_value();
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression4: "<<id_value.str();
#endif
while(id == T_STAR || id == T_DIVIDE || id == T_PERCENT) {
Match(id);
Expression5();
id = *it;
rl_ttype.binary_operator_type = true;
}
}
void RlParser::Expression5()
{
using namespace boost::wave;
Expression6();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression5: "<<id_value.str();
#endif
while(id == T_DOTSTAR || id == T_ARROWSTAR) {
Match(id);
Expression6();
id = *it;
rl_ttype.binary_operator_type = true;
}
}
void RlParser::Expression6()
{
using namespace boost::wave;
Expression7();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression6: "<<id_value.str();
#endif
while(id == T_STAR || id == T_DIVIDE || id == T_PERCENT) {
Match(id);
Expression7();
id = *it;
rl_ttype.binary_operator_type = true;
}
}
void RlParser::Expression7()
{
using namespace boost::wave;
Expression8();
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression7: "<<id_value.str();
#endif
if(id == T_POUND_POUND || id == T_POUND_POUND_ALT ||
id == T_POUND_POUND_TRIGRAPH) {
rl_ccat = RlCCat::open;
throw ExceptionHandler(*it, "'##' operator");
}
}
void RlParser::Expression8()
{
using namespace boost::wave;
int brace_count = 0;
std::stringstream id_value;
boost::wave::token_id id = boost::wave::token_id(*it);
id_value<<it->get_value();
#ifdef DEBUG_RLPARSER
std::cout<<"\nin Expression8: "<<id_value.str()<<"\n";
#endif
switch(id) {
case T_POUND:
case T_POUND_ALT:
case T_POUND_TRIGRAPH:
rl_ccat = RlCCat::open;
throw ExceptionHandler(" - note: '#' operator");
//prefix operators
case T_MINUS:
case T_PLUS:
case T_NOT:
case T_NOT_ALT:
case T_COMPL:
case T_MINUSMINUS:
case T_PLUSPLUS:
// then new operator then new[], delete, and delete [] operators
Match(id);
Expression();
rl_ttype.unary_operator_type = true;
break;
case T_IDENTIFIER:
/// @todo wrong classification for function call(with args) inside
/// a function like macro
rl_idlist.insert(*it);
#ifdef DEBUG_RLPARSER
std::cout<<"\nIn Expression8 Expected: "<<id_value.str();
#endif
//identifier might be defined somewhere
//if parenthesis follows => function
//if square brackets => subscript
//post increment operator
rl_dcat = RlDCat::dependent;
std::for_each(funArgId.begin(),funArgId.end(),
[this](token_type tok) {
if(*this->it == tok)
this->rl_dcat = RlDCat::independent;
});
Match(T_IDENTIFIER);
rl_ttype.reject_type = IsRejectPredefinedMacro(id_value.str());
//this is the id of the next token since Match
//fetches the next token after a Match
id = boost::wave::token_id(*it);
if(id == T_LEFTBRACKET) {
Match(T_LEFTBRACKET);
Expression();
#ifdef DEBUG_RLPARSER
id_value.str(std::string());
id_value<<it->get_value();
std::cout<<"\nMatching right bracket: "<<id_value.str();
#endif
Match(T_RIGHTBRACKET);
}
else if(id == T_PLUSPLUS || id == T_MINUSMINUS) {
//postfix operator okay
Match(id);
}
else if(id == T_LEFTPAREN) { //function --kindof simplification
Match(id);
Assignment();
#ifdef DEBUG_RLPARSER
id_value.str(std::string());
id_value<<it->get_value();
std::cout<<"\nMatching right paren of function: "<<id_value.str();
#endif
Match(T_RIGHTPAREN);
//look if it is followed by parenthesis
//so that function
}
rl_ttype.identifier_type = true;
break;
case T_OCTALINT:
case T_DECIMALINT:
case T_HEXAINT:
case T_INTLIT:
#ifdef DEBUG_RLPARSER
std::cout<<"\nExpecting Num literal_type: "<<id_value.str()<<"\n";
#endif
Match(id);
rl_ttype.literal_type = true;
break;
case T_LONGINTLIT:
case T_FLOATLIT:
case T_FIXEDPOINTLIT: // IDL specific
Match(id);
//to bypass <lit>e<+/-><lit><id>
if(Match(T_IDENTIFIER));
rl_ttype.literal_type = true;
break;
case T_CHARLIT:
case T_STRINGLIT:
Match(id);
rl_ttype.literal_type = true;
break;
case T_LEFTPAREN:
#ifdef DEBUG_RLPARSER
std::cout<<"\nMatch Left Paren";
#endif
Match(T_LEFTPAREN);
Expression();
#ifdef DEBUG_RLPARSER
std::cout<<"\nMatch Right Paren";
#endif
Match(T_RIGHTPAREN);
rl_ttype.paren_type = true;
break;
//reject keyword_type
case T_ASM:
case T_AUTO:
case T_BOOL:
case T_FALSE:
case T_TRUE:
case T_BREAK:
case T_CASE:
case T_CATCH:
case T_CHAR:
case T_CLASS:
case T_CONST:
case T_CONSTCAST:
case T_CONTINUE:
case T_DEFAULT:
case T_DO:
case T_DOUBLE:
case T_ELSE:
case T_ENUM:
case T_EXPLICIT:
case T_EXPORT:
case T_EXTERN:
case T_FLOAT:
case T_FOR:
case T_FRIEND:
case T_IF:
case T_INLINE:
case T_INT:
case T_LONG:
case T_MUTABLE:
case T_NAMESPACE:
case T_OPERATOR:
case T_PRIVATE:
case T_PROTECTED:
case T_PUBLIC:
case T_REGISTER:
case T_SHORT:
case T_SIGNED:
///@todo left for now to be looked into later
///how to handle macros like #define _size_type_ unsigned int
case T_NEW:
case T_DELETE:
case T_DYNAMICCAST:
case T_REINTERPRETCAST:
case T_STATICCAST:
case T_SIZEOF:
// left paren then expr then right paren
case T_STATIC:
case T_STRUCT:
case T_SWITCH:
case T_TEMPLATE:
case T_THIS:
case T_THROW:
case T_TRY:
case T_TYPEDEF:
case T_TYPEID:
case T_TYPENAME:
case T_UNION:
case T_UNSIGNED:
case T_USING:
case T_VIRTUAL:
case T_VOID:
case T_VOLATILE:
case T_WCHART:
case T_WHILE:
case T_PP_DEFINE:
case T_PP_IF:
case T_PP_IFDEF:
case T_PP_IFNDEF:
case T_PP_ELSE:
case T_PP_ELIF:
case T_PP_ENDIF:
case T_PP_ERROR:
case T_PP_LINE:
case T_PP_PRAGMA:
case T_PP_UNDEF:
case T_PP_WARNING:
case T_PP_INCLUDE:
case T_PP_QHEADER:
case T_PP_HHEADER:
case T_PP_INCLUDE_NEXT:
case T_PP_QHEADER_NEXT:
case T_PP_HHEADER_NEXT:
rl_ttype.keyword_type = true;
Match(id);
Expression();
break;
case T_RETURN:
case T_GOTO:
/// @brief macro body with return definitely will be used for code generation.
//rl_ttype.reject_type = true;
rl_ttype.keyword_type = true;
id_value.str(std::string());
id_value<<" - note: found "<<it->get_value()<<", not demacrofying\n";
throw ExceptionHandler(*it, id_value.str());
//Match(id);
//Expression();
break;
// //array dereference
// case T_LEFTBRACKET:
// case T_LEFTBRACKET_ALT:
// case T_LEFTBRACKET_TRIGRAPH:
// Match(id);
// Expression();
// Match(T_RIGHTBRACKET);
//case T_RIGHTBRACKET:
//case T_RIGHTBRACKET_ALT:
//case T_RIGHTBRACKET_TRIGRAPH:
//function like only
case T_LEFTBRACE:
case T_LEFTBRACE_ALT:
case T_LEFTBRACE_TRIGRAPH:
//look for multiple statements within the braces
//assume that statements are correct within the block
#ifdef DEBUG_RLPARSER2
std::cout<<"\nfound left brace: "<<id_value.str()<<"\n";
#endif
++brace_count;
Match(id);
//continue overlooking symbols until a right brace is found
//assumption: the macro is a multi-statement C++ code
//there are no macros which has only open or only close brace
do {
id = *it;
//std::cout<<"Inside do-while loop curr symbol:"<<it->get_value();
if(id == T_RIGHTBRACE) {//for nested braces
#ifdef DEBUG_RLPARSER2
std::cout<<"\nfound right brace: \n";
#endif
--brace_count;
}
if(id == T_LEFTBRACE) {
#ifdef DEBUG_RLPARSER2
std::cout<<"\nfound left brace: \n";
#endif
++brace_count;
}
if(id == boost::wave::T_NEWLINE)
break;
Match(id);
} while(brace_count!=0);
if(brace_count != 0) rl_ccat = RlCCat::open;
rl_ttype.braces_type = true;
//set the category
break;
//if there is a right brace without a left brace that
//meand it is code-snippet a.k.a. partial
case T_RIGHTBRACE:
case T_RIGHTBRACE_ALT:
case T_RIGHTBRACE_TRIGRAPH:
rl_ccat = RlCCat::open;
rl_ttype.braces_type = true;
break;
//skip for now
// case T_POUND_POUND:
// case T_POUND_POUND_ALT:
// case T_POUND_POUND_TRIGRAPH:
// throw ExceptionHandler("##operator");
case T_ELLIPSIS:
Match(id);
Match(T_IDENTIFIER);
rl_ttype.unknown_type = true;
break;
case T_SEMICOLON:
//std::cout<<"\nlooking for semi colon: \n";
Match(id);
// assuming a syntactically corrent macro,
// semicolon marks end of a statement
rl_ttype.statement_type = true;
std::cout<<"\nfound semi colon: \n";
break;
case T_DOT:
case T_ARROW:
// :: has the highest priority
case T_COLON_COLON:
case T_COLON:
case T_DOTSTAR:
case T_ARROWSTAR:
Match(id);
rl_ttype.special_type = true;
break;
case T_QUESTION_MARK:
Match(T_QUESTION_MARK);
Expression();
Match(T_COLON);
Expression();
rl_ttype.ternary_operator_type = true;
break;
//set rl_tcat::assignment_type;
//skips
case T_SPACE:
case T_SPACE2:
case T_CONTLINE:
Match(id);
break;
case T_CCOMMENT://eliminate before analyzing
case T_CPPCOMMENT:
#ifdef DEBUG_RLPARSER2
std::cout<<"\ncomments: "<<id_value.str()<<"\n";
#endif
break;
//may be error
case T_ANY:
case T_ANY_TRIGRAPH:
case T_UNKNOWN:
case T_FIRST_TOKEN:
case T_PP_NUMBER:
rl_ttype.unknown_type = true;
Match(id);
break;
//the new line should come after everything
case T_NEWLINE:
case T_GENERATEDNEWLINE:
/** **************************************************************
Before the ExpressionStatement function, the matched T_NEWLINE
threw error. The only place where a newline comes in the
replacement text is due to T_CPPCOMMENT. where I insert a T_NEWLINE (see Parser.cpp)
So \em{that} T_NEWLINE should come at the end and not here. With the introduction
of ExpressionStatement() it would match the T_NEWLINE \em{here}...
So now I am not throwing any error from this place.
**************************************************************
id_value.str(std::string());
//ErrorMsg Format: <FileName>:<LineNo>:<ColNo>:<ErrorMessage>
id_value <<"found T_NEWLINE, failed parsing. "
<<"expecting literal/identifier/expression "
<<"in the replacement list";
throw ExceptionHandler(*it, id_value.str());
*/
Match(id);
break;
case T_EOF:
case T_EOI:
default:
id_value.str(std::string());
//ErrorMsg Format: <FileName>:<LineNo>:<ColNo>:<ErrorMessage>
id_value <<"found token: '"<<it->get_value()<<"', "
<<"expecting literal/identifier/expression "
<<"in the replacement list";
throw ExceptionHandler(*it, id_value.str());
}
}
bool RlParser::IsRejectPredefinedMacro(std::string str) const
{
//basically all the following macros are categorised as reject types
if(pDemacroficationScheme->macrosPreventingDemacrofication.find(str)
!= pDemacroficationScheme->macrosPreventingDemacrofication.end()){
logFile<<" - note: found the macro "<<str<<", not demacrofying\n";
#ifdef DEBUG_RLPARSER
std::cout<<"\nFound reject type: "<<str;
#endif
return true;
}
else {
return false;
}
}
void RlParser::FillFormattedRL(token_type tok)
{
using namespace boost::wave;
token_id id = tok;
//rl_str_formatted += tok.get_value().c_str();
//rl_str_formatted += " ";
switch(id) {
case T_SEMICOLON:
rl_str_formatted += "\n";
break;
case T_LEFTBRACE:
rl_str_formatted += "\n";
break;
case T_RIGHTBRACE:
rl_str_formatted += "\n";
break;
default:
break;
}
// std::cout<<"FORMATTED REPLACEMENT TEXT:\n";
// std::cout<<rl_str_formatted<<"\n";
}
/*
* might be useful for analyzing how the parsing is done
* capture the output from all the functions like "indside expression 1" etc
void RlParser::print_state_transition()
{
//std::cout<<replacement_list_str<<"\n";
//std::for_each(state_transition.begin(),state_transition.end(),[](int v){//std::cout<<v<<" ";});
//std::cout<<"\n";
}
*/