-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
259 lines (224 loc) · 5.43 KB
/
Copy pathindex.js
File metadata and controls
259 lines (224 loc) · 5.43 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const DIGIT = '0123456789';
const STRING = /^"[\s\S]*"$/;
const types = {
a: 'array',
b: 'boolean',
d: 'double',
i: 'integer',
s: 'string',
N: 'null',
};
function parse(str, raw) {
let stack = []; // array stack
let nodes = []; // current array
let n = Infinity; // array length
let o = 0; // array offset
let errors = [];
NODES: for (let i = 0; i < str.length; i++) {
let ch = str.charAt(i);
if (ch == '') break;
let error, type, value, start = i;
NODE: do {
// Look for a value type.
type = types[ch];
if (!type) {
// Array may be closed too early.
if (ch == '}') {
if (errors.length) break NODES;
if (stack.length) {
type = 'array';
error = 'Array closed too early';
break;
}
}
error = 'Unexpected char: ' + ch;
break;
}
// Null values are easy.
if (ch == 'N') {
value = null;
break;
}
// Arrays and strings have a length.
let length = '';
if (ch == 'a' || ch == 's') {
// Look for a colon.
ch = str.charAt(++i);
if (ch != ':') {
error = 'Missing colon';
break;
}
// Look for the length.
while (~DIGIT.indexOf(ch = str.charAt(++i))) {
length += ch;
}
length = Number(length);
}
else {
ch = str.charAt(++i);
}
// Look for a colon.
if (ch != ':') {
error = 'Missing colon';
break;
}
// Look for the value.
ch = str.charAt(++i);
switch (type) {
case 'array':
if (ch != '{') {
error = 'Array never opened';
break NODE;
}
i++;
// Preserve parent array.
stack.push([nodes, n, o]);
// Create our array.
nodes = [];
n = (2 * length) + 1;
o = start;
break;
case 'boolean':
if (ch != '0' && ch != '1') {
error = 'Invalid boolean value (must be 0 or 1)';
break NODE;
}
i++;
value = ch == '1';
break;
case 'double':
value = str.slice(i, str.indexOf(';', i));
if (isNaN(parseFloat(value))) {
error = 'Invalid double value';
break NODE;
}
i += value.length;
value = parseFloat(value);
break;
case 'integer':
value = str.slice(i, str.indexOf(';', i));
if (isNaN(parseInt(value))) {
error = 'Invalid integer value';
break NODE;
}
i += value.length;
value = parseInt(value);
break;
case 'string':
value = str.substr(i, length += 2);
if (!STRING.test(value)) {
error = 'Invalid string value';
break NODE;
}
i += length;
value = value.slice(1, -1);
break;
}
i--;
// Parse the next node.
} while (0);
if (!error) {
// Look for a semicolon.
if (type != 'array') {
ch = str.charAt(++i);
if (ch != ';') {
error = 'Missing semicolon';
}
else if (raw) {
nodes.push(value);
}
else {
nodes.push({
type,
value,
start,
end: i + 1,
});
}
}
// Look for closed arrays.
while (--n == 0) {
ch = str.charAt(++i);
if (ch == '') {
i = o;
type = 'array';
error = 'Array never closed';
break;
}
if (ch != '}') {
type = 'array';
error = 'Array length exceeded';
break;
}
let parent = stack.pop();
if (!parent) {
type = 'array';
error = 'Unexpected char: }';
break;
}
// Create an array node.
let node = raw ? nodes : {
type: 'array',
value: nodes,
start: o,
end: i + 1,
};
// Raw mode may use an array transformer.
if (typeof raw == 'function') {
node = raw(node);
}
// Restore the parent array.
[nodes, n, o] = parent;
nodes.push(node);
}
}
if (error) {
// Throw the error in raw mode.
if (raw) {
let e = SyntaxError(error);
e.start = i;
throw e;
}
// Push an error node and keep going.
let node = {
type: 'error',
error,
start: i,
};
nodes.push(node);
errors.push(node);
// Continuing past array errors is hard.
if (type == 'array') {
break;
}
// Find a semicolon.
while (true) {
if (ch == ';') {
if (type != 'string') break;
if (str[i - 1] == '"') break;
}
ch = str.charAt(++i);
if (ch == '') break;
}
// Reset the `error` variable.
error = null;
}
}
if (errors.length) {
return errors;
}
// Detect unfinished arrays.
if (stack.length && n > 0) {
errors.push({
type: 'error',
error: 'Array never finished',
start: o,
});
return errors;
}
return nodes;
}
exports.parse = parse;
exports.reduce = require('./reduce');
exports.serialize = require('./serialize');
exports.unserialize = (str, fn) => parse(str, fn || true);