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:
Robert Griesemer 2022-08-31 12:14:58 -07:00 committed by Gopher Robot
parent a31c062c9f
commit a330ca5c54
2 changed files with 25 additions and 12 deletions

View file

@ -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

View file

@ -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"