Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental: support for trailing commas #3804

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions frontends/parsers/p4/p4parser.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,9 @@ annotation
$$ = new IR::Annotation(@1, *$2, body); }
| "@" name "(" annotationBody ")"
{ $$ = new IR::Annotation(@1, *$2, *$4); }
| "@" name "[" expressionList "]"
| "@" name "[" expressionList optTrailingComma "]"
{ $$ = new IR::Annotation(@1, *$2, *$4, true); }
| "@" name "[" kvList "]"
| "@" name "[" kvList optTrailingComma "]"
{ $$ = new IR::Annotation(@1, *$2, *$4, true); }
// Experimental: backwards compatibility with P4-14 pragmas (which
// themselves are experimental!)
Expand Down Expand Up @@ -638,8 +638,8 @@ annotationToken
;

kvList
: kvPair { $$ = new IR::IndexedVector<IR::NamedExpression>; $$->push_back($1); $$->srcInfo = @1; }
| kvList "," kvPair { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3;}
: kvPair { $$ = new IR::IndexedVector<IR::NamedExpression>; $$->push_back($1); $$->srcInfo = @1; }
| kvList "," kvPair { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3;}
;

kvPair
Expand Down Expand Up @@ -826,6 +826,11 @@ tupleKeysetExpression
| "(" reducedSimpleKeysetExpression ")" { $$ = new IR::Vector<IR::Expression>(); $$->push_back($2); $$->srcInfo = @2; }
;

optTrailingComma
: %empty
| ","
;

