github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgView/kmgViewResource/parseImportPath.go (about) 1 package kmgViewResource 2 3 import ( 4 "github.com/bronze1man/kmg/kmgGoSource/kmgGoParser" 5 "github.com/bronze1man/kmg/kmgGoSource/kmgGoReader" 6 ) 7 8 // 一个文件只会读第一个import,其他的都会忽略.如果在第一个/**/ 之前出现了其他任何东西,也都忽略. 9 // /* 10 // import( 11 // "webResource/bootstrap" 12 // "webResource/echart" 13 // ) 14 // */ 15 // 语法错误panic,看上去不是import的情况直接返回nil 16 17 var importToken = []byte("import") 18 19 func parseImportPath(filename string, content []byte) []string { 20 r := kmgGoReader.NewReaderWithPosFile(filename, content) 21 r.ReadAllSpace() 22 if r.IsEof() { 23 return nil 24 } 25 var b byte 26 // 读入 /* 27 b = r.ReadByte() 28 if b != '/' { 29 return nil 30 } 31 if r.IsEof() { 32 return nil 33 } 34 b = r.ReadByte() 35 if b != '*' { 36 return nil 37 } 38 r.ReadAllSpace() 39 // 读入 import ( 40 if !r.IsMatchAfter(importToken) { 41 return nil 42 } 43 r.MustReadMatch(importToken) 44 r.ReadAllSpace() 45 if r.IsEof() { 46 return nil 47 } 48 b = r.ReadByte() 49 if b != '(' { 50 return nil 51 } 52 output := []string{} 53 for { 54 r.ReadAllSpace() 55 if r.IsEof() { 56 panic(r.GetFileLineInfo() + " unexpect EOF") 57 } 58 b = r.ReadByte() 59 if b == ')' { 60 return output 61 } 62 if b == '"' || b == '`' { 63 r.UnreadByte() 64 importPath := kmgGoParser.MustReadGoString(r) 65 output = append(output, string(importPath)) 66 } 67 } 68 }