@loaders.gl/json 3.1.0-alpha.2 → 4.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,578 +0,0 @@
1
- /* eslint-disable */
2
- // @ts-nocheck
3
- const env = {};
4
-
5
- export const EVENTS = [
6
- 'value',
7
- 'string',
8
- 'key',
9
- 'openobject',
10
- 'closeobject',
11
- 'openarray',
12
- 'closearray',
13
- 'error',
14
- 'end',
15
- 'ready'
16
- ];
17
-
18
- // Removes the MAX_BUFFER_LENGTH, originally set to 64 * 1024
19
- const MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;
20
- const DEBUG = env.CDEBUG === 'debug';
21
-
22
- const buffers = {
23
- textNode: undefined,
24
- numberNode: ''
25
- };
26
-
27
- let S = 0;
28
-
29
- const STATE = {
30
- BEGIN: S++,
31
- VALUE: S++, // general stuff
32
- OPEN_OBJECT: S++, // {
33
- CLOSE_OBJECT: S++, // }
34
- OPEN_ARRAY: S++, // [
35
- CLOSE_ARRAY: S++, // ]
36
- TEXT_ESCAPE: S++, // \ stuff
37
- STRING: S++, // ""
38
- BACKSLASH: S++,
39
- END: S++, // No more stack
40
- OPEN_KEY: S++, // , "a"
41
- CLOSE_KEY: S++, // :
42
- TRUE: S++, // r
43
- TRUE2: S++, // u
44
- TRUE3: S++, // e
45
- FALSE: S++, // a
46
- FALSE2: S++, // l
47
- FALSE3: S++, // s
48
- FALSE4: S++, // e
49
- NULL: S++, // u
50
- NULL2: S++, // l
51
- NULL3: S++, // l
52
- NUMBER_DECIMAL_POINT: S++, // .
53
- NUMBER_DIGIT: S++ // [0-9]
54
- };
55
-
56
- for (var s_ in STATE) STATE[STATE[s_]] = s_;
57
-
58
- // switcharoo
59
- S = STATE;
60
-
61
- const Char = {
62
- tab: 0x09, // \t
63
- lineFeed: 0x0a, // \n
64
- carriageReturn: 0x0d, // \r
65
- space: 0x20, // " "
66
-
67
- doubleQuote: 0x22, // "
68
- plus: 0x2b, // +
69
- comma: 0x2c, // ,
70
- minus: 0x2d, // -
71
- period: 0x2e, // .
72
-
73
- _0: 0x30, // 0
74
- _9: 0x39, // 9
75
-
76
- colon: 0x3a, // :
77
-
78
- E: 0x45, // E
79
-
80
- openBracket: 0x5b, // [
81
- backslash: 0x5c, // \
82
- closeBracket: 0x5d, // ]
83
-
84
- a: 0x61, // a
85
- b: 0x62, // b
86
- e: 0x65, // e
87
- f: 0x66, // f
88
- l: 0x6c, // l
89
- n: 0x6e, // n
90
- r: 0x72, // r
91
- s: 0x73, // s
92
- t: 0x74, // t
93
- u: 0x75, // u
94
-
95
- openBrace: 0x7b, // {
96
- closeBrace: 0x7d // }
97
- };
98
-
99
- function checkBufferLength(parser) {
100
- const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);
101
- let maxActual = 0;
102
-
103
- for (var buffer in buffers) {
104
- var len = parser[buffer] === undefined ? 0 : parser[buffer].length;
105
- if (len > maxAllowed) {
106
- switch (buffer) {
107
- case 'text':
108
- closeText(parser);
109
- break;
110
-
111
- default:
112
- error(parser, 'Max buffer length exceeded: ' + buffer);
113
- }
114
- }
115
- maxActual = Math.max(maxActual, len);
116
- }
117
- parser.bufferCheckPosition = MAX_BUFFER_LENGTH - maxActual + parser.position;
118
- }
119
-
120
- var stringTokenPattern = /[\\"\n]/g;
121
-
122
- export default class ClarinetParser {
123
- constructor(options = {}) {
124
- this._initialize(options);
125
- }
126
-
127
- _initialize(options) {
128
- this._clearBuffers(this);
129
- this.bufferCheckPosition = MAX_BUFFER_LENGTH;
130
- this.q = '';
131
- this.c = '';
132
- this.p = '';
133
- this.options = options || {};
134
- this.closed = false;
135
- this.closedRoot = false;
136
- this.sawRoot = false;
137
- this.tag = null;
138
- this.error = null;
139
- this.state = S.BEGIN;
140
- this.stack = new Array();
141
- // mostly just for error reporting
142
- this.position = this.column = 0;
143
- this.line = 1;
144
- this.slashed = false;
145
- this.unicodeI = 0;
146
- this.unicodeS = null;
147
- this.depth = 0;
148
-
149
- // install callbacks
150
- if ('onready' in options) {
151
- this.onready = options.onready;
152
- }
153
-
154
- if ('onopenobject' in options) {
155
- this.onopenobject = options.onopenobject;
156
- }
157
-
158
- if ('onkey' in options) {
159
- this.onkey = options.onkey;
160
- }
161
-
162
- if ('oncloseobject' in options) {
163
- this.oncloseobject = options.oncloseobject;
164
- }
165
-
166
- if ('onopenarray' in options) {
167
- this.onopenarray = options.onopenarray;
168
- }
169
-
170
- if ('onclosearray' in options) {
171
- this.onclosearray = options.onclosearray;
172
- }
173
-
174
- if ('onvalue' in options) {
175
- this.onvalue = options.onvalue;
176
- }
177
-
178
- if ('onerror' in options) {
179
- this.onerror = options.onerror;
180
- }
181
-
182
- if ('onend' in options) {
183
- this.onend = options.onend;
184
- }
185
-
186
- if ('onchunkparsed' in options) {
187
- this.onchunkparsed = options.onchunkparsed;
188
- }
189
-
190
- emit(this, 'onready');
191
- }
192
-
193
- _clearBuffers() {
194
- for (var buffer in buffers) {
195
- this[buffer] = buffers[buffer];
196
- }
197
- }
198
-
199
- end() {
200
- if (this.state !== S.VALUE || this.depth !== 0) error(this, 'Unexpected end');
201
-
202
- closeValue(this);
203
- this.c = '';
204
- this.closed = true;
205
- emit(this, 'onend');
206
- this._initialize(this.options);
207
- return this;
208
- }
209
-
210
- resume() {
211
- this.error = null;
212
- return this;
213
- }
214
-
215
- close() {
216
- return this.write(null);
217
- }
218
-
219
- write(chunk) {
220
- if (this.error) {
221
- throw this.error;
222
- }
223
- if (this.closed) {
224
- return error(this, 'Cannot write after close. Assign an onready handler.');
225
- }
226
- if (chunk === null) {
227
- return this.end();
228
- }
229
- var i = 0,
230
- c = chunk.charCodeAt(0),
231
- p = this.p;
232
- if (DEBUG) console.log('write -> [' + chunk + ']');
233
- while (c) {
234
- p = c;
235
- this.c = c = chunk.charCodeAt(i++);
236
- // if chunk doesnt have next, like streaming char by char
237
- // this way we need to check if previous is really previous
238
- // if not we need to reset to what the this says is the previous
239
- // from buffer
240
- if (p !== c) {
241
- this.p = p;
242
- } else {
243
- p = this.p;
244
- }
245
-
246
- if (!c) break;
247
-
248
- if (DEBUG) console.log(i, c, STATE[this.state]);
249
- this.position++;
250
- if (c === Char.lineFeed) {
251
- this.line++;
252
- this.column = 0;
253
- } else this.column++;
254
-
255
- switch (this.state) {
256
- case S.BEGIN:
257
- if (c === Char.openBrace) this.state = S.OPEN_OBJECT;
258
- else if (c === Char.openBracket) this.state = S.OPEN_ARRAY;
259
- else if (!isWhitespace(c)) {
260
- error(this, 'Non-whitespace before {[.');
261
- }
262
- continue;
263
-
264
- case S.OPEN_KEY:
265
- case S.OPEN_OBJECT:
266
- if (isWhitespace(c)) continue;
267
- if (this.state === S.OPEN_KEY) this.stack.push(S.CLOSE_KEY);
268
- else {
269
- if (c === Char.closeBrace) {
270
- emit(this, 'onopenobject');
271
- this.depth++;
272
- emit(this, 'oncloseobject');
273
- this.depth--;
274
- this.state = this.stack.pop() || S.VALUE;
275
- continue;
276
- } else this.stack.push(S.CLOSE_OBJECT);
277
- }
278
- if (c === Char.doubleQuote) this.state = S.STRING;
279
- else error(this, 'Malformed object key should start with "');
280
- continue;
281
-
282
- case S.CLOSE_KEY:
283
- case S.CLOSE_OBJECT:
284
- if (isWhitespace(c)) continue;
285
- var event = this.state === S.CLOSE_KEY ? 'key' : 'object';
286
- if (c === Char.colon) {
287
- if (this.state === S.CLOSE_OBJECT) {
288
- this.stack.push(S.CLOSE_OBJECT);
289
- closeValue(this, 'onopenobject');
290
- this.depth++;
291
- } else closeValue(this, 'onkey');
292
- this.state = S.VALUE;
293
- } else if (c === Char.closeBrace) {
294
- emitNode(this, 'oncloseobject');
295
- this.depth--;
296
- this.state = this.stack.pop() || S.VALUE;
297
- } else if (c === Char.comma) {
298
- if (this.state === S.CLOSE_OBJECT) this.stack.push(S.CLOSE_OBJECT);
299
- closeValue(this);
300
- this.state = S.OPEN_KEY;
301
- } else error(this, 'Bad object');
302
- continue;
303
-
304
- case S.OPEN_ARRAY: // after an array there always a value
305
- case S.VALUE:
306
- if (isWhitespace(c)) continue;
307
- if (this.state === S.OPEN_ARRAY) {
308
- emit(this, 'onopenarray');
309
- this.depth++;
310
- this.state = S.VALUE;
311
- if (c === Char.closeBracket) {
312
- emit(this, 'onclosearray');
313
- this.depth--;
314
- this.state = this.stack.pop() || S.VALUE;
315
- continue;
316
- } else {
317
- this.stack.push(S.CLOSE_ARRAY);
318
- }
319
- }
320
- if (c === Char.doubleQuote) this.state = S.STRING;
321
- else if (c === Char.openBrace) this.state = S.OPEN_OBJECT;
322
- else if (c === Char.openBracket) this.state = S.OPEN_ARRAY;
323
- else if (c === Char.t) this.state = S.TRUE;
324
- else if (c === Char.f) this.state = S.FALSE;
325
- else if (c === Char.n) this.state = S.NULL;
326
- else if (c === Char.minus) {
327
- // keep and continue
328
- this.numberNode += '-';
329
- } else if (Char._0 <= c && c <= Char._9) {
330
- this.numberNode += String.fromCharCode(c);
331
- this.state = S.NUMBER_DIGIT;
332
- } else error(this, 'Bad value');
333
- continue;
334
-
335
- case S.CLOSE_ARRAY:
336
- if (c === Char.comma) {
337
- this.stack.push(S.CLOSE_ARRAY);
338
- closeValue(this, 'onvalue');
339
- this.state = S.VALUE;
340
- } else if (c === Char.closeBracket) {
341
- emitNode(this, 'onclosearray');
342
- this.depth--;
343
- this.state = this.stack.pop() || S.VALUE;
344
- } else if (isWhitespace(c)) continue;
345
- else error(this, 'Bad array');
346
- continue;
347
-
348
- case S.STRING:
349
- if (this.textNode === undefined) {
350
- this.textNode = '';
351
- }
352
-
353
- // thanks thejh, this is an about 50% performance improvement.
354
- var starti = i - 1,
355
- slashed = this.slashed,
356
- unicodeI = this.unicodeI;
357
- STRING_BIGLOOP: while (true) {
358
- if (DEBUG) console.log(i, c, STATE[this.state], slashed);
359
- // zero means "no unicode active". 1-4 mean "parse some more". end after 4.
360
- while (unicodeI > 0) {
361
- this.unicodeS += String.fromCharCode(c);
362
- c = chunk.charCodeAt(i++);
363
- this.position++;
364
- if (unicodeI === 4) {
365
- // TODO this might be slow? well, probably not used too often anyway
366
- this.textNode += String.fromCharCode(parseInt(this.unicodeS, 16));
367
- unicodeI = 0;
368
- starti = i - 1;
369
- } else {
370
- unicodeI++;
371
- }
372
- // we can just break here: no stuff we skipped that still has to be sliced out or so
373
- if (!c) break STRING_BIGLOOP;
374
- }
375
- if (c === Char.doubleQuote && !slashed) {
376
- this.state = this.stack.pop() || S.VALUE;
377
- this.textNode += chunk.substring(starti, i - 1);
378
- this.position += i - 1 - starti;
379
- break;
380
- }
381
- if (c === Char.backslash && !slashed) {
382
- slashed = true;
383
- this.textNode += chunk.substring(starti, i - 1);
384
- this.position += i - 1 - starti;
385
- c = chunk.charCodeAt(i++);
386
- this.position++;
387
- if (!c) break;
388
- }
389
- if (slashed) {
390
- slashed = false;
391
- if (c === Char.n) {
392
- this.textNode += '\n';
393
- } else if (c === Char.r) {
394
- this.textNode += '\r';
395
- } else if (c === Char.t) {
396
- this.textNode += '\t';
397
- } else if (c === Char.f) {
398
- this.textNode += '\f';
399
- } else if (c === Char.b) {
400
- this.textNode += '\b';
401
- } else if (c === Char.u) {
402
- // \uxxxx. meh!
403
- unicodeI = 1;
404
- this.unicodeS = '';
405
- } else {
406
- this.textNode += String.fromCharCode(c);
407
- }
408
- c = chunk.charCodeAt(i++);
409
- this.position++;
410
- starti = i - 1;
411
- if (!c) break;
412
- else continue;
413
- }
414
-
415
- stringTokenPattern.lastIndex = i;
416
- var reResult = stringTokenPattern.exec(chunk);
417
- if (reResult === null) {
418
- i = chunk.length + 1;
419
- this.textNode += chunk.substring(starti, i - 1);
420
- this.position += i - 1 - starti;
421
- break;
422
- }
423
- i = reResult.index + 1;
424
- c = chunk.charCodeAt(reResult.index);
425
- if (!c) {
426
- this.textNode += chunk.substring(starti, i - 1);
427
- this.position += i - 1 - starti;
428
- break;
429
- }
430
- }
431
- this.slashed = slashed;
432
- this.unicodeI = unicodeI;
433
- continue;
434
-
435
- case S.TRUE:
436
- if (c === Char.r) this.state = S.TRUE2;
437
- else error(this, 'Invalid true started with t' + c);
438
- continue;
439
-
440
- case S.TRUE2:
441
- if (c === Char.u) this.state = S.TRUE3;
442
- else error(this, 'Invalid true started with tr' + c);
443
- continue;
444
-
445
- case S.TRUE3:
446
- if (c === Char.e) {
447
- emit(this, 'onvalue', true);
448
- this.state = this.stack.pop() || S.VALUE;
449
- } else error(this, 'Invalid true started with tru' + c);
450
- continue;
451
-
452
- case S.FALSE:
453
- if (c === Char.a) this.state = S.FALSE2;
454
- else error(this, 'Invalid false started with f' + c);
455
- continue;
456
-
457
- case S.FALSE2:
458
- if (c === Char.l) this.state = S.FALSE3;
459
- else error(this, 'Invalid false started with fa' + c);
460
- continue;
461
-
462
- case S.FALSE3:
463
- if (c === Char.s) this.state = S.FALSE4;
464
- else error(this, 'Invalid false started with fal' + c);
465
- continue;
466
-
467
- case S.FALSE4:
468
- if (c === Char.e) {
469
- emit(this, 'onvalue', false);
470
- this.state = this.stack.pop() || S.VALUE;
471
- } else error(this, 'Invalid false started with fals' + c);
472
- continue;
473
-
474
- case S.NULL:
475
- if (c === Char.u) this.state = S.NULL2;
476
- else error(this, 'Invalid null started with n' + c);
477
- continue;
478
-
479
- case S.NULL2:
480
- if (c === Char.l) this.state = S.NULL3;
481
- else error(this, 'Invalid null started with nu' + c);
482
- continue;
483
-
484
- case S.NULL3:
485
- if (c === Char.l) {
486
- emit(this, 'onvalue', null);
487
- this.state = this.stack.pop() || S.VALUE;
488
- } else error(this, 'Invalid null started with nul' + c);
489
- continue;
490
-
491
- case S.NUMBER_DECIMAL_POINT:
492
- if (c === Char.period) {
493
- this.numberNode += '.';
494
- this.state = S.NUMBER_DIGIT;
495
- } else error(this, 'Leading zero not followed by .');
496
- continue;
497
-
498
- case S.NUMBER_DIGIT:
499
- if (Char._0 <= c && c <= Char._9) this.numberNode += String.fromCharCode(c);
500
- else if (c === Char.period) {
501
- if (this.numberNode.indexOf('.') !== -1) error(this, 'Invalid number has two dots');
502
- this.numberNode += '.';
503
- } else if (c === Char.e || c === Char.E) {
504
- if (this.numberNode.indexOf('e') !== -1 || this.numberNode.indexOf('E') !== -1)
505
- error(this, 'Invalid number has two exponential');
506
- this.numberNode += 'e';
507
- } else if (c === Char.plus || c === Char.minus) {
508
- if (!(p === Char.e || p === Char.E)) error(this, 'Invalid symbol in number');
509
- this.numberNode += String.fromCharCode(c);
510
- } else {
511
- closeNumber(this);
512
- i--; // go back one
513
- this.state = this.stack.pop() || S.VALUE;
514
- }
515
- continue;
516
-
517
- default:
518
- error(this, 'Unknown state: ' + this.state);
519
- }
520
- }
521
- if (this.position >= this.bufferCheckPosition) {
522
- checkBufferLength(this);
523
- }
524
-
525
- emit(this, 'onchunkparsed');
526
-
527
- return this;
528
- }
529
- }
530
-
531
- function emit(parser, event, data) {
532
- if (DEBUG) {
533
- console.log('-- emit', event, data);
534
- }
535
- if (parser[event]) {
536
- parser[event](data, parser);
537
- }
538
- }
539
-
540
- function emitNode(parser, event, data) {
541
- closeValue(parser);
542
- emit(parser, event, data);
543
- }
544
-
545
- function closeValue(parser, event) {
546
- parser.textNode = textopts(parser.options, parser.textNode);
547
- if (parser.textNode !== undefined) {
548
- emit(parser, event ? event : 'onvalue', parser.textNode);
549
- }
550
- parser.textNode = undefined;
551
- }
552
-
553
- function closeNumber(parser) {
554
- if (parser.numberNode) emit(parser, 'onvalue', parseFloat(parser.numberNode));
555
- parser.numberNode = '';
556
- }
557
-
558
- function textopts(opt, text) {
559
- if (text === undefined) {
560
- return text;
561
- }
562
- if (opt.trim) text = text.trim();
563
- if (opt.normalize) text = text.replace(/\s+/g, ' ');
564
- return text;
565
- }
566
-
567
- function error(parser, er) {
568
- closeValue(parser);
569
- er += '\nLine: ' + parser.line + '\nColumn: ' + parser.column + '\nChar: ' + parser.c;
570
- er = new Error(er);
571
- parser.error = er;
572
- emit(parser, 'onerror', er);
573
- return parser;
574
- }
575
-
576
- function isWhitespace(c) {
577
- return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab;
578
- }