@loaders.gl/json 3.1.0-alpha.5 → 3.1.0-beta.1

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 (79) hide show
  1. package/dist/bundle.d.ts +1 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +1839 -0
  4. package/dist/es5/geojson-loader.js +1 -1
  5. package/dist/es5/geojson-loader.js.map +1 -1
  6. package/dist/es5/json-loader.js +1 -1
  7. package/dist/es5/json-loader.js.map +1 -1
  8. package/dist/es5/lib/clarinet/clarinet.js +226 -281
  9. package/dist/es5/lib/clarinet/clarinet.js.map +1 -1
  10. package/dist/es5/lib/parser/json-parser.js +45 -47
  11. package/dist/es5/lib/parser/json-parser.js.map +1 -1
  12. package/dist/es5/lib/parser/streaming-json-parser.js +29 -34
  13. package/dist/es5/lib/parser/streaming-json-parser.js.map +1 -1
  14. package/dist/es5/ndjson-loader.js +5 -2
  15. package/dist/es5/ndjson-loader.js.map +1 -1
  16. package/dist/esm/geojson-loader.js +1 -1
  17. package/dist/esm/geojson-loader.js.map +1 -1
  18. package/dist/esm/json-loader.js +1 -1
  19. package/dist/esm/json-loader.js.map +1 -1
  20. package/dist/esm/lib/clarinet/clarinet.js +222 -260
  21. package/dist/esm/lib/clarinet/clarinet.js.map +1 -1
  22. package/dist/esm/lib/parser/json-parser.js +48 -47
  23. package/dist/esm/lib/parser/json-parser.js.map +1 -1
  24. package/dist/esm/lib/parser/streaming-json-parser.js +29 -34
  25. package/dist/esm/lib/parser/streaming-json-parser.js.map +1 -1
  26. package/dist/esm/ndjson-loader.js +3 -1
  27. package/dist/esm/ndjson-loader.js.map +1 -1
  28. package/dist/geojson-loader.d.ts +1 -0
  29. package/dist/geojson-loader.d.ts.map +1 -0
  30. package/dist/geojson-loader.js +78 -0
  31. package/dist/geojson-worker.js +1702 -2
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +17 -0
  35. package/dist/json-loader.d.ts +1 -0
  36. package/dist/json-loader.d.ts.map +1 -0
  37. package/dist/json-loader.js +57 -0
  38. package/dist/jsonl-loader.d.ts +1 -0
  39. package/dist/jsonl-loader.d.ts.map +1 -0
  40. package/dist/jsonl-loader.js +54 -0
  41. package/dist/lib/clarinet/clarinet.d.ts +72 -38
  42. package/dist/lib/clarinet/clarinet.d.ts.map +1 -0
  43. package/dist/lib/clarinet/clarinet.js +535 -0
  44. package/dist/lib/jsonpath/jsonpath.d.ts +1 -0
  45. package/dist/lib/jsonpath/jsonpath.d.ts.map +1 -0
  46. package/dist/lib/jsonpath/jsonpath.js +89 -0
  47. package/dist/lib/parse-json-in-batches.d.ts +1 -0
  48. package/dist/lib/parse-json-in-batches.d.ts.map +1 -0
  49. package/dist/lib/parse-json-in-batches.js +80 -0
  50. package/dist/lib/parse-json.d.ts +1 -0
  51. package/dist/lib/parse-json.d.ts.map +1 -0
  52. package/dist/lib/parse-json.js +29 -0
  53. package/dist/lib/parse-ndjson-in-batches.d.ts +1 -0
  54. package/dist/lib/parse-ndjson-in-batches.d.ts.map +1 -0
  55. package/dist/lib/parse-ndjson-in-batches.js +35 -0
  56. package/dist/lib/parse-ndjson.d.ts +1 -0
  57. package/dist/lib/parse-ndjson.d.ts.map +1 -0
  58. package/dist/lib/parse-ndjson.js +14 -0
  59. package/dist/lib/parser/json-parser.d.ts +10 -5
  60. package/dist/lib/parser/json-parser.d.ts.map +1 -0
  61. package/dist/lib/parser/json-parser.js +98 -0
  62. package/dist/lib/parser/streaming-json-parser.d.ts +1 -1
  63. package/dist/lib/parser/streaming-json-parser.d.ts.map +1 -0
  64. package/dist/lib/parser/streaming-json-parser.js +100 -0
  65. package/dist/ndjson-loader.d.ts +22 -2
  66. package/dist/ndjson-loader.d.ts.map +1 -0
  67. package/dist/ndjson-loader.js +35 -0
  68. package/dist/workers/geojson-worker.d.ts +1 -0
  69. package/dist/workers/geojson-worker.d.ts.map +1 -0
  70. package/dist/workers/geojson-worker.js +5 -0
  71. package/package.json +7 -8
  72. package/src/lib/clarinet/clarinet.ts +539 -0
  73. package/src/lib/parser/json-parser.ts +52 -55
  74. package/src/lib/parser/streaming-json-parser.ts +28 -32
  75. package/src/ndjson-loader.ts +3 -1
  76. package/dist/dist.min.js +0 -2
  77. package/dist/dist.min.js.map +0 -1
  78. package/dist/geojson-worker.js.map +0 -1
  79. package/src/lib/clarinet/clarinet.js +0 -578
