diff --git a/_examples/hello/src/main.wa b/_examples/hello/src/main.wa index da121e3e4870b42cd65e92095a7ae888d6a562ef..c4d7df568d4721d07bae1f02f3313d35d8636962 100644 --- a/_examples/hello/src/main.wa +++ b/_examples/hello/src/main.wa @@ -3,9 +3,9 @@ 导入 "fmt" import ( - "3rdparty/pkg" - "myapp/mymath" - "myapp/mypkg" + "3rdparty/pkg" => pkg + "myapp/mymath" => mymath + "myapp/mypkg" => mypkg ) 函数 main() { diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 01a311b70faf625484873adaac1b397e6197c59d..0552360c889754e75934b58e99728941b69d25b2 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -2170,14 +2170,33 @@ func (p *parser) parseImportSpec(doc *ast.CommentGroup, _ token.Token, _ int) as } else { p.expect(token.STRING) // use expect() error handling } + + // parse => asname + var arrowPos token.Pos + if ident == nil && p.tok == token.ARROW { + arrowPos = p.pos + p.next() // skip => + + switch p.tok { + case token.PERIOD: + ident = &ast.Ident{NamePos: p.pos, Name: "."} + p.next() + case token.IDENT: + ident = p.parseIdent() + default: + p.expect(token.IDENT) + } + } + p.expectSemi() // call before accessing p.linecomment // collect imports spec := &ast.ImportSpec{ - Doc: doc, - Name: ident, - Path: &ast.BasicLit{ValuePos: pos, Kind: token.STRING, Value: path}, - Comment: p.lineComment, + Doc: doc, + Name: ident, + Path: &ast.BasicLit{ValuePos: pos, Kind: token.STRING, Value: path}, + ArrowPos: arrowPos, + Comment: p.lineComment, } p.imports = append(p.imports, spec)