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.js CHANGED
@@ -3,16 +3,6 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  class Node {
6
- constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
7
- this._type = type;
8
- this._name = name;
9
- this._firstIndex = firstIndex;
10
- this._lastIndex = lastIndex;
11
- this._parent = null;
12
- this._children = children;
13
- this._value = value;
14
- this._children.forEach(c => c._parent = this);
15
- }
16
6
  get type() {
17
7
  return this._type;
18
8
  }
@@ -43,6 +33,16 @@ class Node {
43
33
  get value() {
44
34
  return this.toString();
45
35
  }
36
+ constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
37
+ this._type = type;
38
+ this._name = name;
39
+ this._firstIndex = firstIndex;
40
+ this._lastIndex = lastIndex;
41
+ this._parent = null;
42
+ this._children = children;
43
+ this._value = value;
44
+ this._children.forEach(c => c._parent = this);
45
+ }
46
46
  removeChild(node) {
47
47
  const index = this._children.indexOf(node);
48
48
  if (index > -1) {
@@ -283,12 +283,6 @@ class CursorHistory {
283
283
  }
284
284
 
285
285
  class Cursor {
286
- constructor(text) {
287
- this._text = text;
288
- this._index = 0;
289
- this._length = text.length;
290
- this._history = new CursorHistory();
291
- }
292
286
  get text() {
293
287
  return this._text;
294
288
  }
@@ -331,6 +325,12 @@ class Cursor {
331
325
  get currentChar() {
332
326
  return this._text[this._index];
333
327
  }
328
+ constructor(text) {
329
+ this._text = text;
330
+ this._index = 0;
331
+ this._length = text.length;
332
+ this._history = new CursorHistory();
333
+ }
334
334
  hasNext() {
335
335
  return this._index + 1 < this._length;
336
336
  }
@@ -382,20 +382,6 @@ class Cursor {
382
382
  }
383
383
 
384
384
  class Literal {
385
- constructor(name, value, isOptional = false) {
386
- if (value.length === 0) {
387
- throw new Error("Value Cannot be empty.");
388
- }
389
- this._type = "literal";
390
- this._name = name;
391
- this._literal = value;
392
- this._runes = Array.from(value);
393
- this._isOptional = isOptional;
394
- this._parent = null;
395
- this._firstIndex = 0;
396
- this._lastIndex = 0;
397
- this._endIndex = 0;
398
- }
399
385
  get type() {
400
386
  return this._type;
401
387
  }
@@ -414,6 +400,20 @@ class Literal {
414
400
  get isOptional() {
415
401
  return this._isOptional;
416
402
  }
403
+ constructor(name, value, isOptional = false) {
404
+ if (value.length === 0) {
405
+ throw new Error("Value Cannot be empty.");
406
+ }
407
+ this._type = "literal";
408
+ this._name = name;
409
+ this._literal = value;
410
+ this._runes = Array.from(value);
411
+ this._isOptional = isOptional;
412
+ this._parent = null;
413
+ this._firstIndex = 0;
414
+ this._lastIndex = 0;
415
+ this._endIndex = 0;
416
+ }
417
417
  test(text) {
418
418
  const cursor = new Cursor(text);
419
419
  const ast = this.parse(cursor);
@@ -504,20 +504,6 @@ class Literal {
504
504
  }
505
505
 
506
506
  class Regex {
507
- constructor(name, regex, isOptional = false) {
508
- this._node = null;
509
- this._cursor = null;
510
- this._firstIndex = -1;
511
- this._substring = "";
512
- this._tokens = [];
513
- this._type = "regex";
514
- this._name = name;
515
- this._isOptional = isOptional;
516
- this._parent = null;
517
- this._originalRegexString = regex;
518
- this._regex = new RegExp(`^${regex}`, "g");
519
- this.assertArguments();
520
- }
521
507
  get type() {
522
508
  return this._type;
523
509
  }
@@ -536,6 +522,20 @@ class Regex {
536
522
  get isOptional() {
537
523
  return this._isOptional;
538
524
  }
525
+ constructor(name, regex, isOptional = false) {
526
+ this._node = null;
527
+ this._cursor = null;
528
+ this._firstIndex = -1;
529
+ this._substring = "";
530
+ this._tokens = [];
531
+ this._type = "regex";
532
+ this._name = name;
533
+ this._isOptional = isOptional;
534
+ this._parent = null;
535
+ this._originalRegexString = regex;
536
+ this._regex = new RegExp(`^${regex}`, "g");
537
+ this.assertArguments();
538
+ }
539
539
  assertArguments() {
540
540
  if (this._originalRegexString.length < 1) {
541
541
  throw new Error("Invalid Arguments: The regex string argument needs to be at least one character long.");
@@ -654,14 +654,6 @@ function findPattern(pattern, predicate) {
654
654
  }
655
655
 
656
656
  class Reference {
657
- constructor(name, isOptional = false) {
658
- this._type = "reference";
659
- this._name = name;
660
- this._parent = null;
661
- this._isOptional = isOptional;
662
- this._pattern = null;
663
- this._children = [];
664
- }
665
657
  get type() {
666
658
  return this._type;
667
659
  }
@@ -680,6 +672,14 @@ class Reference {
680
672
  get isOptional() {
681
673
  return this._isOptional;
682
674
  }
675
+ constructor(name, isOptional = false) {
676
+ this._type = "reference";
677
+ this._name = name;
678
+ this._parent = null;
679
+ this._isOptional = isOptional;
680
+ this._pattern = null;
681
+ this._children = [];
682
+ }
683
683
  test(text) {
684
684
  const cursor = new Cursor(text);
685
685
  const ast = this.parse(cursor);
@@ -771,20 +771,6 @@ function clonePatterns(patterns, isOptional) {
771
771
  }
772
772
 
773
773
  class Or {
774
- constructor(name, options, isOptional = false, isGreedy = false) {
775
- if (options.length === 0) {
776
- throw new Error("Need at least one pattern with an 'or' pattern.");
777
- }
778
- const children = clonePatterns(options, false);
779
- this._assignChildrenToParent(children);
780
- this._type = "or";
781
- this._name = name;
782
- this._parent = null;
783
- this._children = children;
784
- this._isOptional = isOptional;
785
- this._firstIndex = 0;
786
- this._isGreedy = isGreedy;
787
- }
788
774
  get type() {
789
775
  return this._type;
790
776
  }
@@ -803,6 +789,20 @@ class Or {
803
789
  get isOptional() {
804
790
  return this._isOptional;
805
791
  }
792
+ constructor(name, options, isOptional = false, isGreedy = false) {
793
+ if (options.length === 0) {
794
+ throw new Error("Need at least one pattern with an 'or' pattern.");
795
+ }
796
+ const children = clonePatterns(options, false);
797
+ this._assignChildrenToParent(children);
798
+ this._type = "or";
799
+ this._name = name;
800
+ this._parent = null;
801
+ this._children = children;
802
+ this._isOptional = isOptional;
803
+ this._firstIndex = 0;
804
+ this._isGreedy = isGreedy;
805
+ }
806
806
  _assignChildrenToParent(children) {
807
807
  for (const child of children) {
808
808
  child.parent = this;
@@ -825,6 +825,7 @@ class Or {
825
825
  this._firstIndex = cursor.index;
826
826
  const node = this._tryToParse(cursor);
827
827
  if (node != null) {
828
+ cursor.moveTo(node.lastIndex);
828
829
  cursor.resolveError();
829
830
  return node;
830
831
  }
@@ -895,28 +896,12 @@ class Or {
895
896
  return findPattern(this, predicate);
896
897
  }
897
898
  clone(name = this._name, isOptional = this._isOptional) {
898
- const or = new Or(name, this._children, isOptional);
899
+ const or = new Or(name, this._children, isOptional, this._isGreedy);
899
900
  return or;
900
901
  }
901
902
  }
902
903
 
903
904
  class FiniteRepeat {
904
- constructor(name, pattern, repeatAmount, options = {}) {
905
- this._type = "finite-repeat";
906
- this._name = name;
907
- this._parent = null;
908
- this._children = [];
909
- this._hasDivider = options.divider != null;
910
- this._min = options.min != null ? options.min : 1;
911
- this._max = repeatAmount;
912
- this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
913
- for (let i = 0; i < repeatAmount; i++) {
914
- this._children.push(pattern.clone(pattern.name));
915
- if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
916
- this._children.push(options.divider.clone(options.divider.name, false));
917
- }
918
- }
919
- }
920
905
  get type() {
921
906
  return this._type;
922
907
  }
@@ -941,6 +926,22 @@ class FiniteRepeat {
941
926
  get max() {
942
927
  return this._max;
943
928
  }
929
+ constructor(name, pattern, repeatAmount, options = {}) {
930
+ this._type = "finite-repeat";
931
+ this._name = name;
932
+ this._parent = null;
933
+ this._children = [];
934
+ this._hasDivider = options.divider != null;
935
+ this._min = options.min != null ? options.min : 1;
936
+ this._max = repeatAmount;
937
+ this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
938
+ for (let i = 0; i < repeatAmount; i++) {
939
+ this._children.push(pattern.clone(pattern.name));
940
+ if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
941
+ this._children.push(options.divider.clone(options.divider.name, false));
942
+ }
943
+ }
944
+ }
944
945
  parse(cursor) {
945
946
  const startIndex = cursor.index;
946
947
  const nodes = [];
@@ -1065,6 +1066,27 @@ class FiniteRepeat {
1065
1066
  }
1066
1067
 
1067
1068
  class InfiniteRepeat {
1069
+ get type() {
1070
+ return this._type;
1071
+ }
1072
+ get name() {
1073
+ return this._name;
1074
+ }
1075
+ get parent() {
1076
+ return this._parent;
1077
+ }
1078
+ set parent(pattern) {
1079
+ this._parent = pattern;
1080
+ }
1081
+ get children() {
1082
+ return this._children;
1083
+ }
1084
+ get isOptional() {
1085
+ return this._min === 0;
1086
+ }
1087
+ get min() {
1088
+ return this._min;
1089
+ }
1068
1090
  constructor(name, pattern, options = {}) {
1069
1091
  const min = options.min != null ? options.min : 1;
1070
1092
  const divider = options.divider;
@@ -1087,27 +1109,6 @@ class InfiniteRepeat {
1087
1109
  this._nodes = [];
1088
1110
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1089
1111
  }
1090
- get type() {
1091
- return this._type;
1092
- }
1093
- get name() {
1094
- return this._name;
1095
- }
1096
- get parent() {
1097
- return this._parent;
1098
- }
1099
- set parent(pattern) {
1100
- this._parent = pattern;
1101
- }
1102
- get children() {
1103
- return this._children;
1104
- }
1105
- get isOptional() {
1106
- return this._min === 0;
1107
- }
1108
- get min() {
1109
- return this._min;
1110
- }
1111
1112
  _assignChildrenToParent(children) {
1112
1113
  for (const child of children) {
1113
1114
  child.parent = this;
@@ -1301,19 +1302,6 @@ class InfiniteRepeat {
1301
1302
  }
1302
1303
 
1303
1304
  class Repeat {
1304
- constructor(name, pattern, options = {}) {
1305
- this._pattern = pattern;
1306
- this._parent = null;
1307
- this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1308
- if (this._options.max !== Infinity) {
1309
- this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1310
- }
1311
- else {
1312
- this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1313
- }
1314
- this._children = [this._repeatPattern];
1315
- this._repeatPattern.parent = this;
1316
- }
1317
1305
  get type() {
1318
1306
  return this._repeatPattern.type;
1319
1307
  }
@@ -1332,6 +1320,19 @@ class Repeat {
1332
1320
  get isOptional() {
1333
1321
  return this._repeatPattern.isOptional;
1334
1322
  }
1323
+ constructor(name, pattern, options = {}) {
1324
+ this._pattern = pattern;
1325
+ this._parent = null;
1326
+ this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1327
+ if (this._options.max !== Infinity) {
1328
+ this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1329
+ }
1330
+ else {
1331
+ this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1332
+ }
1333
+ this._children = [this._repeatPattern];
1334
+ this._repeatPattern.parent = this;
1335
+ }
1335
1336
  parse(cursor) {
1336
1337
  return this._repeatPattern.parse(cursor);
1337
1338
  }
@@ -1401,20 +1402,6 @@ function filterOutNull(nodes) {
1401
1402
  }
1402
1403
 
1403
1404
  class And {
1404
- constructor(name, sequence, isOptional = false) {
1405
- if (sequence.length === 0) {
1406
- throw new Error("Need at least one pattern with an 'and' pattern.");
1407
- }
1408
- const children = clonePatterns(sequence);
1409
- this._assignChildrenToParent(children);
1410
- this._type = "and";
1411
- this._name = name;
1412
- this._isOptional = isOptional;
1413
- this._parent = null;
1414
- this._children = children;
1415
- this._firstIndex = -1;
1416
- this._nodes = [];
1417
- }
1418
1405
  get type() {
1419
1406
  return this._type;
1420
1407
  }
@@ -1433,6 +1420,20 @@ class And {
1433
1420
  get isOptional() {
1434
1421
  return this._isOptional;
1435
1422
  }
1423
+ constructor(name, sequence, isOptional = false) {
1424
+ if (sequence.length === 0) {
1425
+ throw new Error("Need at least one pattern with an 'and' pattern.");
1426
+ }
1427
+ const children = clonePatterns(sequence);
1428
+ this._assignChildrenToParent(children);
1429
+ this._type = "and";
1430
+ this._name = name;
1431
+ this._isOptional = isOptional;
1432
+ this._parent = null;
1433
+ this._children = children;
1434
+ this._firstIndex = -1;
1435
+ this._nodes = [];
1436
+ }
1436
1437
  _assignChildrenToParent(children) {
1437
1438
  for (const child of children) {
1438
1439
  child.parent = this;
@@ -1749,13 +1750,6 @@ const line = new Or("line", [
1749
1750
  const grammar = new Repeat("grammar", line, { divider: newLine });
1750
1751
 
1751
1752
  class Not {
1752
- constructor(name, pattern) {
1753
- this._type = "not";
1754
- this._name = name;
1755
- this._parent = null;
1756
- this._children = [pattern.clone(pattern.name, false)];
1757
- this._children[0].parent = this;
1758
- }
1759
1753
  get type() {
1760
1754
  return this._type;
1761
1755
  }
@@ -1774,6 +1768,13 @@ class Not {
1774
1768
  get isOptional() {
1775
1769
  return false;
1776
1770
  }
1771
+ constructor(name, pattern) {
1772
+ this._type = "not";
1773
+ this._name = name;
1774
+ this._parent = null;
1775
+ this._children = [pattern.clone(pattern.name, false)];
1776
+ this._children[0].parent = this;
1777
+ }
1777
1778
  test(text) {
1778
1779
  const cursor = new Cursor(text);
1779
1780
  this.parse(cursor);
@@ -2138,10 +2139,10 @@ class Grammar {
2138
2139
  _buildOr(statementNode) {
2139
2140
  const nameNode = statementNode.find(n => n.name === "name");
2140
2141
  const orNode = statementNode.find(n => n.name === "or-literal");
2141
- const patternNodes = orNode.children.filter(n => n.name == "pattern-name");
2142
+ const patternNodes = orNode.children.filter(n => n.name === "pattern-name");
2142
2143
  const name = nameNode.value;
2143
2144
  const patterns = patternNodes.map(n => this._getPattern(n.value));
2144
- const or = new Or(name, patterns);
2145
+ const or = new Or(name, patterns, false, true);
2145
2146
  this._parseContext.patternsByName.set(name, or);
2146
2147
  }
2147
2148
  _getPattern(name) {
@@ -2154,7 +2155,7 @@ class Grammar {
2154
2155
  _buildAnd(statementNode) {
2155
2156
  const nameNode = statementNode.find(n => n.name === "name");
2156
2157
  const andNode = statementNode.find(n => n.name === "and-literal");
2157
- const patternNodes = andNode.children.filter(n => n.name == "pattern");
2158
+ const patternNodes = andNode.children.filter(n => n.name === "pattern");
2158
2159
  const name = nameNode.value;
2159
2160
  const patterns = patternNodes.map(n => {
2160
2161
  const nameNode = n.find(n => n.name === "pattern-name");
@@ -2173,7 +2174,7 @@ class Grammar {
2173
2174
  _buildRepeat(statementNode) {
2174
2175
  const nameNode = statementNode.find(n => n.name === "name");
2175
2176
  const repeatNode = statementNode.find(n => n.name === "repeat-literal");
2176
- const patternNode = repeatNode.find(n => n.name == "pattern");
2177
+ const patternNode = repeatNode.find(n => n.name === "pattern");
2177
2178
  const patternNameNode = patternNode.find(n => n.name === "pattern-name");
2178
2179
  const dividerNode = repeatNode.find(n => n.name === "divider-pattern");
2179
2180
  const bounds = repeatNode.find(n => n.name === "bounds");