clarity-pattern-parser 11.2.1 → 11.3.0
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/TODO.md +1 -3
- package/dist/ast/Node.d.ts +3 -0
- package/dist/index.browser.js +36 -21
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +36 -21
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +36 -21
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +2 -2
- package/dist/patterns/CursorHistory.d.ts +2 -6
- package/dist/patterns/HistoryRecord.d.ts +8 -0
- package/package.json +1 -1
- package/src/ast/Node.test.ts +5 -2
- package/src/ast/Node.ts +20 -3
- package/src/patterns/Cursor.ts +2 -2
- package/src/patterns/CursorHistory.ts +19 -20
- package/src/patterns/FiniteRepeat.test.ts +12 -12
- package/src/patterns/HistoryRecord.ts +9 -0
- package/src/patterns/InfiniteRepeat.test.ts +6 -6
- package/src/patterns/Literal.test.ts +3 -3
- package/src/patterns/Not.test.ts +1 -1
- package/src/patterns/Options.test.ts +3 -3
- package/src/patterns/PrecedenceTree.test.ts +9 -9
- package/src/patterns/Reference.test.ts +1 -1
- package/src/patterns/Regex.test.ts +1 -1
- package/src/patterns/Repeat.test.ts +8 -8
- package/src/patterns/Sequence.test.ts +6 -6
- package/src/patterns/TakeUntil.ts +7 -7
package/dist/index.esm.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
function defaultVisitor(node) {
|
|
2
2
|
return node;
|
|
3
3
|
}
|
|
4
|
+
let idIndex$b = 0;
|
|
4
5
|
class Node {
|
|
6
|
+
get id() {
|
|
7
|
+
return this._id;
|
|
8
|
+
}
|
|
5
9
|
get type() {
|
|
6
10
|
return this._type;
|
|
7
11
|
}
|
|
@@ -36,6 +40,7 @@ class Node {
|
|
|
36
40
|
return !this.hasChildren;
|
|
37
41
|
}
|
|
38
42
|
constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
|
|
43
|
+
this._id = String(idIndex$b++);
|
|
39
44
|
this._type = type;
|
|
40
45
|
this._name = name;
|
|
41
46
|
this._firstIndex = firstIndex;
|
|
@@ -196,7 +201,8 @@ class Node {
|
|
|
196
201
|
}
|
|
197
202
|
}
|
|
198
203
|
clone() {
|
|
199
|
-
|
|
204
|
+
const node = new Node(this._type, this._name, this._firstIndex, this._lastIndex, this._children.map((c) => c.clone()), this._value);
|
|
205
|
+
return node;
|
|
200
206
|
}
|
|
201
207
|
normalize(startIndex = this._firstIndex) {
|
|
202
208
|
let length = 0;
|
|
@@ -224,6 +230,7 @@ class Node {
|
|
|
224
230
|
}
|
|
225
231
|
toCycleFreeObject() {
|
|
226
232
|
return {
|
|
233
|
+
id: this._id,
|
|
227
234
|
type: this._type,
|
|
228
235
|
name: this._name,
|
|
229
236
|
value: this.toString(),
|
|
@@ -236,7 +243,12 @@ class Node {
|
|
|
236
243
|
return JSON.stringify(this.toCycleFreeObject(), null, space);
|
|
237
244
|
}
|
|
238
245
|
isEqual(node) {
|
|
239
|
-
return node.
|
|
246
|
+
return node._type === this._type &&
|
|
247
|
+
node._name === this._name &&
|
|
248
|
+
node._firstIndex === this._firstIndex &&
|
|
249
|
+
node._lastIndex === this._lastIndex &&
|
|
250
|
+
node._value === this._value &&
|
|
251
|
+
this._children.every((child, index) => child.isEqual(node._children[index]));
|
|
240
252
|
}
|
|
241
253
|
static createValueNode(type, name, value = "") {
|
|
242
254
|
return new Node(type, name, 0, 0, [], value);
|
|
@@ -348,14 +360,15 @@ class CursorHistory {
|
|
|
348
360
|
return this._patterns;
|
|
349
361
|
}
|
|
350
362
|
recordMatch(pattern, node) {
|
|
363
|
+
const record = {
|
|
364
|
+
pattern,
|
|
365
|
+
ast: node,
|
|
366
|
+
error: null
|
|
367
|
+
};
|
|
351
368
|
if (this._isRecording) {
|
|
352
369
|
this._patterns.push(pattern);
|
|
353
370
|
this._nodes.push(node);
|
|
354
|
-
this._records.push(
|
|
355
|
-
pattern,
|
|
356
|
-
ast: node,
|
|
357
|
-
error: null
|
|
358
|
-
});
|
|
371
|
+
this._records.push(record);
|
|
359
372
|
}
|
|
360
373
|
this._rootMatch.pattern = pattern;
|
|
361
374
|
this._rootMatch.node = node;
|
|
@@ -389,28 +402,29 @@ class CursorHistory {
|
|
|
389
402
|
}
|
|
390
403
|
recordErrorAt(startIndex, lastIndex, pattern) {
|
|
391
404
|
const error = new ParseError(startIndex, lastIndex, pattern);
|
|
405
|
+
const record = {
|
|
406
|
+
pattern,
|
|
407
|
+
ast: null,
|
|
408
|
+
error
|
|
409
|
+
};
|
|
392
410
|
this._currentError = error;
|
|
393
411
|
if (this._furthestError === null || lastIndex > this._furthestError.lastIndex) {
|
|
394
412
|
this._furthestError = error;
|
|
395
413
|
}
|
|
396
414
|
if (this._isRecording) {
|
|
397
415
|
this._errors.push(error);
|
|
398
|
-
this.records.push(
|
|
399
|
-
pattern,
|
|
400
|
-
ast: null,
|
|
401
|
-
error
|
|
402
|
-
});
|
|
416
|
+
this.records.push(record);
|
|
403
417
|
}
|
|
404
418
|
}
|
|
419
|
+
resolveError() {
|
|
420
|
+
this._currentError = null;
|
|
421
|
+
}
|
|
405
422
|
startRecording() {
|
|
406
423
|
this._isRecording = true;
|
|
407
424
|
}
|
|
408
425
|
stopRecording() {
|
|
409
426
|
this._isRecording = false;
|
|
410
427
|
}
|
|
411
|
-
resolveError() {
|
|
412
|
-
this._currentError = null;
|
|
413
|
-
}
|
|
414
428
|
}
|
|
415
429
|
|
|
416
430
|
class Cursor {
|
|
@@ -507,8 +521,8 @@ class Cursor {
|
|
|
507
521
|
recordMatch(pattern, node) {
|
|
508
522
|
this._history.recordMatch(pattern, node);
|
|
509
523
|
}
|
|
510
|
-
recordErrorAt(startIndex,
|
|
511
|
-
this._history.recordErrorAt(startIndex,
|
|
524
|
+
recordErrorAt(startIndex, lastIndex, onPattern) {
|
|
525
|
+
this._history.recordErrorAt(startIndex, lastIndex, onPattern);
|
|
512
526
|
}
|
|
513
527
|
resolveError() {
|
|
514
528
|
this._history.resolveError();
|
|
@@ -3206,15 +3220,16 @@ class TakeUntil {
|
|
|
3206
3220
|
let foundMatch = false;
|
|
3207
3221
|
this._startedOnIndex = cursor.index;
|
|
3208
3222
|
let terminatingResult = this._terminatingPattern.parse(cursor);
|
|
3223
|
+
cursor.resolveError();
|
|
3209
3224
|
if (terminatingResult == null) {
|
|
3210
3225
|
foundMatch = true;
|
|
3211
3226
|
cursor.moveTo(cursorIndex);
|
|
3212
3227
|
cursorIndex += 1;
|
|
3213
3228
|
cursor.hasNext() && cursor.next();
|
|
3214
|
-
cursor.resolveError();
|
|
3215
3229
|
}
|
|
3216
3230
|
while (true) {
|
|
3217
3231
|
terminatingResult = this._terminatingPattern.parse(cursor);
|
|
3232
|
+
cursor.resolveError();
|
|
3218
3233
|
if (terminatingResult == null) {
|
|
3219
3234
|
cursor.moveTo(cursorIndex);
|
|
3220
3235
|
cursorIndex += 1;
|
|
@@ -3224,7 +3239,6 @@ class TakeUntil {
|
|
|
3224
3239
|
else {
|
|
3225
3240
|
break;
|
|
3226
3241
|
}
|
|
3227
|
-
cursor.resolveError();
|
|
3228
3242
|
}
|
|
3229
3243
|
else {
|
|
3230
3244
|
break;
|
|
@@ -3233,10 +3247,11 @@ class TakeUntil {
|
|
|
3233
3247
|
if (foundMatch) {
|
|
3234
3248
|
cursor.moveTo(cursorIndex - 1);
|
|
3235
3249
|
const value = cursor.getChars(this.startedOnIndex, cursorIndex - 1);
|
|
3236
|
-
|
|
3250
|
+
const node = Node.createValueNode(this._type, this._name, value);
|
|
3251
|
+
cursor.recordMatch(this, node);
|
|
3252
|
+
return node;
|
|
3237
3253
|
}
|
|
3238
3254
|
else {
|
|
3239
|
-
cursor.resolveError();
|
|
3240
3255
|
cursor.moveTo(this.startedOnIndex);
|
|
3241
3256
|
cursor.recordErrorAt(this._startedOnIndex, this._startedOnIndex, this);
|
|
3242
3257
|
return null;
|