mirror of
https://github.com/golang/go
synced 2024-11-16 21:04:45 -07:00
cmd/compile/internal/syntax: more tolerant parsing of import declarations
Change-Id: I114548640d51bf69833259578609901fa1602510 Reviewed-on: https://go-review.googlesource.com/c/go/+/427156 TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Run-TryBot: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
a31c062c9f
commit
a330ca5c54
@ -401,15 +401,20 @@ func (p *parser) fileOrNil() *File {
|
||||
return nil
|
||||
}
|
||||
|
||||
// { ImportDecl ";" }
|
||||
for p.got(_Import) {
|
||||
f.DeclList = p.appendGroup(f.DeclList, p.importDecl)
|
||||
p.want(_Semi)
|
||||
}
|
||||
|
||||
// { TopLevelDecl ";" }
|
||||
// Accept import declarations anywhere for error tolerance, but complain.
|
||||
// { ( ImportDecl | TopLevelDecl ) ";" }
|
||||
prev := _Import
|
||||
for p.tok != _EOF {
|
||||
if p.tok == _Import && prev != _Import {
|
||||
p.syntaxError("imports must appear before other declarations")
|
||||
}
|
||||
prev = p.tok
|
||||
|
||||
switch p.tok {
|
||||
case _Import:
|
||||
p.next()
|
||||
f.DeclList = p.appendGroup(f.DeclList, p.importDecl)
|
||||
|
||||
case _Const:
|
||||
p.next()
|
||||
f.DeclList = p.appendGroup(f.DeclList, p.constDecl)
|
||||
@ -435,7 +440,7 @@ func (p *parser) fileOrNil() *File {
|
||||
} else {
|
||||
p.syntaxError("non-declaration statement outside function body")
|
||||
}
|
||||
p.advance(_Const, _Type, _Var, _Func)
|
||||
p.advance(_Import, _Const, _Type, _Var, _Func)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -445,7 +450,7 @@ func (p *parser) fileOrNil() *File {
|
||||
|
||||
if p.tok != _EOF && !p.got(_Semi) {
|
||||
p.syntaxError("after top level declaration")
|
||||
p.advance(_Const, _Type, _Var, _Func)
|
||||
p.advance(_Import, _Const, _Type, _Var, _Func)
|
||||
}
|
||||
}
|
||||
// p.tok == _EOF
|
||||
@ -543,7 +548,7 @@ func (p *parser) importDecl(group *Group) Decl {
|
||||
return d
|
||||
}
|
||||
if !d.Path.Bad && d.Path.Kind != StringLit {
|
||||
p.syntaxError("import path must be a string")
|
||||
p.syntaxErrorAt(d.Path.Pos(), "import path must be a string")
|
||||
d.Path.Bad = true
|
||||
}
|
||||
// d.Path.Bad || d.Path.Kind == StringLit
|
||||
|
@ -7,8 +7,12 @@ package p
|
||||
import ; // ERROR missing import path
|
||||
import
|
||||
var /* ERROR missing import path */ _ int
|
||||
import .; // ERROR missing import path
|
||||
import .; // ERROR missing import path
|
||||
import 'x' // ERROR import path must be a string
|
||||
var _ int
|
||||
import /* ERROR imports must appear before other declarations */ _ "math"
|
||||
|
||||
// Don't repeat previous error for each immediately following import ...
|
||||
import ()
|
||||
import (.) // ERROR missing import path
|
||||
import (
|
||||
@ -16,4 +20,8 @@ import (
|
||||
.
|
||||
) // ERROR missing import path
|
||||
|
||||
var _ = fmt.Println // avoid imported but not used error
|
||||
// ... but remind with error again if we start a new import section after
|
||||
// other declarations
|
||||
var _ = fmt.Println
|
||||
import /* ERROR imports must appear before other declarations */ _ "math"
|
||||
import _ "math"
|
||||
|
Loading…
Reference in New Issue
Block a user