@@ -1,49 +1,44 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
3
5
  Object.defineProperty(exports, "__esModule", {
4
6
  value: true
5
7
  });
6
- exports.default = exports.EVENTS = void 0;
7
- const env = {};
8
- const EVENTS = ['value', 'string', 'key', 'openobject', 'closeobject', 'openarray', 'closearray', 'error', 'end', 'ready'];
9
- exports.EVENTS = EVENTS;
10
- const MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;
11
- const DEBUG = env.CDEBUG === 'debug';
12
- const buffers = {
13
- textNode: undefined,
14
- numberNode: ''
15
- };
16
- let S = 0;
17
- const STATE = {
18
- BEGIN: S++,
19
- VALUE: S++,
20
- OPEN_OBJECT: S++,
21
- CLOSE_OBJECT: S++,
22
- OPEN_ARRAY: S++,
23
- CLOSE_ARRAY: S++,
24
- TEXT_ESCAPE: S++,
25
- STRING: S++,
26
- BACKSLASH: S++,
27
- END: S++,
28
- OPEN_KEY: S++,
29
- CLOSE_KEY: S++,
30
- TRUE: S++,
31
- TRUE2: S++,
32
- TRUE3: S++,
33
- FALSE: S++,
34
- FALSE2: S++,
35
- FALSE3: S++,
36
- FALSE4: S++,
37
- NULL: S++,
38
- NULL2: S++,
39
- NULL3: S++,
40
- NUMBER_DECIMAL_POINT: S++,
41
- NUMBER_DIGIT: S++
42
- };
8
+ exports.default = void 0;
43
9
 
44
- for (var s_ in STATE) STATE[STATE[s_]] = s_;
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ const MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;
13
+ var STATE;
14
+
15
+ (function (STATE) {
16
+ STATE[STATE["BEGIN"] = 0] = "BEGIN";
17
+ STATE[STATE["VALUE"] = 1] = "VALUE";
18
+ STATE[STATE["OPEN_OBJECT"] = 2] = "OPEN_OBJECT";
19
+ STATE[STATE["CLOSE_OBJECT"] = 3] = "CLOSE_OBJECT";
20
+ STATE[STATE["OPEN_ARRAY"] = 4] = "OPEN_ARRAY";
21
+ STATE[STATE["CLOSE_ARRAY"] = 5] = "CLOSE_ARRAY";
22
+ STATE[STATE["TEXT_ESCAPE"] = 6] = "TEXT_ESCAPE";
23
+ STATE[STATE["STRING"] = 7] = "STRING";
24
+ STATE[STATE["BACKSLASH"] = 8] = "BACKSLASH";
25
+ STATE[STATE["END"] = 9] = "END";
26
+ STATE[STATE["OPEN_KEY"] = 10] = "OPEN_KEY";
27
+ STATE[STATE["CLOSE_KEY"] = 11] = "CLOSE_KEY";
28
+ STATE[STATE["TRUE"] = 12] = "TRUE";
29
+ STATE[STATE["TRUE2"] = 13] = "TRUE2";
30
+ STATE[STATE["TRUE3"] = 14] = "TRUE3";
31
+ STATE[STATE["FALSE"] = 15] = "FALSE";
32
+ STATE[STATE["FALSE2"] = 16] = "FALSE2";
33
+ STATE[STATE["FALSE3"] = 17] = "FALSE3";
34
+ STATE[STATE["FALSE4"] = 18] = "FALSE4";
35
+ STATE[STATE["NULL"] = 19] = "NULL";
36
+ STATE[STATE["NULL2"] = 20] = "NULL2";
37
+ STATE[STATE["NULL3"] = 21] = "NULL3";
38
+ STATE[STATE["NUMBER_DECIMAL_POINT"] = 22] = "NUMBER_DECIMAL_POINT";
39
+ STATE[STATE["NUMBER_DIGIT"] = 23] = "NUMBER_DIGIT";
40
+ })(STATE || (STATE = {}));
45
41
 
