Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80738 views
1
/*global Buffer*/
2
// Named constants with unique integer values
3
var C = {};
4
// Tokens
5
var LEFT_BRACE = C.LEFT_BRACE = 0x1;
6
var RIGHT_BRACE = C.RIGHT_BRACE = 0x2;
7
var LEFT_BRACKET = C.LEFT_BRACKET = 0x3;
8
var RIGHT_BRACKET = C.RIGHT_BRACKET = 0x4;
9
var COLON = C.COLON = 0x5;
10
var COMMA = C.COMMA = 0x6;
11
var TRUE = C.TRUE = 0x7;
12
var FALSE = C.FALSE = 0x8;
13
var NULL = C.NULL = 0x9;
14
var STRING = C.STRING = 0xa;
15
var NUMBER = C.NUMBER = 0xb;
16
// Tokenizer States
17
var START = C.START = 0x11;
18
var TRUE1 = C.TRUE1 = 0x21;
19
var TRUE2 = C.TRUE2 = 0x22;
20
var TRUE3 = C.TRUE3 = 0x23;
21
var FALSE1 = C.FALSE1 = 0x31;
22
var FALSE2 = C.FALSE2 = 0x32;
23
var FALSE3 = C.FALSE3 = 0x33;
24
var FALSE4 = C.FALSE4 = 0x34;
25
var NULL1 = C.NULL1 = 0x41;
26
var NULL2 = C.NULL3 = 0x42;
27
var NULL3 = C.NULL2 = 0x43;
28
var NUMBER1 = C.NUMBER1 = 0x51;
29
var NUMBER2 = C.NUMBER2 = 0x52;
30
var NUMBER3 = C.NUMBER3 = 0x53;
31
var NUMBER4 = C.NUMBER4 = 0x54;
32
var NUMBER5 = C.NUMBER5 = 0x55;
33
var NUMBER6 = C.NUMBER6 = 0x56;
34
var NUMBER7 = C.NUMBER7 = 0x57;
35
var NUMBER8 = C.NUMBER8 = 0x58;
36
var STRING1 = C.STRING1 = 0x61;
37
var STRING2 = C.STRING2 = 0x62;
38
var STRING3 = C.STRING3 = 0x63;
39
var STRING4 = C.STRING4 = 0x64;
40
var STRING5 = C.STRING5 = 0x65;
41
var STRING6 = C.STRING6 = 0x66;
42
// Parser States
43
var VALUE = C.VALUE = 0x71;
44
var KEY = C.KEY = 0x72;
45
// Parser Modes
46
var OBJECT = C.OBJECT = 0x81;
47
var ARRAY = C.ARRAY = 0x82;
48
49
// Slow code to string converter (only used when throwing syntax errors)
50
function toknam(code) {
51
var keys = Object.keys(C);
52
for (var i = 0, l = keys.length; i < l; i++) {
53
var key = keys[i];
54
if (C[key] === code) { return key; }
55
}
56
return code && ("0x" + code.toString(16));
57
}
58
59
60
function Parser() {
61
this.tState = START;
62
this.value = undefined;
63
64
this.string = undefined; // string data
65
this.unicode = undefined; // unicode escapes
66
67
// For number parsing
68
this.negative = undefined;
69
this.magnatude = undefined;
70
this.position = undefined;
71
this.exponent = undefined;
72
this.negativeExponent = undefined;
73
74
this.key = undefined;
75
this.mode = undefined;
76
this.stack = [];
77
this.state = VALUE;
78
this.bytes_remaining = 0; // number of bytes remaining in multi byte utf8 char to read after split boundary
79
this.bytes_in_sequence = 0; // bytes in multi byte utf8 char to read
80
this.temp_buffs = { "2": new Buffer(2), "3": new Buffer(3), "4": new Buffer(4) }; // for rebuilding chars split before boundary is reached
81
}
82
var proto = Parser.prototype;
83
proto.charError = function (buffer, i) {
84
this.onError(new Error("Unexpected " + JSON.stringify(String.fromCharCode(buffer[i])) + " at position " + i + " in state " + toknam(this.tState)));
85
};
86
proto.onError = function (err) { throw err; };
87
proto.write = function (buffer) {
88
if (typeof buffer === "string") buffer = new Buffer(buffer);
89
//process.stdout.write("Input: ");
90
//console.dir(buffer.toString());
91
var n;
92
for (var i = 0, l = buffer.length; i < l; i++) {
93
if (this.tState === START){
94
n = buffer[i];
95
if(n === 0x7b){ this.onToken(LEFT_BRACE, "{"); // {
96
}else if(n === 0x7d){ this.onToken(RIGHT_BRACE, "}"); // }
97
}else if(n === 0x5b){ this.onToken(LEFT_BRACKET, "["); // [
98
}else if(n === 0x5d){ this.onToken(RIGHT_BRACKET, "]"); // ]
99
}else if(n === 0x3a){ this.onToken(COLON, ":"); // :
100
}else if(n === 0x2c){ this.onToken(COMMA, ","); // ,
101
}else if(n === 0x74){ this.tState = TRUE1; // t
102
}else if(n === 0x66){ this.tState = FALSE1; // f
103
}else if(n === 0x6e){ this.tState = NULL1; // n
104
}else if(n === 0x22){ this.string = ""; this.tState = STRING1; // "
105
}else if(n === 0x2d){ this.negative = true; this.tState = NUMBER1; // -
106
}else if(n === 0x30){ this.magnatude = 0; this.tState = NUMBER2; // 0
107
}else{
108
if (n > 0x30 && n < 0x40) { // 1-9
109
this.magnatude = n - 0x30; this.tState = NUMBER3;
110
} else if (n === 0x20 || n === 0x09 || n === 0x0a || n === 0x0d) {
111
// whitespace
112
} else { this.charError(buffer, i); }
113
}
114
}else if (this.tState === STRING1){ // After open quote
115
n = buffer[i]; // get current byte from buffer
116
// check for carry over of a multi byte char split between data chunks
117
// & fill temp buffer it with start of this data chunk up to the boundary limit set in the last iteration
118
if (this.bytes_remaining > 0) {
119
for (var j = 0; j < this.bytes_remaining; j++) {
120
this.temp_buffs[this.bytes_in_sequence][this.bytes_in_sequence - this.bytes_remaining + j] = buffer[j];
121
}
122
this.string += this.temp_buffs[this.bytes_in_sequence].toString();
123
this.bytes_in_sequence = this.bytes_remaining = 0;
124
i = i + j - 1;
125
} else if (this.bytes_remaining === 0 && n >= 128) { // else if no remainder bytes carried over, parse multi byte (>=128) chars one at a time
126
if ((n >= 194) && (n <= 223)) this.bytes_in_sequence = 2;
127
if ((n >= 224) && (n <= 239)) this.bytes_in_sequence = 3;
128
if ((n >= 240) && (n <= 244)) this.bytes_in_sequence = 4;
129
if ((this.bytes_in_sequence + i) > buffer.length) { // if bytes needed to complete char fall outside buffer length, we have a boundary split
130
for (var k = 0; k <= (buffer.length - 1 - i); k++) {
131
this.temp_buffs[this.bytes_in_sequence][k] = buffer[i + k]; // fill temp buffer of correct size with bytes available in this chunk
132
}
133
this.bytes_remaining = (i + this.bytes_in_sequence) - buffer.length;
134
i = buffer.length - 1;
135
} else {
136
this.string += buffer.slice(i, (i + this.bytes_in_sequence)).toString();
137
i = i + this.bytes_in_sequence - 1;
138
}
139
} else if (n === 0x22) { this.tState = START; this.onToken(STRING, this.string); this.string = undefined; }
140
else if (n === 0x5c) { this.tState = STRING2; }
141
else if (n >= 0x20) { this.string += String.fromCharCode(n); }
142
else { this.charError(buffer, i); }
143
}else if (this.tState === STRING2){ // After backslash
144
n = buffer[i];
145
if(n === 0x22){ this.string += "\""; this.tState = STRING1;
146
}else if(n === 0x5c){ this.string += "\\"; this.tState = STRING1;
147
}else if(n === 0x2f){ this.string += "\/"; this.tState = STRING1;
148
}else if(n === 0x62){ this.string += "\b"; this.tState = STRING1;
149
}else if(n === 0x66){ this.string += "\f"; this.tState = STRING1;
150
}else if(n === 0x6e){ this.string += "\n"; this.tState = STRING1;
151
}else if(n === 0x72){ this.string += "\r"; this.tState = STRING1;
152
}else if(n === 0x74){ this.string += "\t"; this.tState = STRING1;
153
}else if(n === 0x75){ this.unicode = ""; this.tState = STRING3;
154
}else{
155
this.charError(buffer, i);
156
}
157
}else if (this.tState === STRING3 || this.tState === STRING4 || this.tState === STRING5 || this.tState === STRING6){ // unicode hex codes
158
n = buffer[i];
159
// 0-9 A-F a-f
160
if ((n >= 0x30 && n < 0x40) || (n > 0x40 && n <= 0x46) || (n > 0x60 && n <= 0x66)) {
161
this.unicode += String.fromCharCode(n);
162
if (this.tState++ === STRING6) {
163
this.string += String.fromCharCode(parseInt(this.unicode, 16));
164
this.unicode = undefined;
165
this.tState = STRING1;
166
}
167
} else {
168
this.charError(buffer, i);
169
}
170
}else if (this.tState === NUMBER1){ // after minus
171
n = buffer[i];
172
if (n === 0x30) { this.magnatude = 0; this.tState = NUMBER2; }
173
else if (n > 0x30 && n < 0x40) { this.magnatude = n - 0x30; this.tState = NUMBER3; }
174
else { this.charError(buffer, i); }
175
}else if (this.tState === NUMBER2){ // * After initial zero
176
n = buffer[i];
177
if(n === 0x2e){ // .
178
this.position = 0.1; this.tState = NUMBER4;
179
}else if(n === 0x65 || n === 0x45){ // e/E
180
this.exponent = 0; this.tState = NUMBER6;
181
}else{
182
this.tState = START;
183
this.onToken(NUMBER, 0);
184
this.magnatude = undefined;
185
this.negative = undefined;
186
i--;
187
}
188
}else if (this.tState === NUMBER3){ // * After digit (before period)
189
n = buffer[i];
190
if(n === 0x2e){ // .
191
this.position = 0.1; this.tState = NUMBER4;
192
}else if(n === 0x65 || n === 0x45){ // e/E
193
this.exponent = 0; this.tState = NUMBER6;
194
}else{
195
if (n >= 0x30 && n < 0x40) { this.magnatude = this.magnatude * 10 + n - 0x30; }
196
else {
197
this.tState = START;
198
if (this.negative) {
199
this.magnatude = -this.magnatude;
200
this.negative = undefined;
201
}
202
this.onToken(NUMBER, this.magnatude);
203
this.magnatude = undefined;
204
i--;
205
}
206
}
207
}else if (this.tState === NUMBER4){ // After period
208
n = buffer[i];
209
if (n >= 0x30 && n < 0x40) { // 0-9
210
this.magnatude += this.position * (n - 0x30);
211
this.position /= 10;
212
this.tState = NUMBER5;
213
} else { this.charError(buffer, i); }
214
}else if (this.tState === NUMBER5){ // * After digit (after period)
215
n = buffer[i];
216
if (n >= 0x30 && n < 0x40) { // 0-9
217
this.magnatude += this.position * (n - 0x30);
218
this.position /= 10;
219
}
220
else if (n === 0x65 || n === 0x45) { this.exponent = 0; this.tState = NUMBER6; } // E/e
221
else {
222
this.tState = START;
223
if (this.negative) {
224
this.magnatude = -this.magnatude;
225
this.negative = undefined;
226
}
227
this.onToken(NUMBER, this.negative ? -this.magnatude : this.magnatude);
228
this.magnatude = undefined;
229
this.position = undefined;
230
i--;
231
}
232
}else if (this.tState === NUMBER6){ // After E
233
n = buffer[i];
234
if (n === 0x2b || n === 0x2d) { // +/-
235
if (n === 0x2d) { this.negativeExponent = true; }
236
this.tState = NUMBER7;
237
}
238
else if (n >= 0x30 && n < 0x40) {
239
this.exponent = this.exponent * 10 + (n - 0x30);
240
this.tState = NUMBER8;
241
}
242
else { this.charError(buffer, i); }
243
}else if (this.tState === NUMBER7){ // After +/-
244
n = buffer[i];
245
if (n >= 0x30 && n < 0x40) { // 0-9
246
this.exponent = this.exponent * 10 + (n - 0x30);
247
this.tState = NUMBER8;
248
}
249
else { this.charError(buffer, i); }
250
}else if (this.tState === NUMBER8){ // * After digit (after +/-)
251
n = buffer[i];
252
if (n >= 0x30 && n < 0x40) { // 0-9
253
this.exponent = this.exponent * 10 + (n - 0x30);
254
}
255
else {
256
if (this.negativeExponent) {
257
this.exponent = -this.exponent;
258
this.negativeExponent = undefined;
259
}
260
this.magnatude *= Math.pow(10, this.exponent);
261
this.exponent = undefined;
262
if (this.negative) {
263
this.magnatude = -this.magnatude;
264
this.negative = undefined;
265
}
266
this.tState = START;
267
this.onToken(NUMBER, this.magnatude);
268
this.magnatude = undefined;
269
i--;
270
}
271
}else if (this.tState === TRUE1){ // r
272
if (buffer[i] === 0x72) { this.tState = TRUE2; }
273
else { this.charError(buffer, i); }
274
}else if (this.tState === TRUE2){ // u
275
if (buffer[i] === 0x75) { this.tState = TRUE3; }
276
else { this.charError(buffer, i); }
277
}else if (this.tState === TRUE3){ // e
278
if (buffer[i] === 0x65) { this.tState = START; this.onToken(TRUE, true); }
279
else { this.charError(buffer, i); }
280
}else if (this.tState === FALSE1){ // a
281
if (buffer[i] === 0x61) { this.tState = FALSE2; }
282
else { this.charError(buffer, i); }
283
}else if (this.tState === FALSE2){ // l
284
if (buffer[i] === 0x6c) { this.tState = FALSE3; }
285
else { this.charError(buffer, i); }
286
}else if (this.tState === FALSE3){ // s
287
if (buffer[i] === 0x73) { this.tState = FALSE4; }
288
else { this.charError(buffer, i); }
289
}else if (this.tState === FALSE4){ // e
290
if (buffer[i] === 0x65) { this.tState = START; this.onToken(FALSE, false); }
291
else { this.charError(buffer, i); }
292
}else if (this.tState === NULL1){ // u
293
if (buffer[i] === 0x75) { this.tState = NULL2; }
294
else { this.charError(buffer, i); }
295
}else if (this.tState === NULL2){ // l
296
if (buffer[i] === 0x6c) { this.tState = NULL3; }
297
else { this.charError(buffer, i); }
298
}else if (this.tState === NULL3){ // l
299
if (buffer[i] === 0x6c) { this.tState = START; this.onToken(NULL, null); }
300
else { this.charError(buffer, i); }
301
}
302
}
303
};
304
proto.onToken = function (token, value) {
305
// Override this to get events
306
};
307
308
proto.parseError = function (token, value) {
309
this.onError(new Error("Unexpected " + toknam(token) + (value ? ("(" + JSON.stringify(value) + ")") : "") + " in state " + toknam(this.state)));
310
};
311
proto.onError = function (err) { throw err; };
312
proto.push = function () {
313
this.stack.push({value: this.value, key: this.key, mode: this.mode});
314
};
315
proto.pop = function () {
316
var value = this.value;
317
var parent = this.stack.pop();
318
this.value = parent.value;
319
this.key = parent.key;
320
this.mode = parent.mode;
321
this.emit(value);
322
if (!this.mode) { this.state = VALUE; }
323
};
324
proto.emit = function (value) {
325
if (this.mode) { this.state = COMMA; }
326
this.onValue(value);
327
};
328
proto.onValue = function (value) {
329
// Override me
330
};
331
proto.onToken = function (token, value) {
332
//console.log("OnToken: state=%s token=%s %s", toknam(this.state), toknam(token), value?JSON.stringify(value):"");
333
if(this.state === VALUE){
334
if(token === STRING || token === NUMBER || token === TRUE || token === FALSE || token === NULL){
335
if (this.value) {
336
this.value[this.key] = value;
337
}
338
this.emit(value);
339
}else if(token === LEFT_BRACE){
340
this.push();
341
if (this.value) {
342
this.value = this.value[this.key] = {};
343
} else {
344
this.value = {};
345
}
346
this.key = undefined;
347
this.state = KEY;
348
this.mode = OBJECT;
349
}else if(token === LEFT_BRACKET){
350
this.push();
351
if (this.value) {
352
this.value = this.value[this.key] = [];
353
} else {
354
this.value = [];
355
}
356
this.key = 0;
357
this.mode = ARRAY;
358
this.state = VALUE;
359
}else if(token === RIGHT_BRACE){
360
if (this.mode === OBJECT) {
361
this.pop();
362
} else {
363
this.parseError(token, value);
364
}
365
}else if(token === RIGHT_BRACKET){
366
if (this.mode === ARRAY) {
367
this.pop();
368
} else {
369
this.parseError(token, value);
370
}
371
}else{
372
this.parseError(token, value);
373
}
374
}else if(this.state === KEY){
375
if (token === STRING) {
376
this.key = value;
377
this.state = COLON;
378
} else if (token === RIGHT_BRACE) {
379
this.pop();
380
} else {
381
this.parseError(token, value);
382
}
383
}else if(this.state === COLON){
384
if (token === COLON) { this.state = VALUE; }
385
else { this.parseError(token, value); }
386
}else if(this.state === COMMA){
387
if (token === COMMA) {
388
if (this.mode === ARRAY) { this.key++; this.state = VALUE; }
389
else if (this.mode === OBJECT) { this.state = KEY; }
390
391
} else if (token === RIGHT_BRACKET && this.mode === ARRAY || token === RIGHT_BRACE && this.mode === OBJECT) {
392
this.pop();
393
} else {
394
this.parseError(token, value);
395
}
396
}else{
397
this.parseError(token, value);
398
}
399
};
400
401
module.exports = Parser;
402
403