-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
164 lines (147 loc) · 3.28 KB
/
field.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
package thrifter
import "strconv"
// Field represent a field within struct/union/exception
type Field struct {
NodeCommonField
ID int
Requiredness string
FieldType *FieldType
Ident string
DefaultValue *ConstValue
Options []*Option
}
func NewField(parent Node) *Field {
return &Field{
NodeCommonField: NodeCommonField{
Parent: parent,
},
}
}
func (r *Field) NodeType() string {
return "Field"
}
func (r *Field) NodeValue() interface{} {
return *r
}
func (r *Field) String() string {
return toString(r.StartToken, r.EndToken)
}
func (r *Field) parse(p *Parser) (err error) {
p.peekNonWhitespace()
// parse ID
idToken, err, _, isInt := p.nextNumber()
if err != nil {
return err
}
if !isInt {
return p.unexpected(idToken.Value, "integer")
}
id, err := strconv.ParseInt(idToken.Value, 10, 64)
if err != nil {
return err
}
r.ID = int(id)
r.StartToken = idToken
ru := p.peekNonWhitespace()
if toToken(string(ru)) != T_COLON {
return p.unexpected(string(ru), ":")
}
p.next() // consume :
// parse requiredness
p.peekNonWhitespace()
tok := p.nextIdent(true)
if tok.Value == "required" || tok.Value == "optional" {
r.Requiredness = tok.Value
} else {
p.buf = tok
}
// parse field type
r.FieldType = NewFieldType(r)
if err = r.FieldType.parse(p); err != nil {
return err
}
// parse identifier
p.peekNonWhitespace()
identTok := p.nextIdent(false)
r.Ident = identTok.Raw
// parse DefaultValue/Options
ru = p.peekNonWhitespace()
if toToken(string(ru)) == T_EQUALS {
p.next() // consume =
cst, err := r.parseDefaultValue(p)
if err != nil {
return err
}
r.DefaultValue = cst
// see if there are options
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_LEFTPAREN {
p.next() // consume (
var rightParenTok *Token
r.Options, rightParenTok, err = r.parseOptions(p)
if err != nil {
return err
}
if err = r.parseEnd(rightParenTok, p); err != nil {
return err
}
} else {
if err = r.parseEnd(cst.EndToken, p); err != nil {
return err
}
}
} else if toToken(string(ru)) == T_LEFTPAREN {
p.next() // consume (
var rightParenTok *Token
r.Options, rightParenTok, err = r.parseOptions(p)
if err != nil {
return err
}
if err = r.parseEnd(rightParenTok, p); err != nil {
return err
}
} else {
if err = r.parseEnd(identTok, p); err != nil {
return err
}
}
return
}
func (r *Field) parseEnd(defaultEnd *Token, p *Parser) (err error) {
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_COMMA || toToken(string(ru)) == T_SEMICOLON {
r.EndToken = p.next()
} else {
r.EndToken = defaultEnd
}
return
}
func (r *Field) parseDefaultValue(p *Parser) (res *ConstValue, err error) {
p.peekNonWhitespace()
res = NewConstValue(r)
if err = res.parse(p); err != nil {
return nil, err
}
return
}
func (r *Field) parseOptions(p *Parser) (options []*Option, rightParenTok *Token, err error) {
var currOption *Option
for {
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_RIGHTPAREN {
rightParenTok = p.next()
break
}
currOption = NewOption(r)
err = currOption.parse(p)
if err != nil {
return
}
options = append(options, currOption)
ru = p.peekNonWhitespace()
if toToken(string(ru)) == T_COMMA {
p.next() // consume comma
}
}
return
}