Skip to content

Commit c264475

Browse files
committed
cue/parser: allow attributes before package and import clauses
This is to facilitate "build tags". Change-Id: I8ea85d4f1339a3584e7763d342715b8fad2fe48f Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7062 Reviewed-by: Marcel van Lohuizen <[email protected]> Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent 4c10692 commit c264475

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

cue/parser/parser.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,11 @@ func (p *parser) parseFile() *ast.File {
16061606

16071607
var decls []ast.Decl
16081608

1609+
for p.tok == token.ATTRIBUTE {
1610+
decls = append(decls, p.parseAttribute())
1611+
p.consumeDeclComma()
1612+
}
1613+
16091614
// The package clause is not a declaration: it does not appear in any
16101615
// scope.
16111616
if p.tok == token.IDENT && p.lit == "package" {
@@ -1628,6 +1633,11 @@ func (p *parser) parseFile() *ast.File {
16281633
c.closeNode(p, pkg)
16291634
}
16301635

1636+
for p.tok == token.ATTRIBUTE {
1637+
decls = append(decls, p.parseAttribute())
1638+
p.consumeDeclComma()
1639+
}
1640+
16311641
if p.mode&packageClauseOnlyMode == 0 {
16321642
// import decls
16331643
for p.tok == token.IDENT && p.lit == "import" {

cue/parser/parser_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,20 @@ func TestParse(t *testing.T) {
409409
a: 1 @a() @b() // d
410410
`,
411411
`<[l5// d] a: 1 @a() @b()>`,
412+
}, {
413+
"attribute declarations",
414+
`
415+
@foo()
416+
417+
package bar
418+
419+
@bar()
420+
421+
import "strings"
422+
423+
@baz()
424+
`,
425+
`@foo(), package bar, @bar(), import "strings", @baz()`,
412426
}, {
413427
"comprehension comments",
414428
`

doc/ref/spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2899,7 +2899,7 @@ it will be output instead of its enclosing file when exporting CUE
28992899
to a data format
29002900

29012901
```
2902-
SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
2902+
SourceFile = { attribute "," } [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
29032903
```
29042904

29052905
```

internal/internal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ func PackageInfo(f *ast.File) (p *ast.Package, name string, tok token.Pos) {
131131
for _, d := range f.Decls {
132132
switch x := d.(type) {
133133
case *ast.CommentGroup:
134+
case *ast.Attribute:
134135
case *ast.Package:
135136
if x.Name == nil {
136137
break

0 commit comments

Comments
 (0)