/
text_parser.go
222 lines (210 loc) · 4.56 KB
/
text_parser.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
package fbx
import (
"fmt"
"io"
"strconv"
"strings"
)
type tokenType int
const (
Ident tokenType = iota
Number
String
BlockStart
BlockEnd
Comma
Colon
Asterisk
EOL
)
type textParser struct {
r io.Reader
buf []byte
err error
line int
}
func (p *textParser) errorf(f string, a ...interface{}) error {
if p.err == nil {
p.err = fmt.Errorf("%s (line: %d)", fmt.Sprintf(f, a...), p.line+1)
}
return p.err
}
func (p *textParser) read() byte {
if len(p.buf) > 0 {
b := p.buf[0]
p.buf = p.buf[1:]
return b
}
b := []byte{0}
if p.err == nil {
_, err := io.ReadFull(p.r, b)
p.err = err
}
return b[0]
}
func (p *textParser) getToken() (tokenType, string) {
var c byte
for p.err == nil {
c = p.read()
if c == ';' {
for p.err == nil && c != '\n' {
c = p.read()
}
p.line++
return EOL, ""
} else if c == '{' {
return BlockStart, string(c)
} else if c == '}' {
return BlockEnd, string(c)
} else if c == ',' {
return Comma, string(c)
} else if c == ':' {
return Colon, string(c)
} else if c == '*' {
return Asterisk, string(c)
} else if c >= '0' && c <= '9' || c == '.' || c == '-' {
buf := []byte{c}
c = p.read()
for (c >= '0' && c <= '9' || c == '.' || c == 'e' || c == '-') && p.err == nil {
buf = append(buf, c)
c = p.read()
}
if p.err == nil {
p.buf = append(p.buf, c)
}
return Number, string(buf)
} else if c == '\n' {
p.line++
return EOL, ""
} else if c == '"' {
buf := []byte{}
c = p.read()
for c != '"' && p.err == nil {
buf = append(buf, c)
c = p.read()
}
return String, string(buf)
} else if c >= 'A' && c <= 'z' {
buf := []byte{}
for (c >= 'A' && c <= 'z' || c >= '0' && c <= '9' || c == '-') && p.err == nil {
buf = append(buf, c)
c = p.read()
}
if p.err == nil {
p.buf = append(p.buf, c)
}
return Ident, string(buf)
}
}
return EOL, ""
}
func (p *textParser) Skip(t tokenType) bool {
typ, s := p.getToken()
if typ != t && p.err == nil {
p.errorf("Skip token: error %v != %v(%v)", typ, t, s)
}
return typ == t
}
func (p *textParser) parseArrayProp() *Attribute {
_, s := p.getToken()
size, err := strconv.ParseInt(s, 10, 32)
if err != nil {
p.errorf("failed to parse num: '%v'", s)
}
p.Skip(BlockStart)
for p.err == nil {
if _, s := p.getToken(); s == ":" {
break
}
}
var dvalues []float64
var hasPoint bool
for i := 0; i < int(size) && p.err == nil; {
typ, s := p.getToken()
if typ == EOL || typ == Comma {
continue
} else if typ == BlockEnd {
break
} else if typ == Number {
v, _ := strconv.ParseFloat(s, 64)
dvalues = append(dvalues, v)
hasPoint = hasPoint || strings.Contains(s, ".")
} else {
p.errorf("Invalid token: %v", s)
break
}
}
if len(dvalues) != int(size) {
p.errorf("read array:.size: %v != %v", size, len(dvalues))
}
var values interface{} = dvalues
if !hasPoint {
var i32values []int32
for _, v := range dvalues {
i32values = append(i32values, int32(v))
}
values = i32values
}
return &Attribute{Value: values}
}
func (p *textParser) parseNodeList() []*Node {
var nodes []*Node
for p.err == nil {
typ, s := p.getToken()
if typ == EOL {
continue
} else if typ == BlockEnd {
break
} else if typ == Ident {
p.Skip(Colon)
node := &Node{Name: s}
nodes = append(nodes, node)
prev := Colon
for p.err == nil {
typ, s := p.getToken()
if typ == EOL && prev != Comma {
break
}
prev = typ
if typ == BlockStart {
node.Children = p.parseNodeList()
break
} else if typ == Number {
if strings.Contains(s, ".") {
v, err := strconv.ParseFloat(s, 64)
if err != nil {
p.errorf("failed to parse float: '%v'", s)
}
node.Attributes = append(node.Attributes, &Attribute{Value: v})
} else {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
p.errorf("failed to parse integer: '%v'", s)
}
node.Attributes = append(node.Attributes, &Attribute{Value: v})
}
} else if typ == String {
node.Attributes = append(node.Attributes, &Attribute{Value: s})
} else if typ == Asterisk {
node.Attributes = append(node.Attributes, p.parseArrayProp())
}
}
if p.err == io.EOF {
p.err = fmt.Errorf("Unexpected EOF")
}
} else {
p.errorf("Unexpected Token '%v'", s)
break
}
}
return nodes
}
func (p *textParser) Parse() (*Node, error) {
root := &Node{Name: "_FBX_ROOT"}
root.Children = p.parseNodeList()
// root.Dump(os.Stdout, 0, false)
if p.err != nil && p.err != io.EOF {
return nil, p.err
}
return root, nil
}