-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstruct.go
99 lines (90 loc) · 1.75 KB
/
struct.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
package thrifter
const (
STRUCT = iota + 1
UNION
EXCEPTION
)
type Struct struct {
NodeCommonField
Type int
Ident string
Elems []*Field
Options []*Option
ElemsMap map[string]*Field // startToken hash => Field node
}
func NewStruct(start *Token, parent Node) *Struct {
t := 0
switch start.Value {
case "struct":
t = STRUCT
case "union":
t = UNION
case "exception":
t = EXCEPTION
}
return &Struct{
NodeCommonField: NodeCommonField{
Parent: parent,
StartToken: start,
},
Type: t,
ElemsMap: map[string]*Field{},
}
}
func (r *Struct) NodeType() string {
switch r.Type {
case STRUCT:
return "Struct"
case UNION:
return "Union"
case EXCEPTION:
return "Exception"
}
return ""
}
func (r *Struct) NodeValue() interface{} {
return *r
}
func (r *Struct) String() string {
return toString(r.StartToken, r.EndToken)
}
func (r *Struct) patchFieldToMap(node *Field) {
hash := GenTokenHash(node.StartToken)
r.ElemsMap[hash] = node
}
func (r *Struct) parse(p *Parser) (err error) {
p.peekNonWhitespace()
identTok := p.nextIdent(false)
r.Ident = identTok.Raw
ru := p.peekNonWhitespace()
if toToken(string(ru)) != T_LEFTCURLY {
return p.unexpected(string(ru), "{")
}
p.next() // consume {
var rightParenTok *Token
for {
ru := p.peekNonWhitespace()
if toToken(string(ru)) == T_RIGHTCURLY {
rightParenTok = p.next()
break
}
elem := NewField(r)
if err = elem.parse(p); err != nil {
return err
}
r.patchFieldToMap(elem)
r.Elems = append(r.Elems, elem)
}
// parse options
ru = p.peekNonWhitespace()
if toToken(string(ru)) != T_LEFTPAREN {
r.EndToken = rightParenTok
return
}
p.next() // consume (
r.Options, r.EndToken, err = parseOptions(p, r)
if err != nil {
return err
}
return
}