-
Notifications
You must be signed in to change notification settings - Fork 1
/
snippet_expr.go
247 lines (192 loc) · 4.16 KB
/
snippet_expr.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package codegen
import (
"bytes"
"go/token"
"regexp"
)
type SnippetExpr string
func (tpe SnippetExpr) Bytes() []byte {
return []byte(string(tpe))
}
var Expr = createExpr(LowerSnakeCase)
func createExpr(aliaser ImportPathAliaser) func(f string, args ...interface{}) SnippetExpr {
val := createVal(aliaser)
return func(f string, args ...interface{}) SnippetExpr {
idx := 0
return SnippetExpr(reExprHolder.ReplaceAllStringFunc(f, func(i string) string {
arg := args[idx]
idx++
if s, ok := arg.(Snippet); ok {
return Stringify(s)
}
return Stringify(val(arg))
}))
}
}
var reExprHolder = regexp.MustCompile(`(\$\d+)|\?`)
func KeyValue(key Snippet, value Snippet) *SnippetKeyValueExpr {
return &SnippetKeyValueExpr{
Key: key,
Value: value,
}
}
type SnippetKeyValueExpr struct {
Snippet
Key Snippet
Value Snippet
}
func (tpe *SnippetKeyValueExpr) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(tpe.Key.Bytes())
buf.WriteString(token.COLON.String())
buf.WriteRune(' ')
buf.Write(tpe.Value.Bytes())
return buf.Bytes()
}
func Sel(x Snippet, selectors ...Snippet) *SnippetSelectorExpr {
return &SnippetSelectorExpr{
X: x,
Selectors: selectors,
}
}
type SnippetSelectorExpr struct {
X Snippet
Selectors []Snippet
}
func (expr *SnippetSelectorExpr) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(expr.X.Bytes())
for _, selectorExpr := range expr.Selectors {
buf.WriteRune('.')
buf.Write(selectorExpr.Bytes())
}
return buf.Bytes()
}
type SnippetCanAddr interface {
Snippet
snippetCanAddr()
}
func Star(tpe SnippetType) *SnippetStarExpr {
return &SnippetStarExpr{
X: tpe,
}
}
type SnippetStarExpr struct {
SnippetCanAddr
SnippetType
X SnippetType
}
func (expr *SnippetStarExpr) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.MUL.String())
buf.Write(expr.X.Bytes())
return buf.Bytes()
}
func Unary(addr SnippetCanAddr) *SnippetUnaryExpr {
return &SnippetUnaryExpr{
Elem: addr,
}
}
type SnippetUnaryExpr struct {
Elem SnippetCanAddr
}
func (tpe *SnippetUnaryExpr) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteString(token.AND.String())
buf.Write(tpe.Elem.Bytes())
return buf.Bytes()
}
func Paren(s Snippet) *SnippetParenExpr {
return &SnippetParenExpr{
Elem: s,
}
}
type SnippetParenExpr struct {
SnippetCanAddr
Elem Snippet
}
func (tpe *SnippetParenExpr) Bytes() []byte {
buf := &bytes.Buffer{}
buf.WriteRune('(')
buf.Write(tpe.Elem.Bytes())
buf.WriteRune(')')
return buf.Bytes()
}
func Convert(tpe SnippetType, target Snippet) *SnippetCallExpr {
return CallWith(tpe, target)
}
func CallWith(s Snippet, params ...Snippet) *SnippetCallExpr {
return &SnippetCallExpr{
X: s,
Params: params,
}
}
func Call(name string, params ...Snippet) *SnippetCallExpr {
if isBuiltInFunc(name) {
return &SnippetCallExpr{
X: SnippetIdent(name),
Params: params,
}
}
return &SnippetCallExpr{
X: Id(name),
Params: params,
}
}
type SnippetCallExpr struct {
X Snippet
Params []Snippet
Ellipsis bool
Modifier token.Token
}
func (expr SnippetCallExpr) AsDefer() *SnippetCallExpr {
expr.Modifier = token.DEFER
return &expr
}
func (expr SnippetCallExpr) AsGo() *SnippetCallExpr {
expr.Modifier = token.GO
return &expr
}
func (expr SnippetCallExpr) WithEllipsis() *SnippetCallExpr {
expr.Ellipsis = true
return &expr
}
func (expr *SnippetCallExpr) Bytes() []byte {
buf := &bytes.Buffer{}
if expr.Modifier > 0 {
buf.WriteString(expr.Modifier.String())
buf.WriteRune(' ')
}
buf.Write(expr.X.Bytes())
buf.WriteRune('(')
for i, p := range expr.Params {
if i > 0 {
buf.WriteString(", ")
}
buf.Write(p.Bytes())
}
if expr.Ellipsis {
buf.WriteString(token.ELLIPSIS.String())
}
buf.WriteRune(')')
return buf.Bytes()
}
func TypeAssert(tpe SnippetType, x Snippet) *SnippetTypeAssertExpr {
return &SnippetTypeAssertExpr{
X: x,
Type: tpe,
}
}
type SnippetTypeAssertExpr struct {
X Snippet
Type SnippetType
}
func (expr *SnippetTypeAssertExpr) Bytes() []byte {
buf := &bytes.Buffer{}
buf.Write(expr.X.Bytes())
buf.WriteRune('.')
buf.WriteRune('(')
buf.Write(expr.Type.Bytes())
buf.WriteRune(')')
return buf.Bytes()
}