clarity-pattern-parser 4.0.1 → 4.0.3

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
@@ -898,7 +898,8 @@ class Recursive extends Pattern {
898
898
  if (pattern == null) {
899
899
  return false;
900
900
  }
901
- return pattern.name === this.name;
901
+ return (pattern.type !== "recursive" &&
902
+ pattern.name === this.name);
902
903
  });
903
904
  }
904
905
  climb(pattern, isMatch) {
@@ -944,26 +945,15 @@ class Recursive extends Pattern {
944
945
  }
945
946
  return new Recursive(name, isOptional);
946
947
  }
947
- getTokenValue() {
948
- var _a;
949
- return ((_a = this.getPattern()) === null || _a === void 0 ? void 0 : _a.getTokenValue()) || null;
950
- }
951
948
  getTokens() {
952
949
  var _a;
953
- if (!this.isRecursing) {
954
- this.isRecursing = true;
955
- const tokens = ((_a = this.getPattern()) === null || _a === void 0 ? void 0 : _a.getTokens()) || [];
956
- this.isRecursing = false;
957
- return tokens;
958
- }
959
- return [];
950
+ return ((_a = this.getPattern()) === null || _a === void 0 ? void 0 : _a.getTokens()) || [];
960
951
  }
961
952
  }
962
953
 
