forked from awfeequdng/px_golang2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_import.go
53 lines (47 loc) · 990 Bytes
/
parse_import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"go/ast"
"log"
"path/filepath"
"strings"
)
var importMap map[string]string = make(map[string]string)
func genImportMap(key *ast.Ident, pkg string) {
var k string
if key == nil {
k = filepath.Base(pkg)
} else {
k = key.Name
}
if strings.HasPrefix(k, "\"") {
k = k[1:]
}
if strings.HasSuffix(k, "\"") {
k = k[:len(k) - 1]
}
importMap[k] = pkg
}
func PrintImportMap() {
log.Print("importMap: \n")
for k, v := range importMap {
log.Print(k + " : " + v + "\n")
}
}
func ParseGenDeclImport(decl *ast.GenDecl) []string {
var ret []string
var names []string
for _, spec := range decl.Specs {
if is, ok := spec.(*ast.ImportSpec); ok {
names = append(names, is.Path.Value)
// generate import package map
genImportMap(is.Name, is.Path.Value)
} else {
log.Fatal("can not convert to ImportSpec")
}
}
for _, name := range names {
// do not include this import pack
ret = append(ret, "// #include " + name);
}
return ret
}