github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgGoSource/kmgGoParser/import.go (about) 1 package kmgGoParser 2 3 import "github.com/bronze1man/kmg/kmgGoSource/kmgGoReader" 4 5 // 读取一个import语法里面的数据,此处从import关键词开始 6 func (gofile *File) readImport(r *kmgGoReader.Reader) { 7 r.MustReadMatch([]byte("import")) 8 r.ReadAllSpace() 9 if r.IsEof() { 10 panic(r.GetFileLineInfo() + " unexcept EOF ") 11 } 12 b := r.ReadByte() 13 if b == '(' { 14 for { 15 r.ReadAllSpace() 16 b := r.ReadByte() 17 if b == ')' { 18 return 19 } else { 20 r.UnreadByte() 21 gofile.readImportSpec(r) 22 } 23 } 24 } else { 25 r.UnreadByte() 26 gofile.readImportSpec(r) 27 } 28 } 29 30 // 此处暂时只保留路径,其他数据抛弃. 31 func (gofile *File) readImportSpec(r *kmgGoReader.Reader) { 32 r.ReadAllSpace() 33 b := r.ReadByte() 34 //fmt.Println(b,string(rune(b))) 35 if b == '"' || b == '`' { 36 r.UnreadByte() 37 gofile.AddImport(string(MustReadGoString(r)), "") 38 } else if b == '.' { 39 r.ReadAllSpace() 40 gofile.AddImport(string(MustReadGoString(r)), ".") 41 } else { 42 r.UnreadByte() 43 alias := readIdentifier(r) 44 r.ReadAllSpace() 45 gofile.AddImport(string(MustReadGoString(r)), string(alias)) 46 } 47 }