@bcts/envelope-pattern 1.0.0-alpha.17 → 1.0.0-alpha.18
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/README.md +1 -1
- package/dist/index.cjs +1679 -1401
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +54 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +1680 -1403
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +1670 -1402
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
- package/src/format.ts +23 -4
- package/src/parse/index.ts +138 -5
- package/src/parse/token.ts +4 -3
- package/src/pattern/index.ts +110 -2
- package/src/pattern/leaf/array-pattern.ts +1 -1
- package/src/pattern/leaf/bool-pattern.ts +1 -1
- package/src/pattern/leaf/byte-string-pattern.ts +1 -1
- package/src/pattern/leaf/cbor-pattern.ts +2 -2
- package/src/pattern/leaf/date-pattern.ts +1 -1
- package/src/pattern/leaf/index.ts +1 -2
- package/src/pattern/leaf/known-value-pattern.ts +4 -3
- package/src/pattern/leaf/map-pattern.ts +1 -1
- package/src/pattern/leaf/null-pattern.ts +1 -1
- package/src/pattern/leaf/number-pattern.ts +1 -1
- package/src/pattern/leaf/text-pattern.ts +1 -1
- package/src/pattern/matcher.ts +88 -3
- package/src/pattern/meta/and-pattern.ts +10 -9
- package/src/pattern/meta/capture-pattern.ts +5 -6
- package/src/pattern/meta/group-pattern.ts +9 -6
- package/src/pattern/meta/not-pattern.ts +3 -2
- package/src/pattern/meta/or-pattern.ts +18 -13
- package/src/pattern/meta/search-pattern.ts +4 -4
- package/src/pattern/meta/traverse-pattern.ts +31 -7
- package/src/pattern/structure/assertions-pattern.ts +7 -8
- package/src/pattern/structure/index.ts +1 -0
- package/src/pattern/structure/object-pattern.ts +2 -3
- package/src/pattern/structure/predicate-pattern.ts +2 -3
- package/src/pattern/structure/subject-pattern.ts +2 -3
- package/src/pattern/structure/wrapped-pattern.ts +28 -7
- package/src/pattern/vm.ts +12 -11
package/dist/index.iife.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_values, _bcts_envelope, _bcts_dcbor) {
|
|
1
|
+
var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_values, _bcts_envelope, _bcts_dcbor, _bcts_dcbor_parse) {
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
//#region src/error.ts
|
|
@@ -312,9 +312,17 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
312
312
|
const c = env.case();
|
|
313
313
|
let summary;
|
|
314
314
|
switch (c.type) {
|
|
315
|
-
case "node":
|
|
316
|
-
|
|
315
|
+
case "node": {
|
|
316
|
+
const subjectSummary = env.subject().summary(Number.MAX_SAFE_INTEGER);
|
|
317
|
+
const assertions = env.assertions();
|
|
318
|
+
if (assertions.length > 0) summary = `NODE ${subjectSummary} [ ${assertions.map((a) => {
|
|
319
|
+
const ac = a.case();
|
|
320
|
+
if (ac.type === "assertion") return `${ac.assertion.predicate().summary(Number.MAX_SAFE_INTEGER)}: ${ac.assertion.object().summary(Number.MAX_SAFE_INTEGER)}`;
|
|
321
|
+
return a.summary(Number.MAX_SAFE_INTEGER);
|
|
322
|
+
}).join(", ")} ]`;
|
|
323
|
+
else summary = `NODE ${subjectSummary}`;
|
|
317
324
|
break;
|
|
325
|
+
}
|
|
318
326
|
case "leaf":
|
|
319
327
|
summary = `LEAF ${env.summary(Number.MAX_SAFE_INTEGER)}`;
|
|
320
328
|
break;
|
|
@@ -322,7 +330,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
322
330
|
summary = `WRAPPED ${env.summary(Number.MAX_SAFE_INTEGER)}`;
|
|
323
331
|
break;
|
|
324
332
|
case "assertion":
|
|
325
|
-
summary = `ASSERTION ${
|
|
333
|
+
summary = `ASSERTION ${c.assertion.predicate().summary(Number.MAX_SAFE_INTEGER)}: ${c.assertion.object().summary(Number.MAX_SAFE_INTEGER)}`;
|
|
326
334
|
break;
|
|
327
335
|
case "elided":
|
|
328
336
|
summary = "ELIDED";
|
|
@@ -496,10 +504,15 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
496
504
|
});
|
|
497
505
|
}
|
|
498
506
|
/**
|
|
499
|
-
* Registry for pattern
|
|
500
|
-
* This allows meta patterns to
|
|
507
|
+
* Registry for pattern dispatch functions to break circular dependencies.
|
|
508
|
+
* This allows meta patterns to dispatch to child patterns without importing from index.ts.
|
|
501
509
|
*/
|
|
502
510
|
let patternMatchFn;
|
|
511
|
+
let patternPathsWithCapturesFn;
|
|
512
|
+
let patternPathsFn;
|
|
513
|
+
let patternCompileFn;
|
|
514
|
+
let patternIsComplexFn;
|
|
515
|
+
let patternToStringFn;
|
|
503
516
|
/**
|
|
504
517
|
* Registers the pattern match function.
|
|
505
518
|
* Called from index.ts after all patterns are defined.
|
|
@@ -508,6 +521,17 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
508
521
|
patternMatchFn = fn;
|
|
509
522
|
}
|
|
510
523
|
/**
|
|
524
|
+
* Registers all pattern dispatch functions.
|
|
525
|
+
* Called from index.ts after all patterns are defined.
|
|
526
|
+
*/
|
|
527
|
+
function registerPatternDispatchFns(fns) {
|
|
528
|
+
patternPathsWithCapturesFn = fns.pathsWithCaptures;
|
|
529
|
+
patternPathsFn = fns.paths;
|
|
530
|
+
patternCompileFn = fns.compile;
|
|
531
|
+
patternIsComplexFn = fns.isComplex;
|
|
532
|
+
patternToStringFn = fns.toString;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
511
535
|
* Match a pattern against an envelope using the registered match function.
|
|
512
536
|
* Used by meta patterns to match child patterns.
|
|
513
537
|
*/
|
|
@@ -515,6 +539,41 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
515
539
|
if (patternMatchFn === void 0) throw new Error("Pattern match function not registered");
|
|
516
540
|
return patternMatchFn(pattern, haystack);
|
|
517
541
|
}
|
|
542
|
+
/**
|
|
543
|
+
* Dispatch pathsWithCaptures on a Pattern.
|
|
544
|
+
*/
|
|
545
|
+
function dispatchPathsWithCaptures(pattern, haystack) {
|
|
546
|
+
if (patternPathsWithCapturesFn === void 0) throw new Error("Pattern dispatch functions not registered");
|
|
547
|
+
return patternPathsWithCapturesFn(pattern, haystack);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Dispatch paths on a Pattern.
|
|
551
|
+
*/
|
|
552
|
+
function dispatchPaths(pattern, haystack) {
|
|
553
|
+
if (patternPathsFn === void 0) throw new Error("Pattern dispatch functions not registered");
|
|
554
|
+
return patternPathsFn(pattern, haystack);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Dispatch compile on a Pattern.
|
|
558
|
+
*/
|
|
559
|
+
function dispatchCompile(pattern, code, literals, captures) {
|
|
560
|
+
if (patternCompileFn === void 0) throw new Error("Pattern dispatch functions not registered");
|
|
561
|
+
patternCompileFn(pattern, code, literals, captures);
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Dispatch isComplex on a Pattern.
|
|
565
|
+
*/
|
|
566
|
+
function dispatchIsComplex(pattern) {
|
|
567
|
+
if (patternIsComplexFn === void 0) throw new Error("Pattern dispatch functions not registered");
|
|
568
|
+
return patternIsComplexFn(pattern);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Dispatch toString on a Pattern.
|
|
572
|
+
*/
|
|
573
|
+
function dispatchPatternToString(pattern) {
|
|
574
|
+
if (patternToStringFn === void 0) throw new Error("Pattern dispatch functions not registered");
|
|
575
|
+
return patternToStringFn(pattern);
|
|
576
|
+
}
|
|
518
577
|
|
|
519
578
|
//#endregion
|
|
520
579
|
//#region src/pattern/leaf/bool-pattern.ts
|
|
@@ -560,7 +619,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
560
619
|
return this._inner;
|
|
561
620
|
}
|
|
562
621
|
pathsWithCaptures(haystack) {
|
|
563
|
-
const cbor = haystack.asLeaf();
|
|
622
|
+
const cbor = haystack.subject().asLeaf();
|
|
564
623
|
if (cbor !== void 0) {
|
|
565
624
|
if ((0, _bcts_dcbor_pattern.boolPatternPaths)(this._inner, cbor).length > 0) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
566
625
|
}
|
|
@@ -633,7 +692,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
633
692
|
return this._inner;
|
|
634
693
|
}
|
|
635
694
|
pathsWithCaptures(haystack) {
|
|
636
|
-
const cbor = haystack.asLeaf();
|
|
695
|
+
const cbor = haystack.subject().asLeaf();
|
|
637
696
|
if (cbor !== void 0) {
|
|
638
697
|
if ((0, _bcts_dcbor_pattern.nullPatternPaths)(this._inner, cbor).length > 0) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
639
698
|
}
|
|
@@ -761,7 +820,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
761
820
|
return this._inner;
|
|
762
821
|
}
|
|
763
822
|
pathsWithCaptures(haystack) {
|
|
764
|
-
const cbor = haystack.asLeaf();
|
|
823
|
+
const cbor = haystack.subject().asLeaf();
|
|
765
824
|
if (cbor !== void 0) {
|
|
766
825
|
if ((0, _bcts_dcbor_pattern.numberPatternPaths)(this._inner, cbor).length > 0) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
767
826
|
}
|
|
@@ -892,7 +951,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
892
951
|
return this._inner;
|
|
893
952
|
}
|
|
894
953
|
pathsWithCaptures(haystack) {
|
|
895
|
-
const cbor = haystack.asLeaf();
|
|
954
|
+
const cbor = haystack.subject().asLeaf();
|
|
896
955
|
if (cbor !== void 0) {
|
|
897
956
|
if ((0, _bcts_dcbor_pattern.textPatternPaths)(this._inner, cbor).length > 0) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
898
957
|
}
|
|
@@ -997,7 +1056,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
997
1056
|
return this._inner;
|
|
998
1057
|
}
|
|
999
1058
|
pathsWithCaptures(haystack) {
|
|
1000
|
-
const cbor = haystack.asLeaf();
|
|
1059
|
+
const cbor = haystack.subject().asLeaf();
|
|
1001
1060
|
if (cbor !== void 0) {
|
|
1002
1061
|
if ((0, _bcts_dcbor_pattern.byteStringPatternPaths)(this._inner, cbor).length > 0) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
1003
1062
|
}
|
|
@@ -1133,7 +1192,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
1133
1192
|
return this._inner;
|
|
1134
1193
|
}
|
|
1135
1194
|
pathsWithCaptures(haystack) {
|
|
1136
|
-
const cbor = haystack.asLeaf();
|
|
1195
|
+
const cbor = haystack.subject().asLeaf();
|
|
1137
1196
|
if (cbor !== void 0) {
|
|
1138
1197
|
if ((0, _bcts_dcbor_pattern.datePatternPaths)(this._inner, cbor).length > 0) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
1139
1198
|
}
|
|
@@ -1245,7 +1304,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
1245
1304
|
return this._pattern;
|
|
1246
1305
|
}
|
|
1247
1306
|
pathsWithCaptures(haystack) {
|
|
1248
|
-
const cbor = haystack.asLeaf();
|
|
1307
|
+
const cbor = haystack.subject().asLeaf();
|
|
1249
1308
|
if (cbor === void 0) return [[], /* @__PURE__ */ new Map()];
|
|
1250
1309
|
const array = (0, _bcts_dcbor.asCborArray)(cbor);
|
|
1251
1310
|
if (array === void 0) return [[], /* @__PURE__ */ new Map()];
|
|
@@ -1389,7 +1448,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
1389
1448
|
return this._pattern;
|
|
1390
1449
|
}
|
|
1391
1450
|
pathsWithCaptures(haystack) {
|
|
1392
|
-
const cbor = haystack.asLeaf();
|
|
1451
|
+
const cbor = haystack.subject().asLeaf();
|
|
1393
1452
|
if (cbor === void 0) return [[], /* @__PURE__ */ new Map()];
|
|
1394
1453
|
const map = (0, _bcts_dcbor.asCborMap)(cbor);
|
|
1395
1454
|
if (map === void 0) return [[], /* @__PURE__ */ new Map()];
|
|
@@ -1498,12 +1557,13 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
1498
1557
|
return this._inner;
|
|
1499
1558
|
}
|
|
1500
1559
|
pathsWithCaptures(haystack) {
|
|
1501
|
-
const
|
|
1560
|
+
const subject = haystack.subject();
|
|
1561
|
+
const envCase = subject.case();
|
|
1502
1562
|
if (envCase.type === "knownValue") {
|
|
1503
1563
|
const knownValueCbor = envCase.value.taggedCbor();
|
|
1504
1564
|
if ((0, _bcts_dcbor_pattern.knownValuePatternMatches)(this._inner, knownValueCbor)) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
1505
1565
|
}
|
|
1506
|
-
const leafCbor =
|
|
1566
|
+
const leafCbor = subject.asLeaf();
|
|
1507
1567
|
if (leafCbor !== void 0) {
|
|
1508
1568
|
if ((0, _bcts_dcbor_pattern.knownValuePatternMatches)(this._inner, leafCbor)) return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
1509
1569
|
}
|
|
@@ -1815,7 +1875,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
1815
1875
|
} else i++;
|
|
1816
1876
|
}
|
|
1817
1877
|
pathsWithCaptures(haystack) {
|
|
1818
|
-
const envCase = haystack.case();
|
|
1878
|
+
const envCase = haystack.subject().case();
|
|
1819
1879
|
if (envCase.type === "knownValue") {
|
|
1820
1880
|
const knownValueCbor = envCase.value.taggedCbor();
|
|
1821
1881
|
switch (this._pattern.type) {
|
|
@@ -1840,7 +1900,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
1840
1900
|
}
|
|
1841
1901
|
}
|
|
1842
1902
|
}
|
|
1843
|
-
const leafCbor = haystack.asLeaf();
|
|
1903
|
+
const leafCbor = haystack.subject().asLeaf();
|
|
1844
1904
|
if (leafCbor === void 0) return [[], /* @__PURE__ */ new Map()];
|
|
1845
1905
|
switch (this._pattern.type) {
|
|
1846
1906
|
case "Any": return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
@@ -2027,7 +2087,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
2027
2087
|
*/
|
|
2028
2088
|
function leafPatternPathsWithCaptures(pattern, haystack) {
|
|
2029
2089
|
switch (pattern.type) {
|
|
2030
|
-
case "Cbor": return
|
|
2090
|
+
case "Cbor": return pattern.pattern.pathsWithCaptures(haystack);
|
|
2031
2091
|
case "Number": return pattern.pattern.pathsWithCaptures(haystack);
|
|
2032
2092
|
case "Text": return pattern.pattern.pathsWithCaptures(haystack);
|
|
2033
2093
|
case "ByteString": return pattern.pattern.pathsWithCaptures(haystack);
|
|
@@ -2227,7 +2287,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
2227
2287
|
paths = [[subject]];
|
|
2228
2288
|
break;
|
|
2229
2289
|
case "Pattern":
|
|
2230
|
-
if (this._pattern.pattern
|
|
2290
|
+
if (matchPattern(this._pattern.pattern, subject)) paths = [[subject]];
|
|
2231
2291
|
else paths = [];
|
|
2232
2292
|
break;
|
|
2233
2293
|
}
|
|
@@ -2329,7 +2389,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
2329
2389
|
paths = [[predicate]];
|
|
2330
2390
|
break;
|
|
2331
2391
|
case "Pattern":
|
|
2332
|
-
if (this._pattern.pattern
|
|
2392
|
+
if (matchPattern(this._pattern.pattern, predicate)) paths = [[predicate]];
|
|
2333
2393
|
else paths = [];
|
|
2334
2394
|
break;
|
|
2335
2395
|
}
|
|
@@ -2427,7 +2487,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
2427
2487
|
paths = [[object]];
|
|
2428
2488
|
break;
|
|
2429
2489
|
case "Pattern":
|
|
2430
|
-
if (this._pattern.pattern
|
|
2490
|
+
if (matchPattern(this._pattern.pattern, object)) paths = [[object]];
|
|
2431
2491
|
else paths = [];
|
|
2432
2492
|
break;
|
|
2433
2493
|
}
|
|
@@ -2555,14 +2615,14 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
2555
2615
|
case "WithPredicate": {
|
|
2556
2616
|
const predicate = assertion.asPredicate?.();
|
|
2557
2617
|
if (predicate !== void 0) {
|
|
2558
|
-
if (this._pattern.pattern
|
|
2618
|
+
if (matchPattern(this._pattern.pattern, predicate)) paths.push([assertion]);
|
|
2559
2619
|
}
|
|
2560
2620
|
break;
|
|
2561
2621
|
}
|
|
2562
2622
|
case "WithObject": {
|
|
2563
2623
|
const object = assertion.asObject?.();
|
|
2564
2624
|
if (object !== void 0) {
|
|
2565
|
-
if (this._pattern.pattern
|
|
2625
|
+
if (matchPattern(this._pattern.pattern, object)) paths.push([assertion]);
|
|
2566
2626
|
}
|
|
2567
2627
|
break;
|
|
2568
2628
|
}
|
|
@@ -2570,9 +2630,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
2570
2630
|
const predicate = assertion.asPredicate?.();
|
|
2571
2631
|
const object = assertion.asObject?.();
|
|
2572
2632
|
if (predicate !== void 0 && object !== void 0) {
|
|
2573
|
-
|
|
2574
|
-
const objMatcher = this._pattern.objectPattern;
|
|
2575
|
-
if (predMatcher.matches(predicate) && objMatcher.matches(object)) paths.push([assertion]);
|
|
2633
|
+
if (matchPattern(this._pattern.predicatePattern, predicate) && matchPattern(this._pattern.objectPattern, object)) paths.push([assertion]);
|
|
2576
2634
|
}
|
|
2577
2635
|
break;
|
|
2578
2636
|
}
|
|
@@ -3024,9 +3082,17 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
3024
3082
|
//#endregion
|
|
3025
3083
|
//#region src/pattern/structure/wrapped-pattern.ts
|
|
3026
3084
|
let createStructureWrappedPattern;
|
|
3085
|
+
let dispatchPatternPathsWithCaptures;
|
|
3086
|
+
let dispatchPatternCompile;
|
|
3087
|
+
let dispatchPatternToString$1;
|
|
3027
3088
|
function registerWrappedPatternFactory(factory) {
|
|
3028
3089
|
createStructureWrappedPattern = factory;
|
|
3029
3090
|
}
|
|
3091
|
+
function registerWrappedPatternDispatch(dispatch) {
|
|
3092
|
+
dispatchPatternPathsWithCaptures = dispatch.pathsWithCaptures;
|
|
3093
|
+
dispatchPatternCompile = dispatch.compile;
|
|
3094
|
+
dispatchPatternToString$1 = dispatch.toString;
|
|
3095
|
+
}
|
|
3030
3096
|
/**
|
|
3031
3097
|
* Represents patterns for matching wrapped envelopes.
|
|
3032
3098
|
*
|
|
@@ -3082,10 +3148,12 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
3082
3148
|
break;
|
|
3083
3149
|
case "Unwrap": {
|
|
3084
3150
|
const unwrapped = subject.tryUnwrap?.();
|
|
3085
|
-
if (unwrapped !== void 0
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3151
|
+
if (unwrapped !== void 0 && dispatchPatternPathsWithCaptures !== void 0) {
|
|
3152
|
+
const [innerPaths] = dispatchPatternPathsWithCaptures(this._pattern.pattern, unwrapped);
|
|
3153
|
+
paths = innerPaths.map((path) => {
|
|
3154
|
+
return [haystack, ...path];
|
|
3155
|
+
});
|
|
3156
|
+
} else paths = [];
|
|
3089
3157
|
break;
|
|
3090
3158
|
}
|
|
3091
3159
|
}
|
|
@@ -3120,7 +3188,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
3120
3188
|
type: "PushAxis",
|
|
3121
3189
|
axis: "Wrapped"
|
|
3122
3190
|
});
|
|
3123
|
-
this._pattern.pattern
|
|
3191
|
+
if (dispatchPatternCompile !== void 0) dispatchPatternCompile(this._pattern.pattern, code, literals, captures);
|
|
3124
3192
|
break;
|
|
3125
3193
|
}
|
|
3126
3194
|
}
|
|
@@ -3132,7 +3200,7 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
3132
3200
|
switch (this._pattern.type) {
|
|
3133
3201
|
case "Any": return "wrapped";
|
|
3134
3202
|
case "Unwrap": {
|
|
3135
|
-
const patternStr = this._pattern.pattern
|
|
3203
|
+
const patternStr = dispatchPatternToString$1 !== void 0 ? dispatchPatternToString$1(this._pattern.pattern) : "*";
|
|
3136
3204
|
if (patternStr === "*") return "unwrap";
|
|
3137
3205
|
return `unwrap(${patternStr})`;
|
|
3138
3206
|
}
|
|
@@ -3327,1503 +3395,1520 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
3327
3395
|
}
|
|
3328
3396
|
|
|
3329
3397
|
//#endregion
|
|
3330
|
-
//#region src/pattern/
|
|
3331
|
-
let
|
|
3332
|
-
|
|
3333
|
-
|
|
3398
|
+
//#region src/pattern/vm.ts
|
|
3399
|
+
let _patternPathsWithCaptures$1;
|
|
3400
|
+
let _patternMatches;
|
|
3401
|
+
let _patternPaths;
|
|
3402
|
+
/**
|
|
3403
|
+
* Register the pattern matching functions to resolve circular dependencies.
|
|
3404
|
+
*/
|
|
3405
|
+
function registerVMPatternFunctions(pathsWithCaptures, matches, paths) {
|
|
3406
|
+
_patternPathsWithCaptures$1 = pathsWithCaptures;
|
|
3407
|
+
_patternMatches = matches;
|
|
3408
|
+
_patternPaths = paths;
|
|
3334
3409
|
}
|
|
3335
3410
|
/**
|
|
3336
|
-
*
|
|
3337
|
-
*
|
|
3338
|
-
* Corresponds to the Rust `AnyPattern` struct in any_pattern.rs
|
|
3411
|
+
* Returns (child, EdgeType) pairs reachable from env via this axis.
|
|
3339
3412
|
*/
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
toString() {
|
|
3365
|
-
return "*";
|
|
3366
|
-
}
|
|
3367
|
-
/**
|
|
3368
|
-
* Equality comparison.
|
|
3369
|
-
*/
|
|
3370
|
-
equals(_other) {
|
|
3371
|
-
return true;
|
|
3372
|
-
}
|
|
3373
|
-
/**
|
|
3374
|
-
* Hash code for use in Maps/Sets.
|
|
3375
|
-
*/
|
|
3376
|
-
hashCode() {
|
|
3377
|
-
return 0;
|
|
3413
|
+
function axisChildren(axis, env) {
|
|
3414
|
+
const envCase = env.case();
|
|
3415
|
+
switch (axis) {
|
|
3416
|
+
case "Subject":
|
|
3417
|
+
if (envCase.type === "node") return [[envCase.subject, "Subject"]];
|
|
3418
|
+
return [];
|
|
3419
|
+
case "Assertion":
|
|
3420
|
+
if (envCase.type === "node") return envCase.assertions.map((a) => [a, "Assertion"]);
|
|
3421
|
+
return [];
|
|
3422
|
+
case "Predicate":
|
|
3423
|
+
if (envCase.type === "assertion") return [[envCase.assertion.predicate(), "Predicate"]];
|
|
3424
|
+
return [];
|
|
3425
|
+
case "Object":
|
|
3426
|
+
if (envCase.type === "assertion") return [[envCase.assertion.object(), "Object"]];
|
|
3427
|
+
return [];
|
|
3428
|
+
case "Wrapped":
|
|
3429
|
+
if (envCase.type === "node") {
|
|
3430
|
+
const subject = envCase.subject;
|
|
3431
|
+
if (subject.isWrapped()) {
|
|
3432
|
+
const unwrapped = subject.unwrap();
|
|
3433
|
+
if (unwrapped !== void 0) return [[unwrapped, "Content"]];
|
|
3434
|
+
}
|
|
3435
|
+
} else if (envCase.type === "wrapped") return [[envCase.envelope, "Content"]];
|
|
3436
|
+
return [];
|
|
3378
3437
|
}
|
|
3379
|
-
};
|
|
3380
|
-
|
|
3381
|
-
//#endregion
|
|
3382
|
-
//#region src/pattern/meta/and-pattern.ts
|
|
3383
|
-
let createMetaAndPattern;
|
|
3384
|
-
function registerAndPatternFactory(factory) {
|
|
3385
|
-
createMetaAndPattern = factory;
|
|
3386
3438
|
}
|
|
3387
3439
|
/**
|
|
3388
|
-
*
|
|
3389
|
-
*
|
|
3390
|
-
* Corresponds to the Rust `AndPattern` struct in and_pattern.rs
|
|
3440
|
+
* Clone a thread for forking.
|
|
3391
3441
|
*/
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
}
|
|
3403
|
-
/**
|
|
3404
|
-
* Gets the patterns.
|
|
3405
|
-
*/
|
|
3406
|
-
patterns() {
|
|
3407
|
-
return this._patterns;
|
|
3408
|
-
}
|
|
3409
|
-
pathsWithCaptures(haystack) {
|
|
3410
|
-
return [this._patterns.every((pattern) => matchPattern(pattern, haystack)) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3411
|
-
}
|
|
3412
|
-
paths(haystack) {
|
|
3413
|
-
return this.pathsWithCaptures(haystack)[0];
|
|
3414
|
-
}
|
|
3415
|
-
matches(haystack) {
|
|
3416
|
-
return this.paths(haystack).length > 0;
|
|
3417
|
-
}
|
|
3418
|
-
compile(code, literals, captures) {
|
|
3419
|
-
for (const pattern of this._patterns) pattern.compile(code, literals, captures);
|
|
3420
|
-
}
|
|
3421
|
-
isComplex() {
|
|
3422
|
-
return this._patterns.length > 1 || this._patterns.some((p) => p.isComplex());
|
|
3423
|
-
}
|
|
3424
|
-
toString() {
|
|
3425
|
-
return this._patterns.map((p) => p.toString()).join(" & ");
|
|
3426
|
-
}
|
|
3427
|
-
/**
|
|
3428
|
-
* Equality comparison.
|
|
3429
|
-
*/
|
|
3430
|
-
equals(other) {
|
|
3431
|
-
if (this._patterns.length !== other._patterns.length) return false;
|
|
3432
|
-
for (let i = 0; i < this._patterns.length; i++) if (this._patterns[i] !== other._patterns[i]) return false;
|
|
3433
|
-
return true;
|
|
3434
|
-
}
|
|
3435
|
-
/**
|
|
3436
|
-
* Hash code for use in Maps/Sets.
|
|
3437
|
-
*/
|
|
3438
|
-
hashCode() {
|
|
3439
|
-
return this._patterns.length;
|
|
3440
|
-
}
|
|
3441
|
-
};
|
|
3442
|
-
|
|
3443
|
-
//#endregion
|
|
3444
|
-
//#region src/pattern/meta/or-pattern.ts
|
|
3445
|
-
let createMetaOrPattern;
|
|
3446
|
-
function registerOrPatternFactory(factory) {
|
|
3447
|
-
createMetaOrPattern = factory;
|
|
3442
|
+
function cloneThread(th) {
|
|
3443
|
+
return {
|
|
3444
|
+
pc: th.pc,
|
|
3445
|
+
env: th.env,
|
|
3446
|
+
path: [...th.path],
|
|
3447
|
+
savedPaths: th.savedPaths.map((p) => [...p]),
|
|
3448
|
+
captures: th.captures.map((c) => c.map((p) => [...p])),
|
|
3449
|
+
captureStack: th.captureStack.map((s) => [...s]),
|
|
3450
|
+
seen: new Set(th.seen)
|
|
3451
|
+
};
|
|
3448
3452
|
}
|
|
3449
3453
|
/**
|
|
3450
|
-
*
|
|
3454
|
+
* Get a unique key for a path based on envelope digests.
|
|
3455
|
+
*/
|
|
3456
|
+
function pathKey(path) {
|
|
3457
|
+
return path.map((e) => e.digest().hex()).join(",");
|
|
3458
|
+
}
|
|
3459
|
+
/**
|
|
3460
|
+
* Match atomic patterns without recursion into the VM.
|
|
3451
3461
|
*
|
|
3452
|
-
*
|
|
3462
|
+
* This function handles only the patterns that are safe to use in
|
|
3463
|
+
* MatchPredicate instructions - Leaf, Structure, Any patterns.
|
|
3453
3464
|
*/
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
static new(patterns) {
|
|
3463
|
-
return new OrPattern(patterns);
|
|
3464
|
-
}
|
|
3465
|
-
/**
|
|
3466
|
-
* Gets the patterns.
|
|
3467
|
-
*/
|
|
3468
|
-
patterns() {
|
|
3469
|
-
return this._patterns;
|
|
3470
|
-
}
|
|
3471
|
-
pathsWithCaptures(haystack) {
|
|
3472
|
-
return [this._patterns.some((pattern) => matchPattern(pattern, haystack)) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3473
|
-
}
|
|
3474
|
-
paths(haystack) {
|
|
3475
|
-
return this.pathsWithCaptures(haystack)[0];
|
|
3465
|
+
function atomicPathsWithCaptures(p, env) {
|
|
3466
|
+
switch (p.type) {
|
|
3467
|
+
case "Leaf": return leafPatternPathsWithCaptures(p.pattern, env);
|
|
3468
|
+
case "Structure": return structurePatternPathsWithCaptures(p.pattern, env);
|
|
3469
|
+
case "Meta":
|
|
3470
|
+
if (p.pattern.type === "Any") return p.pattern.pattern.pathsWithCaptures(env);
|
|
3471
|
+
if (p.pattern.type === "Search") throw new Error("SearchPattern should be compiled to Search instruction, not MatchPredicate");
|
|
3472
|
+
throw new Error(`non-atomic meta pattern used in MatchPredicate: ${p.pattern.type}`);
|
|
3476
3473
|
}
|
|
3477
|
-
|
|
3478
|
-
|
|
3474
|
+
}
|
|
3475
|
+
/**
|
|
3476
|
+
* Execute repeat pattern matching.
|
|
3477
|
+
*/
|
|
3478
|
+
function repeatPaths(pat, env, path, quantifier) {
|
|
3479
|
+
const states = [[[env, [...path]]]];
|
|
3480
|
+
const bound = quantifier.max() ?? Number.MAX_SAFE_INTEGER;
|
|
3481
|
+
for (let i = 0; i < bound; i++) {
|
|
3482
|
+
const next = [];
|
|
3483
|
+
const lastState = states[states.length - 1];
|
|
3484
|
+
for (const [e, pth] of lastState) {
|
|
3485
|
+
const subPaths = _patternPaths(pat, e);
|
|
3486
|
+
for (const subPath of subPaths) {
|
|
3487
|
+
const last = subPath[subPath.length - 1];
|
|
3488
|
+
if (last?.digest().hex() === e.digest().hex()) continue;
|
|
3489
|
+
if (last !== void 0) {
|
|
3490
|
+
const combined = [...pth];
|
|
3491
|
+
if (subPath[0]?.digest().hex() === e.digest().hex()) combined.push(...subPath.slice(1));
|
|
3492
|
+
else combined.push(...subPath);
|
|
3493
|
+
next.push([last, combined]);
|
|
3494
|
+
}
|
|
3495
|
+
}
|
|
3496
|
+
}
|
|
3497
|
+
if (next.length === 0) break;
|
|
3498
|
+
states.push(next);
|
|
3479
3499
|
}
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
if (i < this._patterns.length - 1) {
|
|
3502
|
-
const nextPattern = code.length;
|
|
3503
|
-
code[splits[i]] = {
|
|
3504
|
-
type: "Split",
|
|
3505
|
-
a: patternStart,
|
|
3506
|
-
b: nextPattern
|
|
3507
|
-
};
|
|
3508
|
-
}
|
|
3509
|
-
}
|
|
3510
|
-
const pastAll = code.length;
|
|
3511
|
-
for (const jumpIdx of jumps) code[jumpIdx] = {
|
|
3512
|
-
type: "Jump",
|
|
3513
|
-
address: pastAll
|
|
3514
|
-
};
|
|
3515
|
-
}
|
|
3516
|
-
isComplex() {
|
|
3517
|
-
return this._patterns.length > 1 || this._patterns.some((p) => p.isComplex());
|
|
3518
|
-
}
|
|
3519
|
-
toString() {
|
|
3520
|
-
return this._patterns.map((p) => p.toString()).join(" | ");
|
|
3521
|
-
}
|
|
3522
|
-
/**
|
|
3523
|
-
* Equality comparison.
|
|
3524
|
-
*/
|
|
3525
|
-
equals(other) {
|
|
3526
|
-
if (this._patterns.length !== other._patterns.length) return false;
|
|
3527
|
-
for (let i = 0; i < this._patterns.length; i++) if (this._patterns[i] !== other._patterns[i]) return false;
|
|
3528
|
-
return true;
|
|
3529
|
-
}
|
|
3530
|
-
/**
|
|
3531
|
-
* Hash code for use in Maps/Sets.
|
|
3532
|
-
*/
|
|
3533
|
-
hashCode() {
|
|
3534
|
-
return this._patterns.length;
|
|
3535
|
-
}
|
|
3536
|
-
};
|
|
3537
|
-
|
|
3538
|
-
//#endregion
|
|
3539
|
-
//#region src/pattern/meta/not-pattern.ts
|
|
3540
|
-
let createMetaNotPattern;
|
|
3541
|
-
function registerNotPatternFactory(factory) {
|
|
3542
|
-
createMetaNotPattern = factory;
|
|
3543
|
-
}
|
|
3544
|
-
/**
|
|
3545
|
-
* A pattern that negates another pattern; matches when the inner pattern does not match.
|
|
3546
|
-
*
|
|
3547
|
-
* Corresponds to the Rust `NotPattern` struct in not_pattern.rs
|
|
3548
|
-
*/
|
|
3549
|
-
var NotPattern = class NotPattern {
|
|
3550
|
-
_pattern;
|
|
3551
|
-
constructor(pattern) {
|
|
3552
|
-
this._pattern = pattern;
|
|
3553
|
-
}
|
|
3554
|
-
/**
|
|
3555
|
-
* Creates a new NotPattern with the given pattern.
|
|
3556
|
-
*/
|
|
3557
|
-
static new(pattern) {
|
|
3558
|
-
return new NotPattern(pattern);
|
|
3559
|
-
}
|
|
3560
|
-
/**
|
|
3561
|
-
* Gets the inner pattern.
|
|
3562
|
-
*/
|
|
3563
|
-
pattern() {
|
|
3564
|
-
return this._pattern;
|
|
3565
|
-
}
|
|
3566
|
-
pathsWithCaptures(haystack) {
|
|
3567
|
-
return [!matchPattern(this._pattern, haystack) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3568
|
-
}
|
|
3569
|
-
paths(haystack) {
|
|
3570
|
-
return this.pathsWithCaptures(haystack)[0];
|
|
3571
|
-
}
|
|
3572
|
-
matches(haystack) {
|
|
3573
|
-
return this.paths(haystack).length > 0;
|
|
3574
|
-
}
|
|
3575
|
-
compile(code, literals, _captures) {
|
|
3576
|
-
const idx = literals.length;
|
|
3577
|
-
literals.push(this._pattern);
|
|
3578
|
-
code.push({
|
|
3579
|
-
type: "NotMatch",
|
|
3580
|
-
patternIndex: idx
|
|
3581
|
-
});
|
|
3582
|
-
}
|
|
3583
|
-
isComplex() {
|
|
3584
|
-
return false;
|
|
3585
|
-
}
|
|
3586
|
-
toString() {
|
|
3587
|
-
return `!${this._pattern.toString()}`;
|
|
3588
|
-
}
|
|
3589
|
-
/**
|
|
3590
|
-
* Equality comparison.
|
|
3591
|
-
*/
|
|
3592
|
-
equals(other) {
|
|
3593
|
-
return this._pattern === other._pattern;
|
|
3594
|
-
}
|
|
3595
|
-
/**
|
|
3596
|
-
* Hash code for use in Maps/Sets.
|
|
3597
|
-
*/
|
|
3598
|
-
hashCode() {
|
|
3599
|
-
return 1;
|
|
3600
|
-
}
|
|
3601
|
-
};
|
|
3602
|
-
|
|
3603
|
-
//#endregion
|
|
3604
|
-
//#region src/pattern/meta/capture-pattern.ts
|
|
3605
|
-
let createMetaCapturePattern;
|
|
3606
|
-
function registerCapturePatternFactory(factory) {
|
|
3607
|
-
createMetaCapturePattern = factory;
|
|
3608
|
-
}
|
|
3609
|
-
/**
|
|
3610
|
-
* A pattern that captures a match with a name.
|
|
3611
|
-
*
|
|
3612
|
-
* Corresponds to the Rust `CapturePattern` struct in capture_pattern.rs
|
|
3613
|
-
*/
|
|
3614
|
-
var CapturePattern = class CapturePattern {
|
|
3615
|
-
_name;
|
|
3616
|
-
_pattern;
|
|
3617
|
-
constructor(name, pattern) {
|
|
3618
|
-
this._name = name;
|
|
3619
|
-
this._pattern = pattern;
|
|
3620
|
-
}
|
|
3621
|
-
/**
|
|
3622
|
-
* Creates a new CapturePattern with the given name and pattern.
|
|
3623
|
-
*/
|
|
3624
|
-
static new(name, pattern) {
|
|
3625
|
-
return new CapturePattern(name, pattern);
|
|
3626
|
-
}
|
|
3627
|
-
/**
|
|
3628
|
-
* Gets the name of the capture.
|
|
3629
|
-
*/
|
|
3630
|
-
name() {
|
|
3631
|
-
return this._name;
|
|
3632
|
-
}
|
|
3633
|
-
/**
|
|
3634
|
-
* Gets the inner pattern.
|
|
3635
|
-
*/
|
|
3636
|
-
pattern() {
|
|
3637
|
-
return this._pattern;
|
|
3500
|
+
const hasZeroRep = quantifier.min() === 0;
|
|
3501
|
+
const zeroRepResult = hasZeroRep ? [[env, [...path]]] : [];
|
|
3502
|
+
const maxPossible = states.length - 1;
|
|
3503
|
+
const maxAllowed = Math.min(bound, maxPossible);
|
|
3504
|
+
if (maxAllowed < quantifier.min() && quantifier.min() > 0) return [];
|
|
3505
|
+
const minCount = quantifier.min() === 0 ? 1 : quantifier.min();
|
|
3506
|
+
if (maxAllowed < minCount) return zeroRepResult;
|
|
3507
|
+
const maxCount = maxAllowed;
|
|
3508
|
+
let counts;
|
|
3509
|
+
switch (quantifier.reluctance()) {
|
|
3510
|
+
case _bcts_dcbor_pattern.Reluctance.Greedy:
|
|
3511
|
+
counts = [];
|
|
3512
|
+
for (let c = maxCount; c >= minCount; c--) counts.push(c);
|
|
3513
|
+
break;
|
|
3514
|
+
case _bcts_dcbor_pattern.Reluctance.Lazy:
|
|
3515
|
+
counts = [];
|
|
3516
|
+
for (let c = minCount; c <= maxCount; c++) counts.push(c);
|
|
3517
|
+
break;
|
|
3518
|
+
case _bcts_dcbor_pattern.Reluctance.Possessive:
|
|
3519
|
+
counts = maxCount >= minCount ? [maxCount] : [];
|
|
3520
|
+
break;
|
|
3638
3521
|
}
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
const
|
|
3643
|
-
|
|
3522
|
+
const out = [];
|
|
3523
|
+
if (quantifier.reluctance() === _bcts_dcbor_pattern.Reluctance.Greedy) {
|
|
3524
|
+
for (const c of counts) {
|
|
3525
|
+
const list = states[c];
|
|
3526
|
+
if (list !== void 0) out.push(...list);
|
|
3527
|
+
}
|
|
3528
|
+
if (hasZeroRep && out.length === 0) out.push([env, [...path]]);
|
|
3529
|
+
} else {
|
|
3530
|
+
if (hasZeroRep) out.push([env, [...path]]);
|
|
3531
|
+
for (const c of counts) {
|
|
3532
|
+
const list = states[c];
|
|
3533
|
+
if (list !== void 0) out.push(...list);
|
|
3644
3534
|
}
|
|
3645
|
-
return [paths, caps];
|
|
3646
|
-
}
|
|
3647
|
-
paths(haystack) {
|
|
3648
|
-
return this.pathsWithCaptures(haystack)[0];
|
|
3649
|
-
}
|
|
3650
|
-
matches(haystack) {
|
|
3651
|
-
return this.paths(haystack).length > 0;
|
|
3652
|
-
}
|
|
3653
|
-
compile(code, literals, captures) {
|
|
3654
|
-
const id = captures.length;
|
|
3655
|
-
captures.push(this._name);
|
|
3656
|
-
code.push({
|
|
3657
|
-
type: "CaptureStart",
|
|
3658
|
-
captureIndex: id
|
|
3659
|
-
});
|
|
3660
|
-
this._pattern.compile(code, literals, captures);
|
|
3661
|
-
code.push({
|
|
3662
|
-
type: "CaptureEnd",
|
|
3663
|
-
captureIndex: id
|
|
3664
|
-
});
|
|
3665
|
-
}
|
|
3666
|
-
isComplex() {
|
|
3667
|
-
return false;
|
|
3668
|
-
}
|
|
3669
|
-
toString() {
|
|
3670
|
-
return `@${this._name}(${this._pattern.toString()})`;
|
|
3671
|
-
}
|
|
3672
|
-
/**
|
|
3673
|
-
* Equality comparison.
|
|
3674
|
-
*/
|
|
3675
|
-
equals(other) {
|
|
3676
|
-
return this._name === other._name && this._pattern === other._pattern;
|
|
3677
|
-
}
|
|
3678
|
-
/**
|
|
3679
|
-
* Hash code for use in Maps/Sets.
|
|
3680
|
-
*/
|
|
3681
|
-
hashCode() {
|
|
3682
|
-
let hash = 0;
|
|
3683
|
-
for (const char of this._name) hash = hash * 31 + char.charCodeAt(0) | 0;
|
|
3684
|
-
return hash;
|
|
3685
3535
|
}
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
//#endregion
|
|
3689
|
-
//#region src/pattern/meta/search-pattern.ts
|
|
3690
|
-
let createMetaSearchPattern;
|
|
3691
|
-
function registerSearchPatternFactory(factory) {
|
|
3692
|
-
createMetaSearchPattern = factory;
|
|
3536
|
+
return out;
|
|
3693
3537
|
}
|
|
3694
3538
|
/**
|
|
3695
|
-
*
|
|
3696
|
-
*
|
|
3697
|
-
* Corresponds to the Rust `SearchPattern` struct in search_pattern.rs
|
|
3539
|
+
* Execute a single thread until it halts.
|
|
3540
|
+
* Returns true if any paths were produced.
|
|
3698
3541
|
*/
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3542
|
+
function runThread(prog, start, out) {
|
|
3543
|
+
let produced = false;
|
|
3544
|
+
const stack = [start];
|
|
3545
|
+
while (stack.length > 0) {
|
|
3546
|
+
const th = stack.pop();
|
|
3547
|
+
if (th === void 0) break;
|
|
3548
|
+
while (true) {
|
|
3549
|
+
const instr = prog.code[th.pc];
|
|
3550
|
+
switch (instr.type) {
|
|
3551
|
+
case "MatchPredicate": {
|
|
3552
|
+
const [paths, patternCaptures] = atomicPathsWithCaptures(prog.literals[instr.literalIndex], th.env);
|
|
3553
|
+
if (paths.length === 0) break;
|
|
3554
|
+
th.pc += 1;
|
|
3555
|
+
const distributedCaptures = paths.map(() => /* @__PURE__ */ new Map());
|
|
3556
|
+
for (const [name, capturePaths] of patternCaptures) if (capturePaths.length === paths.length) {
|
|
3557
|
+
for (let pathIdx = 0; pathIdx < capturePaths.length; pathIdx++) if (pathIdx < distributedCaptures.length) {
|
|
3558
|
+
const existing = distributedCaptures[pathIdx].get(name) ?? [];
|
|
3559
|
+
existing.push(capturePaths[pathIdx]);
|
|
3560
|
+
distributedCaptures[pathIdx].set(name, existing);
|
|
3561
|
+
}
|
|
3562
|
+
} else if (distributedCaptures.length > 0) {
|
|
3563
|
+
const existing = distributedCaptures[0].get(name) ?? [];
|
|
3564
|
+
existing.push(...capturePaths);
|
|
3565
|
+
distributedCaptures[0].set(name, existing);
|
|
3566
|
+
}
|
|
3567
|
+
const firstPath = paths[0];
|
|
3568
|
+
if (firstPath.length === 1 && firstPath[0].digest().hex() === th.env.digest().hex()) {} else {
|
|
3569
|
+
th.path = [...firstPath];
|
|
3570
|
+
const lastEnv = firstPath[firstPath.length - 1];
|
|
3571
|
+
if (lastEnv !== void 0) th.env = lastEnv;
|
|
3572
|
+
}
|
|
3573
|
+
const pathCaptures = distributedCaptures[0];
|
|
3574
|
+
if (pathCaptures !== void 0) for (const [name, capPaths] of pathCaptures) {
|
|
3575
|
+
const captureIdx = prog.captureNames.indexOf(name);
|
|
3576
|
+
if (captureIdx >= 0 && captureIdx < th.captures.length) th.captures[captureIdx].push(...capPaths);
|
|
3577
|
+
}
|
|
3578
|
+
for (let i = paths.length - 1; i >= 1; i--) {
|
|
3579
|
+
const fork = cloneThread(th);
|
|
3580
|
+
for (const captureVec of fork.captures) captureVec.length = 0;
|
|
3581
|
+
const pathI = paths[i];
|
|
3582
|
+
if (pathI === void 0) continue;
|
|
3583
|
+
fork.path = [...pathI];
|
|
3584
|
+
const lastEnv = pathI[pathI.length - 1];
|
|
3585
|
+
if (lastEnv !== void 0) fork.env = lastEnv;
|
|
3586
|
+
const forkCaptures = distributedCaptures[i];
|
|
3587
|
+
if (forkCaptures !== void 0) for (const [name, capPaths] of forkCaptures) {
|
|
3588
|
+
const captureIdx = prog.captureNames.indexOf(name);
|
|
3589
|
+
if (captureIdx >= 0 && captureIdx < fork.captures.length) fork.captures[captureIdx].push(...capPaths);
|
|
3590
|
+
}
|
|
3591
|
+
stack.push(fork);
|
|
3592
|
+
}
|
|
3593
|
+
continue;
|
|
3728
3594
|
}
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
for (const assertion of envelope.assertions()) {
|
|
3752
|
-
this._walkEnvelope(assertion, newPath, visitor);
|
|
3753
|
-
const predicate = assertion.asPredicate?.();
|
|
3754
|
-
if (predicate !== void 0) {
|
|
3755
|
-
const assertionPath = [...newPath, assertion];
|
|
3756
|
-
this._walkEnvelope(predicate, assertionPath, visitor);
|
|
3757
|
-
}
|
|
3758
|
-
const object = assertion.asObject?.();
|
|
3759
|
-
if (object !== void 0) {
|
|
3760
|
-
const assertionPath = [...newPath, assertion];
|
|
3761
|
-
this._walkEnvelope(object, assertionPath, visitor);
|
|
3762
|
-
}
|
|
3763
|
-
}
|
|
3764
|
-
if (subject.isWrapped()) {
|
|
3765
|
-
const unwrapped = subject.tryUnwrap?.();
|
|
3766
|
-
if (unwrapped !== void 0) this._walkEnvelope(unwrapped, newPath, visitor);
|
|
3767
|
-
}
|
|
3768
|
-
}
|
|
3769
|
-
paths(haystack) {
|
|
3770
|
-
return this.pathsWithCaptures(haystack)[0];
|
|
3771
|
-
}
|
|
3772
|
-
matches(haystack) {
|
|
3773
|
-
return this.paths(haystack).length > 0;
|
|
3774
|
-
}
|
|
3775
|
-
compile(code, literals, captures) {
|
|
3776
|
-
const idx = literals.length;
|
|
3777
|
-
literals.push(this._pattern);
|
|
3778
|
-
const innerNames = [];
|
|
3779
|
-
collectCaptureNames$1(this._pattern, innerNames);
|
|
3780
|
-
const captureMap = [];
|
|
3781
|
-
for (const name of innerNames) {
|
|
3782
|
-
let pos = captures.indexOf(name);
|
|
3783
|
-
if (pos === -1) {
|
|
3784
|
-
pos = captures.length;
|
|
3785
|
-
captures.push(name);
|
|
3786
|
-
}
|
|
3787
|
-
captureMap.push([name, pos]);
|
|
3788
|
-
}
|
|
3789
|
-
code.push({
|
|
3790
|
-
type: "Search",
|
|
3791
|
-
patternIndex: idx,
|
|
3792
|
-
captureMap
|
|
3793
|
-
});
|
|
3794
|
-
}
|
|
3795
|
-
isComplex() {
|
|
3796
|
-
return true;
|
|
3797
|
-
}
|
|
3798
|
-
toString() {
|
|
3799
|
-
return `search(${this._pattern.toString()})`;
|
|
3800
|
-
}
|
|
3801
|
-
/**
|
|
3802
|
-
* Equality comparison.
|
|
3803
|
-
*/
|
|
3804
|
-
equals(other) {
|
|
3805
|
-
return this._pattern === other._pattern;
|
|
3806
|
-
}
|
|
3807
|
-
/**
|
|
3808
|
-
* Hash code for use in Maps/Sets.
|
|
3809
|
-
*/
|
|
3810
|
-
hashCode() {
|
|
3811
|
-
return 1;
|
|
3812
|
-
}
|
|
3813
|
-
};
|
|
3814
|
-
/**
|
|
3815
|
-
* Collect capture names from a pattern.
|
|
3816
|
-
*/
|
|
3817
|
-
function collectCaptureNames$1(pattern, out) {
|
|
3818
|
-
const p = pattern;
|
|
3819
|
-
if (p.collectCaptureNames !== void 0) p.collectCaptureNames(out);
|
|
3820
|
-
}
|
|
3821
|
-
|
|
3822
|
-
//#endregion
|
|
3823
|
-
//#region src/pattern/meta/traverse-pattern.ts
|
|
3824
|
-
let createMetaTraversePattern;
|
|
3825
|
-
function registerTraversePatternFactory(factory) {
|
|
3826
|
-
createMetaTraversePattern = factory;
|
|
3827
|
-
}
|
|
3828
|
-
/**
|
|
3829
|
-
* A pattern that matches a traversal order of patterns.
|
|
3830
|
-
*
|
|
3831
|
-
* Corresponds to the Rust `TraversePattern` struct in traverse_pattern.rs
|
|
3832
|
-
*/
|
|
3833
|
-
var TraversePattern = class TraversePattern {
|
|
3834
|
-
_first;
|
|
3835
|
-
_rest;
|
|
3836
|
-
constructor(first, rest) {
|
|
3837
|
-
this._first = first;
|
|
3838
|
-
this._rest = rest;
|
|
3839
|
-
}
|
|
3840
|
-
/**
|
|
3841
|
-
* Creates a new TraversePattern with the given patterns.
|
|
3842
|
-
*/
|
|
3843
|
-
static new(patterns) {
|
|
3844
|
-
if (patterns.length === 0) throw new Error("TraversePattern requires at least one pattern");
|
|
3845
|
-
const firstPat = patterns[0];
|
|
3846
|
-
const restPatterns = patterns.slice(1);
|
|
3847
|
-
return new TraversePattern(firstPat, restPatterns.length === 0 ? void 0 : TraversePattern.new(restPatterns));
|
|
3848
|
-
}
|
|
3849
|
-
/**
|
|
3850
|
-
* Gets all patterns in this traversal.
|
|
3851
|
-
*/
|
|
3852
|
-
patterns() {
|
|
3853
|
-
const result = [this._first];
|
|
3854
|
-
if (this._rest !== void 0) result.push(...this._rest.patterns());
|
|
3855
|
-
return result;
|
|
3856
|
-
}
|
|
3857
|
-
pathsWithCaptures(haystack) {
|
|
3858
|
-
const headPaths = this._first.paths(haystack);
|
|
3859
|
-
if (this._rest === void 0) return [headPaths, /* @__PURE__ */ new Map()];
|
|
3860
|
-
const result = [];
|
|
3861
|
-
for (const path of headPaths) {
|
|
3862
|
-
const lastEnv = path[path.length - 1];
|
|
3863
|
-
if (lastEnv !== void 0) {
|
|
3864
|
-
const tailPaths = this._rest.paths(lastEnv);
|
|
3865
|
-
for (const tailPath of tailPaths) {
|
|
3866
|
-
const combined = [...path, ...tailPath];
|
|
3867
|
-
result.push(combined);
|
|
3595
|
+
case "MatchStructure": {
|
|
3596
|
+
const literal = prog.literals[instr.literalIndex];
|
|
3597
|
+
if (literal.type !== "Structure") throw new Error("MatchStructure used with non-structure pattern");
|
|
3598
|
+
const structurePaths = structurePatternPaths(literal.pattern, th.env);
|
|
3599
|
+
if (structurePaths.length === 0) break;
|
|
3600
|
+
th.pc += 1;
|
|
3601
|
+
const firstStructPath = structurePaths[0];
|
|
3602
|
+
if (firstStructPath !== void 0) {
|
|
3603
|
+
th.path = [...firstStructPath];
|
|
3604
|
+
const firstLast = firstStructPath[firstStructPath.length - 1];
|
|
3605
|
+
if (firstLast !== void 0) th.env = firstLast;
|
|
3606
|
+
}
|
|
3607
|
+
for (let i = structurePaths.length - 1; i >= 1; i--) {
|
|
3608
|
+
const structPathI = structurePaths[i];
|
|
3609
|
+
if (structPathI === void 0) continue;
|
|
3610
|
+
const fork = cloneThread(th);
|
|
3611
|
+
fork.path = [...structPathI];
|
|
3612
|
+
const lastEnv = structPathI[structPathI.length - 1];
|
|
3613
|
+
if (lastEnv !== void 0) fork.env = lastEnv;
|
|
3614
|
+
stack.push(fork);
|
|
3615
|
+
}
|
|
3616
|
+
continue;
|
|
3868
3617
|
}
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3618
|
+
case "Split": {
|
|
3619
|
+
const fork = cloneThread(th);
|
|
3620
|
+
fork.pc = instr.a;
|
|
3621
|
+
stack.push(fork);
|
|
3622
|
+
th.pc = instr.b;
|
|
3623
|
+
continue;
|
|
3624
|
+
}
|
|
3625
|
+
case "Jump":
|
|
3626
|
+
th.pc = instr.address;
|
|
3627
|
+
continue;
|
|
3628
|
+
case "PushAxis": {
|
|
3629
|
+
th.pc += 1;
|
|
3630
|
+
const children = axisChildren(instr.axis, th.env);
|
|
3631
|
+
for (const [child, _edge] of children) {
|
|
3632
|
+
const fork = cloneThread(th);
|
|
3633
|
+
fork.env = child;
|
|
3634
|
+
fork.path.push(child);
|
|
3635
|
+
stack.push(fork);
|
|
3636
|
+
}
|
|
3637
|
+
break;
|
|
3638
|
+
}
|
|
3639
|
+
case "Pop":
|
|
3640
|
+
th.path.pop();
|
|
3641
|
+
th.pc += 1;
|
|
3642
|
+
continue;
|
|
3643
|
+
case "Save":
|
|
3644
|
+
out.push([[...th.path], th.captures.map((c) => c.map((p) => [...p]))]);
|
|
3645
|
+
produced = true;
|
|
3646
|
+
th.pc += 1;
|
|
3647
|
+
continue;
|
|
3648
|
+
case "Accept":
|
|
3649
|
+
out.push([[...th.path], th.captures.map((c) => c.map((p) => [...p]))]);
|
|
3650
|
+
produced = true;
|
|
3651
|
+
break;
|
|
3652
|
+
case "Search": {
|
|
3653
|
+
const inner = prog.literals[instr.patternIndex];
|
|
3654
|
+
if (inner === void 0) break;
|
|
3655
|
+
const [foundPaths, caps] = _patternPathsWithCaptures$1(inner, th.env);
|
|
3656
|
+
if (foundPaths.length > 0) {
|
|
3657
|
+
produced = true;
|
|
3658
|
+
for (const foundPath of foundPaths) {
|
|
3659
|
+
const resultPath = [...th.path];
|
|
3660
|
+
if (foundPath[0]?.digest().hex() === th.env.digest().hex()) resultPath.push(...foundPath.slice(1));
|
|
3661
|
+
else resultPath.push(...foundPath);
|
|
3662
|
+
const resultCaps = th.captures.map((c) => c.map((p) => [...p]));
|
|
3663
|
+
for (const [name, idx] of instr.captureMap) {
|
|
3664
|
+
const pths = caps.get(name);
|
|
3665
|
+
if (pths !== void 0) resultCaps[idx].push(...pths);
|
|
3666
|
+
}
|
|
3667
|
+
const key = pathKey(resultPath);
|
|
3668
|
+
if (!th.seen.has(key)) {
|
|
3669
|
+
th.seen.add(key);
|
|
3670
|
+
out.push([resultPath, resultCaps]);
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
const allChildren = [];
|
|
3675
|
+
const envCase = th.env.case();
|
|
3676
|
+
switch (envCase.type) {
|
|
3677
|
+
case "node":
|
|
3678
|
+
allChildren.push(envCase.subject);
|
|
3679
|
+
for (const assertion of envCase.assertions) allChildren.push(assertion);
|
|
3680
|
+
break;
|
|
3681
|
+
case "wrapped":
|
|
3682
|
+
allChildren.push(envCase.envelope);
|
|
3683
|
+
break;
|
|
3684
|
+
case "assertion":
|
|
3685
|
+
allChildren.push(envCase.assertion.predicate());
|
|
3686
|
+
allChildren.push(envCase.assertion.object());
|
|
3687
|
+
break;
|
|
3688
|
+
case "elided":
|
|
3689
|
+
case "encrypted":
|
|
3690
|
+
case "compressed":
|
|
3691
|
+
case "leaf":
|
|
3692
|
+
case "knownValue": break;
|
|
3693
|
+
}
|
|
3694
|
+
for (let i = allChildren.length - 1; i >= 0; i--) {
|
|
3695
|
+
const child = allChildren[i];
|
|
3696
|
+
if (child === void 0) continue;
|
|
3697
|
+
const fork = cloneThread(th);
|
|
3698
|
+
fork.env = child;
|
|
3699
|
+
fork.path.push(child);
|
|
3700
|
+
stack.push(fork);
|
|
3701
|
+
}
|
|
3702
|
+
break;
|
|
3703
|
+
}
|
|
3704
|
+
case "ExtendTraversal": {
|
|
3705
|
+
const lastEnv = th.path[th.path.length - 1];
|
|
3706
|
+
if (lastEnv !== void 0) {
|
|
3707
|
+
th.savedPaths.push([...th.path]);
|
|
3708
|
+
th.env = lastEnv;
|
|
3709
|
+
th.path = [lastEnv];
|
|
3710
|
+
}
|
|
3711
|
+
th.pc += 1;
|
|
3712
|
+
continue;
|
|
3713
|
+
}
|
|
3714
|
+
case "CombineTraversal": {
|
|
3715
|
+
const savedPath = th.savedPaths.pop();
|
|
3716
|
+
if (savedPath !== void 0) {
|
|
3717
|
+
const combined = [...savedPath];
|
|
3718
|
+
const savedLast = savedPath[savedPath.length - 1];
|
|
3719
|
+
if (savedLast?.digest().hex() === th.path[0]?.digest().hex() && savedLast !== void 0) combined.push(...th.path.slice(1));
|
|
3720
|
+
else combined.push(...th.path);
|
|
3721
|
+
th.path = combined;
|
|
3722
|
+
}
|
|
3723
|
+
th.pc += 1;
|
|
3724
|
+
continue;
|
|
3725
|
+
}
|
|
3726
|
+
case "NavigateSubject":
|
|
3727
|
+
if (th.env.isNode()) {
|
|
3728
|
+
const subject = th.env.subject();
|
|
3729
|
+
th.env = subject;
|
|
3730
|
+
th.path.push(subject);
|
|
3731
|
+
}
|
|
3732
|
+
th.pc += 1;
|
|
3733
|
+
continue;
|
|
3734
|
+
case "NotMatch": {
|
|
3735
|
+
const pattern = prog.literals[instr.patternIndex];
|
|
3736
|
+
if (_patternMatches(pattern, th.env)) break;
|
|
3737
|
+
else {
|
|
3738
|
+
th.pc += 1;
|
|
3739
|
+
continue;
|
|
3740
|
+
}
|
|
3741
|
+
}
|
|
3742
|
+
case "Repeat": {
|
|
3743
|
+
const pat = prog.literals[instr.patternIndex];
|
|
3744
|
+
const results = repeatPaths(pat, th.env, th.path, instr.quantifier);
|
|
3745
|
+
if (results.length === 0) break;
|
|
3746
|
+
const nextPc = th.pc + 1;
|
|
3747
|
+
let success = false;
|
|
3748
|
+
for (const [envAfter, pathAfter] of results) {
|
|
3749
|
+
const fork = cloneThread(th);
|
|
3750
|
+
fork.pc = nextPc;
|
|
3751
|
+
fork.env = envAfter;
|
|
3752
|
+
fork.path = pathAfter;
|
|
3753
|
+
if (runThread(prog, fork, out)) {
|
|
3754
|
+
produced = true;
|
|
3755
|
+
success = true;
|
|
3756
|
+
break;
|
|
3757
|
+
}
|
|
3758
|
+
}
|
|
3759
|
+
if (!success) {}
|
|
3760
|
+
break;
|
|
3761
|
+
}
|
|
3762
|
+
case "CaptureStart": {
|
|
3763
|
+
const id = instr.captureIndex;
|
|
3764
|
+
if (id < th.captureStack.length) th.captureStack[id].push(th.path.length - 1);
|
|
3765
|
+
th.pc += 1;
|
|
3766
|
+
continue;
|
|
3767
|
+
}
|
|
3768
|
+
case "CaptureEnd": {
|
|
3769
|
+
const id = instr.captureIndex;
|
|
3770
|
+
if (id < th.captureStack.length) {
|
|
3771
|
+
const startIdx = th.captureStack[id].pop();
|
|
3772
|
+
if (startIdx !== void 0 && id < th.captures.length) {
|
|
3773
|
+
let end = th.path.length;
|
|
3774
|
+
if (prog.code[th.pc + 1]?.type === "ExtendTraversal") end = Math.max(0, end - 1);
|
|
3775
|
+
const cap = th.path.slice(startIdx, end);
|
|
3776
|
+
th.captures[id].push(cap);
|
|
3777
|
+
}
|
|
3778
|
+
}
|
|
3779
|
+
th.pc += 1;
|
|
3780
|
+
continue;
|
|
3781
|
+
}
|
|
3782
|
+
}
|
|
3783
|
+
break;
|
|
3784
|
+
}
|
|
3908
3785
|
}
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
//#endregion
|
|
3912
|
-
//#region src/pattern/meta/group-pattern.ts
|
|
3913
|
-
let createMetaGroupPattern;
|
|
3914
|
-
function registerGroupPatternFactory(factory) {
|
|
3915
|
-
createMetaGroupPattern = factory;
|
|
3786
|
+
return produced;
|
|
3916
3787
|
}
|
|
3917
3788
|
/**
|
|
3918
|
-
*
|
|
3919
|
-
*
|
|
3920
|
-
* Corresponds to the Rust `GroupPattern` struct in repeat_pattern.rs
|
|
3789
|
+
* Execute prog starting at root.
|
|
3790
|
+
* Every time SAVE or ACCEPT executes, the current path is pushed into the result.
|
|
3921
3791
|
*/
|
|
3922
|
-
|
|
3923
|
-
|
|
3924
|
-
|
|
3925
|
-
|
|
3926
|
-
|
|
3927
|
-
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
|
|
3937
|
-
|
|
3938
|
-
|
|
3939
|
-
return
|
|
3940
|
-
}
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
}
|
|
3971
|
-
isComplex() {
|
|
3972
|
-
return true;
|
|
3973
|
-
}
|
|
3974
|
-
toString() {
|
|
3975
|
-
const formattedRange = this._quantifier.toString();
|
|
3976
|
-
return `(${this._pattern.toString()})${formattedRange}`;
|
|
3977
|
-
}
|
|
3978
|
-
/**
|
|
3979
|
-
* Equality comparison.
|
|
3980
|
-
*/
|
|
3981
|
-
equals(other) {
|
|
3982
|
-
return this._pattern === other._pattern && this._quantifier.equals(other._quantifier);
|
|
3983
|
-
}
|
|
3984
|
-
/**
|
|
3985
|
-
* Hash code for use in Maps/Sets.
|
|
3986
|
-
*/
|
|
3987
|
-
hashCode() {
|
|
3988
|
-
return this._quantifier.min() * 31 + (this._quantifier.max() ?? 0);
|
|
3989
|
-
}
|
|
3990
|
-
};
|
|
3991
|
-
|
|
3992
|
-
//#endregion
|
|
3993
|
-
//#region src/pattern/meta/index.ts
|
|
3994
|
-
/**
|
|
3995
|
-
* Creates an Any meta pattern.
|
|
3996
|
-
*/
|
|
3997
|
-
function metaAny(pattern) {
|
|
3998
|
-
return {
|
|
3999
|
-
type: "Any",
|
|
4000
|
-
pattern
|
|
4001
|
-
};
|
|
4002
|
-
}
|
|
4003
|
-
/**
|
|
4004
|
-
* Creates an And meta pattern.
|
|
4005
|
-
*/
|
|
4006
|
-
function metaAnd(pattern) {
|
|
4007
|
-
return {
|
|
4008
|
-
type: "And",
|
|
4009
|
-
pattern
|
|
4010
|
-
};
|
|
4011
|
-
}
|
|
4012
|
-
/**
|
|
4013
|
-
* Creates an Or meta pattern.
|
|
4014
|
-
*/
|
|
4015
|
-
function metaOr(pattern) {
|
|
4016
|
-
return {
|
|
4017
|
-
type: "Or",
|
|
4018
|
-
pattern
|
|
4019
|
-
};
|
|
4020
|
-
}
|
|
4021
|
-
/**
|
|
4022
|
-
* Creates a Not meta pattern.
|
|
4023
|
-
*/
|
|
4024
|
-
function metaNot(pattern) {
|
|
4025
|
-
return {
|
|
4026
|
-
type: "Not",
|
|
4027
|
-
pattern
|
|
4028
|
-
};
|
|
4029
|
-
}
|
|
4030
|
-
/**
|
|
4031
|
-
* Creates a Capture meta pattern.
|
|
4032
|
-
*/
|
|
4033
|
-
function metaCapture(pattern) {
|
|
4034
|
-
return {
|
|
4035
|
-
type: "Capture",
|
|
4036
|
-
pattern
|
|
4037
|
-
};
|
|
4038
|
-
}
|
|
4039
|
-
/**
|
|
4040
|
-
* Creates a Search meta pattern.
|
|
4041
|
-
*/
|
|
4042
|
-
function metaSearch(pattern) {
|
|
4043
|
-
return {
|
|
4044
|
-
type: "Search",
|
|
4045
|
-
pattern
|
|
4046
|
-
};
|
|
4047
|
-
}
|
|
4048
|
-
/**
|
|
4049
|
-
* Creates a Traverse meta pattern.
|
|
4050
|
-
*/
|
|
4051
|
-
function metaTraverse(pattern) {
|
|
4052
|
-
return {
|
|
4053
|
-
type: "Traverse",
|
|
4054
|
-
pattern
|
|
4055
|
-
};
|
|
4056
|
-
}
|
|
4057
|
-
/**
|
|
4058
|
-
* Creates a Group meta pattern.
|
|
4059
|
-
*/
|
|
4060
|
-
function metaGroup(pattern) {
|
|
4061
|
-
return {
|
|
4062
|
-
type: "Group",
|
|
4063
|
-
pattern
|
|
4064
|
-
};
|
|
4065
|
-
}
|
|
4066
|
-
/**
|
|
4067
|
-
* Gets paths with captures for a meta pattern.
|
|
4068
|
-
*/
|
|
4069
|
-
function metaPatternPathsWithCaptures(pattern, haystack) {
|
|
4070
|
-
switch (pattern.type) {
|
|
4071
|
-
case "Any": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4072
|
-
case "And": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4073
|
-
case "Or": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4074
|
-
case "Not": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4075
|
-
case "Capture": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4076
|
-
case "Search": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4077
|
-
case "Traverse": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4078
|
-
case "Group": return pattern.pattern.pathsWithCaptures(haystack);
|
|
3792
|
+
function run(prog, root) {
|
|
3793
|
+
const out = [];
|
|
3794
|
+
runThread(prog, {
|
|
3795
|
+
pc: 0,
|
|
3796
|
+
env: root,
|
|
3797
|
+
path: [root],
|
|
3798
|
+
savedPaths: [],
|
|
3799
|
+
captures: prog.captureNames.map(() => []),
|
|
3800
|
+
captureStack: prog.captureNames.map(() => []),
|
|
3801
|
+
seen: /* @__PURE__ */ new Set()
|
|
3802
|
+
}, out);
|
|
3803
|
+
return out.map(([path, caps]) => {
|
|
3804
|
+
const map = /* @__PURE__ */ new Map();
|
|
3805
|
+
for (let i = 0; i < caps.length; i++) {
|
|
3806
|
+
const paths = caps[i];
|
|
3807
|
+
if (paths.length > 0) map.set(prog.captureNames[i], paths);
|
|
3808
|
+
}
|
|
3809
|
+
return [path, map];
|
|
3810
|
+
});
|
|
3811
|
+
}
|
|
3812
|
+
/**
|
|
3813
|
+
* Compile a pattern to bytecode program.
|
|
3814
|
+
*/
|
|
3815
|
+
function compile(pattern) {
|
|
3816
|
+
const code = [];
|
|
3817
|
+
const literals = [];
|
|
3818
|
+
const captureNames = [];
|
|
3819
|
+
collectCaptureNames$1(pattern, captureNames);
|
|
3820
|
+
compilePattern(pattern, code, literals, captureNames);
|
|
3821
|
+
code.push({ type: "Accept" });
|
|
3822
|
+
return {
|
|
3823
|
+
code,
|
|
3824
|
+
literals,
|
|
3825
|
+
captureNames
|
|
3826
|
+
};
|
|
3827
|
+
}
|
|
3828
|
+
/**
|
|
3829
|
+
* Collect capture names from a pattern recursively.
|
|
3830
|
+
*/
|
|
3831
|
+
function collectCaptureNames$1(pattern, out) {
|
|
3832
|
+
switch (pattern.type) {
|
|
3833
|
+
case "Leaf": break;
|
|
3834
|
+
case "Structure":
|
|
3835
|
+
collectStructureCaptureNames(pattern.pattern, out);
|
|
3836
|
+
break;
|
|
3837
|
+
case "Meta":
|
|
3838
|
+
collectMetaCaptureNames(pattern.pattern, out);
|
|
3839
|
+
break;
|
|
4079
3840
|
}
|
|
4080
3841
|
}
|
|
4081
|
-
|
|
4082
|
-
* Compiles a meta pattern to bytecode.
|
|
4083
|
-
*/
|
|
4084
|
-
function metaPatternCompile(pattern, code, literals, captures) {
|
|
3842
|
+
function collectStructureCaptureNames(pattern, out) {
|
|
4085
3843
|
switch (pattern.type) {
|
|
4086
|
-
case "
|
|
4087
|
-
pattern.pattern.
|
|
4088
|
-
|
|
4089
|
-
case "And":
|
|
4090
|
-
pattern.pattern.compile(code, literals, captures);
|
|
4091
|
-
break;
|
|
4092
|
-
case "Or":
|
|
4093
|
-
pattern.pattern.compile(code, literals, captures);
|
|
3844
|
+
case "Subject": {
|
|
3845
|
+
const inner = pattern.pattern.innerPattern();
|
|
3846
|
+
if (inner !== void 0) collectCaptureNames$1(inner, out);
|
|
4094
3847
|
break;
|
|
4095
|
-
|
|
4096
|
-
|
|
3848
|
+
}
|
|
3849
|
+
case "Predicate": {
|
|
3850
|
+
const inner = pattern.pattern.innerPattern();
|
|
3851
|
+
if (inner !== void 0) collectCaptureNames$1(inner, out);
|
|
4097
3852
|
break;
|
|
4098
|
-
|
|
4099
|
-
|
|
3853
|
+
}
|
|
3854
|
+
case "Object": {
|
|
3855
|
+
const inner = pattern.pattern.innerPattern();
|
|
3856
|
+
if (inner !== void 0) collectCaptureNames$1(inner, out);
|
|
4100
3857
|
break;
|
|
4101
|
-
|
|
4102
|
-
|
|
3858
|
+
}
|
|
3859
|
+
case "Assertions": {
|
|
3860
|
+
const predPat = pattern.pattern.predicatePattern();
|
|
3861
|
+
if (predPat !== void 0) collectCaptureNames$1(predPat, out);
|
|
3862
|
+
const objPat = pattern.pattern.objectPattern();
|
|
3863
|
+
if (objPat !== void 0) collectCaptureNames$1(objPat, out);
|
|
4103
3864
|
break;
|
|
4104
|
-
|
|
4105
|
-
|
|
3865
|
+
}
|
|
3866
|
+
case "Node": {
|
|
3867
|
+
const subjPat = pattern.pattern.subjectPattern();
|
|
3868
|
+
if (subjPat !== void 0) collectCaptureNames$1(subjPat, out);
|
|
3869
|
+
for (const assertionPat of pattern.pattern.assertionPatterns()) collectCaptureNames$1(assertionPat, out);
|
|
4106
3870
|
break;
|
|
4107
|
-
|
|
4108
|
-
|
|
3871
|
+
}
|
|
3872
|
+
case "Wrapped": {
|
|
3873
|
+
const inner = pattern.pattern.innerPattern();
|
|
3874
|
+
if (inner !== void 0) collectCaptureNames$1(inner, out);
|
|
4109
3875
|
break;
|
|
3876
|
+
}
|
|
3877
|
+
case "Digest":
|
|
3878
|
+
case "Obscured":
|
|
3879
|
+
case "Leaf": break;
|
|
4110
3880
|
}
|
|
4111
3881
|
}
|
|
4112
|
-
|
|
4113
|
-
* Checks if a meta pattern is complex.
|
|
4114
|
-
*/
|
|
4115
|
-
function metaPatternIsComplex(pattern) {
|
|
4116
|
-
switch (pattern.type) {
|
|
4117
|
-
case "Any": return pattern.pattern.isComplex();
|
|
4118
|
-
case "And": return pattern.pattern.isComplex();
|
|
4119
|
-
case "Or": return pattern.pattern.isComplex();
|
|
4120
|
-
case "Not": return pattern.pattern.isComplex();
|
|
4121
|
-
case "Capture": return pattern.pattern.isComplex();
|
|
4122
|
-
case "Search": return pattern.pattern.isComplex();
|
|
4123
|
-
case "Traverse": return pattern.pattern.isComplex();
|
|
4124
|
-
case "Group": return pattern.pattern.isComplex();
|
|
4125
|
-
}
|
|
4126
|
-
}
|
|
4127
|
-
/**
|
|
4128
|
-
* Converts a meta pattern to string.
|
|
4129
|
-
*/
|
|
4130
|
-
function metaPatternToString(pattern) {
|
|
4131
|
-
switch (pattern.type) {
|
|
4132
|
-
case "Any": return pattern.pattern.toString();
|
|
4133
|
-
case "And": return pattern.pattern.toString();
|
|
4134
|
-
case "Or": return pattern.pattern.toString();
|
|
4135
|
-
case "Not": return pattern.pattern.toString();
|
|
4136
|
-
case "Capture": return pattern.pattern.toString();
|
|
4137
|
-
case "Search": return pattern.pattern.toString();
|
|
4138
|
-
case "Traverse": return pattern.pattern.toString();
|
|
4139
|
-
case "Group": return pattern.pattern.toString();
|
|
4140
|
-
}
|
|
4141
|
-
}
|
|
4142
|
-
/**
|
|
4143
|
-
* Collects capture names from a meta pattern.
|
|
4144
|
-
*/
|
|
4145
|
-
function metaPatternCollectCaptureNames(pattern, out) {
|
|
3882
|
+
function collectMetaCaptureNames(pattern, out) {
|
|
4146
3883
|
switch (pattern.type) {
|
|
4147
3884
|
case "Any": break;
|
|
4148
3885
|
case "And":
|
|
4149
|
-
for (const
|
|
3886
|
+
for (const p of pattern.pattern.patterns()) collectCaptureNames$1(p, out);
|
|
4150
3887
|
break;
|
|
4151
3888
|
case "Or":
|
|
4152
|
-
for (const
|
|
3889
|
+
for (const p of pattern.pattern.patterns()) collectCaptureNames$1(p, out);
|
|
4153
3890
|
break;
|
|
4154
3891
|
case "Not":
|
|
4155
|
-
|
|
3892
|
+
collectCaptureNames$1(pattern.pattern.pattern(), out);
|
|
4156
3893
|
break;
|
|
4157
3894
|
case "Capture": {
|
|
4158
3895
|
const name = pattern.pattern.name();
|
|
4159
3896
|
if (!out.includes(name)) out.push(name);
|
|
4160
|
-
|
|
3897
|
+
collectCaptureNames$1(pattern.pattern.pattern(), out);
|
|
4161
3898
|
break;
|
|
4162
3899
|
}
|
|
4163
3900
|
case "Search":
|
|
4164
|
-
|
|
3901
|
+
collectCaptureNames$1(pattern.pattern.pattern(), out);
|
|
4165
3902
|
break;
|
|
4166
3903
|
case "Traverse":
|
|
4167
|
-
for (const
|
|
3904
|
+
for (const p of pattern.pattern.patterns()) collectCaptureNames$1(p, out);
|
|
4168
3905
|
break;
|
|
4169
3906
|
case "Group":
|
|
4170
|
-
|
|
3907
|
+
collectCaptureNames$1(pattern.pattern.pattern(), out);
|
|
4171
3908
|
break;
|
|
4172
3909
|
}
|
|
4173
3910
|
}
|
|
4174
3911
|
/**
|
|
4175
|
-
*
|
|
4176
|
-
*/
|
|
4177
|
-
function collectCaptureNamesFromPattern(pattern, out) {
|
|
4178
|
-
const p = pattern;
|
|
4179
|
-
if (p.collectCaptureNames !== void 0) p.collectCaptureNames(out);
|
|
4180
|
-
}
|
|
4181
|
-
|
|
4182
|
-
//#endregion
|
|
4183
|
-
//#region src/pattern/vm.ts
|
|
4184
|
-
let _patternPathsWithCaptures;
|
|
4185
|
-
let _patternMatches;
|
|
4186
|
-
let _patternPaths;
|
|
4187
|
-
/**
|
|
4188
|
-
* Register the pattern matching functions to resolve circular dependencies.
|
|
3912
|
+
* Compile a pattern to bytecode.
|
|
4189
3913
|
*/
|
|
4190
|
-
function
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
3914
|
+
function compilePattern(pattern, code, literals, captureNames) {
|
|
3915
|
+
switch (pattern.type) {
|
|
3916
|
+
case "Leaf":
|
|
3917
|
+
case "Structure":
|
|
3918
|
+
literals.push(pattern);
|
|
3919
|
+
code.push({
|
|
3920
|
+
type: "MatchPredicate",
|
|
3921
|
+
literalIndex: literals.length - 1
|
|
3922
|
+
});
|
|
3923
|
+
break;
|
|
3924
|
+
case "Meta":
|
|
3925
|
+
compileMetaPattern(pattern.pattern, code, literals, captureNames);
|
|
3926
|
+
break;
|
|
3927
|
+
}
|
|
4194
3928
|
}
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4215
|
-
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
3929
|
+
function compileMetaPattern(pattern, code, literals, captureNames) {
|
|
3930
|
+
switch (pattern.type) {
|
|
3931
|
+
case "Any": {
|
|
3932
|
+
const anyPattern = {
|
|
3933
|
+
type: "Meta",
|
|
3934
|
+
pattern
|
|
3935
|
+
};
|
|
3936
|
+
literals.push(anyPattern);
|
|
3937
|
+
code.push({
|
|
3938
|
+
type: "MatchPredicate",
|
|
3939
|
+
literalIndex: literals.length - 1
|
|
3940
|
+
});
|
|
3941
|
+
break;
|
|
3942
|
+
}
|
|
3943
|
+
case "And": {
|
|
3944
|
+
const patterns = pattern.pattern.patterns();
|
|
3945
|
+
for (const p of patterns) compilePattern(p, code, literals, captureNames);
|
|
3946
|
+
break;
|
|
3947
|
+
}
|
|
3948
|
+
case "Or": {
|
|
3949
|
+
const patterns = pattern.pattern.patterns();
|
|
3950
|
+
if (patterns.length === 0) return;
|
|
3951
|
+
if (patterns.length === 1) {
|
|
3952
|
+
compilePattern(patterns[0], code, literals, captureNames);
|
|
3953
|
+
return;
|
|
3954
|
+
}
|
|
3955
|
+
const jumpAddresses = [];
|
|
3956
|
+
for (let i = 0; i < patterns.length - 1; i++) {
|
|
3957
|
+
const splitAddr = code.length;
|
|
3958
|
+
code.push({
|
|
3959
|
+
type: "Split",
|
|
3960
|
+
a: 0,
|
|
3961
|
+
b: 0
|
|
3962
|
+
});
|
|
3963
|
+
const aStart = code.length;
|
|
3964
|
+
compilePattern(patterns[i], code, literals, captureNames);
|
|
3965
|
+
jumpAddresses.push(code.length);
|
|
3966
|
+
code.push({
|
|
3967
|
+
type: "Jump",
|
|
3968
|
+
address: 0
|
|
3969
|
+
});
|
|
3970
|
+
const bStart = code.length;
|
|
3971
|
+
code[splitAddr].a = aStart;
|
|
3972
|
+
code[splitAddr].b = bStart;
|
|
3973
|
+
}
|
|
3974
|
+
compilePattern(patterns[patterns.length - 1], code, literals, captureNames);
|
|
3975
|
+
const endAddr = code.length;
|
|
3976
|
+
for (const jumpAddr of jumpAddresses) code[jumpAddr].address = endAddr;
|
|
3977
|
+
break;
|
|
3978
|
+
}
|
|
3979
|
+
case "Not": {
|
|
3980
|
+
const innerPattern = pattern.pattern.pattern();
|
|
3981
|
+
literals.push(innerPattern);
|
|
3982
|
+
code.push({
|
|
3983
|
+
type: "NotMatch",
|
|
3984
|
+
patternIndex: literals.length - 1
|
|
3985
|
+
});
|
|
3986
|
+
break;
|
|
3987
|
+
}
|
|
3988
|
+
case "Capture": {
|
|
3989
|
+
const name = pattern.pattern.name();
|
|
3990
|
+
const captureIndex = captureNames.indexOf(name);
|
|
3991
|
+
code.push({
|
|
3992
|
+
type: "CaptureStart",
|
|
3993
|
+
captureIndex
|
|
3994
|
+
});
|
|
3995
|
+
compilePattern(pattern.pattern.pattern(), code, literals, captureNames);
|
|
3996
|
+
code.push({
|
|
3997
|
+
type: "CaptureEnd",
|
|
3998
|
+
captureIndex
|
|
3999
|
+
});
|
|
4000
|
+
break;
|
|
4001
|
+
}
|
|
4002
|
+
case "Search": {
|
|
4003
|
+
const innerCaptureNames = [];
|
|
4004
|
+
collectCaptureNames$1(pattern.pattern.pattern(), innerCaptureNames);
|
|
4005
|
+
const captureMap = innerCaptureNames.map((name) => {
|
|
4006
|
+
const idx = captureNames.indexOf(name);
|
|
4007
|
+
return [name, idx >= 0 ? idx : 0];
|
|
4008
|
+
});
|
|
4009
|
+
literals.push(pattern.pattern.pattern());
|
|
4010
|
+
code.push({
|
|
4011
|
+
type: "Search",
|
|
4012
|
+
patternIndex: literals.length - 1,
|
|
4013
|
+
captureMap
|
|
4014
|
+
});
|
|
4015
|
+
break;
|
|
4016
|
+
}
|
|
4017
|
+
case "Traverse": {
|
|
4018
|
+
const patterns = pattern.pattern.patterns();
|
|
4019
|
+
if (patterns.length > 0) {
|
|
4020
|
+
compilePattern(patterns[0], code, literals, captureNames);
|
|
4021
|
+
for (let i = 1; i < patterns.length; i++) {
|
|
4022
|
+
code.push({ type: "ExtendTraversal" });
|
|
4023
|
+
compilePattern(patterns[i], code, literals, captureNames);
|
|
4219
4024
|
}
|
|
4220
|
-
|
|
4221
|
-
|
|
4025
|
+
for (let i = 1; i < patterns.length; i++) code.push({ type: "CombineTraversal" });
|
|
4026
|
+
}
|
|
4027
|
+
break;
|
|
4028
|
+
}
|
|
4029
|
+
case "Group": {
|
|
4030
|
+
const quantifier = pattern.pattern.quantifier();
|
|
4031
|
+
if (quantifier !== void 0 && !(quantifier.min() === 1 && quantifier.max() === 1)) {
|
|
4032
|
+
literals.push(pattern.pattern.pattern());
|
|
4033
|
+
code.push({
|
|
4034
|
+
type: "Repeat",
|
|
4035
|
+
patternIndex: literals.length - 1,
|
|
4036
|
+
quantifier
|
|
4037
|
+
});
|
|
4038
|
+
} else compilePattern(pattern.pattern.pattern(), code, literals, captureNames);
|
|
4039
|
+
break;
|
|
4040
|
+
}
|
|
4222
4041
|
}
|
|
4223
4042
|
}
|
|
4224
|
-
|
|
4225
|
-
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
|
|
4229
|
-
|
|
4230
|
-
env: th.env,
|
|
4231
|
-
path: [...th.path],
|
|
4232
|
-
savedPaths: th.savedPaths.map((p) => [...p]),
|
|
4233
|
-
captures: th.captures.map((c) => c.map((p) => [...p])),
|
|
4234
|
-
captureStack: th.captureStack.map((s) => [...s]),
|
|
4235
|
-
seen: new Set(th.seen)
|
|
4236
|
-
};
|
|
4237
|
-
}
|
|
4238
|
-
/**
|
|
4239
|
-
* Get a unique key for a path based on envelope digests.
|
|
4240
|
-
*/
|
|
4241
|
-
function pathKey(path) {
|
|
4242
|
-
return path.map((e) => e.digest().hex()).join(",");
|
|
4043
|
+
|
|
4044
|
+
//#endregion
|
|
4045
|
+
//#region src/pattern/meta/any-pattern.ts
|
|
4046
|
+
let createMetaAnyPattern;
|
|
4047
|
+
function registerAnyPatternFactory(factory) {
|
|
4048
|
+
createMetaAnyPattern = factory;
|
|
4243
4049
|
}
|
|
4244
4050
|
/**
|
|
4245
|
-
*
|
|
4051
|
+
* A pattern that matches any element.
|
|
4246
4052
|
*
|
|
4247
|
-
*
|
|
4248
|
-
* MatchPredicate instructions - Leaf, Structure, Any patterns.
|
|
4053
|
+
* Corresponds to the Rust `AnyPattern` struct in any_pattern.rs
|
|
4249
4054
|
*/
|
|
4250
|
-
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4256
|
-
|
|
4257
|
-
|
|
4055
|
+
var AnyPattern = class AnyPattern {
|
|
4056
|
+
constructor() {}
|
|
4057
|
+
/**
|
|
4058
|
+
* Creates a new AnyPattern.
|
|
4059
|
+
*/
|
|
4060
|
+
static new() {
|
|
4061
|
+
return new AnyPattern();
|
|
4062
|
+
}
|
|
4063
|
+
pathsWithCaptures(haystack) {
|
|
4064
|
+
return [[[haystack]], /* @__PURE__ */ new Map()];
|
|
4065
|
+
}
|
|
4066
|
+
paths(haystack) {
|
|
4067
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4068
|
+
}
|
|
4069
|
+
matches(_haystack) {
|
|
4070
|
+
return true;
|
|
4071
|
+
}
|
|
4072
|
+
compile(code, literals, captures) {
|
|
4073
|
+
if (createMetaAnyPattern === void 0) throw new Error("AnyPattern factory not registered");
|
|
4074
|
+
compileAsAtomic(createMetaAnyPattern(this), code, literals, captures);
|
|
4075
|
+
}
|
|
4076
|
+
isComplex() {
|
|
4077
|
+
return false;
|
|
4078
|
+
}
|
|
4079
|
+
toString() {
|
|
4080
|
+
return "*";
|
|
4081
|
+
}
|
|
4082
|
+
/**
|
|
4083
|
+
* Equality comparison.
|
|
4084
|
+
*/
|
|
4085
|
+
equals(_other) {
|
|
4086
|
+
return true;
|
|
4258
4087
|
}
|
|
4088
|
+
/**
|
|
4089
|
+
* Hash code for use in Maps/Sets.
|
|
4090
|
+
*/
|
|
4091
|
+
hashCode() {
|
|
4092
|
+
return 0;
|
|
4093
|
+
}
|
|
4094
|
+
};
|
|
4095
|
+
|
|
4096
|
+
//#endregion
|
|
4097
|
+
//#region src/pattern/meta/and-pattern.ts
|
|
4098
|
+
let createMetaAndPattern;
|
|
4099
|
+
function registerAndPatternFactory(factory) {
|
|
4100
|
+
createMetaAndPattern = factory;
|
|
4259
4101
|
}
|
|
4260
4102
|
/**
|
|
4261
|
-
*
|
|
4103
|
+
* A pattern that matches if all contained patterns match.
|
|
4104
|
+
*
|
|
4105
|
+
* Corresponds to the Rust `AndPattern` struct in and_pattern.rs
|
|
4262
4106
|
*/
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
const next = [];
|
|
4268
|
-
const lastState = states[states.length - 1];
|
|
4269
|
-
for (const [e, pth] of lastState) {
|
|
4270
|
-
const subPaths = _patternPaths(pat, e);
|
|
4271
|
-
for (const subPath of subPaths) {
|
|
4272
|
-
const last = subPath[subPath.length - 1];
|
|
4273
|
-
if (last?.digest().hex() === e.digest().hex()) continue;
|
|
4274
|
-
if (last !== void 0) {
|
|
4275
|
-
const combined = [...pth];
|
|
4276
|
-
if (subPath[0]?.digest().hex() === e.digest().hex()) combined.push(...subPath.slice(1));
|
|
4277
|
-
else combined.push(...subPath);
|
|
4278
|
-
next.push([last, combined]);
|
|
4279
|
-
}
|
|
4280
|
-
}
|
|
4281
|
-
}
|
|
4282
|
-
if (next.length === 0) break;
|
|
4283
|
-
states.push(next);
|
|
4284
|
-
}
|
|
4285
|
-
const hasZeroRep = quantifier.min() === 0;
|
|
4286
|
-
const zeroRepResult = hasZeroRep ? [[env, [...path]]] : [];
|
|
4287
|
-
const maxPossible = states.length - 1;
|
|
4288
|
-
const maxAllowed = Math.min(bound, maxPossible);
|
|
4289
|
-
if (maxAllowed < quantifier.min() && quantifier.min() > 0) return [];
|
|
4290
|
-
const minCount = quantifier.min() === 0 ? 1 : quantifier.min();
|
|
4291
|
-
if (maxAllowed < minCount) return zeroRepResult;
|
|
4292
|
-
const maxCount = maxAllowed;
|
|
4293
|
-
let counts;
|
|
4294
|
-
switch (quantifier.reluctance()) {
|
|
4295
|
-
case _bcts_dcbor_pattern.Reluctance.Greedy:
|
|
4296
|
-
counts = [];
|
|
4297
|
-
for (let c = maxCount; c >= minCount; c--) counts.push(c);
|
|
4298
|
-
break;
|
|
4299
|
-
case _bcts_dcbor_pattern.Reluctance.Lazy:
|
|
4300
|
-
counts = [];
|
|
4301
|
-
for (let c = minCount; c <= maxCount; c++) counts.push(c);
|
|
4302
|
-
break;
|
|
4303
|
-
case _bcts_dcbor_pattern.Reluctance.Possessive:
|
|
4304
|
-
counts = maxCount >= minCount ? [maxCount] : [];
|
|
4305
|
-
break;
|
|
4107
|
+
var AndPattern = class AndPattern {
|
|
4108
|
+
_patterns;
|
|
4109
|
+
constructor(patterns) {
|
|
4110
|
+
this._patterns = patterns;
|
|
4306
4111
|
}
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4112
|
+
/**
|
|
4113
|
+
* Creates a new AndPattern with the given patterns.
|
|
4114
|
+
*/
|
|
4115
|
+
static new(patterns) {
|
|
4116
|
+
return new AndPattern(patterns);
|
|
4117
|
+
}
|
|
4118
|
+
/**
|
|
4119
|
+
* Gets the patterns.
|
|
4120
|
+
*/
|
|
4121
|
+
patterns() {
|
|
4122
|
+
return this._patterns;
|
|
4123
|
+
}
|
|
4124
|
+
pathsWithCaptures(haystack) {
|
|
4125
|
+
return [this._patterns.every((pattern) => matchPattern(pattern, haystack)) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
4126
|
+
}
|
|
4127
|
+
paths(haystack) {
|
|
4128
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4129
|
+
}
|
|
4130
|
+
matches(haystack) {
|
|
4131
|
+
return this.paths(haystack).length > 0;
|
|
4132
|
+
}
|
|
4133
|
+
compile(code, literals, captures) {
|
|
4134
|
+
for (const pattern of this._patterns) dispatchCompile(pattern, code, literals, captures);
|
|
4135
|
+
}
|
|
4136
|
+
isComplex() {
|
|
4137
|
+
return this._patterns.length > 1 || this._patterns.some((p) => dispatchIsComplex(p));
|
|
4138
|
+
}
|
|
4139
|
+
toString() {
|
|
4140
|
+
return this._patterns.map((p) => dispatchPatternToString(p)).join(" & ");
|
|
4141
|
+
}
|
|
4142
|
+
/**
|
|
4143
|
+
* Equality comparison.
|
|
4144
|
+
*/
|
|
4145
|
+
equals(other) {
|
|
4146
|
+
if (this._patterns.length !== other._patterns.length) return false;
|
|
4147
|
+
for (let i = 0; i < this._patterns.length; i++) if (this._patterns[i] !== other._patterns[i]) return false;
|
|
4148
|
+
return true;
|
|
4149
|
+
}
|
|
4150
|
+
/**
|
|
4151
|
+
* Hash code for use in Maps/Sets.
|
|
4152
|
+
*/
|
|
4153
|
+
hashCode() {
|
|
4154
|
+
return this._patterns.length;
|
|
4155
|
+
}
|
|
4156
|
+
};
|
|
4157
|
+
|
|
4158
|
+
//#endregion
|
|
4159
|
+
//#region src/pattern/meta/or-pattern.ts
|
|
4160
|
+
let createMetaOrPattern;
|
|
4161
|
+
function registerOrPatternFactory(factory) {
|
|
4162
|
+
createMetaOrPattern = factory;
|
|
4163
|
+
}
|
|
4164
|
+
/**
|
|
4165
|
+
* A pattern that matches if any contained pattern matches.
|
|
4166
|
+
*
|
|
4167
|
+
* Corresponds to the Rust `OrPattern` struct in or_pattern.rs
|
|
4168
|
+
*/
|
|
4169
|
+
var OrPattern = class OrPattern {
|
|
4170
|
+
_patterns;
|
|
4171
|
+
constructor(patterns) {
|
|
4172
|
+
this._patterns = patterns;
|
|
4173
|
+
}
|
|
4174
|
+
/**
|
|
4175
|
+
* Creates a new OrPattern with the given patterns.
|
|
4176
|
+
*/
|
|
4177
|
+
static new(patterns) {
|
|
4178
|
+
return new OrPattern(patterns);
|
|
4179
|
+
}
|
|
4180
|
+
/**
|
|
4181
|
+
* Gets the patterns.
|
|
4182
|
+
*/
|
|
4183
|
+
patterns() {
|
|
4184
|
+
return this._patterns;
|
|
4185
|
+
}
|
|
4186
|
+
pathsWithCaptures(haystack) {
|
|
4187
|
+
for (const pattern of this._patterns) {
|
|
4188
|
+
const [paths, captures] = dispatchPathsWithCaptures(pattern, haystack);
|
|
4189
|
+
if (paths.length > 0) return [paths, captures];
|
|
4312
4190
|
}
|
|
4313
|
-
|
|
4314
|
-
}
|
|
4315
|
-
|
|
4316
|
-
|
|
4317
|
-
|
|
4318
|
-
|
|
4191
|
+
return [[], /* @__PURE__ */ new Map()];
|
|
4192
|
+
}
|
|
4193
|
+
paths(haystack) {
|
|
4194
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4195
|
+
}
|
|
4196
|
+
matches(haystack) {
|
|
4197
|
+
return this.paths(haystack).length > 0;
|
|
4198
|
+
}
|
|
4199
|
+
compile(code, literals, captures) {
|
|
4200
|
+
if (this._patterns.length === 0) return;
|
|
4201
|
+
const splits = [];
|
|
4202
|
+
for (let i = 0; i < this._patterns.length - 1; i++) {
|
|
4203
|
+
splits.push(code.length);
|
|
4204
|
+
code.push({
|
|
4205
|
+
type: "Split",
|
|
4206
|
+
a: 0,
|
|
4207
|
+
b: 0
|
|
4208
|
+
});
|
|
4209
|
+
}
|
|
4210
|
+
const jumps = [];
|
|
4211
|
+
for (let i = 0; i < this._patterns.length; i++) {
|
|
4212
|
+
const patternStart = code.length;
|
|
4213
|
+
const pattern = this._patterns[i];
|
|
4214
|
+
dispatchCompile(pattern, code, literals, captures);
|
|
4215
|
+
const jumpPastAll = code.length;
|
|
4216
|
+
code.push({
|
|
4217
|
+
type: "Jump",
|
|
4218
|
+
address: 0
|
|
4219
|
+
});
|
|
4220
|
+
jumps.push(jumpPastAll);
|
|
4221
|
+
if (i < this._patterns.length - 1) {
|
|
4222
|
+
const nextPattern = code.length;
|
|
4223
|
+
code[splits[i]] = {
|
|
4224
|
+
type: "Split",
|
|
4225
|
+
a: patternStart,
|
|
4226
|
+
b: nextPattern
|
|
4227
|
+
};
|
|
4228
|
+
}
|
|
4319
4229
|
}
|
|
4230
|
+
const pastAll = code.length;
|
|
4231
|
+
for (const jumpIdx of jumps) code[jumpIdx] = {
|
|
4232
|
+
type: "Jump",
|
|
4233
|
+
address: pastAll
|
|
4234
|
+
};
|
|
4320
4235
|
}
|
|
4321
|
-
|
|
4236
|
+
isComplex() {
|
|
4237
|
+
return this._patterns.length > 1 || this._patterns.some((p) => dispatchIsComplex(p));
|
|
4238
|
+
}
|
|
4239
|
+
toString() {
|
|
4240
|
+
return this._patterns.map((p) => dispatchPatternToString(p)).join(" | ");
|
|
4241
|
+
}
|
|
4242
|
+
/**
|
|
4243
|
+
* Equality comparison.
|
|
4244
|
+
*/
|
|
4245
|
+
equals(other) {
|
|
4246
|
+
if (this._patterns.length !== other._patterns.length) return false;
|
|
4247
|
+
for (let i = 0; i < this._patterns.length; i++) if (this._patterns[i] !== other._patterns[i]) return false;
|
|
4248
|
+
return true;
|
|
4249
|
+
}
|
|
4250
|
+
/**
|
|
4251
|
+
* Hash code for use in Maps/Sets.
|
|
4252
|
+
*/
|
|
4253
|
+
hashCode() {
|
|
4254
|
+
return this._patterns.length;
|
|
4255
|
+
}
|
|
4256
|
+
};
|
|
4257
|
+
|
|
4258
|
+
//#endregion
|
|
4259
|
+
//#region src/pattern/meta/not-pattern.ts
|
|
4260
|
+
let createMetaNotPattern;
|
|
4261
|
+
function registerNotPatternFactory(factory) {
|
|
4262
|
+
createMetaNotPattern = factory;
|
|
4322
4263
|
}
|
|
4323
4264
|
/**
|
|
4324
|
-
*
|
|
4325
|
-
*
|
|
4265
|
+
* A pattern that negates another pattern; matches when the inner pattern does not match.
|
|
4266
|
+
*
|
|
4267
|
+
* Corresponds to the Rust `NotPattern` struct in not_pattern.rs
|
|
4326
4268
|
*/
|
|
4327
|
-
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4334
|
-
|
|
4335
|
-
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
|
|
4354
|
-
|
|
4355
|
-
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4370
|
-
|
|
4371
|
-
|
|
4372
|
-
|
|
4373
|
-
|
|
4374
|
-
|
|
4375
|
-
|
|
4376
|
-
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
|
|
4383
|
-
|
|
4384
|
-
|
|
4385
|
-
|
|
4386
|
-
|
|
4387
|
-
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
|
|
4391
|
-
|
|
4392
|
-
|
|
4393
|
-
|
|
4394
|
-
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4422
|
-
|
|
4423
|
-
|
|
4424
|
-
|
|
4425
|
-
|
|
4426
|
-
|
|
4427
|
-
|
|
4428
|
-
|
|
4429
|
-
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
4451
|
-
|
|
4452
|
-
|
|
4453
|
-
|
|
4454
|
-
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4461
|
-
|
|
4462
|
-
|
|
4463
|
-
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
|
|
4468
|
-
|
|
4469
|
-
|
|
4470
|
-
|
|
4471
|
-
|
|
4472
|
-
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4477
|
-
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4484
|
-
|
|
4485
|
-
|
|
4486
|
-
|
|
4487
|
-
|
|
4488
|
-
|
|
4489
|
-
|
|
4490
|
-
|
|
4491
|
-
|
|
4492
|
-
|
|
4493
|
-
|
|
4494
|
-
|
|
4495
|
-
|
|
4496
|
-
|
|
4497
|
-
|
|
4498
|
-
|
|
4499
|
-
|
|
4500
|
-
|
|
4501
|
-
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
else combined.push(...th.path);
|
|
4506
|
-
th.path = combined;
|
|
4507
|
-
}
|
|
4508
|
-
th.pc += 1;
|
|
4509
|
-
continue;
|
|
4510
|
-
}
|
|
4511
|
-
case "NavigateSubject":
|
|
4512
|
-
if (th.env.isNode()) {
|
|
4513
|
-
const subject = th.env.subject();
|
|
4514
|
-
th.env = subject;
|
|
4515
|
-
th.path.push(subject);
|
|
4516
|
-
}
|
|
4517
|
-
th.pc += 1;
|
|
4518
|
-
continue;
|
|
4519
|
-
case "NotMatch": {
|
|
4520
|
-
const pattern = prog.literals[instr.patternIndex];
|
|
4521
|
-
if (_patternMatches(pattern, th.env)) break;
|
|
4522
|
-
else {
|
|
4523
|
-
th.pc += 1;
|
|
4524
|
-
continue;
|
|
4525
|
-
}
|
|
4526
|
-
}
|
|
4527
|
-
case "Repeat": {
|
|
4528
|
-
const pat = prog.literals[instr.patternIndex];
|
|
4529
|
-
const results = repeatPaths(pat, th.env, th.path, instr.quantifier);
|
|
4530
|
-
if (results.length === 0) break;
|
|
4531
|
-
const nextPc = th.pc + 1;
|
|
4532
|
-
let success = false;
|
|
4533
|
-
for (const [envAfter, pathAfter] of results) {
|
|
4534
|
-
const fork = cloneThread(th);
|
|
4535
|
-
fork.pc = nextPc;
|
|
4536
|
-
fork.env = envAfter;
|
|
4537
|
-
fork.path = pathAfter;
|
|
4538
|
-
if (runThread(prog, fork, out)) {
|
|
4539
|
-
produced = true;
|
|
4540
|
-
success = true;
|
|
4541
|
-
break;
|
|
4542
|
-
}
|
|
4543
|
-
}
|
|
4544
|
-
if (!success) {}
|
|
4545
|
-
break;
|
|
4546
|
-
}
|
|
4547
|
-
case "CaptureStart": {
|
|
4548
|
-
const id = instr.captureIndex;
|
|
4549
|
-
if (id < th.captureStack.length) th.captureStack[id].push(th.path.length - 1);
|
|
4550
|
-
th.pc += 1;
|
|
4551
|
-
continue;
|
|
4269
|
+
var NotPattern = class NotPattern {
|
|
4270
|
+
_pattern;
|
|
4271
|
+
constructor(pattern) {
|
|
4272
|
+
this._pattern = pattern;
|
|
4273
|
+
}
|
|
4274
|
+
/**
|
|
4275
|
+
* Creates a new NotPattern with the given pattern.
|
|
4276
|
+
*/
|
|
4277
|
+
static new(pattern) {
|
|
4278
|
+
return new NotPattern(pattern);
|
|
4279
|
+
}
|
|
4280
|
+
/**
|
|
4281
|
+
* Gets the inner pattern.
|
|
4282
|
+
*/
|
|
4283
|
+
pattern() {
|
|
4284
|
+
return this._pattern;
|
|
4285
|
+
}
|
|
4286
|
+
pathsWithCaptures(haystack) {
|
|
4287
|
+
return [!matchPattern(this._pattern, haystack) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
4288
|
+
}
|
|
4289
|
+
paths(haystack) {
|
|
4290
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4291
|
+
}
|
|
4292
|
+
matches(haystack) {
|
|
4293
|
+
return this.paths(haystack).length > 0;
|
|
4294
|
+
}
|
|
4295
|
+
compile(code, literals, _captures) {
|
|
4296
|
+
const idx = literals.length;
|
|
4297
|
+
literals.push(this._pattern);
|
|
4298
|
+
code.push({
|
|
4299
|
+
type: "NotMatch",
|
|
4300
|
+
patternIndex: idx
|
|
4301
|
+
});
|
|
4302
|
+
}
|
|
4303
|
+
isComplex() {
|
|
4304
|
+
return false;
|
|
4305
|
+
}
|
|
4306
|
+
toString() {
|
|
4307
|
+
return `!${dispatchPatternToString(this._pattern)}`;
|
|
4308
|
+
}
|
|
4309
|
+
/**
|
|
4310
|
+
* Equality comparison.
|
|
4311
|
+
*/
|
|
4312
|
+
equals(other) {
|
|
4313
|
+
return this._pattern === other._pattern;
|
|
4314
|
+
}
|
|
4315
|
+
/**
|
|
4316
|
+
* Hash code for use in Maps/Sets.
|
|
4317
|
+
*/
|
|
4318
|
+
hashCode() {
|
|
4319
|
+
return 1;
|
|
4320
|
+
}
|
|
4321
|
+
};
|
|
4322
|
+
|
|
4323
|
+
//#endregion
|
|
4324
|
+
//#region src/pattern/meta/capture-pattern.ts
|
|
4325
|
+
let createMetaCapturePattern;
|
|
4326
|
+
function registerCapturePatternFactory(factory) {
|
|
4327
|
+
createMetaCapturePattern = factory;
|
|
4328
|
+
}
|
|
4329
|
+
/**
|
|
4330
|
+
* A pattern that captures a match with a name.
|
|
4331
|
+
*
|
|
4332
|
+
* Corresponds to the Rust `CapturePattern` struct in capture_pattern.rs
|
|
4333
|
+
*/
|
|
4334
|
+
var CapturePattern = class CapturePattern {
|
|
4335
|
+
_name;
|
|
4336
|
+
_pattern;
|
|
4337
|
+
constructor(name, pattern) {
|
|
4338
|
+
this._name = name;
|
|
4339
|
+
this._pattern = pattern;
|
|
4340
|
+
}
|
|
4341
|
+
/**
|
|
4342
|
+
* Creates a new CapturePattern with the given name and pattern.
|
|
4343
|
+
*/
|
|
4344
|
+
static new(name, pattern) {
|
|
4345
|
+
return new CapturePattern(name, pattern);
|
|
4346
|
+
}
|
|
4347
|
+
/**
|
|
4348
|
+
* Gets the name of the capture.
|
|
4349
|
+
*/
|
|
4350
|
+
name() {
|
|
4351
|
+
return this._name;
|
|
4352
|
+
}
|
|
4353
|
+
/**
|
|
4354
|
+
* Gets the inner pattern.
|
|
4355
|
+
*/
|
|
4356
|
+
pattern() {
|
|
4357
|
+
return this._pattern;
|
|
4358
|
+
}
|
|
4359
|
+
pathsWithCaptures(haystack) {
|
|
4360
|
+
const [paths, caps] = dispatchPathsWithCaptures(this._pattern, haystack);
|
|
4361
|
+
if (paths.length > 0) {
|
|
4362
|
+
const existing = caps.get(this._name) ?? [];
|
|
4363
|
+
caps.set(this._name, [...existing, ...paths]);
|
|
4364
|
+
}
|
|
4365
|
+
return [paths, caps];
|
|
4366
|
+
}
|
|
4367
|
+
paths(haystack) {
|
|
4368
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4369
|
+
}
|
|
4370
|
+
matches(haystack) {
|
|
4371
|
+
return this.paths(haystack).length > 0;
|
|
4372
|
+
}
|
|
4373
|
+
compile(code, literals, captures) {
|
|
4374
|
+
const id = captures.length;
|
|
4375
|
+
captures.push(this._name);
|
|
4376
|
+
code.push({
|
|
4377
|
+
type: "CaptureStart",
|
|
4378
|
+
captureIndex: id
|
|
4379
|
+
});
|
|
4380
|
+
dispatchCompile(this._pattern, code, literals, captures);
|
|
4381
|
+
code.push({
|
|
4382
|
+
type: "CaptureEnd",
|
|
4383
|
+
captureIndex: id
|
|
4384
|
+
});
|
|
4385
|
+
}
|
|
4386
|
+
isComplex() {
|
|
4387
|
+
return false;
|
|
4388
|
+
}
|
|
4389
|
+
toString() {
|
|
4390
|
+
return `@${this._name}(${dispatchPatternToString(this._pattern)})`;
|
|
4391
|
+
}
|
|
4392
|
+
/**
|
|
4393
|
+
* Equality comparison.
|
|
4394
|
+
*/
|
|
4395
|
+
equals(other) {
|
|
4396
|
+
return this._name === other._name && this._pattern === other._pattern;
|
|
4397
|
+
}
|
|
4398
|
+
/**
|
|
4399
|
+
* Hash code for use in Maps/Sets.
|
|
4400
|
+
*/
|
|
4401
|
+
hashCode() {
|
|
4402
|
+
let hash = 0;
|
|
4403
|
+
for (const char of this._name) hash = hash * 31 + char.charCodeAt(0) | 0;
|
|
4404
|
+
return hash;
|
|
4405
|
+
}
|
|
4406
|
+
};
|
|
4407
|
+
|
|
4408
|
+
//#endregion
|
|
4409
|
+
//#region src/pattern/meta/search-pattern.ts
|
|
4410
|
+
let createMetaSearchPattern;
|
|
4411
|
+
function registerSearchPatternFactory(factory) {
|
|
4412
|
+
createMetaSearchPattern = factory;
|
|
4413
|
+
}
|
|
4414
|
+
/**
|
|
4415
|
+
* A pattern that searches the entire envelope tree for matches.
|
|
4416
|
+
*
|
|
4417
|
+
* Corresponds to the Rust `SearchPattern` struct in search_pattern.rs
|
|
4418
|
+
*/
|
|
4419
|
+
var SearchPattern = class SearchPattern {
|
|
4420
|
+
_pattern;
|
|
4421
|
+
constructor(pattern) {
|
|
4422
|
+
this._pattern = pattern;
|
|
4423
|
+
}
|
|
4424
|
+
/**
|
|
4425
|
+
* Creates a new SearchPattern with the given pattern.
|
|
4426
|
+
*/
|
|
4427
|
+
static new(pattern) {
|
|
4428
|
+
return new SearchPattern(pattern);
|
|
4429
|
+
}
|
|
4430
|
+
/**
|
|
4431
|
+
* Gets the inner pattern.
|
|
4432
|
+
*/
|
|
4433
|
+
pattern() {
|
|
4434
|
+
return this._pattern;
|
|
4435
|
+
}
|
|
4436
|
+
pathsWithCaptures(haystack) {
|
|
4437
|
+
const resultPaths = [];
|
|
4438
|
+
this._walkEnvelope(haystack, [], (currentEnvelope, pathToCurrent) => {
|
|
4439
|
+
const newPath = [...pathToCurrent, currentEnvelope];
|
|
4440
|
+
const patternPaths = dispatchPaths(this._pattern, currentEnvelope);
|
|
4441
|
+
for (const patternPath of patternPaths) {
|
|
4442
|
+
const fullPath = [...newPath];
|
|
4443
|
+
if (patternPath.length > 1) fullPath.push(...patternPath.slice(1));
|
|
4444
|
+
else if (patternPath.length === 1) {
|
|
4445
|
+
const firstEnv = patternPath[0];
|
|
4446
|
+
if (firstEnv !== void 0 && !firstEnv.digest().equals(currentEnvelope.digest())) fullPath.push(...patternPath);
|
|
4552
4447
|
}
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4564
|
-
|
|
4565
|
-
|
|
4448
|
+
resultPaths.push(fullPath);
|
|
4449
|
+
}
|
|
4450
|
+
});
|
|
4451
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4452
|
+
const uniquePaths = [];
|
|
4453
|
+
for (const path of resultPaths) {
|
|
4454
|
+
const digestPath = path.map((e) => e.digest().hex()).join(",");
|
|
4455
|
+
if (!seen.has(digestPath)) {
|
|
4456
|
+
seen.add(digestPath);
|
|
4457
|
+
uniquePaths.push(path);
|
|
4458
|
+
}
|
|
4459
|
+
}
|
|
4460
|
+
return [uniquePaths, /* @__PURE__ */ new Map()];
|
|
4461
|
+
}
|
|
4462
|
+
/**
|
|
4463
|
+
* Walk the envelope tree recursively.
|
|
4464
|
+
*/
|
|
4465
|
+
_walkEnvelope(envelope, pathToCurrent, visitor) {
|
|
4466
|
+
visitor(envelope, pathToCurrent);
|
|
4467
|
+
const subject = envelope.subject();
|
|
4468
|
+
const newPath = [...pathToCurrent, envelope];
|
|
4469
|
+
if (!subject.digest().equals(envelope.digest())) this._walkEnvelope(subject, newPath, visitor);
|
|
4470
|
+
for (const assertion of envelope.assertions()) {
|
|
4471
|
+
this._walkEnvelope(assertion, newPath, visitor);
|
|
4472
|
+
const predicate = assertion.asPredicate?.();
|
|
4473
|
+
if (predicate !== void 0) {
|
|
4474
|
+
const assertionPath = [...newPath, assertion];
|
|
4475
|
+
this._walkEnvelope(predicate, assertionPath, visitor);
|
|
4476
|
+
}
|
|
4477
|
+
const object = assertion.asObject?.();
|
|
4478
|
+
if (object !== void 0) {
|
|
4479
|
+
const assertionPath = [...newPath, assertion];
|
|
4480
|
+
this._walkEnvelope(object, assertionPath, visitor);
|
|
4481
|
+
}
|
|
4482
|
+
}
|
|
4483
|
+
if (subject.isWrapped()) {
|
|
4484
|
+
const unwrapped = subject.tryUnwrap?.();
|
|
4485
|
+
if (unwrapped !== void 0) this._walkEnvelope(unwrapped, newPath, visitor);
|
|
4486
|
+
}
|
|
4487
|
+
}
|
|
4488
|
+
paths(haystack) {
|
|
4489
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4490
|
+
}
|
|
4491
|
+
matches(haystack) {
|
|
4492
|
+
return this.paths(haystack).length > 0;
|
|
4493
|
+
}
|
|
4494
|
+
compile(code, literals, captures) {
|
|
4495
|
+
const idx = literals.length;
|
|
4496
|
+
literals.push(this._pattern);
|
|
4497
|
+
const innerNames = [];
|
|
4498
|
+
collectCaptureNames(this._pattern, innerNames);
|
|
4499
|
+
const captureMap = [];
|
|
4500
|
+
for (const name of innerNames) {
|
|
4501
|
+
let pos = captures.indexOf(name);
|
|
4502
|
+
if (pos === -1) {
|
|
4503
|
+
pos = captures.length;
|
|
4504
|
+
captures.push(name);
|
|
4505
|
+
}
|
|
4506
|
+
captureMap.push([name, pos]);
|
|
4507
|
+
}
|
|
4508
|
+
code.push({
|
|
4509
|
+
type: "Search",
|
|
4510
|
+
patternIndex: idx,
|
|
4511
|
+
captureMap
|
|
4512
|
+
});
|
|
4513
|
+
}
|
|
4514
|
+
isComplex() {
|
|
4515
|
+
return true;
|
|
4516
|
+
}
|
|
4517
|
+
toString() {
|
|
4518
|
+
return `search(${dispatchPatternToString(this._pattern)})`;
|
|
4519
|
+
}
|
|
4520
|
+
/**
|
|
4521
|
+
* Equality comparison.
|
|
4522
|
+
*/
|
|
4523
|
+
equals(other) {
|
|
4524
|
+
return this._pattern === other._pattern;
|
|
4525
|
+
}
|
|
4526
|
+
/**
|
|
4527
|
+
* Hash code for use in Maps/Sets.
|
|
4528
|
+
*/
|
|
4529
|
+
hashCode() {
|
|
4530
|
+
return 1;
|
|
4531
|
+
}
|
|
4532
|
+
};
|
|
4533
|
+
/**
|
|
4534
|
+
* Collect capture names from a pattern.
|
|
4535
|
+
*/
|
|
4536
|
+
function collectCaptureNames(pattern, out) {
|
|
4537
|
+
const p = pattern;
|
|
4538
|
+
if (p.collectCaptureNames !== void 0) p.collectCaptureNames(out);
|
|
4539
|
+
}
|
|
4540
|
+
|
|
4541
|
+
//#endregion
|
|
4542
|
+
//#region src/pattern/meta/traverse-pattern.ts
|
|
4543
|
+
let createMetaTraversePattern;
|
|
4544
|
+
function registerTraversePatternFactory(factory) {
|
|
4545
|
+
createMetaTraversePattern = factory;
|
|
4546
|
+
}
|
|
4547
|
+
let _patternPathsWithCaptures;
|
|
4548
|
+
let _patternCompile;
|
|
4549
|
+
let _patternIsComplex;
|
|
4550
|
+
function registerTraverseDispatchFunctions(pathsWithCaptures, compile, isComplex) {
|
|
4551
|
+
_patternPathsWithCaptures = pathsWithCaptures;
|
|
4552
|
+
_patternCompile = compile;
|
|
4553
|
+
_patternIsComplex = isComplex;
|
|
4554
|
+
}
|
|
4555
|
+
/**
|
|
4556
|
+
* A pattern that matches a traversal order of patterns.
|
|
4557
|
+
*
|
|
4558
|
+
* Corresponds to the Rust `TraversePattern` struct in traverse_pattern.rs
|
|
4559
|
+
*/
|
|
4560
|
+
var TraversePattern = class TraversePattern {
|
|
4561
|
+
_first;
|
|
4562
|
+
_rest;
|
|
4563
|
+
constructor(first, rest) {
|
|
4564
|
+
this._first = first;
|
|
4565
|
+
this._rest = rest;
|
|
4566
|
+
}
|
|
4567
|
+
/**
|
|
4568
|
+
* Creates a new TraversePattern with the given patterns.
|
|
4569
|
+
*/
|
|
4570
|
+
static new(patterns) {
|
|
4571
|
+
if (patterns.length === 0) throw new Error("TraversePattern requires at least one pattern");
|
|
4572
|
+
const firstPat = patterns[0];
|
|
4573
|
+
const restPatterns = patterns.slice(1);
|
|
4574
|
+
return new TraversePattern(firstPat, restPatterns.length === 0 ? void 0 : TraversePattern.new(restPatterns));
|
|
4575
|
+
}
|
|
4576
|
+
/**
|
|
4577
|
+
* Gets all patterns in this traversal.
|
|
4578
|
+
*/
|
|
4579
|
+
patterns() {
|
|
4580
|
+
const result = [this._first];
|
|
4581
|
+
if (this._rest !== void 0) result.push(...this._rest.patterns());
|
|
4582
|
+
return result;
|
|
4583
|
+
}
|
|
4584
|
+
pathsWithCaptures(haystack) {
|
|
4585
|
+
if (_patternPathsWithCaptures === void 0) throw new Error("TraversePattern dispatch functions not registered");
|
|
4586
|
+
const headPaths = _patternPathsWithCaptures(this._first, haystack)[0];
|
|
4587
|
+
if (this._rest === void 0) return [headPaths, /* @__PURE__ */ new Map()];
|
|
4588
|
+
const result = [];
|
|
4589
|
+
for (const path of headPaths) {
|
|
4590
|
+
const lastEnv = path[path.length - 1];
|
|
4591
|
+
if (lastEnv !== void 0) {
|
|
4592
|
+
const tailPaths = this._rest.paths(lastEnv);
|
|
4593
|
+
for (const tailPath of tailPaths) {
|
|
4594
|
+
const combined = [...path, ...tailPath];
|
|
4595
|
+
result.push(combined);
|
|
4566
4596
|
}
|
|
4567
4597
|
}
|
|
4568
|
-
break;
|
|
4569
4598
|
}
|
|
4599
|
+
return [result, /* @__PURE__ */ new Map()];
|
|
4570
4600
|
}
|
|
4571
|
-
|
|
4601
|
+
paths(haystack) {
|
|
4602
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4603
|
+
}
|
|
4604
|
+
matches(haystack) {
|
|
4605
|
+
return this.paths(haystack).length > 0;
|
|
4606
|
+
}
|
|
4607
|
+
compile(code, literals, captures) {
|
|
4608
|
+
if (_patternCompile === void 0) throw new Error("TraversePattern dispatch functions not registered");
|
|
4609
|
+
_patternCompile(this._first, code, literals, captures);
|
|
4610
|
+
if (this._rest !== void 0) {
|
|
4611
|
+
code.push({ type: "ExtendTraversal" });
|
|
4612
|
+
this._rest.compile(code, literals, captures);
|
|
4613
|
+
code.push({ type: "CombineTraversal" });
|
|
4614
|
+
}
|
|
4615
|
+
}
|
|
4616
|
+
isComplex() {
|
|
4617
|
+
if (_patternIsComplex === void 0) throw new Error("TraversePattern dispatch functions not registered");
|
|
4618
|
+
return _patternIsComplex(this._first) || this._rest !== void 0;
|
|
4619
|
+
}
|
|
4620
|
+
toString() {
|
|
4621
|
+
return this.patterns().map((p) => p.toString()).join(" -> ");
|
|
4622
|
+
}
|
|
4623
|
+
/**
|
|
4624
|
+
* Equality comparison.
|
|
4625
|
+
*/
|
|
4626
|
+
equals(other) {
|
|
4627
|
+
const thisPatterns = this.patterns();
|
|
4628
|
+
const otherPatterns = other.patterns();
|
|
4629
|
+
if (thisPatterns.length !== otherPatterns.length) return false;
|
|
4630
|
+
for (let i = 0; i < thisPatterns.length; i++) if (thisPatterns[i] !== otherPatterns[i]) return false;
|
|
4631
|
+
return true;
|
|
4632
|
+
}
|
|
4633
|
+
/**
|
|
4634
|
+
* Hash code for use in Maps/Sets.
|
|
4635
|
+
*/
|
|
4636
|
+
hashCode() {
|
|
4637
|
+
return this.patterns().length;
|
|
4638
|
+
}
|
|
4639
|
+
};
|
|
4640
|
+
|
|
4641
|
+
//#endregion
|
|
4642
|
+
//#region src/pattern/meta/group-pattern.ts
|
|
4643
|
+
let createMetaGroupPattern;
|
|
4644
|
+
function registerGroupPatternFactory(factory) {
|
|
4645
|
+
createMetaGroupPattern = factory;
|
|
4572
4646
|
}
|
|
4573
4647
|
/**
|
|
4574
|
-
*
|
|
4575
|
-
*
|
|
4648
|
+
* A pattern that matches with repetition.
|
|
4649
|
+
*
|
|
4650
|
+
* Corresponds to the Rust `GroupPattern` struct in repeat_pattern.rs
|
|
4576
4651
|
*/
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4588
|
-
|
|
4589
|
-
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
return
|
|
4595
|
-
}
|
|
4652
|
+
var GroupPattern = class GroupPattern {
|
|
4653
|
+
_pattern;
|
|
4654
|
+
_quantifier;
|
|
4655
|
+
constructor(pattern, quantifier) {
|
|
4656
|
+
this._pattern = pattern;
|
|
4657
|
+
this._quantifier = quantifier;
|
|
4658
|
+
}
|
|
4659
|
+
/**
|
|
4660
|
+
* Creates a new GroupPattern with the specified sub-pattern and quantifier.
|
|
4661
|
+
*/
|
|
4662
|
+
static repeat(pattern, quantifier) {
|
|
4663
|
+
return new GroupPattern(pattern, quantifier);
|
|
4664
|
+
}
|
|
4665
|
+
/**
|
|
4666
|
+
* Creates a new GroupPattern with a quantifier that matches exactly once.
|
|
4667
|
+
*/
|
|
4668
|
+
static new(pattern) {
|
|
4669
|
+
return new GroupPattern(pattern, _bcts_dcbor_pattern.Quantifier.exactly(1));
|
|
4670
|
+
}
|
|
4671
|
+
/**
|
|
4672
|
+
* Gets the sub-pattern of this group pattern.
|
|
4673
|
+
*/
|
|
4674
|
+
pattern() {
|
|
4675
|
+
return this._pattern;
|
|
4676
|
+
}
|
|
4677
|
+
/**
|
|
4678
|
+
* Gets the quantifier of this group pattern.
|
|
4679
|
+
*/
|
|
4680
|
+
quantifier() {
|
|
4681
|
+
return this._quantifier;
|
|
4682
|
+
}
|
|
4683
|
+
pathsWithCaptures(haystack) {
|
|
4684
|
+
if (this._quantifier.min() === 1 && this._quantifier.max() === 1) return dispatchPathsWithCaptures(this._pattern, haystack);
|
|
4685
|
+
throw new Error("GroupPattern does not support pathsWithCaptures directly; use compile instead");
|
|
4686
|
+
}
|
|
4687
|
+
paths(haystack) {
|
|
4688
|
+
return this.pathsWithCaptures(haystack)[0];
|
|
4689
|
+
}
|
|
4690
|
+
matches(haystack) {
|
|
4691
|
+
return matchPattern(this._pattern, haystack);
|
|
4692
|
+
}
|
|
4693
|
+
compile(code, literals, _captures) {
|
|
4694
|
+
const idx = literals.length;
|
|
4695
|
+
literals.push(this._pattern);
|
|
4696
|
+
code.push({
|
|
4697
|
+
type: "Repeat",
|
|
4698
|
+
patternIndex: idx,
|
|
4699
|
+
quantifier: this._quantifier
|
|
4700
|
+
});
|
|
4701
|
+
}
|
|
4702
|
+
isComplex() {
|
|
4703
|
+
return true;
|
|
4704
|
+
}
|
|
4705
|
+
toString() {
|
|
4706
|
+
const formattedRange = this._quantifier.toString();
|
|
4707
|
+
return `(${dispatchPatternToString(this._pattern)})${formattedRange}`;
|
|
4708
|
+
}
|
|
4709
|
+
/**
|
|
4710
|
+
* Equality comparison.
|
|
4711
|
+
*/
|
|
4712
|
+
equals(other) {
|
|
4713
|
+
return this._pattern === other._pattern && this._quantifier.equals(other._quantifier);
|
|
4714
|
+
}
|
|
4715
|
+
/**
|
|
4716
|
+
* Hash code for use in Maps/Sets.
|
|
4717
|
+
*/
|
|
4718
|
+
hashCode() {
|
|
4719
|
+
return this._quantifier.min() * 31 + (this._quantifier.max() ?? 0);
|
|
4720
|
+
}
|
|
4721
|
+
};
|
|
4722
|
+
|
|
4723
|
+
//#endregion
|
|
4724
|
+
//#region src/pattern/meta/index.ts
|
|
4725
|
+
/**
|
|
4726
|
+
* Creates an Any meta pattern.
|
|
4727
|
+
*/
|
|
4728
|
+
function metaAny(pattern) {
|
|
4729
|
+
return {
|
|
4730
|
+
type: "Any",
|
|
4731
|
+
pattern
|
|
4732
|
+
};
|
|
4596
4733
|
}
|
|
4597
4734
|
/**
|
|
4598
|
-
*
|
|
4735
|
+
* Creates an And meta pattern.
|
|
4599
4736
|
*/
|
|
4600
|
-
function
|
|
4601
|
-
const code = [];
|
|
4602
|
-
const literals = [];
|
|
4603
|
-
const captureNames = [];
|
|
4604
|
-
collectCaptureNames(pattern, captureNames);
|
|
4605
|
-
compilePattern(pattern, code, literals, captureNames);
|
|
4606
|
-
code.push({ type: "Accept" });
|
|
4737
|
+
function metaAnd(pattern) {
|
|
4607
4738
|
return {
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
captureNames
|
|
4739
|
+
type: "And",
|
|
4740
|
+
pattern
|
|
4611
4741
|
};
|
|
4612
4742
|
}
|
|
4613
4743
|
/**
|
|
4614
|
-
*
|
|
4744
|
+
* Creates an Or meta pattern.
|
|
4615
4745
|
*/
|
|
4616
|
-
function
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
break;
|
|
4622
|
-
case "Meta":
|
|
4623
|
-
collectMetaCaptureNames(pattern.pattern, out);
|
|
4624
|
-
break;
|
|
4625
|
-
}
|
|
4746
|
+
function metaOr(pattern) {
|
|
4747
|
+
return {
|
|
4748
|
+
type: "Or",
|
|
4749
|
+
pattern
|
|
4750
|
+
};
|
|
4626
4751
|
}
|
|
4627
|
-
|
|
4628
|
-
|
|
4629
|
-
|
|
4630
|
-
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
-
|
|
4634
|
-
|
|
4635
|
-
|
|
4636
|
-
|
|
4637
|
-
|
|
4638
|
-
|
|
4639
|
-
|
|
4640
|
-
|
|
4641
|
-
|
|
4642
|
-
|
|
4643
|
-
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
|
|
4649
|
-
|
|
4650
|
-
|
|
4651
|
-
|
|
4652
|
-
|
|
4653
|
-
|
|
4654
|
-
|
|
4655
|
-
|
|
4656
|
-
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4752
|
+
/**
|
|
4753
|
+
* Creates a Not meta pattern.
|
|
4754
|
+
*/
|
|
4755
|
+
function metaNot(pattern) {
|
|
4756
|
+
return {
|
|
4757
|
+
type: "Not",
|
|
4758
|
+
pattern
|
|
4759
|
+
};
|
|
4760
|
+
}
|
|
4761
|
+
/**
|
|
4762
|
+
* Creates a Capture meta pattern.
|
|
4763
|
+
*/
|
|
4764
|
+
function metaCapture(pattern) {
|
|
4765
|
+
return {
|
|
4766
|
+
type: "Capture",
|
|
4767
|
+
pattern
|
|
4768
|
+
};
|
|
4769
|
+
}
|
|
4770
|
+
/**
|
|
4771
|
+
* Creates a Search meta pattern.
|
|
4772
|
+
*/
|
|
4773
|
+
function metaSearch(pattern) {
|
|
4774
|
+
return {
|
|
4775
|
+
type: "Search",
|
|
4776
|
+
pattern
|
|
4777
|
+
};
|
|
4778
|
+
}
|
|
4779
|
+
/**
|
|
4780
|
+
* Creates a Traverse meta pattern.
|
|
4781
|
+
*/
|
|
4782
|
+
function metaTraverse(pattern) {
|
|
4783
|
+
return {
|
|
4784
|
+
type: "Traverse",
|
|
4785
|
+
pattern
|
|
4786
|
+
};
|
|
4787
|
+
}
|
|
4788
|
+
/**
|
|
4789
|
+
* Creates a Group meta pattern.
|
|
4790
|
+
*/
|
|
4791
|
+
function metaGroup(pattern) {
|
|
4792
|
+
return {
|
|
4793
|
+
type: "Group",
|
|
4794
|
+
pattern
|
|
4795
|
+
};
|
|
4796
|
+
}
|
|
4797
|
+
/**
|
|
4798
|
+
* Gets paths with captures for a meta pattern.
|
|
4799
|
+
*/
|
|
4800
|
+
function metaPatternPathsWithCaptures(pattern, haystack) {
|
|
4801
|
+
switch (pattern.type) {
|
|
4802
|
+
case "Any": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4803
|
+
case "And": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4804
|
+
case "Or": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4805
|
+
case "Not": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4806
|
+
case "Capture": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4807
|
+
case "Search": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4808
|
+
case "Traverse": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4809
|
+
case "Group": return pattern.pattern.pathsWithCaptures(haystack);
|
|
4665
4810
|
}
|
|
4666
4811
|
}
|
|
4667
|
-
|
|
4812
|
+
/**
|
|
4813
|
+
* Compiles a meta pattern to bytecode.
|
|
4814
|
+
*/
|
|
4815
|
+
function metaPatternCompile(pattern, code, literals, captures) {
|
|
4668
4816
|
switch (pattern.type) {
|
|
4669
|
-
case "Any":
|
|
4817
|
+
case "Any":
|
|
4818
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4819
|
+
break;
|
|
4670
4820
|
case "And":
|
|
4671
|
-
|
|
4821
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4672
4822
|
break;
|
|
4673
4823
|
case "Or":
|
|
4674
|
-
|
|
4824
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4675
4825
|
break;
|
|
4676
4826
|
case "Not":
|
|
4677
|
-
|
|
4827
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4678
4828
|
break;
|
|
4679
|
-
case "Capture":
|
|
4680
|
-
|
|
4681
|
-
if (!out.includes(name)) out.push(name);
|
|
4682
|
-
collectCaptureNames(pattern.pattern.pattern(), out);
|
|
4829
|
+
case "Capture":
|
|
4830
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4683
4831
|
break;
|
|
4684
|
-
}
|
|
4685
4832
|
case "Search":
|
|
4686
|
-
|
|
4833
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4687
4834
|
break;
|
|
4688
4835
|
case "Traverse":
|
|
4689
|
-
|
|
4836
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4690
4837
|
break;
|
|
4691
4838
|
case "Group":
|
|
4692
|
-
|
|
4839
|
+
pattern.pattern.compile(code, literals, captures);
|
|
4693
4840
|
break;
|
|
4694
4841
|
}
|
|
4695
4842
|
}
|
|
4696
4843
|
/**
|
|
4697
|
-
*
|
|
4844
|
+
* Checks if a meta pattern is complex.
|
|
4698
4845
|
*/
|
|
4699
|
-
function
|
|
4846
|
+
function metaPatternIsComplex(pattern) {
|
|
4700
4847
|
switch (pattern.type) {
|
|
4701
|
-
case "
|
|
4702
|
-
case "
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
case "Meta":
|
|
4710
|
-
compileMetaPattern(pattern.pattern, code, literals, captureNames);
|
|
4711
|
-
break;
|
|
4848
|
+
case "Any": return pattern.pattern.isComplex();
|
|
4849
|
+
case "And": return pattern.pattern.isComplex();
|
|
4850
|
+
case "Or": return pattern.pattern.isComplex();
|
|
4851
|
+
case "Not": return pattern.pattern.isComplex();
|
|
4852
|
+
case "Capture": return pattern.pattern.isComplex();
|
|
4853
|
+
case "Search": return pattern.pattern.isComplex();
|
|
4854
|
+
case "Traverse": return pattern.pattern.isComplex();
|
|
4855
|
+
case "Group": return pattern.pattern.isComplex();
|
|
4712
4856
|
}
|
|
4713
4857
|
}
|
|
4714
|
-
|
|
4858
|
+
/**
|
|
4859
|
+
* Converts a meta pattern to string.
|
|
4860
|
+
*/
|
|
4861
|
+
function metaPatternToString(pattern) {
|
|
4715
4862
|
switch (pattern.type) {
|
|
4716
|
-
case "Any":
|
|
4717
|
-
|
|
4718
|
-
|
|
4719
|
-
|
|
4720
|
-
|
|
4721
|
-
|
|
4722
|
-
|
|
4723
|
-
|
|
4724
|
-
|
|
4725
|
-
|
|
4726
|
-
|
|
4727
|
-
|
|
4728
|
-
|
|
4729
|
-
|
|
4730
|
-
|
|
4863
|
+
case "Any": return pattern.pattern.toString();
|
|
4864
|
+
case "And": return pattern.pattern.toString();
|
|
4865
|
+
case "Or": return pattern.pattern.toString();
|
|
4866
|
+
case "Not": return pattern.pattern.toString();
|
|
4867
|
+
case "Capture": return pattern.pattern.toString();
|
|
4868
|
+
case "Search": return pattern.pattern.toString();
|
|
4869
|
+
case "Traverse": return pattern.pattern.toString();
|
|
4870
|
+
case "Group": return pattern.pattern.toString();
|
|
4871
|
+
}
|
|
4872
|
+
}
|
|
4873
|
+
/**
|
|
4874
|
+
* Collects capture names from a meta pattern.
|
|
4875
|
+
*/
|
|
4876
|
+
function metaPatternCollectCaptureNames(pattern, out) {
|
|
4877
|
+
switch (pattern.type) {
|
|
4878
|
+
case "Any": break;
|
|
4879
|
+
case "And":
|
|
4880
|
+
for (const pat of pattern.pattern.patterns()) collectCaptureNamesFromPattern(pat, out);
|
|
4731
4881
|
break;
|
|
4732
|
-
|
|
4733
|
-
|
|
4734
|
-
const patterns = pattern.pattern.patterns();
|
|
4735
|
-
if (patterns.length === 0) return;
|
|
4736
|
-
if (patterns.length === 1) {
|
|
4737
|
-
compilePattern(patterns[0], code, literals, captureNames);
|
|
4738
|
-
return;
|
|
4739
|
-
}
|
|
4740
|
-
const jumpAddresses = [];
|
|
4741
|
-
for (let i = 0; i < patterns.length - 1; i++) {
|
|
4742
|
-
const splitAddr = code.length;
|
|
4743
|
-
code.push({
|
|
4744
|
-
type: "Split",
|
|
4745
|
-
a: 0,
|
|
4746
|
-
b: 0
|
|
4747
|
-
});
|
|
4748
|
-
const aStart = code.length;
|
|
4749
|
-
compilePattern(patterns[i], code, literals, captureNames);
|
|
4750
|
-
jumpAddresses.push(code.length);
|
|
4751
|
-
code.push({
|
|
4752
|
-
type: "Jump",
|
|
4753
|
-
address: 0
|
|
4754
|
-
});
|
|
4755
|
-
const bStart = code.length;
|
|
4756
|
-
code[splitAddr].a = aStart;
|
|
4757
|
-
code[splitAddr].b = bStart;
|
|
4758
|
-
}
|
|
4759
|
-
compilePattern(patterns[patterns.length - 1], code, literals, captureNames);
|
|
4760
|
-
const endAddr = code.length;
|
|
4761
|
-
for (const jumpAddr of jumpAddresses) code[jumpAddr].address = endAddr;
|
|
4882
|
+
case "Or":
|
|
4883
|
+
for (const pat of pattern.pattern.patterns()) collectCaptureNamesFromPattern(pat, out);
|
|
4762
4884
|
break;
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
const innerPattern = pattern.pattern.pattern();
|
|
4766
|
-
literals.push(innerPattern);
|
|
4767
|
-
code.push({
|
|
4768
|
-
type: "NotMatch",
|
|
4769
|
-
patternIndex: literals.length - 1
|
|
4770
|
-
});
|
|
4885
|
+
case "Not":
|
|
4886
|
+
collectCaptureNamesFromPattern(pattern.pattern.pattern(), out);
|
|
4771
4887
|
break;
|
|
4772
|
-
}
|
|
4773
4888
|
case "Capture": {
|
|
4774
4889
|
const name = pattern.pattern.name();
|
|
4775
|
-
|
|
4776
|
-
|
|
4777
|
-
type: "CaptureStart",
|
|
4778
|
-
captureIndex
|
|
4779
|
-
});
|
|
4780
|
-
compilePattern(pattern.pattern.pattern(), code, literals, captureNames);
|
|
4781
|
-
code.push({
|
|
4782
|
-
type: "CaptureEnd",
|
|
4783
|
-
captureIndex
|
|
4784
|
-
});
|
|
4890
|
+
if (!out.includes(name)) out.push(name);
|
|
4891
|
+
collectCaptureNamesFromPattern(pattern.pattern.pattern(), out);
|
|
4785
4892
|
break;
|
|
4786
4893
|
}
|
|
4787
|
-
case "Search":
|
|
4788
|
-
|
|
4789
|
-
collectCaptureNames(pattern.pattern.pattern(), innerCaptureNames);
|
|
4790
|
-
const captureMap = innerCaptureNames.map((name) => {
|
|
4791
|
-
const idx = captureNames.indexOf(name);
|
|
4792
|
-
return [name, idx >= 0 ? idx : 0];
|
|
4793
|
-
});
|
|
4794
|
-
literals.push(pattern.pattern.pattern());
|
|
4795
|
-
code.push({
|
|
4796
|
-
type: "Search",
|
|
4797
|
-
patternIndex: literals.length - 1,
|
|
4798
|
-
captureMap
|
|
4799
|
-
});
|
|
4894
|
+
case "Search":
|
|
4895
|
+
collectCaptureNamesFromPattern(pattern.pattern.pattern(), out);
|
|
4800
4896
|
break;
|
|
4801
|
-
|
|
4802
|
-
|
|
4803
|
-
const patterns = pattern.pattern.patterns();
|
|
4804
|
-
for (let i = 0; i < patterns.length; i++) {
|
|
4805
|
-
const pat = patterns[i];
|
|
4806
|
-
if (pat === void 0) continue;
|
|
4807
|
-
compilePattern(pat, code, literals, captureNames);
|
|
4808
|
-
if (i < patterns.length - 1) code.push({ type: "ExtendTraversal" });
|
|
4809
|
-
}
|
|
4810
|
-
if (patterns.length > 1) code.push({ type: "CombineTraversal" });
|
|
4897
|
+
case "Traverse":
|
|
4898
|
+
for (const pat of pattern.pattern.patterns()) collectCaptureNamesFromPattern(pat, out);
|
|
4811
4899
|
break;
|
|
4812
|
-
|
|
4813
|
-
|
|
4814
|
-
const quantifier = pattern.pattern.quantifier();
|
|
4815
|
-
if (quantifier !== void 0) {
|
|
4816
|
-
literals.push(pattern.pattern.pattern());
|
|
4817
|
-
code.push({
|
|
4818
|
-
type: "Repeat",
|
|
4819
|
-
patternIndex: literals.length - 1,
|
|
4820
|
-
quantifier
|
|
4821
|
-
});
|
|
4822
|
-
} else compilePattern(pattern.pattern.pattern(), code, literals, captureNames);
|
|
4900
|
+
case "Group":
|
|
4901
|
+
collectCaptureNamesFromPattern(pattern.pattern.pattern(), out);
|
|
4823
4902
|
break;
|
|
4824
|
-
}
|
|
4825
4903
|
}
|
|
4826
4904
|
}
|
|
4905
|
+
/**
|
|
4906
|
+
* Helper to collect capture names from any Pattern.
|
|
4907
|
+
*/
|
|
4908
|
+
function collectCaptureNamesFromPattern(pattern, out) {
|
|
4909
|
+
const p = pattern;
|
|
4910
|
+
if (p.collectCaptureNames !== void 0) p.collectCaptureNames(out);
|
|
4911
|
+
}
|
|
4827
4912
|
|
|
4828
4913
|
//#endregion
|
|
4829
4914
|
//#region src/pattern/dcbor-integration.ts
|
|
@@ -4988,9 +5073,38 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
4988
5073
|
};
|
|
4989
5074
|
}
|
|
4990
5075
|
/**
|
|
5076
|
+
* Check if a pattern requires the VM for execution.
|
|
5077
|
+
* Patterns containing Group (repeat), Traverse, or Search require compilation.
|
|
5078
|
+
*/
|
|
5079
|
+
function patternNeedsVM(pattern) {
|
|
5080
|
+
switch (pattern.type) {
|
|
5081
|
+
case "Leaf":
|
|
5082
|
+
case "Structure": return false;
|
|
5083
|
+
case "Meta": return metaPatternNeedsVM(pattern.pattern);
|
|
5084
|
+
}
|
|
5085
|
+
}
|
|
5086
|
+
function metaPatternNeedsVM(pattern) {
|
|
5087
|
+
switch (pattern.type) {
|
|
5088
|
+
case "Any": return false;
|
|
5089
|
+
case "And": return pattern.pattern.patterns().some(patternNeedsVM);
|
|
5090
|
+
case "Or": return pattern.pattern.patterns().some(patternNeedsVM);
|
|
5091
|
+
case "Not": return patternNeedsVM(pattern.pattern.pattern());
|
|
5092
|
+
case "Capture": return patternNeedsVM(pattern.pattern.pattern());
|
|
5093
|
+
case "Search":
|
|
5094
|
+
case "Traverse": return true;
|
|
5095
|
+
case "Group": {
|
|
5096
|
+
const q = pattern.pattern.quantifier();
|
|
5097
|
+
if (q.min() === 1 && q.max() === 1) return patternNeedsVM(pattern.pattern.pattern());
|
|
5098
|
+
return true;
|
|
5099
|
+
}
|
|
5100
|
+
}
|
|
5101
|
+
}
|
|
5102
|
+
/**
|
|
4991
5103
|
* Gets paths with captures for a pattern.
|
|
5104
|
+
* Routes through the VM for complex patterns that require compilation.
|
|
4992
5105
|
*/
|
|
4993
5106
|
function patternPathsWithCaptures(pattern, haystack) {
|
|
5107
|
+
if (patternNeedsVM(pattern)) return patternPathsWithCapturesViaVM(pattern, haystack);
|
|
4994
5108
|
switch (pattern.type) {
|
|
4995
5109
|
case "Leaf": return leafPatternPathsWithCaptures(pattern.pattern, haystack);
|
|
4996
5110
|
case "Structure": return structurePatternPathsWithCaptures(pattern.pattern, haystack);
|
|
@@ -4998,6 +5112,23 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
4998
5112
|
}
|
|
4999
5113
|
}
|
|
5000
5114
|
/**
|
|
5115
|
+
* Execute a pattern through the VM, merging results.
|
|
5116
|
+
*/
|
|
5117
|
+
function patternPathsWithCapturesViaVM(pattern, haystack) {
|
|
5118
|
+
const results = run(compile(pattern), haystack);
|
|
5119
|
+
const allPaths = [];
|
|
5120
|
+
const mergedCaptures = /* @__PURE__ */ new Map();
|
|
5121
|
+
for (const [path, captures] of results) {
|
|
5122
|
+
allPaths.push(path);
|
|
5123
|
+
for (const [name, paths] of captures) {
|
|
5124
|
+
const existing = mergedCaptures.get(name) ?? [];
|
|
5125
|
+
existing.push(...paths);
|
|
5126
|
+
mergedCaptures.set(name, existing);
|
|
5127
|
+
}
|
|
5128
|
+
}
|
|
5129
|
+
return [allPaths, mergedCaptures];
|
|
5130
|
+
}
|
|
5131
|
+
/**
|
|
5001
5132
|
* Gets paths for a pattern.
|
|
5002
5133
|
*/
|
|
5003
5134
|
function patternPaths(pattern, haystack) {
|
|
@@ -5118,6 +5249,24 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
5118
5249
|
return patternLeaf(leafDate(DatePattern.range(earliest, latest)));
|
|
5119
5250
|
}
|
|
5120
5251
|
/**
|
|
5252
|
+
* Creates a new Pattern that matches Date values on or after the specified date.
|
|
5253
|
+
*/
|
|
5254
|
+
function dateEarliest(d) {
|
|
5255
|
+
return patternLeaf(leafDate(DatePattern.earliest(d)));
|
|
5256
|
+
}
|
|
5257
|
+
/**
|
|
5258
|
+
* Creates a new Pattern that matches Date values on or before the specified date.
|
|
5259
|
+
*/
|
|
5260
|
+
function dateLatest(d) {
|
|
5261
|
+
return patternLeaf(leafDate(DatePattern.latest(d)));
|
|
5262
|
+
}
|
|
5263
|
+
/**
|
|
5264
|
+
* Creates a new Pattern that matches Date values whose ISO-8601 string matches the regex.
|
|
5265
|
+
*/
|
|
5266
|
+
function dateRegex(pattern) {
|
|
5267
|
+
return patternLeaf(leafDate(DatePattern.regex(pattern)));
|
|
5268
|
+
}
|
|
5269
|
+
/**
|
|
5121
5270
|
* Creates a new Pattern that matches any number value.
|
|
5122
5271
|
*/
|
|
5123
5272
|
function anyNumber() {
|
|
@@ -5404,6 +5553,11 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
5404
5553
|
registerNodePatternFactory((p) => patternStructure(structureNode(p)));
|
|
5405
5554
|
registerObscuredPatternFactory((p) => patternStructure(structureObscured(p)));
|
|
5406
5555
|
registerWrappedPatternFactory((p) => patternStructure(structureWrapped(p)));
|
|
5556
|
+
registerWrappedPatternDispatch({
|
|
5557
|
+
pathsWithCaptures: patternPathsWithCaptures,
|
|
5558
|
+
compile: patternCompile,
|
|
5559
|
+
toString: patternToString
|
|
5560
|
+
});
|
|
5407
5561
|
registerAnyPatternFactory((p) => patternMeta(metaAny(p)));
|
|
5408
5562
|
registerAndPatternFactory((p) => patternMeta(metaAnd(p)));
|
|
5409
5563
|
registerOrPatternFactory((p) => patternMeta(metaOr(p)));
|
|
@@ -5416,6 +5570,14 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
5416
5570
|
registerAllFactories();
|
|
5417
5571
|
registerVMPatternFunctions(patternPathsWithCaptures, patternMatches, patternPaths);
|
|
5418
5572
|
registerPatternMatchFn(patternMatches);
|
|
5573
|
+
registerPatternDispatchFns({
|
|
5574
|
+
pathsWithCaptures: patternPathsWithCaptures,
|
|
5575
|
+
paths: patternPaths,
|
|
5576
|
+
compile: patternCompile,
|
|
5577
|
+
isComplex: patternIsComplex,
|
|
5578
|
+
toString: patternToString
|
|
5579
|
+
});
|
|
5580
|
+
registerTraverseDispatchFunctions(patternPathsWithCaptures, patternCompile, patternIsComplex);
|
|
5419
5581
|
|
|
5420
5582
|
//#endregion
|
|
5421
5583
|
//#region src/parse/token.ts
|
|
@@ -6135,7 +6297,13 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
6135
6297
|
token: keyword,
|
|
6136
6298
|
span: this.span()
|
|
6137
6299
|
};
|
|
6138
|
-
return
|
|
6300
|
+
return {
|
|
6301
|
+
token: {
|
|
6302
|
+
type: "Identifier",
|
|
6303
|
+
value: ident
|
|
6304
|
+
},
|
|
6305
|
+
span: this.span()
|
|
6306
|
+
};
|
|
6139
6307
|
}
|
|
6140
6308
|
this.bump(1);
|
|
6141
6309
|
}
|
|
@@ -6405,7 +6573,8 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
6405
6573
|
case "BracketClose":
|
|
6406
6574
|
case "Comma":
|
|
6407
6575
|
case "Ellipsis":
|
|
6408
|
-
case "Range":
|
|
6576
|
+
case "Range":
|
|
6577
|
+
case "Identifier": return err(unexpectedToken(token, span));
|
|
6409
6578
|
}
|
|
6410
6579
|
}
|
|
6411
6580
|
/**
|
|
@@ -6544,6 +6713,48 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
6544
6713
|
* Parse date content from date'...' pattern.
|
|
6545
6714
|
*/
|
|
6546
6715
|
function parseDateContent(content, span) {
|
|
6716
|
+
if (content.startsWith("/") && content.endsWith("/")) {
|
|
6717
|
+
const regexStr = content.slice(1, -1);
|
|
6718
|
+
try {
|
|
6719
|
+
return ok(dateRegex(new RegExp(regexStr)));
|
|
6720
|
+
} catch {
|
|
6721
|
+
return err(invalidRegex(span));
|
|
6722
|
+
}
|
|
6723
|
+
}
|
|
6724
|
+
const rangeIdx = content.indexOf("...");
|
|
6725
|
+
if (rangeIdx !== -1) {
|
|
6726
|
+
const left = content.slice(0, rangeIdx).trim();
|
|
6727
|
+
const right = content.slice(rangeIdx + 3).trim();
|
|
6728
|
+
if (left.length === 0 && right.length > 0) {
|
|
6729
|
+
const parsed = Date.parse(right);
|
|
6730
|
+
if (isNaN(parsed)) return err({
|
|
6731
|
+
type: "InvalidDateFormat",
|
|
6732
|
+
span
|
|
6733
|
+
});
|
|
6734
|
+
return ok(dateLatest(_bcts_dcbor.CborDate.fromDatetime(new Date(parsed))));
|
|
6735
|
+
}
|
|
6736
|
+
if (left.length > 0 && right.length === 0) {
|
|
6737
|
+
const parsed = Date.parse(left);
|
|
6738
|
+
if (isNaN(parsed)) return err({
|
|
6739
|
+
type: "InvalidDateFormat",
|
|
6740
|
+
span
|
|
6741
|
+
});
|
|
6742
|
+
return ok(dateEarliest(_bcts_dcbor.CborDate.fromDatetime(new Date(parsed))));
|
|
6743
|
+
}
|
|
6744
|
+
if (left.length > 0 && right.length > 0) {
|
|
6745
|
+
const parsedStart = Date.parse(left);
|
|
6746
|
+
const parsedEnd = Date.parse(right);
|
|
6747
|
+
if (isNaN(parsedStart) || isNaN(parsedEnd)) return err({
|
|
6748
|
+
type: "InvalidDateFormat",
|
|
6749
|
+
span
|
|
6750
|
+
});
|
|
6751
|
+
return ok(dateRange(_bcts_dcbor.CborDate.fromDatetime(new Date(parsedStart)), _bcts_dcbor.CborDate.fromDatetime(new Date(parsedEnd))));
|
|
6752
|
+
}
|
|
6753
|
+
return err({
|
|
6754
|
+
type: "InvalidDateFormat",
|
|
6755
|
+
span
|
|
6756
|
+
});
|
|
6757
|
+
}
|
|
6547
6758
|
const parsed = Date.parse(content);
|
|
6548
6759
|
if (isNaN(parsed)) return err({
|
|
6549
6760
|
type: "InvalidDateFormat",
|
|
@@ -6561,10 +6772,40 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
6561
6772
|
}
|
|
6562
6773
|
/**
|
|
6563
6774
|
* Parse CBOR pattern.
|
|
6775
|
+
*
|
|
6776
|
+
* Matches Rust parse_cbor: tries dcbor-pattern regex first (/keyword/),
|
|
6777
|
+
* then CBOR diagnostic notation via parseDcborItemPartial, then falls
|
|
6778
|
+
* back to parseOr for envelope pattern expressions.
|
|
6564
6779
|
*/
|
|
6565
6780
|
function parseCbor(lexer) {
|
|
6566
|
-
if (lexer.peekToken()?.token.type !== "ParenOpen") return ok(
|
|
6781
|
+
if (lexer.peekToken()?.token.type !== "ParenOpen") return ok(anyCbor());
|
|
6567
6782
|
lexer.next();
|
|
6783
|
+
if (lexer.peek() === "/") {
|
|
6784
|
+
const regexTokenResult = lexer.next();
|
|
6785
|
+
if (regexTokenResult?.token.type === "Regex") {
|
|
6786
|
+
const regexToken = regexTokenResult.token;
|
|
6787
|
+
if (!regexToken.value.ok) return err(regexToken.value.error);
|
|
6788
|
+
const keyword = regexToken.value.value;
|
|
6789
|
+
const dcborResult = (0, _bcts_dcbor_pattern.parse)(keyword);
|
|
6790
|
+
if (!dcborResult.ok) return err(unexpectedToken(regexToken, regexTokenResult.span));
|
|
6791
|
+
if (lexer.next()?.token.type !== "ParenClose") return err({
|
|
6792
|
+
type: "ExpectedCloseParen",
|
|
6793
|
+
span: lexer.span()
|
|
6794
|
+
});
|
|
6795
|
+
return ok(cborPattern(dcborResult.value));
|
|
6796
|
+
}
|
|
6797
|
+
}
|
|
6798
|
+
const cborResult = (0, _bcts_dcbor_parse.parseDcborItemPartial)(lexer.remainder());
|
|
6799
|
+
if (cborResult.ok) {
|
|
6800
|
+
const [cborData, consumed] = cborResult.value;
|
|
6801
|
+
lexer.bump(consumed);
|
|
6802
|
+
while (lexer.peek() === " " || lexer.peek() === " " || lexer.peek() === "\n") lexer.bump(1);
|
|
6803
|
+
if (lexer.next()?.token.type !== "ParenClose") return err({
|
|
6804
|
+
type: "ExpectedCloseParen",
|
|
6805
|
+
span: lexer.span()
|
|
6806
|
+
});
|
|
6807
|
+
return ok(cborValue(cborData));
|
|
6808
|
+
}
|
|
6568
6809
|
const inner = parseOr(lexer);
|
|
6569
6810
|
if (!inner.ok) return inner;
|
|
6570
6811
|
if (lexer.next()?.token.type !== "ParenClose") return err({
|
|
@@ -6576,6 +6817,18 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
6576
6817
|
function parseNode(lexer) {
|
|
6577
6818
|
if (lexer.peekToken()?.token.type !== "ParenOpen") return ok(anyNode());
|
|
6578
6819
|
lexer.next();
|
|
6820
|
+
const afterParen = lexer.peekToken();
|
|
6821
|
+
if (afterParen?.token.type === "Range") {
|
|
6822
|
+
lexer.next();
|
|
6823
|
+
const rangeToken = afterParen.token;
|
|
6824
|
+
if (!rangeToken.value.ok) return err(rangeToken.value.error);
|
|
6825
|
+
const interval = rangeToken.value.value.interval();
|
|
6826
|
+
if (lexer.next()?.token.type !== "ParenClose") return err({
|
|
6827
|
+
type: "ExpectedCloseParen",
|
|
6828
|
+
span: lexer.span()
|
|
6829
|
+
});
|
|
6830
|
+
return ok(patternStructure(structureNode(NodePattern.fromInterval(interval))));
|
|
6831
|
+
}
|
|
6579
6832
|
const inner = parseOr(lexer);
|
|
6580
6833
|
if (!inner.ok) return inner;
|
|
6581
6834
|
if (lexer.next()?.token.type !== "ParenClose") return err({
|
|
@@ -6634,6 +6887,20 @@ var bctsEnvelopePattern = (function(exports, _bcts_dcbor_pattern, _bcts_known_va
|
|
|
6634
6887
|
});
|
|
6635
6888
|
return ok(digestPrefix(digestToken.token.value.value));
|
|
6636
6889
|
}
|
|
6890
|
+
if (digestToken.token.type === "Identifier") {
|
|
6891
|
+
const hexStr = digestToken.token.value;
|
|
6892
|
+
if (hexStr.length === 0 || hexStr.length % 2 !== 0 || !/^[0-9a-fA-F]+$/.test(hexStr)) return err({
|
|
6893
|
+
type: "InvalidHexString",
|
|
6894
|
+
span: digestToken.span
|
|
6895
|
+
});
|
|
6896
|
+
const bytes = new Uint8Array(hexStr.length / 2);
|
|
6897
|
+
for (let i = 0; i < hexStr.length; i += 2) bytes[i / 2] = Number.parseInt(hexStr.slice(i, i + 2), 16);
|
|
6898
|
+
if (lexer.next()?.token.type !== "ParenClose") return err({
|
|
6899
|
+
type: "ExpectedCloseParen",
|
|
6900
|
+
span: lexer.span()
|
|
6901
|
+
});
|
|
6902
|
+
return ok(digestPrefix(bytes));
|
|
6903
|
+
}
|
|
6637
6904
|
return err(unexpectedToken(digestToken.token, digestToken.span));
|
|
6638
6905
|
}
|
|
6639
6906
|
function parseObject(lexer) {
|
|
@@ -6769,13 +7036,21 @@ exports.compileAsAtomic = compileAsAtomic;
|
|
|
6769
7036
|
exports.compressed = compressed;
|
|
6770
7037
|
exports.convertDcborPatternToEnvelopePattern = convertDcborPatternToEnvelopePattern;
|
|
6771
7038
|
exports.date = date;
|
|
7039
|
+
exports.dateEarliest = dateEarliest;
|
|
7040
|
+
exports.dateLatest = dateLatest;
|
|
6772
7041
|
exports.dateRange = dateRange;
|
|
7042
|
+
exports.dateRegex = dateRegex;
|
|
6773
7043
|
exports.dcborPatternError = dcborPatternError;
|
|
6774
7044
|
exports.defaultFormatPathsOpts = defaultFormatPathsOpts;
|
|
6775
7045
|
exports.defaultPathElementFormat = defaultPathElementFormat;
|
|
6776
7046
|
exports.digest = digest;
|
|
6777
7047
|
exports.digestPrefix = digestPrefix;
|
|
6778
7048
|
exports.digestURFormat = digestURFormat;
|
|
7049
|
+
exports.dispatchCompile = dispatchCompile;
|
|
7050
|
+
exports.dispatchIsComplex = dispatchIsComplex;
|
|
7051
|
+
exports.dispatchPaths = dispatchPaths;
|
|
7052
|
+
exports.dispatchPathsWithCaptures = dispatchPathsWithCaptures;
|
|
7053
|
+
exports.dispatchPatternToString = dispatchPatternToString;
|
|
6779
7054
|
exports.elided = elided;
|
|
6780
7055
|
exports.emptyInput = emptyInput;
|
|
6781
7056
|
exports.encrypted = encrypted;
|
|
@@ -6884,6 +7159,7 @@ exports.registerNumberPatternFactory = registerNumberPatternFactory;
|
|
|
6884
7159
|
exports.registerObjectPatternFactory = registerObjectPatternFactory;
|
|
6885
7160
|
exports.registerObscuredPatternFactory = registerObscuredPatternFactory;
|
|
6886
7161
|
exports.registerOrPatternFactory = registerOrPatternFactory;
|
|
7162
|
+
exports.registerPatternDispatchFns = registerPatternDispatchFns;
|
|
6887
7163
|
exports.registerPatternMatchFn = registerPatternMatchFn;
|
|
6888
7164
|
exports.registerPredicatePatternFactory = registerPredicatePatternFactory;
|
|
6889
7165
|
exports.registerSearchPatternFactory = registerSearchPatternFactory;
|
|
@@ -6892,6 +7168,7 @@ exports.registerTaggedPatternFactory = registerTaggedPatternFactory;
|
|
|
6892
7168
|
exports.registerTextPatternFactory = registerTextPatternFactory;
|
|
6893
7169
|
exports.registerTraversePatternFactory = registerTraversePatternFactory;
|
|
6894
7170
|
exports.registerVMPatternFunctions = registerVMPatternFunctions;
|
|
7171
|
+
exports.registerWrappedPatternDispatch = registerWrappedPatternDispatch;
|
|
6895
7172
|
exports.registerWrappedPatternFactory = registerWrappedPatternFactory;
|
|
6896
7173
|
exports.repeat = repeat;
|
|
6897
7174
|
exports.run = run;
|
|
@@ -6930,5 +7207,5 @@ exports.unwrapMatching = unwrapMatching;
|
|
|
6930
7207
|
exports.unwrapOr = unwrapOr;
|
|
6931
7208
|
exports.wrapped = wrapped;
|
|
6932
7209
|
return exports;
|
|
6933
|
-
})({}, bctsDcborPattern, bctsKnownValues, bctsEnvelope, bctsDcbor);
|
|
7210
|
+
})({}, bctsDcborPattern, bctsKnownValues, bctsEnvelope, bctsDcbor, bctsDcborParse);
|
|
6934
7211
|
//# sourceMappingURL=index.iife.js.map
|