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