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