doc/ref/spec.md: allow `...` in an ast.File
This is consistent with allowing ... in non-schema.
Other benefits:
- code generation can generate []Decl regardless of
whether it is used for a File or StructLit.
- once we have topological sort, `...` could be used
to hint insertion points when allowed at an arbitrary
position in the Decls list (although [string]: foo could
be used for that too).
- when adopting JSON Schema semantics, ...T may
has meaning at the file level as well.
Change-Id: Icfb299f951f5531119d33aeb5d89237eb64e33a1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/5940
Reviewed-by: roger peppe <rogpeppe@gmail.com>
diff --git a/cue/ast.go b/cue/ast.go
index 1738463..fa50077 100644
--- a/cue/ast.go
+++ b/cue/ast.go
@@ -219,7 +219,7 @@
astState: v.astState,
object: obj,
}
- for _, e := range n.Decls {
+ for i, e := range n.Decls {
switch x := e.(type) {
case *ast.EmbedDecl:
if v1.object.emit == nil {
@@ -227,6 +227,11 @@
} else {
v1.object.emit = mkBin(v.ctx(), token.NoPos, opUnify, v1.object.emit, v1.walk(x.Expr))
}
+ case *ast.Ellipsis:
+ if i != len(n.Decls)-1 {
+ return v1.walk(x.Type) // Generate an error
+ }
+
default:
v1.walk(e)
}
diff --git a/doc/ref/spec.md b/doc/ref/spec.md
index d7505b6..a0af53e 100644
--- a/doc/ref/spec.md
+++ b/doc/ref/spec.md
@@ -2859,8 +2859,9 @@
it will be output instead of its enclosing file when exporting CUE
to a data format
+<!-- TODO: allow ... anywhere in SourceFile and struct. -->
```
-SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
+SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } [ "..." ] .
```
```