@loaders.gl/json 4.2.0-alpha.4 → 4.2.0-alpha.6

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