forked from satya-das/cppparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
2340 lines (2124 loc) · 83 KB
/
parser.y
File metadata and controls
2340 lines (2124 loc) · 83 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
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
/*
TODOs:
(1) Parsing of following needs improvements/support:
(a) Imp: Function pointer
(b) Sup: Reference to array
(c) Sup: Pointer to array
Need to borrow ideas from https://www.nongnu.org/hcb/
and may be http://www.computing.surrey.ac.uk/research/dsrg/fog/CxxGrammar.y too can help.
*/
// clang-format off
%{
#include "cpptoken.h"
#include "cpp_entity_builders.h"
#include "cppast/cppast.h"
#include "optional.h"
#include "parser.tab.h"
#include "parser.l.h"
#include "utils.h"
#include "memory_util.h"
#include <cstdio>
#include <iostream>
#include <unordered_map>
#include <stack>
#include <vector>
//////////////////////////////////////////////////////////////////////////
#ifndef NDEBUG
# define YYDEBUG 1
#else
# define YYDEBUG 0
#endif //#ifndef NDEBUG
#define YYERROR_DETAILED
#define YYDELETEPOSN(x, y)
#define YYDELETEVAL(x, y)
#ifndef TRUE // Need this to fix BtYacc compilation error.
# define TRUE true
#endif
static int gParseLog = 0;
#define ZZLOG \
{ \
if (gParseLog) \
printf("ZZLOG @line#%d, parsing stream line#%d\n", __LINE__, g.mLineNo); \
}
static int gDisableYyValid = 0;
#define ZZVALID { \
if (gParseLog) \
printf("ZZVALID: "); \
ZZLOG; \
if (!gDisableYyValid) \
YYVALID; \
}
#define ZZERROR \
do { \
if (gParseLog) \
printf("ZZERROR: "); \
ZZLOG; \
YYERROR; \
} while(0)
#define ZZVALID_DISABLE \
++gDisableYyValid;
#define ZZVALID_ENABLE \
--gDisableYyValid;
/** {Globals} */
/**
* A program unit is the entire parse tree of a source/header file
*/
static cppast::CppCompound* gProgUnit;
// FuncdeclHack:
// Following gets parsed as variable with initialization:
// Type Identifier(Type * Id);
// `Type * Id` gets parsed as expression involving multiplication and so `Identifier`
// followed by expression in brackets becomes a call to constructor of `Type`.
// Actually there is an ambiguity in the grammer which compilers solve by using context.
// For purpose of this parser we cannot collect all required context to solve this ambiguity.
// So, we use a hack:
// We define a production rule for this case and flag it as error. But before flagging error
// we save the position of operator '*' (or '&', or "&&") and then we check for location of
// the same operator in other expression production rule before accepting that as valid expression.
// For us we always want to parse it as function declaration rather than call to constructor by passing an expression,
// and so the hack is expected to serve us well.
static const char* gParamModPos = nullptr;
// TemplateParamHack:
// Template parameter gets parsed as vardecl which then gets reduced as templateparam without name as used in forward declaration.
// We don't want that, so to avoid such templateparam getting reduced as vardecl we apply some hack.
static const char* gTemplateParamStart = nullptr;
static bool gInTemplateSpec = false;
/**
* A stack to know where (i.e. how deep inside class defnition) the current parsing activity is taking place.
*/
using CppCompoundStack = std::stack<CppToken>;
static CppCompoundStack gCompoundStack;
/** {End of Globals} */
#define YYPOSN char*
extern int yylex();
// Yacc generated code causes warnings that need suppression.
// This pragma should be at the end.
#if defined(__clang__) || defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wwrite-strings"
#endif
using namespace cppast;
// FIXME: Improve template arg parsing.
// Template argument needs more robust support.
// As of now we are treating them just as string.
// But for parsing we need to have a type.
class CppTemplateArg;
%}
%union {
struct CppToken str;
struct CppFunctionData funcDeclData;
cppast::CppMemberInit* memInit;
cppast::CppEntity* cppEntity;
cppast::CppEntityAccessSpecifier* accessSpecifier;
cppast::CppTypeModifier typeModifier;
cppast::CppVarType* cppVarType;
cppast::CppVar* cppVarObj;
cppast::CppEnum* cppEnum;
cppast::CppEnumItem* enumItem;
std::list<cppast::CppEnumItem>* enumItemList;
cppast::CppTypedefName* typedefName;
cppast::CppTypedefList* typedefList;
cppast::CppUsingDecl* usingDecl;
cppast::CppUsingNamespaceDecl* usingNamespaceDecl;
cppast::CppNamespaceAlias* namespaceAlias;
cppast::CppCompound* cppCompundObj;
cppast::CppTemplateParam* templateParam;
std::vector<cppast::CppTemplateParam>* templateParamList;
cppast::CppDocumentationComment* docCommentObj;
cppast::CppForwardClassDecl* fwdDeclObj;
cppast::CppVarList* cppVarObjList;
cppast::CppPreprocessorUnrecognized* unRecogPreProObj;
cppast::CppExpression* cppExprObj;
cppast::CppCallArgs* exprList;
cppast::CppLambda* cppLambda;
cppast::CppFunction* cppFuncObj;
cppast::CppFunctionPointer* cppFuncPointerObj;
cppast::CppEntity* varOrFuncPtr;
std::vector<std::unique_ptr<cppast::CppEntity>>* paramList;
cppast::CppConstructor* cppCtorObj;
cppast::CppDestructor* cppDtorObj;
cppast::CppTypeConverter* cppTypeConverter;
cppast::CppMemberInits* memInitList;
std::list<cppast::CppInheritanceInfo>* inheritList;
bool inheritType;
std::vector<std::string>* identifierList;
std::vector<std::string>* funcThrowSpec;
class CppTemplateArg* templateArg;
cppast::CppAsmBlock* asmBlock;
cppast::CppCompoundType compoundType;
unsigned short ptrLevel;
cppast::CppRefType refType;
unsigned int attr;
Optional<cppast::CppAccessType> objAccessType;
cppast::CppCallArgs* attribSpecifiers;
cppast::CppIfBlock* ifBlock;
cppast::CppWhileBlock* whileBlock;
cppast::CppDoWhileBlock* doWhileBlock;
cppast::CppForBlock* forBlock;
cppast::CppRangeForBlock* forRangeBlock;
cppast::CppSwitchBlock* switchBlock;
std::vector<cppast::CppCase>* switchBody;
cppast::CppTryBlock* tryBlock;
cppast::CppCatchBlock* catchBlock;
cppast::CppPreprocessorDefine* hashDefine;
cppast::CppPreprocessorUndef* hashUndef;
cppast::CppPreprocessorInclude* hashInclude;
cppast::CppPreprocessorImport* hashImport;
cppast::CppPreprocessorConditional* hashIf;
cppast::CppPreprocessorError* hashError;
cppast::CppPreprocessorWarning* hashWarning;
cppast::CppPreprocessorPragma* hashPragma;
cppast::CppReturnStatement* returnStmt;
cppast::CppThrowStatement* throwStmt;
cppast::CppGotoStatement* gotoStmt;
cppast::CppBlob* blob;
cppast::CppLabel* label;
cppast::CppVarInitInfo* cppVarInitInfo;
}
%token <str> tknName tknID tknStrLit tknCharLit tknNumber tknMacro tknApiDecor
%token <str> tknTypedef tknUsing
%token <str> tknInteger tknChar tknDouble tknFloat
%token <str> tknEnum
%token <str> tknAuto
%token <str> tknPreProDef
%token <str> tknClass tknStruct tknUnion tknNamespace
%token <str> tknTemplate tknTypename tknDecltype
%token <str> tknFreeStandingBlockComment tknSideBlockComment tknFreeStandingLineComment tknSideLineComment
%token <str> tknScopeResOp
%token <str> tknNumSignSpec // signed/unsigned
%token <str> tknPublic tknProtected tknPrivate
%token <str> tknExternC
%token <str> tknUnRecogPrePro
%token <str> tknStdHdrInclude
%token <str> tknPragma tknHashError tknHashWarning
%token <str> tknEllipsis
%token <str> tknConstCast tknStaticCast tknDynamicCast tknReinterpretCast
%token <str> tknTry tknCatch tknThrow tknSizeOf
%token <str> tknOperator tknPlusEq tknMinusEq tknMulEq tknDivEq tknPerEq tknXorEq tknAndEq tknOrEq
%token <str> tknLShift tknRShift tknLShiftEq tknRShiftEq tknCmpEq tknNotEq tknLessEq tknGreaterEq
%token <str> tkn3WayCmp tknAnd tknOr tknInc tknDec tknArrow tknArrowStar
%token <str> tknLT tknGT // We will need the position of these operators in stream when used for declaring template instance.
%token <str> '+' '-' '*' '/' '%' '^' '&' '|' '~' '!' '=' ',' '(' ')' '[' ']' ';' '.'
%token <str> tknNew tknDelete
%token <str> tknConst tknConstExpr
%token <str> tknVoid // For the cases when void is used as function parameter.
%token <str> tknOverride tknFinal // override, final are not a reserved keywords
%token <str> tknAsm
%token <str> tknBlob
%token <str> tknGoto
%token tknStatic tknExtern tknVirtual tknInline tknExplicit tknFriend tknVolatile tknMutable tknNoExcept
%token tknPreProHash /* When # is encountered for pre processor definition */
%token tknDefine tknUndef
%token tknInclude tknImport
%token tknIf tknIfDef tknIfNDef tknElse tknElIf tknEndIf
%token tknFor tknWhile tknDo tknSwitch tknCase tknDefault
%token tknReturn
%token tknBlankLine
%type <str> strlit
%type <str> optapidecor apidecor apidecortokensq
%type <str> identifier optidentifier numbertype typeidentifier varidentifier optname id name designatedname operfuncname funcname
%type <str> templidentifier templqualifiedid
%type <str> doccommentstr optdoccommentstr
%type <str> rshift
%type <str> macrocall
%type <cppEntity> stmt
%type <typeModifier> opttypemodifier typemodifier
%type <cppEnum> enumdefn enumfwddecl enumdefnstmt
%type <enumItem> enumitem
%type <enumItemList> enumitemlist
%type <fwdDeclObj> fwddecl
%type <cppVarType> vartype
%type <cppVarObj> vardecl varinit vardeclstmt
%type <cppVarInitInfo> varassign optvarassign
%type <varOrFuncPtr> param
%type <str> funcobjstr /* Identify funcobjstr as str, at least for time being */
%type <templateArg> templatearg templatearglist /* For time being. We may need to make it more robust in future. */
%type <asmBlock> asmblock
%type <cppVarObjList> vardecllist vardeclliststmt
%type <paramList> paramlist lambdaparams
%type <typedefName> typedefname typedefnamestmt
%type <typedefList> typedeflist typedefliststmt
%type <usingNamespaceDecl> usingnamespacedecl
%type <namespaceAlias> namespacealias
%type <usingDecl> usingdecl
%type <cppCompundObj> stmtlist optstmtlist progunit classdefn namespacedefn classdefnstmt externcblock block
%type <templateParamList> templatespecifier templateparamlist
%type <templateParam> templateparam
%type <docCommentObj> doccomment
%type <cppExprObj> expr exprstmt optexpr lambdacapture captureallbyref captureallbyval exprorlist optexprorlist desinatedinitialization
%type <exprList> exprlist optexprlist
%type <cppExprObj> objcarg objcarglist
%type <cppLambda> lambda
%type <ifBlock> ifblock;
%type <whileBlock> whileblock;
%type <doWhileBlock> dowhileblock;
%type <forBlock> forblock;
%type <forRangeBlock> forrangeblock;
%type <switchBlock> switchstmt;
%type <switchBody> caselist;
%type <tryBlock> tryblock;
%type <catchBlock> catchblock;
%type <cppFuncPointerObj> functionpointer functionptrtype funcpointerdecl funcptrtypedef funcptrortype funcobj
%type <funcDeclData> funcdecldata
%type <cppFuncObj> funcdecl funcdeclstmt funcdefn
%type <cppCtorObj> ctordecl ctordeclstmt ctordefn
%type <cppDtorObj> dtordecl dtordeclstmt dtordefn
%type <cppTypeConverter> typeconverter typeconverterstmt
%type <memInitList> meminitlist
%type <memInit> meminit
%type <compoundType> classspecifier
%type <attr> varattrib exptype optfuncattrib functype optfunctype optfinal
%type <inheritList> optinheritlist
%type <inheritType> optinherittype
%type <objAccessType> protlevel
%type <accessSpecifier> entityaccessspecifier
%type <identifierList> identifierlist
%type <funcThrowSpec> functhrowspec optfuncthrowspec
%type <attribSpecifiers> attribs optattribs attribspecifier attribspecifiers optattribspecifiers
%type <hashDefine> define
%type <hashUndef> undef
%type <hashInclude> include
%type <hashImport> import
%type <hashIf> hashif
%type <hashError> hasherror
%type <hashWarning> hashwarning
%type <hashPragma> pragma
%type <returnStmt> returnstmt
%type <throwStmt> throwstmt
%type <gotoStmt> gotostmt
%type <cppEntity> preprocessor
%type <blob> blob
%type <label> label
// precedence as mentioned at https://en.cppreference.com/w/cpp/language/operator_precedence
%left COMMA
// &=, ^=, |=, <<=, >>=, *=, /=, %=, +=, -=, =, throw, a?b:c
%right tknAndEq tknXorEq tknOrEq tknLShiftEq tknRShiftEq tknMulEq tknDivEq tknPerEq tknPlusEq tknMinusEq '=' tknThrow '?' TERNARYCOND tknReturn
%left tknOr
%left tknAnd
%left '|'
%left '^'
%left '&'
%left tknCmpEq tknNotEq // ==, !=
// tknLT and tknGT are used instead of '<', and '>' because otherwise parsing template and template args is very difficult.
%left tknLT tknGT tknLessEq tknGreaterEq
%left tkn3WayCmp // <=>
%left tknLShift tknRShift RSHIFT
%left '+' '-'
%left '*' '/' '%'
%left tknArrowStar
%right PREINCR PREDECR UNARYMINUS '!' '~' CSTYLECAST DEREF ADDRESSOF tknSizeOf tknNew tknDelete
%left POSTINCR POSTDECR FUNCTIONALCAST FUNCCALL SUBSCRIPT '.' tknArrow
%left tknScopeResOp
%right GLOBAL
%left TEMPLATE
/*
These are required to remove following ambiguity in the grammer.
Consider the following example:
x * y;
Now it can be parsed as:
(1) y is a pointer to type-x.
(2) Or, the expression is multiplication of x and y.
Same ambiguity exists for:
x * y = z;
x & y;
x & y = z;
x && y;
x && y = z;
PTRDECL and REFDECL solve this problem by giving variable declaration higher precedence.
*/
%left PTRDECL REFDECL
/*
These are required to remove following ambiguity in the grammer.
Consider the following example:
class A
{
A(); // ctor declaration
~A(); // dtor declaration
};
Now A() can be parsed in two different ways:
(1) As a constructor declaration.
(2) Or, as a function call.
Also, ~A() can be parsed as:
(1) As a destructor declaration.
(2) Or, as an expression where bit toggle operation is done on a return value of function call.
CTORDECL and DTORDECL solve this problem by giving constructor and destructor declarations higher precedence.
*/
%left CTORDECL DTORDECL
%%
/* A program unit is a source file, be it header file or implementation file */
progunit
: optstmtlist [ZZLOG;] {
gProgUnit = $$ = $1;
if (gProgUnit)
gProgUnit->compoundType(CppCompoundType::FILE);
}
;
optstmtlist
: [ZZLOG;] {
$$ = nullptr;
}
| stmtlist [ZZLOG;] {
$$ = $1;
}
;
stmtlist
: stmt [ZZLOG;] {
$$ = new cppast::CppCompound();
if ($1)
{
$$->add(Ptr($1));
} // Avoid 'comment-btyacc-constructs.sh' to act on this
}
| stmtlist stmt [ZZLOG;] {
$$ = ($1 == 0) ? new cppast::CppCompound() : $1;
if ($2)
{
$$->add(Ptr($2));
} // Avoid 'comment-btyacc-constructs.sh' to act on this
}
;
stmt
: vardeclstmt [ZZLOG;] { $$ = $1; }
| vardeclliststmt [ZZLOG;] { $$ = $1; }
| enumdefnstmt [ZZLOG;] { $$ = $1; }
| enumfwddecl [ZZLOG;] { $$ = $1; }
| typedefnamestmt [ZZLOG;] { $$ = $1; }
| typedefliststmt [ZZLOG;] { $$ = $1; }
| classdefnstmt [ZZLOG;] { $$ = $1; }
| namespacedefn [ZZLOG;] { $$ = $1; }
| fwddecl [ZZLOG;] { $$ = $1; }
| doccomment [ZZLOG;] { $$ = $1; }
| exprstmt [ZZLOG;] { $$ = $1; }
| ifblock [ZZLOG;] { $$ = $1; }
| whileblock [ZZLOG;] { $$ = $1; }
| dowhileblock [ZZLOG;] { $$ = $1; }
| forblock [ZZLOG;] { $$ = $1; }
| forrangeblock [ZZLOG;] { $$ = $1; }
| funcpointerdecl [ZZLOG;] { $$ = $1; }
| funcdeclstmt [ZZLOG;] { $$ = $1; }
| funcdefn [ZZLOG;] { $$ = $1; }
| ctordeclstmt [ZZLOG;] { $$ = $1; }
| ctordefn [ZZLOG;] { $$ = $1; }
| dtordeclstmt [ZZLOG;] { $$ = $1; }
| dtordefn [ZZLOG;] { $$ = $1; }
| typeconverterstmt [ZZLOG;] { $$ = $1; }
| externcblock [ZZLOG;] { $$ = $1; }
| funcptrtypedef [ZZLOG;] { $$ = $1; }
| preprocessor [ZZLOG;] { $$ = $1; }
| block [ZZLOG;] { $$ = $1; }
| switchstmt [ZZLOG;] { $$ = $1; }
| tryblock [ZZLOG;] { $$ = $1; }
| usingdecl [ZZLOG;] { $$ = $1; }
| usingnamespacedecl [ZZLOG;] { $$ = $1; }
| namespacealias [ZZLOG;] { $$ = $1; }
| macrocall [ZZLOG;] { $$ = new cppast::CppMacroCall($1); }
| macrocall ';' [ZZLOG;] { $$ = new cppast::CppMacroCall(MergeCppToken($1, $2)); }
| apidecortokensq macrocall [ZZLOG;] { $$ = new cppast::CppMacroCall(MergeCppToken($1, $2)); }
| ';' [ZZLOG;] { $$ = nullptr; } /* blank statement */
| asmblock [ZZLOG;] { $$ = $1; }
| blob [ZZLOG;] { $$ = $1; }
| label [ZZLOG;] { $$ = $1; }
| returnstmt [ZZLOG;] { $$ = $1; }
| throwstmt [ZZLOG;] { $$ = $1; }
| gotostmt [ZZLOG;] { $$ = $1; }
| entityaccessspecifier [ZZLOG;] { $$ = $1; }
;
label
: name ':' [ZZLOG;] { $$ = new cppast::CppLabel($1); }
;
preprocessor
: define [ZZLOG;] { $$ = $1; }
| undef [ZZLOG;] { $$ = $1; }
| include [ZZLOG;] { $$ = $1; }
| import [ZZLOG;] { $$ = $1; }
| hashif [ZZLOG;] { $$ = $1; }
| hasherror [ZZLOG;] { $$ = $1; }
| hashwarning [ZZLOG;] { $$ = $1; }
| pragma [ZZLOG;] { $$ = $1; }
;
asmblock
: tknAsm [ZZLOG;] { $$ = new cppast::CppAsmBlock($1); }
;
macrocall
: tknMacro [ZZLOG; $$ = $1;] {}
| macrocall '(' ')' [ZZLOG; $$ = MergeCppToken($1, $3); ] {}
| macrocall '(' expr ')' [
ZZLOG;
$$ = MergeCppToken($1, $4);
delete $3;
] {}
;
switchstmt
: tknSwitch '(' expr ')' '{' caselist '}' [ZZLOG;] {
$$ = new cppast::CppSwitchBlock(Ptr($3), std::move(*Ptr($6)));
}
;
caselist
: [ZZLOG;] {
$$ = new std::vector<cppast::CppCase>;
}
| caselist tknCase expr ':' optstmtlist [ZZLOG;] {
$$ = $1;
$$->emplace_back(Ptr($3), Ptr($5));
}
| caselist tknDefault ':' optstmtlist [ZZLOG;] {
$$ = $1;
$$->emplace_back(nullptr, Ptr($4));
}
| doccommentstr caselist [ZZLOG;] { $$ = $2; }
| caselist doccommentstr [ZZLOG;] { $$ = $1; }
;
block
: '{' optstmtlist '}' [ZZLOG;] {
$$ = $2;
if ($$ == nullptr)
$$ = new cppast::CppCompound(CppCompoundType::BLOCK);
else
$$->compoundType(CppCompoundType::BLOCK);
}
| doccomment block [ZZLOG;] {
$$ = $2;
}
;
ifblock
: tknIf '(' expr ')' stmt [ZZLOG;] {
$$ = new cppast::CppIfBlock(Ptr($3), Ptr($5));
}
| tknIf '(' expr ')' stmt tknElse stmt [ZZLOG;] {
$$ = new cppast::CppIfBlock(Ptr($3), Ptr($5), Ptr($7));
}
| tknIf '(' varinit ')' stmt [ZZLOG;] {
$$ = new cppast::CppIfBlock(Ptr($3), Ptr($5));
}
| tknIf '(' varinit ')' stmt tknElse stmt [ZZLOG;] {
$$ = new cppast::CppIfBlock(Ptr($3), Ptr($5), Ptr($7));
}
/* TODO: Add support for else-if: compare the if.cpp file and its output by e2e test. */
;
whileblock
: tknWhile '(' expr ')' stmt [ZZLOG;] {
$$ = new cppast::CppWhileBlock(Ptr($3), Ptr($5));
}
| tknWhile '(' varinit ')' stmt [ZZLOG;] {
$$ = new cppast::CppWhileBlock(Ptr($3), Ptr($5));
}
;
dowhileblock
: tknDo stmt tknWhile '(' expr ')' [ZZLOG;] {
$$ = new cppast::CppDoWhileBlock(Ptr($5), Ptr($2));
}
;
forblock
: tknFor '(' optexprorlist ';' optexprorlist ';' optexprorlist ')' stmt [ZZLOG;] {
$$ = new cppast::CppForBlock(Ptr($3), Ptr($5), Ptr($7), Ptr($9));
}
| tknFor '(' varinit ';' optexprorlist ';' optexprorlist ')' stmt [ZZLOG;] {
$$ = new cppast::CppForBlock(Ptr($3), Ptr($5), Ptr($7), Ptr($9));
}
| tknFor '(' vardecllist ';' optexprorlist ';' optexprorlist ')' stmt [ZZLOG;] {
$$ = new cppast::CppForBlock(Ptr($3), Ptr($5), Ptr($7), Ptr($9));
}
;
forrangeblock
: tknFor '(' vardecl ':' expr ')' stmt [ZZLOG;] {
$$ = new cppast::CppRangeForBlock(Ptr($3), Ptr($5), Ptr($7));
}
;
tryblock
: tknTry block catchblock [ZZLOG;] {
$$ = new cppast::CppTryBlock(Ptr($2), Ptr($3));
}
| tryblock catchblock [ZZLOG;] {
$$ = $1;
$$->addCatchBlock(Ptr($2));
}
;
catchblock
: tknCatch '(' vartype optname ')' block [ZZLOG;] {
$$ = new cppast::CppCatchBlock{Ptr($3), $4, Ptr($6)};
}
;
optexpr
: {
$$ = nullptr;
}
| expr [ZZLOG;] {
$$ = $1;
}
/* | exprlist [ZZLOG;] {
$$ = $1;
} */
;
define
: tknPreProHash tknDefine name name [ZZLOG;] {
$$ = new cppast::CppPreprocessorDefine(cppast::CppPreprocessorDefineType::RENAME, $3, $4);
}
| tknPreProHash tknDefine name [ZZLOG;] {
$$ = new cppast::CppPreprocessorDefine(cppast::CppPreprocessorDefineType::RENAME, $3);
}
| tknPreProHash tknDefine name tknNumber [ZZLOG;] {
$$ = new cppast::CppPreprocessorDefine(cppast::CppPreprocessorDefineType::NUMBER, $3, $4);
}
| tknPreProHash tknDefine name tknStrLit [ZZLOG;] {
$$ = new cppast::CppPreprocessorDefine(cppast::CppPreprocessorDefineType::STRING, $3, $4);
}
| tknPreProHash tknDefine name tknCharLit [ZZLOG;] {
$$ = new cppast::CppPreprocessorDefine(cppast::CppPreprocessorDefineType::CHARACTER, $3, $4);
}
| tknPreProHash tknDefine name tknPreProDef [ZZLOG;] {
$$ = new cppast::CppPreprocessorDefine(cppast::CppPreprocessorDefineType::COMPLEX_DEFN, $3, $4);
}
;
undef
: tknPreProHash tknUndef name [ZZLOG;] { $$ = new cppast::CppPreprocessorUndef($3); }
;
include
: tknPreProHash tknInclude tknStrLit [ZZLOG;] { $$ = new cppast::CppPreprocessorInclude($3); }
| tknPreProHash tknInclude tknStdHdrInclude [ZZLOG;] { $$ = new cppast::CppPreprocessorInclude($3); }
;
import
: tknPreProHash tknImport tknStrLit [ZZLOG;] { $$ = new cppast::CppPreprocessorImport($3); }
| tknPreProHash tknImport tknStdHdrInclude [ZZLOG;] { $$ = new cppast::CppPreprocessorImport($3); }
;
hashif
: tknPreProHash tknIf tknPreProDef [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::IF, $3); }
| tknPreProHash tknIfDef name [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::IFDEF, $3); }
| tknPreProHash tknIfNDef name [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::IFNDEF, $3); }
| tknPreProHash tknIfNDef tknApiDecor [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::IFNDEF, $3); }
| tknPreProHash tknElse [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::ELSE ); }
| tknPreProHash tknElIf tknPreProDef [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::ELIF, $3); }
| tknPreProHash tknEndIf [ZZLOG;] { $$ = new cppast::CppPreprocessorConditional(PreprocessorConditionalType::ENDIF ); }
;
hasherror
: tknPreProHash tknHashError [ZZLOG;] { $$ = new cppast::CppPreprocessorError($2); }
| tknPreProHash tknHashError strlit [ZZLOG;] { $$ = new cppast::CppPreprocessorError(MergeCppToken($2, $3)); }
;
hashwarning
: tknPreProHash tknHashWarning [ZZLOG;] { $$ = new cppast::CppPreprocessorWarning($2); }
| tknPreProHash tknHashWarning strlit [ZZLOG;] { $$ = new cppast::CppPreprocessorWarning(MergeCppToken($2, $3)); }
;
pragma
: tknPreProHash tknPragma tknPreProDef [ZZLOG;] { $$ = new cppast::CppPreprocessorPragma($3); }
;
doccomment
: doccommentstr [ZZLOG;] { $$ = new cppast::CppDocumentationComment((std::string) $1); }
;
optdoccommentstr
: [ZZLOG;] { $$ = CppToken{nullptr, 0}; }
| doccommentstr [ZZLOG;] { $$ = $1; }
;
doccommentstr
: tknFreeStandingBlockComment [ZZLOG;] { $$ = $1; }
| tknFreeStandingLineComment [ZZLOG;] { $$ = $1; }
| doccommentstr tknFreeStandingBlockComment [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| doccommentstr tknFreeStandingLineComment [ZZLOG;] { $$ = MergeCppToken($1, $2); }
;
identifier
: name [ZZLOG; $$ = $1; ] {}
| identifier tknScopeResOp identifier [ZZLOG; $$ = MergeCppToken($1, $3); ] {}
| id [ZZLOG; $$ = $1; ] {}
| templidentifier [ZZLOG; $$ = $1; ] {}
| tknOverride [ZZLOG; $$ = $1;] { /* override is not a reserved keyword */ }
| identifier tknEllipsis [ZZLOG; $$ = MergeCppToken($1, $2); ] {}
| macrocall [ZZLOG; $$ = $1; ] {}
| templqualifiedid [ZZLOG; $$ = $1; ] {}
;
numbertype
: tknInteger [ZZLOG;] { $$ = $1; }
| tknFloat [ZZLOG;] { $$ = $1; }
| tknDouble [ZZLOG;] { $$ = $1; }
| tknChar [ZZLOG;] { $$ = $1; }
| tknNumSignSpec [ZZLOG;] { $$ = $1; }
| tknNumSignSpec numbertype [ZZLOG;] { $$ = MergeCppToken($1, $2); }
;
typeidentifier
: identifier [ZZLOG;] { $$ = $1; }
| tknScopeResOp identifier %prec GLOBAL [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| typeidentifier tknScopeResOp typeidentifier [ZZLOG;] { $$ = MergeCppToken($1, $3); }
| numbertype [ZZLOG;] { $$ = $1; }
| typeidentifier '[' ']' [ZZLOG;] { $$ = MergeCppToken($1, $3); }
| tknAuto [ZZLOG;] { $$ = $1; }
| tknVoid [ZZLOG;] { $$ = $1; }
| tknEnum identifier [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| tknTypename identifier [
if (gTemplateParamStart == $1.sz)
ZZERROR;
else
ZZLOG;
] [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| tknEllipsis [ZZLOG;] { $$ = $1; }
| tknTypename tknEllipsis [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| tknClass tknEllipsis [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| typeidentifier tknEllipsis [ZZLOG;] { $$ = MergeCppToken($1, $2); }
| tknDecltype '(' expr ')' [ZZLOG;] { $$ = MergeCppToken($1, $4); delete $3; }
;
templidentifier
: identifier tknLT templatearglist tknGT [ZZLOG; $$ = MergeCppToken($1, $4); ] {}
// The following rule is needed to parse an ambiguous input as template identifier,
// see the test "vardecl-or-expr-ambiguity".
| identifier tknLT expr tknNotEq expr tknGT [ZZLOG; $$ = MergeCppToken($1, $6); ] {}
// The following rule is needed to parse a template identifier which otherwise fails to parse
// because of higher precedence of tknLT and tknGT,
// see the test "C<Class, v != 0> x;".
| identifier tknLT templatearglist ',' expr tknNotEq expr tknGT [ZZLOG; $$ = MergeCppToken($1, $8); ] {}
;
templqualifiedid
: tknTemplate templidentifier [ZZLOG; $$ = MergeCppToken($1, $2); ] {}
;
name
: tknName [ZZLOG; $$ = $1;] {}
;
designatedname
: '.' name [ZZLOG;] {$$ = MergeCppToken($1, $2);}
;
id
: tknID [ZZLOG; $$ = $1; ] {}
;
optname
: [ZZLOG;] { $$ = MakeCppToken(nullptr, nullptr); }
| name [ZZLOG;] { $$ = $1; }
optidentifier
: [ZZLOG;] { $$ = MakeCppToken(nullptr, nullptr); }
| identifier [ZZLOG;] { $$ = $1; }
;
enumitem
: name [ZZLOG;] { $$ = new cppast::CppEnumItem($1); }
| name '=' expr [ZZLOG;] { $$ = new cppast::CppEnumItem($1, Ptr($3)); }
| doccomment [ZZLOG;] { $$ = new cppast::CppEnumItem(Ptr($1)); }
| preprocessor [ZZLOG;] { $$ = new cppast::CppEnumItem(Ptr($1)); }
| macrocall [ZZLOG;] { $$ = new cppast::CppEnumItem(Ptr(new cppast::CppMacroCall($1))); }
| blob [ZZLOG;] { $$ = new cppast::CppEnumItem(Ptr($1)); }
;
blob
: tknBlob [ZZLOG;] { $$ = new cppast::CppBlob($1); }
;
enumitemlist
: [ZZLOG;] { $$ = 0; }
| enumitemlist enumitem [ZZLOG;] {
$$ = $1 ? $1 : new std::list<cppast::CppEnumItem>;
$$->push_back(Obj($2));
}
| enumitemlist ',' enumitem [ZZLOG;] {
$$ = $1 ? $1 : new std::list<cppast::CppEnumItem>;
$$->push_back(Obj($3));
}
| enumitemlist ',' [ZZLOG;] {
$$ = $1;
}
;
enumdefn
: tknEnum optname '{' enumitemlist '}' [ZZVALID;] {
$$ = new cppast::CppEnum($2, Obj($4));
}
| tknEnum optapidecor name ':' typeidentifier '{' enumitemlist '}' [ZZVALID;] {
$$ = new cppast::CppEnum($3, Obj($7), false, $5);
};
| tknEnum ':' typeidentifier '{' enumitemlist '}' [ZZVALID;] {
$$ = new cppast::CppEnum("", Obj($5), false, $3);
};
| tknEnum optapidecor name '{' enumitemlist '}' [ZZVALID;] {
$$ = new cppast::CppEnum($3, Obj($5), false);
};
| tknEnum tknClass optapidecor name ':' typeidentifier '{' enumitemlist '}' [ZZVALID;] {
$$ = new cppast::CppEnum($4, Obj($8), true, $6);
}
| tknEnum tknClass optapidecor name '{' enumitemlist '}' [ZZVALID;] {
$$ = new cppast::CppEnum($4, Obj($6), true);
}
| tknTypedef tknEnum optapidecor optname '{' enumitemlist '}' name [ZZVALID;] {
$$ = new cppast::CppEnum($8, Obj($6));
}
;
enumdefnstmt
: enumdefn ';' [ZZLOG;] { $$ = $1; }
;
enumfwddecl
: tknEnum name ':' typeidentifier ';' [ZZVALID;] {
$$ = new cppast::CppEnum($2, {}, false, $4);
}
| tknEnum tknClass name ':' typeidentifier ';' [ZZVALID;] {
$$ = new cppast::CppEnum($3, {}, true, $5);
}
| tknEnum tknClass name ';' [ZZVALID;] {
$$ = new cppast::CppEnum($3, {}, true);
}
;
funcptrtypedef
: tknTypedef functionpointer ';' [ZZVALID;] {
$2->addAttr(TYPEDEF);
$$ = $2;
}
typedefnamestmt
: typedefname ';' [ZZVALID;] { $$ = $1; }
;
typedefliststmt
: typedeflist ';' [ZZVALID;] { $$ = $1; }
;
typedeflist
: tknTypedef vardecllist [ZZLOG;] { $$ = new cppast::CppTypedefList(Ptr($2)); }
;
typedefname
: tknTypedef vardecl [ZZLOG;] { $$ = new cppast::CppTypedefName(Ptr($2)); }
;
usingdecl
: tknUsing name '=' vartype ';' [ZZLOG;] {
$$ = new cppast::CppUsingDecl($2, Ptr($4));
}
| tknUsing name '=' functionptrtype ';' [ZZLOG;] {
$$ = new cppast::CppUsingDecl($2, Ptr($4));
}
| tknUsing name '=' funcobj ';' [ZZLOG;] {
$$ = new cppast::CppUsingDecl($2, Ptr($4));
}
| tknUsing name '=' classdefn ';' [ZZLOG;] {
$$ = new cppast::CppUsingDecl($2, Ptr($4));
}
| templatespecifier usingdecl [ZZLOG;] {
$$ = $2;
$$->templateSpecification(Obj($1));
}
| tknUsing identifier ';' [ZZLOG;] {
$$ = new cppast::CppUsingDecl($2);
}
;
namespacealias
: tknNamespace name '=' identifier ';' [ZZLOG;] {
$$ = new cppast::CppNamespaceAlias($2, $4);
}
;
usingnamespacedecl
: tknUsing tknNamespace identifier ';' [ZZLOG;] {
$$ = new cppast::CppUsingNamespaceDecl($3);
}
;
vardeclliststmt
: vardecllist ';' [ZZVALID;] { $$ = $1; }
| exptype vardecllist ';' [ZZVALID;] { $$ = $2; }
;
vardeclstmt
: vardecl ';' [ZZVALID;] { $$ = $1; }
| varinit ';' [ZZVALID;] { $$ = $1; }
| apidecor vardeclstmt [ZZVALID;] { $$ = $2; $$->apidecor($1); }
| exptype vardeclstmt [ZZVALID;] { $$ = $2; $$->addAttr($1); }
| varattrib vardeclstmt [ZZVALID;] { $$ = $2; $$->addAttr($1); }
;
vardecllist
: optfunctype varinit ',' opttypemodifier name optvarassign [ZZLOG;] {
$2->addAttr($1);
$$ = new cppast::CppVarList($2, CppVarDeclInList($4, VarDecl($5, $6)));
}
| optfunctype vardecl ',' opttypemodifier name optvarassign [ZZLOG;] {
$2->addAttr($1);
$$ = new cppast::CppVarList($2, CppVarDeclInList($4, VarDecl($5, $6)));
}
| optfunctype vardecl ',' opttypemodifier name '[' expr ']' [ZZLOG;] {
$2->addAttr($1);
CppVarDecl var2($5);
var2.addArraySize($7);
$$ = new cppast::CppVarList($2, CppVarDeclInList($4, std::move(var2)));
}
| vardecllist ',' opttypemodifier name '[' expr ']' [ZZLOG;] {
$$ = $1;
CppVarDecl var2($4);
var2.addArraySize($6);
$$->addVarDecl(CppVarDeclInList($3, std::move(var2)));
}
| optfunctype vardecl ',' opttypemodifier name ':' expr [ZZLOG;] {
$2->addAttr($1);
$$ = new cppast::CppVarList($2, CppVarDeclInList($4, CppVarDecl{$5}));
/* TODO: Use optvarassign as well */
}
| vardecllist ',' opttypemodifier name optvarassign [ZZLOG;] {
$$ = $1;
$$->addVarDecl(CppVarDeclInList($3, VarDecl($4, $5)));
}
| vardecllist ',' opttypemodifier name optvarassign ':' expr [ZZLOG;] {
$$ = $1;
$$->addVarDecl(CppVarDeclInList($3, VarDecl($4, $5)));
/* TODO: Use optvarassign as well */
}
;
varinit
: vardecl '(' typeidentifier '*' name [gParamModPos = $4.sz; ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl '(' typeidentifier '*' '*' name [gParamModPos = $4.sz; ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl '(' typeidentifier '*' '&' name [gParamModPos = $4.sz; ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl '(' typeidentifier '&' name [gParamModPos = $4.sz; ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl '(' typeidentifier tknAnd name [gParamModPos = $4.sz; ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl '(' typeidentifier ')' [gParamModPos = $3.sz; ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl '(' ')' [ZZERROR;] { /*FuncDeclHack*/ $$ = nullptr; }
| vardecl varassign [ZZLOG;] {
$$ = $1;
$$->initialize(Obj($2));
}
| tknConstExpr varinit [ZZLOG;] {
$$ = $2;
$$->addAttr(CONST_EXPR);
}
;
varassign
: '=' expr [ZZLOG;] {
$$ = VarInitInfo($2);
}
| '(' exprlist ')' [ZZLOG;] {
$$ = VarInitInfo($2, CppConstructorCallStyle::USING_PARENTHESES);
}
| '{' optexprlist '}' [ZZLOG;] {
$$ = VarInitInfo($2, CppConstructorCallStyle::USING_BRACES);
}
;
optvarassign
: [ZZLOG;] { $$ = nullptr; }
| varassign [ZZLOG;] { $$ = $1; }
;
vardecl
: vartype varidentifier [ZZLOG;] {
$$ = new cppast::CppVar($1, $2.toString());
}
| vartype apidecor varidentifier [ZZLOG;] {
$$ = new cppast::CppVar($1, $3.toString());
$$->apidecor($2);
}
| functionpointer [ZZLOG;] {
$$ = new cppast::CppVar($1, CppTypeModifier());
}
| vardecl '[' expr ']' [ZZLOG;] {
$$ = $1;
$$->addArraySize($3);
}
| vardecl '[' ']' [ZZLOG;] {
$$ = $1;
$$->addArraySize(nullptr);
}
| vardecl ':' expr [ZZLOG;] {