-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbuilder.go
254 lines (223 loc) · 5.28 KB
/
builder.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
248
249
250
251
252
253
254
package restructure
import (
"errors"
"fmt"
"reflect"
"regexp/syntax"
"strings"
)
// A Role determines how a struct field is inflated
type Role int
const (
EmptyRole Role = iota
PosRole
SubstructRole
StringScalarRole
IntScalarRole
ByteSliceScalarRole
SubmatchScalarRole
)
// A Struct describes how to inflate a match into a struct
type Struct struct {
capture int
fields []*Field
}
// A Field describes how to inflate a match into a field
type Field struct {
capture int // index of the capture for this field
index []int // index of this field within its parent struct
child *Struct // descendant struct; nil for terminals
role Role
}
func isExported(f reflect.StructField) bool {
return f.PkgPath == ""
}
// A builder builds stencils from structs using reflection
type builder struct {
numCaptures int
opts Options
}
func newBuilder(opts Options) *builder {
return &builder{
opts: opts,
}
}
func (b *builder) nextCaptureIndex() int {
k := b.numCaptures
b.numCaptures++
return k
}
func (b *builder) extractTag(tag reflect.StructTag) (string, error) {
// Allow tags that look like either `regexp:"\\w+"` or just `\w+`
if s := tag.Get("regexp"); s != "" {
return s, nil
} else if strings.Contains(string(tag), `regexp:"`) {
return "", errors.New("incorrectly escaped struct tag")
} else {
return string(tag), nil
}
}
func removeCaptures(expr *syntax.Regexp) ([]*syntax.Regexp, error) {
if expr.Op == syntax.OpCapture {
return expr.Sub, nil
}
return []*syntax.Regexp{expr}, nil
}
func (b *builder) terminal(f reflect.StructField, fullName string) (*Field, *syntax.Regexp, error) {
pattern, err := b.extractTag(f.Tag)
if err != nil {
return nil, nil, fmt.Errorf("%s: %v", fullName, err)
}
if pattern == "" {
return nil, nil, nil
}
// Parse the pattern
expr, err := syntax.Parse(pattern, b.opts.SyntaxFlags)
if err != nil {
return nil, nil, fmt.Errorf(`%s: %v (pattern was "%s")`, fullName, err, f.Tag)
}
// Remove capture nodes within the AST
expr, err = transform(expr, removeCaptures)
if err != nil {
return nil, nil, fmt.Errorf(`failed to remove captures from "%s": %v`, pattern, err)
}
// Determine the kind
t := f.Type
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
var role Role
switch t {
case emptyType:
role = EmptyRole
case stringType:
role = StringScalarRole
case intType:
role = IntScalarRole
case byteSliceType:
role = ByteSliceScalarRole
case submatchType:
role = SubmatchScalarRole
}
captureIndex := -1
if isExported(f) {
captureIndex = b.nextCaptureIndex()
expr = &syntax.Regexp{
Op: syntax.OpCapture,
Sub: []*syntax.Regexp{expr},
Name: f.Name,
Cap: captureIndex,
}
}
field := &Field{
index: f.Index,
capture: captureIndex,
role: role,
}
return field, expr, nil
}
func (b *builder) pos(f reflect.StructField, fullName string) (*Field, *syntax.Regexp, error) {
if !isExported(f) {
return nil, nil, nil
}
captureIndex := b.nextCaptureIndex()
empty := &syntax.Regexp{
Op: syntax.OpEmptyMatch,
}
expr := &syntax.Regexp{
Op: syntax.OpCapture,
Sub: []*syntax.Regexp{empty},
Name: f.Name,
Cap: captureIndex,
}
field := &Field{
index: f.Index,
capture: captureIndex,
role: PosRole,
}
return field, expr, nil
}
func (b *builder) nonterminal(f reflect.StructField, fullName string) (*Field, *syntax.Regexp, error) {
opstr, err := b.extractTag(f.Tag)
if err != nil {
return nil, nil, err
}
child, expr, err := b.structure(f.Type)
if err != nil {
return nil, nil, err
}
switch opstr {
case "?":
if f.Type.Kind() != reflect.Ptr {
return nil, nil, fmt.Errorf(`%s is marked with "?" but is not a pointer`, fullName)
}
expr = &syntax.Regexp{
Sub: []*syntax.Regexp{expr},
Op: syntax.OpQuest,
}
case "":
// nothing to do
default:
return nil, nil, fmt.Errorf("invalid op \"%s\" for non-slice field on %s", opstr, fullName)
}
captureIndex := b.nextCaptureIndex()
expr = &syntax.Regexp{
Op: syntax.OpCapture,
Sub: []*syntax.Regexp{expr},
Name: f.Name,
Cap: captureIndex,
}
field := &Field{
index: f.Index,
capture: captureIndex,
child: child,
role: SubstructRole,
}
return field, expr, nil
}
func (b *builder) field(f reflect.StructField, fullName string) (*Field, *syntax.Regexp, error) {
if isScalar(f.Type) {
return b.terminal(f, fullName)
} else if isStruct(f.Type) {
return b.nonterminal(f, fullName)
} else if f.Type == posType {
return b.pos(f, fullName)
}
return nil, nil, nil
}
func (b *builder) structure(t reflect.Type) (*Struct, *syntax.Regexp, error) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// Select a capture index first so that the struct comes before its fields
captureIndex := b.nextCaptureIndex()
var exprs []*syntax.Regexp
var fields []*Field
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
field, expr, err := b.field(f, t.Name()+"."+f.Name)
if err != nil {
return nil, nil, err
}
if field != nil {
exprs = append(exprs, expr)
fields = append(fields, field)
}
}
// Wrap in a concat
expr := &syntax.Regexp{
Sub: exprs,
Op: syntax.OpConcat,
}
// Wrap in a capture
expr = &syntax.Regexp{
Sub: []*syntax.Regexp{expr},
Op: syntax.OpCapture,
Cap: captureIndex,
}
st := &Struct{
fields: fields,
capture: captureIndex,
}
return st, expr, nil
}