Skip to content

Commit d3db4cf

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
[C++] Disallow trailing commas in function call argument lists in Pratt parser
According to the CEL grammar specification, trailing commas are only permitted in collection literals (lists, maps, structs), not in function call argument lists (`exprList`). Disallows trailing commas in `PrattParserWorker::ParseArguments` and adds test cases to verify syntax error reporting. PiperOrigin-RevId: 972265157
1 parent 11dba5a commit d3db4cf

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

parser/internal/pratt_parser_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,12 @@ std::vector<ErrorTestCase> GetErrorTestCases() {
12831283
" | f(1, 2\n"
12841284
" | ......^",
12851285
},
1286+
ErrorTestCase{
1287+
.source = "foo(a,b,)",
1288+
.expected_error = "ERROR: <input>:1:9: unexpected token\n"
1289+
" | foo(a,b,)\n"
1290+
" | ........^",
1291+
},
12861292
ErrorTestCase{
12871293
.source = "999999999999999999999999999999999999999",
12881294
.expected_error = "ERROR: <input>:1:1: invalid int literal\n"

parser/internal/pratt_parser_worker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ std::vector<ExprNode> PrattParserWorker<ExprNode>::ParseArguments(
852852
if (peek_token_.type == TokenType::kComma) {
853853
NextToken();
854854
if (peek_token_.type == close_token) {
855+
ReportError(peek_token_, "unexpected token");
855856
break;
856857
}
857858
continue;

parser/parser_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,17 @@ std::vector<TestInfo> test_cases = {
501501
"ERROR: <input>:1:3: expected struct field name\n"
502502
" | t{>C}\n"
503503
" | ..^"},
504+
{"foo(a,b,)", "",
505+
"ERROR: <input>:1:9: Syntax error: mismatched input ')' expecting "
506+
"{'[', '{', '(', '.', '-', '!', 'true', 'false', 'null', NUM_FLOAT, "
507+
"NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER}\n"
508+
" | foo(a,b,)\n"
509+
" | ........^",
510+
"", "", "", "",
511+
// PRATT PARSER ERROR MESSAGE
512+
"ERROR: <input>:1:9: unexpected token\n"
513+
" | foo(a,b,)\n"
514+
" | ........^"},
504515

505516
// Macro tests
506517
{"has(m.f)", "m^#2:Expr.Ident#.f~test-only~^#4:Expr.Select#", "",

0 commit comments

Comments
 (0)