-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathp4parser.ypp
More file actions
1869 lines (1642 loc) · 78 KB
/
Copy pathp4parser.ypp
File metadata and controls
1869 lines (1642 loc) · 78 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
/*
SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
SPDX-License-Identifier: Apache-2.0
*/
// Use Bison's C++ mode.
%skeleton "lalr1.cc" /* -*-C++-*- */
%require "3.0.0"
// Set up names.
%defines
%define api.namespace {P4}
%define api.parser.class {P4Parser}
// Use the C++-native variant representation of tokens.
%define api.token.constructor
%define api.value.type variant
// Add asserts to ensure that we use the API correctly.
%define parse.assert true
// Enable verbose error reporting.
%define parse.error verbose
// Declare dependencies.
%code requires {
#include <cassert> // NOLINT(build/include_order)
#include <iostream> // NOLINT(build/include_order)
#include "frontends/common/constantParsing.h"
#include "frontends/common/options.h"
#include "ir/ir.h"
#include "lib/cstring.h"
#include "lib/source_file.h"
// Ignore a warning caused by autogenerated code.
// The #if pragma is a little awkward because some preprocessors do not like ||
#pragma GCC diagnostic push
#if defined(__has_warning)
#if __has_warning("-Wunused-but-set-variable")
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
#if __has_warning("-Wswitch-enum")
#pragma GCC diagnostic ignored "-Wswitch-enum"
#endif
#else
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wswitch-enum"
#endif
namespace P4 {
class AbstractP4Lexer;
class P4ParserDriver;
// This is a workaround for an UndefinedBehaviorSanitizer issue triggered by
// Bison's variant implementation. When variant::move() is used to move a value
// from an initialized instance of variant to an uninitialized instance, it uses
// placement new to initialize the uninitialized instance, then calls
// variant::swap(), then destroys the moved-from instance. The problem is that
// placement new does not perform any initialization for primitive types, and
// for bool or enum types that can result in a value that isn't a valid element
// of those types, which UndefinedBehaviorSanitizer doesn't like.
struct OptionalConst {
OptionalConst() = default;
explicit OptionalConst(bool isConst) : isConst(isConst) { }
bool isConst = false;
};
inline std::ostream& operator<<(std::ostream& out, const OptionalConst& oc) {
out << "OptionalConst(" << oc.isConst << ')';
return out;
}
// Bison uses the types you provide to %type to make constructors for the
// variant type it uses under the hood, but its code generation is a little
// naive and it always prepends 'const' to the type. This is problematic when
// the symbol type we want is itself const, since duplicate const qualifiers are
// forbidden in C++. We avoid the problem using a typedef.
typedef const IR::Type ConstType;
struct NameTypePair {
IR::ID name;
const IR::Type *type;
};
inline std::ostream &operator<<(std::ostream &out, const NameTypePair &nt) {
return out << nt.name << ":" << nt.type;
}
} // namespace P4
#ifndef YYDEBUG
#define YYDEBUG 1
#endif
#define YY_NULLPTR nullptr
namespace P4 {
using namespace literals;
class Token {
public:
int type;
cstring text;
UnparsedConstant* unparsedConstant;
Token() : Token(0, cstring::empty, nullptr) { }
Token(int type, cstring text) : Token(type, text, nullptr) { }
Token(int type, const char* text) : Token(type, cstring(text), nullptr) { }
Token(int type, UnparsedConstant unparsedConstant)
: Token(type, unparsedConstant.text,
new UnparsedConstant(unparsedConstant)) { }
private:
Token(int type, cstring text, UnparsedConstant* unparsedConstant)
: type(type), text(text), unparsedConstant(unparsedConstant) { }
};
inline std::ostream& operator<<(std::ostream& out, const Token& t) {
out << t.text;
return out;
}
} // namespace P4
}
%param { P4::P4ParserDriver& driver }
%parse-param { P4::AbstractP4Lexer& lexer }
// Use location tracking with our custom location type.
%locations
%define api.location.type {Util::SourceInfo}
%{ /* -*-C++-*- */
#include "frontends/parsers/parserDriver.h"
#include "frontends/parsers/p4/p4lexer.hpp"
#include "frontends/parsers/p4/p4parser.hpp"
using namespace P4;
#define YYLLOC_DEFAULT(Cur, Rhs, N) \
((Cur) = (N) ? YYRHSLOC(Rhs, 1) + YYRHSLOC(Rhs, N) \
: YYRHSLOC(Rhs, 0) ? Util::SourceInfo(driver.sources, YYRHSLOC(Rhs, 0).getEnd()) \
: Util::SourceInfo(driver.sources, driver.sources->getCurrentPosition()))
#undef yylex
#define yylex lexer.yylex
%}
// Use iostreams to print all values.
%printer { yyoutput << $$; } <*>;
%define api.token.prefix {TOK_}
%token START_PROGRAM
// Lists
START_EXPRESSION_LIST
START_KV_LIST
START_INTEGER_LIST
START_INTEGER_OR_STRING_LITERAL_LIST
START_STRING_LITERAL_LIST
// Singletons
START_EXPRESSION
START_INTEGER
START_INTEGER_OR_STRING_LITERAL
START_STRING_LITERAL
// Pairs
START_EXPRESSION_PAIR
START_INTEGER_PAIR
START_STRING_LITERAL_PAIR
// Triples
START_EXPRESSION_TRIPLE
START_INTEGER_TRIPLE
START_STRING_LITERAL_TRIPLE
// P4Runtime annotations
START_P4RT_TRANSLATION_ANNOTATION // @p4runtime_translation
%token END END_ANNOTATION
///////////////////////////////////////////////////////////////////////////////
//
// Language token definitions.
//
// NB: Tokens defined here should be added to the annotationToken production.
//
///////////////////////////////////////////////////////////////////////////////
%token<Token> UNEXPECTED_TOKEN END_PRAGMA
%token<Token> LE "<="
%token<Token> GE ">="
%token<Token> SHL "<<"
%token<Token> AND "&&"
%token<Token> OR "||"
%token<Token> NE "!="
%token<Token> EQ "=="
%token<Token> PLUS "+"
%token<Token> MINUS "-"
%token<Token> PLUS_SAT "|+|"
%token<Token> MINUS_SAT "|-|"
%token<Token> MUL "*"
%token<Token> INVALID "{#}"
%token<Token> DIV "/"
%token<Token> MOD "%"
%token<Token> BIT_OR "|"
%token<Token> BIT_AND "&"
%token<Token> BIT_XOR "^"
%token<Token> COMPLEMENT "~"
%token<Token> L_BRACKET "["
%token<Token> R_BRACKET "]"
%token<Token> L_BRACE "{"
%token<Token> R_BRACE "}"
%token<Token> L_ANGLE "<"
%token<Token> L_ANGLE_ARGS // < where template args are expected
%token<Token> R_ANGLE ">"
%token<Token> R_ANGLE_SHIFT // > immediately followed by another >
%token<Token> L_PAREN "("
%token<Token> R_PAREN ")"
%token<Token> NOT "!"
%token<Token> COLON ":"
%token<Token> PLUS_COLON "+:"
%token<Token> COMMA ","
%token<Token> QUESTION "?"
%token<Token> DOT "."
%token<Token> ASSIGN "="
%token<Token> ASSIGN_PLUS "+="
%token<Token> ASSIGN_MINUS "-="
%token<Token> ASSIGN_MUL "*="
%token<Token> ASSIGN_DIV "/="
%token<Token> ASSIGN_MOD "%="
%token<Token> ASSIGN_BIT_AND "&="
%token<Token> ASSIGN_BIT_OR "|="
%token<Token> ASSIGN_XOR "^="
%token<Token> ASSIGN_SHL "<<="
%token<Token> ASSIGN_SHR ">>="
%token<Token> ASSIGN_PLUS_SAT "|+|="
%token<Token> ASSIGN_MINUS_SAT "|-|="
%token<Token> SEMICOLON ";"
%token<Token> AT "@"
%token<Token> PP "++"
%token<Token> DONTCARE "_"
%token<Token> MASK "&&&"
%token<Token> DOTS "..."
%token<Token> RANGE ".."
%token<Token> ABSTRACT ACTION ACTIONS APPLY BOOL BIT BREAK CONST CONTINUE CONTROL DEFAULT
ELSE ENTRIES ENUM ERROR EXIT EXTERN FALSE FOR HEADER HEADER_UNION IF IN INOUT
INT KEY LIST MATCH_KIND OUT PACKAGE PARSER PRAGMA PRIORITY RETURN SELECT
STATE STRING STRUCT SWITCH TABLE THIS TRANSITION TRUE TUPLE TYPE TYPEDEF
VARBIT VALUESET VOID
%token<cstring> IDENTIFIER TYPE_IDENTIFIER STRING_LITERAL
%token<UnparsedConstant> INTEGER
// End of token definitions ///////////////////////////////////////////////////
%type<IR::ID> name dot_name nonTypeName nonTableKwName annotationName
%type<NameTypePair> declarator
%type<IR::Direction> direction
%type<IR::Path*> prefixedNonTypeName prefixedType
%type<IR::TypeParameters*> optTypeParameters typeParameters typeParameterList
%type<IR::Expression*> expression lvalue keysetExpression selectExpression
stateExpression optInitializer initializer
simpleKeysetExpression transitionStatement switchLabel
p4rtControllerType reducedSimpleKeysetExpression entryPriority
nonBraceExpression nonGrtExpression forCollectionExpr
%type<ConstType*> baseType typeOrVoid specializedType arrayType
typeRef tupleType typeArg realTypeArg namedType p4listType
derivedTypeDeclarationAsType
%type<IR::Type_Name*> typeName
%type<IR::Argument*> argument
%type<IR::Vector<IR::Argument>> argumentList nonEmptyArgList
%type<IR::Parameter*> parameter
%type<OptionalConst> optCONST
%type<IR::Vector<IR::Annotation>> optAnnotations annotations
%type<IR::Annotation*> annotation
%type<IR::Vector<IR::AnnotationToken>> annotationBody
%type<Token> annotationToken
%type<IR::IndexedVector<IR::Parameter>> parameterList nonEmptyParameterList
optConstructorParameters
%type<IR::Vector<IR::Expression>> expressionList
simpleExpressionList tupleKeysetExpression
%type<IR::IndexedVector<IR::Declaration_ID>> identifierList
%type<IR::SelectCase*> selectCase
%type<IR::Vector<IR::SelectCase>> selectCaseList
%type<IR::Statement*> statement parserStatement emptyStatement returnStatement
switchStatement exitStatement
assignmentOrMethodCallStatement conditionalStatement parserConditionalStatement
assignmentOrMethodCallStatementWithoutSemicolon
forStatement breakStatement continueStatement
%type<IR::BlockStatement*> blockStatement parserBlockStatement controlBody
optObjInitializer
%type<IR::StatOrDecl*> statementOrDeclaration parserStatementOrDeclaration
declOrAssignmentOrMethodCallStatement
%type<IR::IndexedVector<IR::StatOrDecl>> objDeclarations statOrDeclList parserStatOrDeclList
forInitStatements forInitStatementsNonEmpty
forUpdateStatements forUpdateStatementsNonEmpty
%type<IR::SwitchCase*> switchCase
%type<IR::Vector<IR::SwitchCase>> switchCases
%type<IR::Vector<IR::Type>> typeArgumentList realTypeArgumentList
%type<IR::ParserState*> parserState
%type<IR::IndexedVector<IR::ParserState>> parserStates
%type<IR::Declaration*> constantDeclaration actionDeclaration
variableDeclaration variableDeclarationWithoutSemicolon instantiation functionDeclaration
objDeclaration tableDeclaration controlLocalDeclaration
parserLocalElement valueSetDeclaration
%type<IR::Type_Declaration*> headerTypeDeclaration structTypeDeclaration
headerUnionDeclaration derivedTypeDeclaration
parserDeclaration controlDeclaration enumDeclaration
typedefDeclaration packageTypeDeclaration typeDeclaration
%type<IR::Type_Error*> errorDeclaration
%type<IR::Node*> fragment
%type<IR::Node*> declaration externDeclaration matchKindDeclaration
%type<IR::Type_Parser*> parserTypeDeclaration
%type<IR::Type_Control*> controlTypeDeclaration
%type<IR::IndexedVector<IR::Declaration>> parserLocalElements controlLocalDeclarations
%type<IR::StructField*> structField
%type<IR::IndexedVector<IR::StructField>> structFieldList
%type<IR::IndexedVector<IR::SerEnumMember>> specifiedIdentifierList
%type<IR::SerEnumMember*> specifiedIdentifier
%type<IR::Method*> methodPrototype functionPrototype
%type<IR::Vector<IR::Method>> methodPrototypes
%type<IR::Property*> tableProperty
%type<IR::TableProperties*> tablePropertyList
%type<IR::KeyElement*> keyElement
%type<IR::Vector<IR::KeyElement>> keyElementList
%type<IR::Expression*> actionRef
%type<IR::IndexedVector<IR::ActionListElement>> actionList
%type<IR::Entry*> entry
%type<IR::Vector<IR::Entry>> entriesList
%type<IR::IndexedVector<IR::NamedExpression>> kvList
%type<IR::NamedExpression*> kvPair
%type<IR::Vector<IR::Expression>> intList
%type<IR::Vector<IR::Expression>> intOrStrList
%type<IR::Vector<IR::Expression>> strList
%type<IR::Expression*> intOrStr
%type<IR::P4Program*> input
// %precedence COMMA
%precedence QUESTION
%precedence COLON PLUS_COLON
%left OR
%left AND
%left EQ NE
%left L_ANGLE R_ANGLE LE GE
%left BIT_OR
%left BIT_XOR
%left BIT_AND
%left SHL R_ANGLE_SHIFT
%left PP PLUS MINUS PLUS_SAT MINUS_SAT
%left MUL DIV MOD
%precedence PREFIX
%precedence R_BRACKET L_PAREN L_BRACKET L_ANGLE_ARGS
%precedence DOT
%right THEN ELSE /* THEN is a fake token */
/*
FIXME -- we need this %destructor for correct error recovery, but because of the
broken bison C++ skeleton, it gets declarations in the wrong order and it won't
compile. Not having it (just) means that the symbol table may get confused after
a syntax error causing a spurious error cascade, which isn't too bad.
%destructor { driver.structure->pop(); } functionPrototype parserTypeDeclaration
controlTypeDeclaration
*/
%%
/*
This grammar may look weird in some places, but a lot of effort was put into
eliminating conflicts. This sometimes required having the grammar be more
lenient than necessary. Moreover, the grammar is context-sensitive, and it
needs some rudiments of type information to properly perform parsing. This
is done with the help of P4ParserDriver's "structure" object, which keeps
track of which identifiers represent types, and which represent namespaces.
*/
// Simulate multiple start symbols to allow the parser to be reused for
// annotation bodies.
start
: fragment END_ANNOTATION { driver.result = $1->getNode(); YYACCEPT; }
| START_PROGRAM program
;
fragment
// Lists
: START_EXPRESSION_LIST expressionList {
$$ = new IR::Vector<IR::Expression>(std::move($2)); }
| START_KV_LIST kvList {
$$ = new IR::IndexedVector<IR::NamedExpression>(std::move($2)); }
| START_INTEGER_LIST intList {
$$ = new IR::Vector<IR::Expression>(std::move($2)); }
| START_INTEGER_OR_STRING_LITERAL_LIST intOrStrList {
$$ = new IR::Vector<IR::Expression>(std::move($2)); }
| START_STRING_LITERAL_LIST strList {
$$ = new IR::Vector<IR::Expression>(std::move($2)); }
// Singletons
| START_EXPRESSION expression { $$ = $2; }
| START_INTEGER INTEGER { $$ = parseConstant(@2, $2, 0); }
| START_INTEGER_OR_STRING_LITERAL intOrStr { $$ = $2; }
| START_STRING_LITERAL STRING_LITERAL { $$ = new IR::StringLiteral(@2, $2); }
// Pairs
| START_EXPRESSION_PAIR expression "," expression
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back($2);
result->push_back($4);
result->srcInfo = @2 + @4;
$$ = result; }
| START_INTEGER_PAIR INTEGER "," INTEGER
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back(parseConstant(@2, $2, 0));
result->push_back(parseConstant(@4, $4, 0));
result->srcInfo = @2 + @4;
$$ = result; }
| START_STRING_LITERAL_PAIR STRING_LITERAL "," STRING_LITERAL
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back(new IR::StringLiteral(@2, $2));
result->push_back(new IR::StringLiteral(@4, $4));
result->srcInfo = @2 + @4;
$$ = result; }
// Triples
| START_EXPRESSION_TRIPLE expression "," expression "," expression
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back($2);
result->push_back($4);
result->push_back($6);
result->srcInfo = @2 + @6;
$$ = result; }
| START_INTEGER_TRIPLE INTEGER "," INTEGER "," INTEGER
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back(parseConstant(@2, $2, 0));
result->push_back(parseConstant(@4, $4, 0));
result->push_back(parseConstant(@6, $6, 0));
result->srcInfo = @2 + @6;
$$ = result; }
| START_STRING_LITERAL_TRIPLE STRING_LITERAL "," STRING_LITERAL ","
STRING_LITERAL
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back(new IR::StringLiteral(@2, $2));
result->push_back(new IR::StringLiteral(@4, $4));
result->push_back(new IR::StringLiteral(@6, $6));
result->srcInfo = @2 + @6;
$$ = result; }
// P4Runtime annotations
| START_P4RT_TRANSLATION_ANNOTATION STRING_LITERAL "," p4rtControllerType
{ auto* result = new IR::Vector<IR::Expression>();
result->push_back(new IR::StringLiteral(@2, $2));
result->push_back($4);
result->srcInfo = @2 + @4;
$$ = result; }
;
// We want this to return an `IR::Type`, but we need it to return an
// `IR::Expression` for things to type-check. We thus use `IR::StringLiteral` to
// encode the type "string", and `IR::Constant(W)` to encode the type "bit<W>".
// TODO: Could this be redesigned to avoid this hack?
p4rtControllerType
: STRING
{ $$ = new IR::StringLiteral(@1, ""_cs); }
| BIT l_angle INTEGER r_angle
{ $$ = new IR::Constant(parseConstantChecked(@3, $3)); }
// Legacy syntax; to be removed in P4RT 2.0
| INTEGER
{ $$ = new IR::Constant(parseConstantChecked(@1, $1)); }
;
program : input END { YYACCEPT; };
input
: %empty { driver.result = $$ = new IR::P4Program; }
| input declaration { if ($2) $1->objects.push_back($2->getNode());
$$ = $1; }
| input ";" { $$ = $1; } // empty declaration
;
declaration
: constantDeclaration { $$ = $1; }
| externDeclaration { $$ = $1; }
| actionDeclaration { $$ = $1; }
| parserDeclaration { $$ = $1; }
| typeDeclaration { $$ = $1; }
| controlDeclaration { $$ = $1; }
| instantiation { $$ = $1; }
| errorDeclaration { $$ = driver.onReadErrorDeclaration($1) ? $1 : nullptr; }
| matchKindDeclaration { $$ = $1; }
| functionDeclaration { $$ = $1; }
;
nonTypeName
: IDENTIFIER { $$ = IR::ID(@1, $1); }
| ACTIONS { $$ = IR::ID(@1, "actions"_cs); }
| APPLY { $$ = IR::ID(@1, "apply"_cs); }
| ENTRIES { $$ = IR::ID(@1, "entries"_cs); }
| KEY { $$ = IR::ID(@1, "key"_cs); }
| PRIORITY { $$ = IR::ID(@1, "priority"_cs); }
| STATE { $$ = IR::ID(@1, "state"_cs); }
| TYPE { $$ = IR::ID(@1, "type"_cs); }
;
name
: nonTypeName { $$ = $1; }
| LIST { $$ = IR::ID(@1, "list"_cs); }
| TYPE_IDENTIFIER { $$ = IR::ID(@1, $1); }
;
nonTableKwName
: IDENTIFIER { $$ = IR::ID(@1, $1); }
| TYPE_IDENTIFIER { $$ = IR::ID(@1, $1); }
| APPLY { $$ = IR::ID(@1, "apply"_cs); }
| PRIORITY { $$ = IR::ID(@1, "priority"_cs); }
| STATE { $$ = IR::ID(@1, "state"_cs); }
| TYPE { $$ = IR::ID(@1, "type"_cs); }
;
optCONST
: %empty { $$ = OptionalConst{false}; }
| CONST { $$ = OptionalConst{true}; }
;
optAnnotations
: %empty { $$ = {}; }
| annotations { $$ = $1; }
;
annotations
: annotation {
$$ = {};
if (!P4CContext::get().options().isAnnotationDisabled($1)) {
$$.push_back($1);
$$.srcInfo = @1;
}}
| annotations annotation {
$$ = std::move($1);
if (!P4CContext::get().options().isAnnotationDisabled($2)) {
$$.push_back($2);
$$.srcInfo = @1 + @2;
}}
;
annotation
: "@" annotationName
{ // Initialize with an empty sequence of annotation tokens so that the
// annotation node is marked as unparsed.
$$ = new IR::Annotation(@1, $2, IR::Vector<IR::AnnotationToken>()); }
| "@" annotationName "(" annotationBody ")"
{ $$ = new IR::Annotation(@1, $2, std::move($4)); }
| "@" annotationName "[" expressionList optTrailingComma "]"
{ $$ = new IR::Annotation(@1, $2, std::move($4), true); }
| "@" annotationName "[" kvList optTrailingComma "]"
{ $$ = new IR::Annotation(@1, $2, std::move($4), true); }
// Experimental: backwards compatibility with P4-14 pragmas (which
// themselves are experimental!)
| PRAGMA annotationName annotationBody END_PRAGMA
{ $$ = new IR::Annotation(@1, $2, std::move($3), false); }
;
annotationBody
: %empty { $$ = {}; }
| annotationBody "(" annotationBody ")"
{ ($$ = std::move($1)).push_back(new IR::AnnotationToken(@2, $2.type, $2.text));
$$.append($3);
$$.push_back(new IR::AnnotationToken(@4, $4.type, $4.text));
$$.srcInfo = @1 + @4;
}
| annotationBody annotationToken
{ $$ = std::move($1);
$$.push_back(new IR::AnnotationToken(@2, $2.type, $2.text, $2.unparsedConstant));
$$.srcInfo = @1 + @2;
}
;
annotationName
: ABSTRACT { $$ = IR::ID(@1, "abstract"_cs); }
| ACTION { $$ = IR::ID(@1, "action"_cs); }
| ACTIONS { $$ = IR::ID(@1, "actions"_cs); }
| APPLY { $$ = IR::ID(@1, "apply"_cs); }
| BOOL { $$ = IR::ID(@1, "bool"_cs); }
| BIT { $$ = IR::ID(@1, "bit"_cs); }
| BREAK { $$ = IR::ID(@1, "break"_cs); }
| CONST { $$ = IR::ID(@1, "const"_cs); }
| CONTINUE { $$ = IR::ID(@1, "continue"_cs); }
| CONTROL { $$ = IR::ID(@1, "control"_cs); }
| DEFAULT { $$ = IR::ID(@1, "default"_cs); }
| ELSE { $$ = IR::ID(@1, "else"_cs); }
| ENTRIES { $$ = IR::ID(@1, "entries"_cs); }
| ENUM { $$ = IR::ID(@1, "enum"_cs); }
| ERROR { $$ = IR::ID(@1, "error"_cs); }
| EXIT { $$ = IR::ID(@1, "exit"_cs); }
| EXTERN { $$ = IR::ID(@1, "extern"_cs); }
| FALSE { $$ = IR::ID(@1, "false"_cs); }
| FOR { $$ = IR::ID(@1, "for"_cs); }
| HEADER { $$ = IR::ID(@1, "header"_cs); }
| HEADER_UNION { $$ = IR::ID(@1, "header_union"_cs); }
| IF { $$ = IR::ID(@1, "if"_cs); }
| IN { $$ = IR::ID(@1, "in"_cs); }
| INOUT { $$ = IR::ID(@1, "inout"_cs); }
| INT { $$ = IR::ID(@1, "int"_cs); }
| KEY { $$ = IR::ID(@1, "key"_cs); }
| LIST { $$ = IR::ID(@1, "list"_cs); }
| MATCH_KIND { $$ = IR::ID(@1, "match_kind"_cs); }
| OUT { $$ = IR::ID(@1, "out"_cs); }
| PARSER { $$ = IR::ID(@1, "parser"_cs); }
| PACKAGE { $$ = IR::ID(@1, "package"_cs); }
| PRIORITY { $$ = IR::ID(@1, "priority"_cs); }
| RETURN { $$ = IR::ID(@1, "return"_cs); }
| SELECT { $$ = IR::ID(@1, "select"_cs); }
| STATE { $$ = IR::ID(@1, "state"_cs); }
| STRING { $$ = IR::ID(@1, "string"_cs); }
| STRUCT { $$ = IR::ID(@1, "struct"_cs); }
| SWITCH { $$ = IR::ID(@1, "switch"_cs); }
| TABLE { $$ = IR::ID(@1, "table"_cs); }
| THIS { $$ = IR::ID(@1, "this"_cs); }
| TRANSITION { $$ = IR::ID(@1, "transition"_cs); }
| TRUE { $$ = IR::ID(@1, "true"_cs); }
| TUPLE { $$ = IR::ID(@1, "tuple"_cs); }
| TYPE { $$ = IR::ID(@1, "type"_cs); }
| TYPEDEF { $$ = IR::ID(@1, "typedef"_cs); }
| VARBIT { $$ = IR::ID(@1, "varbit"_cs); }
| VALUESET { $$ = IR::ID(@1, "valueset"_cs); }
| VOID { $$ = IR::ID(@1, "void"_cs); }
| "_" { $$ = IR::ID(@1, "_"_cs); }
| IDENTIFIER { $$ = IR::ID(@1, $1); }
| TYPE_IDENTIFIER { $$ = IR::ID(@1, $1); }
;
annotationToken
: UNEXPECTED_TOKEN { $$ = $1; }
| ABSTRACT { $$ = $1; }
| ACTION { $$ = $1; }
| ACTIONS { $$ = $1; }
| APPLY { $$ = $1; }
| BOOL { $$ = $1; }
| BIT { $$ = $1; }
| BREAK { $$ = $1; }
| CONST { $$ = $1; }
| CONTINUE { $$ = $1; }
| CONTROL { $$ = $1; }
| DEFAULT { $$ = $1; }
| ELSE { $$ = $1; }
| ENTRIES { $$ = $1; }
| ENUM { $$ = $1; }
| ERROR { $$ = $1; }
| EXIT { $$ = $1; }
| EXTERN { $$ = $1; }
| FALSE { $$ = $1; }
| FOR { $$ = $1; }
| HEADER { $$ = $1; }
| HEADER_UNION { $$ = $1; }
| IF { $$ = $1; }
| IN { $$ = $1; }
| INOUT { $$ = $1; }
| INT { $$ = $1; }
| KEY { $$ = $1; }
| LIST { $$ = $1; }
| MATCH_KIND { $$ = $1; }
| OUT { $$ = $1; }
| PARSER { $$ = $1; }
| PACKAGE { $$ = $1; }
| PRAGMA { $$ = $1; }
| RETURN { $$ = $1; }
| SELECT { $$ = $1; }
| STATE { $$ = $1; }
| STRING { $$ = $1; }
| STRUCT { $$ = $1; }
| SWITCH { $$ = $1; }
| TABLE { $$ = $1; }
| THIS { $$ = $1; }
| TRANSITION { $$ = $1; }
| TRUE { $$ = $1; }
| TUPLE { $$ = $1; }
| TYPE { $$ = $1; }
| TYPEDEF { $$ = $1; }
| VARBIT { $$ = $1; }
| VALUESET { $$ = $1; }
| VOID { $$ = $1; }
| "_" { $$ = $1; }
| IDENTIFIER { $$ = Token(token::TOK_IDENTIFIER, $1); }
| TYPE_IDENTIFIER { $$ = Token(token::TOK_TYPE_IDENTIFIER, $1); }
| STRING_LITERAL { $$ = Token(token::TOK_STRING_LITERAL, $1); }
| INTEGER { $$ = Token(token::TOK_INTEGER, $1); }
| "&&&" { $$ = $1; }
| ".." { $$ = $1; }
| "<<" { $$ = $1; }
| "&&" { $$ = $1; }
| "||" { $$ = $1; }
| "==" { $$ = $1; }
| "!=" { $$ = $1; }
| ">=" { $$ = $1; }
| "<=" { $$ = $1; }
| "++" { $$ = $1; }
| "+" { $$ = $1; }
| "|+|" { $$ = $1; }
| "-" { $$ = $1; }
| "|-|" { $$ = $1; }
| "*" { $$ = $1; }
| "/" { $$ = $1; }
| "%" { $$ = $1; }
| "|" { $$ = $1; }
| "&" { $$ = $1; }
| "^" { $$ = $1; }
| "~" { $$ = $1; }
// Omit parens. These are handled in annotationBody, since they must be
// balanced.
// | "(" { $$ = $1; }
// | ")" { $$ = $1; }
| "[" { $$ = $1; }
| "]" { $$ = $1; }
| "{" { $$ = $1; }
| "}" { $$ = $1; }
| "<" { $$ = $1; }
| L_ANGLE_ARGS { $$ = $1; }
| ">" { $$ = $1; }
| R_ANGLE_SHIFT { $$ = $1; }
| "!" { $$ = $1; }
| ":" { $$ = $1; }
| "," { $$ = $1; }
| "?" { $$ = $1; }
| "." { $$ = $1; }
| "=" { $$ = $1; }
| ";" { $$ = $1; }
| "@" { $$ = $1; }
;
kvList
: kvPair { $$ = {}; $$.push_back($1); $$.srcInfo = @1; }
| kvList "," kvPair { ($$ = std::move($1)).push_back($3); $$.srcInfo = @1 + @3;}
;
kvPair
: name "=" expression { $$ = new IR::NamedExpression(@1, $1, $3); }
;
parameterList
: %empty { $$ = {}; }
| nonEmptyParameterList { $$ = $1; }
;
nonEmptyParameterList
: parameter { $$ = {}; $$.push_back($1); $$.srcInfo = @1; }
| nonEmptyParameterList "," parameter { ($$ = std::move($1)).push_back($3); $$.srcInfo = @1 + @3; }
;
parameter
: optAnnotations direction typeRef declarator {
$$ = new IR::Parameter(@4, $4.name, $1, $2, $4.type, nullptr); }
| optAnnotations direction typeRef declarator "=" expression {
$$ = new IR::Parameter(@4, $4.name, $1, $2, $4.type, $6); }
;
direction
: IN { $$ = IR::Direction::In; }
| INOUT { $$ = IR::Direction::InOut; }
| OUT { $$ = IR::Direction::Out; }
| %empty { $$ = IR::Direction::None; }
;
packageTypeDeclaration
: optAnnotations PACKAGE name { driver.structure->pushContainerType($3, false); }
optTypeParameters {
if (!$5->empty()) driver.structure->markAsTemplate($3);
driver.structure->declareTypes($5->parameters); }
"(" parameterList ")" {
driver.structure->declareParameters($8);
auto pl = new IR::ParameterList(@8, $8);
$$ = new IR::Type_Package(@3, $3, std::move($1), $5, pl); }
;
instantiation
: annotations typeRef "(" argumentList ")" <ConstType*>{ $$ = $2; } declarator optObjInitializer ";"
{ $$ = new IR::Declaration_Instance(@7, $7.name, std::move($1), $7.type, std::move($4), $8);
driver.structure->declareObject($7.name, $2->toString()); }
| typeRef "(" argumentList ")" <ConstType*>{ $$ = $1; } declarator optObjInitializer ";"
{ $$ = new IR::Declaration_Instance(@6, $6.name, $6.type, std::move($3), $7);
driver.structure->declareObject($6.name, $1->toString()); }
;
optObjInitializer
: %empty { $$ = nullptr; }
| "=" "{" { driver.structure->pushNamespace(@1, false); } objDeclarations "}"
{ driver.structure->pop();
$$ = new IR::BlockStatement(@2+@5, std::move($4)); }
;
objDeclarations
: %empty { $$ = {}; }
| objDeclarations objDeclaration { ($$ = std::move($1)).push_back($2); $$.srcInfo = @1 + @2;}
;
objDeclaration
: functionDeclaration { $$ = $1; }
| instantiation { $$ = $1; }
;
optConstructorParameters
: %empty { $$ = {}; }
| "(" parameterList ")" { $$ = $2; }
;
dotPrefix
: "." { driver.structure->startAbsolutePath(); }
;
/**************************** PARSER ******************************/
parserDeclaration
: parserTypeDeclaration optConstructorParameters
"{" parserLocalElements parserStates "}"
{ driver.structure->pop();
auto pl = new IR::ParameterList(@2, std::move($2));
$$ = new IR::P4Parser($1->name.srcInfo, $1->name,
$1, pl, std::move($4), std::move($5));}
;
parserLocalElements
: %empty { $$ = {}; }
| parserLocalElements parserLocalElement { ($$ = std::move($1)).push_back($2); $$.srcInfo = @1 + @2; }
;
parserLocalElement
: constantDeclaration { $$ = $1; }
| instantiation { $$ = $1; }
| variableDeclaration { $$ = $1; }
| valueSetDeclaration { $$ = $1; }
;
parserTypeDeclaration
: optAnnotations
PARSER name { driver.structure->pushContainerType($3, true); }
optTypeParameters { if (!$5->empty()) driver.structure->markAsTemplate($3);
driver.structure->declareTypes($5->parameters); }
"(" parameterList ")" {
driver.structure->declareParameters($8);
auto pl = new IR::ParameterList(@8, std::move($8));
$$ = new IR::Type_Parser(@3, std::move($3), std::move($1), $5, pl); }
;
parserStates
: parserState { $$ = {}; $$.push_back($1); $$.srcInfo = @1; }
| parserStates parserState { ($$ = std::move($1)).push_back($2); $$.srcInfo = @1 + @2; }
;
parserState
: optAnnotations STATE name { driver.structure->pushContainerType($3, false); }
"{" parserStatOrDeclList transitionStatement "}" {
driver.structure->pop();
$$ = new IR::ParserState(@3, std::move($3), std::move($1), std::move($6), $7); }
;
parserStatement
: assignmentOrMethodCallStatement { $$ = $1; }
| emptyStatement { $$ = $1; }
| parserBlockStatement { $$ = $1; }
| parserConditionalStatement { $$ = $1; }
;
parserStatementOrDeclaration
: variableDeclaration { $$ = $1; }
| constantDeclaration { $$ = $1; }
| parserStatement { $$ = $1; }
;
parserStatOrDeclList
: %empty { $$ = {}; }
| parserStatOrDeclList parserStatementOrDeclaration { ($$ = std::move($1)).push_back($2);
$$.srcInfo = @1 + @2; }
;
parserBlockStatement
: optAnnotations "{" { driver.structure->pushNamespace(@2, false); }
parserStatOrDeclList "}" { driver.structure->pop();
$$ = new IR::BlockStatement(@1+@5, std::move($1), std::move($4)); }
;
parserConditionalStatement
: IF "(" expression ")" parserStatement %prec THEN
{ $$ = new IR::IfStatement(@1, $3, $5, nullptr); }
| IF "(" expression ")" parserStatement ELSE parserStatement %prec THEN
{ $$ = new IR::IfStatement(@1, $3, $5, $7); }
;
transitionStatement
: %empty { $$ = nullptr; }
| TRANSITION stateExpression { $$ = $2; }
;
stateExpression
: name ";" { $$ = new IR::PathExpression($1); }
| selectExpression { $$ = $1; }
;
selectExpression
: SELECT "(" expressionList ")" "{" selectCaseList "}" {
$$ = new IR::SelectExpression(@1 + @7,
new IR::ListExpression(@3, std::move($3)), std::move($6)); }
;
selectCaseList
: %empty { $$ = {}; }
| selectCaseList selectCase { ($$ = std::move($1)).push_back($2); $$.srcInfo = @1 + @2;}
;
selectCase
: keysetExpression ":" name ";" { auto expr = new IR::PathExpression($3);
$$ = new IR::SelectCase(@1 + @3, $1, expr); }
;
keysetExpression
: tupleKeysetExpression { $$ = new IR::ListExpression(@1, std::move($1)); }
| simpleKeysetExpression { $$ = $1; }
;
tupleKeysetExpression
: "(" simpleKeysetExpression "," simpleExpressionList ")" {
$$ = std::move($4);
$$.insert($$.begin(), $2);
$$.srcInfo = @2 + @4; }
| "(" reducedSimpleKeysetExpression ")" { $$ = {}; $$.push_back($2); $$.srcInfo = @2; }
;
optTrailingComma
: %empty
| ","
;
simpleExpressionList
: simpleKeysetExpression { $$ = {}; $$.push_back($1); $$.srcInfo = @1; }
| simpleExpressionList "," simpleKeysetExpression { ($$ = std::move($1)).push_back($3);
$$.srcInfo = @1 + @3; }
;
reducedSimpleKeysetExpression
// like simpleKeysetExpression, but without 'expression', to avoid a conflict
: expression "&&&" expression { $$ = new IR::Mask(@1 + @3, $1, $3); }
| expression ".." expression { $$ = new IR::Range(@1 + @3, $1, $3); }
| DEFAULT { $$ = new IR::DefaultExpression(@1); }
| "_" { $$ = new IR::DefaultExpression(@1); }
;
simpleKeysetExpression
: expression { $$ = $1; }
| expression "&&&" expression { $$ = new IR::Mask(@1 + @3, $1, $3); }
| expression ".." expression { $$ = new IR::Range(@1 + @3, $1, $3); }
| DEFAULT { $$ = new IR::DefaultExpression(@1); }
| "_" { $$ = new IR::DefaultExpression(@1); }
;
valueSetDeclaration
: optAnnotations VALUESET l_angle baseType r_angle "(" expression ")" name ";" {
$$ = new IR::P4ValueSet(@9, std::move($9), std::move($1), $4, $7); }
| optAnnotations VALUESET l_angle tupleType r_angle "(" expression ")" name ";" {
$$ = new IR::P4ValueSet(@9, std::move($9), std::move($1), $4, $7); }
| optAnnotations VALUESET l_angle typeName r_angle "(" expression ")" name ";" {
$$ = new IR::P4ValueSet(@9, std::move($9), std::move($1), $4, $7); }
;
/*************************** CONTROL ************************/
controlDeclaration
: controlTypeDeclaration optConstructorParameters
"{" controlLocalDeclarations APPLY controlBody "}"
{ driver.structure->pop();
auto pl = new IR::ParameterList(@2, std::move($2));
$$ = new IR::P4Control($1->name.srcInfo, $1->name, $1, pl, std::move($4), $6); }
;
controlTypeDeclaration
: optAnnotations
CONTROL name { driver.structure->pushContainerType($3, true); }
optTypeParameters { if (!$5->empty()) driver.structure->markAsTemplate($3);
driver.structure->declareTypes($5->parameters); }
"(" parameterList ")" { driver.structure->declareParameters($8);
auto pl = new IR::ParameterList(@8, std::move($8));
$$ = new IR::Type_Control(@3, std::move($3), std::move($1), $5, pl); }
;
controlLocalDeclarations
: %empty { $$ = {}; }
| controlLocalDeclarations controlLocalDeclaration { ($$ = std::move($1)).push_back($2); $$.srcInfo = @1 + @2; }
;
controlLocalDeclaration
: constantDeclaration { $$ = $1; }