@bcts/envelope-pattern 1.0.0-alpha.12

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.
Files changed (53) hide show
  1. package/LICENSE +48 -0
  2. package/README.md +13 -0
  3. package/dist/index.cjs +6781 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +2628 -0
  6. package/dist/index.d.cts.map +1 -0
  7. package/dist/index.d.mts +2628 -0
  8. package/dist/index.d.mts.map +1 -0
  9. package/dist/index.iife.js +6781 -0
  10. package/dist/index.iife.js.map +1 -0
  11. package/dist/index.mjs +6545 -0
  12. package/dist/index.mjs.map +1 -0
  13. package/package.json +77 -0
  14. package/src/error.ts +262 -0
  15. package/src/format.ts +375 -0
  16. package/src/index.ts +27 -0
  17. package/src/parse/index.ts +923 -0
  18. package/src/parse/token.ts +906 -0
  19. package/src/parse/utils.ts +339 -0
  20. package/src/pattern/index.ts +719 -0
  21. package/src/pattern/leaf/array-pattern.ts +273 -0
  22. package/src/pattern/leaf/bool-pattern.ts +140 -0
  23. package/src/pattern/leaf/byte-string-pattern.ts +172 -0
  24. package/src/pattern/leaf/cbor-pattern.ts +355 -0
  25. package/src/pattern/leaf/date-pattern.ts +178 -0
  26. package/src/pattern/leaf/index.ts +280 -0
  27. package/src/pattern/leaf/known-value-pattern.ts +192 -0
  28. package/src/pattern/leaf/map-pattern.ts +152 -0
  29. package/src/pattern/leaf/null-pattern.ts +110 -0
  30. package/src/pattern/leaf/number-pattern.ts +248 -0
  31. package/src/pattern/leaf/tagged-pattern.ts +228 -0
  32. package/src/pattern/leaf/text-pattern.ts +165 -0
  33. package/src/pattern/matcher.ts +88 -0
  34. package/src/pattern/meta/and-pattern.ts +109 -0
  35. package/src/pattern/meta/any-pattern.ts +81 -0
  36. package/src/pattern/meta/capture-pattern.ts +111 -0
  37. package/src/pattern/meta/group-pattern.ts +110 -0
  38. package/src/pattern/meta/index.ts +269 -0
  39. package/src/pattern/meta/not-pattern.ts +91 -0
  40. package/src/pattern/meta/or-pattern.ts +146 -0
  41. package/src/pattern/meta/search-pattern.ts +201 -0
  42. package/src/pattern/meta/traverse-pattern.ts +146 -0
  43. package/src/pattern/structure/assertions-pattern.ts +244 -0
  44. package/src/pattern/structure/digest-pattern.ts +225 -0
  45. package/src/pattern/structure/index.ts +272 -0
  46. package/src/pattern/structure/leaf-structure-pattern.ts +85 -0
  47. package/src/pattern/structure/node-pattern.ts +188 -0
  48. package/src/pattern/structure/object-pattern.ts +149 -0
  49. package/src/pattern/structure/obscured-pattern.ts +159 -0
  50. package/src/pattern/structure/predicate-pattern.ts +151 -0
  51. package/src/pattern/structure/subject-pattern.ts +152 -0
  52. package/src/pattern/structure/wrapped-pattern.ts +195 -0
  53. package/src/pattern/vm.ts +1021 -0
