clarity-pattern-parser 8.2.0 → 8.2.1

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/index.esm.js CHANGED
@@ -1,14 +1,4 @@
1
1
  class Node {
2
- constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
3
- this._type = type;
4
- this._name = name;
5
- this._firstIndex = firstIndex;
6
- this._lastIndex = lastIndex;
7
- this._parent = null;
8
- this._children = children;
9
- this._value = value;
10
- this._children.forEach(c => c._parent = this);
11
- }
12
2
  get type() {
13
3
  return this._type;
14
4
  }
@@ -39,6 +29,16 @@ class Node {
39
29
  get value() {
40
30
  return this.toString();
41
31
  }
32
+ constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
33
+ this._type = type;
34
+ this._name = name;
35
+ this._firstIndex = firstIndex;
36
+ this._lastIndex = lastIndex;
37
+ this._parent = null;
38
+ this._children = children;
39
+ this._value = value;
40
+ this._children.forEach(c => c._parent = this);
41
+ }
42
42
  removeChild(node) {
43
43
  const index = this._children.indexOf(node);
44
44
  if (index > -1) {
@@ -279,12 +279,6 @@ class CursorHistory {
279
279
  }
280
280
 
281
281
  class Cursor {
282
- constructor(text) {
283
- this._text = text;
284
- this._index = 0;
285
- this._length = text.length;
286
- this._history = new CursorHistory();
287
- }
288
282
  get text() {
289
283
  return this._text;
290
284
  }
@@ -327,6 +321,12 @@ class Cursor {
327
321
  get currentChar() {
328
322
  return this._text[this._index];
329
323
  }
324
+ constructor(text) {
325
+ this._text = text;
326
+ this._index = 0;
327
+ this._length = text.length;
328
+ this._history = new CursorHistory();
329
+ }
330
330
  hasNext() {
331
331
  return this._index + 1 < this._length;
332
332
  }
@@ -378,20 +378,6 @@ class Cursor {
378
378
  }
379
379
 
380
380
  class Literal {
381
- constructor(name, value, isOptional = false) {
382
- if (value.length === 0) {
383
- throw new Error("Value Cannot be empty.");
384
- }
385
- this._type = "literal";
386
- this._name = name;
387
- this._literal = value;
388
- this._runes = Array.from(value);
389
- this._isOptional = isOptional;
390
- this._parent = null;
391
- this._firstIndex = 0;
392
- this._lastIndex = 0;
393
- this._endIndex = 0;
394
- }
395
381
  get type() {
396
382
  return this._type;
397
383
  }
@@ -410,6 +396,20 @@ class Literal {
410
396
  get isOptional() {
411
397
  return this._isOptional;
412
398
  }
399
+ constructor(name, value, isOptional = false) {
400
+ if (value.length === 0) {
401
+ throw new Error("Value Cannot be empty.");
402
+ }
403
+ this._type = "literal";
404
+ this._name = name;
405
+ this._literal = value;
406
+ this._runes = Array.from(value);
407
+ this._isOptional = isOptional;
408
+ this._parent = null;
409
+ this._firstIndex = 0;
410
+ this._lastIndex = 0;
411
+ this._endIndex = 0;
412
+ }
413
413
  test(text) {
414
414
  const cursor = new Cursor(text);
415
415
  const ast = this.parse(cursor);
@@ -500,20 +500,6 @@ class Literal {
500
500
  }
501
501
 
502
502
  class Regex {
503
- constructor(name, regex, isOptional = false) {
504
- this._node = null;
505
- this._cursor = null;
506
- this._firstIndex = -1;
507
- this._substring = "";
508
- this._tokens = [];
509
- this._type = "regex";
510
- this._name = name;
511
- this._isOptional = isOptional;
512
- this._parent = null;
513
- this._originalRegexString = regex;
514
- this._regex = new RegExp(`^${regex}`, "g");
515
- this.assertArguments();
516
- }
517
503
  get type() {
518
504
  return this._type;
519
505
  }
@@ -532,6 +518,20 @@ class Regex {
532
518
  get isOptional() {
533
519
  return this._isOptional;
534
520
  }
521
+ constructor(name, regex, isOptional = false) {
522
+ this._node = null;
523
+ this._cursor = null;
524
+ this._firstIndex = -1;
525
+ this._substring = "";
526
+ this._tokens = [];
527
+ this._type = "regex";
528
+ this._name = name;
529
+ this._isOptional = isOptional;
530
+ this._parent = null;
531
+ this._originalRegexString = regex;
532
+ this._regex = new RegExp(`^${regex}`, "g");
533
+ this.assertArguments();
534
+ }
535
535
  assertArguments() {
536
536
  if (this._originalRegexString.length < 1) {
537
537
  throw new Error("Invalid Arguments: The regex string argument needs to be at least one character long.");
@@ -650,14 +650,6 @@ function findPattern(pattern, predicate) {
650
650
  }
651
651
 
652
652
  class Reference {
653
- constructor(name, isOptional = false) {
654
- this._type = "reference";
655
- this._name = name;
656
- this._parent = null;
657
- this._isOptional = isOptional;
658
- this._pattern = null;
659
- this._children = [];
660
- }
661
653
  get type() {
662
654
  return this._type;
663
655
  }
@@ -676,6 +668,14 @@ class Reference {
676
668
  get isOptional() {
677
669
  return this._isOptional;
678
670
  }
671
+ constructor(name, isOptional = false) {
672
+ this._type = "reference";
673
+ this._name = name;
674
+ this._parent = null;
675
+ this._isOptional = isOptional;
676
+ this._pattern = null;
677
+ this._children = [];
678
+ }
679
679
  test(text) {
680
680
  const cursor = new Cursor(text);
681
681
  const ast = this.parse(cursor);
@@ -767,20 +767,6 @@ function clonePatterns(patterns, isOptional) {
767
767
  }
768
768
 
769
769
  class Or {
770
- constructor(name, options, isOptional = false, isGreedy = false) {
771
- if (options.length === 0) {
772
- throw new Error("Need at least one pattern with an 'or' pattern.");
773
- }
774
- const children = clonePatterns(options, false);
775
- this._assignChildrenToParent(children);
776
- this._type = "or";
777
- this._name = name;
778
- this._parent = null;
779
- this._children = children;
780
- this._isOptional = isOptional;
781
- this._firstIndex = 0;
782
- this._isGreedy = isGreedy;
783
- }
784
770
  get type() {
785
771
  return this._type;
786
772
  }
@@ -799,6 +785,20 @@ class Or {
799
785
  get isOptional() {
800
786
  return this._isOptional;
801
787
  }
788
+ constructor(name, options, isOptional = false, isGreedy = false) {
789
+ if (options.length === 0) {
790
+ throw new Error("Need at least one pattern with an 'or' pattern.");
791
+ }
792
+ const children = clonePatterns(options, false);
793
+ this._assignChildrenToParent(children);
794
+ this._type = "or";
795
+ this._name = name;
796
+ this._parent = null;
797
+ this._children = children;
798
+ this._isOptional = isOptional;
799
+ this._firstIndex = 0;
800
+ this._isGreedy = isGreedy;
801
+ }
802
802
  _assignChildrenToParent(children) {
803
803
  for (const child of children) {
804
804
  child.parent = this;
@@ -821,6 +821,7 @@ class Or {
821
821
  this._firstIndex = cursor.index;
822
822
  const node = this._tryToParse(cursor);
823
823
  if (node != null) {
824
+ cursor.moveTo(node.lastIndex);
824
825
  cursor.resolveError();
825
826
  return node;
826
827
  }
@@ -891,28 +892,12 @@ class Or {
891
892
  return findPattern(this, predicate);
892
893
  }
893
894
  clone(name = this._name, isOptional = this._isOptional) {
894
- const or = new Or(name, this._children, isOptional);
895
+ const or = new Or(name, this._children, isOptional, this._isGreedy);
895
896
  return or;
896
897
  }
897
898
  }
898
899
 
899
900
  class FiniteRepeat {
900
- constructor(name, pattern, repeatAmount, options = {}) {
901
- this._type = "finite-repeat";
902
- this._name = name;
903
- this._parent = null;
904
- this._children = [];
905
- this._hasDivider = options.divider != null;
906
- this._min = options.min != null ? options.min : 1;
907
- this._max = repeatAmount;
908
- this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
909
- for (let i = 0; i < repeatAmount; i++) {
910
- this._children.push(pattern.clone(pattern.name));
911
- if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
912
- this._children.push(options.divider.clone(options.divider.name, false));
913
- }
914
- }
915
- }
916
901
  get type() {
917
902
  return this._type;
918
903
  }
@@ -937,6 +922,22 @@ class FiniteRepeat {
937
922
  get max() {
938
923
  return this._max;
939
924
  }
925
+ constructor(name, pattern, repeatAmount, options = {}) {
926
+ this._type = "finite-repeat";
927
+ this._name = name;
928
+ this._parent = null;
929
+ this._children = [];
930
+ this._hasDivider = options.divider != null;
931
+ this._min = options.min != null ? options.min : 1;
932
+ this._max = repeatAmount;
933
+ this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
934
+ for (let i = 0; i < repeatAmount; i++) {
935
+ this._children.push(pattern.clone(pattern.name));
936
+ if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
937
+ this._children.push(options.divider.clone(options.divider.name, false));
938
+ }
939
+ }
940
+ }
940
941
  parse(cursor) {
941
942
  const startIndex = cursor.index;
942
943
  const nodes = [];
@@ -1061,6 +1062,27 @@ class FiniteRepeat {
1061
1062
  }
1062
1063
 
1063
1064
  class InfiniteRepeat {
1065
+ get type() {
1066
+ return this._type;
1067
+ }
1068
+ get name() {
1069
+ return this._name;
1070
+ }
1071
+ get parent() {
1072
+ return this._parent;
1073
+ }
1074
+ set parent(pattern) {
1075
+ this._parent = pattern;
1076
+ }
1077
+ get children() {
1078
+ return this._children;
1079
+ }
1080
+ get isOptional() {
1081
+ return this._min === 0;
1082
+ }
1083
+ get min() {
1084
+ return this._min;
1085
+ }
1064
1086
  constructor(name, pattern, options = {}) {
1065
1087
  const min = options.min != null ? options.min : 1;
1066
1088
  const divider = options.divider;
@@ -1083,27 +1105,6 @@ class InfiniteRepeat {
1083
1105
  this._nodes = [];
1084
1106
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1085
1107
  }
1086
- get type() {
1087
- return this._type;
1088
- }
1089
- get name() {
1090
- return this._name;
1091
- }
1092
- get parent() {
1093
- return this._parent;
1094
- }
1095
- set parent(pattern) {
1096
- this._parent = pattern;
1097
- }
1098
- get children() {
1099
- return this._children;
1100
- }
1101
- get isOptional() {
1102
- return this._min === 0;
1103
- }
1104
- get min() {
1105
- return this._min;
1106
- }
1107
1108
  _assignChildrenToParent(children) {
1108
1109
  for (const child of children) {
1109
1110
  child.parent = this;
@@ -1297,19 +1298,6 @@ class InfiniteRepeat {
1297
1298
  }
1298
1299
 
1299
1300
  class Repeat {
1300
- constructor(name, pattern, options = {}) {
1301
- this._pattern = pattern;
1302
- this._parent = null;
1303
- this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1304
- if (this._options.max !== Infinity) {
1305
- this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1306
- }
1307
- else {
1308
- this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1309
- }
1310
- this._children = [this._repeatPattern];
1311
- this._repeatPattern.parent = this;
1312
- }
1313
1301
  get type() {
1314
1302
  return this._repeatPattern.type;
1315
1303
  }
@@ -1328,6 +1316,19 @@ class Repeat {
1328
1316
  get isOptional() {
1329
1317
  return this._repeatPattern.isOptional;
1330
1318
  }
1319
+ constructor(name, pattern, options = {}) {
1320
+ this._pattern = pattern;
1321
+ this._parent = null;
1322
+ this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1323
+ if (this._options.max !== Infinity) {
1324
+ this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1325
+ }
1326
+ else {
1327
+ this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1328
+ }
1329
+ this._children = [this._repeatPattern];
1330
+ this._repeatPattern.parent = this;
1331
+ }
1331
1332
  parse(cursor) {
1332
1333
  return this._repeatPattern.parse(cursor);
1333
1334
  }
@@ -1397,20 +1398,6 @@ function filterOutNull(nodes) {
1397
1398
  }
1398
1399
 
1399
1400
  class And {
1400
- constructor(name, sequence, isOptional = false) {
1401
- if (sequence.length === 0) {
1402
- throw new Error("Need at least one pattern with an 'and' pattern.");
1403
- }
1404
- const children = clonePatterns(sequence);
1405
- this._assignChildrenToParent(children);
1406
- this._type = "and";
1407
- this._name = name;
1408
- this._isOptional = isOptional;
1409
- this._parent = null;
1410
- this._children = children;
1411
- this._firstIndex = -1;
1412
- this._nodes = [];
1413
- }
1414
1401
  get type() {
1415
1402
  return this._type;
1416
1403
  }
@@ -1429,6 +1416,20 @@ class And {
1429
1416
  get isOptional() {
1430
1417
  return this._isOptional;
1431
1418
  }
1419
+ constructor(name, sequence, isOptional = false) {
1420
+ if (sequence.length === 0) {
1421
+ throw new Error("Need at least one pattern with an 'and' pattern.");
1422
+ }
1423
+ const children = clonePatterns(sequence);
1424
+ this._assignChildrenToParent(children);
1425
+ this._type = "and";
1426
+ this._name = name;
1427
+ this._isOptional = isOptional;
1428
+ this._parent = null;
1429
+ this._children = children;
1430
+ this._firstIndex = -1;
1431
+ this._nodes = [];
1432
+ }
1432
1433
  _assignChildrenToParent(children) {
1433
1434
  for (const child of children) {
1434
1435
  child.parent = this;
@@ -1745,13 +1746,6 @@ const line = new Or("line", [
1745
1746
  const grammar = new Repeat("grammar", line, { divider: newLine });
1746
1747
 
1747
1748
  class Not {
1748
- constructor(name, pattern) {
1749
- this._type = "not";
1750
- this._name = name;
1751
- this._parent = null;
1752
- this._children = [pattern.clone(pattern.name, false)];
1753
- this._children[0].parent = this;
1754
- }
1755
1749
  get type() {
1756
1750
  return this._type;
1757
1751
  }
@@ -1770,6 +1764,13 @@ class Not {
1770
1764
  get isOptional() {
1771
1765
  return false;
1772
1766
  }
1767
+ constructor(name, pattern) {
1768
+ this._type = "not";
1769
+ this._name = name;
1770
+ this._parent = null;
1771
+ this._children = [pattern.clone(pattern.name, false)];
1772
+ this._children[0].parent = this;
1773
+ }
1773
1774
  test(text) {
1774
1775
  const cursor = new Cursor(text);
1775
1776
  this.parse(cursor);
@@ -2134,10 +2135,10 @@ class Grammar {
2134
2135
  _buildOr(statementNode) {
2135
2136
  const nameNode = statementNode.find(n => n.name === "name");
2136
2137
  const orNode = statementNode.find(n => n.name === "or-literal");
2137
- const patternNodes = orNode.children.filter(n => n.name == "pattern-name");
2138
+ const patternNodes = orNode.children.filter(n => n.name === "pattern-name");
2138
2139
  const name = nameNode.value;
2139
2140
  const patterns = patternNodes.map(n => this._getPattern(n.value));
2140
- const or = new Or(name, patterns);
2141
+ const or = new Or(name, patterns, false, true);
2141
2142
  this._parseContext.patternsByName.set(name, or);
2142
2143
  }
2143
2144
  _getPattern(name) {
@@ -2150,7 +2151,7 @@ class Grammar {
2150
2151
  _buildAnd(statementNode) {
2151
2152
  const nameNode = statementNode.find(n => n.name === "name");
2152
2153
  const andNode = statementNode.find(n => n.name === "and-literal");
2153
- const patternNodes = andNode.children.filter(n => n.name == "pattern");
2154
+ const patternNodes = andNode.children.filter(n => n.name === "pattern");
2154
2155
  const name = nameNode.value;
2155
2156
  const patterns = patternNodes.map(n => {
2156
2157
  const nameNode = n.find(n => n.name === "pattern-name");
@@ -2169,7 +2170,7 @@ class Grammar {
2169
2170
  _buildRepeat(statementNode) {
2170
2171
  const nameNode = statementNode.find(n => n.name === "name");
2171
2172
  const repeatNode = statementNode.find(n => n.name === "repeat-literal");
2172
- const patternNode = repeatNode.find(n => n.name == "pattern");
2173
+ const patternNode = repeatNode.find(n => n.name === "pattern");
2173
2174
  const patternNameNode = patternNode.find(n => n.name === "pattern-name");
2174
2175
  const dividerNode = repeatNode.find(n => n.name === "divider-pattern");
2175
2176
  const bounds = repeatNode.find(n => n.name === "bounds");