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