46
- S = STATE;
47
42
  const Char = {
48
43
  tab: 0x09,
49
44
  lineFeed: 0x0a,
@@ -74,118 +69,58 @@ const Char = {
74
69
  openBrace: 0x7b,
75
70
  closeBrace: 0x7d
76
71
  };
77
-
78
- function checkBufferLength(parser) {
79
- const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);
80
- let maxActual = 0;
81
-
82
- for (var buffer in buffers) {
83
- var len = parser[buffer] === undefined ? 0 : parser[buffer].length;
84
-
85
- if (len > maxAllowed) {
86
- switch (buffer) {
87
- case 'text':
88
- closeText(parser);
89
- break;
90
-
91
- default:
92
- error(parser, 'Max buffer length exceeded: ' + buffer);
93
- }
94
- }
95
-
96
- maxActual = Math.max(maxActual, len);
97
- }
98
-
99
- parser.bufferCheckPosition = MAX_BUFFER_LENGTH - maxActual + parser.position;
100
- }
101
-
102
- var stringTokenPattern = /[\\"\n]/g;
72
+ const stringTokenPattern = /[\\"\n]/g;
73
+ const DEFAULT_OPTIONS = {
74
+ onready: () => {},
75
+ onopenobject: () => {},
76
+ onkey: () => {},
77
+ oncloseobject: () => {},
78
+ onopenarray: () => {},
79
+ onclosearray: () => {},
80
+ onvalue: () => {},
81
+ onerror: () => {},
82
+ onend: () => {},
83
+ onchunkparsed: () => {}
84
+ };
103
85
 
104
86
  class ClarinetParser {
105
87
  constructor(options = {}) {
106
- this._initialize(options);
88
+ (0, _defineProperty2.default)(this, "options", DEFAULT_OPTIONS);
89
+ (0, _defineProperty2.default)(this, "bufferCheckPosition", MAX_BUFFER_LENGTH);
90
+ (0, _defineProperty2.default)(this, "q", '');
91
+ (0, _defineProperty2.default)(this, "c", '');
92
+ (0, _defineProperty2.default)(this, "p", '');
93
+ (0, _defineProperty2.default)(this, "closed", false);
94
+ (0, _defineProperty2.default)(this, "closedRoot", false);
95
+ (0, _defineProperty2.default)(this, "sawRoot", false);
96
+ (0, _defineProperty2.default)(this, "error", null);
97
+ (0, _defineProperty2.default)(this, "state", STATE.BEGIN);
98
+ (0, _defineProperty2.default)(this, "stack", []);
99
+ (0, _defineProperty2.default)(this, "position", 0);
100
+ (0, _defineProperty2.default)(this, "column", 0);
101
+ (0, _defineProperty2.default)(this, "line", 1);
102
+ (0, _defineProperty2.default)(this, "slashed", false);
103
+ (0, _defineProperty2.default)(this, "unicodeI", 0);
104
+ (0, _defineProperty2.default)(this, "unicodeS", null);
105
+ (0, _defineProperty2.default)(this, "depth", 0);
106
+ (0, _defineProperty2.default)(this, "textNode", void 0);
107
+ (0, _defineProperty2.default)(this, "numberNode", void 0);
108
+ this.options = { ...DEFAULT_OPTIONS,
109
+ ...options
110
+ };
111
+ this.textNode = undefined;
112
+ this.numberNode = '';
113
+ this.emit('onready');
107
114
  }
108
115
 
109
- _initialize(options) {
110
- this._clearBuffers(this);
111
-
112
- this.bufferCheckPosition = MAX_BUFFER_LENGTH;
113
- this.q = '';
114
- this.c = '';
115
- this.p = '';
116
- this.options = options || {};
117
- this.closed = false;
118
- this.closedRoot = false;
119
- this.sawRoot = false;
120
- this.tag = null;
121
- this.error = null;
122
- this.state = S.BEGIN;
123
- this.stack = new Array();
124
- this.position = this.column = 0;
125
- this.line = 1;
126
- this.slashed = false;
127
- this.unicodeI = 0;
128
- this.unicodeS = null;
129
- this.depth = 0;
130
-
131
- if ('onready' in options) {
132
- this.onready = options.onready;
133
- }
134
-
135
- if ('onopenobject' in options) {
136
- this.onopenobject = options.onopenobject;
137
- }
138
-
139
- if ('onkey' in options) {
140
- this.onkey = options.onkey;
141
- }
142
-
143
- if ('oncloseobject' in options) {
144
- this.oncloseobject = options.oncloseobject;
145
- }
146
-
147
- if ('onopenarray' in options) {
148
- this.onopenarray = options.onopenarray;
149
- }
150
-
151
- if ('onclosearray' in options) {
152
- this.onclosearray = options.onclosearray;
153
- }
154
-
155
- if ('onvalue' in options) {
156
- this.onvalue = options.onvalue;
157
- }
158
-
159
- if ('onerror' in options) {
160
- this.onerror = options.onerror;
161
- }
162
-
163
- if ('onend' in options) {
164
- this.onend = options.onend;
165
- }
166
-
167
- if ('onchunkparsed' in options) {
168
- this.onchunkparsed = options.onchunkparsed;
169
- }
116
+ end() {
117
+ if (this.state !== STATE.VALUE || this.depth !== 0) this._error('Unexpected end');
170
118
 
171
- emit(this, 'onready');
172
- }
119
+ this._closeValue();
173
120
 
174
- _clearBuffers() {
175
- for (var buffer in buffers) {
176
- this[buffer] = buffers[buffer];
177
- }
178
- }
179
-
180
- end() {
181
- if (this.state !== S.VALUE || this.depth !== 0) error(this, 'Unexpected end');
182
- closeValue(this);
183
121
  this.c = '';
184
122
  this.closed = true;
185
- emit(this, 'onend');
186
-
187
- this._initialize(this.options);
188
-
123
+ this.emit('onend');
189
124
  return this;
190
125
  }
191
126
 
@@ -198,23 +133,34 @@ class ClarinetParser {
198
133
  return this.write(null);
199
134
  }
200
135
 
136
+ emit(event, data) {
137
+ var _this$options$event, _this$options;
138
+
139
+ (_this$options$event = (_this$options = this.options)[event]) === null || _this$options$event === void 0 ? void 0 : _this$options$event.call(_this$options, data, this);
140
+ }
141
+
142
+ emitNode(event, data) {
143
+ this._closeValue();
144
+
145
+ this.emit(event, data);
146
+ }
147
+
201
148
  write(chunk) {
202
149
  if (this.error) {
203
150
  throw this.error;
204
151
  }
205
152
 
206
153
  if (this.closed) {
207
- return error(this, 'Cannot write after close. Assign an onready handler.');
154
+ return this._error('Cannot write after close. Assign an onready handler.');
208
155
  }
209
156
 
210
157
  if (chunk === null) {
211
158
  return this.end();
212
159
  }
213
160
 
214
- var i = 0,
215
- c = chunk.charCodeAt(0),
216
- p = this.p;
217
- if (DEBUG) console.log('write -> [' + chunk + ']');
161
+ let i = 0;
162
+ let c = chunk.charCodeAt(0);
163
+ let p = this.p;
218
164
 
219
165
  while (c) {
220
166
  p = c;
@@ -227,7 +173,6 @@ class ClarinetParser {
227
173
  }
228
174
 
229
175
  if (!c) break;
230
- if (DEBUG) console.log(i, c, STATE[this.state]);
231
176
  this.position++;
232
177
 
233
178
  if (c === Char.lineFeed) {
@@ -236,105 +181,106 @@ class ClarinetParser {
236
181
  } else this.column++;
237
182
 
238
183
  switch (this.state) {
239
- case S.BEGIN:
240
- if (c === Char.openBrace) this.state = S.OPEN_OBJECT;else if (c === Char.openBracket) this.state = S.OPEN_ARRAY;else if (!isWhitespace(c)) {
241
- error(this, 'Non-whitespace before {[.');
184
+ case STATE.BEGIN:
185
+ if (c === Char.openBrace) this.state = STATE.OPEN_OBJECT;else if (c === Char.openBracket) this.state = STATE.OPEN_ARRAY;else if (!isWhitespace(c)) {
186
+ this._error('Non-whitespace before {[.');
242
187
  }
243
188
  continue;
244
189
 
245
- case S.OPEN_KEY:
246
- case S.OPEN_OBJECT:
190
+ case STATE.OPEN_KEY:
191
+ case STATE.OPEN_OBJECT:
247
192
  if (isWhitespace(c)) continue;
248
- if (this.state === S.OPEN_KEY) this.stack.push(S.CLOSE_KEY);else {
249
- if (c === Char.closeBrace) {
250
- emit(this, 'onopenobject');
251
- this.depth++;
252
- emit(this, 'oncloseobject');
253
- this.depth--;
254
- this.state = this.stack.pop() || S.VALUE;
255
- continue;
256
- } else this.stack.push(S.CLOSE_OBJECT);
257
- }
258
- if (c === Char.doubleQuote) this.state = S.STRING;else error(this, 'Malformed object key should start with "');
193
+ if (this.state === STATE.OPEN_KEY) this.stack.push(STATE.CLOSE_KEY);else if (c === Char.closeBrace) {
194
+ this.emit('onopenobject');
195
+ this.depth++;
196
+ this.emit('oncloseobject');
197
+ this.depth--;
198
+ this.state = this.stack.pop() || STATE.VALUE;
199
+ continue;
200
+ } else this.stack.push(STATE.CLOSE_OBJECT);
201
+ if (c === Char.doubleQuote) this.state = STATE.STRING;else this._error('Malformed object key should start with "');
259
202
  continue;
260
203
 
261
- case S.CLOSE_KEY:
262
- case S.CLOSE_OBJECT:
204
+ case STATE.CLOSE_KEY:
205
+ case STATE.CLOSE_OBJECT:
263
206
  if (isWhitespace(c)) continue;
264
- var event = this.state === S.CLOSE_KEY ? 'key' : 'object';
265
207
 
266
208
  if (c === Char.colon) {
267
- if (this.state === S.CLOSE_OBJECT) {
268
- this.stack.push(S.CLOSE_OBJECT);
269
- closeValue(this, 'onopenobject');
209
+ if (this.state === STATE.CLOSE_OBJECT) {
210
+ this.stack.push(STATE.CLOSE_OBJECT);
211
+
212
+ this._closeValue('onopenobject');
213
+
270
214
  this.depth++;
271
- } else closeValue(this, 'onkey');
215
+ } else this._closeValue('onkey');
272
216
 
273
- this.state = S.VALUE;
217
+ this.state = STATE.VALUE;
274
218
  } else if (c === Char.closeBrace) {
275
- emitNode(this, 'oncloseobject');
219
+ this.emitNode('oncloseobject');
276
220
  this.depth--;
277
- this.state = this.stack.pop() || S.VALUE;
221
+ this.state = this.stack.pop() || STATE.VALUE;
278
222
  } else if (c === Char.comma) {
279
- if (this.state === S.CLOSE_OBJECT) this.stack.push(S.CLOSE_OBJECT);
280
- closeValue(this);
281
- this.state = S.OPEN_KEY;
282
- } else error(this, 'Bad object');
223
+ if (this.state === STATE.CLOSE_OBJECT) this.stack.push(STATE.CLOSE_OBJECT);
224
+
225
+ this._closeValue();
226
+
227
+ this.state = STATE.OPEN_KEY;
228
+ } else this._error('Bad object');
283
229
 
284
230
  continue;
285
231
 
286
- case S.OPEN_ARRAY:
287
- case S.VALUE:
232
+ case STATE.OPEN_ARRAY:
233
+ case STATE.VALUE:
288
234
  if (isWhitespace(c)) continue;
289
235
 
290
- if (this.state === S.OPEN_ARRAY) {
291
- emit(this, 'onopenarray');
236
+ if (this.state === STATE.OPEN_ARRAY) {
237
+ this.emit('onopenarray');
292
238
  this.depth++;
293
- this.state = S.VALUE;
239
+ this.state = STATE.VALUE;
294
240
 
295
241
  if (c === Char.closeBracket) {
296
- emit(this, 'onclosearray');
242
+ this.emit('onclosearray');
297
243
  this.depth--;
298
- this.state = this.stack.pop() || S.VALUE;
244
+ this.state = this.stack.pop() || STATE.VALUE;
299
245
  continue;
300
246
  } else {
301
- this.stack.push(S.CLOSE_ARRAY);
247
+ this.stack.push(STATE.CLOSE_ARRAY);
302
248
  }
303
249
  }
304
250
 
305
- if (c === Char.doubleQuote) this.state = S.STRING;else if (c === Char.openBrace) this.state = S.OPEN_OBJECT;else if (c === Char.openBracket) this.state = S.OPEN_ARRAY;else if (c === Char.t) this.state = S.TRUE;else if (c === Char.f) this.state = S.FALSE;else if (c === Char.n) this.state = S.NULL;else if (c === Char.minus) {
251
+ 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) {
306
252
  this.numberNode += '-';
307
253
  } else if (Char._0 <= c && c <= Char._9) {
308
254
  this.numberNode += String.fromCharCode(c);
309
- this.state = S.NUMBER_DIGIT;
310
- } else error(this, 'Bad value');
255
+ this.state = STATE.NUMBER_DIGIT;
256
+ } else this._error('Bad value');
311
257
  continue;
312
258
 
313
- case S.CLOSE_ARRAY:
259
+ case STATE.CLOSE_ARRAY:
314
260
  if (c === Char.comma) {
315
- this.stack.push(S.CLOSE_ARRAY);
316
- closeValue(this, 'onvalue');
317
- this.state = S.VALUE;
261
+ this.stack.push(STATE.CLOSE_ARRAY);
262
+
263
+ this._closeValue('onvalue');
264
+
265
+ this.state = STATE.VALUE;
318
266
  } else if (c === Char.closeBracket) {
319
- emitNode(this, 'onclosearray');
267
+ this.emitNode('onclosearray');
320
268
  this.depth--;
321
- this.state = this.stack.pop() || S.VALUE;
322
- } else if (isWhitespace(c)) continue;else error(this, 'Bad array');
269
+ this.state = this.stack.pop() || STATE.VALUE;
270
+ } else if (isWhitespace(c)) continue;else this._error('Bad array');
323
271
 
324
272
  continue;
325
273
 
326
- case S.STRING:
274
+ case STATE.STRING:
327
275
  if (this.textNode === undefined) {
328
276
  this.textNode = '';
329
277
  }
330
278
 
331
- var starti = i - 1,
332
- slashed = this.slashed,
333
- unicodeI = this.unicodeI;
279
+ let starti = i - 1;
280
+ let slashed = this.slashed;
281
+ let unicodeI = this.unicodeI;
334
282
 
335
283
  STRING_BIGLOOP: while (true) {
336
- if (DEBUG) console.log(i, c, STATE[this.state], slashed);
337
-
338
284
  while (unicodeI > 0) {
339
285
  this.unicodeS += String.fromCharCode(c);
340
286
  c = chunk.charCodeAt(i++);
@@ -352,7 +298,7 @@ class ClarinetParser {
352
298
  }
353
299
 
354
300
  if (c === Char.doubleQuote && !slashed) {
355
- this.state = this.stack.pop() || S.VALUE;
301
+ this.state = this.stack.pop() || STATE.VALUE;
356
302
  this.textNode += chunk.substring(starti, i - 1);
357
303
  this.position += i - 1 - starti;
358
304
  break;
@@ -394,7 +340,7 @@ class ClarinetParser {
394
340
  }
395
341
 
396
342
  stringTokenPattern.lastIndex = i;
397
- var reResult = stringTokenPattern.exec(chunk);
343
+ const reResult = stringTokenPattern.exec(chunk);
398
344
 
399
345
  if (reResult === null) {
400
346
  i = chunk.length + 1;
@@ -417,85 +363,87 @@ class ClarinetParser {
417
363
  this.unicodeI = unicodeI;
418
364
  continue;
419
365
 
420
- case S.TRUE:
421
- if (c === Char.r) this.state = S.TRUE2;else error(this, 'Invalid true started with t' + c);
366
+ case STATE.TRUE:
367
+ if (c === Char.r) this.state = STATE.TRUE2;else this._error("Invalid true started with t".concat(c));
422
368
  continue;
423
369
 
424
- case S.TRUE2:
425
- if (c === Char.u) this.state = S.TRUE3;else error(this, 'Invalid true started with tr' + c);
370
+ case STATE.TRUE2:
371
+ if (c === Char.u) this.state = STATE.TRUE3;else this._error("Invalid true started with tr".concat(c));
426
372
  continue;
427
373
 
428
- case S.TRUE3:
374
+ case STATE.TRUE3:
429
375
  if (c === Char.e) {
430
- emit(this, 'onvalue', true);
431
- this.state = this.stack.pop() || S.VALUE;
432
- } else error(this, 'Invalid true started with tru' + c);
376
+ this.emit('onvalue', true);
377
+ this.state = this.stack.pop() || STATE.VALUE;
378
+ } else this._error("Invalid true started with tru".concat(c));
433
379
 
434
380
  continue;
435
381
 
436
- case S.FALSE:
437
- if (c === Char.a) this.state = S.FALSE2;else error(this, 'Invalid false started with f' + c);
382
+ case STATE.FALSE:
383
+ if (c === Char.a) this.state = STATE.FALSE2;else this._error("Invalid false started with f".concat(c));
438
384
  continue;
439
385
 
440
- case S.FALSE2:
441
- if (c === Char.l) this.state = S.FALSE3;else error(this, 'Invalid false started with fa' + c);
386
+ case STATE.FALSE2:
387
+ if (c === Char.l) this.state = STATE.FALSE3;else this._error("Invalid false started with fa".concat(c));
442
388
  continue;
443
389
 
444
- case S.FALSE3:
445
- if (c === Char.s) this.state = S.FALSE4;else error(this, 'Invalid false started with fal' + c);
390
+ case STATE.FALSE3:
391
+ if (c === Char.s) this.state = STATE.FALSE4;else this._error("Invalid false started with fal".concat(c));
446
392
  continue;
447
393
 
448
- case S.FALSE4:
394
+ case STATE.FALSE4:
449
395
  if (c === Char.e) {
450
- emit(this, 'onvalue', false);
451
- this.state = this.stack.pop() || S.VALUE;
452
- } else error(this, 'Invalid false started with fals' + c);
396
+ this.emit('onvalue', false);
397
+ this.state = this.stack.pop() || STATE.VALUE;
398
+ } else this._error("Invalid false started with fals".concat(c));
453
399
 
454
400
  continue;
455
401
 
456
- case S.NULL:
457
- if (c === Char.u) this.state = S.NULL2;else error(this, 'Invalid null started with n' + c);
402
+ case STATE.NULL:
403
+ if (c === Char.u) this.state = STATE.NULL2;else this._error("Invalid null started with n".concat(c));
458
404
  continue;
459
405
 
460
- case S.NULL2:
461
- if (c === Char.l) this.state = S.NULL3;else error(this, 'Invalid null started with nu' + c);
406
+ case STATE.NULL2:
407
+ if (c === Char.l) this.state = STATE.NULL3;else this._error("Invalid null started with nu".concat(c));
462
408
  continue;
463
409
 
464
- case S.NULL3:
410
+ case STATE.NULL3:
465
411
  if (c === Char.l) {
466
- emit(this, 'onvalue', null);
467
- this.state = this.stack.pop() || S.VALUE;
468
- } else error(this, 'Invalid null started with nul' + c);
412
+ this.emit('onvalue', null);
413
+ this.state = this.stack.pop() || STATE.VALUE;
414
+ } else this._error("Invalid null started with nul".concat(c));
469
415
 
470
416
  continue;
471
417
 
472
- case S.NUMBER_DECIMAL_POINT:
418
+ case STATE.NUMBER_DECIMAL_POINT:
473
419
  if (c === Char.period) {
474
420
  this.numberNode += '.';
475
- this.state = S.NUMBER_DIGIT;
476
- } else error(this, 'Leading zero not followed by .');
421
+ this.state = STATE.NUMBER_DIGIT;
422
+ } else this._error('Leading zero not followed by .');
477
423
 
478
424
  continue;
479
425
 
480
- case S.NUMBER_DIGIT:
426
+ case STATE.NUMBER_DIGIT:
481
427
  if (Char._0 <= c && c <= Char._9) this.numberNode += String.fromCharCode(c);else if (c === Char.period) {
482
- if (this.numberNode.indexOf('.') !== -1) error(this, 'Invalid number has two dots');
428
+ if (this.numberNode.indexOf('.') !== -1) this._error('Invalid number has two dots');
483
429
  this.numberNode += '.';
484
430
  } else if (c === Char.e || c === Char.E) {
485
- if (this.numberNode.indexOf('e') !== -1 || this.numberNode.indexOf('E') !== -1) error(this, 'Invalid number has two exponential');
431
+ if (this.numberNode.indexOf('e') !== -1 || this.numberNode.indexOf('E') !== -1) this._error('Invalid number has two exponential');
486
432
  this.numberNode += 'e';
487
433
  } else if (c === Char.plus || c === Char.minus) {
488
- if (!(p === Char.e || p === Char.E)) error(this, 'Invalid symbol in number');
434
+ if (!(p === Char.e || p === Char.E)) this._error('Invalid symbol in number');
489
435
  this.numberNode += String.fromCharCode(c);
490
436
  } else {
491
- closeNumber(this);
437
+ this._closeNumber();
438
+
492
439
  i--;
493
- this.state = this.stack.pop() || S.VALUE;
440
+ this.state = this.stack.pop() || STATE.VALUE;
494
441
  }
495
442
  continue;
496
443
 
497
444
  default:
498
- error(this, 'Unknown state: ' + this.state);
445
+ this._error("Unknown state: ".concat(this.state));
446
+
499
447
  }
500
448
  }
501
449
 
@@ -503,64 +451,61 @@ class ClarinetParser {
503
451
  checkBufferLength(this);
504
452
  }
505
453
 
506
- emit(this, 'onchunkparsed');
454
+ this.emit('onchunkparsed');
507
455
  return this;
508
456
  }
509
457
 
510
- }
511
-
512
- exports.default = ClarinetParser;
458
+ _closeValue(event = 'onvalue') {
459
+ if (this.textNode !== undefined) {
460
+ this.emit(event, this.textNode);
461
+ }
513
462
 
514
- function emit(parser, event, data) {
515
- if (DEBUG) {
516
- console.log('-- emit', event, data);
463
+ this.textNode = undefined;
517
464
  }
518
465
 
519
- if (parser[event]) {
520
- parser[event](data, parser);
466
+ _closeNumber() {
467
+ if (this.numberNode) this.emit('onvalue', parseFloat(this.numberNode));
468
+ this.numberNode = '';
521
469
  }
522
- }
523
-
524
- function emitNode(parser, event, data) {
525
- closeValue(parser);
526
- emit(parser, event, data);
527
- }
528
470
 
529
- function closeValue(parser, event) {
530
- parser.textNode = textopts(parser.options, parser.textNode);
471
+ _error(message = '') {
472
+ this._closeValue();
531
473
 
532
- if (parser.textNode !== undefined) {
533
- emit(parser, event ? event : 'onvalue', parser.textNode);
474
+ message += "\nLine: ".concat(this.line, "\nColumn: ").concat(this.column, "\nChar: ").concat(this.c);
475
+ const error = new Error(message);
476
+ this.error = error;
477
+ this.emit('onerror', error);
534
478
  }
535
479
 
536
- parser.textNode = undefined;
537
480
  }
538
481
 
539
- function closeNumber(parser) {
540
- if (parser.numberNode) emit(parser, 'onvalue', parseFloat(parser.numberNode));
541
- parser.numberNode = '';
482
+ exports.default = ClarinetParser;
483
+
484
+ function isWhitespace(c) {
485
+ return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab;
542
486
  }
543
487
 
544
- function textopts(opt, text) {
545
- if (text === undefined) {
546
- return text;
547
- }
488
+ function checkBufferLength(parser) {
489
+ const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);
490
+ let maxActual = 0;
548
491
 
549
- if (opt.trim) text = text.trim();
550
- if (opt.normalize) text = text.replace(/\s+/g, ' ');
551
- return text;
552
- }
492
+ for (const buffer of ['textNode', 'numberNode']) {
493
+ const len = parser[buffer] === undefined ? 0 : parser[buffer].length;
553
494
 
554
- function error(parser, er) {
555
- closeValue(parser);
556
- er += '\nLine: ' + parser.line + '\nColumn: ' + parser.column + '\nChar: ' + parser.c;
557
- er = new Error(er);
558
- parser.error = er;
559
- emit(parser, 'onerror', er);
560
- return parser;
561
- }
495
+ if (len > maxAllowed) {
496
+ switch (buffer) {
497
+ case 'text':
498
+ break;
562
499
 
563
- function isWhitespace(c) {
564
- return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab;
500
+ default:
501
+ parser._error("Max buffer length exceeded: ".concat(buffer));
502
+
503
+ }
504
+ }
505
+
506
+ maxActual = Math.max(maxActual, len);
507
+ }
508
+
509
+ parser.bufferCheckPosition = MAX_BUFFER_LENGTH - maxActual + parser.position;
565
510
  }
566
511
  //# sourceMappingURL=clarinet.js.map