@@ -0,0 +1,719 @@
1
+ /**
2
+ * @bcts/envelope-pattern - Pattern module
3
+ *
4
+ * This is a 1:1 TypeScript port of bc-envelope-pattern-rust pattern/mod.rs
5
+ *
6
+ * @module envelope-pattern/pattern
7
+ */
8
+
9
+ import type { Envelope, Digest } from "@bcts/envelope";
10
+ import type { CborInput, CborDate, Tag } from "@bcts/dcbor";
11
+ import type { KnownValue } from "@bcts/known-values";
12
+ import { UNIT as KNOWN_VALUE_UNIT } from "@bcts/known-values";
13
+ import {
14
+ type Pattern as DCBORPattern,
15
+ Quantifier,
16
+ Interval,
17
+ Reluctance,
18
+ } from "@bcts/dcbor-pattern";
19
+
20
+ import type { Path } from "../format";
21
+ import type { Instr } from "./vm";
22
+
23
+ // Re-export sub-modules
24
+ export * from "./leaf";
25
+ export * from "./structure";
26
+ export * from "./meta";
27
+ export * from "./matcher";
28
+ export * from "./vm";
29
+
30
+ // Import leaf patterns
31
+ import {
32
+ type LeafPattern,
33
+ leafCbor,
34
+ leafNumber,
35
+ leafText,
36
+ leafByteString,
37
+ leafTag,
38
+ leafArray,
39
+ leafMap,
40
+ leafBool,
41
+ leafNull,
42
+ leafDate,
43
+ leafKnownValue,
44
+ leafPatternPathsWithCaptures,
45
+ leafPatternCompile,
46
+ leafPatternIsComplex,
47
+ leafPatternToString,
48
+ CBORPattern,
49
+ BoolPattern,
50
+ NullPattern,
51
+ NumberPattern,
52
+ TextPattern,
53
+ ByteStringPattern,
54
+ DatePattern,
55
+ ArrayPattern,
56
+ MapPattern,
57
+ KnownValuePattern,
58
+ TaggedPattern,
59
+ registerBoolPatternFactory,
60
+ registerNullPatternFactory,
61
+ registerNumberPatternFactory,
62
+ registerTextPatternFactory,
63
+ registerByteStringPatternFactory,
64
+ registerDatePatternFactory,
65
+ registerArrayPatternFactory,
66
+ registerMapPatternFactory,
67
+ registerKnownValuePatternFactory,
68
+ registerTaggedPatternFactory,
69
+ registerCBORPatternFactory,
70
+ } from "./leaf";
71
+
72
+ // Import structure patterns
73
+ import {
74
+ type StructurePattern,
75
+ structureLeaf,
76
+ structureSubject,
77
+ structurePredicate,
78
+ structureObject,
79
+ structureAssertions,
80
+ structureDigest,
81
+ structureNode,
82
+ structureObscured,
83
+ structureWrapped,
84
+ structurePatternPathsWithCaptures,
85
+ structurePatternCompile,
86
+ structurePatternIsComplex,
87
+ structurePatternToString,
88
+ LeafStructurePattern,
89
+ SubjectPattern,
90
+ PredicatePattern,
91
+ ObjectPattern,
92
+ AssertionsPattern,
93
+ DigestPattern,
94
+ NodePattern,
95
+ ObscuredPattern,
96
+ WrappedPattern,
97
+ registerLeafStructurePatternFactory,
98
+ registerSubjectPatternFactory,
99
+ registerPredicatePatternFactory,
100
+ registerObjectPatternFactory,
101
+ registerAssertionsPatternFactory,
102
+ registerDigestPatternFactory,
103
+ registerNodePatternFactory,
104
+ registerObscuredPatternFactory,
105
+ registerWrappedPatternFactory,
106
+ } from "./structure";
107
+
108
+ // Import meta patterns
109
+ import {
110
+ type MetaPattern,
111
+ metaAny,
112
+ metaAnd,
113
+ metaOr,
114
+ metaNot,
115
+ metaCapture,
116
+ metaSearch,
117
+ metaTraverse,
118
+ metaGroup,
119
+ metaPatternPathsWithCaptures,
120
+ metaPatternCompile,
121
+ metaPatternIsComplex,
122
+ metaPatternToString,
123
+ metaPatternCollectCaptureNames,
124
+ AnyPattern,
125
+ AndPattern,
126
+ OrPattern,
127
+ NotPattern,
128
+ CapturePattern,
129
+ SearchPattern,
130
+ TraversePattern,
131
+ GroupPattern,
132
+ registerAnyPatternFactory,
133
+ registerAndPatternFactory,
134
+ registerOrPatternFactory,
135
+ registerNotPatternFactory,
136
+ registerCapturePatternFactory,
137
+ registerSearchPatternFactory,
138
+ registerTraversePatternFactory,
139
+ registerGroupPatternFactory,
140
+ } from "./meta";
141
+
142
+ /**
143
+ * The main pattern type used for matching envelopes.
144
+ *
145
+ * Corresponds to the Rust `Pattern` enum in pattern/mod.rs
146
+ */
147
+ export type Pattern =
148
+ | { readonly type: "Leaf"; readonly pattern: LeafPattern }
149
+ | { readonly type: "Structure"; readonly pattern: StructurePattern }
150
+ | { readonly type: "Meta"; readonly pattern: MetaPattern };
151
+
152
+ // Pattern factory functions
153
+ /**
154
+ * Creates a Leaf pattern.
155
+ */
156
+ export function patternLeaf(leaf: LeafPattern): Pattern {
157
+ return { type: "Leaf", pattern: leaf };
158
+ }
159
+
160
+ /**
161
+ * Creates a Structure pattern.
162
+ */
163
+ export function patternStructure(structure: StructurePattern): Pattern {
164
+ return { type: "Structure", pattern: structure };
165
+ }
166
+
167
+ /**
168
+ * Creates a Meta pattern.
169
+ */
170
+ export function patternMeta(meta: MetaPattern): Pattern {
171
+ return { type: "Meta", pattern: meta };
172
+ }
173
+
174
+ // ============================================================================
175
+ // Pattern Matcher Implementation
176
+ // ============================================================================
177
+
178
+ /**
179
+ * Gets paths with captures for a pattern.
180
+ */
181
+ export function patternPathsWithCaptures(
182
+ pattern: Pattern,
183
+ haystack: Envelope,
184
+ ): [Path[], Map<string, Path[]>] {
185
+ switch (pattern.type) {
186
+ case "Leaf":
187
+ return leafPatternPathsWithCaptures(pattern.pattern, haystack);
188
+ case "Structure":
189
+ return structurePatternPathsWithCaptures(pattern.pattern, haystack);
190
+ case "Meta":
191
+ return metaPatternPathsWithCaptures(pattern.pattern, haystack);
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Gets paths for a pattern.
197
+ */
198
+ export function patternPaths(pattern: Pattern, haystack: Envelope): Path[] {
199
+ return patternPathsWithCaptures(pattern, haystack)[0];
200
+ }
201
+
202
+ /**
203
+ * Checks if a pattern matches.
204
+ */
205
+ export function patternMatches(pattern: Pattern, haystack: Envelope): boolean {
206
+ return patternPaths(pattern, haystack).length > 0;
207
+ }
208
+
209
+ /**
210
+ * Checks if a pattern is complex.
211
+ */
212
+ export function patternIsComplex(pattern: Pattern): boolean {
213
+ switch (pattern.type) {
214
+ case "Leaf":
215
+ return leafPatternIsComplex(pattern.pattern);
216
+ case "Structure":
217
+ return structurePatternIsComplex(pattern.pattern);
218
+ case "Meta":
219
+ return metaPatternIsComplex(pattern.pattern);
220
+ }
221
+ }
222
+
223
+ /**
224
+ * Compiles a pattern to bytecode.
225
+ */
226
+ export function patternCompile(
227
+ pattern: Pattern,
228
+ code: Instr[],
229
+ literals: Pattern[],
230
+ captures: string[],
231
+ ): void {
232
+ switch (pattern.type) {
233
+ case "Leaf":
234
+ leafPatternCompile(pattern.pattern, code, literals, captures);
235
+ break;
236
+ case "Structure":
237
+ structurePatternCompile(pattern.pattern, code, literals, captures);
238
+ break;
239
+ case "Meta":
240
+ metaPatternCompile(pattern.pattern, code, literals, captures);
241
+ break;
242
+ }
243
+ }
244
+
245
+ /**
246
+ * Converts a pattern to string.
247
+ */
248
+ export function patternToString(pattern: Pattern): string {
249
+ switch (pattern.type) {
250
+ case "Leaf":
251
+ return leafPatternToString(pattern.pattern);
252
+ case "Structure":
253
+ return structurePatternToString(pattern.pattern);
254
+ case "Meta":
255
+ return metaPatternToString(pattern.pattern);
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Collects capture names from a pattern.
261
+ */
262
+ export function patternCollectCaptureNames(pattern: Pattern, out: string[]): void {
263
+ if (pattern.type === "Meta") {
264
+ metaPatternCollectCaptureNames(pattern.pattern, out);
265
+ }
266
+ }
267
+
268
+ // ============================================================================
269
+ // Convenience Constructors - Leaf Patterns
270
+ // ============================================================================
271
+
272
+ /**
273
+ * Creates a new Pattern that matches any CBOR value.
274
+ */
275
+ export function anyCbor(): Pattern {
276
+ return patternLeaf(leafCbor(CBORPattern.any()));
277
+ }
278
+
279
+ /**
280
+ * Creates a new Pattern that matches a specific CBOR value.
281
+ */
282
+ export function cborValue(value: CborInput): Pattern {
283
+ return patternLeaf(leafCbor(CBORPattern.value(value)));
284
+ }
285
+
286
+ /**
287
+ * Creates a new Pattern that matches CBOR values using dcbor-pattern expressions.
288
+ */
289
+ export function cborPattern(pattern: DCBORPattern): Pattern {
290
+ return patternLeaf(leafCbor(CBORPattern.pattern(pattern)));
291
+ }
292
+
293
+ /**
294
+ * Creates a new Pattern that matches any boolean value.
295
+ */
296
+ export function anyBool(): Pattern {
297
+ return patternLeaf(leafBool(BoolPattern.any()));
298
+ }
299
+
300
+ /**
301
+ * Creates a new Pattern that matches a specific boolean value.
302
+ */
303
+ export function bool(b: boolean): Pattern {
304
+ return patternLeaf(leafBool(BoolPattern.value(b)));
305
+ }
306
+
307
+ /**
308
+ * Creates a new Pattern that matches any text value.
309
+ */
310
+ export function anyText(): Pattern {
311
+ return patternLeaf(leafText(TextPattern.any()));
312
+ }
313
+
314
+ /**
315
+ * Creates a new Pattern that matches a specific text value.
316
+ */
317
+ export function text(value: string): Pattern {
318
+ return patternLeaf(leafText(TextPattern.value(value)));
319
+ }
320
+
321
+ /**
322
+ * Creates a new Pattern that matches text values that match the given regex.
323
+ */
324
+ export function textRegex(regex: RegExp): Pattern {
325
+ return patternLeaf(leafText(TextPattern.regex(regex)));
326
+ }
327
+
328
+ /**
329
+ * Creates a new Pattern that matches any Date value.
330
+ */
331
+ export function anyDate(): Pattern {
332
+ return patternLeaf(leafDate(DatePattern.any()));
333
+ }
334
+
335
+ /**
336
+ * Creates a new Pattern that matches a specific Date value.
337
+ */
338
+ export function date(d: CborDate): Pattern {
339
+ return patternLeaf(leafDate(DatePattern.value(d)));
340
+ }
341
+
342
+ /**
343
+ * Creates a new Pattern that matches Date values within a specified range.
344
+ */
345
+ export function dateRange(earliest: CborDate, latest: CborDate): Pattern {
346
+ return patternLeaf(leafDate(DatePattern.range(earliest, latest)));
347
+ }
348
+
349
+ /**
350
+ * Creates a new Pattern that matches any number value.
351
+ */
352
+ export function anyNumber(): Pattern {
353
+ return patternLeaf(leafNumber(NumberPattern.any()));
354
+ }
355
+
356
+ /**
357
+ * Creates a new Pattern that matches a specific number value.
358
+ */
359
+ export function number(value: number): Pattern {
360
+ return patternLeaf(leafNumber(NumberPattern.exact(value)));
361
+ }
362
+
363
+ /**
364
+ * Creates a new Pattern that matches number values within a range.
365
+ */
366
+ export function numberRange(min: number, max: number): Pattern {
367
+ return patternLeaf(leafNumber(NumberPattern.range(min, max)));
368
+ }
369
+
370
+ /**
371
+ * Creates a new Pattern that matches number values greater than the specified value.
372
+ */
373
+ export function numberGreaterThan(value: number): Pattern {
374
+ return patternLeaf(leafNumber(NumberPattern.greaterThan(value)));
375
+ }
376
+
377
+ /**
378
+ * Creates a new Pattern that matches number values less than the specified value.
379
+ */
380
+ export function numberLessThan(value: number): Pattern {
381
+ return patternLeaf(leafNumber(NumberPattern.lessThan(value)));
382
+ }
383
+
384
+ /**
385
+ * Creates a new Pattern that matches any byte string value.
386
+ */
387
+ export function anyByteString(): Pattern {
388
+ return patternLeaf(leafByteString(ByteStringPattern.any()));
389
+ }
390
+
391
+ /**
392
+ * Creates a new Pattern that matches a specific byte string value.
393
+ */
394
+ export function byteString(value: Uint8Array): Pattern {
395
+ return patternLeaf(leafByteString(ByteStringPattern.value(value)));
396
+ }
397
+
398
+ /**
399
+ * Creates a new Pattern that matches any known value.
400
+ */
401
+ export function anyKnownValue(): Pattern {
402
+ return patternLeaf(leafKnownValue(KnownValuePattern.any()));
403
+ }
404
+
405
+ /**
406
+ * Creates a new Pattern that matches a specific known value.
407
+ */
408
+ export function knownValue(value: KnownValue): Pattern {
409
+ return patternLeaf(leafKnownValue(KnownValuePattern.value(value)));
410
+ }
411
+
412
+ /**
413
+ * Creates a new Pattern that matches the unit known value.
414
+ */
415
+ export function unit(): Pattern {
416
+ return knownValue(KNOWN_VALUE_UNIT);
417
+ }
418
+
419
+ /**
420
+ * Creates a new Pattern that matches any array.
421
+ */
422
+ export function anyArray(): Pattern {
423
+ return patternLeaf(leafArray(ArrayPattern.any()));
424
+ }
425
+
426
+ /**
427
+ * Creates a new Pattern that matches any map.
428
+ */
429
+ export function anyMap(): Pattern {
430
+ return patternLeaf(leafMap(MapPattern.any()));
431
+ }
432
+
433
+ /**
434
+ * Creates a new Pattern that matches null.
435
+ */
436
+ export function nullPattern(): Pattern {
437
+ return patternLeaf(leafNull(NullPattern.new()));
438
+ }
439
+
440
+ /**
441
+ * Creates a new Pattern that matches any tagged value.
442
+ */
443
+ export function anyTag(): Pattern {
444
+ return patternLeaf(leafTag(TaggedPattern.any()));
445
+ }
446
+
447
+ /**
448
+ * Creates a new Pattern that matches a specific tagged value.
449
+ */
450
+ export function tagged(tag: Tag, pattern: DCBORPattern): Pattern {
451
+ return patternLeaf(leafTag(TaggedPattern.withTag(tag, pattern)));
452
+ }
453
+
454
+ // ============================================================================
455
+ // Convenience Constructors - Structure Patterns
456
+ // ============================================================================
457
+
458
+ /**
459
+ * Creates a new Pattern that matches leaf envelopes.
460
+ */
461
+ export function leaf(): Pattern {
462
+ return patternStructure(structureLeaf(LeafStructurePattern.new()));
463
+ }
464
+
465
+ /**
466
+ * Creates a new Pattern that matches any assertion.
467
+ */
468
+ export function anyAssertion(): Pattern {
469
+ return patternStructure(structureAssertions(AssertionsPattern.any()));
470
+ }
471
+
472
+ /**
473
+ * Creates a new Pattern that matches assertions with predicates matching pattern.
474
+ */
475
+ export function assertionWithPredicate(pattern: Pattern): Pattern {
476
+ return patternStructure(structureAssertions(AssertionsPattern.withPredicate(pattern)));
477
+ }
478
+
479
+ /**
480
+ * Creates a new Pattern that matches assertions with objects matching pattern.
481
+ */
482
+ export function assertionWithObject(pattern: Pattern): Pattern {
483
+ return patternStructure(structureAssertions(AssertionsPattern.withObject(pattern)));
484
+ }
485
+
486
+ /**
487
+ * Creates a new Pattern that matches any subject.
488
+ */
489
+ export function anySubject(): Pattern {
490
+ return patternStructure(structureSubject(SubjectPattern.any()));
491
+ }
492
+
493
+ /**
494
+ * Creates a new Pattern that matches subjects matching pattern.
495
+ */
496
+ export function subject(pattern: Pattern): Pattern {
497
+ return patternStructure(structureSubject(SubjectPattern.pattern(pattern)));
498
+ }
499
+
500
+ /**
501
+ * Creates a new Pattern that matches any predicate.
502
+ */
503
+ export function anyPredicate(): Pattern {
504
+ return patternStructure(structurePredicate(PredicatePattern.any()));
505
+ }
506
+
507
+ /**
508
+ * Creates a new Pattern that matches predicates matching pattern.
509
+ */
510
+ export function predicate(pattern: Pattern): Pattern {
511
+ return patternStructure(structurePredicate(PredicatePattern.pattern(pattern)));
512
+ }
513
+
514
+ /**
515
+ * Creates a new Pattern that matches any object.
516
+ */
517
+ export function anyObject(): Pattern {
518
+ return patternStructure(structureObject(ObjectPattern.any()));
519
+ }
520
+
521
+ /**
522
+ * Creates a new Pattern that matches objects matching pattern.
523
+ */
524
+ export function object(pattern: Pattern): Pattern {
525
+ return patternStructure(structureObject(ObjectPattern.pattern(pattern)));
526
+ }
527
+
528
+ /**
529
+ * Creates a new Pattern that matches a specific digest.
530
+ */
531
+ export function digest(d: Digest): Pattern {
532
+ return patternStructure(structureDigest(DigestPattern.digest(d)));
533
+ }
534
+
535
+ /**
536
+ * Creates a new Pattern that matches digests with a prefix.
537
+ */
538
+ export function digestPrefix(prefix: Uint8Array): Pattern {
539
+ return patternStructure(structureDigest(DigestPattern.prefix(prefix)));
540
+ }
541
+
542
+ /**
543
+ * Creates a new Pattern that matches any node.
544
+ */
545
+ export function anyNode(): Pattern {
546
+ return patternStructure(structureNode(NodePattern.any()));
547
+ }
548
+
549
+ /**
550
+ * Creates a new Pattern that matches any obscured element.
551
+ */
552
+ export function obscured(): Pattern {
553
+ return patternStructure(structureObscured(ObscuredPattern.any()));
554
+ }
555
+
556
+ /**
557
+ * Creates a new Pattern that matches elided elements.
558
+ */
559
+ export function elided(): Pattern {
560
+ return patternStructure(structureObscured(ObscuredPattern.elided()));
561
+ }
562
+
563
+ /**
564
+ * Creates a new Pattern that matches encrypted elements.
565
+ */
566
+ export function encrypted(): Pattern {
567
+ return patternStructure(structureObscured(ObscuredPattern.encrypted()));
568
+ }
569
+
570
+ /**
571
+ * Creates a new Pattern that matches compressed elements.
572
+ */
573
+ export function compressed(): Pattern {
574
+ return patternStructure(structureObscured(ObscuredPattern.compressed()));
575
+ }
576
+
577
+ /**
578
+ * Creates a new Pattern that matches wrapped envelopes.
579
+ */
580
+ export function wrapped(): Pattern {
581
+ return patternStructure(structureWrapped(WrappedPattern.new()));
582
+ }
583
+
584
+ /**
585
+ * Creates a new Pattern that matches wrapped envelopes and descends.
586
+ * Named `unwrapEnvelope` to avoid conflict with Result.unwrap.
587
+ */
588
+ export function unwrapEnvelope(): Pattern {
589
+ return patternStructure(structureWrapped(WrappedPattern.unwrapMatching(any())));
590
+ }
591
+
592
+ /**
593
+ * Creates a new Pattern that matches wrapped envelopes matching pattern.
594
+ */
595
+ export function unwrapMatching(pattern: Pattern): Pattern {
596
+ return patternStructure(structureWrapped(WrappedPattern.unwrapMatching(pattern)));
597
+ }
598
+
599
+ // ============================================================================
600
+ // Convenience Constructors - Meta Patterns
601
+ // ============================================================================
602
+
603
+ /**
604
+ * Creates a new Pattern that matches any element.
605
+ */
606
+ export function any(): Pattern {
607
+ return patternMeta(metaAny(AnyPattern.new()));
608
+ }
609
+
610
+ /**
611
+ * Creates a new Pattern that matches if all patterns match.
612
+ */
613
+ export function and(patterns: Pattern[]): Pattern {
614
+ return patternMeta(metaAnd(AndPattern.new(patterns)));
615
+ }
616
+
617
+ /**
618
+ * Creates a new Pattern that matches if any pattern matches.
619
+ */
620
+ export function or(patterns: Pattern[]): Pattern {
621
+ return patternMeta(metaOr(OrPattern.new(patterns)));
622
+ }
623
+
624
+ /**
625
+ * Creates a new Pattern that matches if the pattern does not match.
626
+ */
627
+ export function notMatching(pattern: Pattern): Pattern {
628
+ return patternMeta(metaNot(NotPattern.new(pattern)));
629
+ }
630
+
631
+ /**
632
+ * Creates a new Pattern that captures a match with a name.
633
+ */
634
+ export function capture(name: string, pattern: Pattern): Pattern {
635
+ return patternMeta(metaCapture(CapturePattern.new(name, pattern)));
636
+ }
637
+
638
+ /**
639
+ * Creates a new Pattern that searches for matches in the envelope tree.
640
+ */
641
+ export function search(pattern: Pattern): Pattern {
642
+ return patternMeta(metaSearch(SearchPattern.new(pattern)));
643
+ }
644
+
645
+ /**
646
+ * Creates a new Pattern that matches a traversal order of patterns.
647
+ */
648
+ export function traverse(patterns: Pattern[]): Pattern {
649
+ return patternMeta(metaTraverse(TraversePattern.new(patterns)));
650
+ }
651
+
652
+ /**
653
+ * Creates a new Pattern that matches with repetition.
654
+ */
655
+ export function repeat(
656
+ pattern: Pattern,
657
+ min: number,
658
+ max?: number,
659
+ reluctance: Reluctance = Reluctance.Greedy,
660
+ ): Pattern {
661
+ const interval = max !== undefined ? Interval.from(min, max) : Interval.atLeast(min);
662
+ const quantifier = new Quantifier(interval, reluctance);
663
+ return patternMeta(metaGroup(GroupPattern.repeat(pattern, quantifier)));
664
+ }
665
+
666
+ /**
667
+ * Creates a new Pattern for grouping.
668
+ */
669
+ export function group(pattern: Pattern): Pattern {
670
+ return patternMeta(metaGroup(GroupPattern.new(pattern)));
671
+ }
672
+
673
+ // ============================================================================
674
+ // Factory Registration
675
+ // ============================================================================
676
+
677
+ // Register all pattern factories to resolve circular dependencies
678
+ function registerAllFactories(): void {
679
+ // Leaf pattern factories
680
+ registerBoolPatternFactory((p) => patternLeaf(leafBool(p)));
681
+ registerNullPatternFactory((p) => patternLeaf(leafNull(p)));
682
+ registerNumberPatternFactory((p) => patternLeaf(leafNumber(p)));
683
+ registerTextPatternFactory((p) => patternLeaf(leafText(p)));
684
+ registerByteStringPatternFactory((p) => patternLeaf(leafByteString(p)));
685
+ registerDatePatternFactory((p) => patternLeaf(leafDate(p)));
686
+ registerArrayPatternFactory((p) => patternLeaf(leafArray(p)));
687
+ registerMapPatternFactory((p) => patternLeaf(leafMap(p)));
688
+ registerKnownValuePatternFactory((p) => patternLeaf(leafKnownValue(p)));
689
+ registerTaggedPatternFactory((p) => patternLeaf(leafTag(p)));
690
+ registerCBORPatternFactory((p) => patternLeaf(leafCbor(p)));
691
+
692
+ // Structure pattern factories
693
+ registerLeafStructurePatternFactory((p) => patternStructure(structureLeaf(p)));
694
+ registerSubjectPatternFactory((p) => patternStructure(structureSubject(p)));
695
+ registerPredicatePatternFactory((p) => patternStructure(structurePredicate(p)));
696
+ registerObjectPatternFactory((p) => patternStructure(structureObject(p)));
697
+ registerAssertionsPatternFactory((p) => patternStructure(structureAssertions(p)));
698
+ registerDigestPatternFactory((p) => patternStructure(structureDigest(p)));
699
+ registerNodePatternFactory((p) => patternStructure(structureNode(p)));
700
+ registerObscuredPatternFactory((p) => patternStructure(structureObscured(p)));
701
+ registerWrappedPatternFactory((p) => patternStructure(structureWrapped(p)));
702
+
703
+ // Meta pattern factories
704
+ registerAnyPatternFactory((p) => patternMeta(metaAny(p)));
705
+ registerAndPatternFactory((p) => patternMeta(metaAnd(p)));
706
+ registerOrPatternFactory((p) => patternMeta(metaOr(p)));
707
+ registerNotPatternFactory((p) => patternMeta(metaNot(p)));
708
+ registerCapturePatternFactory((p) => patternMeta(metaCapture(p)));
709
+ registerSearchPatternFactory((p) => patternMeta(metaSearch(p)));
710
+ registerTraversePatternFactory((p) => patternMeta(metaTraverse(p)));
711
+ registerGroupPatternFactory((p) => patternMeta(metaGroup(p)));
712
+ }
713
+
714
+ // Register factories immediately on module load
715
+ registerAllFactories();
716
+
717
+ // Register VM pattern functions to resolve circular dependencies
718
+ import { registerVMPatternFunctions } from "./vm";
719
+ registerVMPatternFunctions(patternPathsWithCaptures, patternMatches, patternPaths);