-
Notifications
You must be signed in to change notification settings - Fork 5
/
func.go
204 lines (186 loc) · 4.61 KB
/
func.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package udwGoParser
import (
"fmt"
"github.com/tachyon-protocol/udw/udwGoSource/udwGoReader"
)
func MustParseGoFunc(s string) *FuncOrMethodDeclaration {
gofile := &file{
importMap: map[string]bool{},
aliasImportMap: map[string]string{},
pkg: &Package{
program: NewProgramFromDefault(),
pkgImportPath: "main",
},
}
r := udwGoReader.NewReaderWithBuf([]byte(s))
return gofile.readGoFunc(r)
}
type MustParseGoFuncDeclarationRequest struct {
Pkg *Package
ImportList []string
GoFuncDeclarationContent string
}
func MustParseGoFuncDeclaration(req MustParseGoFuncDeclarationRequest) *FuncOrMethodDeclaration {
if req.Pkg == nil {
req.Pkg = &Package{
program: NewProgramFromDefault(),
pkgImportPath: "main",
}
}
gofile := &file{
importMap: map[string]bool{},
aliasImportMap: map[string]string{},
}
gofile.pkg = req.Pkg
for _, importPath := range req.ImportList {
gofile.addImport(importPath, "")
}
r := udwGoReader.NewReaderWithBuf([]byte(req.GoFuncDeclarationContent))
return gofile.readGoFunc(r)
}
func (gofile *file) readGoFunc(r *udwGoReader.Reader) *FuncOrMethodDeclaration {
var isVariadic bool
funcDecl := &FuncOrMethodDeclaration{}
r.MustReadMatch([]byte("func"))
r.ReadAllSpace()
b := r.ReadByte()
if b == '(' {
r.UnreadByte()
receiver, isVariadic := gofile.readParameters(r)
if len(receiver) != 1 {
panic(fmt.Errorf("%s receiver must have one parameter", r.GetFileLineInfo()))
}
funcDecl.ReceiverType = receiver[0].Type
if isVariadic {
panic("[gofile.readGoFunc] found isVariadic(...) in receiver")
}
r.ReadAllSpace()
} else {
r.UnreadByte()
}
id := readIdentifier(r)
funcDecl.Name = string(id)
if funcDecl.Name == "" {
panic(fmt.Errorf("%s need function name", r.GetFileLineInfo()))
}
r.ReadAllSpace()
b = r.ReadByte()
if b != '(' {
panic(fmt.Errorf("%s unexcept %s", r.GetFileLineInfo(), string(rune(b))))
}
r.UnreadByte()
funcDecl.InParameter, isVariadic = gofile.readParameters(r)
funcDecl.IsVariadic = isVariadic
r.ReadAllSpaceWithoutLineBreak()
if r.IsEof() {
return funcDecl
}
b = r.ReadByte()
if b == '\n' {
return funcDecl
} else if b != '{' {
r.UnreadByte()
funcDecl.OutParameter, isVariadic = gofile.readParameters(r)
if isVariadic {
panic("[gofile.readGoFunc] found isVariadic(...) in outParameter")
}
r.ReadAllSpaceWithoutLineBreak()
if r.IsEof() {
return funcDecl
}
b = r.ReadByte()
}
if b == '\n' {
return funcDecl
}
if b != '{' {
panic(fmt.Errorf("%s unexcept %s", r.GetFileLineInfo(), string(rune(b))))
}
readMatchBigParantheses(r)
return funcDecl
}
func (gofile *file) readParameters(r *udwGoReader.Reader) (output []FuncParameter, isVariadic bool) {
b := r.ReadByte()
if b != '(' {
r.UnreadByte()
return []FuncParameter{
{
Type: gofile.readType(r),
},
}, false
}
parameterPartList := []*astParameterPart{}
lastPart := &astParameterPart{}
for {
r.ReadAllSpace()
b := r.ReadByte()
if b == ')' || b == ',' {
if lastPart.partList[0].originByte != nil {
parameterPartList = append(parameterPartList, lastPart)
lastPart = &astParameterPart{}
}
if b == ')' {
break
}
if b == ',' {
continue
}
}
r.UnreadByte()
if r.IsMatchAfter([]byte("...")) {
r.MustReadMatch([]byte("..."))
isVariadic = true
}
startPos := r.Pos()
typ := gofile.readType(r)
buf := r.BufToCurrent(startPos)
hasSet := false
for i := range lastPart.partList {
if lastPart.partList[i].originByte == nil {
lastPart.partList[i].originByte = buf
lastPart.partList[i].typ = typ
hasSet = true
break
}
}
if !hasSet {
panic(r.GetFileLineInfo() + " unexcept func parameterList.")
}
}
output = make([]FuncParameter, len(parameterPartList))
onlyHavePart1Num := 0
for i := range parameterPartList {
if parameterPartList[i].partList[1].originByte == nil {
onlyHavePart1Num++
}
}
if onlyHavePart1Num == len(parameterPartList) {
for i := range parameterPartList {
output[i].Type = parameterPartList[i].partList[0].typ
}
return output, isVariadic
}
for i, parameterPart := range parameterPartList {
output[i].Name = string(parameterPart.partList[0].originByte)
if parameterPart.partList[1].typ != nil {
output[i].Type = parameterPart.partList[1].typ
}
}
for i := range parameterPartList {
if output[i].Type == nil {
for j := i + 1; j < len(parameterPartList); j++ {
if output[j].Type != nil {
output[i].Type = output[j].Type
}
}
}
}
return output, isVariadic
}
type astParameterPart struct {
partList [2]struct {
originByte []byte
typ Type
}
isVariadic bool
}