-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
156 lines (133 loc) · 4.51 KB
/
scanner.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
package flatjson
func scanPairName(data []byte, from int) (Prefix, int, error) {
// scan the name
pos, err := scanString(data, from)
pfx := newObjectKeyPrefix(pos.From, pos.To)
if err != nil {
return pfx, 0, syntaxErr(from, expectingNameBeforeValue, err.(*SyntaxError))
}
// scan the separator
i, err := scanSeparator(data, pos.To)
if err != nil {
return pfx, 0, err
}
return pfx, i, nil
}
func scanSeparator(data []byte, from int) (int, error) {
i := skipWhitespace(data, from)
if i >= len(data) {
return i, syntaxErr(i, endOfDataNoColon, nil)
}
if data[i] != ':' {
return i, syntaxErr(i, noColonFound, nil)
}
i++
i = skipWhitespace(data, i)
if i >= len(data) {
return i, syntaxErr(i, endOfDataNoValueForName, nil)
}
return i, nil
}
// wip
// ScanArray according to the spec at http://www.json.org/
// but ignoring nested objects and arrays
func ScanArray(data []byte, from int, cb *Callbacks) (pos Pos, found bool, err error) {
return scanArray(data, from, nil, cb)
}
func scanArray(data []byte, from int, prefixes []Prefix, cb *Callbacks) (pos Pos, found bool, _ error) {
pos.From, pos.To = -1, -1
start := skipWhitespace(data, from)
if len(data) == 0 || data[start] != '[' {
return pos, false, syntaxErr(start, noOpeningSquareBracketFound, nil)
}
i := start + 1
for index := -1; i < len(data); i++ {
index++
i = skipWhitespace(data, i)
if i >= len(data) {
return pos, false, syntaxErr(i, endOfDataNoNamePair, nil)
}
if data[i] == ']' {
return Pos{start, i + 1}, true, nil
}
// decide if the value is a number, string, object, array, bool or null
et := GuessNextEntityType(data, i)
var (
valPos Pos
err error
)
if et == EntityType_String { // strings
valPos, err = scanString(data, i)
if err != nil {
return pos, false, syntaxErr(i, beginStringValueButError, err.(*SyntaxError))
}
if cb != nil && cb.OnString != nil && cb.MaxDepth >= len(prefixes) {
cb.OnString(prefixes, String{Name: newArrayIndexPrefix(index), Value: valPos})
}
i = valPos.To
} else if et == EntityType_Object { // objects
valPos, found, err = scanObject(data, i, append(prefixes, newArrayIndexPrefix(index)), cb) // TODO: fix recursion
if err != nil {
return Pos{}, found, syntaxErr(i, beginObjectValueButError, err.(*SyntaxError))
} else if !found {
return Pos{}, found, syntaxErr(i, expectValueButNoKnownType, nil)
}
i = valPos.To
} else if et == EntityType_Array { // arrays
valPos, found, err = scanArray(data, i, append(prefixes, newArrayIndexPrefix(index)), cb) // TODO: fix recursion
if err != nil {
return Pos{}, found, syntaxErr(i, beginArrayValueButError, err.(*SyntaxError))
} else if !found {
return Pos{}, found, syntaxErr(i, expectValueButNoKnownType, nil)
}
i = valPos.To
} else if et == EntityType_Number { // numbers
val, j, err := ScanNumber(data, i)
if err != nil {
return pos, false, syntaxErr(i, beginNumberValueButError, err.(*SyntaxError))
}
j = skipWhitespace(data, j)
if j < len(data) && data[j] != ',' && data[j] != ']' {
return pos, false, syntaxErr(i, malformedNumber, nil)
}
if cb != nil && cb.OnNumber != nil && cb.MaxDepth >= len(prefixes) {
cb.OnNumber(prefixes, Number{Name: newArrayIndexPrefix(index), Value: val})
}
valPos = Pos{From: i, To: j}
i = j
} else if et == EntityType_Boolean_True {
if cb != nil && cb.OnBoolean != nil && cb.MaxDepth >= len(prefixes) {
cb.OnBoolean(prefixes, Bool{Name: newArrayIndexPrefix(index), Value: true})
}
valPos = Pos{From: i, To: i + 4}
i += 4
} else if et == EntityType_Boolean_False {
if cb != nil && cb.OnBoolean != nil && cb.MaxDepth >= len(prefixes) {
cb.OnBoolean(prefixes, Bool{Name: newArrayIndexPrefix(index), Value: false})
}
valPos = Pos{From: i, To: i + 5}
i += 5
} else if et == EntityType_Null {
if cb != nil && cb.OnNull != nil && cb.MaxDepth >= len(prefixes) {
cb.OnNull(prefixes, Null{Name: newArrayIndexPrefix(index)})
}
valPos = Pos{From: i, To: i + 4}
i += 4
} else {
return pos, false, syntaxErr(i, expectValueButNoKnownType, nil)
}
if cb != nil && cb.OnRaw != nil && cb.MaxDepth >= len(prefixes) {
cb.OnRaw(prefixes, newArrayIndexPrefix(index), valPos)
}
i = skipWhitespace(data, i)
if i < len(data) {
if data[i] == ',' {
// more values to come
// TODO(antoine): be kind and accept trailing commas
} else if data[i] == ']' {
return Pos{start, i + 1}, true, nil
}
}
}
return pos, false, syntaxErr(i, endOfDataNoClosingSquareBracket, nil)
}