clarity-pattern-parser 4.0.2 → 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 +205 -205
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.esm.js +205 -205
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +205 -205
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +7 -2
package/dist/index.esm.js
CHANGED
|
@@ -1038,6 +1038,211 @@ class Reference extends Pattern {
|
|
|
1038
1038
|
}
|
|
1039
1039
|
}
|
|
1040
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;
|
|
1063
|
+
}
|
|
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;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1041
1246
|
class TextSuggester {
|
|
1042
1247
|
constructor() {
|
|
1043
1248
|
this.cursor = null;
|
|
@@ -1282,210 +1487,5 @@ class TextSuggester {
|
|
|
1282
1487
|
}
|
|
1283
1488
|
}
|
|
1284
1489
|
|
|
1285
|
-
class Visitor {
|
|
1286
|
-
constructor(root = null, selectedNodes = []) {
|
|
1287
|
-
this.root = root;
|
|
1288
|
-
this.selectedNodes = selectedNodes;
|
|
1289
|
-
}
|
|
1290
|
-
flatten() {
|
|
1291
|
-
this.selectedNodes.forEach((node) => {
|
|
1292
|
-
if (node.children.length > 0) {
|
|
1293
|
-
const children = [];
|
|
1294
|
-
Visitor.walkUp(node, (descendant) => {
|
|
1295
|
-
if (descendant.children.length === 0) {
|
|
1296
|
-
children.push(descendant);
|
|
1297
|
-
}
|
|
1298
|
-
});
|
|
1299
|
-
node.children = children;
|
|
1300
|
-
}
|
|
1301
|
-
});
|
|
1302
|
-
return this;
|
|
1303
|
-
}
|
|
1304
|
-
remove() {
|
|
1305
|
-
if (this.root == null) {
|
|
1306
|
-
return this;
|
|
1307
|
-
}
|
|
1308
|
-
this.recursiveRemove(this.root);
|
|
1309
|
-
return this;
|
|
1310
|
-
}
|
|
1311
|
-
recursiveRemove(node) {
|
|
1312
|
-
const nodesToRemove = this.selectedNodes;
|
|
1313
|
-
for (let x = 0; x < node.children.length; x++) {
|
|
1314
|
-
if (nodesToRemove.indexOf(node.children[x]) > -1) {
|
|
1315
|
-
node.children.splice(x, 1);
|
|
1316
|
-
x--;
|
|
1317
|
-
}
|
|
1318
|
-
else {
|
|
1319
|
-
this.recursiveRemove(node.children[x]);
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
1323
|
-
wrap(callback) {
|
|
1324
|
-
const visitor = new Visitor(this.root);
|
|
1325
|
-
visitor.selectRoot().transform((node) => {
|
|
1326
|
-
if (this.selectedNodes.includes(node)) {
|
|
1327
|
-
return callback(node);
|
|
1328
|
-
}
|
|
1329
|
-
return node;
|
|
1330
|
-
});
|
|
1331
|
-
return this;
|
|
1332
|
-
}
|
|
1333
|
-
unwrap() {
|
|
1334
|
-
if (this.root == null) {
|
|
1335
|
-
return this;
|
|
1336
|
-
}
|
|
1337
|
-
Visitor.walkDown(this.root, (node, stack) => {
|
|
1338
|
-
if (this.selectedNodes.includes(node)) {
|
|
1339
|
-
const parent = stack[stack.length - 1];
|
|
1340
|
-
const grandParent = stack[stack.length - 2];
|
|
1341
|
-
if (parent != null && grandParent != null) {
|
|
1342
|
-
const index = grandParent.children.indexOf(parent);
|
|
1343
|
-
if (index > -1) {
|
|
1344
|
-
grandParent.children.splice(index, 1, ...parent.children);
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
});
|
|
1349
|
-
return this;
|
|
1350
|
-
}
|
|
1351
|
-
prepend(callback) {
|
|
1352
|
-
if (this.root == null) {
|
|
1353
|
-
return this;
|
|
1354
|
-
}
|
|
1355
|
-
Visitor.walkUp(this.root, (node, stack) => {
|
|
1356
|
-
if (this.selectedNodes.includes(node)) {
|
|
1357
|
-
const parent = stack[stack.length - 1];
|
|
1358
|
-
if (parent != null) {
|
|
1359
|
-
const index = parent.children.indexOf(node);
|
|
1360
|
-
if (index > -1) {
|
|
1361
|
-
parent.children.splice(index, 0, callback(node));
|
|
1362
|
-
}
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
});
|
|
1366
|
-
return this;
|
|
1367
|
-
}
|
|
1368
|
-
append(callback) {
|
|
1369
|
-
if (this.root == null) {
|
|
1370
|
-
return this;
|
|
1371
|
-
}
|
|
1372
|
-
Visitor.walkDown(this.root, (node, stack) => {
|
|
1373
|
-
if (this.selectedNodes.includes(node)) {
|
|
1374
|
-
const parent = stack[stack.length - 1];
|
|
1375
|
-
if (parent != null) {
|
|
1376
|
-
const index = parent.children.indexOf(node);
|
|
1377
|
-
if (index > -1) {
|
|
1378
|
-
parent.children.splice(index + 1, 0, callback(node));
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
|
-
}
|
|
1382
|
-
});
|
|
1383
|
-
return this;
|
|
1384
|
-
}
|
|
1385
|
-
transform(callback) {
|
|
1386
|
-
this.selectedNodes.forEach((node) => {
|
|
1387
|
-
return this.recursiveTransform(node, callback);
|
|
1388
|
-
});
|
|
1389
|
-
return this;
|
|
1390
|
-
}
|
|
1391
|
-
recursiveTransform(node, callback) {
|
|
1392
|
-
const length = node.children.length;
|
|
1393
|
-
for (let x = 0; x < length; x++) {
|
|
1394
|
-
node.children[x] = this.recursiveTransform(node.children[x], callback);
|
|
1395
|
-
}
|
|
1396
|
-
return callback(node);
|
|
1397
|
-
}
|
|
1398
|
-
selectAll() {
|
|
1399
|
-
return this.select((n) => true);
|
|
1400
|
-
}
|
|
1401
|
-
selectNode(node) {
|
|
1402
|
-
return new Visitor(this.root, [...this.selectedNodes, node]);
|
|
1403
|
-
}
|
|
1404
|
-
deselectNode(node) {
|
|
1405
|
-
const visitor = new Visitor(this.root, this.selectedNodes.slice());
|
|
1406
|
-
return visitor.filter((n) => n !== node);
|
|
1407
|
-
}
|
|
1408
|
-
select(callback) {
|
|
1409
|
-
if (this.root == null) {
|
|
1410
|
-
return this;
|
|
1411
|
-
}
|
|
1412
|
-
const node = this.root;
|
|
1413
|
-
const selectedNodes = [];
|
|
1414
|
-
if (node.children.length > 0) {
|
|
1415
|
-
Visitor.walkDown(node, (descendant) => {
|
|
1416
|
-
if (callback(descendant)) {
|
|
1417
|
-
selectedNodes.push(descendant);
|
|
1418
|
-
}
|
|
1419
|
-
});
|
|
1420
|
-
}
|
|
1421
|
-
return new Visitor(this.root, selectedNodes);
|
|
1422
|
-
}
|
|
1423
|
-
forEach(callback) {
|
|
1424
|
-
this.selectedNodes.forEach(callback);
|
|
1425
|
-
return this;
|
|
1426
|
-
}
|
|
1427
|
-
filter(callback) {
|
|
1428
|
-
return new Visitor(this.root, this.selectedNodes.filter(callback));
|
|
1429
|
-
}
|
|
1430
|
-
map(callback) {
|
|
1431
|
-
return new Visitor(this.root, this.selectedNodes.map(callback));
|
|
1432
|
-
}
|
|
1433
|
-
selectRoot() {
|
|
1434
|
-
if (this.root == null) {
|
|
1435
|
-
return this;
|
|
1436
|
-
}
|
|
1437
|
-
return new Visitor(this.root, [this.root]);
|
|
1438
|
-
}
|
|
1439
|
-
first() {
|
|
1440
|
-
return this.get(0);
|
|
1441
|
-
}
|
|
1442
|
-
last() {
|
|
1443
|
-
return this.get(this.selectedNodes.length - 1);
|
|
1444
|
-
}
|
|
1445
|
-
get(index) {
|
|
1446
|
-
const node = this.selectedNodes[index];
|
|
1447
|
-
if (node == null) {
|
|
1448
|
-
throw new Error(`Couldn't find node at index: ${index}, out of ${this.selectedNodes.length}.`);
|
|
1449
|
-
}
|
|
1450
|
-
return new Visitor(node, []);
|
|
1451
|
-
}
|
|
1452
|
-
clear() {
|
|
1453
|
-
this.selectedNodes = [];
|
|
1454
|
-
return this;
|
|
1455
|
-
}
|
|
1456
|
-
setRoot(root) {
|
|
1457
|
-
this.root = root;
|
|
1458
|
-
return this;
|
|
1459
|
-
}
|
|
1460
|
-
static select(root, callback) {
|
|
1461
|
-
if (callback != null) {
|
|
1462
|
-
return new Visitor(root).select(callback);
|
|
1463
|
-
}
|
|
1464
|
-
else {
|
|
1465
|
-
return new Visitor(root);
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
static walkUp(node, callback, ancestors = []) {
|
|
1469
|
-
ancestors.push(node);
|
|
1470
|
-
if (node.children.length > 0) {
|
|
1471
|
-
const children = node.children.slice();
|
|
1472
|
-
children.forEach((c) => this.walkUp(c, callback, ancestors));
|
|
1473
|
-
}
|
|
1474
|
-
ancestors.pop();
|
|
1475
|
-
callback(node, ancestors);
|
|
1476
|
-
return this;
|
|
1477
|
-
}
|
|
1478
|
-
static walkDown(node, callback, ancestors = []) {
|
|
1479
|
-
callback(node, ancestors);
|
|
1480
|
-
ancestors.push(node);
|
|
1481
|
-
if (node.children.length > 0) {
|
|
1482
|
-
const children = node.children.slice();
|
|
1483
|
-
children.forEach((c) => this.walkDown(c, callback, ancestors));
|
|
1484
|
-
}
|
|
1485
|
-
ancestors.pop();
|
|
1486
|
-
return this;
|
|
1487
|
-
}
|
|
1488
|
-
}
|
|
1489
|
-
|
|
1490
1490
|
export { And, Cursor, Literal, LookAhead, Node, Not, Or, ParseError, Pattern, Recursive, Reference, Regex, Repeat, TextSuggester, Visitor };
|
|
1491
1491
|
//# sourceMappingURL=index.esm.js.map
|