-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtag.go
290 lines (269 loc) · 6.17 KB
/
tag.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package restruct
import (
"encoding/binary"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
func lower(ch rune) rune {
return ('a' - 'A') | ch
}
func isdecimal(ch rune) bool {
return '0' <= ch && ch <= '9'
}
func ishex(ch rune) bool {
return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f'
}
func isletter(c rune) bool {
return 'a' <= lower(c) && lower(c) <= 'z' || c == '_' || c >= utf8.RuneSelf && unicode.IsLetter(c)
}
func isdigit(c rune) bool {
return isdecimal(c)
}
func isident(c rune) bool {
return isletter(c) || isdigit(c)
}
func isint(c rune) bool {
return isdigit(c) || ishex(c) || lower(c) == 'x'
}
// tagOptions represents a parsed struct tag.
type tagOptions struct {
Ignore bool
Type reflect.Type
SizeOf string
SizeFrom string
Skip int
Order binary.ByteOrder
BitSize uint8
VariantBoolFlag bool
InvertedBoolFlag bool
RootFlag bool
ParentFlag bool
DefaultFlag bool
IfExpr string
SizeExpr string
BitsExpr string
InExpr string
OutExpr string
WhileExpr string
SwitchExpr string
CaseExpr string
}
func (opts *tagOptions) parse(tag string) error {
// Empty tag
if len(tag) == 0 {
return nil
} else if tag == "-" {
opts.Ignore = true
return nil
}
tag += "\x00"
accept := func(v string) bool {
if strings.HasPrefix(tag, v) {
tag = tag[len(v):]
return true
}
return false
}
acceptIdent := func() (string, error) {
var (
i int
r rune
)
for i, r = range tag {
if r == ',' || r == 0 {
break
}
if i == 0 && !isletter(r) || !isident(r) {
return "", fmt.Errorf("invalid identifier character %c", r)
}
}
result := tag[:i]
tag = tag[i:]
return result, nil
}
acceptInt := func() (int, error) {
var (
i int
r rune
)
for i, r = range tag {
if r == ',' || r == 0 {
break
}
if !isint(r) {
return 0, fmt.Errorf("invalid integer character %c", r)
}
}
result := tag[:i]
tag = tag[i:]
d, err := strconv.ParseInt(result, 0, 64)
return int(d), err
}
acceptExpr := func() (string, error) {
stack := []byte{0}
current := func() byte { return stack[len(stack)-1] }
push := func(r byte) { stack = append(stack, r) }
pop := func() { stack = stack[:len(stack)-1] }
var i int
expr:
for i = 0; i < len(tag); i++ {
switch tag[i] {
case ',':
if len(stack) == 1 {
break expr
}
case '(':
push(')')
case '[':
push(']')
case '{':
push('}')
case '"', '\'':
term := tag[i]
i++
lit:
for {
if i >= len(tag) {
return "", errors.New("unexpected eof in literal")
}
switch tag[i] {
case term:
break lit
case '\\':
i++
}
i++
}
case current():
pop()
if len(stack) == 0 {
break expr
}
default:
if tag[i] == 0 {
return "", errors.New("unexpected eof in expr")
}
}
}
result := tag[:i]
tag = tag[i:]
return result, nil
}
var err error
for {
switch {
case accept("lsb"), accept("little"):
opts.Order = binary.LittleEndian
case accept("msb"), accept("big"), accept("network"):
opts.Order = binary.BigEndian
case accept("variantbool"):
opts.VariantBoolFlag = true
case accept("invertedbool"):
opts.InvertedBoolFlag = true
case accept("root"):
opts.RootFlag = true
case accept("parent"):
opts.ParentFlag = true
case accept("default"):
opts.DefaultFlag = true
case accept("sizeof="):
if opts.SizeOf, err = acceptIdent(); err != nil {
return fmt.Errorf("sizeof: %v", err)
}
case accept("sizefrom="):
if opts.SizeFrom, err = acceptIdent(); err != nil {
return fmt.Errorf("sizefrom: %v", err)
}
case accept("skip="):
if opts.Skip, err = acceptInt(); err != nil {
return fmt.Errorf("skip: %v", err)
}
case accept("if="):
if opts.IfExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("if: %v", err)
}
case accept("size="):
if opts.SizeExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("size: %v", err)
}
case accept("bits="):
if opts.BitsExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("bits: %v", err)
}
case accept("in="):
if opts.InExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("in: %v", err)
}
case accept("out="):
if opts.OutExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("out: %v", err)
}
case accept("while="):
if opts.WhileExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("while: %v", err)
}
case accept("switch="):
if opts.SwitchExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("switch: %v", err)
}
case accept("case="):
if opts.CaseExpr, err = acceptExpr(); err != nil {
return fmt.Errorf("case: %v", err)
}
case accept("-"):
return errors.New("extra options on ignored field")
default:
typeexpr, err := acceptExpr()
if err != nil {
return fmt.Errorf("struct type: %v", err)
}
parts := strings.SplitN(typeexpr, ":", 2)
opts.Type, err = parseType(parts[0])
if err != nil {
return fmt.Errorf("struct type: %v", err)
}
if len(parts) < 2 {
break
}
if !validBitType(opts.Type) {
return fmt.Errorf("struct type bits specified on non-bitwise type %s", opts.Type)
}
bits, err := strconv.ParseUint(parts[1], 0, 8)
if err != nil {
return errors.New("struct type bits: invalid integer syntax")
}
opts.BitSize = uint8(bits)
if opts.BitSize >= uint8(opts.Type.Bits()) || opts.BitSize == 0 {
return fmt.Errorf("bit size %d out of range (%d to %d)", opts.BitSize, 1, opts.Type.Bits()-1)
}
}
if accept("\x00") {
return nil
}
if !accept(",") {
return errors.New("tag: expected comma")
}
}
}
// mustParseTag calls ParseTag but panics if there is an error, to help make
// sure programming errors surface quickly.
func mustParseTag(tag string) tagOptions {
opt, err := parseTag(tag)
if err != nil {
panic(err)
}
return opt
}
// parseTag parses a struct tag into a TagOptions structure.
func parseTag(tag string) (tagOptions, error) {
opts := tagOptions{}
if err := opts.parse(tag); err != nil {
return tagOptions{}, err
}
return opts, nil
}