963
954
  class Reference extends Pattern {
964
955
  constructor(name, isOptional = false) {
965
956
  super("reference", name, [], isOptional);
966
- this.isRecursing = false;
967
957
  }
968
958
  getRoot() {
969
959
  let node = this.parent;
@@ -1034,9 +1024,6 @@ class Reference extends Pattern {
1034
1024
  }
1035
1025
  return new Reference(name, isOptional);
1036
1026
  }
1037
- getTokenValue() {
1038
- return this.safelyGetPattern().getTokenValue();
1039
- }
1040
1027
  safelyGetPattern() {
1041
1028
  let pattern = this.children[0];
1042
1029
  const hasNoPattern = pattern == null;
@@ -1051,14 +1038,212 @@ class Reference extends Pattern {
1051
1038
  return pattern;
1052
1039
  }
1053
1040
  getTokens() {
1054
- if (!this.isRecursing) {
1055
- this.isRecursing = true;
1056
- let pattern = this.safelyGetPattern();
1057
- const tokens = pattern.getTokens();
1058
- this.isRecursing = false;
1059
- return tokens;
1041
+ return this.safelyGetPattern().getTokens();
1042
+ }
1043
+ }
1044
+
1045
+ class Visitor {
1046
+ constructor(root = null, selectedNodes = []) {
1047
+ this.root = root;
1048
+ this.selectedNodes = selectedNodes;
1049
+ }
1050
+ flatten() {
1051
+ this.selectedNodes.forEach((node) => {
1052
+ if (node.children.length > 0) {
1053
+ const children = [];
1054
+ Visitor.walkUp(node, (descendant) => {
1055
+ if (descendant.children.length === 0) {
1056
+ children.push(descendant);
1057
+ }
1058
+ });
1059
+ node.children = children;
1060
+ }
1061
+ });
1062
+ return this;
1063
+ }
1064
+ remove() {
1065
+ if (this.root == null) {
1066
+ return this;
1060
1067
  }
1061
- return [];
1068
+ this.recursiveRemove(this.root);
1069
+ return this;
1070
+ }
1071
+ recursiveRemove(node) {
1072
+ const nodesToRemove = this.selectedNodes;
1073
+ for (let x = 0; x < node.children.length; x++) {
1074
+ if (nodesToRemove.indexOf(node.children[x]) > -1) {
1075
+ node.children.splice(x, 1);
1076
+ x--;
1077
+ }
1078
+ else {
1079
+ this.recursiveRemove(node.children[x]);
1080
+ }
1081
+ }
1082
+ }
1083
+ wrap(callback) {
1084
+ const visitor = new Visitor(this.root);
1085
+ visitor.selectRoot().transform((node) => {
1086
+ if (this.selectedNodes.includes(node)) {
1087
+ return callback(node);
1088
+ }
1089
+ return node;
1090
+ });
1091
+ return this;
1092
+ }
1093
+ unwrap() {
1094
+ if (this.root == null) {
1095
+ return this;
1096
+ }
1097
+ Visitor.walkDown(this.root, (node, stack) => {
1098
+ if (this.selectedNodes.includes(node)) {
1099
+ const parent = stack[stack.length - 1];
1100
+ const grandParent = stack[stack.length - 2];
1101
+ if (parent != null && grandParent != null) {
1102
+ const index = grandParent.children.indexOf(parent);
1103
+ if (index > -1) {
1104
+ grandParent.children.splice(index, 1, ...parent.children);
1105
+ }
1106
+ }
1107
+ }
1108
+ });
1109
+ return this;
1110
+ }
1111
+ prepend(callback) {
1112
+ if (this.root == null) {
1113
+ return this;
1114
+ }
1115
+ Visitor.walkUp(this.root, (node, stack) => {
1116
+ if (this.selectedNodes.includes(node)) {
1117
+ const parent = stack[stack.length - 1];
1118
+ if (parent != null) {
1119
+ const index = parent.children.indexOf(node);
1120
+ if (index > -1) {
1121
+ parent.children.splice(index, 0, callback(node));
1122
+ }
1123
+ }
1124
+ }
1125
+ });
1126
+ return this;
1127
+ }
1128
+ append(callback) {
1129
+ if (this.root == null) {
1130
+ return this;
1131
+ }
1132
+ Visitor.walkDown(this.root, (node, stack) => {
1133
+ if (this.selectedNodes.includes(node)) {
1134
+ const parent = stack[stack.length - 1];
1135
+ if (parent != null) {
1136
+ const index = parent.children.indexOf(node);
1137
+ if (index > -1) {
1138
+ parent.children.splice(index + 1, 0, callback(node));
1139
+ }
1140
+ }
1141
+ }
1142
+ });
1143
+ return this;
1144
+ }
1145
+ transform(callback) {
1146
+ this.selectedNodes.forEach((node) => {
1147
+ return this.recursiveTransform(node, callback);
1148
+ });
1149
+ return this;
1150
+ }
1151
+ recursiveTransform(node, callback) {
1152
+ const length = node.children.length;
1153
+ for (let x = 0; x < length; x++) {
1154
+ node.children[x] = this.recursiveTransform(node.children[x], callback);
1155
+ }
1156
+ return callback(node);
1157
+ }
1158
+ selectAll() {
1159
+ return this.select((n) => true);
1160
+ }
1161
+ selectNode(node) {
1162
+ return new Visitor(this.root, [...this.selectedNodes, node]);
1163
+ }
1164
+ deselectNode(node) {
1165
+ const visitor = new Visitor(this.root, this.selectedNodes.slice());
1166
+ return visitor.filter((n) => n !== node);
1167
+ }
1168
+ select(callback) {
1169
+ if (this.root == null) {
1170
+ return this;
1171
+ }
1172
+ const node = this.root;
1173
+ const selectedNodes = [];
1174
+ if (node.children.length > 0) {
1175
+ Visitor.walkDown(node, (descendant) => {
1176
+ if (callback(descendant)) {
1177
+ selectedNodes.push(descendant);
1178
+ }
1179
+ });
1180
+ }
1181
+ return new Visitor(this.root, selectedNodes);
1182
+ }
1183
+ forEach(callback) {
1184
+ this.selectedNodes.forEach(callback);
1185
+ return this;
1186
+ }
1187
+ filter(callback) {
1188
+ return new Visitor(this.root, this.selectedNodes.filter(callback));
1189
+ }
1190
+ map(callback) {
1191
+ return new Visitor(this.root, this.selectedNodes.map(callback));
1192
+ }
1193
+ selectRoot() {
1194
+ if (this.root == null) {
1195
+ return this;
1196
+ }
1197
+ return new Visitor(this.root, [this.root]);
1198
+ }
1199
+ first() {
1200
+ return this.get(0);
1201
+ }
1202
+ last() {
1203
+ return this.get(this.selectedNodes.length - 1);
1204
+ }
1205
+ get(index) {
1206
+ const node = this.selectedNodes[index];
1207
+ if (node == null) {
1208
+ throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`);
1209
+ }
1210
+ return new Visitor(node, []);
1211
+ }
1212
+ clear() {
1213
+ this.selectedNodes = [];
1214
+ return this;
1215
+ }
1216
+ setRoot(root) {
1217
+ this.root = root;
1218
+ return this;
1219
+ }
1220
+ static select(root, callback) {
1221
+ if (callback != null) {
1222
+ return new Visitor(root).select(callback);
1223
+ }
1224
+ else {
1225
+ return new Visitor(root);
1226
+ }
1227
+ }
1228
+ static walkUp(node, callback, ancestors = []) {
1229
+ ancestors.push(node);
1230
+ if (node.children.length > 0) {
1231
+ const children = node.children.slice();
1232
+ children.forEach((c) => this.walkUp(c, callback, ancestors));
1233
+ }
1234
+ ancestors.pop();
1235
+ callback(node, ancestors);
1236
+ return this;
1237
+ }
1238
+ static walkDown(node, callback, ancestors = []) {
1239
+ callback(node, ancestors);
1240
+ ancestors.push(node);
1241
+ if (node.children.length > 0) {
1242
+ const children = node.children.slice();
1243
+ children.forEach((c) => this.walkDown(c, callback, ancestors));
1244
+ }
1245
+ ancestors.pop();
1246
+ return this;
1062
1247
  }
1063
1248
  }
1064
1249
 
@@ -1205,7 +1390,7 @@ class TextSuggester {
1205
1390
  if (noMatch) {
1206
1391
  let options = (_d = this.rootPattern) === null || _d === void 0 ? void 0 : _d.getTokens();
1207
1392
  options = options === null || options === void 0 ? void 0 : options.filter((option) => {
1208
- return option.indexOf(this.text) > -1;
1393
+ return option.indexOf(this.text) === 0;
1209
1394
  });
1210
1395
  if ((options === null || options === void 0 ? void 0 : options.length) === 0) {
1211
1396
  this.tokens = null;
@@ -1306,211 +1491,6 @@ class TextSuggester {
1306
1491
  }
1307
1492
  }
1308
1493
 
1309
- class Visitor {
1310
- constructor(root = null, selectedNodes = []) {
1311
- this.root = root;
1312
- this.selectedNodes = selectedNodes;
1313
- }
1314
- flatten() {
1315
- this.selectedNodes.forEach((node) => {
1316
- if (node.children.length > 0) {
1317
- const children = [];
1318
- Visitor.walkUp(node, (descendant) => {
1319
- if (descendant.children.length === 0) {
1320
- children.push(descendant);
1321
- }
1322
- });
1323
- node.children = children;
1324
- }
1325
- });
1326
- return this;
1327
- }
1328
- remove() {
1329
- if (this.root == null) {
1330
- return this;
1331
- }
1332
- this.recursiveRemove(this.root);
1333
- return this;
1334
- }
1335
- recursiveRemove(node) {
1336
- const nodesToRemove = this.selectedNodes;
1337
- for (let x = 0; x < node.children.length; x++) {
1338
- if (nodesToRemove.indexOf(node.children[x]) > -1) {
1339
- node.children.splice(x, 1);
1340
- x--;
1341
- }
1342
- else {
1343
- this.recursiveRemove(node.children[x]);
1344
- }
1345
- }
1346
- }
1347
- wrap(callback) {
1348
- const visitor = new Visitor(this.root);
1349
- visitor.selectRoot().transform((node) => {
1350
- if (this.selectedNodes.includes(node)) {
1351
- return callback(node);
1352
- }
1353
- return node;
1354
- });
1355
- return this;
1356
- }
1357
- unwrap() {
1358
- if (this.root == null) {
1359
- return this;
1360
- }
1361
- Visitor.walkDown(this.root, (node, stack) => {
1362
- if (this.selectedNodes.includes(node)) {
1363
- const parent = stack[stack.length - 1];
1364
- const grandParent = stack[stack.length - 2];
1365
- if (parent != null && grandParent != null) {
1366
- const index = grandParent.children.indexOf(parent);
1367
- if (index > -1) {
1368
- grandParent.children.splice(index, 1, ...parent.children);
1369
- }
1370
- }
1371
- }
1372
- });
1373
- return this;
1374
- }
1375
- prepend(callback) {
1376
- if (this.root == null) {
1377
- return this;
1378
- }
1379
- Visitor.walkUp(this.root, (node, stack) => {
1380
- if (this.selectedNodes.includes(node)) {
1381
- const parent = stack[stack.length - 1];
1382
- if (parent != null) {
1383
- const index = parent.children.indexOf(node);
1384
- if (index > -1) {
1385
- parent.children.splice(index, 0, callback(node));
1386
- }
1387
- }
1388
- }
1389
- });
1390
- return this;
1391
- }
1392
- append(callback) {
1393
- if (this.root == null) {
1394
- return this;
1395
- }
1396
- Visitor.walkDown(this.root, (node, stack) => {
1397
- if (this.selectedNodes.includes(node)) {
1398
- const parent = stack[stack.length - 1];
1399
- if (parent != null) {
1400
- const index = parent.children.indexOf(node);
1401
- if (index > -1) {
1402
- parent.children.splice(index + 1, 0, callback(node));
1403
- }
1404
- }
1405
- }
1406
- });
1407
- return this;
1408
- }
1409
- transform(callback) {
1410
- this.selectedNodes.forEach((node) => {
1411
- return this.recursiveTransform(node, callback);
1412
- });
1413
- return this;
1414
- }
1415
- recursiveTransform(node, callback) {
1416
- const length = node.children.length;
1417
- for (let x = 0; x < length; x++) {
1418
- node.children[x] = this.recursiveTransform(node.children[x], callback);
1419
- }
1420
- return callback(node);
1421
- }
1422
- selectAll() {
1423
- return this.select((n) => true);
1424
- }
1425
- selectNode(node) {
1426
- return new Visitor(this.root, [...this.selectedNodes, node]);
1427
- }
1428
- deselectNode(node) {
1429
- const visitor = new Visitor(this.root, this.selectedNodes.slice());
1430
- return visitor.filter((n) => n !== node);
1431
- }
1432
- select(callback) {
1433
- if (this.root == null) {
1434
- return this;
1435
- }
1436
- const node = this.root;
1437
- const selectedNodes = [];
1438
- if (node.children.length > 0) {
1439
- Visitor.walkDown(node, (descendant) => {
1440
- if (callback(descendant)) {
1441
- selectedNodes.push(descendant);
1442
- }
1443
- });
1444
- }
1445
- return new Visitor(this.root, selectedNodes);
1446
- }
1447
- forEach(callback) {
1448
- this.selectedNodes.forEach(callback);
1449
- return this;
1450
- }
1451
- filter(callback) {
1452
- return new Visitor(this.root, this.selectedNodes.filter(callback));
1453
- }
1454
- map(callback) {
1455
- return new Visitor(this.root, this.selectedNodes.map(callback));
1456
- }
1457
- selectRoot() {
1458
- if (this.root == null) {
1459
- return this;
1460
- }
1461
- return new Visitor(this.root, [this.root]);
1462
- }
1463
- first() {
1464
- return this.get(0);
1465
- }
1466
- last() {
1467
- return this.get(this.selectedNodes.length - 1);
1468
- }
1469
- get(index) {
1470
- const node = this.selectedNodes[index];
1471
- if (node == null) {
1472
- throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`);
1473
- }
1474
- return new Visitor(node, []);
1475
- }
1476
- clear() {
1477
- this.selectedNodes = [];
1478
- return this;
1479
- }
1480
- setRoot(root) {
1481
- this.root = root;
1482
- return this;
1483
- }
1484
- static select(root, callback) {
1485
- if (callback != null) {
1486
- return new Visitor(root).select(callback);
1487
- }
1488
- else {
1489
- return new Visitor(root);
1490
- }
1491
- }
1492
- static walkUp(node, callback, ancestors = []) {
1493
- ancestors.push(node);
1494
- if (node.children.length > 0) {
1495
- const children = node.children.slice();
1496
- children.forEach((c) => this.walkUp(c, callback, ancestors));
1497
- }
1498
- ancestors.pop();
1499
- callback(node, ancestors);
1500
- return this;
1501
- }
1502
- static walkDown(node, callback, ancestors = []) {
1503
- callback(node, ancestors);
1504
- ancestors.push(node);
1505
- if (node.children.length > 0) {
1506
- const children = node.children.slice();
1507
- children.forEach((c) => this.walkDown(c, callback, ancestors));
1508
- }
1509
- ancestors.pop();
1510
- return this;
1511
- }
1512
- }
1513
-
1514
1494
  exports.And = And;
1515
1495
  exports.Cursor = Cursor;
1516
1496
  exports.Literal = Literal;