clarity-pattern-parser 8.1.7 → 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,19 +773,6 @@
773
773
  }
774
774
 
775
775
  class Or {
776
- constructor(name, options, isOptional = 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
- }
789
776
  get type() {
790
777
  return this._type;
791
778
  }
@@ -804,6 +791,20 @@
804
791
  get isOptional() {
805
792
  return this._isOptional;
806
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
+ }
807
808
  _assignChildrenToParent(children) {
808
809
  for (const child of children) {
809
810
  child.parent = this;
@@ -826,6 +827,7 @@
826
827
  this._firstIndex = cursor.index;
827
828
  const node = this._tryToParse(cursor);
828
829
  if (node != null) {
830
+ cursor.moveTo(node.lastIndex);
829
831
  cursor.resolveError();
830
832
  return node;
831
833
  }
@@ -838,15 +840,21 @@
838
840
  return null;
839
841
  }
840
842
  _tryToParse(cursor) {
843
+ const results = [];
841
844
  for (const pattern of this._children) {
842
845
  cursor.moveTo(this._firstIndex);
843
846
  const result = pattern.parse(cursor);
844
- if (!cursor.hasError) {
847
+ if (this._isGreedy) {
848
+ results.push(result);
849
+ }
850
+ if (!cursor.hasError && !this._isGreedy) {
845
851
  return result;
846
852
  }
847
853
  cursor.resolveError();
848
854
  }
849
- return null;
855
+ const nonNullResults = results.filter(r => r != null);
856
+ nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
857
+ return nonNullResults[0] || null;
850
858
  }
851
859
  getTokens() {
852
860
  const tokens = [];
@@ -890,28 +898,12 @@
890
898
  return findPattern(this, predicate);
891
899
  }
892
900
  clone(name = this._name, isOptional = this._isOptional) {
893
- const or = new Or(name, this._children, isOptional);
901
+ const or = new Or(name, this._children, isOptional, this._isGreedy);
894
902
  return or;
895
903
  }
896
904
  }
897
905
 
898
906
  class FiniteRepeat {
899
- constructor(name, pattern, repeatAmount, options = {}) {
900
- this._type = "finite-repeat";
901
- this._name = name;
902
- this._parent = null;
903
- this._children = [];
904
- this._hasDivider = options.divider != null;
905
- this._min = options.min != null ? options.min : 1;
906
- this._max = repeatAmount;
907
- this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
908
- for (let i = 0; i < repeatAmount; i++) {
909
- this._children.push(pattern.clone(pattern.name));
910
- if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
911
- this._children.push(options.divider.clone(options.divider.name, false));
912
- }
913
- }
914
- }
915
907
  get type() {
916
908
  return this._type;
917
909
  }
@@ -936,6 +928,22 @@
936
928
  get max() {
937
929
  return this._max;
938
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
+ }
939
947
  parse(cursor) {
940
948
  const startIndex = cursor.index;
941
949
  const nodes = [];
@@ -1060,6 +1068,27 @@
1060
1068
  }
1061
1069
 
1062
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
+ }
1063
1092
  constructor(name, pattern, options = {}) {
1064
1093
  const min = options.min != null ? options.min : 1;
1065
1094
  const divider = options.divider;
@@ -1082,27 +1111,6 @@
1082
1111
  this._nodes = [];
1083
1112
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1084
1113
  }
1085
- get type() {
1086
- return this._type;
1087
- }
1088
- get name() {
1089
- return this._name;
1090
- }
1091
- get parent() {
1092
- return this._parent;
1093
- }
1094
- set parent(pattern) {
1095
- this._parent = pattern;
1096
- }
1097
- get children() {
1098
- return this._children;
1099
- }
1100
- get isOptional() {
1101
- return this._min === 0;
1102
- }
1103
- get min() {
1104
- return this._min;
1105
- }
1106
1114
  _assignChildrenToParent(children) {
1107
1115
  for (const child of children) {
1108
1116
  child.parent = this;
@@ -1296,19 +1304,6 @@
1296
1304
  }
1297
1305
 
1298
1306
  class Repeat {
1299
- constructor(name, pattern, options = {}) {
1300
- this._pattern = pattern;
1301
- this._parent = null;
1302
- this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1303
- if (this._options.max !== Infinity) {
1304
- this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1305
- }
1306
- else {
1307
- this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
1308
- }
1309
- this._children = [this._repeatPattern];
1310
- this._repeatPattern.parent = this;
1311
- }
1312
1307
  get type() {
1313
1308
  return this._repeatPattern.type;
1314
1309
  }
@@ -1327,6 +1322,19 @@
1327
1322
  get isOptional() {
1328
1323
  return this._repeatPattern.isOptional;
1329
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
+ }
1330
1338
  parse(cursor) {
1331
1339
  return this._repeatPattern.parse(cursor);
1332
1340
  }
@@ -1396,20 +1404,6 @@
1396
1404
  }
1397
1405
 
1398
1406
  class And {
1399
- constructor(name, sequence, isOptional = false) {
1400
- if (sequence.length === 0) {
1401
- throw new Error("Need at least one pattern with an 'and' pattern.");
1402
- }
1403
- const children = clonePatterns(sequence);
1404
- this._assignChildrenToParent(children);
1405
- this._type = "and";
1406
- this._name = name;
1407
- this._isOptional = isOptional;
1408
- this._parent = null;
1409
- this._children = children;
1410
- this._firstIndex = -1;
1411
- this._nodes = [];
1412
- }
1413
1407
  get type() {
1414
1408
  return this._type;
1415
1409
  }
@@ -1428,6 +1422,20 @@
1428
1422
  get isOptional() {
1429
1423
  return this._isOptional;
1430
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
+ }
1431
1439
  _assignChildrenToParent(children) {
1432
1440
  for (const child of children) {
1433
1441
  child.parent = this;
@@ -1744,13 +1752,6 @@
1744
1752
  const grammar = new Repeat("grammar", line, { divider: newLine });
1745
1753
 
1746
1754
  class Not {
1747
- constructor(name, pattern) {
1748
- this._type = "not";
1749
- this._name = name;
1750
- this._parent = null;
1751
- this._children = [pattern.clone(pattern.name, false)];
1752
- this._children[0].parent = this;
1753
- }
1754
1755
  get type() {
1755
1756
  return this._type;
1756
1757
  }
@@ -1769,6 +1770,13 @@
1769
1770
  get isOptional() {
1770
1771
  return false;
1771
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
+ }
1772
1780
  test(text) {
1773
1781
  const cursor = new Cursor(text);
1774
1782
  this.parse(cursor);
@@ -2133,10 +2141,10 @@
2133
2141
  _buildOr(statementNode) {
2134
2142
  const nameNode = statementNode.find(n => n.name === "name");
2135
2143
  const orNode = statementNode.find(n => n.name === "or-literal");
2136
- const patternNodes = orNode.children.filter(n => n.name == "pattern-name");
2144
+ const patternNodes = orNode.children.filter(n => n.name === "pattern-name");
2137
2145
  const name = nameNode.value;
2138
2146
  const patterns = patternNodes.map(n => this._getPattern(n.value));
2139
- const or = new Or(name, patterns);
2147
+ const or = new Or(name, patterns, false, true);
2140
2148
  this._parseContext.patternsByName.set(name, or);
2141
2149
  }
2142
2150
  _getPattern(name) {
@@ -2149,7 +2157,7 @@
2149
2157
  _buildAnd(statementNode) {
2150
2158
  const nameNode = statementNode.find(n => n.name === "name");
2151
2159
  const andNode = statementNode.find(n => n.name === "and-literal");
2152
- const patternNodes = andNode.children.filter(n => n.name == "pattern");
2160
+ const patternNodes = andNode.children.filter(n => n.name === "pattern");
2153
2161
  const name = nameNode.value;
2154
2162
  const patterns = patternNodes.map(n => {
2155
2163
  const nameNode = n.find(n => n.name === "pattern-name");
@@ -2168,7 +2176,7 @@
2168
2176
  _buildRepeat(statementNode) {
2169
2177
  const nameNode = statementNode.find(n => n.name === "name");
2170
2178
  const repeatNode = statementNode.find(n => n.name === "repeat-literal");
2171
- const patternNode = repeatNode.find(n => n.name == "pattern");
2179
+ const patternNode = repeatNode.find(n => n.name === "pattern");
2172
2180
  const patternNameNode = patternNode.find(n => n.name === "pattern-name");
2173
2181
  const dividerNode = repeatNode.find(n => n.name === "divider-pattern");
2174
2182
  const bounds = repeatNode.find(n => n.name === "bounds");
@@ -2247,5 +2255,5 @@
2247
2255
 
2248
2256
  Object.defineProperty(exports, '__esModule', { value: true });
2249
2257
 
2250
- })));
2258
+ }));
2251
2259
  //# sourceMappingURL=index.browser.js.map