-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoparser.go
136 lines (119 loc) · 2.54 KB
/
goparser.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
//
// goparser
// program that parses a go file and lists functions and types
//
// author prr
// created 28/2/2022
//
// copyright 2022 prr
//
package main
import (
"os"
"fmt"
)
func fatErr(fs string, msg string, err error) {
if err != nil {
fmt.Printf("error %s:: %s!%v\n", fs, msg, err)
} else {
fmt.Printf("error %s:: %s!\n", fs, msg)
}
os.Exit(2)
}
func main() {
numArg := len(os.Args)
if numArg < 2 {fatErr("main", "insufficient arguments", nil)}
infilnam := os.Args[1]
if infilnam[len(infilnam)-3:] != ".go" {
fatErr("main", "not a go file", nil)
}
infil, err := os.Open(infilnam)
if err != nil {fatErr("Open File", "could not open infil",err)}
defer infil.Close()
filInfo, err := infil.Stat()
if err != nil {fatErr("File Stat", "could get fileinfo",err)}
nb := filInfo.Size()
fmt.Printf("file size is %d!\n", nb)
buf := make([]byte, nb)
_, err = infil.Read(buf)
if err != nil {fatErr("File read", "could not read",err)}
outfilnam := infilnam[:len(infilnam) - 3]+ ".gdat"
fmt.Printf("out file name: %s \n", outfilnam)
outfil, err := os.Create(outfilnam)
if err != nil {fatErr("Outpput file", "could not create",err)}
defer outfil.Close()
istate := 0
ilin:= 0
var i, ist, j, opst int64
// linst =0
ist = 0
// outstr := ""
for i=0; i< nb - 4; i++ {
switch istate {
case 0:
if string(buf[i: i+5]) == "func " {
// need to find function name
// fmt.Printf("dbg: %s line: %d\n", string(buf[i: i+10]), ilin)
ist = i
opst = 0
istate = 3
i = i+ 4
} else {
if buf[i] == '\n' {
ilin++
// linst = i+1
istate = 0
opst = 0
} else {
istate = 1
}
}
case 1:
if buf[i] == '\n' {
// linst = i+1
ilin++
opst = 0
istate = 0
}
case 2:
if string(buf[i: i+2]) == "//" {
istate =1
i= i+2
}
case 3:
if buf[i] == '{' {
method := false
istate = 1
for j=ist+5; j< i; j++ {
if buf[j] == ' ' {continue}
if (buf[j] >= 'a' && buf[j] <= 'z') || (buf[j] >= 'A' && buf[j] <= 'Z') {
opst = 0
break
}
if buf[j] == '(' {
opst = j
break
}
}
if opst > 0 {
for j= opst+1; j< i; j++ {
if buf[j] == ')' {
method = true
break
}
}
}
if method {
fmt.Printf("line: %4d method found: %s \n", ilin+1, string(buf[ist: i]))
} else {
fmt.Printf("line: %4d function found: %s \n", ilin+1, string(buf[ist: i]))
}
ist =0
}
default:
}
}
// fmt.Printf("%s\n", outstr)
fmt.Printf("total lines: %d\n", ilin)
fmt.Println("success!")
}