From fa44ce4a51f3ec7fb4eb0e1afb061d950fa2506f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 28 May 2026 17:20:22 +0100 Subject: [PATCH] cue/parser: allow a newline before the closing bracket of an index expression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per the existing comma insertion rules, a newline after an expression token inserts a comma. Index expressions did not allow a trailing comma, so splitting an index expression across multiple lines failed to parse: bar[ "baz" ] would report "expected ']', found newline". Amend the Index grammar to permit an optional trailing comma, matching both Go's grammar and the trailing-comma allowance already present in list and argument literals. This lets users split long index expressions across lines for readability without surprising errors. Fixes #2738. Signed-off-by: Daniel Martí Change-Id: Ibdf11f78d16deac90d8d27ab7b66b9138980ce0d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1238111 TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine Reviewed-by: Marcel van Lohuizen --- cue/format/testdata/expressions.txtar | 28 +++++++++++++++++++++++++++ cue/parser/parser.go | 3 +++ cue/parser/parser_test.go | 12 ++++++++++++ doc/ref/spec.md | 2 +- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cue/format/testdata/expressions.txtar b/cue/format/testdata/expressions.txtar index 02f79e0c6..063d14833 100644 --- a/cue/format/testdata/expressions.txtar +++ b/cue/format/testdata/expressions.txtar @@ -282,6 +282,20 @@ import "list" r: b & [1, 2, {a: 4}] s: [string]: [string]: a s: [string]: {s: string} + + // Index expressions, including multi-line forms allowed via the + // optional trailing comma — see cuelang.org/issue/2738. + idx1: bar["baz"] + idx2: bar["baz",] + idx3: bar[ + "baz" + ] + idx4: bar[ + "baz", + ] + idx5: bar[foo[ + "baz" + ]] } -- out/format/expressions.golden -- package expressions @@ -564,6 +578,20 @@ import "list" r: b & [1, 2, {a: 4}] s: [string]: [string]: a s: [string]: {s: string} + + // Index expressions, including multi-line forms allowed via the + // optional trailing comma — see cuelang.org/issue/2738. + idx1: bar["baz"] + idx2: bar["baz"] + idx3: bar[ + "baz" + ] + idx4: bar[ + "baz" + ] + idx5: bar[foo[ + "baz" + ]] } -- issue2496.input -- machine_type: [ diff --git a/cue/parser/parser.go b/cue/parser/parser.go index 771b12d11..8be14bf58 100644 --- a/cue/parser/parser.go +++ b/cue/parser/parser.go @@ -717,6 +717,9 @@ func (p *parser) parseIndexOrSlice(x ast.Expr) (expr ast.Expr) { index[nColons] = p.parseRHS() } } + if nColons == 0 && p.tok == token.COMMA { + p.next() + } p.exprLev-- rbrack := p.expect(token.RBRACK) diff --git a/cue/parser/parser_test.go b/cue/parser/parser_test.go index 18184a5e0..40bee274e 100644 --- a/cue/parser/parser_test.go +++ b/cue/parser/parser_test.go @@ -700,6 +700,18 @@ missing ',' in struct literal (and 1 more errors)`, }`, out: `@experiment(try), {a: {try {x: list[0]?}}}`, }, + { + desc: "index expression with trailing comma", + in: `a: b["x",]`, + out: `a: b["x"]`, + }, + { + desc: "index expression with newline before closing bracket", + in: `a: b[ + "x" + ]`, + out: `a: b["x"]`, + }, { desc: "multiple else clauses error", version: "v0.16.0", diff --git a/doc/ref/spec.md b/doc/ref/spec.md index 70f3a82db..6a6e6fd63 100644 --- a/doc/ref/spec.md +++ b/doc/ref/spec.md @@ -2067,7 +2067,7 @@ PrimaryExpr = PrimaryExpr Arguments . Selector = "." (identifier | simple_string_lit) . -Index = "[" Expression "]" . +Index = "[" Expression [ "," ] "]" . Argument = Expression . Arguments = "(" [ ( Argument { "," Argument } ) [ "," ] ] ")" . ``` -- 2.51.2