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