From cd5d785bae029dbfc2addc0a9d05512c1263faf0 Mon Sep 17 00:00:00 2001 From: chai2010 Date: Sun, 28 Aug 2022 00:23:55 +0800 Subject: [PATCH] =?UTF-8?q?import=20=E6=94=AF=E6=8C=81=E5=8F=AF=E9=80=89?= =?UTF-8?q?=20=3D>=20=E7=AC=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _examples/hello/src/main.wa | 6 +++--- internal/parser/parser.go | 27 +++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/_examples/hello/src/main.wa b/_examples/hello/src/main.wa index da121e3..c4d7df5 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 01a311b..0552360 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) -- GitLab