@loaders.gl/json 3.1.0-alpha.3 → 3.1.0-beta.2

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