clarity-pattern-parser 10.0.5 → 10.0.6

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.
@@ -285,7 +285,6 @@
285
285
  this._patterns = [];
286
286
  this._nodes = [];
287
287
  this._errors = [];
288
- this._trace = [];
289
288
  }
290
289
  get isRecording() {
291
290
  return this._isRecording;
@@ -314,9 +313,6 @@
314
313
  get patterns() {
315
314
  return this._patterns;
316
315
  }
317
- get trace() {
318
- return this._trace;
319
- }
320
316
  recordMatch(pattern, node) {
321
317
  if (this._isRecording) {
322
318
  this._patterns.push(pattern);
@@ -371,21 +367,8 @@
371
367
  resolveError() {
372
368
  this._currentError = null;
373
369
  }
374
- pushStackTrace(trace) {
375
- if (this._isRecording) {
376
- this._trace.push(trace);
377
- }
378
- }
379
370
  }
380
371
 
381
- class CyclicalParseError extends Error {
382
- constructor(patternId, patternName, cursorIndex) {
383
- super("Cyclical Parse Error");
384
- this.patternId = patternId;
385
- this.patternName = patternName;
386
- this.cursorIndex = cursorIndex;
387
- }
388
- }
389
372
  class Cursor {
390
373
  get text() {
391
374
  return this._text;
@@ -440,7 +423,6 @@
440
423
  this._index = 0;
441
424
  this._length = [...text].length;
442
425
  this._history = new CursorHistory();
443
- this._stackTrace = [];
444
426
  }
445
427
  hasNext() {
446
428
  return this._index + 1 < this._length;
@@ -490,35 +472,6 @@
490
472
  stopRecording() {
491
473
  this._history.stopRecording();
492
474
  }
493
- startParseWith(pattern) {
494
- const trace = {
495
- pattern,
496
- cursorIndex: this.index
497
- };
498
- const hasCycle = this._stackTrace.filter(t => t.pattern.id === pattern.id && this.index === t.cursorIndex).length > 1;
499
- if (hasCycle) {
500
- throw new CyclicalParseError(pattern.id, pattern.name, this.index);
501
- }
502
- this._history.pushStackTrace(trace);
503
- this._stackTrace.push(trace);
504
- }
505
- endParse() {
506
- this._stackTrace.pop();
507
- }
508
- audit() {
509
- return this._history.trace.map(t => {
510
- const onChar = this.getChars(t.cursorIndex, t.cursorIndex);
511
- const restChars = this.getChars(t.cursorIndex + 1, t.cursorIndex + 5);
512
- const context = `{${t.cursorIndex}}[${onChar}]${restChars}`;
513
- return `${this._buildPatternContext(t.pattern)}-->${context}`;
514
- });
515
- }
516
- _buildPatternContext(pattern) {
517
- if (pattern.parent != null) {
518
- return `${pattern.parent.name}.${pattern.name}`;
519
- }
520
- return pattern.name;
521
- }
522
475
  }
523
476
 
524
477
  let idIndex$9 = 0;
@@ -574,18 +527,15 @@
574
527
  };
575
528
  }
576
529
  parse(cursor) {
577
- cursor.startParseWith(this);
578
530
  this._firstIndex = cursor.index;
579
531
  const passed = this._tryToParse(cursor);
580
532
  if (passed) {
581
533
  cursor.resolveError();
582
534
  const node = this._createNode();
583
535
  cursor.recordMatch(this, node);
584
- cursor.endParse();
585
536
  return node;
586
537
  }
587
538
  cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
588
- cursor.endParse();
589
539
  return null;
590
540
  }
591
541
  _tryToParse(cursor) {
@@ -714,11 +664,9 @@
714
664
  };
715
665
  }
716
666
  parse(cursor) {
717
- cursor.startParseWith(this);
718
667
  this._firstIndex = cursor.index;
719
668
  this.resetState(cursor);
720
669
  this.tryToParse(cursor);
721
- cursor.endParse();
722
670
  return this._node;
723
671
  }
724
672
  resetState(cursor) {
@@ -988,30 +936,26 @@
988
936
  };
989
937
  }
990
938
  parse(cursor) {
991
- cursor.startParseWith(this);
992
939
  this._firstIndex = cursor.index;
993
940
  const node = this._tryToParse(cursor);
994
941
  if (node != null) {
995
942
  cursor.moveTo(node.lastIndex);
996
943
  cursor.resolveError();
997
- cursor.endParse();
998
944
  return node;
999
945
  }
1000
946
  cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1001
- cursor.endParse();
1002
947
  return null;
1003
948
  }
