"go/internal/typeparams"
"go/scanner"
"go/token"
- "strconv"
- "strings"
- "unicode"
)
// The parser structure holds the parser's internal state.
type parseSpecFunction func(doc *ast.CommentGroup, pos token.Pos, keyword token.Token, iota int) ast.Spec
-func isValidImport(lit string) bool {
- const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
- s, _ := strconv.Unquote(lit) // go/scanner returns a legal string literal
- for _, r := range s {
- if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
- return false
- }
- }
- return s != ""
-}
-
func (p *parser) parseImportSpec(doc *ast.CommentGroup, _ token.Pos, _ token.Token, _ int) ast.Spec {
if p.trace {
defer un(trace(p, "ImportSpec"))
var path string
if p.tok == token.STRING {
path = p.lit
- if !isValidImport(path) {
- p.error(pos, "invalid import path: "+path)
- }
p.next()
} else {
p.expect(token.STRING) // use expect() error handling
}
}
-var imports = map[string]bool{
- `"a"`: true,
- "`a`": true,
- `"a/b"`: true,
- `"a.b"`: true,
- `"m\x61th"`: true,
- `"greek/αβ"`: true,
- `""`: false,
-
- // Each of these pairs tests both `` vs "" strings
- // and also use of invalid characters spelled out as
- // escape sequences and written directly.
- // For example `"\x00"` tests import "\x00"
- // while "`\x00`" tests import `<actual-NUL-byte>`.
- `"\x00"`: false,
- "`\x00`": false,
- `"\x7f"`: false,
- "`\x7f`": false,
- `"a!"`: false,
- "`a!`": false,
- `"a b"`: false,
- "`a b`": false,
- `"a\\b"`: false,
- "`a\\b`": false,
- "\"`a`\"": false,
- "`\"a\"`": false,
- `"\x80\x80"`: false,
- "`\x80\x80`": false,
- `"\xFFFD"`: false,
- "`\xFFFD`": false,
-}
-
-func TestImports(t *testing.T) {
- for path, isValid := range imports {
- src := fmt.Sprintf("package p; import %s", path)
- _, err := ParseFile(token.NewFileSet(), "", src, 0)
- switch {
- case err != nil && isValid:
- t.Errorf("ParseFile(%s): got %v; expected no error", src, err)
- case err == nil && !isValid:
- t.Errorf("ParseFile(%s): got no error; expected one", src)
- }
- }
-}
-
func TestCommentGroups(t *testing.T) {
f, err := ParseFile(token.NewFileSet(), "", `
package p /* 1a */ /* 1b */ /* 1c */ // 1d
import . "fmt" // declares Println in file scope
import (
- // TODO(gri) At the moment, 2 errors are reported because both go/parser
- // and the type checker report it. Eventually, this test should not be
- // done by the parser anymore.
- "" /* ERROR invalid import path */ /* ERROR invalid import path */
- "a!b" /* ERROR invalid import path */ /* ERROR invalid import path */
- "abc\xffdef" /* ERROR invalid import path */ /* ERROR invalid import path */
+ "" /* ERROR invalid import path */
+ "a!b" /* ERROR invalid import path */
+ "abc\xffdef" /* ERROR invalid import path */
)
// using "math" in this file doesn't affect its use in other files