clarity-pattern-parser 9.0.0 → 9.1.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,13 @@ class Repeat {
1497
1480
  get children() {
1498
1481
  return this._children;
1499
1482
  }
1500
- get isOptional() {
1501
- return this._repeatPattern.isOptional;
1502
- }
1503
1483
  constructor(name, pattern, options = {}) {
1504
- this._id = `repeat-${idIndex$2++}`;
1484
+ this._id = `repeat-${idIndex$3++}`;
1505
1485
  this._pattern = pattern;
1506
1486
  this._parent = null;
1507
1487
  this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1508
1488
  if (this._options.max !== Infinity) {
1509
- this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1489
+ this._repeatPattern = new FiniteRepeat(name, pattern, this._options);
1510
1490
  }
1511
1491
  else {
1512
1492
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
@@ -1523,16 +1503,8 @@ class Repeat {
1523
1503
  test(text) {
1524
1504
  return this._repeatPattern.test(text);
1525
1505
  }
1526
- clone(name = this.name, isOptional) {
1506
+ clone(name = this.name) {
1527
1507
  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
1508
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1537
1509
  clone._id = this._id;
1538
1510
  return clone;
@@ -1576,6 +1548,7 @@ class Repeat {
1576
1548
  }
1577
1549
 
1578
1550
  const comment = new Regex("comment", "#[^\r\n]+");
1551
+ comment.setTokens(["# "]);
1579
1552
 
1580
1553
  function filterOutNull(nodes) {
1581
1554
  const filteredNodes = [];
@@ -1587,8 +1560,8 @@ function filterOutNull(nodes) {
1587
1560
  return filteredNodes;
1588
1561
  }
1589
1562
 
1590
- let idIndex$1 = 0;
1591
- class And {
1563
+ let idIndex$2 = 0;
1564
+ class Sequence {
1592
1565
  get id() {
1593
1566
  return this._id;
1594
1567
  }
@@ -1607,19 +1580,15 @@ class And {
1607
1580
  get children() {
1608
1581
  return this._children;
1609
1582
  }
1610
- get isOptional() {
1611
- return this._isOptional;
1612
- }
1613
- constructor(name, sequence, isOptional = false) {
1583
+ constructor(name, sequence) {
1614
1584
  if (sequence.length === 0) {
1615
- throw new Error("Need at least one pattern with an 'and' pattern.");
1585
+ throw new Error("Need at least one pattern with a 'sequence' pattern.");
1616
1586
  }
1617
1587
  const children = clonePatterns(sequence);
1618
1588
  this._assignChildrenToParent(children);
1619
- this._id = `and-${idIndex$1++}`;
1620
- this._type = "and";
1589
+ this._id = `sequence-${idIndex$2++}`;
1590
+ this._type = "sequence";
1621
1591
  this._name = name;
1622
- this._isOptional = isOptional;
1623
1592
  this._parent = null;
1624
1593
  this._children = children;
1625
1594
  this._firstIndex = -1;
@@ -1657,9 +1626,6 @@ class And {
1657
1626
  cursor.endParse();
1658
1627
  return node;
1659
1628
  }
1660
- if (this._isOptional) {
1661
- cursor.resolveError();
1662
- }
1663
1629
  cursor.endParse();
1664
1630
  return null;
1665
1631
  }
@@ -1732,7 +1698,7 @@ class And {
1732
1698
  const length = this._children.length;
1733
1699
  for (let i = startOnIndex; i < length; i++) {
1734
1700
  const pattern = this._children[i];
1735
- if (!pattern.isOptional) {
1701
+ if (pattern.type !== "optional") {
1736
1702
  return false;
1737
1703
  }
1738
1704
  }
@@ -1742,13 +1708,13 @@ class And {
1742
1708
  const children = filterOutNull(this._nodes);
1743
1709
  const lastIndex = children[children.length - 1].lastIndex;
1744
1710
  cursor.moveTo(lastIndex);
1745
- return new Node("and", this._name, this._firstIndex, lastIndex, children);
1711
+ return new Node("sequence", this._name, this._firstIndex, lastIndex, children);
1746
1712
  }
1747
1713
  getTokens() {
1748
1714
  const tokens = [];
1749
1715
  for (const child of this._children) {
1750
1716
  tokens.push(...child.getTokens());
1751
- if (!child.isOptional) {
1717
+ if (child.type !== "optional") {
1752
1718
  break;
1753
1719
  }
1754
1720
  }
@@ -1768,9 +1734,9 @@ class And {
1768
1734
  }
1769
1735
  getPatterns() {
1770
1736
  const patterns = [];
1771
- for (const pattern of this._children) {
1772
- patterns.push(...pattern.getPatterns());
1773
- if (!pattern.isOptional) {
1737
+ for (const child of this._children) {
1738
+ patterns.push(...child.getPatterns());
1739
+ if (child.type !== "optional") {
1774
1740
  break;
1775
1741
  }
1776
1742
  }
@@ -1799,7 +1765,7 @@ class And {
1799
1765
  for (let i = nextSiblingIndex; i < this._children.length; i++) {
1800
1766
  const child = this._children[i];
1801
1767
  patterns.push(child);
1802
- if (!child.isOptional) {
1768
+ if (child.type !== "optional") {
1803
1769
  break;
1804
1770
  }
1805
1771
  // If we are on the last child and its options then ask for the next pattern from the parent.
@@ -1818,8 +1784,8 @@ class And {
1818
1784
  find(predicate) {
1819
1785
  return findPattern(this, predicate);
1820
1786
  }
1821
- clone(name = this._name, isOptional = this._isOptional) {
1822
- const clone = new And(name, this._children, isOptional);
1787
+ clone(name = this._name) {
1788
+ const clone = new Sequence(name, this._children);
1823
1789
  clone._id = this._id;
1824
1790
  return clone;
1825
1791
  }
@@ -1829,77 +1795,183 @@ class And {
1829
1795
  }
1830
1796
 
1831
1797
  const literal = new Regex("literal", '"(?:\\\\.|[^"\\\\])*"');
1798
+ literal.setTokens(["[LITERAL]"]);
1832
1799
 
1833
1800
  const tabs$1 = new Regex("tabs", "\\t+");
1801
+ tabs$1.setTokens(["\t"]);
1834
1802
  const spaces$1 = new Regex("spaces", "[ ]+");
1835
- const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1836
1803
  spaces$1.setTokens([" "]);
1837
- tabs$1.setTokens(["\t"]);
1804
+ const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1838
1805
  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);
1806
+ const lineSpaces$1 = new Repeat("line-spaces", new Options("line-space", [tabs$1, spaces$1]));
1807
+ const allSpaces = new Regex("all-spaces", "\\s+");
1808
+ allSpaces.setTokens([" "]);
1841
1809
 
1842
1810
  const name$1 = new Regex("name", "[a-zA-Z_-]+[a-zA-Z0-9_-]*");
1843
1811
 
1844
1812
  const regexLiteral = new Regex("regex-literal", "/(\\\\/|[^/\\n\\r])*/");
1813
+ regexLiteral.setTokens(["[REGEX_EXPRESSION]"]);
1845
1814
 
1846
1815
  const patternName$3 = name$1.clone("pattern-name");
1847
- const anonymousLiterals = new Or("anonymous-literals", [
1816
+ const anonymousLiterals = new Options("anonymous-literals", [
1848
1817
  literal,
1849
1818
  regexLiteral,
1850
1819
  patternName$3,
1851
1820
  new Reference("repeat-literal"),
1852
1821
  ]);
1853
- const anonymousWrappedLiterals = new Or("anonymous-wrapped-literals", [
1854
- new Reference("or-literal"),
1855
- new Reference("and-literal"),
1822
+ const anonymousWrappedLiterals = new Options("anonymous-wrapped-literals", [
1823
+ new Reference("options-literal"),
1824
+ new Reference("sequence-literal"),
1856
1825
  new Reference("complex-anonymous-pattern")
1857
1826
  ]);
1858
1827
 
1828
+ let idIndex$1 = 0;
1829
+ class Optional {
1830
+ get id() {
1831
+ return this._id;
1832
+ }
1833
+ get type() {
1834
+ return this._type;
1835
+ }
1836
+ get name() {
1837
+ return this._name;
1838
+ }
1839
+ get parent() {
1840
+ return this._parent;
1841
+ }
1842
+ set parent(pattern) {
1843
+ this._parent = pattern;
1844
+ }
1845
+ get children() {
1846
+ return this._children;
1847
+ }
1848
+ constructor(name, pattern) {
1849
+ this._id = `optional-${idIndex$1++}`;
1850
+ this._type = "optional";
1851
+ this._name = name;
1852
+ this._parent = null;
1853
+ this._children = [pattern.clone()];
1854
+ this._children[0].parent = this;
1855
+ }
1856
+ test(text) {
1857
+ const cursor = new Cursor(text);
1858
+ this.parse(cursor);
1859
+ return !cursor.hasError;
1860
+ }
1861
+ exec(text, record = false) {
1862
+ const cursor = new Cursor(text);
1863
+ record && cursor.startRecording();
1864
+ const ast = this.parse(cursor);
1865
+ return {
1866
+ ast: (ast === null || ast === void 0 ? void 0 : ast.value) === text ? ast : null,
1867
+ cursor
1868
+ };
1869
+ }
1870
+ parse(cursor) {
1871
+ cursor.startParseWith(this);
1872
+ const firstIndex = cursor.index;
1873
+ const node = this._children[0].parse(cursor);
1874
+ if (cursor.hasError) {
1875
+ cursor.resolveError();
1876
+ cursor.moveTo(firstIndex);
1877
+ cursor.endParse();
1878
+ return null;
1879
+ }
1880
+ else {
1881
+ cursor.endParse();
1882
+ return node;
1883
+ }
1884
+ }
1885
+ clone(name = this._name) {
1886
+ const optional = new Optional(name, this._children[0]);
1887
+ optional._id = this._id;
1888
+ return optional;
1889
+ }
1890
+ getTokens() {
1891
+ return this._children[0].getTokens();
1892
+ }
1893
+ getTokensAfter(_childReference) {
1894
+ const parent = this._parent;
1895
+ if (parent != null) {
1896
+ return parent.getTokensAfter(this);
1897
+ }
1898
+ return [];
1899
+ }
1900
+ getNextTokens() {
1901
+ if (this.parent == null) {
1902
+ return [];
1903
+ }
1904
+ return this.parent.getTokensAfter(this);
1905
+ }
1906
+ getPatterns() {
1907
+ return this._children[0].getPatterns();
1908
+ }
1909
+ getPatternsAfter(_childReference) {
1910
+ const parent = this._parent;
1911
+ if (parent != null) {
1912
+ return parent.getPatternsAfter(this);
1913
+ }
1914
+ return [];
1915
+ }
1916
+ getNextPatterns() {
1917
+ if (this.parent == null) {
1918
+ return [];
1919
+ }
1920
+ return this.parent.getPatternsAfter(this);
1921
+ }
1922
+ find(predicate) {
1923
+ return predicate(this._children[0]) ? this._children[0] : null;
1924
+ }
1925
+ isEqual(pattern) {
1926
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
1927
+ }
1928
+ }
1929
+
1859
1930
  const inlinePatternOpenParen = new Literal("anonymous-pattern-open-paren", "(");
1860
1931
  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", [
1932
+ const optionalLineSpaces$3 = new Optional("optional-line-spaces", lineSpaces$1);
1933
+ const complexAnonymousPattern = new Sequence("complex-anonymous-pattern", [
1863
1934
  inlinePatternOpenParen,
1864
- optionalLineSpaces$1,
1935
+ optionalLineSpaces$3,
1865
1936
  anonymousWrappedLiterals,
1866
- optionalLineSpaces$1,
1937
+ optionalLineSpaces$3,
1867
1938
  inlinePatternCloseParen,
1868
1939
  ]);
1869
- const anonymousPattern = new Or("anonymous-pattern", [
1940
+ const anonymousPattern = new Options("anonymous-pattern", [
1870
1941
  anonymousLiterals,
1871
1942
  complexAnonymousPattern
1872
1943
  ]);
1873
1944
 
1874
- const optionalSpaces$2 = spaces$1.clone("optional-spaces", true);
1945
+ const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1875
1946
  const openBracket$1 = new Literal("open-bracket", "{");
1876
1947
  const closeBracket$1 = new Literal("close-bracket", "}");
1877
1948
  const comma = new Literal("comma", ",");
1878
1949
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1879
1950
  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", [
1951
+ const min = new Optional("optional-min", integer.clone("min"));
1952
+ const max = new Optional("optional-max", integer.clone("max"));
1953
+ const trimKeyword = new Literal("trim-keyword", "trim");
1954
+ const trimFlag = new Optional("optional-trim-flag", new Sequence("trim-flag", [lineSpaces$1, trimKeyword]));
1955
+ const bounds = new Sequence("bounds", [
1884
1956
  openBracket$1,
1885
- optionalSpaces$2,
1886
- optionalInteger.clone("min"),
1887
- optionalSpaces$2,
1957
+ optionalSpaces$3,
1958
+ min,
1959
+ optionalSpaces$3,
1888
1960
  comma,
1889
- optionalSpaces$2,
1890
- optionalInteger.clone("max"),
1961
+ optionalSpaces$3,
1962
+ max,
1891
1963
  closeBracket$1
1892
1964
  ]);
1893
- const exactCount = new And("exact-count", [
1965
+ const exactCount = new Sequence("exact-count", [
1894
1966
  openBracket$1,
1895
- optionalSpaces$2,
1967
+ optionalSpaces$3,
1896
1968
  integer,
1897
- optionalSpaces$2,
1969
+ optionalSpaces$3,
1898
1970
  closeBracket$1,
1899
1971
  ]);
1900
1972
  const quantifierShorthand = new Regex("quantifier-shorthand", "\\*|\\+");
1901
1973
  quantifierShorthand.setTokens(["*", "+"]);
1902
- const quantifier = new Or("quantifier", [
1974
+ const quantifier = new Options("quantifier", [
1903
1975
  quantifierShorthand,
1904
1976
  exactCount,
1905
1977
  bounds
@@ -1909,127 +1981,134 @@ const closeParen = new Literal("repeat-close-paren", ")");
1909
1981
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
1910
1982
  dividerComma.setTokens([", "]);
1911
1983
  const patternName$2 = name$1.clone("pattern-name");
1912
- const patterns$3 = new Or("or-patterns", [patternName$2, anonymousPattern]);
1984
+ const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
1913
1985
  const dividerPattern = patterns$3.clone("divider-pattern");
1914
- const repeatLiteral = new And("repeat-literal", [
1986
+ const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
1987
+ const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
1988
+ const repeatLiteral = new Sequence("repeat-literal", [
1915
1989
  openParen,
1916
- optionalSpaces$2,
1990
+ optionalSpaces$3,
1917
1991
  patterns$3,
1918
- new And("optional-divider-section", [dividerComma, dividerPattern, trimFlag], true),
1919
- optionalSpaces$2,
1992
+ optionalDividerSection,
1993
+ optionalSpaces$3,
1920
1994
  closeParen,
1921
- new And("quantifier-section", [quantifier]),
1995
+ new Sequence("quantifier-section", [quantifier]),
1922
1996
  ]);
1923
1997
 
1924
- const optionalNot = new Literal("not", "!", true);
1925
- const optionalIsOptional$1 = new Literal("is-optional", "?", true);
1998
+ const optionalNot = new Optional("optional-not", new Literal("not", "!"));
1999
+ const optionalIsOptional$1 = new Optional("optional-is-optional", new Literal("is-optional", "?"));
1926
2000
  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", [
2001
+ const patterns$2 = new Options("and-patterns", [patternName$1, anonymousPattern]);
2002
+ const pattern$1 = new Sequence("and-child-pattern", [
1929
2003
  optionalNot,
1930
2004
  patterns$2,
1931
2005
  optionalIsOptional$1,
1932
2006
  ]);
1933
2007
  const divider$1 = new Regex("and-divider", "\\s*[+]\\s*");
1934
2008
  divider$1.setTokens([" + "]);
1935
- const andLiteral = new Repeat("and-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
2009
+ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
1936
2010
 
1937
2011
  const patternName = name$1.clone("pattern-name");
1938
- const patterns$1 = new Or("or-patterns", [patternName, anonymousPattern]);
2012
+ patternName.setTokens(["[PATTERN_NAME]"]);
2013
+ const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
1939
2014
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2015
+ defaultDivider.setTokens(["|"]);
1940
2016
  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 });
2017
+ greedyDivider.setTokens(["<|>"]);
2018
+ const divider = new Options("options-divider", [defaultDivider, greedyDivider]);
2019
+ const optionsLiteral = new Repeat("options-literal", patterns$1, { divider, min: 2, trimDivider: true });
1945
2020
 
1946
2021
  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", [
2022
+ aliasLiteral.setTokens(["[ALIAS_LITERAL]"]);
2023
+ const optionalIsOptional = new Optional("optional-flag", new Literal("is-optional", "?"));
2024
+ const configurableAnonymousPattern = new Sequence("configurable-anonymous-pattern", [anonymousPattern, optionalIsOptional]);
2025
+ const pattern = new Options("pattern", [
1950
2026
  literal,
1951
2027
  regexLiteral,
1952
2028
  repeatLiteral,
1953
2029
  aliasLiteral,
1954
- orLiteral,
1955
- andLiteral,
2030
+ optionsLiteral,
2031
+ sequenceLiteral,
1956
2032
  configurableAnonymousPattern,
1957
- ], false, true);
2033
+ ], true);
1958
2034
 
1959
- const optionalSpaces$1 = spaces$1.clone("optional-spaces", true);
2035
+ const optionalSpaces$2 = new Optional("optional-spaces", spaces$1);
1960
2036
  const assignOperator = new Literal("assign-operator", "=");
1961
- const assignStatement = new And("assign-statement", [
1962
- optionalSpaces$1,
2037
+ const assignStatement = new Sequence("assign-statement", [
2038
+ optionalSpaces$2,
1963
2039
  name$1,
1964
- optionalSpaces$1,
2040
+ optionalSpaces$2,
1965
2041
  assignOperator,
1966
- optionalSpaces$1,
2042
+ optionalSpaces$2,
1967
2043
  pattern
1968
2044
  ]);
1969
- const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
2045
+ const statement = new Options("statement", [assignStatement, name$1.clone("export-name")]);
1970
2046
 
1971
- const bodyLineContent = new Or("body-line-content", [
2047
+ const bodyLineContent = new Options("body-line-content", [
1972
2048
  comment,
1973
2049
  statement
1974
2050
  ]);
1975
- const bodyLine = new And("body-line", [
1976
- lineSpaces$1.clone("line-spaces", true),
2051
+ const optionalLineSpaces$2 = new Optional("optional-line-spaces", lineSpaces$1);
2052
+ const bodyLine = new Sequence("body-line", [
2053
+ optionalLineSpaces$2,
1977
2054
  bodyLineContent,
1978
- lineSpaces$1.clone("line-spaces", true),
2055
+ optionalLineSpaces$2,
1979
2056
  ]);
1980
2057
  const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
1981
2058
 
1982
- const optionalSpaces = allSpaces.clone("optional-spaces", true);
1983
- const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
2059
+ const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2060
+ const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
1984
2061
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2062
+ importNameDivider.setTokens([", "]);
2063
+ const name = new Regex("import-name", "[^}\\s,]+");
2064
+ name.setTokens(["[IMPORT_NAME]"]);
1985
2065
  const importKeyword = new Literal("import", "import");
1986
2066
  const useParamsKeyword = new Literal("use-params", "use params");
1987
2067
  const asKeyword = new Literal("as", "as");
1988
2068
  const fromKeyword = new Literal("from", "from");
1989
2069
  const openBracket = new Literal("open-bracket", "{");
1990
2070
  const closeBracket = new Literal("close-bracket", "}");
1991
- const name = new Regex("import-name", "[^}\\s,]+");
1992
2071
  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 });
2072
+ const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
2073
+ const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
1995
2074
  const paramName = name.clone("param-name");
1996
2075
  const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
1997
2076
  const resource = literal.clone("resource");
1998
- const useParams = new And("import-params", [
2077
+ const useParams = new Sequence("import-params", [
1999
2078
  useParamsKeyword,
2000
- optionalLineSpaces,
2079
+ optionalLineSpaces$1,
2001
2080
  openBracket,
2002
- optionalSpaces,
2081
+ optionalSpaces$1,
2003
2082
  paramNames,
2004
- optionalSpaces,
2083
+ optionalSpaces$1,
2005
2084
  closeBracket
2006
2085
  ]);
2007
2086
  const withParamsKeyword = new Literal("with-params", "with params");
2008
- const withParamsStatement = new And("with-params-statement", [
2087
+ const withParamsStatement = new Optional("optional-with-params-statement", new Sequence("with-params-statement", [
2009
2088
  withParamsKeyword,
2010
- optionalLineSpaces,
2089
+ optionalLineSpaces$1,
2011
2090
  openBracket,
2012
- optionalSpaces,
2091
+ optionalSpaces$1,
2013
2092
  body.clone("with-params-body"),
2014
- optionalSpaces,
2093
+ optionalSpaces$1,
2015
2094
  closeBracket
2016
- ], true);
2017
- const importFromStatement = new And("import-from", [
2095
+ ]));
2096
+ const importFromStatement = new Sequence("import-from", [
2018
2097
  importKeyword,
2019
- optionalLineSpaces,
2098
+ optionalLineSpaces$1,
2020
2099
  openBracket,
2021
- optionalSpaces,
2100
+ optionalSpaces$1,
2022
2101
  importedNames,
2023
- optionalSpaces,
2102
+ optionalSpaces$1,
2024
2103
  closeBracket,
2025
- optionalLineSpaces,
2104
+ optionalLineSpaces$1,
2026
2105
  fromKeyword,
2027
- optionalLineSpaces,
2106
+ optionalLineSpaces$1,
2028
2107
  resource,
2029
- optionalLineSpaces,
2108
+ optionalLineSpaces$1,
2030
2109
  withParamsStatement
2031
2110
  ]);
2032
- const importStatement = new Or("import-statement", [
2111
+ const importStatement = new Options("import-statement", [
2033
2112
  useParams,
2034
2113
  importFromStatement
2035
2114
  ]);
@@ -2040,23 +2119,25 @@ const newLine = new Regex("new-line", "(\\r?\\n)+");
2040
2119
  spaces.setTokens([" "]);
2041
2120
  tabs.setTokens(["\t"]);
2042
2121
  newLine.setTokens(["\n"]);
2043
- const lineSpaces = new Repeat("line-spaces", new Or("line-space", [tabs, spaces]));
2044
- const headLineContent = new Or("head-line-content", [
2122
+ const lineSpaces = new Repeat("line-spaces", new Options("line-space", [tabs, spaces]));
2123
+ const optionalLineSpaces = new Optional("optional-line-spaces", lineSpaces);
2124
+ const headLineContent = new Options("head-line-content", [
2045
2125
  comment,
2046
2126
  importStatement
2047
2127
  ]);
2048
- const headLine = new And("head-line-content", [
2049
- lineSpaces.clone("line-spaces", true),
2128
+ const headLine = new Sequence("head-line-content", [
2129
+ optionalLineSpaces,
2050
2130
  headLineContent,
2051
- lineSpaces.clone("line-spaces", true),
2131
+ optionalLineSpaces,
2052
2132
  ]);
2053
- const head = new Repeat("head", headLine, { divider: newLine, min: 0 });
2054
- const grammar = new And("grammar", [
2055
- allSpaces,
2133
+ const head = new Optional("optional-head", new Repeat("head", headLine, { divider: newLine }));
2134
+ const optionalSpaces = new Optional("optional-spaces", allSpaces);
2135
+ const grammar = new Sequence("grammar", [
2136
+ optionalSpaces,
2056
2137
  head,
2057
- allSpaces,
2138
+ optionalSpaces,
2058
2139
  body,
2059
- allSpaces
2140
+ optionalSpaces
2060
2141
  ]);
2061
2142
 
2062
2143
  let idIndex = 0;
@@ -2087,7 +2168,7 @@ class Not {
2087
2168
  this._type = "not";
2088
2169
  this._name = name;
2089
2170
  this._parent = null;
2090
- this._children = [pattern.clone(pattern.name, false)];
2171
+ this._children = [pattern.clone()];
2091
2172
  this._children[0].parent = this;
2092
2173
  }
2093
2174
  test(text) {
@@ -2375,8 +2456,8 @@ let anonymousIndexId = 0;
2375
2456
  const patternNodes = {
2376
2457
  "literal": true,
2377
2458
  "regex-literal": true,
2378
- "or-literal": true,
2379
- "and-literal": true,
2459
+ "options-literal": true,
2460
+ "sequence-literal": true,
2380
2461
  "repeat-literal": true,
2381
2462
  "alias-literal": true,
2382
2463
  "configurable-anonymous-pattern": true
@@ -2479,12 +2560,12 @@ class Grammar {
2479
2560
  this._saveRegex(n);
2480
2561
  break;
2481
2562
  }
2482
- case "or-literal": {
2483
- this._saveOr(n);
2563
+ case "options-literal": {
2564
+ this._saveOptions(n);
2484
2565
  break;
2485
2566
  }
2486
- case "and-literal": {
2487
- this._saveAnd(n);
2567
+ case "sequence-literal": {
2568
+ this._saveSequence(n);
2488
2569
  break;
2489
2570
  }
2490
2571
  case "repeat-literal": {
@@ -2539,18 +2620,18 @@ class Grammar {
2539
2620
  const value = node.value.slice(1, node.value.length - 1);
2540
2621
  return new Regex(name, value);
2541
2622
  }
2542
- _saveOr(statementNode) {
2623
+ _saveOptions(statementNode) {
2543
2624
  const nameNode = statementNode.find(n => n.name === "name");
2544
2625
  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);
2626
+ const optionsNode = statementNode.find(n => n.name === "options-literal");
2627
+ const options = this._buildOptions(name, optionsNode);
2628
+ this._parseContext.patternsByName.set(name, options);
2548
2629
  }
2549
- _buildOr(name, node) {
2630
+ _buildOptions(name, node) {
2550
2631
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
2551
2632
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
2552
2633
  const patterns = patternNodes.map(n => this._buildPattern(n));
2553
- const or = new Or(name, patterns, false, isGreedy);
2634
+ const or = new Options(name, patterns, isGreedy);
2554
2635
  return or;
2555
2636
  }
2556
2637
  _buildPattern(node) {
@@ -2569,11 +2650,11 @@ class Grammar {
2569
2650
  case "repeat-literal": {
2570
2651
  return this._buildRepeat(name, node);
2571
2652
  }
2572
- case "or-literal": {
2573
- return this._buildOr(name, node);
2653
+ case "options-literal": {
2654
+ return this._buildOptions(name, node);
2574
2655
  }
2575
- case "and-literal": {
2576
- return this._buildAnd(name, node);
2656
+ case "sequence-literal": {
2657
+ return this._buildSequence(name, node);
2577
2658
  }
2578
2659
  case "complex-anonymous-pattern": {
2579
2660
  return this._buildComplexAnonymousPattern(node);
@@ -2581,26 +2662,27 @@ class Grammar {
2581
2662
  }
2582
2663
  throw new Error(`Couldn't build node: ${node.name}.`);
2583
2664
  }
2584
- _saveAnd(statementNode) {
2665
+ _saveSequence(statementNode) {
2585
2666
  const nameNode = statementNode.find(n => n.name === "name");
2586
2667
  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);
2668
+ const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
2669
+ const sequence = this._buildSequence(name, sequenceNode);
2670
+ this._parseContext.patternsByName.set(name, sequence);
2590
2671
  }
2591
- _buildAnd(name, node) {
2672
+ _buildSequence(name, node) {
2592
2673
  const patternNodes = node.children.filter(n => n.name !== "and-divider");
2593
2674
  const patterns = patternNodes.map(n => {
2594
2675
  const patternNode = n.children[0].name === "not" ? n.children[1] : n.children[0];
2595
2676
  const isNot = n.find(n => n.name === "not") != null;
2596
2677
  const isOptional = n.find(n => n.name === "is-optional");
2597
- const pattern = this._buildPattern(patternNode).clone(undefined, isOptional == null ? undefined : true);
2678
+ const pattern = this._buildPattern(patternNode);
2679
+ const finalPattern = isOptional ? new Optional(pattern.name, pattern) : pattern;
2598
2680
  if (isNot) {
2599
- return new Not(`not-${pattern.name}`, pattern);
2681
+ return new Not(`not-${finalPattern.name}`, finalPattern);
2600
2682
  }
2601
- return pattern;
2683
+ return finalPattern;
2602
2684
  });
2603
- return new And(name, patterns);
2685
+ return new Sequence(name, patterns);
2604
2686
  }
2605
2687
  _saveRepeat(statementNode) {
2606
2688
  const nameNode = statementNode.find(n => n.name === "name");
@@ -2610,13 +2692,14 @@ class Grammar {
2610
2692
  this._parseContext.patternsByName.set(name, repeat);
2611
2693
  }
2612
2694
  _buildRepeat(name, repeatNode) {
2695
+ let isOptional = false;
2613
2696
  const bounds = repeatNode.find(n => n.name === "bounds");
2614
2697
  const exactCount = repeatNode.find(n => n.name === "exact-count");
2615
2698
  const quantifier = repeatNode.find(n => n.name === "quantifier-shorthand");
2616
2699
  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];
2700
+ const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2618
2701
  const pattern = this._buildPattern(patterNode);
2619
- const dividerSectionNode = repeatNode.find(n => n.name === "optional-divider-section");
2702
+ const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2620
2703
  const options = {
2621
2704
  min: 1,
2622
2705
  max: Infinity
@@ -2649,18 +2732,17 @@ class Grammar {
2649
2732
  options.max = Infinity;
2650
2733
  }
2651
2734
  else {
2652
- options.min = 0;
2653
- options.max = Infinity;
2735
+ isOptional = true;
2654
2736
  }
2655
2737
  }
2656
- return new Repeat(name, pattern.clone(pattern.name), options);
2738
+ return isOptional ? new Optional(name, new Repeat(name, pattern, options)) : new Repeat(name, pattern, options);
2657
2739
  }
2658
2740
  _saveConfigurableAnonymous(node) {
2659
2741
  const nameNode = node.find(n => n.name === "name");
2660
2742
  const name = nameNode.value;
2661
2743
  const anonymousNode = node.find(n => n.name === "complex-anonymous-pattern");
2662
2744
  const isOptional = node.children[1] != null;
2663
- const anonymous = this._buildPattern(anonymousNode).clone(name, isOptional);
2745
+ const anonymous = isOptional ? new Optional(name, this._buildPattern(anonymousNode)) : this._buildPattern(anonymousNode);
2664
2746
  this._parseContext.patternsByName.set(name, anonymous);
2665
2747
  }
2666
2748
  _buildComplexAnonymousPattern(node) {
@@ -2803,5 +2885,5 @@ function patterns(strings, ...values) {
2803
2885
  return result;
2804
2886
  }
2805
2887
 
2806
- export { And, AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Or, ParseError, Reference, Regex, Repeat, arePatternsEqual, grammar, patterns };
2888
+ export { AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, arePatternsEqual, grammar, patterns };
2807
2889
  //# sourceMappingURL=index.esm.js.map