From d58314c631eb5f0925b4151187875e9c55f55b34 Mon Sep 17 00:00:00 2001 From: Mihai Budiu Date: Thu, 22 Dec 2022 17:26:24 -0800 Subject: [PATCH 1/2] Experimental: support for trailing commas Signed-off-by: Mihai Budiu --- frontends/parsers/p4/p4parser.ypp | 35 +++++++++++-------- testdata/p4_16_samples/trailing-comma.p4 | 24 +++++++++++++ .../trailing-comma-first.p4 | 22 ++++++++++++ .../trailing-comma-frontend.p4 | 8 +++++ .../p4_16_samples_outputs/trailing-comma.p4 | 22 ++++++++++++ .../trailing-comma.p4-stderr | 1 + 6 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 testdata/p4_16_samples/trailing-comma.p4 create mode 100644 testdata/p4_16_samples_outputs/trailing-comma-first.p4 create mode 100644 testdata/p4_16_samples_outputs/trailing-comma-frontend.p4 create mode 100644 testdata/p4_16_samples_outputs/trailing-comma.p4 create mode 100644 testdata/p4_16_samples_outputs/trailing-comma.p4-stderr diff --git a/frontends/parsers/p4/p4parser.ypp b/frontends/parsers/p4/p4parser.ypp index 8c494fa87b9..b4963f1fdff 100644 --- a/frontends/parsers/p4/p4parser.ypp +++ b/frontends/parsers/p4/p4parser.ypp @@ -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!) @@ -638,8 +638,8 @@ annotationToken ; kvList - : kvPair { $$ = new IR::IndexedVector; $$->push_back($1); $$->srcInfo = @1; } - | kvList "," kvPair { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3;} + : kvPair { $$ = new IR::IndexedVector; $$->push_back($1); $$->srcInfo = @1; } + | kvList "," kvPair { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3;} ; kvPair @@ -826,6 +826,11 @@ tupleKeysetExpression | "(" reducedSimpleKeysetExpression ")" { $$ = new IR::Vector(); $$->push_back($2); $$->srcInfo = @2; } ; +optTrailingComma + : %empty + | "," + ; + simpleExpressionList : simpleKeysetExpression { $$ = new IR::Vector(); $$->push_back($1); $$->srcInfo = @1; } | simpleExpressionList "," simpleKeysetExpression { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3; } @@ -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(); $$->push_back($1); $$->srcInfo = @1; } + : specifiedIdentifier { $$ = new IR::IndexedVector(); + $$->push_back($1); $$->srcInfo = @1; } | specifiedIdentifierList "," specifiedIdentifier { $$ = $1; $1->push_back($3); $$->srcInfo = @1 + @3; } ; @@ -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(); $$->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 @@ -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); } @@ -1545,10 +1552,10 @@ intList ; intOrStrList - : intOrStr { $$ = new IR::Vector(); + : intOrStr { $$ = new IR::Vector(); $$->push_back($1); $$->srcInfo = @1; } - | intOrStrList "," intOrStr { $$ = $1; + | intOrStrList "," intOrStr { $$ = $1; $$->push_back($3); $$->srcInfo = @1 + @3; } ; diff --git a/testdata/p4_16_samples/trailing-comma.p4 b/testdata/p4_16_samples/trailing-comma.p4 new file mode 100644 index 00000000000..00977b3edb5 --- /dev/null +++ b/testdata/p4_16_samples/trailing-comma.p4 @@ -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, }; +} \ No newline at end of file diff --git a/testdata/p4_16_samples_outputs/trailing-comma-first.p4 b/testdata/p4_16_samples_outputs/trailing-comma-first.p4 new file mode 100644 index 00000000000..00f24578a8f --- /dev/null +++ b/testdata/p4_16_samples_outputs/trailing-comma-first.p4 @@ -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}; +} diff --git a/testdata/p4_16_samples_outputs/trailing-comma-frontend.p4 b/testdata/p4_16_samples_outputs/trailing-comma-frontend.p4 new file mode 100644 index 00000000000..407989e686f --- /dev/null +++ b/testdata/p4_16_samples_outputs/trailing-comma-frontend.p4 @@ -0,0 +1,8 @@ +match_kind { + new_one +} + +@annotation(2 , 3 ,) @annotation2[k="v"] header H { + bit<32> f; +} + diff --git a/testdata/p4_16_samples_outputs/trailing-comma.p4 b/testdata/p4_16_samples_outputs/trailing-comma.p4 new file mode 100644 index 00000000000..82279b922ac --- /dev/null +++ b/testdata/p4_16_samples_outputs/trailing-comma.p4 @@ -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}; +} diff --git a/testdata/p4_16_samples_outputs/trailing-comma.p4-stderr b/testdata/p4_16_samples_outputs/trailing-comma.p4-stderr new file mode 100644 index 00000000000..7e57a518ffd --- /dev/null +++ b/testdata/p4_16_samples_outputs/trailing-comma.p4-stderr @@ -0,0 +1 @@ +[--Wwarn=missing] warning: Program does not contain a `main' module From c1259541c844708563e9713896a71b7cded2fd3f Mon Sep 17 00:00:00 2001 From: Mihai Budiu Date: Thu, 22 Dec 2022 19:01:37 -0800 Subject: [PATCH 2/2] Changed error messages Signed-off-by: Mihai Budiu --- .../p4_16_errors_outputs/structure-valued-expr-errs-2.p4-stderr | 2 +- testdata/p4_16_errors_outputs/structured-annotation-e.p4-stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/testdata/p4_16_errors_outputs/structure-valued-expr-errs-2.p4-stderr b/testdata/p4_16_errors_outputs/structure-valued-expr-errs-2.p4-stderr index 5b8f7fea938..567f7901296 100644 --- a/testdata/p4_16_errors_outputs/structure-valued-expr-errs-2.p4-stderr +++ b/testdata/p4_16_errors_outputs/structure-valued-expr-errs-2.p4-stderr @@ -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 diff --git a/testdata/p4_16_errors_outputs/structured-annotation-e.p4-stderr b/testdata/p4_16_errors_outputs/structured-annotation-e.p4-stderr index 8b82c979206..7898178c450 100644 --- a/testdata/p4_16_errors_outputs/structured-annotation-e.p4-stderr +++ b/testdata/p4_16_errors_outputs/structured-annotation-e.p4-stderr @@ -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