-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser.js
495 lines (423 loc) · 13.3 KB
/
parser.js
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
var util = require('util');
var Writable = require('stream').Writable;
var toDate = require('./toDate');
var toBool = require('./toBool');
util.inherits(Parser, Writable);
function Parser(options) {
if (!(this instanceof Parser)) {
return new Parser(options);
}
Writable.call(this, options);
this.on('finish', function() {
this.isEnding = true;
this._parse(function() { });
}.bind(this));
// The current object being population
this.obj = {};
// Data structure to monitor the object as we go deeper into the hierarchy.
// Once the object has been read completely, it is popped off
this.nest = [];
// Current byte in the stream
this.current = '';
// Buffer used to aggregate characters across chunks
this.tok = new Buffer(256);
}
var eq = '='.charCodeAt(0);
var rcurl = '{'.charCodeAt(0);
var lcurl = '}'.charCodeAt(0);
var hash = '#'.charCodeAt(0);
var comma = ','.charCodeAt(0);
var semicolon = ';'.charCodeAt(0);
var quote = '"'.charCodeAt(0);
var tab = '\t'.charCodeAt(0);
var space = ' '.charCodeAt(0);
var newline = '\n'.charCodeAt(0);
var carriage = '\r'.charCodeAt(0);
// Returns whether the given byte is untyped. Untyped means it is not a
// delimiter for these types of files. Examples of untyped are alphanumeric
// characters and whitespace
Parser._untyped = function(c) {
return !(c === eq || c === rcurl || c === lcurl || c === hash ||
c === comma || c === semicolon);
};
// Advances the stream through all whitespace and comments
Parser.prototype._trimmer = function() {
var retry = false;
do {
while (this._read() && Parser._isspace(this.current)) {
}
retry = false;
if (Parser._untyped(this.current) && !Parser._isspace(this.current)) {
this._unpeek();
} else if (this.current === hash) {
while (this._read() && this.current !== carriage) {
}
retry = true;
}
} while (retry);
};
// Returns whether the given byte is a white space character
Parser._isspace = function(c) {
return c === space || c === tab || c === newline || c === carriage;
};
Parser.prototype._read = function() {
if (this.eoc === true) {
return false;
}
if (this.readFirst) {
this.current = this.prevBuf[this.bufPos++];
if (this.bufPos === this.prevBuf.length) {
this.readFirst = false;
this.bufPos = 0;
}
return true;
}
if (this.bufPos < this.buf.length) {
this.current = this.buf[this.bufPos++];
return true;
}
this.eoc = true;
return false;
};
// Moves the stream backwards by one byte. If a previous buffer exists and the
// buffer position is zero, start backing up at the end of the previous buffer.
Parser.prototype._unpeek = function() {
this.eoc = false;
if (this.bufPos === 0 && this.prevBuf) {
this.bufPos = this.prevBuf.length - 1;
this.readFirst = true;
} else {
this.bufPos--;
}
};
// Returns the numerical value of the string if it is a number else undefined
Parser._number = function(str) {
var result = +str;
if (!isNaN(result) && str !== '') {
return result;
}
};
// Advances the streams returns the next identifier. The result will be
// undefined if the function didn't get enough room to extract the identifier
Parser.prototype._sliceIdentifier = function() {
if (this.eoc === true) {
return undefined;
}
do {
this._trimmer();
// The while check is important so we skip empty objects with no
// identifier (I think it is a bug with Paradox, but we'll be
// accomodating)
} while ((this.current === lcurl || this.current === rcurl) && !this.eoc);
var pos = 0;
// Check for a quote. If we are looking at a quote, then the identifier
// stretches through all until the end quote is found. This means that the
// resulting value can contain whitespace! If we don't see a quote then we
// continue until the chunk ends or a delimiter is found (such as an equals
// or whitespace)
if (this.current === quote) {
this._read();
while (this._read() && this.current !== quote) {
this.tok[pos++] = this.current;
}
} else {
while (this._read() && Parser._untyped(this.current) &&
!Parser._isspace(this.current)) {
this.tok[pos++] = this.current;
}
// We read too far if it is not the end of the chunk, so back up
if (this.eoc === false) {
this._unpeek();
}
}
if (this.eoc && !this.isEnding) {
return undefined;
}
var result = this.tok.toString('utf8', 0, pos);
return result;
};
// Reads through the stream and attempts to detect a list. If a list is
// detected, the object that is being parsed changes to a list and the function
// returns true.
Parser.prototype._list = function() {
while (this._read() &&
(Parser._isspace(this.current) || this.current === eq)) {
}
if (this.current === rcurl) {
this.nest.push(this.obj);
this.obj = [];
this.realBufPos = this.bufPos;
return true;
} else {
this._unpeek();
return undefined;
}
};
// Reads through the stream and attemps to detect a list. If a list is
// detected, the object that is being parsed changes to a list and the function
// returns true. If the function knows that the stream doesn't contain an
// object, it returns false. If there isn't enough data to determine, it
// returns undefined.
Parser.prototype._obj = function() {
while (this._read() &&
(Parser._isspace(this.current) || this.current === eq)) {
}
// If we hit the end of the chunk, well we don't know if we are looking at an
// object. Else if we aren't looking at a right curly then we aren't looking
// at an object
if (this.eoc === true) {
return undefined;
} else if (this.current !== rcurl) {
this._unpeek();
return false;
}
var pos = this.bufPos;
var foundUntyped = false;
// Attempt to advance the stream to the next delimiter, we are looking for an
// equal
while (this._read() &&
(Parser._untyped(this.current) || Parser._isspace(this.current))) {
foundUntyped |= Parser._untyped(this.current) &&
!Parser._isspace(this.current);
}
// We possibly read into the next chunk, so make a note of that as we reset
// to an earlier position so that we can re-read the identifier
this.readFirst = pos > this.bufPos;
this.bufPos = pos - 1;
if (this.eoc === true && (this.current !== eq && this.current !== lcurl) &&
Parser._untyped(this.current)) {
return undefined;
} else {
this.eoc = false;
}
// We hit '=', so we know we are parsing an object! And that is cool and
// all that we are in an object, but make sure we rewind ourselves to the
// start of the first property.
if (this.current === eq || (this.current === lcurl && !foundUntyped)) {
this.nest.push(this.obj);
this.obj = {};
this.bufPos++;
if (this.current === lcurl) {
this.emptyObject = true;
}
return true;
}
return false;
};
// Convert the string value to the most restrictive type. Return the new value
// in its restrictive type
Parser.prototype._identify = function(value) {
var val = toBool(value);
if (val !== undefined) {
return val;
}
val = toDate(value);
if (val) {
return val;
}
val = Parser._number(value);
if (val !== undefined) {
return val;
}
return value;
};
// Convert the array to an array of the least common demoninator types. An
// array of strings and ints will be converted to an array of strings.
Parser._lcd = function(arr) {
var i = 0;
// If the array is an array of objects. Let's jettison.
if (arr.length > 0 && typeof arr[0] === 'object') {
return;
}
// Is this an array of bools?
bools = arr.map(function(val) { return toBool(val); });
if (bools.every(function(val) { return val !== undefined; })) {
for (; i < arr.length; i++) {
arr[i] = toBool(arr[i]);
}
return;
}
// Is this an array of dates?
dates = arr.map(function(val) { return toDate(val); });
if (dates.every(function(val) { return val !== undefined; })) {
for (; i < arr.length; i++) {
arr[i] = toDate(arr[i]);
}
return;
}
// Is this an array of ints?
var nums = arr.map(function(val) { return Parser._number(val); });
if (nums.every(function(val) { return val !== undefined; })) {
for (; i < arr.length; i++) {
arr[i] = Parser._number(arr[i]);
}
}
};
Parser.prototype._parseList = function() {
// This could be a list of objects so we first check if we are lookgin at an
// object. We may not have enough buffer space to properly evaluate
var isObj = this._obj();
if (isObj === undefined) {
this.bufPos = this.realBufPos;
this.eoc = false;
return true;
} else if (isObj === true) {
this.nest[this.nest.length - 1].push(this.obj);
return this._parseObj();
}
var value = this._sliceIdentifier();
// The end of the list, the list was empty, or we ran out of buffer
if (value === undefined) {
if (this.current === lcurl) {
Parser._lcd(this.obj);
this.obj = this.nest.pop();
return false;
}
return true;
}
this.obj.push(value);
while (this._read() && Parser._isspace(this.current)) {
}
if (this.eoc === true && !this.isEnding) {
return true;
}
// We probably read too far so backup by one if the current character is
// something we probably want to be looking at.
if (Parser._untyped(this.current)) {
this._unpeek();
}
// Convert the list to the least common denominator type and pop it from the
// list as we are done parsing the list
if (this.current === lcurl) {
Parser._lcd(this.obj);
this.obj = this.nest.pop();
this.realBufPos = this.bufPos;
return false;
}
};
Parser._setProp = function(obj, identifier, value) {
if (obj.hasOwnProperty(identifier)) {
// Since the object has the key, we need to check if the value is an array
// or is single valued. If the property is already an array, push the new
// value to the end. Else the property is still single valued, then create
// a list with the two elements
if (util.isArray(obj[identifier])) {
obj[identifier].push(value);
} else {
obj[identifier] = [obj[identifier], value];
}
} else {
// New property so we just shove it into the object
obj[identifier] = value;
}
};
Parser.prototype._parseObj = function() {
this._trimmer();
while (this.current === rcurl && !this.eoc) {
this._trimmer();
if (this.current === lcurl) {
return false;
}
}
if (this.current === lcurl && !this.eoc && this.nest.length > 0) {
this.obj = this.nest.pop();
this.realBufPos = this.bufPos;
return false;
}
var identifier = this._sliceIdentifier();
var isObj = this._obj();
if (isObj === undefined) {
this.bufPos = this.realBufPos;
this.eoc = false;
return true;
} else if (isObj === true) {
Parser._setProp(this.nest[this.nest.length - 1], identifier, this.obj);
if (this.emptyObject) {
this.realBufPos = this.bufPos;
this.emptyObject = false;
return false;
}
return this._parseObj();
}
var isList = this._list();
if (isList) {
this.nest[this.nest.length - 1][identifier] = this.obj;
return this._parseList();
}
var value = this._identify(this._sliceIdentifier());
if (identifier === undefined || value === undefined) {
this.readFirst = this.realBufPos >= this.bufPos;
this.bufPos = this.realBufPos;
this.eoc = false;
return true;
}
this.realBufPos = this.bufPos;
Parser._setProp(this.obj, identifier, value);
this._trimmer();
// Another chance to chunk empty objects
while (this.current === rcurl && !this.eoc) {
this._trimmer();
if (this.current === lcurl) {
this._read();
}
}
if (this.current === lcurl) {
this.obj = this.nest.pop();
this.realBufPos = this.bufPos;
}
return false;
};
Parser.prototype._parse = function(cb) {
while (this.eoc === false) {
// If the object we are adding to is an array. We keep on processing
// elements and adding it to the end of the array. Else if we are
// dealing with an object, continue processing key value pairs
var cutoffed = util.isArray(this.obj) ? this._parseList() :
this._parseObj();
// While parsing, we may have run out of buffer room for parsing. If so we
// invoke the call back and wait for more data.
if (cutoffed) {
cb();
// Because this could be the last chunk before the "finish" event
if (this.buf.prevBuf) {
this.readFirst = true;
}
return;
} else if (this.current === lcurl) {
// As long as there are left curlies lined up in the buffer, pop them off
// and finalize them
var redo = true;
while (redo) {
redo = false;
while (this._read() && Parser._isspace(this.current)) {
}
if (this.current === lcurl && this.nest.length > 0) {
redo = true;
if (util.isArray(this.obj)) {
Parser._lcd(this.obj);
}
this.obj = this.nest.pop();
} else if (this.current !== lcurl) {
this._unpeek();
}
}
}
}
cb();
};
Parser.prototype._write = function(chunk, enc, cb) {
// If there is something in the buffer we squirrel it away as we may need to
// reference the data in it
if (this.buf !== undefined) {
this.prevBuf = this.buf;
this.readFirst = true;
this.bufPos = Math.min(this.bufPos, this.buf.length - 1);
} else {
this.bufPos = 0;
this.realBufPos = 0;
}
this.eoc = false;
this.buf = chunk;
this._parse(cb);
};
module.exports = Parser;