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