1004
949
  _tryToParse(cursor) {
950
+ if (this._isBeyondRecursiveLimit()) {
951
+ cursor.recordErrorAt(cursor.index, cursor.index, this);
952
+ return null;
953
+ }
1005
954
  const results = [];
1006
955
  for (const pattern of this._children) {
1007
956
  cursor.moveTo(this._firstIndex);
1008
957
  let result = null;
1009
- try {
1010
- result = pattern.parse(cursor);
1011
- }
1012
- catch (_a) {
1013
- continue;
1014
- }
958
+ result = pattern.parse(cursor);
1015
959
  if (this._isGreedy) {
1016
960
  results.push(result);
1017
961
  }
@@ -1024,6 +968,25 @@
1024
968
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1025
969
  return nonNullResults[0] || null;
1026
970
  }
971
+ _isBeyondRecursiveLimit() {
972
+ let pattern = this;
973
+ const matches = [];
974
+ while (pattern.parent != null) {
975
+ if (pattern.type !== "options") {
976
+ pattern = pattern.parent;
977
+ continue;
978
+ }
979
+ const optionsPattern = pattern;
980
+ if (pattern.id === this.id && optionsPattern._firstIndex === this._firstIndex) {
981
+ matches.push(pattern);
982
+ if (matches.length > 2) {
983
+ return true;
984
+ }
985
+ }
986
+ pattern = pattern.parent;
987
+ }
988
+ return false;
989
+ }
1027
990
  getTokens() {
1028
991
  const tokens = [];
1029
992
  for (const child of this._children) {
@@ -1123,7 +1086,6 @@
1123
1086
  }
1124
1087
  }
1125
1088
  parse(cursor) {
1126
- cursor.startParseWith(this);
1127
1089
  const startIndex = cursor.index;
1128
1090
  const nodes = [];
1129
1091
  const modulo = this._hasDivider ? 2 : 1;
@@ -1162,19 +1124,16 @@
1162
1124
  const lastIndex = cursor.index;
1163
1125
  cursor.moveTo(startIndex);
1164
1126
  cursor.recordErrorAt(startIndex, lastIndex, this);
1165
- cursor.endParse();
1166
1127
  return null;
1167
1128
  }
1168
1129
  if (nodes.length === 0 && !cursor.hasError) {
1169
1130
  cursor.moveTo(startIndex);
1170
- cursor.endParse();
1171
1131
  return null;
1172
1132
  }
1173
1133
  const firstIndex = nodes[0].firstIndex;
1174
1134
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1175
1135
  cursor.resolveError();
1176
1136
  cursor.moveTo(lastIndex);
1177
- cursor.endParse();
1178
1137
  return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
1179
1138
  }
1180
1139
  test(text) {
@@ -1320,7 +1279,6 @@
1320
1279
  };
1321
1280
  }
1322
1281
  parse(cursor) {
1323
- cursor.startParseWith(this);
1324
1282
  this._firstIndex = cursor.index;
1325
1283
  this._nodes = [];
1326
1284
  const passed = this._tryToParse(cursor);
@@ -1331,15 +1289,12 @@
1331
1289
  cursor.moveTo(node.lastIndex);
1332
1290
  cursor.recordMatch(this, node);
1333
1291
  }
1334
- cursor.endParse();
1335
1292
  return node;
1336
1293
  }
1337
1294
  if (this._min > 0) {
1338
- cursor.endParse();
1339
1295
  return null;
1340
1296
  }
1341
1297
  cursor.resolveError();
1342
- cursor.endParse();
1343
1298
  return null;
1344
1299
  }
1345
1300
  _meetsMin() {
@@ -1678,19 +1633,20 @@
1678
1633
  };
1679
1634
  }
1680
1635
  parse(cursor) {
1681
- cursor.startParseWith(this);
1682
1636
  this._firstIndex = cursor.index;
1683
1637
  this._nodes = [];
1638
+ if (this._isBeyondRecursiveLimit()) {
1639
+ cursor.recordErrorAt(cursor.index, cursor.index, this);
1640
+ return null;
1641
+ }
1684
1642
  const passed = this.tryToParse(cursor);
1685
1643
  if (passed) {
1686
1644
  const node = this.createNode(cursor);
1687
1645
  if (node !== null) {
1688
1646
  cursor.recordMatch(this, node);
1689
1647
  }
1690
- cursor.endParse();
1691
1648
  return node;
1692
1649
  }
1693
- cursor.endParse();
1694
1650
  return null;
1695
1651
  }
1696
1652
  tryToParse(cursor) {
@@ -1750,6 +1706,25 @@
1750
1706
  }
1751
1707
  return passed;
1752
1708
  }
1709
+ _isBeyondRecursiveLimit() {
1710
+ let pattern = this;
1711
+ const matches = [];
1712
+ while (pattern.parent != null) {
1713
+ if (pattern.type !== "sequence") {
1714
+ pattern = pattern.parent;
1715
+ continue;
1716
+ }
1717
+ const sequencePattern = pattern;
1718
+ if (pattern.id === this.id && sequencePattern._firstIndex === this._firstIndex) {
1719
+ matches.push(pattern);
1720
+ if (matches.length > 1) {
1721
+ return true;
1722
+ }
1723
+ }
1724
+ pattern = pattern.parent;
1725
+ }
1726
+ return false;
1727
+ }
1753
1728
  getLastValidNode() {
1754
1729
  const nodes = filterOutNull(this._nodes);
1755
1730
  if (nodes.length === 0) {
@@ -1932,17 +1907,14 @@
1932
1907
  };
1933
1908
  }
1934
1909
  parse(cursor) {
1935
- cursor.startParseWith(this);
1936
1910
  const firstIndex = cursor.index;
1937
1911
  const node = this._children[0].parse(cursor);
1938
1912
  if (cursor.hasError) {
1939
1913
  cursor.resolveError();
1940
1914
  cursor.moveTo(firstIndex);
1941
- cursor.endParse();
1942
1915
  return null;
1943
1916
  }
1944
1917
  else {
1945
- cursor.endParse();
1946
1918
  return node;
1947
1919
  }
1948
1920
  }
@@ -2247,7 +2219,6 @@
2247
2219
  };
2248
2220
  }
2249
2221
  parse(cursor) {
2250
- cursor.startParseWith(this);
2251
2222
  const firstIndex = cursor.index;
2252
2223
  this._children[0].parse(cursor);
2253
2224
  if (cursor.hasError) {
@@ -2259,7 +2230,6 @@
2259
2230
  cursor.resolveError();
2260
2231
  cursor.recordErrorAt(firstIndex, firstIndex, this);
2261
2232
  }
2262
- cursor.endParse();
2263
2233
  return null;
2264
2234
  }
2265
2235
  clone(name = this._name) {