Skip to content

Commit afdde34

Browse files
committed
cue/parser: use more robust logic for detecting the end of an interpolation
The current logic for parsing a string interpolation assumes that an open-parenthesis implies an expression inside the interpolation, but that's not necessarily the case and the current logic panics when that's not the case (although that panic is hidden by the `defer` logic in the parser). Fix the logic by checking the end of the string segment: it can only be an open-paren when the string starts a new interpolation expression. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Ib9cc874a78aec455b9597d8387cd5e0f20885f82 Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1223785 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 276bcce commit afdde34

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

cue/parser/parser.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,9 @@ func (p *parser) parseInterpolation() (expr ast.Expr) {
15951595
last := &ast.BasicLit{ValuePos: pos, Kind: token.STRING, Value: lit}
15961596
exprs := []ast.Expr{last}
15971597

1598-
for p.tok == token.LPAREN {
1598+
// Note: we can only tell if the string returned by ResumeInterpolation
1599+
// starts a new interpolated expression by whether it ends in a parenthesis.
1600+
for strings.HasSuffix(last.Value, "(") {
15991601
c.pos = 1
16001602
p.expect(token.LPAREN)
16011603
cc.closeExpr(p, last)

cue/parser/parser_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,7 @@ cannot import package as definition identifier`,
572572
{
573573
desc: "paren after interpolation",
574574
in: `"\(1)"()`,
575-
// Note: this actually masks an out-of-bounds slice index panic
576-
// in scanner.Scanner.popInterpolation.
577-
out: "\nexpected operand, found ')' (and 1 more errors)",
575+
out: `"\(1)"()`,
578576
},
579577
{
580578
desc: "file comments",

0 commit comments

Comments
 (0)