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 } ) [ "," ] ] ")" . ```