-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
197 lines (182 loc) · 4.37 KB
/
parse.go
File metadata and controls
197 lines (182 loc) · 4.37 KB
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
package jsonparse
import (
"bytes"
"errors"
)
type Token []byte
var ErrNotJson = errors.New("unable to parse non-json data")
var ErrEOF = errors.New("EOF")
var (
Comma byte = ','
Quote byte = '"'
Colon byte = ':'
BraceOpen byte = '{'
BraceClose byte = '}'
BracketOpen byte = '['
BracketClose byte = ']'
Null string = "null"
BoolTrue string = "true"
BoolFalse string = "false"
)
var Separators = []byte{
Comma,
Colon,
BraceOpen,
BraceClose,
BracketOpen,
BracketClose,
}
var CertainValues = []string{
Null,
BoolTrue,
BoolFalse,
}
type Parser struct {
data []byte
root *Elem
unassignedKey string
currentContainer *Elem
stack []byte
}
func NewParser(data []byte) (p *Parser) {
return &Parser{data: data}
}
func (p *Parser) Parse() (root *Elem, err error) {
var offset int64
var ele *Elem
for {
token, length, err1 := readToken(p.data, offset)
if err1 == ErrEOF {
break
}
if err1 != nil {
err = err1
return
}
if length == 1 && isSeparator(token[0]) {
tk := token[0]
switch {
case tk == BraceOpen || tk == BracketOpen:
if tk == BraceOpen {
ele = newElem(T_OBJECT, p, offset)
} else {
ele = newElem(T_ARRAY, p, offset)
}
p.currentContainer = ele
p.stackPush(tk)
case tk == BraceClose || tk == BracketClose:
pre, err := p.stackPull()
if err != nil {
return nil, ErrNotJson
}
if tk == BraceClose && pre != BraceOpen {
return nil, ErrNotJson
}
if tk == BracketClose && pre != BracketOpen {
return nil, ErrNotJson
}
p.currentContainer.limit = offset + length
p.currentContainer = p.currentContainer.Parent
case tk == Comma:
if p.currentContainer == nil || (p.currentContainer.Type != T_OBJECT && p.currentContainer.Type != T_ARRAY) {
return nil, ErrNotJson
}
case tk == Colon:
if p.currentContainer == nil || (p.currentContainer.Type != T_OBJECT && p.currentContainer.Type != T_ARRAY) {
return nil, ErrNotJson
}
}
} else {
switch {
case token[0] == Quote && token[length-1] == Quote:
//string
if p.currentContainer == nil && offset != 0 {
return nil, ErrNotJson
}
//if the string is not a key in an object, create an element
if p.currentContainer != nil && p.currentContainer.Type == T_OBJECT && p.unassignedKey == "" {
p.unassignedKey = string(bytes.Trim(token, "\""))
} else {
ele = newElem(T_STRING, p, offset+1)
ele.limit = offset + length - 1
}
case isCertainValue(token, length):
if string(token) == Null {
ele = newElem(T_NULL, p, offset)
} else {
ele = newElem(T_BOOL, p, offset)
}
ele.limit = offset + length
default:
ele = newElem(T_NUMBER, p, offset)
ele.limit = offset + length
}
}
if p.root == nil && ele != nil {
for _, b := range p.data {
ele.data = append(ele.data, b)
}
p.root = ele
root = ele
}
offset += length
}
return
}
//push a token to the stack
func (p *Parser) stackPush(token byte) {
p.stack = append(p.stack, token)
}
//pull the top element from the stack
func (p *Parser) stackPull() (byte, error) {
length := len(p.stack)
if length == 0 {
return byte(0), errors.New("can't pull from an empty stack")
}
b := p.stack[length-1]
p.stack = p.stack[:length-1]
return b, nil
}
func readToken(data []byte, offset int64) (token []byte, length int64, err error) {
if offset >= int64(len(data)) {
return []byte{}, 0, ErrEOF
}
switch {
case isSeparator(data[offset]):
return data[offset : offset+1], 1, nil
case data[offset] == '"':
//string, begin with quote, keep reading until the other half comes
for i := offset + 1; i < int64(len(data)); i++ {
if data[i] == '"' && data[i-1] != '\\' {
return data[offset : i+1], i - offset + 1, nil
}
}
return []byte{}, 0, ErrNotJson
default:
//number, bool or null, read till a separator
for i := offset + 1; i < int64(len(data)); i++ {
if isSeparator(data[i]) && data[i-1] != '\\' {
return data[offset:i], i - offset, nil
}
}
return []byte{}, 0, ErrNotJson
}
}
func isCertainValue(token []byte, length int64) bool {
if length > 5 {
return false
}
tk := string(token)
if tk == BoolTrue || tk == BoolFalse || tk == Null {
return true
}
return false
}
func isSeparator(b byte) bool {
for _, s := range Separators {
if s == b {
return true
}
}
return false
}