This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
/
gosem.go
179 lines (147 loc) · 4.05 KB
/
gosem.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"strings"
)
var fmtPrintf = fmt.Printf
var fmtPrintln = fmt.Println
var fileNamePtr *string
var fset *token.FileSet
var fileAst *ast.File
var fieldsFound []string
var functionsFound []function
var functionVarsFound []string
func init() {
fileNamePtr = flag.String("f", "", "Filename to parse")
}
func main() {
defer recoverPanicWhenThrown()
file := getFileFromArgs()
parseFile(file)
extractWantedFromAst()
printFound()
}
func recoverPanicWhenThrown() {
if r := recover(); r != nil {
fmtPrintln("Error: ", r)
}
}
func getFileFromArgs() string {
flag.Parse()
return *fileNamePtr
}
func parseFile(file string) {
fset = token.NewFileSet() // positions are relative to fset
f, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
panic("error parsing file \"" + file + "\".")
}
fileAst = f
}
func extractWantedFromAst() {
extractFieldsFromAst()
extractFunctionsFromAst()
}
func printFound() {
var groups []string
fields := fieldsFound
groups = append(groups, strings.Join(fields, " "))
functions := functionsFound
for _, currentFunction := range functions {
varString := strings.Join(currentFunction.variables, " ")
functionString := fmt.Sprintf("%d,%d,%s", currentFunction.bodyStart, currentFunction.bodyEnd, varString)
groups = append(groups, functionString)
}
fmtPrintf(strings.Join(groups, "|"))
}
// FIELDS
//================================================================================
func extractFieldsFromAst() {
for _, declaration := range fileAst.Decls {
if genDecl, isGenDecl := declaration.(*ast.GenDecl); isGenDecl {
for _, s := range genDecl.Specs {
if value, isValueSpec := s.(*ast.ValueSpec); isValueSpec {
addFoundField(value.Names[0].String())
}
}
}
}
}
func addFoundField(f string) {
fieldsFound = append(fieldsFound, f)
}
// FUNCTIONS
//================================================================================
type function struct {
bodyStart int //Line the function starts at
bodyEnd int //Line the function ends at
variables []string //a list of variables
}
func extractFunctionsFromAst() {
for _, declaration := range fileAst.Decls {
if funcDecl, isFuncDecl := declaration.(*ast.FuncDecl); isFuncDecl {
processFunction(funcDecl)
}
}
}
func processFunction(funcDecl *ast.FuncDecl) {
m := function{}
m.bodyStart = fset.Position(funcDecl.Pos()).Line
m.bodyEnd = fset.Position(funcDecl.End()).Line
m.variables = getFunctionVariables(funcDecl)
addFoundFunctions(m)
}
func addFoundFunctions(m ...function) {
functionsFound = append(functionsFound, m...)
}
// Variables
//--------------------------------------------------------------------------------
func getFunctionVariables(funcDecl *ast.FuncDecl) []string {
functionVarsFound = nil
findParameters(funcDecl.Type)
findFunctionVariables(funcDecl.Body)
return functionVarsFound
}
func findParameters(funcType *ast.FuncType) {
for _, param := range funcType.Params.List {
for _, name := range param.Names {
addFoundFunctionVar(name.Name)
}
}
}
func findFunctionVariables(functionBlock *ast.BlockStmt) {
for _, functionStmt := range functionBlock.List {
if decl, isDecl := functionStmt.(*ast.DeclStmt); isDecl {
getVariablesFromDeclStmt(decl)
} else if assign, isAssign := functionStmt.(*ast.AssignStmt); isAssign {
if assign.Tok != token.ASSIGN {
getVariablesFromAssignStmt(assign)
}
}
}
}
func getVariablesFromAssignStmt(assignStmt *ast.AssignStmt) {
for _, itemLeftOfAssignment := range assignStmt.Lhs {
if variable, isVarIdent := itemLeftOfAssignment.(*ast.Ident); isVarIdent {
addFoundFunctionVar(variable.Name)
}
}
}
func getVariablesFromDeclStmt(decl *ast.DeclStmt) {
if genDecl, isGenDecl := decl.Decl.(*ast.GenDecl); isGenDecl {
for _, s := range genDecl.Specs {
if value, isValueSpec := s.(*ast.ValueSpec); isValueSpec {
for _, ident := range value.Names {
addFoundFunctionVar(ident.Name)
}
}
}
}
}
func addFoundFunctionVar(v ...string) {
functionVarsFound = append(functionVarsFound, v...)
}