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