clarity-pattern-parser 9.0.0 → 9.2.0

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
@@ -471,7 +471,7 @@ class Cursor {
471
471
  }
472
472
  }
473
473
 
474
- let idIndex$8 = 0;
474
+ let idIndex$9 = 0;
475
475
  class Literal {
476
476
  get id() {
477
477
  return this._id;
@@ -482,6 +482,9 @@ class Literal {
482
482
  get name() {
483
483
  return this._name;
484
484
  }
485
+ get value() {
486
+ return this._text;
487
+ }
485
488
  get parent() {
486
489
  return this._parent;
487
490
  }
@@ -491,19 +494,15 @@ class Literal {
491
494
  get children() {
492
495
  return [];
493
496
  }
494
- get isOptional() {
495
- return this._isOptional;
496
- }
497
- constructor(name, value, isOptional = false) {
497
+ constructor(name, value) {
498
498
  if (value.length === 0) {
499
499
  throw new Error("Value Cannot be empty.");
500
500
  }
501
- this._id = `literal-${idIndex$8++}`;
501
+ this._id = `literal-${idIndex$9++}`;
502
502
  this._type = "literal";
503
503
  this._name = name;
504
504
  this._text = value;
505
505
  this._runes = Array.from(value);
506
- this._isOptional = isOptional;
507
506
  this._parent = null;
508
507
  this._firstIndex = 0;
509
508
  this._lastIndex = 0;
@@ -534,13 +533,7 @@ class Literal {
534
533
  cursor.endParse();
535
534
  return node;
536
535
  }
537
- if (!this._isOptional) {
538
- cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
539
- cursor.endParse();
540
- return null;
541
- }
542
- cursor.resolveError();
543
- cursor.moveTo(this._firstIndex);
536
+ cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
544
537
  cursor.endParse();
545
538
  return null;
546
539
  }
@@ -570,8 +563,8 @@ class Literal {
570
563
  _createNode() {
571
564
  return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._text);
572
565
  }
573
- clone(name = this._name, isOptional = this._isOptional) {
574
- const clone = new Literal(name, this._text, isOptional);
566
+ clone(name = this._name) {
567
+ const clone = new Literal(name, this._text);
575
568
  clone._id = this._id;
576
569
  return clone;
577
570
  }
@@ -607,7 +600,7 @@ class Literal {
607
600
  }
608
601
  }
609
602
 
610
- let idIndex$7 = 0;
603
+ let idIndex$8 = 0;
611
604
  class Regex {
612
605
  get id() {
613
606
  return this._id;
@@ -618,6 +611,9 @@ class Regex {
618
611
  get name() {
619
612
  return this._name;
620
613
  }
614
+ get value() {
615
+ return this._originalRegexString;
616
+ }
621
617
  get parent() {
622
618
  return this._parent;
623
619
  }
@@ -627,19 +623,15 @@ class Regex {
627
623
  get children() {
628
624
  return [];
629
625
  }
630
- get isOptional() {
631
- return this._isOptional;
632
- }
633
- constructor(name, regex, isOptional = false) {
626
+ constructor(name, regex) {
634
627
  this._node = null;
635
628
  this._cursor = null;
636
629
  this._firstIndex = -1;
637
630
  this._substring = "";
638
631
  this._tokens = [];
639
- this._id = `regex-${idIndex$7++}`;
632
+ this._id = `regex-${idIndex$8++}`;
640
633
  this._type = "regex";
641
634
  this._name = name;
642
- this._isOptional = isOptional;
643
635
  this._parent = null;
644
636
  this._originalRegexString = regex;
645
637
  this._regex = new RegExp(`^${regex}`, "g");
@@ -701,13 +693,11 @@ class Regex {
701
693
  cursor.recordMatch(this, this._node);
702
694
  }
703
695
  processError(cursor) {
704
- if (!this._isOptional) {
705
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
706
- }
696
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
707
697
  this._node = null;
708
698
  }
709
- clone(name = this._name, isOptional = this._isOptional) {
710
- const clone = new Regex(name, this._originalRegexString, isOptional);
699
+ clone(name = this._name) {
700
+ const clone = new Regex(name, this._originalRegexString);
711
701
  clone._tokens = this._tokens.slice();
712
702
  clone._id = this._id;
713
703
  return clone;
@@ -769,7 +759,7 @@ function findPattern(pattern, predicate) {
769
759
  }
770
760
  }
771
761
 
772
- let idIndex$6 = 0;
762
+ let idIndex$7 = 0;
773
763
  class Reference {
774
764
  get id() {
775
765
  return this._id;
@@ -789,15 +779,11 @@ class Reference {
789
779
  get children() {
790
780
  return this._children;
791
781
  }
792
- get isOptional() {
793
- return this._isOptional;
794
- }
795
- constructor(name, isOptional = false) {
796
- this._id = `reference-${idIndex$6++}`;
782
+ constructor(name) {
783
+ this._id = `reference-${idIndex$7++}`;
797
784
  this._type = "reference";
798
785
  this._name = name;
799
786
  this._parent = null;
800
- this._isOptional = isOptional;
801
787
  this._pattern = null;
802
788
  this._children = [];
803
789
  }
@@ -824,7 +810,7 @@ class Reference {
824
810
  if (pattern === null) {
825
811
  throw new Error(`Couldn't find '${this._name}' pattern within tree.`);
826
812
  }
827
- const clonedPattern = pattern.clone(this._name, this._isOptional);
813
+ const clonedPattern = pattern.clone();
828
814
  clonedPattern.parent = this;
829
815
  this._pattern = clonedPattern;
830
816
  this._children = [this._pattern];
@@ -883,8 +869,8 @@ class Reference {
883
869
  find(_predicate) {
884
870
  return null;
885
871
  }
886
- clone(name = this._name, isOptional = this._isOptional) {
887
- const clone = new Reference(name, isOptional);
872
+ clone(name = this._name) {
873
+ const clone = new Reference(name);
888
874
  clone._id = this._id;
889
875
  return clone;
890
876
  }
@@ -893,12 +879,12 @@ class Reference {
893
879
  }
894
880
  }
895
881
 
896
- function clonePatterns(patterns, isOptional) {
897
- return patterns.map(p => p.clone(p.name, isOptional));
882
+ function clonePatterns(patterns) {
883
+ return patterns.map(p => p.clone());
898
884
  }
899
885
 
900
- let idIndex$5 = 0;
901
- class Or {
886
+ let idIndex$6 = 0;
887
+ class Options {
902
888
  get id() {
903
889
  return this._id;
904
890
  }
@@ -917,21 +903,17 @@ class Or {
917
903
  get children() {
918
904
  return this._children;
919
905
  }
920
- get isOptional() {
921
- return this._isOptional;
922
- }
923
- constructor(name, options, isOptional = false, isGreedy = false) {
906
+ constructor(name, options, isGreedy = false) {
924
907
  if (options.length === 0) {
925
908
  throw new Error("Need at least one pattern with an 'or' pattern.");
926
909
  }
927
- const children = clonePatterns(options, false);
910
+ const children = clonePatterns(options);
928
911
  this._assignChildrenToParent(children);
929
- this._id = `or-${idIndex$5++}`;
912
+ this._id = `or-${idIndex$6++}`;
930
913
  this._type = "or";
931
914
  this._name = name;
932
915
  this._parent = null;
933
916
  this._children = children;
934
- this._isOptional = isOptional;
935
917
  this._firstIndex = 0;
936
918
  this._isGreedy = isGreedy;
937
919
  }
@@ -964,13 +946,7 @@ class Or {
964
946
  cursor.endParse();
965
947
  return node;
966
948
  }
967
- if (!this._isOptional) {
968
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
969
- cursor.endParse();
970
- return null;
971
- }
972
- cursor.resolveError();
973
- cursor.moveTo(this._firstIndex);
949
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
974
950
  cursor.endParse();
975
951
  return null;
976
952
  }
@@ -982,7 +958,7 @@ class Or {
982
958
  if (this._isGreedy) {
983
959
  results.push(result);
984
960
  }
985
- if (!cursor.hasError && !this._isGreedy) {
961
+ if (result != null && !this._isGreedy) {
986
962
  return result;
987
963
  }
988
964
  cursor.resolveError();
@@ -1032,8 +1008,8 @@ class Or {
1032
1008
  find(predicate) {
1033
1009
  return findPattern(this, predicate);
1034
1010
  }
1035
- clone(name = this._name, isOptional = this._isOptional) {
1036
- const or = new Or(name, this._children, isOptional, this._isGreedy);
1011
+ clone(name = this._name) {
1012
+ const or = new Options(name, this._children, this._isGreedy);
1037
1013
  or._id = this._id;
1038
1014
  return or;
1039
1015
  }
@@ -1042,7 +1018,7 @@ class Or {
1042
1018
  }
1043
1019
  }
1044
1020
 
1045
- let idIndex$4 = 0;
1021
+ let idIndex$5 = 0;
1046
1022
  class FiniteRepeat {
1047
1023
  get id() {
1048
1024
  return this._id;
@@ -1062,29 +1038,30 @@ class FiniteRepeat {
1062
1038
  get children() {
1063
1039
  return this._children;
1064
1040
  }
1065
- get isOptional() {
1066
- return this._min === 0;
1067
- }
1068
1041
  get min() {
1069
1042
  return this._min;
1070
1043
  }
1071
1044
  get max() {
1072
1045
  return this._max;
1073
1046
  }
1074
- constructor(name, pattern, repeatAmount, options = {}) {
1075
- this._id = `finite-repeat-${idIndex$4++}`;
1047
+ constructor(name, pattern, options = {}) {
1048
+ this._id = `finite-repeat-${idIndex$5++}`;
1076
1049
  this._type = "finite-repeat";
1077
1050
  this._name = name;
1078
1051
  this._parent = null;
1079
1052
  this._children = [];
1080
1053
  this._hasDivider = options.divider != null;
1081
- this._min = options.min != null ? options.min : 1;
1082
- this._max = repeatAmount;
1054
+ this._min = options.min != null ? Math.max(options.min, 1) : 1;
1055
+ this._max = Math.max(this.min, options.max || this.min);
1083
1056
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1084
- for (let i = 0; i < repeatAmount; i++) {
1085
- this._children.push(pattern.clone(pattern.name));
1086
- if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
1087
- this._children.push(options.divider.clone(options.divider.name, false));
1057
+ for (let i = 0; i < this._max; i++) {
1058
+ const child = pattern.clone();
1059
+ child.parent = this;
1060
+ this._children.push(child);
1061
+ if (options.divider != null && (i < this._max - 1 || !this._trimDivider)) {
1062
+ const divider = options.divider.clone();
1063
+ divider.parent = this;
1064
+ this._children.push(divider);
1088
1065
  }
1089
1066
  }
1090
1067
  }
@@ -1096,15 +1073,18 @@ class FiniteRepeat {
1096
1073
  let matchCount = 0;
1097
1074
  for (let i = 0; i < this._children.length; i++) {
1098
1075
  const childPattern = this._children[i];
1076
+ const runningIndex = cursor.index;
1099
1077
  const node = childPattern.parse(cursor);
1078
+ if (cursor.hasError) {
1079
+ break;
1080
+ }
1100
1081
  if (i % modulo === 0 && !cursor.hasError) {
1101
1082
  matchCount++;
1102
1083
  }
1103
- if (cursor.hasError) {
1104
- cursor.resolveError();
1105
- break;
1084
+ if (node == null) {
1085
+ cursor.moveTo(runningIndex);
1106
1086
  }
1107
- if (node != null) {
1087
+ else {
1108
1088
  nodes.push(node);
1109
1089
  if (cursor.hasNext()) {
1110
1090
  cursor.next();
@@ -1115,7 +1095,8 @@ class FiniteRepeat {
1115
1095
  }
1116
1096
  }
1117
1097
  if (this._trimDivider && this._hasDivider) {
1118
- if (cursor.leafMatch.pattern === this.children[1]) {
1098
+ const isDividerLastMatch = cursor.leafMatch.pattern === this.children[1];
1099
+ if (isDividerLastMatch) {
1119
1100
  const node = nodes.pop();
1120
1101
  cursor.moveTo(node.firstIndex);
1121
1102
  }
@@ -1127,14 +1108,14 @@ class FiniteRepeat {
1127
1108
  cursor.endParse();
1128
1109
  return null;
1129
1110
  }
1130
- else if (nodes.length === 0) {
1131
- cursor.resolveError();
1111
+ if (nodes.length === 0 && !cursor.hasError) {
1132
1112
  cursor.moveTo(startIndex);
1133
1113
  cursor.endParse();
1134
1114
  return null;
1135
1115
  }
1136
1116
  const firstIndex = nodes[0].firstIndex;
1137
1117
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1118
+ cursor.resolveError();
1138
1119
  cursor.moveTo(lastIndex);
1139
1120
  cursor.endParse();
1140
1121
  return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
@@ -1153,19 +1134,13 @@ class FiniteRepeat {
1153
1134
  cursor
1154
1135
  };
1155
1136
  }
1156
- clone(name = this._name, isOptional) {
1137
+ clone(name = this._name) {
1157
1138
  let min = this._min;
1158
- if (isOptional != null) {
1159
- if (isOptional) {
1160
- min = 0;
1161
- }
1162
- else {
1163
- min = Math.max(this._min, 1);
1164
- }
1165
- }
1166
- const clone = new FiniteRepeat(name, this._children[0], this._max, {
1139
+ let max = this._max;
1140
+ const clone = new FiniteRepeat(name, this._children[0], {
1167
1141
  divider: this._hasDivider ? this._children[1] : undefined,
1168
1142
  min,
1143
+ max,
1169
1144
  trimDivider: this._trimDivider
1170
1145
  });
1171
1146
  clone._id = this._id;
@@ -1222,7 +1197,7 @@ class FiniteRepeat {
1222
1197
  }
1223
1198
  }
1224
1199
 
1225
- let idIndex$3 = 0;
1200
+ let idIndex$4 = 0;
1226
1201
  class InfiniteRepeat {
1227
1202
  get id() {
1228
1203
  return this._id;
@@ -1242,24 +1217,21 @@ class InfiniteRepeat {
1242
1217
  get children() {
1243
1218
  return this._children;
1244
1219
  }
1245
- get isOptional() {
1246
- return this._min === 0;
1247
- }
1248
1220
  get min() {
1249
1221
  return this._min;
1250
1222
  }
1251
1223
  constructor(name, pattern, options = {}) {
1252
- const min = options.min != null ? options.min : 1;
1224
+ const min = options.min != null ? Math.max(options.min, 1) : 1;
1253
1225
  const divider = options.divider;
1254
1226
  let children;
1255
1227
  if (divider != null) {
1256
- children = [pattern.clone(pattern.name, false), divider.clone(divider.name, false)];
1228
+ children = [pattern.clone(), divider.clone()];
1257
1229
  }
1258
1230
  else {
1259
- children = [pattern.clone(pattern.name, false)];
1231
+ children = [pattern.clone()];
1260
1232
  }
1261
1233
  this._assignChildrenToParent(children);
1262
- this._id = `infinite-repeat-${idIndex$3++}`;
1234
+ this._id = `infinite-repeat-${idIndex$4++}`;
1263
1235
  this._type = "infinite-repeat";
1264
1236
  this._name = name;
1265
1237
  this._min = min;
@@ -1324,8 +1296,12 @@ class InfiniteRepeat {
1324
1296
  let passed = false;
1325
1297
  while (true) {
1326
1298
  const runningCursorIndex = cursor.index;
1327
- const repeatedNode = this._pattern.parse(cursor);
1328
- if (cursor.hasError) {
1299
+ const repeatNode = this._pattern.parse(cursor);
1300
+ const hasError = cursor.hasError;
1301
+ const hasNoErrorAndNoResult = !cursor.hasError && repeatNode == null;
1302
+ const hasDivider = this._divider != null;
1303
+ const hasNoDivider = !hasDivider;
1304
+ if (hasError) {
1329
1305
  const lastValidNode = this._getLastValidNode();
1330
1306
  if (lastValidNode != null) {
1331
1307
  passed = true;
@@ -1338,8 +1314,12 @@ class InfiniteRepeat {
1338
1314
  break;
1339
1315
  }
1340
1316
  else {
1341
- if (repeatedNode != null) {
1342
- this._nodes.push(repeatedNode);
1317
+ if (hasNoErrorAndNoResult && hasNoDivider) {
1318
+ // If we didn't match and didn't error we need to get out. Nothing different will happen.
1319
+ break;
1320
+ }
1321
+ if (repeatNode != null) {
1322
+ this._nodes.push(repeatNode);
1343
1323
  if (!cursor.hasNext()) {
1344
1324
  passed = true;
1345
1325
  break;
@@ -1347,18 +1327,29 @@ class InfiniteRepeat {
1347
1327
  cursor.next();
1348
1328
  }
1349
1329
  if (this._divider != null) {
1330
+ const dividerStartIndex = cursor.index;
1350
1331
  const dividerNode = this._divider.parse(cursor);
1351
1332
  if (cursor.hasError) {
1352
1333
  passed = true;
1353
1334
  break;
1354
1335
  }
1355
- else if (dividerNode != null) {
1356
- this._nodes.push(dividerNode);
1357
- if (!cursor.hasNext()) {
1358
- passed = true;
1359
- break;
1336
+ else {
1337
+ if (dividerNode == null) {
1338
+ cursor.moveTo(dividerStartIndex);
1339
+ if (dividerNode == null && repeatNode == null) {
1340
+ // If neither the repeat pattern or divider pattern matched get out.
1341
+ passed = true;
1342
+ break;
1343
+ }
1344
+ }
1345
+ else {
1346
+ this._nodes.push(dividerNode);
1347
+ if (!cursor.hasNext()) {
1348
+ passed = true;
1349
+ break;
1350
+ }
1351
+ cursor.next();
1360
1352
  }
1361
- cursor.next();
1362
1353
  }
1363
1354
  }
1364
1355
  }
@@ -1382,10 +1373,10 @@ class InfiniteRepeat {
1382
1373
  const dividerNode = this._nodes.pop();
1383
1374
  cursor.moveTo(dividerNode.firstIndex);
1384
1375
  }
1385
- // if (this._nodes.length === 0) {
1386
- // cursor.moveTo(this._firstIndex);
1387
- // return null;
1388
- // }
1376
+ if (this._nodes.length === 0) {
1377
+ cursor.moveTo(this._firstIndex);
1378
+ return null;
1379
+ }
1389
1380
  const lastIndex = this._nodes[this._nodes.length - 1].lastIndex;
1390
1381
  cursor.moveTo(lastIndex);
1391
1382
  return new Node(this._type, this._name, this._firstIndex, lastIndex, this._nodes);
@@ -1454,16 +1445,8 @@ class InfiniteRepeat {
1454
1445
  find(predicate) {
1455
1446
  return findPattern(this, predicate);
1456
1447
  }
1457
- clone(name = this._name, isOptional) {
1448
+ clone(name = this._name) {
1458
1449
  let min = this._min;
1459
- if (isOptional != null) {
1460
- if (isOptional) {
1461
- min = 0;
1462
- }
1463
- else {
1464
- min = Math.max(this._min, 1);
1465
- }
1466
- }
1467
1450
  const clone = new InfiniteRepeat(name, this._pattern, {
1468
1451
  divider: this._divider == null ? undefined : this._divider,
1469
1452
  min: min,
@@ -1477,7 +1460,7 @@ class InfiniteRepeat {
1477
1460
  }
1478
1461
  }
1479
1462
 
1480
- let idIndex$2 = 0;
1463
+ let idIndex$3 = 0;
1481
1464
  class Repeat {
1482
1465
  get id() {
1483
1466
  return this._id;
@@ -1497,16 +1480,19 @@ class Repeat {
1497
1480
  get children() {
1498
1481
  return this._children;
1499
1482
  }
1500
- get isOptional() {
1501
- return this._repeatPattern.isOptional;
1483
+ get min() {
1484
+ return this.children[0].min;
1485
+ }
1486
+ get max() {
1487
+ return this.children[0].max || Infinity;
1502
1488
  }
1503
1489
  constructor(name, pattern, options = {}) {
1504
- this._id = `repeat-${idIndex$2++}`;
1490
+ this._id = `repeat-${idIndex$3++}`;
1505
1491
  this._pattern = pattern;
1506
1492
  this._parent = null;
1507
1493
  this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1508
1494
  if (this._options.max !== Infinity) {
1509
- this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1495
+ this._repeatPattern = new FiniteRepeat(name, pattern, this._options);
1510
1496
  }
1511
1497
  else {
1512
1498
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
@@ -1523,16 +1509,8 @@ class Repeat {
1523
1509
  test(text) {
1524
1510
  return this._repeatPattern.test(text);
1525
1511
  }
1526
- clone(name = this.name, isOptional) {
1512
+ clone(name = this.name) {
1527
1513
  let min = this._options.min;
1528
- if (isOptional != null) {
1529
- if (isOptional) {
1530
- min = 0;
1531
- }
1532
- else {
1533
- min = Math.max(this._options.min, 1);
1534
- }
1535
- }
1536
1514
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1537
1515
  clone._id = this._id;
1538
1516
  return clone;
@@ -1576,6 +1554,7 @@ class Repeat {
1576
1554
  }
1577
1555
 
1578
1556
  const comment = new Regex("comment", "#[^\r\n]+");
1557
+ comment.setTokens(["# "]);
1579
1558
 
1580
1559
  function filterOutNull(nodes) {
1581
1560
  const filteredNodes = [];
@@ -1587,8 +1566,8 @@ function filterOutNull(nodes) {
1587
1566
  return filteredNodes;
1588
1567
  }
1589
1568
 
1590
- let idIndex$1 = 0;
1591
- class And {
1569
+ let idIndex$2 = 0;
1570
+ class Sequence {
1592
1571
  get id() {
1593
1572
  return this._id;
1594
1573
  }
@@ -1607,19 +1586,15 @@ class And {
1607
1586
  get children() {
1608
1587
  return this._children;
1609
1588
  }
1610
- get isOptional() {
1611
- return this._isOptional;
1612
- }
1613
- constructor(name, sequence, isOptional = false) {
1589
+ constructor(name, sequence) {
1614
1590
  if (sequence.length === 0) {
1615
- throw new Error("Need at least one pattern with an 'and' pattern.");
1591
+ throw new Error("Need at least one pattern with a 'sequence' pattern.");
1616
1592
  }
1617
1593
  const children = clonePatterns(sequence);
1618
1594
  this._assignChildrenToParent(children);
1619
- this._id = `and-${idIndex$1++}`;
1620
- this._type = "and";
1595
+ this._id = `sequence-${idIndex$2++}`;
1596
+ this._type = "sequence";
1621
1597
  this._name = name;
1622
- this._isOptional = isOptional;
1623
1598
  this._parent = null;
1624
1599
  this._children = children;
1625
1600
  this._firstIndex = -1;
@@ -1657,9 +1632,6 @@ class And {
1657
1632
  cursor.endParse();
1658
1633
  return node;
1659
1634
  }
1660
- if (this._isOptional) {
1661
- cursor.resolveError();
1662
- }
1663
1635
  cursor.endParse();
1664
1636
  return null;
1665
1637
  }
@@ -1732,7 +1704,7 @@ class And {
1732
1704
  const length = this._children.length;
1733
1705
  for (let i = startOnIndex; i < length; i++) {
1734
1706
  const pattern = this._children[i];
1735
- if (!pattern.isOptional) {
1707
+ if (pattern.type !== "optional") {
1736
1708
  return false;
1737
1709
  }
1738
1710
  }
@@ -1742,13 +1714,13 @@ class And {
1742
1714
  const children = filterOutNull(this._nodes);
1743
1715
  const lastIndex = children[children.length - 1].lastIndex;
1744
1716
  cursor.moveTo(lastIndex);
1745
- return new Node("and", this._name, this._firstIndex, lastIndex, children);
1717
+ return new Node("sequence", this._name, this._firstIndex, lastIndex, children);
1746
1718
  }
1747
1719
  getTokens() {
1748
1720
  const tokens = [];
1749
1721
  for (const child of this._children) {
1750
1722
  tokens.push(...child.getTokens());
1751
- if (!child.isOptional) {
1723
+ if (child.type !== "optional") {
1752
1724
  break;
1753
1725
  }
1754
1726
  }
@@ -1768,9 +1740,9 @@ class And {
1768
1740
  }
1769
1741
  getPatterns() {
1770
1742
  const patterns = [];
1771
- for (const pattern of this._children) {
1772
- patterns.push(...pattern.getPatterns());
1773
- if (!pattern.isOptional) {
1743
+ for (const child of this._children) {
1744
+ patterns.push(...child.getPatterns());
1745
+ if (child.type !== "optional") {
1774
1746
  break;
1775
1747
  }
1776
1748
  }
@@ -1799,7 +1771,7 @@ class And {
1799
1771
  for (let i = nextSiblingIndex; i < this._children.length; i++) {
1800
1772
  const child = this._children[i];
1801
1773
  patterns.push(child);
1802
- if (!child.isOptional) {
1774
+ if (child.type !== "optional") {
1803
1775
  break;
1804
1776
  }
1805
1777
  // If we are on the last child and its options then ask for the next pattern from the parent.
@@ -1818,8 +1790,8 @@ class And {
1818
1790
  find(predicate) {
1819
1791
  return findPattern(this, predicate);
1820
1792
  }
1821
- clone(name = this._name, isOptional = this._isOptional) {
1822
- const clone = new And(name, this._children, isOptional);
1793
+ clone(name = this._name) {
1794
+ const clone = new Sequence(name, this._children);
1823
1795
  clone._id = this._id;
1824
1796
  return clone;
1825
1797
  }
@@ -1829,77 +1801,183 @@ class And {
1829
1801
  }
1830
1802
 
1831
1803
  const literal = new Regex("literal", '"(?:\\\\.|[^"\\\\])*"');
1804
+ literal.setTokens(["[LITERAL]"]);
1832
1805
 
1833
1806
  const tabs$1 = new Regex("tabs", "\\t+");
1807
+ tabs$1.setTokens(["\t"]);
1834
1808
  const spaces$1 = new Regex("spaces", "[ ]+");
1835
- const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1836
1809
  spaces$1.setTokens([" "]);
1837
- tabs$1.setTokens(["\t"]);
1810
+ const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1838
1811
  newLine$1.setTokens(["\n"]);
1839
- const lineSpaces$1 = new Repeat("line-spaces", new Or("line-space", [tabs$1, spaces$1]));
1840
- const allSpaces = new Regex("all-spaces", "\\s+", true);
1812
+ const lineSpaces$1 = new Repeat("line-spaces", new Options("line-space", [tabs$1, spaces$1]));
1813
+ const allSpaces = new Regex("all-spaces", "\\s+");
1814
+ allSpaces.setTokens([" "]);
1841
1815
 
1842
1816
  const name$1 = new Regex("name", "[a-zA-Z_-]+[a-zA-Z0-9_-]*");
1843
1817
 
1844
1818
  const regexLiteral = new Regex("regex-literal", "/(\\\\/|[^/\\n\\r])*/");
1819
+ regexLiteral.setTokens(["[REGEX_EXPRESSION]"]);
1845
1820
 
1846
1821
  const patternName$3 = name$1.clone("pattern-name");
1847
- const anonymousLiterals = new Or("anonymous-literals", [
1822
+ const anonymousLiterals = new Options("anonymous-literals", [
1848
1823
  literal,
1849
1824
  regexLiteral,
1850
1825
  patternName$3,
1851
1826
  new Reference("repeat-literal"),
1852
1827
  ]);
1853
- const anonymousWrappedLiterals = new Or("anonymous-wrapped-literals", [
1854
- new Reference("or-literal"),
1855
- new Reference("and-literal"),
1828
+ const anonymousWrappedLiterals = new Options("anonymous-wrapped-literals", [
1829
+ new Reference("options-literal"),
1830
+ new Reference("sequence-literal"),
1856
1831
  new Reference("complex-anonymous-pattern")
1857
1832
  ]);
1858
1833
 
1834
+ let idIndex$1 = 0;
1835
+ class Optional {
1836
+ get id() {
1837
+ return this._id;
1838
+ }
1839
+ get type() {
1840
+ return this._type;
1841
+ }
1842
+ get name() {
1843
+ return this._name;
1844
+ }
1845
+ get parent() {
1846
+ return this._parent;
1847
+ }
1848
+ set parent(pattern) {
1849
+ this._parent = pattern;
1850
+ }
1851
+ get children() {
1852
+ return this._children;
1853
+ }
1854
+ constructor(name, pattern) {
1855
+ this._id = `optional-${idIndex$1++}`;
1856
+ this._type = "optional";
1857
+ this._name = name;
1858
+ this._parent = null;
1859
+ this._children = [pattern.clone()];
1860
+ this._children[0].parent = this;
1861
+ }
1862
+ test(text) {
1863
+ const cursor = new Cursor(text);
1864
+ this.parse(cursor);
1865
+ return !cursor.hasError;
1866
+ }
1867
+ exec(text, record = false) {
1868
+ const cursor = new Cursor(text);
1869
+ record && cursor.startRecording();
1870
+ const ast = this.parse(cursor);
1871
+ return {
1872
+ ast: (ast === null || ast === void 0 ? void 0 : ast.value) === text ? ast : null,
1873
+ cursor
1874
+ };
1875
+ }
1876
+ parse(cursor) {
1877
+ cursor.startParseWith(this);
1878
+ const firstIndex = cursor.index;
1879
+ const node = this._children[0].parse(cursor);
1880
+ if (cursor.hasError) {
1881
+ cursor.resolveError();
1882
+ cursor.moveTo(firstIndex);
1883
+ cursor.endParse();
1884
+ return null;
1885
+ }
1886
+ else {
1887
+ cursor.endParse();
1888
+ return node;
1889
+ }
1890
+ }
1891
+ clone(name = this._name) {
1892
+ const optional = new Optional(name, this._children[0]);
1893
+ optional._id = this._id;
1894
+ return optional;
1895
+ }
1896
+ getTokens() {
1897
+ return this._children[0].getTokens();
1898
+ }
1899
+ getTokensAfter(_childReference) {
1900
+ const parent = this._parent;
1901
+ if (parent != null) {
1902
+ return parent.getTokensAfter(this);
1903
+ }
1904
+ return [];
1905
+ }
1906
+ getNextTokens() {
1907
+ if (this.parent == null) {
1908
+ return [];
1909
+ }
1910
+ return this.parent.getTokensAfter(this);
1911
+ }
1912
+ getPatterns() {
1913
+ return this._children[0].getPatterns();
1914
+ }
1915
+ getPatternsAfter(_childReference) {
1916
+ const parent = this._parent;
1917
+ if (parent != null) {
1918
+ return parent.getPatternsAfter(this);
1919
+ }
1920
+ return [];
1921
+ }
1922
+ getNextPatterns() {
1923
+ if (this.parent == null) {
1924
+ return [];
1925
+ }
1926
+ return this.parent.getPatternsAfter(this);
1927
+ }
1928
+ find(predicate) {
1929
+ return predicate(this._children[0]) ? this._children[0] : null;
1930
+ }
1931
+ isEqual(pattern) {
1932
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
1933
+ }
1934
+ }
1935
+
1859
1936
  const inlinePatternOpenParen = new Literal("anonymous-pattern-open-paren", "(");
1860
1937
  const inlinePatternCloseParen = new Literal("anonymous-pattern-close-paren", ")");
1861
- const optionalLineSpaces$1 = lineSpaces$1.clone(undefined, true);
1862
- const complexAnonymousPattern = new And("complex-anonymous-pattern", [
1938
+ const optionalLineSpaces$3 = new Optional("optional-line-spaces", lineSpaces$1);
1939
+ const complexAnonymousPattern = new Sequence("complex-anonymous-pattern", [
1863
1940
  inlinePatternOpenParen,
1864
- optionalLineSpaces$1,
1941
+ optionalLineSpaces$3,
1865
1942
  anonymousWrappedLiterals,
1866
- optionalLineSpaces$1,
1943
+ optionalLineSpaces$3,
1867
1944
  inlinePatternCloseParen,
1868
1945
  ]);
1869
- const anonymousPattern = new Or("anonymous-pattern", [
1946
+ const anonymousPattern = new Options("anonymous-pattern", [
1870
1947
  anonymousLiterals,
1871
1948
  complexAnonymousPattern
1872
1949
  ]);
1873
1950
 
1874
- const optionalSpaces$2 = spaces$1.clone("optional-spaces", true);
1951
+ const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1875
1952
  const openBracket$1 = new Literal("open-bracket", "{");
1876
1953
  const closeBracket$1 = new Literal("close-bracket", "}");
1877
1954
  const comma = new Literal("comma", ",");
1878
1955
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1879
1956
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
1880
- const optionalInteger = integer.clone("integer", true);
1881
- const trimKeyword = new Literal("trim-keyword", "trim", true);
1882
- const trimFlag = new And("trim-flag", [lineSpaces$1, trimKeyword], true);
1883
- const bounds = new And("bounds", [
1957
+ const min = new Optional("optional-min", integer.clone("min"));
1958
+ const max = new Optional("optional-max", integer.clone("max"));
1959
+ const trimKeyword = new Literal("trim-keyword", "trim");
1960
+ const trimFlag = new Optional("optional-trim-flag", new Sequence("trim-flag", [lineSpaces$1, trimKeyword]));
1961
+ const bounds = new Sequence("bounds", [
1884
1962
  openBracket$1,
1885
- optionalSpaces$2,
1886
- optionalInteger.clone("min"),
1887
- optionalSpaces$2,
1963
+ optionalSpaces$3,
1964
+ min,
1965
+ optionalSpaces$3,
1888
1966
  comma,
1889
- optionalSpaces$2,
1890
- optionalInteger.clone("max"),
1967
+ optionalSpaces$3,
1968
+ max,
1891
1969
  closeBracket$1
1892
1970
  ]);
1893
- const exactCount = new And("exact-count", [
1971
+ const exactCount = new Sequence("exact-count", [
1894
1972
  openBracket$1,
1895
- optionalSpaces$2,
1973
+ optionalSpaces$3,
1896
1974
  integer,
1897
- optionalSpaces$2,
1975
+ optionalSpaces$3,
1898
1976
  closeBracket$1,
1899
1977
  ]);
1900
1978
  const quantifierShorthand = new Regex("quantifier-shorthand", "\\*|\\+");
1901
1979
  quantifierShorthand.setTokens(["*", "+"]);
1902
- const quantifier = new Or("quantifier", [
1980
+ const quantifier = new Options("quantifier", [
1903
1981
  quantifierShorthand,
1904
1982
  exactCount,
1905
1983
  bounds
@@ -1909,127 +1987,134 @@ const closeParen = new Literal("repeat-close-paren", ")");
1909
1987
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
1910
1988
  dividerComma.setTokens([", "]);
1911
1989
  const patternName$2 = name$1.clone("pattern-name");
1912
- const patterns$3 = new Or("or-patterns", [patternName$2, anonymousPattern]);
1990
+ const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
1913
1991
  const dividerPattern = patterns$3.clone("divider-pattern");
1914
- const repeatLiteral = new And("repeat-literal", [
1992
+ const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
1993
+ const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
1994
+ const repeatLiteral = new Sequence("repeat-literal", [
1915
1995
  openParen,
1916
- optionalSpaces$2,
1996
+ optionalSpaces$3,
1917
1997
  patterns$3,
1918
- new And("optional-divider-section", [dividerComma, dividerPattern, trimFlag], true),
1919
- optionalSpaces$2,
1998
+ optionalDividerSection,
1999
+ optionalSpaces$3,
1920
2000
  closeParen,
1921
- new And("quantifier-section", [quantifier]),
2001
+ new Sequence("quantifier-section", [quantifier]),
1922
2002
  ]);
1923
2003
 
1924
- const optionalNot = new Literal("not", "!", true);
1925
- const optionalIsOptional$1 = new Literal("is-optional", "?", true);
2004
+ const optionalNot = new Optional("optional-not", new Literal("not", "!"));
2005
+ const optionalIsOptional$1 = new Optional("optional-is-optional", new Literal("is-optional", "?"));
1926
2006
  const patternName$1 = name$1.clone("pattern-name");
1927
- const patterns$2 = new Or("and-patterns", [patternName$1, anonymousPattern]);
1928
- const pattern$1 = new And("and-child-pattern", [
2007
+ const patterns$2 = new Options("and-patterns", [patternName$1, anonymousPattern]);
2008
+ const pattern$1 = new Sequence("and-child-pattern", [
1929
2009
  optionalNot,
1930
2010
  patterns$2,
1931
2011
  optionalIsOptional$1,
1932
2012
  ]);
1933
2013
  const divider$1 = new Regex("and-divider", "\\s*[+]\\s*");
1934
2014
  divider$1.setTokens([" + "]);
1935
- const andLiteral = new Repeat("and-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
2015
+ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
1936
2016
 
1937
2017
  const patternName = name$1.clone("pattern-name");
1938
- const patterns$1 = new Or("or-patterns", [patternName, anonymousPattern]);
2018
+ patternName.setTokens(["[PATTERN_NAME]"]);
2019
+ const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
1939
2020
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2021
+ defaultDivider.setTokens(["|"]);
1940
2022
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
1941
- const divider = new Or("or-divider", [defaultDivider, greedyDivider]);
1942
- defaultDivider.setTokens([" | "]);
1943
- greedyDivider.setTokens([" <|> "]);
1944
- const orLiteral = new Repeat("or-literal", patterns$1, { divider, min: 2, trimDivider: true });
2023
+ greedyDivider.setTokens(["<|>"]);
2024
+ const divider = new Options("options-divider", [defaultDivider, greedyDivider]);
2025
+ const optionsLiteral = new Repeat("options-literal", patterns$1, { divider, min: 2, trimDivider: true });
1945
2026
 
1946
2027
  const aliasLiteral = name$1.clone("alias-literal");
1947
- const optionalIsOptional = new Literal("is-optional", "?", true);
1948
- const configurableAnonymousPattern = new And("configurable-anonymous-pattern", [anonymousPattern, optionalIsOptional]);
1949
- const pattern = new Or("pattern", [
2028
+ aliasLiteral.setTokens(["[ALIAS_LITERAL]"]);
2029
+ const optionalIsOptional = new Optional("optional-flag", new Literal("is-optional", "?"));
2030
+ const configurableAnonymousPattern = new Sequence("configurable-anonymous-pattern", [anonymousPattern, optionalIsOptional]);
2031
+ const pattern = new Options("pattern", [
1950
2032
  literal,
1951
2033
  regexLiteral,
1952
2034
  repeatLiteral,
1953
2035
  aliasLiteral,
1954
- orLiteral,
1955
- andLiteral,
2036
+ optionsLiteral,
2037
+ sequenceLiteral,
1956
2038
  configurableAnonymousPattern,
1957
- ], false, true);
2039
+ ], true);
1958
2040
 
1959
- const optionalSpaces$1 = spaces$1.clone("optional-spaces", true);
2041
+ const optionalSpaces$2 = new Optional("optional-spaces", spaces$1);
1960
2042
  const assignOperator = new Literal("assign-operator", "=");
1961
- const assignStatement = new And("assign-statement", [
1962
- optionalSpaces$1,
2043
+ const assignStatement = new Sequence("assign-statement", [
2044
+ optionalSpaces$2,
1963
2045
  name$1,
1964
- optionalSpaces$1,
2046
+ optionalSpaces$2,
1965
2047
  assignOperator,
1966
- optionalSpaces$1,
2048
+ optionalSpaces$2,
1967
2049
  pattern
1968
2050
  ]);
1969
- const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
2051
+ const statement = new Options("statement", [assignStatement, name$1.clone("export-name")]);
1970
2052
 
1971
- const bodyLineContent = new Or("body-line-content", [
2053
+ const bodyLineContent = new Options("body-line-content", [
1972
2054
  comment,
1973
2055
  statement
1974
2056
  ]);
1975
- const bodyLine = new And("body-line", [
1976
- lineSpaces$1.clone("line-spaces", true),
2057
+ const optionalLineSpaces$2 = new Optional("optional-line-spaces", lineSpaces$1);
2058
+ const bodyLine = new Sequence("body-line", [
2059
+ optionalLineSpaces$2,
1977
2060
  bodyLineContent,
1978
- lineSpaces$1.clone("line-spaces", true),
2061
+ optionalLineSpaces$2,
1979
2062
  ]);
1980
2063
  const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
1981
2064
 
1982
- const optionalSpaces = allSpaces.clone("optional-spaces", true);
1983
- const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
2065
+ const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2066
+ const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
1984
2067
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2068
+ importNameDivider.setTokens([", "]);
2069
+ const name = new Regex("import-name", "[^}\\s,]+");
2070
+ name.setTokens(["[IMPORT_NAME]"]);
1985
2071
  const importKeyword = new Literal("import", "import");
1986
2072
  const useParamsKeyword = new Literal("use-params", "use params");
1987
2073
  const asKeyword = new Literal("as", "as");
1988
2074
  const fromKeyword = new Literal("from", "from");
1989
2075
  const openBracket = new Literal("open-bracket", "{");
1990
2076
  const closeBracket = new Literal("close-bracket", "}");
1991
- const name = new Regex("import-name", "[^}\\s,]+");
1992
2077
  const importNameAlias = name.clone("import-name-alias");
1993
- const importAlias = new And("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
1994
- const importedNames = new Repeat("imported-names", new Or("import-names", [importAlias, name]), { divider: importNameDivider });
2078
+ const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
2079
+ const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
1995
2080
  const paramName = name.clone("param-name");
1996
2081
  const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
1997
2082
  const resource = literal.clone("resource");
1998
- const useParams = new And("import-params", [
2083
+ const useParams = new Sequence("import-params", [
1999
2084
  useParamsKeyword,
2000
- optionalLineSpaces,
2085
+ optionalLineSpaces$1,
2001
2086
  openBracket,
2002
- optionalSpaces,
2087
+ optionalSpaces$1,
2003
2088
  paramNames,
2004
- optionalSpaces,
2089
+ optionalSpaces$1,
2005
2090
  closeBracket
2006
2091
  ]);
2007
2092
  const withParamsKeyword = new Literal("with-params", "with params");
2008
- const withParamsStatement = new And("with-params-statement", [
2093
+ const withParamsStatement = new Optional("optional-with-params-statement", new Sequence("with-params-statement", [
2009
2094
  withParamsKeyword,
2010
- optionalLineSpaces,
2095
+ optionalLineSpaces$1,
2011
2096
  openBracket,
2012
- optionalSpaces,
2097
+ optionalSpaces$1,
2013
2098
  body.clone("with-params-body"),
2014
- optionalSpaces,
2099
+ optionalSpaces$1,
2015
2100
  closeBracket
2016
- ], true);
2017
- const importFromStatement = new And("import-from", [
2101
+ ]));
2102
+ const importFromStatement = new Sequence("import-from", [
2018
2103
  importKeyword,
2019
- optionalLineSpaces,
2104
+ optionalLineSpaces$1,
2020
2105
  openBracket,
2021
- optionalSpaces,
2106
+ optionalSpaces$1,
2022
2107
  importedNames,
2023
- optionalSpaces,
2108
+ optionalSpaces$1,
2024
2109
  closeBracket,
2025
- optionalLineSpaces,
2110
+ optionalLineSpaces$1,
2026
2111
  fromKeyword,
2027
- optionalLineSpaces,
2112
+ optionalLineSpaces$1,
2028
2113
  resource,
2029
- optionalLineSpaces,
2114
+ optionalLineSpaces$1,
2030
2115
  withParamsStatement
2031
2116
  ]);
2032
- const importStatement = new Or("import-statement", [
2117
+ const importStatement = new Options("import-statement", [
2033
2118
  useParams,
2034
2119
  importFromStatement
2035
2120
  ]);
@@ -2040,23 +2125,25 @@ const newLine = new Regex("new-line", "(\\r?\\n)+");
2040
2125
  spaces.setTokens([" "]);
2041
2126
  tabs.setTokens(["\t"]);
2042
2127
  newLine.setTokens(["\n"]);
2043
- const lineSpaces = new Repeat("line-spaces", new Or("line-space", [tabs, spaces]));
2044
- const headLineContent = new Or("head-line-content", [
2128
+ const lineSpaces = new Repeat("line-spaces", new Options("line-space", [tabs, spaces]));
2129
+ const optionalLineSpaces = new Optional("optional-line-spaces", lineSpaces);
2130
+ const headLineContent = new Options("head-line-content", [
2045
2131
  comment,
2046
2132
  importStatement
2047
2133
  ]);
2048
- const headLine = new And("head-line-content", [
2049
- lineSpaces.clone("line-spaces", true),
2134
+ const headLine = new Sequence("head-line-content", [
2135
+ optionalLineSpaces,
2050
2136
  headLineContent,
2051
- lineSpaces.clone("line-spaces", true),
2137
+ optionalLineSpaces,
2052
2138
  ]);
2053
- const head = new Repeat("head", headLine, { divider: newLine, min: 0 });
2054
- const grammar = new And("grammar", [
2055
- allSpaces,
2139
+ const head = new Optional("optional-head", new Repeat("head", headLine, { divider: newLine }));
2140
+ const optionalSpaces = new Optional("optional-spaces", allSpaces);
2141
+ const grammar = new Sequence("grammar", [
2142
+ optionalSpaces,
2056
2143
  head,
2057
- allSpaces,
2144
+ optionalSpaces,
2058
2145
  body,
2059
- allSpaces
2146
+ optionalSpaces
2060
2147
  ]);
2061
2148
 
2062
2149
  let idIndex = 0;
@@ -2087,7 +2174,7 @@ class Not {
2087
2174
  this._type = "not";
2088
2175
  this._name = name;
2089
2176
  this._parent = null;
2090
- this._children = [pattern.clone(pattern.name, false)];
2177
+ this._children = [pattern.clone()];
2091
2178
  this._children[0].parent = this;
2092
2179
  }
2093
2180
  test(text) {
@@ -2375,8 +2462,8 @@ let anonymousIndexId = 0;
2375
2462
  const patternNodes = {
2376
2463
  "literal": true,
2377
2464
  "regex-literal": true,
2378
- "or-literal": true,
2379
- "and-literal": true,
2465
+ "options-literal": true,
2466
+ "sequence-literal": true,
2380
2467
  "repeat-literal": true,
2381
2468
  "alias-literal": true,
2382
2469
  "configurable-anonymous-pattern": true
@@ -2479,12 +2566,12 @@ class Grammar {
2479
2566
  this._saveRegex(n);
2480
2567
  break;
2481
2568
  }
2482
- case "or-literal": {
2483
- this._saveOr(n);
2569
+ case "options-literal": {
2570
+ this._saveOptions(n);
2484
2571
  break;
2485
2572
  }
2486
- case "and-literal": {
2487
- this._saveAnd(n);
2573
+ case "sequence-literal": {
2574
+ this._saveSequence(n);
2488
2575
  break;
2489
2576
  }
2490
2577
  case "repeat-literal": {
@@ -2539,18 +2626,18 @@ class Grammar {
2539
2626
  const value = node.value.slice(1, node.value.length - 1);
2540
2627
  return new Regex(name, value);
2541
2628
  }
2542
- _saveOr(statementNode) {
2629
+ _saveOptions(statementNode) {
2543
2630
  const nameNode = statementNode.find(n => n.name === "name");
2544
2631
  const name = nameNode.value;
2545
- const orNode = statementNode.find(n => n.name === "or-literal");
2546
- const or = this._buildOr(name, orNode);
2547
- this._parseContext.patternsByName.set(name, or);
2632
+ const optionsNode = statementNode.find(n => n.name === "options-literal");
2633
+ const options = this._buildOptions(name, optionsNode);
2634
+ this._parseContext.patternsByName.set(name, options);
2548
2635
  }
2549
- _buildOr(name, node) {
2636
+ _buildOptions(name, node) {
2550
2637
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
2551
2638
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
2552
2639
  const patterns = patternNodes.map(n => this._buildPattern(n));
2553
- const or = new Or(name, patterns, false, isGreedy);
2640
+ const or = new Options(name, patterns, isGreedy);
2554
2641
  return or;
2555
2642
  }
2556
2643
  _buildPattern(node) {
@@ -2569,11 +2656,11 @@ class Grammar {
2569
2656
  case "repeat-literal": {
2570
2657
  return this._buildRepeat(name, node);
2571
2658
  }
2572
- case "or-literal": {
2573
- return this._buildOr(name, node);
2659
+ case "options-literal": {
2660
+ return this._buildOptions(name, node);
2574
2661
  }
2575
- case "and-literal": {
2576
- return this._buildAnd(name, node);
2662
+ case "sequence-literal": {
2663
+ return this._buildSequence(name, node);
2577
2664
  }
2578
2665
  case "complex-anonymous-pattern": {
2579
2666
  return this._buildComplexAnonymousPattern(node);
@@ -2581,26 +2668,27 @@ class Grammar {
2581
2668
  }
2582
2669
  throw new Error(`Couldn't build node: ${node.name}.`);
2583
2670
  }
2584
- _saveAnd(statementNode) {
2671
+ _saveSequence(statementNode) {
2585
2672
  const nameNode = statementNode.find(n => n.name === "name");
2586
2673
  const name = nameNode.value;
2587
- const andNode = statementNode.find(n => n.name === "and-literal");
2588
- const and = this._buildAnd(name, andNode);
2589
- this._parseContext.patternsByName.set(name, and);
2674
+ const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
2675
+ const sequence = this._buildSequence(name, sequenceNode);
2676
+ this._parseContext.patternsByName.set(name, sequence);
2590
2677
  }
2591
- _buildAnd(name, node) {
2678
+ _buildSequence(name, node) {
2592
2679
  const patternNodes = node.children.filter(n => n.name !== "and-divider");
2593
2680
  const patterns = patternNodes.map(n => {
2594
2681
  const patternNode = n.children[0].name === "not" ? n.children[1] : n.children[0];
2595
2682
  const isNot = n.find(n => n.name === "not") != null;
2596
2683
  const isOptional = n.find(n => n.name === "is-optional");
2597
- const pattern = this._buildPattern(patternNode).clone(undefined, isOptional == null ? undefined : true);
2684
+ const pattern = this._buildPattern(patternNode);
2685
+ const finalPattern = isOptional ? new Optional(pattern.name, pattern) : pattern;
2598
2686
  if (isNot) {
2599
- return new Not(`not-${pattern.name}`, pattern);
2687
+ return new Not(`not-${finalPattern.name}`, finalPattern);
2600
2688
  }
2601
- return pattern;
2689
+ return finalPattern;
2602
2690
  });
2603
- return new And(name, patterns);
2691
+ return new Sequence(name, patterns);
2604
2692
  }
2605
2693
  _saveRepeat(statementNode) {
2606
2694
  const nameNode = statementNode.find(n => n.name === "name");
@@ -2610,13 +2698,14 @@ class Grammar {
2610
2698
  this._parseContext.patternsByName.set(name, repeat);
2611
2699
  }
2612
2700
  _buildRepeat(name, repeatNode) {
2701
+ let isOptional = false;
2613
2702
  const bounds = repeatNode.find(n => n.name === "bounds");
2614
2703
  const exactCount = repeatNode.find(n => n.name === "exact-count");
2615
2704
  const quantifier = repeatNode.find(n => n.name === "quantifier-shorthand");
2616
2705
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2617
- const patterNode = repeatNode.children[1].type === "optional-spaces" ? repeatNode.children[2] : repeatNode.children[1];
2706
+ const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2618
2707
  const pattern = this._buildPattern(patterNode);
2619
- const dividerSectionNode = repeatNode.find(n => n.name === "optional-divider-section");
2708
+ const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2620
2709
  const options = {
2621
2710
  min: 1,
2622
2711
  max: Infinity
@@ -2649,18 +2738,17 @@ class Grammar {
2649
2738
  options.max = Infinity;
2650
2739
  }
2651
2740
  else {
2652
- options.min = 0;
2653
- options.max = Infinity;
2741
+ isOptional = true;
2654
2742
  }
2655
2743
  }
2656
- return new Repeat(name, pattern.clone(pattern.name), options);
2744
+ return isOptional ? new Optional(name, new Repeat(name, pattern, options)) : new Repeat(name, pattern, options);
2657
2745
  }
2658
2746
  _saveConfigurableAnonymous(node) {
2659
2747
  const nameNode = node.find(n => n.name === "name");
2660
2748
  const name = nameNode.value;
2661
2749
  const anonymousNode = node.find(n => n.name === "complex-anonymous-pattern");
2662
2750
  const isOptional = node.children[1] != null;
2663
- const anonymous = this._buildPattern(anonymousNode).clone(name, isOptional);
2751
+ const anonymous = isOptional ? new Optional(name, this._buildPattern(anonymousNode)) : this._buildPattern(anonymousNode);
2664
2752
  this._parseContext.patternsByName.set(name, anonymous);
2665
2753
  }
2666
2754
  _buildComplexAnonymousPattern(node) {
@@ -2803,5 +2891,5 @@ function patterns(strings, ...values) {
2803
2891
  return result;
2804
2892
  }
2805
2893
 
2806
- export { And, AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Or, ParseError, Reference, Regex, Repeat, arePatternsEqual, grammar, patterns };
2894
+ export { AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, arePatternsEqual, grammar, patterns };
2807
2895
  //# sourceMappingURL=index.esm.js.map