Skip to content

Commit

Permalink
cue/parser: allow attributes before package and import clauses
Browse files Browse the repository at this point in the history
This is to facilitate "build tags".

Change-Id: I8ea85d4f1339a3584e7763d342715b8fad2fe48f
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7062
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
  • Loading branch information
mpvl committed Sep 15, 2020
1 parent 4c10692 commit c264475
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
10 changes: 10 additions & 0 deletions cue/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,11 @@ func (p *parser) parseFile() *ast.File {

var decls []ast.Decl

for p.tok == token.ATTRIBUTE {
decls = append(decls, p.parseAttribute())
p.consumeDeclComma()
}

// The package clause is not a declaration: it does not appear in any
// scope.
if p.tok == token.IDENT && p.lit == "package" {
Expand All @@ -1628,6 +1633,11 @@ func (p *parser) parseFile() *ast.File {
c.closeNode(p, pkg)
}

for p.tok == token.ATTRIBUTE {
decls = append(decls, p.parseAttribute())
p.consumeDeclComma()
}

if p.mode&packageClauseOnlyMode == 0 {
// import decls
for p.tok == token.IDENT && p.lit == "import" {
Expand Down
14 changes: 14 additions & 0 deletions cue/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ func TestParse(t *testing.T) {
a: 1 @a() @b() // d
`,
`<[l5// d] a: 1 @a() @b()>`,
}, {
"attribute declarations",
`
@foo()
package bar
@bar()
import "strings"
@baz()
`,
`@foo(), package bar, @bar(), import "strings", @baz()`,
}, {
"comprehension comments",
`
Expand Down
2 changes: 1 addition & 1 deletion doc/ref/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -2899,7 +2899,7 @@ it will be output instead of its enclosing file when exporting CUE
to a data format

```
SourceFile = [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
SourceFile = { attribute "," } [ PackageClause "," ] { ImportDecl "," } { Declaration "," } .
```

```
Expand Down
1 change: 1 addition & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func PackageInfo(f *ast.File) (p *ast.Package, name string, tok token.Pos) {
for _, d := range f.Decls {
switch x := d.(type) {
case *ast.CommentGroup:
case *ast.Attribute:
case *ast.Package:
if x.Name == nil {
break
Expand Down

0 comments on commit c264475

Please sign in to comment.