simpleExpressionList
: simpleKeysetExpression { $$ = new IR::Vector<IR::Expression>(); $$->push_back($1); $$->srcInfo = @1; }
| simpleExpressionList "," simpleKeysetExpression { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3; }
Expand Down Expand Up @@ -1113,16 +1118,17 @@ structField
enumDeclaration
: optAnnotations
ENUM name { driver.structure->declareType(*$3); }
"{" identifierList "}" { $$ = new IR::Type_Enum(@3, *$3, *$6); }
"{" identifierList optTrailingComma "}" { $$ = new IR::Type_Enum(@3, *$3, *$6); }
| optAnnotations ENUM typeRef name { driver.structure->declareType(*$4); }
"{" specifiedIdentifierList "}" {
"{" specifiedIdentifierList optTrailingComma "}" {
auto type = $typeRef;
$$ = new IR::Type_SerEnum(@4, *$4, type, *$7);
}
;

specifiedIdentifierList
: specifiedIdentifier { $$ = new IR::IndexedVector<IR::SerEnumMember>(); $$->push_back($1); $$->srcInfo = @1; }
: specifiedIdentifier { $$ = new IR::IndexedVector<IR::SerEnumMember>();
$$->push_back($1); $$->srcInfo = @1; }
| specifiedIdentifierList "," specifiedIdentifier { $$ = $1; $1->push_back($3); $$->srcInfo = @1 + @3; }
;

Expand All @@ -1136,14 +1142,15 @@ errorDeclaration
;

matchKindDeclaration
: MATCH_KIND "{" identifierList "}"
: MATCH_KIND "{" identifierList optTrailingComma "}"
{ $$ = new IR::Declaration_MatchKind(@1 + @4, *$3); }
;

identifierList
: name { $$ = new IR::IndexedVector<IR::Declaration_ID>();
$$->push_back(new IR::Declaration_ID(@1, *$1)); $$->srcInfo = @1; }
| identifierList "," name { $$ = $1; $$->push_back(new IR::Declaration_ID(@3, *$3)); $$->srcInfo = @1 + @3; }
| identifierList "," name { $$ = $1; $$->push_back(new IR::Declaration_ID(@3, *$3));
$$->srcInfo = @1 + @3; }
;

typedefDeclaration
Expand Down Expand Up @@ -1428,11 +1435,11 @@ expression
| prefixedNonTypeName { $$ = new IR::PathExpression($1); }
| expression "[" expression "]" { $$ = new IR::ArrayIndex(@1 + @4, $1, $3); }
| expression "[" expression ":" expression "]" { $$ = new IR::Slice(@1 + @6, $1, $3, $5); }
| "{" expressionList "}" { $$ = new IR::ListExpression(@1 + @3, *$2); }
| "{" expressionList optTrailingComma "}" { $$ = new IR::ListExpression(@1 + @4, *$2); }
| "{" SHARP "}" { $$ = new IR::InvalidHeader(
@1 + @3, IR::Type::Unknown::get(), nullptr); }
| "{" kvList "}" { $$ = new IR::StructExpression(
@1 + @3, IR::Type::Unknown::get(), (IR::Type_Name*)nullptr, *$2); }
| "{" kvList optTrailingComma "}" { $$ = new IR::StructExpression(
@1 + @4, IR::Type::Unknown::get(), (IR::Type_Name*)nullptr, *$2); }
| "(" expression ")" { $$ = $2; }
| "!" expression %prec PREFIX { $$ = new IR::LNot(@1 + @2, $2); }
| "~" expression %prec PREFIX { $$ = new IR::Cmpl(@1 + @2, $2); }
Expand Down Expand Up @@ -1545,10 +1552,10 @@ intList
;

intOrStrList
: intOrStr { $$ = new IR::Vector<IR::Expression>();
: intOrStr { $$ = new IR::Vector<IR::Expression>();
$$->push_back($1);
$$->srcInfo = @1; }
| intOrStrList "," intOrStr { $$ = $1;
| intOrStrList "," intOrStr { $$ = $1;
$$->push_back($3);
$$->srcInfo = @1 + @3; }
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ structure-valued-expr-errs-2.p4(100): [--Werror=duplicate] error: f2: Duplicates
structure-valued-expr-errs-2.p4(100)
hdr.hstructs.s2 = {f2=5, f1=2, f2=5}
^^
structure-valued-expr-errs-2.p4(106):syntax error, unexpected =, expecting } or ","
structure-valued-expr-errs-2.p4(106):syntax error, unexpected =, expecting }
hdr.h2 = {2, f2=
^
[--Werror=overlimit] error: 3 errors encountered, aborting compilation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
structured-annotation-e.p4(1):syntax error, unexpected INTEGER
structured-annotation-e.p4(1):syntax error, unexpected INTEGER, expecting ]
@IllegalMixing[key=4, 5
^
[--Werror=overlimit] error: 1 errors encountered, aborting compilation
24 changes: 24 additions & 0 deletions testdata/p4_16_samples/trailing-comma.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
enum A {
X,
Y,
}

enum bit<32> X {
Z = 0,
W = 3,
}

match_kind {
new_one,
}

@annotation(2, 3,)
@annotation2[k="v",]
header H {
bit<32> f;
}

void f(out H h) {
h = { 20, };
h = { f = 10, };
}
22 changes: 22 additions & 0 deletions testdata/p4_16_samples_outputs/trailing-comma-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
enum A {
X,
Y
}

enum bit<32> X {
Z = 32w0,
W = 32w3
}

match_kind {
new_one
}

@annotation(2 , 3 ,) @annotation2[k="v"] header H {
bit<32> f;
}

void f(out H h) {
h = (H){f = 32w20};
h = (H){f = 32w10};
}
8 changes: 8 additions & 0 deletions testdata/p4_16_samples_outputs/trailing-comma-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
match_kind {
new_one
}

@annotation(2 , 3 ,) @annotation2[k="v"] header H {
bit<32> f;
}

22 changes: 22 additions & 0 deletions testdata/p4_16_samples_outputs/trailing-comma.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
enum A {
X,
Y
}

enum bit<32> X {
Z = 0,
W = 3
}

match_kind {
new_one
}

@annotation(2 , 3 ,) @annotation2[k="v"] header H {
bit<32> f;
}

void f(out H h) {
h = { 20 };
h = {f = 10};
}
1 change: 1 addition & 0 deletions testdata/p4_16_samples_outputs/trailing-comma.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[--Wwarn=missing] warning: Program does not contain a `main' module