cue/parser: bug fix: allow embeddings on one line
Change-Id: I9567c3d059eda27c26d5aae9b2507595fc418c3e
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2956
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/parser/parser.go b/cue/parser/parser.go
index 840be6d..31eb2e5 100644
--- a/cue/parser/parser.go
+++ b/cue/parser/parser.go
@@ -707,7 +707,6 @@
p.next()
}
- _ = multipleLabels
if p.tok == token.COLON || p.tok == token.ISA {
if p.tok == token.ISA && multipleLabels {
p.errf(p.pos, "more than one label before '::' (only one allowed)")
@@ -723,23 +722,28 @@
// allowComprehension = false
switch p.tok {
- default:
- if p.tok != token.COMMA {
- p.errorExpected(p.pos, "label or ':'")
- }
- switch tok {
- case token.IDENT, token.LBRACK, token.STRING, token.INTERPOLATION, token.NULL, token.TRUE, token.FALSE:
- if p.tok == token.COMMA {
- p.expectComma()
- return &ast.EmbedDecl{Expr: expr}
- }
- }
- return &ast.BadDecl{From: pos, To: p.pos}
-
case token.IDENT, token.STRING, token.LSS, token.INTERPOLATION, token.LBRACK:
field := &ast.Field{}
m.Value = &ast.StructLit{Elts: []ast.Decl{field}}
m = field
+
+ case token.COMMA:
+ p.expectComma() // sync parser.
+ fallthrough
+
+ case token.RBRACE:
+ if i == 0 {
+ switch tok {
+ case token.IDENT, token.LBRACK, token.STRING, token.INTERPOLATION,
+ token.NULL, token.TRUE, token.FALSE:
+ return &ast.EmbedDecl{Expr: expr}
+ }
+ }
+ fallthrough
+
+ default:
+ p.errorExpected(p.pos, "label or ':'")
+ return &ast.BadDecl{From: pos, To: p.pos}
}
}
diff --git a/cue/parser/parser_test.go b/cue/parser/parser_test.go
index c73ec5a..a4fd97f 100644
--- a/cue/parser/parser_test.go
+++ b/cue/parser/parser_test.go
@@ -92,6 +92,10 @@
`,
`Def :: {b: "2", c: 3, embedding}`,
}, {
+ "one-line embedding",
+ `{ V1, V2 }`,
+ `{V1, V2}`,
+ }, {
"ellipsis in structs",
`Def :: {
b: "2"
@@ -524,3 +528,14 @@
})
}
}
+
+// For debugging, do not delete.
+func TestX(t *testing.T) {
+ t.Skip()
+
+ f, err := ParseFile("input", ``)
+ if err != nil {
+ t.Errorf("unexpected error: %v", err)
+ }
+ t.Error(debugStr(f))
+}
diff --git a/cue/parser/short_test.go b/cue/parser/short_test.go
index 6916135..6e5e428 100644
--- a/cue/parser/short_test.go
+++ b/cue/parser/short_test.go
@@ -36,7 +36,6 @@
func TestInvalid(t *testing.T) {
invalids := []string{
`foo !/* ERROR "expected label or ':', found '!'" */`,
- // `foo: /* ERROR "expected operand, found '}'" */}`, // TODO: wrong position
`{ <Name
/* ERROR "expected '>', found newline" */ >: foo }`,
// TODO: