@loaders.gl/json 3.1.0-alpha.5 → 3.1.0-beta.5
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.
- package/dist/bundle.d.ts +1 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +1839 -0
- package/dist/es5/geojson-loader.js +1 -1
- package/dist/es5/geojson-loader.js.map +1 -1
- package/dist/es5/json-loader.js +1 -1
- package/dist/es5/json-loader.js.map +1 -1
- package/dist/es5/lib/clarinet/clarinet.js +226 -281
- package/dist/es5/lib/clarinet/clarinet.js.map +1 -1
- package/dist/es5/lib/parser/json-parser.js +45 -47
- package/dist/es5/lib/parser/json-parser.js.map +1 -1
- package/dist/es5/lib/parser/streaming-json-parser.js +29 -34
- package/dist/es5/lib/parser/streaming-json-parser.js.map +1 -1
- package/dist/es5/ndjson-loader.js +5 -2
- package/dist/es5/ndjson-loader.js.map +1 -1
- package/dist/esm/geojson-loader.js +1 -1
- package/dist/esm/geojson-loader.js.map +1 -1
- package/dist/esm/json-loader.js +1 -1
- package/dist/esm/json-loader.js.map +1 -1
- package/dist/esm/lib/clarinet/clarinet.js +222 -260
- package/dist/esm/lib/clarinet/clarinet.js.map +1 -1
- package/dist/esm/lib/parser/json-parser.js +48 -47
- package/dist/esm/lib/parser/json-parser.js.map +1 -1
- package/dist/esm/lib/parser/streaming-json-parser.js +29 -34
- package/dist/esm/lib/parser/streaming-json-parser.js.map +1 -1
- package/dist/esm/ndjson-loader.js +3 -1
- package/dist/esm/ndjson-loader.js.map +1 -1
- package/dist/geojson-loader.d.ts +1 -0
- package/dist/geojson-loader.d.ts.map +1 -0
- package/dist/geojson-loader.js +78 -0
- package/dist/geojson-worker.js +1702 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/json-loader.d.ts +1 -0
- package/dist/json-loader.d.ts.map +1 -0
- package/dist/json-loader.js +57 -0
- package/dist/jsonl-loader.d.ts +1 -0
- package/dist/jsonl-loader.d.ts.map +1 -0
- package/dist/jsonl-loader.js +54 -0
- package/dist/lib/clarinet/clarinet.d.ts +72 -38
- package/dist/lib/clarinet/clarinet.d.ts.map +1 -0
- package/dist/lib/clarinet/clarinet.js +535 -0
- package/dist/lib/jsonpath/jsonpath.d.ts +1 -0
- package/dist/lib/jsonpath/jsonpath.d.ts.map +1 -0
- package/dist/lib/jsonpath/jsonpath.js +89 -0
- package/dist/lib/parse-json-in-batches.d.ts +1 -0
- package/dist/lib/parse-json-in-batches.d.ts.map +1 -0
- package/dist/lib/parse-json-in-batches.js +80 -0
- package/dist/lib/parse-json.d.ts +1 -0
- package/dist/lib/parse-json.d.ts.map +1 -0
- package/dist/lib/parse-json.js +29 -0
- package/dist/lib/parse-ndjson-in-batches.d.ts +1 -0
- package/dist/lib/parse-ndjson-in-batches.d.ts.map +1 -0
- package/dist/lib/parse-ndjson-in-batches.js +35 -0
- package/dist/lib/parse-ndjson.d.ts +1 -0
- package/dist/lib/parse-ndjson.d.ts.map +1 -0
- package/dist/lib/parse-ndjson.js +14 -0
- package/dist/lib/parser/json-parser.d.ts +10 -5
- package/dist/lib/parser/json-parser.d.ts.map +1 -0
- package/dist/lib/parser/json-parser.js +98 -0
- package/dist/lib/parser/streaming-json-parser.d.ts +1 -1
- package/dist/lib/parser/streaming-json-parser.d.ts.map +1 -0
- package/dist/lib/parser/streaming-json-parser.js +100 -0
- package/dist/ndjson-loader.d.ts +22 -2
- package/dist/ndjson-loader.d.ts.map +1 -0
- package/dist/ndjson-loader.js +35 -0
- package/dist/workers/geojson-worker.d.ts +1 -0
- package/dist/workers/geojson-worker.d.ts.map +1 -0
- package/dist/workers/geojson-worker.js +5 -0
- package/package.json +7 -8
- package/src/lib/clarinet/clarinet.ts +539 -0
- package/src/lib/parser/json-parser.ts +52 -55
- package/src/lib/parser/streaming-json-parser.ts +28 -32
- package/src/ndjson-loader.ts +3 -1
- package/dist/dist.min.js +0 -2
- package/dist/dist.min.js.map +0 -1
- package/dist/geojson-worker.js.map +0 -1
- 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 =
|
|
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
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
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
|
-
|
|
110
|
-
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
|
-
|
|
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(
|
|
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
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
|
240
|
-
if (c === Char.openBrace) this.state =
|
|
241
|
-
|
|
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
|
|
246
|
-
case
|
|
190
|
+
case STATE.OPEN_KEY:
|
|
191
|
+
case STATE.OPEN_OBJECT:
|
|
247
192
|
if (isWhitespace(c)) continue;
|
|
248
|
-
if (this.state ===
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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
|
|
262
|
-
case
|
|
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 ===
|
|
268
|
-
this.stack.push(
|
|
269
|
-
|
|
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
|
|
215
|
+
} else this._closeValue('onkey');
|
|
272
216
|
|
|
273
|
-
this.state =
|
|
217
|
+
this.state = STATE.VALUE;
|
|
274
218
|
} else if (c === Char.closeBrace) {
|
|
275
|
-
emitNode(
|
|
219
|
+
this.emitNode('oncloseobject');
|
|
276
220
|
this.depth--;
|
|
277
|
-
this.state = this.stack.pop() ||
|
|
221
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
278
222
|
} else if (c === Char.comma) {
|
|
279
|
-
if (this.state ===
|
|
280
|
-
|
|
281
|
-
this.
|
|
282
|
-
|
|
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
|
|
287
|
-
case
|
|
232
|
+
case STATE.OPEN_ARRAY:
|
|
233
|
+
case STATE.VALUE:
|
|
288
234
|
if (isWhitespace(c)) continue;
|
|
289
235
|
|
|
290
|
-
if (this.state ===
|
|
291
|
-
emit(
|
|
236
|
+
if (this.state === STATE.OPEN_ARRAY) {
|
|
237
|
+
this.emit('onopenarray');
|
|
292
238
|
this.depth++;
|
|
293
|
-
this.state =
|
|
239
|
+
this.state = STATE.VALUE;
|
|
294
240
|
|
|
295
241
|
if (c === Char.closeBracket) {
|
|
296
|
-
emit(
|
|
242
|
+
this.emit('onclosearray');
|
|
297
243
|
this.depth--;
|
|
298
|
-
this.state = this.stack.pop() ||
|
|
244
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
299
245
|
continue;
|
|
300
246
|
} else {
|
|
301
|
-
this.stack.push(
|
|
247
|
+
this.stack.push(STATE.CLOSE_ARRAY);
|
|
302
248
|
}
|
|
303
249
|
}
|
|
304
250
|
|
|
305
|
-
if (c === Char.doubleQuote) this.state =
|
|
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 =
|
|
310
|
-
} else
|
|
255
|
+
this.state = STATE.NUMBER_DIGIT;
|
|
256
|
+
} else this._error('Bad value');
|
|
311
257
|
continue;
|
|
312
258
|
|
|
313
|
-
case
|
|
259
|
+
case STATE.CLOSE_ARRAY:
|
|
314
260
|
if (c === Char.comma) {
|
|
315
|
-
this.stack.push(
|
|
316
|
-
|
|
317
|
-
this.
|
|
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(
|
|
267
|
+
this.emitNode('onclosearray');
|
|
320
268
|
this.depth--;
|
|
321
|
-
this.state = this.stack.pop() ||
|
|
322
|
-
} else if (isWhitespace(c)) continue;else
|
|
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
|
|
274
|
+
case STATE.STRING:
|
|
327
275
|
if (this.textNode === undefined) {
|
|
328
276
|
this.textNode = '';
|
|
329
277
|
}
|
|
330
278
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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() ||
|
|
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
|
-
|
|
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
|
|
421
|
-
if (c === Char.r) this.state =
|
|
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
|
|
425
|
-
if (c === Char.u) this.state =
|
|
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
|
|
374
|
+
case STATE.TRUE3:
|
|
429
375
|
if (c === Char.e) {
|
|
430
|
-
emit(
|
|
431
|
-
this.state = this.stack.pop() ||
|
|
432
|
-
} else
|
|
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
|
|
437
|
-
if (c === Char.a) this.state =
|
|
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
|
|
441
|
-
if (c === Char.l) this.state =
|
|
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
|
|
445
|
-
if (c === Char.s) this.state =
|
|
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
|
|
394
|
+
case STATE.FALSE4:
|
|
449
395
|
if (c === Char.e) {
|
|
450
|
-
emit(
|
|
451
|
-
this.state = this.stack.pop() ||
|
|
452
|
-
} else
|
|
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
|
|
457
|
-
if (c === Char.u) this.state =
|
|
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
|
|
461
|
-
if (c === Char.l) this.state =
|
|
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
|
|
410
|
+
case STATE.NULL3:
|
|
465
411
|
if (c === Char.l) {
|
|
466
|
-
emit(
|
|
467
|
-
this.state = this.stack.pop() ||
|
|
468
|
-
} else
|
|
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
|
|
418
|
+
case STATE.NUMBER_DECIMAL_POINT:
|
|
473
419
|
if (c === Char.period) {
|
|
474
420
|
this.numberNode += '.';
|
|
475
|
-
this.state =
|
|
476
|
-
} else
|
|
421
|
+
this.state = STATE.NUMBER_DIGIT;
|
|
422
|
+
} else this._error('Leading zero not followed by .');
|
|
477
423
|
|
|
478
424
|
continue;
|
|
479
425
|
|
|
480
|
-
case
|
|
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)
|
|
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)
|
|
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))
|
|
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
|
-
|
|
437
|
+
this._closeNumber();
|
|
438
|
+
|
|
492
439
|
i--;
|
|
493
|
-
this.state = this.stack.pop() ||
|
|
440
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
494
441
|
}
|
|
495
442
|
continue;
|
|
496
443
|
|
|
497
444
|
default:
|
|
498
|
-
|
|
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(
|
|
454
|
+
this.emit('onchunkparsed');
|
|
507
455
|
return this;
|
|
508
456
|
}
|
|
509
457
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
458
|
+
_closeValue(event = 'onvalue') {
|
|
459
|
+
if (this.textNode !== undefined) {
|
|
460
|
+
this.emit(event, this.textNode);
|
|
461
|
+
}
|
|
513
462
|
|
|
514
|
-
|
|
515
|
-
if (DEBUG) {
|
|
516
|
-
console.log('-- emit', event, data);
|
|
463
|
+
this.textNode = undefined;
|
|
517
464
|
}
|
|
518
465
|
|
|
519
|
-
|
|
520
|
-
|
|
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
|
-
|
|
530
|
-
|
|
471
|
+
_error(message = '') {
|
|
472
|
+
this._closeValue();
|
|
531
473
|
|
|
532
|
-
|
|
533
|
-
|
|
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
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
}
|
|
488
|
+
function checkBufferLength(parser) {
|
|
489
|
+
const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);
|
|
490
|
+
let maxActual = 0;
|
|
548
491
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
return text;
|
|
552
|
-
}
|
|
492
|
+
for (const buffer of ['textNode', 'numberNode']) {
|
|
493
|
+
const len = parser[buffer] === undefined ? 0 : parser[buffer].length;
|
|
553
494
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
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
|
-
|
|
564
|
-
|
|
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
|