/
utils.go
135 lines (120 loc) · 2.99 KB
/
utils.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package utils
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path"
"github.com/gofed/symbols-extractor/pkg/parser/types"
"github.com/gofed/symbols-extractor/pkg/symbols"
"github.com/gofed/symbols-extractor/pkg/symbols/tables"
gotypes "github.com/gofed/symbols-extractor/pkg/types"
)
func BuiltinSymbolTable() *tables.Table {
st := tables.NewTable()
addType := func(name string) {
st.AddDataType(&symbols.SymbolDef{
Name: name,
Package: "builtin",
Def: &gotypes.Builtin{
Def: name,
},
})
}
addType("int")
addType("string")
addType("uint32")
addType("uint16")
addType("float32")
return st
}
func GetAst(gopkg, filename string, gocode interface{}) (*ast.File, *token.FileSet, error) {
fset := token.NewFileSet()
gofile := path.Join(os.Getenv("GOPATH"), "src", gopkg, filename)
f, err := parser.ParseFile(fset, gofile, gocode, 0)
if err != nil {
return nil, fset, err
}
return f, fset, nil
}
func ParseNonFunc(config *types.Config, astF *ast.File) error {
//TODO: later parsing of values will be required
for _, d := range astF.Decls {
switch decl := d.(type) {
case *ast.GenDecl:
for _, spec := range decl.Specs {
//fmt.Printf("=== %#v", spec)
switch d := spec.(type) {
case *ast.TypeSpec:
if err := config.SymbolTable.AddDataType(&symbols.SymbolDef{
Name: d.Name.Name,
Package: config.PackageName,
Def: nil,
}); err != nil {
return err
}
typeDef, err := config.TypeParser.Parse(d.Type)
if err != nil {
return err
}
if err := config.SymbolTable.AddDataType(&symbols.SymbolDef{
Name: d.Name.Name,
Package: config.PackageName,
Def: typeDef,
}); err != nil {
return err
}
case *ast.ValueSpec:
//TODO(pstodulk):
// - maybe identifier will be added automatically
// by typeparser into the symtab. Watch..
// - store type into the variable - now it is not possible
// varType, err := tp.ParseTypeExpr(d.Type)
_, err := config.TypeParser.Parse(d.Type)
if err != nil {
return err
}
err = config.SymbolTable.AddVariable(&symbols.SymbolDef{
Name: d.Names[0].Name,
Package: config.PackageName,
Def: &gotypes.Identifier{
Def: d.Names[0].Name,
Package: config.PackageName,
},
})
fmt.Printf("Storing %v..., err: %v\n", d.Names[0].Name, err)
}
}
default:
continue
}
}
return nil
}
func IterVar(astF *ast.File) []*ast.ValueSpec {
var specs []*ast.ValueSpec
for _, decl := range astF.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
for _, valSpec := range genDecl.Specs {
varDecl, ok := valSpec.(*ast.ValueSpec)
if !ok {
continue
}
specs = append(specs, varDecl)
}
}
return specs
}
func IterFunc(astF *ast.File) []*ast.FuncDecl {
var decls []*ast.FuncDecl
for _, decl := range astF.Decls {
if fdecl, ok := decl.(*ast.FuncDecl); ok {
decls = append(decls, fdecl)
}
}
return decls
}