/
parse.go
169 lines (151 loc) · 3.34 KB
/
parse.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
package main
import (
"crypto/md5"
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"strings"
)
var fset *token.FileSet
var fast *ast.File
type typeInfo struct {
typeName string
comment string
methods []string
safeMethods []string
// imports []string
ctype string
cdecl string
}
var typeMap = make(map[string]*typeInfo)
var typeList = make([]*typeInfo, 0, 10)
var packageName string
var crc string
func parseGoFile(gofile string) (err error) {
fset = token.NewFileSet()
fast, err = parser.ParseFile(fset, gofile, nil, parser.ParseComments)
if err != nil {
return
}
packageName = fast.Name.Name
for _, dc := range fast.Decls {
err = parseDecl(dc)
if err != nil {
return err
}
}
for _, info := range typeMap {
err = info.parseComment()
if err != nil {
return err
}
}
bytes, err := ioutil.ReadFile(gofile)
if err != nil {
return err
}
tmp := fmt.Sprintf("%x", md5.Sum(bytes))
crc = "0x" + tmp[0:16]
return
}
func parseDecl(decl ast.Decl) error {
gdl, ok := decl.(*ast.GenDecl)
if !ok {
p := fset.Position(decl.Pos())
return fmt.Errorf("Go file should contain only types, not %T at %d", p.Line)
}
comments := addComments("", gdl.Doc)
for _, spec := range gdl.Specs {
err := parseSpec(spec, comments)
if err != nil {
return err
}
}
return nil
}
func parseSpec(spec ast.Spec, parentComment string) error {
_, ok := spec.(*ast.ValueSpec)
if ok {
return nil // Skip values and constants
}
_, ok = spec.(*ast.ImportSpec)
if ok {
return nil // Skip imports
}
tsp, ok := spec.(*ast.TypeSpec)
if !ok {
p := fset.Position(spec.Pos())
return fmt.Errorf("Invalid specification %T, should be typespec %d", spec, p.Line)
}
comments := addComments(parentComment, tsp.Doc)
tt := &typeInfo{
typeName: tsp.Name.String(), comment: comments,
}
typeMap[tsp.Name.String()] = tt
typeList = append(typeList, tt)
return nil
}
func addComments(current string, group *ast.CommentGroup) string {
if group == nil || len(group.List) == 0 {
return current
}
sb := &strings.Builder{}
sb.WriteString(current)
for _, l := range group.List {
sb.WriteString(l.Text)
sb.WriteRune('\n')
}
return sb.String()
}
var methods = []string{"#cmethod", "#csafe_method", "#ctype", "#c"}
func (info *typeInfo) parseComment() error {
var sb *strings.Builder
for _, line := range strings.Split(info.comment, "\n") {
idx := 0
pos := -1
for ; idx < len(methods); idx++ {
pos = strings.Index(line, methods[idx])
if pos >= 0 {
break
}
}
if idx == 3 {
return fmt.Errorf("Invalid #c directive at line: %s", line)
}
if sb != nil {
if pos >= 0 {
return errors.New("#ctype or #cmethod inside #ctype declaration")
}
idx1 := strings.Index(line, "*/")
if idx1 < 0 {
idx1 = len(line)
}
sb.WriteString(line[:idx1])
sb.WriteRune('\n')
continue
}
if idx == 0 { // #cmethod
for _, m := range strings.Split(line[pos+8:], ",") {
info.methods = append(info.methods, strings.Trim(m, " \t"))
}
continue
}
if idx == 1 { // #csafe_method
for _, m := range strings.Split(line[pos+13:], ",") {
info.safeMethods = append(info.safeMethods, strings.Trim(m, " \t"))
}
continue
}
if idx == 2 { // #ctype
info.ctype = strings.Trim(line[pos+6:], " \t")
sb = &strings.Builder{}
}
}
if sb != nil {
info.cdecl = sb.String()
}
return nil
}