forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreference.gox
112 lines (101 loc) · 2.22 KB
/
reference.gox
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import (
"go/ast"
"go/parser"
"go/token"
"gop/env"
"runtime"
)
var (
Pkg string
Name string
)
func .toType(t *ast.FuncType, at string) *ast.FuncType {
return {
Params: t.Params,
Results: toParams(t.Results, at),
}
}
func .toMethodType(t *ast.FuncType, at string) (mt *ast.FuncType, recvType ast.Expr) {
list := t.Params.List
first := list[0]
recvType = first.Type
if len(first.Names) == 1 {
list = list[1:]
} else {
list[0] = {
Doc: first.Doc,
Names: first.Names[1:],
Type: recvType,
Comment: first.Comment,
}
}
mt = {
Params: {List: list},
Results: toParams(t.Results, at),
}
return
}
func .docFor(doc *ast.CommentGroup, oldName, newName string) *ast.CommentGroup {
if doc == nil {
return nil
}
list := make([]*ast.Comment, len(doc.List))
for i, c := range doc.List {
text := c.Text.replaceAll(oldName, newName)
list[i] = {Text: text}
}
return {List: list}
}
var (
fset = token.newFileSet
pkgs = map[string]*ast.Package{}
root = env.GOPROOT()
goroot = runtime.GOROOT()
pkgDirs = {
"": "${goroot}/src/builtin",
"fmt": "${goroot}/src/fmt",
"os": "${goroot}/src/os",
"reflect": "${goroot}/src/reflect",
"strconv": "${goroot}/src/strconv",
"strings": "${goroot}/src/strings",
"buil": "${root}/builtin",
"iox": "${root}/builtin/iox",
"stringslice": "${root}/builtin/stringslice",
"strx": "${root}/../x/stringutil",
}
)
func getAST() *ast.FuncDecl {
name := Name
if Pkg == "" {
name = name.trimPrefix("bto").toLower
}
pkg := reference.pkgOf(Pkg)
return reference.funcDecl(pkg, name)
}
func .funcDecl(pkg *ast.Package, name string) *ast.FuncDecl {
for file <- pkg.Files {
for decl <- file.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok && fn.Name.Name == name {
return fn
}
}
}
panic("function not found: ${pkg.Name}.${name}")
}
func .pkgOf(pkgPath string) *ast.Package {
if pkg, ok := pkgs[pkgPath]; ok {
return pkg
}
dir, ok := pkgDirs[pkgPath]
if !ok {
panic("unknown package: ${pkgPath}")
}
for name, pkg <- parser.parseDir(fset, dir, nil, parser.ParseComments)! {
if name.hasSuffix("_test") {
continue
}
pkgs[pkgPath] = pkg
return pkg
}
panic("package not found: ${pkgPath}")
}