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.js CHANGED
@@ -475,7 +475,7 @@ class Cursor {
475
475
  }
476
476
  }
477
477
 
478
- let idIndex$8 = 0;
478
+ let idIndex$9 = 0;
479
479
  class Literal {
480
480
  get id() {
481
481
  return this._id;
@@ -486,6 +486,9 @@ class Literal {
486
486
  get name() {
487
487
  return this._name;
488
488
  }
489
+ get value() {
490
+ return this._text;
491
+ }
489
492
  get parent() {
490
493
  return this._parent;
491
494
  }
@@ -495,19 +498,15 @@ class Literal {
495
498
  get children() {
496
499
  return [];
497
500
  }
498
- get isOptional() {
499
- return this._isOptional;
500
- }
501
- constructor(name, value, isOptional = false) {
501
+ constructor(name, value) {
502
502
  if (value.length === 0) {
503
503
  throw new Error("Value Cannot be empty.");
504
504
  }
505
- this._id = `literal-${idIndex$8++}`;
505
+ this._id = `literal-${idIndex$9++}`;
506
506
  this._type = "literal";
507
507
  this._name = name;
508
508
  this._text = value;
509
509
  this._runes = Array.from(value);
510
- this._isOptional = isOptional;
511
510
  this._parent = null;
512
511
  this._firstIndex = 0;
513
512
  this._lastIndex = 0;
@@ -538,13 +537,7 @@ class Literal {
538
537
  cursor.endParse();
539
538
  return node;
540
539
  }
541
- if (!this._isOptional) {
542
- cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
543
- cursor.endParse();
544
- return null;
545
- }
546
- cursor.resolveError();
547
- cursor.moveTo(this._firstIndex);
540
+ cursor.recordErrorAt(this._firstIndex, this._endIndex, this);
548
541
  cursor.endParse();
549
542
  return null;
550
543
  }
@@ -574,8 +567,8 @@ class Literal {
574
567
  _createNode() {
575
568
  return new Node("literal", this._name, this._firstIndex, this._lastIndex, undefined, this._text);
576
569
  }
577
- clone(name = this._name, isOptional = this._isOptional) {
578
- const clone = new Literal(name, this._text, isOptional);
570
+ clone(name = this._name) {
571
+ const clone = new Literal(name, this._text);
579
572
  clone._id = this._id;
580
573
  return clone;
581
574
  }
@@ -611,7 +604,7 @@ class Literal {
611
604
  }
612
605
  }
613
606
 
614
- let idIndex$7 = 0;
607
+ let idIndex$8 = 0;
615
608
  class Regex {
616
609
  get id() {
617
610
  return this._id;
@@ -622,6 +615,9 @@ class Regex {
622
615
  get name() {
623
616
  return this._name;
624
617
  }
618
+ get value() {
619
+ return this._originalRegexString;
620
+ }
625
621
  get parent() {
626
622
  return this._parent;
627
623
  }
@@ -631,19 +627,15 @@ class Regex {
631
627
  get children() {
632
628
  return [];
633
629
  }
634
- get isOptional() {
635
- return this._isOptional;
636
- }
637
- constructor(name, regex, isOptional = false) {
630
+ constructor(name, regex) {
638
631
  this._node = null;
639
632
  this._cursor = null;
640
633
  this._firstIndex = -1;
641
634
  this._substring = "";
642
635
  this._tokens = [];
643
- this._id = `regex-${idIndex$7++}`;
636
+ this._id = `regex-${idIndex$8++}`;
644
637
  this._type = "regex";
645
638
  this._name = name;
646
- this._isOptional = isOptional;
647
639
  this._parent = null;
648
640
  this._originalRegexString = regex;
649
641
  this._regex = new RegExp(`^${regex}`, "g");
@@ -705,13 +697,11 @@ class Regex {
705
697
  cursor.recordMatch(this, this._node);
706
698
  }
707
699
  processError(cursor) {
708
- if (!this._isOptional) {
709
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
710
- }
700
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
711
701
  this._node = null;
712
702
  }
713
- clone(name = this._name, isOptional = this._isOptional) {
714
- const clone = new Regex(name, this._originalRegexString, isOptional);
703
+ clone(name = this._name) {
704
+ const clone = new Regex(name, this._originalRegexString);
715
705
  clone._tokens = this._tokens.slice();
716
706
  clone._id = this._id;
717
707
  return clone;
@@ -773,7 +763,7 @@ function findPattern(pattern, predicate) {
773
763
  }
774
764
  }
775
765
 
776
- let idIndex$6 = 0;
766
+ let idIndex$7 = 0;
777
767
  class Reference {
778
768
  get id() {
779
769
  return this._id;
@@ -793,15 +783,11 @@ class Reference {
793
783
  get children() {
794
784
  return this._children;
795
785
  }
796
- get isOptional() {
797
- return this._isOptional;
798
- }
799
- constructor(name, isOptional = false) {
800
- this._id = `reference-${idIndex$6++}`;
786
+ constructor(name) {
787
+ this._id = `reference-${idIndex$7++}`;
801
788
  this._type = "reference";
802
789
  this._name = name;
803
790
  this._parent = null;
804
- this._isOptional = isOptional;
805
791
  this._pattern = null;
806
792
  this._children = [];
807
793
  }
@@ -828,7 +814,7 @@ class Reference {
828
814
  if (pattern === null) {
829
815
  throw new Error(`Couldn't find '${this._name}' pattern within tree.`);
830
816
  }
831
- const clonedPattern = pattern.clone(this._name, this._isOptional);
817
+ const clonedPattern = pattern.clone();
832
818
  clonedPattern.parent = this;
833
819
  this._pattern = clonedPattern;
834
820
  this._children = [this._pattern];
@@ -887,8 +873,8 @@ class Reference {
887
873
  find(_predicate) {
888
874
  return null;
889
875
  }
890
- clone(name = this._name, isOptional = this._isOptional) {
891
- const clone = new Reference(name, isOptional);
876
+ clone(name = this._name) {
877
+ const clone = new Reference(name);
892
878
  clone._id = this._id;
893
879
  return clone;
894
880
  }
@@ -897,12 +883,12 @@ class Reference {
897
883
  }
898
884
  }
899
885
 
900
- function clonePatterns(patterns, isOptional) {
901
- return patterns.map(p => p.clone(p.name, isOptional));
886
+ function clonePatterns(patterns) {
887
+ return patterns.map(p => p.clone());
902
888
  }
903
889
 
904
- let idIndex$5 = 0;
905
- class Or {
890
+ let idIndex$6 = 0;
891
+ class Options {
906
892
  get id() {
907
893
  return this._id;
908
894
  }
@@ -921,21 +907,17 @@ class Or {
921
907
  get children() {
922
908
  return this._children;
923
909
  }
924
- get isOptional() {
925
- return this._isOptional;
926
- }
927
- constructor(name, options, isOptional = false, isGreedy = false) {
910
+ constructor(name, options, isGreedy = false) {
928
911
  if (options.length === 0) {
929
912
  throw new Error("Need at least one pattern with an 'or' pattern.");
930
913
  }
931
- const children = clonePatterns(options, false);
914
+ const children = clonePatterns(options);
932
915
  this._assignChildrenToParent(children);
933
- this._id = `or-${idIndex$5++}`;
916
+ this._id = `or-${idIndex$6++}`;
934
917
  this._type = "or";
935
918
  this._name = name;
936
919
  this._parent = null;
937
920
  this._children = children;
938
- this._isOptional = isOptional;
939
921
  this._firstIndex = 0;
940
922
  this._isGreedy = isGreedy;
941
923
  }
@@ -968,13 +950,7 @@ class Or {
968
950
  cursor.endParse();
969
951
  return node;
970
952
  }
971
- if (!this._isOptional) {
972
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
973
- cursor.endParse();
974
- return null;
975
- }
976
- cursor.resolveError();
977
- cursor.moveTo(this._firstIndex);
953
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
978
954
  cursor.endParse();
979
955
  return null;
980
956
  }
@@ -986,7 +962,7 @@ class Or {
986
962
  if (this._isGreedy) {
987
963
  results.push(result);
988
964
  }
989
- if (!cursor.hasError && !this._isGreedy) {
965
+ if (result != null && !this._isGreedy) {
990
966
  return result;
991
967
  }
992
968
  cursor.resolveError();
@@ -1036,8 +1012,8 @@ class Or {
1036
1012
  find(predicate) {
1037
1013
  return findPattern(this, predicate);
1038
1014
  }
1039
- clone(name = this._name, isOptional = this._isOptional) {
1040
- const or = new Or(name, this._children, isOptional, this._isGreedy);
1015
+ clone(name = this._name) {
1016
+ const or = new Options(name, this._children, this._isGreedy);
1041
1017
  or._id = this._id;
1042
1018
  return or;
1043
1019
  }
@@ -1046,7 +1022,7 @@ class Or {
1046
1022
  }
1047
1023
  }
1048
1024
 
1049
- let idIndex$4 = 0;
1025
+ let idIndex$5 = 0;
1050
1026
  class FiniteRepeat {
1051
1027
  get id() {
1052
1028
  return this._id;
@@ -1066,29 +1042,30 @@ class FiniteRepeat {
1066
1042
  get children() {
1067
1043
  return this._children;
1068
1044
  }
1069
- get isOptional() {
1070
- return this._min === 0;
1071
- }
1072
1045
  get min() {
1073
1046
  return this._min;
1074
1047
  }
1075
1048
  get max() {
1076
1049
  return this._max;
1077
1050
  }
1078
- constructor(name, pattern, repeatAmount, options = {}) {
1079
- this._id = `finite-repeat-${idIndex$4++}`;
1051
+ constructor(name, pattern, options = {}) {
1052
+ this._id = `finite-repeat-${idIndex$5++}`;
1080
1053
  this._type = "finite-repeat";
1081
1054
  this._name = name;
1082
1055
  this._parent = null;
1083
1056
  this._children = [];
1084
1057
  this._hasDivider = options.divider != null;
1085
- this._min = options.min != null ? options.min : 1;
1086
- this._max = repeatAmount;
1058
+ this._min = options.min != null ? Math.max(options.min, 1) : 1;
1059
+ this._max = Math.max(this.min, options.max || this.min);
1087
1060
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1088
- for (let i = 0; i < repeatAmount; i++) {
1089
- this._children.push(pattern.clone(pattern.name));
1090
- if (options.divider != null && (i < repeatAmount - 1 || !this._trimDivider)) {
1091
- this._children.push(options.divider.clone(options.divider.name, false));
1061
+ for (let i = 0; i < this._max; i++) {
1062
+ const child = pattern.clone();
1063
+ child.parent = this;
1064
+ this._children.push(child);
1065
+ if (options.divider != null && (i < this._max - 1 || !this._trimDivider)) {
1066
+ const divider = options.divider.clone();
1067
+ divider.parent = this;
1068
+ this._children.push(divider);
1092
1069
  }
1093
1070
  }
1094
1071
  }
@@ -1100,15 +1077,18 @@ class FiniteRepeat {
1100
1077
  let matchCount = 0;
1101
1078
  for (let i = 0; i < this._children.length; i++) {
1102
1079
  const childPattern = this._children[i];
1080
+ const runningIndex = cursor.index;
1103
1081
  const node = childPattern.parse(cursor);
1082
+ if (cursor.hasError) {
1083
+ break;
1084
+ }
1104
1085
  if (i % modulo === 0 && !cursor.hasError) {
1105
1086
  matchCount++;
1106
1087
  }
1107
- if (cursor.hasError) {
1108
- cursor.resolveError();
1109
- break;
1088
+ if (node == null) {
1089
+ cursor.moveTo(runningIndex);
1110
1090
  }
1111
- if (node != null) {
1091
+ else {
1112
1092
  nodes.push(node);
1113
1093
  if (cursor.hasNext()) {
1114
1094
  cursor.next();
@@ -1119,7 +1099,8 @@ class FiniteRepeat {
1119
1099
  }
1120
1100
  }
1121
1101
  if (this._trimDivider && this._hasDivider) {
1122
- if (cursor.leafMatch.pattern === this.children[1]) {
1102
+ const isDividerLastMatch = cursor.leafMatch.pattern === this.children[1];
1103
+ if (isDividerLastMatch) {
1123
1104
  const node = nodes.pop();
1124
1105
  cursor.moveTo(node.firstIndex);
1125
1106
  }
@@ -1131,14 +1112,14 @@ class FiniteRepeat {
1131
1112
  cursor.endParse();
1132
1113
  return null;
1133
1114
  }
1134
- else if (nodes.length === 0) {
1135
- cursor.resolveError();
1115
+ if (nodes.length === 0 && !cursor.hasError) {
1136
1116
  cursor.moveTo(startIndex);
1137
1117
  cursor.endParse();
1138
1118
  return null;
1139
1119
  }
1140
1120
  const firstIndex = nodes[0].firstIndex;
1141
1121
  const lastIndex = nodes[nodes.length - 1].lastIndex;
1122
+ cursor.resolveError();
1142
1123
  cursor.moveTo(lastIndex);
1143
1124
  cursor.endParse();
1144
1125
  return new Node(this._type, this.name, firstIndex, lastIndex, nodes);
@@ -1157,19 +1138,13 @@ class FiniteRepeat {
1157
1138
  cursor
1158
1139
  };
1159
1140
  }
1160
- clone(name = this._name, isOptional) {
1141
+ clone(name = this._name) {
1161
1142
  let min = this._min;
1162
- if (isOptional != null) {
1163
- if (isOptional) {
1164
- min = 0;
1165
- }
1166
- else {
1167
- min = Math.max(this._min, 1);
1168
- }
1169
- }
1170
- const clone = new FiniteRepeat(name, this._children[0], this._max, {
1143
+ let max = this._max;
1144
+ const clone = new FiniteRepeat(name, this._children[0], {
1171
1145
  divider: this._hasDivider ? this._children[1] : undefined,
1172
1146
  min,
1147
+ max,
1173
1148
  trimDivider: this._trimDivider
1174
1149
  });
1175
1150
  clone._id = this._id;
@@ -1226,7 +1201,7 @@ class FiniteRepeat {
1226
1201
  }
1227
1202
  }
1228
1203
 
1229
- let idIndex$3 = 0;
1204
+ let idIndex$4 = 0;
1230
1205
  class InfiniteRepeat {
1231
1206
  get id() {
1232
1207
  return this._id;
@@ -1246,24 +1221,21 @@ class InfiniteRepeat {
1246
1221
  get children() {
1247
1222
  return this._children;
1248
1223
  }
1249
- get isOptional() {
1250
- return this._min === 0;
1251
- }
1252
1224
  get min() {
1253
1225
  return this._min;
1254
1226
  }
1255
1227
  constructor(name, pattern, options = {}) {
1256
- const min = options.min != null ? options.min : 1;
1228
+ const min = options.min != null ? Math.max(options.min, 1) : 1;
1257
1229
  const divider = options.divider;
1258
1230
  let children;
1259
1231
  if (divider != null) {
1260
- children = [pattern.clone(pattern.name, false), divider.clone(divider.name, false)];
1232
+ children = [pattern.clone(), divider.clone()];
1261
1233
  }
1262
1234
  else {
1263
- children = [pattern.clone(pattern.name, false)];
1235
+ children = [pattern.clone()];
1264
1236
  }
1265
1237
  this._assignChildrenToParent(children);
1266
- this._id = `infinite-repeat-${idIndex$3++}`;
1238
+ this._id = `infinite-repeat-${idIndex$4++}`;
1267
1239
  this._type = "infinite-repeat";
1268
1240
  this._name = name;
1269
1241
  this._min = min;
@@ -1328,8 +1300,12 @@ class InfiniteRepeat {
1328
1300
  let passed = false;
1329
1301
  while (true) {
1330
1302
  const runningCursorIndex = cursor.index;
1331
- const repeatedNode = this._pattern.parse(cursor);
1332
- if (cursor.hasError) {
1303
+ const repeatNode = this._pattern.parse(cursor);
1304
+ const hasError = cursor.hasError;
1305
+ const hasNoErrorAndNoResult = !cursor.hasError && repeatNode == null;
1306
+ const hasDivider = this._divider != null;
1307
+ const hasNoDivider = !hasDivider;
1308
+ if (hasError) {
1333
1309
  const lastValidNode = this._getLastValidNode();
1334
1310
  if (lastValidNode != null) {
1335
1311
  passed = true;
@@ -1342,8 +1318,12 @@ class InfiniteRepeat {
1342
1318
  break;
1343
1319
  }
1344
1320
  else {
1345
- if (repeatedNode != null) {
1346
- this._nodes.push(repeatedNode);
1321
+ if (hasNoErrorAndNoResult && hasNoDivider) {
1322
+ // If we didn't match and didn't error we need to get out. Nothing different will happen.
1323
+ break;
1324
+ }
1325
+ if (repeatNode != null) {
1326
+ this._nodes.push(repeatNode);
1347
1327
  if (!cursor.hasNext()) {
1348
1328
  passed = true;
1349
1329
  break;
@@ -1351,18 +1331,29 @@ class InfiniteRepeat {
1351
1331
  cursor.next();
1352
1332
  }
1353
1333
  if (this._divider != null) {
1334
+ const dividerStartIndex = cursor.index;
1354
1335
  const dividerNode = this._divider.parse(cursor);
1355
1336
  if (cursor.hasError) {
1356
1337
  passed = true;
1357
1338
  break;
1358
1339
  }
1359
- else if (dividerNode != null) {
1360
- this._nodes.push(dividerNode);
1361
- if (!cursor.hasNext()) {
1362
- passed = true;
1363
- break;
1340
+ else {
1341
+ if (dividerNode == null) {
1342
+ cursor.moveTo(dividerStartIndex);
1343
+ if (dividerNode == null && repeatNode == null) {
1344
+ // If neither the repeat pattern or divider pattern matched get out.
1345
+ passed = true;
1346
+ break;
1347
+ }
1348
+ }
1349
+ else {
1350
+ this._nodes.push(dividerNode);
1351
+ if (!cursor.hasNext()) {
1352
+ passed = true;
1353
+ break;
1354
+ }
1355
+ cursor.next();
1364
1356
  }
1365
- cursor.next();
1366
1357
  }
1367
1358
  }
1368
1359
  }
@@ -1386,10 +1377,10 @@ class InfiniteRepeat {
1386
1377
  const dividerNode = this._nodes.pop();
1387
1378
  cursor.moveTo(dividerNode.firstIndex);
1388
1379
  }
1389
- // if (this._nodes.length === 0) {
1390
- // cursor.moveTo(this._firstIndex);
1391
- // return null;
1392
- // }
1380
+ if (this._nodes.length === 0) {
1381
+ cursor.moveTo(this._firstIndex);
1382
+ return null;
1383
+ }
1393
1384
  const lastIndex = this._nodes[this._nodes.length - 1].lastIndex;
1394
1385
  cursor.moveTo(lastIndex);
1395
1386
  return new Node(this._type, this._name, this._firstIndex, lastIndex, this._nodes);
@@ -1458,16 +1449,8 @@ class InfiniteRepeat {
1458
1449
  find(predicate) {
1459
1450
  return findPattern(this, predicate);
1460
1451
  }
1461
- clone(name = this._name, isOptional) {
1452
+ clone(name = this._name) {
1462
1453
  let min = this._min;
1463
- if (isOptional != null) {
1464
- if (isOptional) {
1465
- min = 0;
1466
- }
1467
- else {
1468
- min = Math.max(this._min, 1);
1469
- }
1470
- }
1471
1454
  const clone = new InfiniteRepeat(name, this._pattern, {
1472
1455
  divider: this._divider == null ? undefined : this._divider,
1473
1456
  min: min,
@@ -1481,7 +1464,7 @@ class InfiniteRepeat {
1481
1464
  }
1482
1465
  }
1483
1466
 
1484
- let idIndex$2 = 0;
1467
+ let idIndex$3 = 0;
1485
1468
  class Repeat {
1486
1469
  get id() {
1487
1470
  return this._id;
@@ -1501,16 +1484,19 @@ class Repeat {
1501
1484
  get children() {
1502
1485
  return this._children;
1503
1486
  }
1504
- get isOptional() {
1505
- return this._repeatPattern.isOptional;
1487
+ get min() {
1488
+ return this.children[0].min;
1489
+ }
1490
+ get max() {
1491
+ return this.children[0].max || Infinity;
1506
1492
  }
1507
1493
  constructor(name, pattern, options = {}) {
1508
- this._id = `repeat-${idIndex$2++}`;
1494
+ this._id = `repeat-${idIndex$3++}`;
1509
1495
  this._pattern = pattern;
1510
1496
  this._parent = null;
1511
1497
  this._options = Object.assign(Object.assign({}, options), { min: options.min == null ? 1 : options.min, max: options.max == null ? Infinity : options.max });
1512
1498
  if (this._options.max !== Infinity) {
1513
- this._repeatPattern = new FiniteRepeat(name, pattern, this._options.max, this._options);
1499
+ this._repeatPattern = new FiniteRepeat(name, pattern, this._options);
1514
1500
  }
1515
1501
  else {
1516
1502
  this._repeatPattern = new InfiniteRepeat(name, pattern, this._options);
@@ -1527,16 +1513,8 @@ class Repeat {
1527
1513
  test(text) {
1528
1514
  return this._repeatPattern.test(text);
1529
1515
  }
1530
- clone(name = this.name, isOptional) {
1516
+ clone(name = this.name) {
1531
1517
  let min = this._options.min;
1532
- if (isOptional != null) {
1533
- if (isOptional) {
1534
- min = 0;
1535
- }
1536
- else {
1537
- min = Math.max(this._options.min, 1);
1538
- }
1539
- }
1540
1518
  const clone = new Repeat(name, this._pattern, Object.assign(Object.assign({}, this._options), { min }));
1541
1519
  clone._id = this._id;
1542
1520
  return clone;
@@ -1580,6 +1558,7 @@ class Repeat {
1580
1558
  }
1581
1559
 
1582
1560
  const comment = new Regex("comment", "#[^\r\n]+");
1561
+ comment.setTokens(["# "]);
1583
1562
 
1584
1563
  function filterOutNull(nodes) {
1585
1564
  const filteredNodes = [];
@@ -1591,8 +1570,8 @@ function filterOutNull(nodes) {
1591
1570
  return filteredNodes;
1592
1571
  }
1593
1572
 
1594
- let idIndex$1 = 0;
1595
- class And {
1573
+ let idIndex$2 = 0;
1574
+ class Sequence {
1596
1575
  get id() {
1597
1576
  return this._id;
1598
1577
  }
@@ -1611,19 +1590,15 @@ class And {
1611
1590
  get children() {
1612
1591
  return this._children;
1613
1592
  }
1614
- get isOptional() {
1615
- return this._isOptional;
1616
- }
1617
- constructor(name, sequence, isOptional = false) {
1593
+ constructor(name, sequence) {
1618
1594
  if (sequence.length === 0) {
1619
- throw new Error("Need at least one pattern with an 'and' pattern.");
1595
+ throw new Error("Need at least one pattern with a 'sequence' pattern.");
1620
1596
  }
1621
1597
  const children = clonePatterns(sequence);
1622
1598
  this._assignChildrenToParent(children);
1623
- this._id = `and-${idIndex$1++}`;
1624
- this._type = "and";
1599
+ this._id = `sequence-${idIndex$2++}`;
1600
+ this._type = "sequence";
1625
1601
  this._name = name;
1626
- this._isOptional = isOptional;
1627
1602
  this._parent = null;
1628
1603
  this._children = children;
1629
1604
  this._firstIndex = -1;
@@ -1661,9 +1636,6 @@ class And {
1661
1636
  cursor.endParse();
1662
1637
  return node;
1663
1638
  }
1664
- if (this._isOptional) {
1665
- cursor.resolveError();
1666
- }
1667
1639
  cursor.endParse();
1668
1640
  return null;
1669
1641
  }
@@ -1736,7 +1708,7 @@ class And {
1736
1708
  const length = this._children.length;
1737
1709
  for (let i = startOnIndex; i < length; i++) {
1738
1710
  const pattern = this._children[i];
1739
- if (!pattern.isOptional) {
1711
+ if (pattern.type !== "optional") {
1740
1712
  return false;
1741
1713
  }
1742
1714
  }
@@ -1746,13 +1718,13 @@ class And {
1746
1718
  const children = filterOutNull(this._nodes);
1747
1719
  const lastIndex = children[children.length - 1].lastIndex;
1748
1720
  cursor.moveTo(lastIndex);
1749
- return new Node("and", this._name, this._firstIndex, lastIndex, children);
1721
+ return new Node("sequence", this._name, this._firstIndex, lastIndex, children);
1750
1722
  }
1751
1723
  getTokens() {
1752
1724
  const tokens = [];
1753
1725
  for (const child of this._children) {
1754
1726
  tokens.push(...child.getTokens());
1755
- if (!child.isOptional) {
1727
+ if (child.type !== "optional") {
1756
1728
  break;
1757
1729
  }
1758
1730
  }
@@ -1772,9 +1744,9 @@ class And {
1772
1744
  }
1773
1745
  getPatterns() {
1774
1746
  const patterns = [];
1775
- for (const pattern of this._children) {
1776
- patterns.push(...pattern.getPatterns());
1777
- if (!pattern.isOptional) {
1747
+ for (const child of this._children) {
1748
+ patterns.push(...child.getPatterns());
1749
+ if (child.type !== "optional") {
1778
1750
  break;
1779
1751
  }
1780
1752
  }
@@ -1803,7 +1775,7 @@ class And {
1803
1775
  for (let i = nextSiblingIndex; i < this._children.length; i++) {
1804
1776
  const child = this._children[i];
1805
1777
  patterns.push(child);
1806
- if (!child.isOptional) {
1778
+ if (child.type !== "optional") {
1807
1779
  break;
1808
1780
  }
1809
1781
  // If we are on the last child and its options then ask for the next pattern from the parent.
@@ -1822,8 +1794,8 @@ class And {
1822
1794
  find(predicate) {
1823
1795
  return findPattern(this, predicate);
1824
1796
  }
1825
- clone(name = this._name, isOptional = this._isOptional) {
1826
- const clone = new And(name, this._children, isOptional);
1797
+ clone(name = this._name) {
1798
+ const clone = new Sequence(name, this._children);
1827
1799
  clone._id = this._id;
1828
1800
  return clone;
1829
1801
  }
@@ -1833,77 +1805,183 @@ class And {
1833
1805
  }
1834
1806
 
1835
1807
  const literal = new Regex("literal", '"(?:\\\\.|[^"\\\\])*"');
1808
+ literal.setTokens(["[LITERAL]"]);
1836
1809
 
1837
1810
  const tabs$1 = new Regex("tabs", "\\t+");
1811
+ tabs$1.setTokens(["\t"]);
1838
1812
  const spaces$1 = new Regex("spaces", "[ ]+");
1839
- const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1840
1813
  spaces$1.setTokens([" "]);
1841
- tabs$1.setTokens(["\t"]);
1814
+ const newLine$1 = new Regex("new-line", "(\\r?\\n)+");
1842
1815
  newLine$1.setTokens(["\n"]);
1843
- const lineSpaces$1 = new Repeat("line-spaces", new Or("line-space", [tabs$1, spaces$1]));
1844
- const allSpaces = new Regex("all-spaces", "\\s+", true);
1816
+ const lineSpaces$1 = new Repeat("line-spaces", new Options("line-space", [tabs$1, spaces$1]));
1817
+ const allSpaces = new Regex("all-spaces", "\\s+");
1818
+ allSpaces.setTokens([" "]);
1845
1819
 
1846
1820
  const name$1 = new Regex("name", "[a-zA-Z_-]+[a-zA-Z0-9_-]*");
1847
1821
 
1848
1822
  const regexLiteral = new Regex("regex-literal", "/(\\\\/|[^/\\n\\r])*/");
1823
+ regexLiteral.setTokens(["[REGEX_EXPRESSION]"]);
1849
1824
 
1850
1825
  const patternName$3 = name$1.clone("pattern-name");
1851
- const anonymousLiterals = new Or("anonymous-literals", [
1826
+ const anonymousLiterals = new Options("anonymous-literals", [
1852
1827
  literal,
1853
1828
  regexLiteral,
1854
1829
  patternName$3,
1855
1830
  new Reference("repeat-literal"),
1856
1831
  ]);
1857
- const anonymousWrappedLiterals = new Or("anonymous-wrapped-literals", [
1858
- new Reference("or-literal"),
1859
- new Reference("and-literal"),
1832
+ const anonymousWrappedLiterals = new Options("anonymous-wrapped-literals", [
1833
+ new Reference("options-literal"),
1834
+ new Reference("sequence-literal"),
1860
1835
  new Reference("complex-anonymous-pattern")
1861
1836
  ]);
1862
1837
 
1838
+ let idIndex$1 = 0;
1839
+ class Optional {
1840
+ get id() {
1841
+ return this._id;
1842
+ }
1843
+ get type() {
1844
+ return this._type;
1845
+ }
1846
+ get name() {
1847
+ return this._name;
1848
+ }
1849
+ get parent() {
1850
+ return this._parent;
1851
+ }
1852
+ set parent(pattern) {
1853
+ this._parent = pattern;
1854
+ }
1855
+ get children() {
1856
+ return this._children;
1857
+ }
1858
+ constructor(name, pattern) {
1859
+ this._id = `optional-${idIndex$1++}`;
1860
+ this._type = "optional";
1861
+ this._name = name;
1862
+ this._parent = null;
1863
+ this._children = [pattern.clone()];
1864
+ this._children[0].parent = this;
1865
+ }
1866
+ test(text) {
1867
+ const cursor = new Cursor(text);
1868
+ this.parse(cursor);
1869
+ return !cursor.hasError;
1870
+ }
1871
+ exec(text, record = false) {
1872
+ const cursor = new Cursor(text);
1873
+ record && cursor.startRecording();
1874
+ const ast = this.parse(cursor);
1875
+ return {
1876
+ ast: (ast === null || ast === void 0 ? void 0 : ast.value) === text ? ast : null,
1877
+ cursor
1878
+ };
1879
+ }
1880
+ parse(cursor) {
1881
+ cursor.startParseWith(this);
1882
+ const firstIndex = cursor.index;
1883
+ const node = this._children[0].parse(cursor);
1884
+ if (cursor.hasError) {
1885
+ cursor.resolveError();
1886
+ cursor.moveTo(firstIndex);
1887
+ cursor.endParse();
1888
+ return null;
1889
+ }
1890
+ else {
1891
+ cursor.endParse();
1892
+ return node;
1893
+ }
1894
+ }
1895
+ clone(name = this._name) {
1896
+ const optional = new Optional(name, this._children[0]);
1897
+ optional._id = this._id;
1898
+ return optional;
1899
+ }
1900
+ getTokens() {
1901
+ return this._children[0].getTokens();
1902
+ }
1903
+ getTokensAfter(_childReference) {
1904
+ const parent = this._parent;
1905
+ if (parent != null) {
1906
+ return parent.getTokensAfter(this);
1907
+ }
1908
+ return [];
1909
+ }
1910
+ getNextTokens() {
1911
+ if (this.parent == null) {
1912
+ return [];
1913
+ }
1914
+ return this.parent.getTokensAfter(this);
1915
+ }
1916
+ getPatterns() {
1917
+ return this._children[0].getPatterns();
1918
+ }
1919
+ getPatternsAfter(_childReference) {
1920
+ const parent = this._parent;
1921
+ if (parent != null) {
1922
+ return parent.getPatternsAfter(this);
1923
+ }
1924
+ return [];
1925
+ }
1926
+ getNextPatterns() {
1927
+ if (this.parent == null) {
1928
+ return [];
1929
+ }
1930
+ return this.parent.getPatternsAfter(this);
1931
+ }
1932
+ find(predicate) {
1933
+ return predicate(this._children[0]) ? this._children[0] : null;
1934
+ }
1935
+ isEqual(pattern) {
1936
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
1937
+ }
1938
+ }
1939
+
1863
1940
  const inlinePatternOpenParen = new Literal("anonymous-pattern-open-paren", "(");
1864
1941
  const inlinePatternCloseParen = new Literal("anonymous-pattern-close-paren", ")");
1865
- const optionalLineSpaces$1 = lineSpaces$1.clone(undefined, true);
1866
- const complexAnonymousPattern = new And("complex-anonymous-pattern", [
1942
+ const optionalLineSpaces$3 = new Optional("optional-line-spaces", lineSpaces$1);
1943
+ const complexAnonymousPattern = new Sequence("complex-anonymous-pattern", [
1867
1944
  inlinePatternOpenParen,
1868
- optionalLineSpaces$1,
1945
+ optionalLineSpaces$3,
1869
1946
  anonymousWrappedLiterals,
1870
- optionalLineSpaces$1,
1947
+ optionalLineSpaces$3,
1871
1948
  inlinePatternCloseParen,
1872
1949
  ]);
1873
- const anonymousPattern = new Or("anonymous-pattern", [
1950
+ const anonymousPattern = new Options("anonymous-pattern", [
1874
1951
  anonymousLiterals,
1875
1952
  complexAnonymousPattern
1876
1953
  ]);
1877
1954
 
1878
- const optionalSpaces$2 = spaces$1.clone("optional-spaces", true);
1955
+ const optionalSpaces$3 = new Optional("optional-spaces", spaces$1);
1879
1956
  const openBracket$1 = new Literal("open-bracket", "{");
1880
1957
  const closeBracket$1 = new Literal("close-bracket", "}");
1881
1958
  const comma = new Literal("comma", ",");
1882
1959
  const integer = new Regex("integer", "([1-9][0-9]*)|0");
1883
1960
  integer.setTokens(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
1884
- const optionalInteger = integer.clone("integer", true);
1885
- const trimKeyword = new Literal("trim-keyword", "trim", true);
1886
- const trimFlag = new And("trim-flag", [lineSpaces$1, trimKeyword], true);
1887
- const bounds = new And("bounds", [
1961
+ const min = new Optional("optional-min", integer.clone("min"));
1962
+ const max = new Optional("optional-max", integer.clone("max"));
1963
+ const trimKeyword = new Literal("trim-keyword", "trim");
1964
+ const trimFlag = new Optional("optional-trim-flag", new Sequence("trim-flag", [lineSpaces$1, trimKeyword]));
1965
+ const bounds = new Sequence("bounds", [
1888
1966
  openBracket$1,
1889
- optionalSpaces$2,
1890
- optionalInteger.clone("min"),
1891
- optionalSpaces$2,
1967
+ optionalSpaces$3,
1968
+ min,
1969
+ optionalSpaces$3,
1892
1970
  comma,
1893
- optionalSpaces$2,
1894
- optionalInteger.clone("max"),
1971
+ optionalSpaces$3,
1972
+ max,
1895
1973
  closeBracket$1
1896
1974
  ]);
1897
- const exactCount = new And("exact-count", [
1975
+ const exactCount = new Sequence("exact-count", [
1898
1976
  openBracket$1,
1899
- optionalSpaces$2,
1977
+ optionalSpaces$3,
1900
1978
  integer,
1901
- optionalSpaces$2,
1979
+ optionalSpaces$3,
1902
1980
  closeBracket$1,
1903
1981
  ]);
1904
1982
  const quantifierShorthand = new Regex("quantifier-shorthand", "\\*|\\+");
1905
1983
  quantifierShorthand.setTokens(["*", "+"]);
1906
- const quantifier = new Or("quantifier", [
1984
+ const quantifier = new Options("quantifier", [
1907
1985
  quantifierShorthand,
1908
1986
  exactCount,
1909
1987
  bounds
@@ -1913,127 +1991,134 @@ const closeParen = new Literal("repeat-close-paren", ")");
1913
1991
  const dividerComma = new Regex("divider-comma", "\\s*,\\s*");
1914
1992
  dividerComma.setTokens([", "]);
1915
1993
  const patternName$2 = name$1.clone("pattern-name");
1916
- const patterns$3 = new Or("or-patterns", [patternName$2, anonymousPattern]);
1994
+ const patterns$3 = new Options("or-patterns", [patternName$2, anonymousPattern]);
1917
1995
  const dividerPattern = patterns$3.clone("divider-pattern");
1918
- const repeatLiteral = new And("repeat-literal", [
1996
+ const dividerSection = new Sequence("divider-section", [dividerComma, dividerPattern, trimFlag]);
1997
+ const optionalDividerSection = new Optional("optional-divider-section", dividerSection);
1998
+ const repeatLiteral = new Sequence("repeat-literal", [
1919
1999
  openParen,
1920
- optionalSpaces$2,
2000
+ optionalSpaces$3,
1921
2001
  patterns$3,
1922
- new And("optional-divider-section", [dividerComma, dividerPattern, trimFlag], true),
1923
- optionalSpaces$2,
2002
+ optionalDividerSection,
2003
+ optionalSpaces$3,
1924
2004
  closeParen,
1925
- new And("quantifier-section", [quantifier]),
2005
+ new Sequence("quantifier-section", [quantifier]),
1926
2006
  ]);
1927
2007
 
1928
- const optionalNot = new Literal("not", "!", true);
1929
- const optionalIsOptional$1 = new Literal("is-optional", "?", true);
2008
+ const optionalNot = new Optional("optional-not", new Literal("not", "!"));
2009
+ const optionalIsOptional$1 = new Optional("optional-is-optional", new Literal("is-optional", "?"));
1930
2010
  const patternName$1 = name$1.clone("pattern-name");
1931
- const patterns$2 = new Or("and-patterns", [patternName$1, anonymousPattern]);
1932
- const pattern$1 = new And("and-child-pattern", [
2011
+ const patterns$2 = new Options("and-patterns", [patternName$1, anonymousPattern]);
2012
+ const pattern$1 = new Sequence("and-child-pattern", [
1933
2013
  optionalNot,
1934
2014
  patterns$2,
1935
2015
  optionalIsOptional$1,
1936
2016
  ]);
1937
2017
  const divider$1 = new Regex("and-divider", "\\s*[+]\\s*");
1938
2018
  divider$1.setTokens([" + "]);
1939
- const andLiteral = new Repeat("and-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
2019
+ const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
1940
2020
 
1941
2021
  const patternName = name$1.clone("pattern-name");
1942
- const patterns$1 = new Or("or-patterns", [patternName, anonymousPattern]);
2022
+ patternName.setTokens(["[PATTERN_NAME]"]);
2023
+ const patterns$1 = new Options("or-patterns", [patternName, anonymousPattern]);
1943
2024
  const defaultDivider = new Regex("default-divider", "\\s*[|]\\s*");
2025
+ defaultDivider.setTokens(["|"]);
1944
2026
  const greedyDivider = new Regex("greedy-divider", "\\s*[<][|][>]\\s*");
1945
- const divider = new Or("or-divider", [defaultDivider, greedyDivider]);
1946
- defaultDivider.setTokens([" | "]);
1947
- greedyDivider.setTokens([" <|> "]);
1948
- const orLiteral = new Repeat("or-literal", patterns$1, { divider, min: 2, trimDivider: true });
2027
+ greedyDivider.setTokens(["<|>"]);
2028
+ const divider = new Options("options-divider", [defaultDivider, greedyDivider]);
2029
+ const optionsLiteral = new Repeat("options-literal", patterns$1, { divider, min: 2, trimDivider: true });
1949
2030
 
1950
2031
  const aliasLiteral = name$1.clone("alias-literal");
1951
- const optionalIsOptional = new Literal("is-optional", "?", true);
1952
- const configurableAnonymousPattern = new And("configurable-anonymous-pattern", [anonymousPattern, optionalIsOptional]);
1953
- const pattern = new Or("pattern", [
2032
+ aliasLiteral.setTokens(["[ALIAS_LITERAL]"]);
2033
+ const optionalIsOptional = new Optional("optional-flag", new Literal("is-optional", "?"));
2034
+ const configurableAnonymousPattern = new Sequence("configurable-anonymous-pattern", [anonymousPattern, optionalIsOptional]);
2035
+ const pattern = new Options("pattern", [
1954
2036
  literal,
1955
2037
  regexLiteral,
1956
2038
  repeatLiteral,
1957
2039
  aliasLiteral,
1958
- orLiteral,
1959
- andLiteral,
2040
+ optionsLiteral,
2041
+ sequenceLiteral,
1960
2042
  configurableAnonymousPattern,
1961
- ], false, true);
2043
+ ], true);
1962
2044
 
1963
- const optionalSpaces$1 = spaces$1.clone("optional-spaces", true);
2045
+ const optionalSpaces$2 = new Optional("optional-spaces", spaces$1);
1964
2046
  const assignOperator = new Literal("assign-operator", "=");
1965
- const assignStatement = new And("assign-statement", [
1966
- optionalSpaces$1,
2047
+ const assignStatement = new Sequence("assign-statement", [
2048
+ optionalSpaces$2,
1967
2049
  name$1,
1968
- optionalSpaces$1,
2050
+ optionalSpaces$2,
1969
2051
  assignOperator,
1970
- optionalSpaces$1,
2052
+ optionalSpaces$2,
1971
2053
  pattern
1972
2054
  ]);
1973
- const statement = new Or("statement", [assignStatement, name$1.clone("export-name")]);
2055
+ const statement = new Options("statement", [assignStatement, name$1.clone("export-name")]);
1974
2056
 
1975
- const bodyLineContent = new Or("body-line-content", [
2057
+ const bodyLineContent = new Options("body-line-content", [
1976
2058
  comment,
1977
2059
  statement
1978
2060
  ]);
1979
- const bodyLine = new And("body-line", [
1980
- lineSpaces$1.clone("line-spaces", true),
2061
+ const optionalLineSpaces$2 = new Optional("optional-line-spaces", lineSpaces$1);
2062
+ const bodyLine = new Sequence("body-line", [
2063
+ optionalLineSpaces$2,
1981
2064
  bodyLineContent,
1982
- lineSpaces$1.clone("line-spaces", true),
2065
+ optionalLineSpaces$2,
1983
2066
  ]);
1984
2067
  const body = new Repeat("body", bodyLine, { divider: newLine$1, min: 0 });
1985
2068
 
1986
- const optionalSpaces = allSpaces.clone("optional-spaces", true);
1987
- const optionalLineSpaces = lineSpaces$1.clone("options-line-spaces", true);
2069
+ const optionalSpaces$1 = new Optional("optional-spaces", allSpaces);
2070
+ const optionalLineSpaces$1 = new Optional("options-line-spaces", lineSpaces$1);
1988
2071
  const importNameDivider = new Regex("import-name-divider", "(\\s+)?,(\\s+)?");
2072
+ importNameDivider.setTokens([", "]);
2073
+ const name = new Regex("import-name", "[^}\\s,]+");
2074
+ name.setTokens(["[IMPORT_NAME]"]);
1989
2075
  const importKeyword = new Literal("import", "import");
1990
2076
  const useParamsKeyword = new Literal("use-params", "use params");
1991
2077
  const asKeyword = new Literal("as", "as");
1992
2078
  const fromKeyword = new Literal("from", "from");
1993
2079
  const openBracket = new Literal("open-bracket", "{");
1994
2080
  const closeBracket = new Literal("close-bracket", "}");
1995
- const name = new Regex("import-name", "[^}\\s,]+");
1996
2081
  const importNameAlias = name.clone("import-name-alias");
1997
- const importAlias = new And("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
1998
- const importedNames = new Repeat("imported-names", new Or("import-names", [importAlias, name]), { divider: importNameDivider });
2082
+ const importAlias = new Sequence("import-alias", [name, lineSpaces$1, asKeyword, lineSpaces$1, importNameAlias]);
2083
+ const importedNames = new Repeat("imported-names", new Options("import-names", [importAlias, name]), { divider: importNameDivider });
1999
2084
  const paramName = name.clone("param-name");
2000
2085
  const paramNames = new Repeat("param-names", paramName, { divider: importNameDivider });
2001
2086
  const resource = literal.clone("resource");
2002
- const useParams = new And("import-params", [
2087
+ const useParams = new Sequence("import-params", [
2003
2088
  useParamsKeyword,
2004
- optionalLineSpaces,
2089
+ optionalLineSpaces$1,
2005
2090
  openBracket,
2006
- optionalSpaces,
2091
+ optionalSpaces$1,
2007
2092
  paramNames,
2008
- optionalSpaces,
2093
+ optionalSpaces$1,
2009
2094
  closeBracket
2010
2095
  ]);
2011
2096
  const withParamsKeyword = new Literal("with-params", "with params");
2012
- const withParamsStatement = new And("with-params-statement", [
2097
+ const withParamsStatement = new Optional("optional-with-params-statement", new Sequence("with-params-statement", [
2013
2098
  withParamsKeyword,
2014
- optionalLineSpaces,
2099
+ optionalLineSpaces$1,
2015
2100
  openBracket,
2016
- optionalSpaces,
2101
+ optionalSpaces$1,
2017
2102
  body.clone("with-params-body"),
2018
- optionalSpaces,
2103
+ optionalSpaces$1,
2019
2104
  closeBracket
2020
- ], true);
2021
- const importFromStatement = new And("import-from", [
2105
+ ]));
2106
+ const importFromStatement = new Sequence("import-from", [
2022
2107
  importKeyword,
2023
- optionalLineSpaces,
2108
+ optionalLineSpaces$1,
2024
2109
  openBracket,
2025
- optionalSpaces,
2110
+ optionalSpaces$1,
2026
2111
  importedNames,
2027
- optionalSpaces,
2112
+ optionalSpaces$1,
2028
2113
  closeBracket,
2029
- optionalLineSpaces,
2114
+ optionalLineSpaces$1,
2030
2115
  fromKeyword,
2031
- optionalLineSpaces,
2116
+ optionalLineSpaces$1,
2032
2117
  resource,
2033
- optionalLineSpaces,
2118
+ optionalLineSpaces$1,
2034
2119
  withParamsStatement
2035
2120
  ]);
2036
- const importStatement = new Or("import-statement", [
2121
+ const importStatement = new Options("import-statement", [
2037
2122
  useParams,
2038
2123
  importFromStatement
2039
2124
  ]);
@@ -2044,23 +2129,25 @@ const newLine = new Regex("new-line", "(\\r?\\n)+");
2044
2129
  spaces.setTokens([" "]);
2045
2130
  tabs.setTokens(["\t"]);
2046
2131
  newLine.setTokens(["\n"]);
2047
- const lineSpaces = new Repeat("line-spaces", new Or("line-space", [tabs, spaces]));
2048
- const headLineContent = new Or("head-line-content", [
2132
+ const lineSpaces = new Repeat("line-spaces", new Options("line-space", [tabs, spaces]));
2133
+ const optionalLineSpaces = new Optional("optional-line-spaces", lineSpaces);
2134
+ const headLineContent = new Options("head-line-content", [
2049
2135
  comment,
2050
2136
  importStatement
2051
2137
  ]);
2052
- const headLine = new And("head-line-content", [
2053
- lineSpaces.clone("line-spaces", true),
2138
+ const headLine = new Sequence("head-line-content", [
2139
+ optionalLineSpaces,
2054
2140
  headLineContent,
2055
- lineSpaces.clone("line-spaces", true),
2141
+ optionalLineSpaces,
2056
2142
  ]);
2057
- const head = new Repeat("head", headLine, { divider: newLine, min: 0 });
2058
- const grammar = new And("grammar", [
2059
- allSpaces,
2143
+ const head = new Optional("optional-head", new Repeat("head", headLine, { divider: newLine }));
2144
+ const optionalSpaces = new Optional("optional-spaces", allSpaces);
2145
+ const grammar = new Sequence("grammar", [
2146
+ optionalSpaces,
2060
2147
  head,
2061
- allSpaces,
2148
+ optionalSpaces,
2062
2149
  body,
2063
- allSpaces
2150
+ optionalSpaces
2064
2151
  ]);
2065
2152
 
2066
2153
  let idIndex = 0;
@@ -2091,7 +2178,7 @@ class Not {
2091
2178
  this._type = "not";
2092
2179
  this._name = name;
2093
2180
  this._parent = null;
2094
- this._children = [pattern.clone(pattern.name, false)];
2181
+ this._children = [pattern.clone()];
2095
2182
  this._children[0].parent = this;
2096
2183
  }
2097
2184
  test(text) {
@@ -2379,8 +2466,8 @@ let anonymousIndexId = 0;
2379
2466
  const patternNodes = {
2380
2467
  "literal": true,
2381
2468
  "regex-literal": true,
2382
- "or-literal": true,
2383
- "and-literal": true,
2469
+ "options-literal": true,
2470
+ "sequence-literal": true,
2384
2471
  "repeat-literal": true,
2385
2472
  "alias-literal": true,
2386
2473
  "configurable-anonymous-pattern": true
@@ -2483,12 +2570,12 @@ class Grammar {
2483
2570
  this._saveRegex(n);
2484
2571
  break;
2485
2572
  }
2486
- case "or-literal": {
2487
- this._saveOr(n);
2573
+ case "options-literal": {
2574
+ this._saveOptions(n);
2488
2575
  break;
2489
2576
  }
2490
- case "and-literal": {
2491
- this._saveAnd(n);
2577
+ case "sequence-literal": {
2578
+ this._saveSequence(n);
2492
2579
  break;
2493
2580
  }
2494
2581
  case "repeat-literal": {
@@ -2543,18 +2630,18 @@ class Grammar {
2543
2630
  const value = node.value.slice(1, node.value.length - 1);
2544
2631
  return new Regex(name, value);
2545
2632
  }
2546
- _saveOr(statementNode) {
2633
+ _saveOptions(statementNode) {
2547
2634
  const nameNode = statementNode.find(n => n.name === "name");
2548
2635
  const name = nameNode.value;
2549
- const orNode = statementNode.find(n => n.name === "or-literal");
2550
- const or = this._buildOr(name, orNode);
2551
- this._parseContext.patternsByName.set(name, or);
2636
+ const optionsNode = statementNode.find(n => n.name === "options-literal");
2637
+ const options = this._buildOptions(name, optionsNode);
2638
+ this._parseContext.patternsByName.set(name, options);
2552
2639
  }
2553
- _buildOr(name, node) {
2640
+ _buildOptions(name, node) {
2554
2641
  const patternNodes = node.children.filter(n => n.name !== "default-divider" && n.name !== "greedy-divider");
2555
2642
  const isGreedy = node.find(n => n.name === "greedy-divider") != null;
2556
2643
  const patterns = patternNodes.map(n => this._buildPattern(n));
2557
- const or = new Or(name, patterns, false, isGreedy);
2644
+ const or = new Options(name, patterns, isGreedy);
2558
2645
  return or;
2559
2646
  }
2560
2647
  _buildPattern(node) {
@@ -2573,11 +2660,11 @@ class Grammar {
2573
2660
  case "repeat-literal": {
2574
2661
  return this._buildRepeat(name, node);
2575
2662
  }
2576
- case "or-literal": {
2577
- return this._buildOr(name, node);
2663
+ case "options-literal": {
2664
+ return this._buildOptions(name, node);
2578
2665
  }
2579
- case "and-literal": {
2580
- return this._buildAnd(name, node);
2666
+ case "sequence-literal": {
2667
+ return this._buildSequence(name, node);
2581
2668
  }
2582
2669
  case "complex-anonymous-pattern": {
2583
2670
  return this._buildComplexAnonymousPattern(node);
@@ -2585,26 +2672,27 @@ class Grammar {
2585
2672
  }
2586
2673
  throw new Error(`Couldn't build node: ${node.name}.`);
2587
2674
  }
2588
- _saveAnd(statementNode) {
2675
+ _saveSequence(statementNode) {
2589
2676
  const nameNode = statementNode.find(n => n.name === "name");
2590
2677
  const name = nameNode.value;
2591
- const andNode = statementNode.find(n => n.name === "and-literal");
2592
- const and = this._buildAnd(name, andNode);
2593
- this._parseContext.patternsByName.set(name, and);
2678
+ const sequenceNode = statementNode.find(n => n.name === "sequence-literal");
2679
+ const sequence = this._buildSequence(name, sequenceNode);
2680
+ this._parseContext.patternsByName.set(name, sequence);
2594
2681
  }
2595
- _buildAnd(name, node) {
2682
+ _buildSequence(name, node) {
2596
2683
  const patternNodes = node.children.filter(n => n.name !== "and-divider");
2597
2684
  const patterns = patternNodes.map(n => {
2598
2685
  const patternNode = n.children[0].name === "not" ? n.children[1] : n.children[0];
2599
2686
  const isNot = n.find(n => n.name === "not") != null;
2600
2687
  const isOptional = n.find(n => n.name === "is-optional");
2601
- const pattern = this._buildPattern(patternNode).clone(undefined, isOptional == null ? undefined : true);
2688
+ const pattern = this._buildPattern(patternNode);
2689
+ const finalPattern = isOptional ? new Optional(pattern.name, pattern) : pattern;
2602
2690
  if (isNot) {
2603
- return new Not(`not-${pattern.name}`, pattern);
2691
+ return new Not(`not-${finalPattern.name}`, finalPattern);
2604
2692
  }
2605
- return pattern;
2693
+ return finalPattern;
2606
2694
  });
2607
- return new And(name, patterns);
2695
+ return new Sequence(name, patterns);
2608
2696
  }
2609
2697
  _saveRepeat(statementNode) {
2610
2698
  const nameNode = statementNode.find(n => n.name === "name");
@@ -2614,13 +2702,14 @@ class Grammar {
2614
2702
  this._parseContext.patternsByName.set(name, repeat);
2615
2703
  }
2616
2704
  _buildRepeat(name, repeatNode) {
2705
+ let isOptional = false;
2617
2706
  const bounds = repeatNode.find(n => n.name === "bounds");
2618
2707
  const exactCount = repeatNode.find(n => n.name === "exact-count");
2619
2708
  const quantifier = repeatNode.find(n => n.name === "quantifier-shorthand");
2620
2709
  const trimDivider = repeatNode.find(n => n.name === "trim-flag") != null;
2621
- const patterNode = repeatNode.children[1].type === "optional-spaces" ? repeatNode.children[2] : repeatNode.children[1];
2710
+ const patterNode = repeatNode.children[1].type === "spaces" ? repeatNode.children[2] : repeatNode.children[1];
2622
2711
  const pattern = this._buildPattern(patterNode);
2623
- const dividerSectionNode = repeatNode.find(n => n.name === "optional-divider-section");
2712
+ const dividerSectionNode = repeatNode.find(n => n.name === "divider-section");
2624
2713
  const options = {
2625
2714
  min: 1,
2626
2715
  max: Infinity
@@ -2653,18 +2742,17 @@ class Grammar {
2653
2742
  options.max = Infinity;
2654
2743
  }
2655
2744
  else {
2656
- options.min = 0;
2657
- options.max = Infinity;
2745
+ isOptional = true;
2658
2746
  }
2659
2747
  }
2660
- return new Repeat(name, pattern.clone(pattern.name), options);
2748
+ return isOptional ? new Optional(name, new Repeat(name, pattern, options)) : new Repeat(name, pattern, options);
2661
2749
  }
2662
2750
  _saveConfigurableAnonymous(node) {
2663
2751
  const nameNode = node.find(n => n.name === "name");
2664
2752
  const name = nameNode.value;
2665
2753
  const anonymousNode = node.find(n => n.name === "complex-anonymous-pattern");
2666
2754
  const isOptional = node.children[1] != null;
2667
- const anonymous = this._buildPattern(anonymousNode).clone(name, isOptional);
2755
+ const anonymous = isOptional ? new Optional(name, this._buildPattern(anonymousNode)) : this._buildPattern(anonymousNode);
2668
2756
  this._parseContext.patternsByName.set(name, anonymous);
2669
2757
  }
2670
2758
  _buildComplexAnonymousPattern(node) {
@@ -2807,7 +2895,6 @@ function patterns(strings, ...values) {
2807
2895
  return result;
2808
2896
  }
2809
2897
 
2810
- exports.And = And;
2811
2898
  exports.AutoComplete = AutoComplete;
2812
2899
  exports.Cursor = Cursor;
2813
2900
  exports.CursorHistory = CursorHistory;
@@ -2815,11 +2902,13 @@ exports.Grammar = Grammar;
2815
2902
  exports.Literal = Literal;
2816
2903
  exports.Node = Node;
2817
2904
  exports.Not = Not;
2818
- exports.Or = Or;
2905
+ exports.Optional = Optional;
2906
+ exports.Options = Options;
2819
2907
  exports.ParseError = ParseError;
2820
2908
  exports.Reference = Reference;
2821
2909
  exports.Regex = Regex;
2822
2910
  exports.Repeat = Repeat;
2911
+ exports.Sequence = Sequence;
2823
2912
  exports.arePatternsEqual = arePatternsEqual;
2824
2913
  exports.grammar = grammar;
2825
2914
  exports.patterns = patterns;