@bcts/envelope-pattern 1.0.0-alpha.12 → 1.0.0-alpha.14
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 +162 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +23 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +162 -9
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +160 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -9
- package/src/parse/utils.ts +13 -14
- package/src/pattern/dcbor-integration.ts +208 -0
- package/src/pattern/index.ts +5 -0
- package/src/pattern/matcher.ts +31 -0
- package/src/pattern/meta/and-pattern.ts +2 -5
- package/src/pattern/meta/not-pattern.ts +2 -3
- package/src/pattern/meta/or-pattern.ts +2 -5
package/README.md
CHANGED
|
@@ -10,4 +10,4 @@ The pattern language is designed to be expressive yet concise, allowing you to m
|
|
|
10
10
|
|
|
11
11
|
## Rust Reference Implementation
|
|
12
12
|
|
|
13
|
-
This TypeScript implementation is based on [bc-envelope-pattern-rust](https://github.com/BlockchainCommons/bc-envelope-pattern-rust) **v0.11.0
|
|
13
|
+
This TypeScript implementation is based on [bc-envelope-pattern-rust](https://github.com/BlockchainCommons/bc-envelope-pattern-rust) **v0.11.0** ([commit](https://github.com/BlockchainCommons/bc-envelope-pattern-rust/tree/35c8ea1af47eed148649c5462e66aed9a5064620)).
|
package/dist/index.cjs
CHANGED
|
@@ -497,6 +497,26 @@ function compileAsAtomic(pat, code, literals, _captures) {
|
|
|
497
497
|
literalIndex: idx
|
|
498
498
|
});
|
|
499
499
|
}
|
|
500
|
+
/**
|
|
501
|
+
* Registry for pattern matching function to break circular dependencies.
|
|
502
|
+
* This allows meta patterns to match child patterns without importing from index.ts.
|
|
503
|
+
*/
|
|
504
|
+
let patternMatchFn;
|
|
505
|
+
/**
|
|
506
|
+
* Registers the pattern match function.
|
|
507
|
+
* Called from index.ts after all patterns are defined.
|
|
508
|
+
*/
|
|
509
|
+
function registerPatternMatchFn(fn) {
|
|
510
|
+
patternMatchFn = fn;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Match a pattern against an envelope using the registered match function.
|
|
514
|
+
* Used by meta patterns to match child patterns.
|
|
515
|
+
*/
|
|
516
|
+
function matchPattern(pattern, haystack) {
|
|
517
|
+
if (patternMatchFn === void 0) throw new Error("Pattern match function not registered");
|
|
518
|
+
return patternMatchFn(pattern, haystack);
|
|
519
|
+
}
|
|
500
520
|
|
|
501
521
|
//#endregion
|
|
502
522
|
//#region src/pattern/leaf/bool-pattern.ts
|
|
@@ -3389,9 +3409,7 @@ var AndPattern = class AndPattern {
|
|
|
3389
3409
|
return this.#patterns;
|
|
3390
3410
|
}
|
|
3391
3411
|
pathsWithCaptures(haystack) {
|
|
3392
|
-
return [this.#patterns.every((pattern) =>
|
|
3393
|
-
return pattern.matches(haystack);
|
|
3394
|
-
}) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3412
|
+
return [this.#patterns.every((pattern) => matchPattern(pattern, haystack)) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3395
3413
|
}
|
|
3396
3414
|
paths(haystack) {
|
|
3397
3415
|
return this.pathsWithCaptures(haystack)[0];
|
|
@@ -3453,9 +3471,7 @@ var OrPattern = class OrPattern {
|
|
|
3453
3471
|
return this.#patterns;
|
|
3454
3472
|
}
|
|
3455
3473
|
pathsWithCaptures(haystack) {
|
|
3456
|
-
return [this.#patterns.some((pattern) =>
|
|
3457
|
-
return pattern.matches(haystack);
|
|
3458
|
-
}) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3474
|
+
return [this.#patterns.some((pattern) => matchPattern(pattern, haystack)) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3459
3475
|
}
|
|
3460
3476
|
paths(haystack) {
|
|
3461
3477
|
return this.pathsWithCaptures(haystack)[0];
|
|
@@ -3550,7 +3566,7 @@ var NotPattern = class NotPattern {
|
|
|
3550
3566
|
return this.#pattern;
|
|
3551
3567
|
}
|
|
3552
3568
|
pathsWithCaptures(haystack) {
|
|
3553
|
-
return [!this.#pattern
|
|
3569
|
+
return [!matchPattern(this.#pattern, haystack) ? [[haystack]] : [], /* @__PURE__ */ new Map()];
|
|
3554
3570
|
}
|
|
3555
3571
|
paths(haystack) {
|
|
3556
3572
|
return this.pathsWithCaptures(haystack)[0];
|
|
@@ -4811,6 +4827,139 @@ function compileMetaPattern(pattern, code, literals, captureNames) {
|
|
|
4811
4827
|
}
|
|
4812
4828
|
}
|
|
4813
4829
|
|
|
4830
|
+
//#endregion
|
|
4831
|
+
//#region src/pattern/dcbor-integration.ts
|
|
4832
|
+
/**
|
|
4833
|
+
* Convert a dcbor-pattern Pattern to a bc-envelope-pattern Pattern.
|
|
4834
|
+
*
|
|
4835
|
+
* This function serves as the bridge between the two pattern systems,
|
|
4836
|
+
* allowing dcbor-pattern expressions to be used in envelope pattern contexts.
|
|
4837
|
+
*
|
|
4838
|
+
* @param dcborPattern - The dcbor-pattern Pattern to convert
|
|
4839
|
+
* @returns The converted envelope pattern, or an error if conversion fails
|
|
4840
|
+
*/
|
|
4841
|
+
function convertDcborPatternToEnvelopePattern(dcborPattern) {
|
|
4842
|
+
switch (dcborPattern.kind) {
|
|
4843
|
+
case "Value": return convertValuePatternToEnvelopePattern(dcborPattern.pattern);
|
|
4844
|
+
case "Structure": return convertStructurePatternToEnvelopePattern(dcborPattern.pattern);
|
|
4845
|
+
case "Meta": return convertMetaPatternToEnvelopePattern(dcborPattern.pattern, dcborPattern);
|
|
4846
|
+
}
|
|
4847
|
+
}
|
|
4848
|
+
/**
|
|
4849
|
+
* Convert a dcbor-pattern ValuePattern to an envelope leaf pattern.
|
|
4850
|
+
*/
|
|
4851
|
+
function convertValuePatternToEnvelopePattern(valuePattern) {
|
|
4852
|
+
switch (valuePattern.type) {
|
|
4853
|
+
case "Bool": return ok({
|
|
4854
|
+
type: "Leaf",
|
|
4855
|
+
pattern: leafBool(BoolPattern.fromDcborPattern(valuePattern.pattern))
|
|
4856
|
+
});
|
|
4857
|
+
case "Number": return ok({
|
|
4858
|
+
type: "Leaf",
|
|
4859
|
+
pattern: leafNumber(NumberPattern.fromDcborPattern(valuePattern.pattern))
|
|
4860
|
+
});
|
|
4861
|
+
case "Text": return ok({
|
|
4862
|
+
type: "Leaf",
|
|
4863
|
+
pattern: leafText(TextPattern.fromDcborPattern(valuePattern.pattern))
|
|
4864
|
+
});
|
|
4865
|
+
case "ByteString": return ok({
|
|
4866
|
+
type: "Leaf",
|
|
4867
|
+
pattern: leafByteString(ByteStringPattern.fromDcborPattern(valuePattern.pattern))
|
|
4868
|
+
});
|
|
4869
|
+
case "Date": return ok({
|
|
4870
|
+
type: "Leaf",
|
|
4871
|
+
pattern: leafDate(DatePattern.fromDcborPattern(valuePattern.pattern))
|
|
4872
|
+
});
|
|
4873
|
+
case "KnownValue": return ok({
|
|
4874
|
+
type: "Leaf",
|
|
4875
|
+
pattern: leafKnownValue(KnownValuePattern.fromDcborPattern(valuePattern.pattern))
|
|
4876
|
+
});
|
|
4877
|
+
case "Null": return ok({
|
|
4878
|
+
type: "Leaf",
|
|
4879
|
+
pattern: leafNull(NullPattern.new())
|
|
4880
|
+
});
|
|
4881
|
+
case "Digest": return ok({
|
|
4882
|
+
type: "Leaf",
|
|
4883
|
+
pattern: leafCbor(CBORPattern.fromDcborPattern({
|
|
4884
|
+
kind: "Value",
|
|
4885
|
+
pattern: valuePattern
|
|
4886
|
+
}))
|
|
4887
|
+
});
|
|
4888
|
+
}
|
|
4889
|
+
}
|
|
4890
|
+
/**
|
|
4891
|
+
* Convert a dcbor-pattern StructurePattern to an envelope pattern.
|
|
4892
|
+
*/
|
|
4893
|
+
function convertStructurePatternToEnvelopePattern(structurePattern) {
|
|
4894
|
+
switch (structurePattern.type) {
|
|
4895
|
+
case "Array": return ok({
|
|
4896
|
+
type: "Leaf",
|
|
4897
|
+
pattern: leafArray(ArrayPattern.fromDcborPattern({
|
|
4898
|
+
kind: "Structure",
|
|
4899
|
+
pattern: structurePattern
|
|
4900
|
+
}))
|
|
4901
|
+
});
|
|
4902
|
+
case "Map": return ok({
|
|
4903
|
+
type: "Leaf",
|
|
4904
|
+
pattern: leafMap(MapPattern.any())
|
|
4905
|
+
});
|
|
4906
|
+
case "Tagged": return ok({
|
|
4907
|
+
type: "Leaf",
|
|
4908
|
+
pattern: leafTag(TaggedPattern.fromDcborPattern(structurePattern.pattern))
|
|
4909
|
+
});
|
|
4910
|
+
}
|
|
4911
|
+
}
|
|
4912
|
+
/**
|
|
4913
|
+
* Convert a dcbor-pattern MetaPattern to an envelope meta pattern.
|
|
4914
|
+
*/
|
|
4915
|
+
function convertMetaPatternToEnvelopePattern(metaPattern, originalPattern) {
|
|
4916
|
+
switch (metaPattern.type) {
|
|
4917
|
+
case "Any": return ok({
|
|
4918
|
+
type: "Meta",
|
|
4919
|
+
pattern: metaAny(AnyPattern.new())
|
|
4920
|
+
});
|
|
4921
|
+
case "And": {
|
|
4922
|
+
const convertedPatterns = [];
|
|
4923
|
+
for (const pattern of metaPattern.pattern.patterns) {
|
|
4924
|
+
const result = convertDcborPatternToEnvelopePattern(pattern);
|
|
4925
|
+
if (!result.ok) return result;
|
|
4926
|
+
convertedPatterns.push(result.value);
|
|
4927
|
+
}
|
|
4928
|
+
return ok({
|
|
4929
|
+
type: "Meta",
|
|
4930
|
+
pattern: metaAnd(AndPattern.new(convertedPatterns))
|
|
4931
|
+
});
|
|
4932
|
+
}
|
|
4933
|
+
case "Or": {
|
|
4934
|
+
const convertedPatterns = [];
|
|
4935
|
+
for (const pattern of metaPattern.pattern.patterns) {
|
|
4936
|
+
const result = convertDcborPatternToEnvelopePattern(pattern);
|
|
4937
|
+
if (!result.ok) return result;
|
|
4938
|
+
convertedPatterns.push(result.value);
|
|
4939
|
+
}
|
|
4940
|
+
return ok({
|
|
4941
|
+
type: "Meta",
|
|
4942
|
+
pattern: metaOr(OrPattern.new(convertedPatterns))
|
|
4943
|
+
});
|
|
4944
|
+
}
|
|
4945
|
+
case "Not": {
|
|
4946
|
+
const innerResult = convertDcborPatternToEnvelopePattern(metaPattern.pattern.pattern);
|
|
4947
|
+
if (!innerResult.ok) return innerResult;
|
|
4948
|
+
return ok({
|
|
4949
|
+
type: "Meta",
|
|
4950
|
+
pattern: metaNot(NotPattern.new(innerResult.value))
|
|
4951
|
+
});
|
|
4952
|
+
}
|
|
4953
|
+
case "Capture":
|
|
4954
|
+
case "Repeat":
|
|
4955
|
+
case "Search":
|
|
4956
|
+
case "Sequence": return ok({
|
|
4957
|
+
type: "Leaf",
|
|
4958
|
+
pattern: leafCbor(CBORPattern.fromDcborPattern(originalPattern))
|
|
4959
|
+
});
|
|
4960
|
+
}
|
|
4961
|
+
}
|
|
4962
|
+
|
|
4814
4963
|
//#endregion
|
|
4815
4964
|
//#region src/pattern/index.ts
|
|
4816
4965
|
/**
|
|
@@ -5268,6 +5417,7 @@ function registerAllFactories() {
|
|
|
5268
5417
|
}
|
|
5269
5418
|
registerAllFactories();
|
|
5270
5419
|
registerVMPatternFunctions(patternPathsWithCaptures, patternMatches, patternPaths);
|
|
5420
|
+
registerPatternMatchFn(patternMatches);
|
|
5271
5421
|
|
|
5272
5422
|
//#endregion
|
|
5273
5423
|
//#region src/parse/token.ts
|
|
@@ -6021,7 +6171,7 @@ function parse(input) {
|
|
|
6021
6171
|
const result = parseOr(lexer);
|
|
6022
6172
|
if (!result.ok) {
|
|
6023
6173
|
const dcborResult = (0, _bcts_dcbor_pattern.parse)(input);
|
|
6024
|
-
if (dcborResult.ok) return convertDcborPatternToEnvelopePattern(dcborResult.value);
|
|
6174
|
+
if (dcborResult.ok) return convertDcborPatternToEnvelopePattern$1(dcborResult.value);
|
|
6025
6175
|
return result;
|
|
6026
6176
|
}
|
|
6027
6177
|
const next = lexer.next();
|
|
@@ -6040,7 +6190,7 @@ function parsePartial(input) {
|
|
|
6040
6190
|
/**
|
|
6041
6191
|
* Convert a dcbor-pattern Pattern to an envelope-pattern Pattern.
|
|
6042
6192
|
*/
|
|
6043
|
-
function convertDcborPatternToEnvelopePattern(_dcborPattern) {
|
|
6193
|
+
function convertDcborPatternToEnvelopePattern$1(_dcborPattern) {
|
|
6044
6194
|
return ok(any());
|
|
6045
6195
|
}
|
|
6046
6196
|
/**
|
|
@@ -6619,6 +6769,7 @@ exports.cborValue = cborValue;
|
|
|
6619
6769
|
exports.compile = compile;
|
|
6620
6770
|
exports.compileAsAtomic = compileAsAtomic;
|
|
6621
6771
|
exports.compressed = compressed;
|
|
6772
|
+
exports.convertDcborPatternToEnvelopePattern = convertDcborPatternToEnvelopePattern;
|
|
6622
6773
|
exports.date = date;
|
|
6623
6774
|
exports.dateRange = dateRange;
|
|
6624
6775
|
exports.dcborPatternError = dcborPatternError;
|
|
@@ -6677,6 +6828,7 @@ exports.leafPatternToString = leafPatternToString;
|
|
|
6677
6828
|
exports.leafTag = leafTag;
|
|
6678
6829
|
exports.leafText = leafText;
|
|
6679
6830
|
exports.map = map;
|
|
6831
|
+
exports.matchPattern = matchPattern;
|
|
6680
6832
|
exports.metaAnd = metaAnd;
|
|
6681
6833
|
exports.metaAny = metaAny;
|
|
6682
6834
|
exports.metaCapture = metaCapture;
|
|
@@ -6734,6 +6886,7 @@ exports.registerNumberPatternFactory = registerNumberPatternFactory;
|
|
|
6734
6886
|
exports.registerObjectPatternFactory = registerObjectPatternFactory;
|
|
6735
6887
|
exports.registerObscuredPatternFactory = registerObscuredPatternFactory;
|
|
6736
6888
|
exports.registerOrPatternFactory = registerOrPatternFactory;
|
|
6889
|
+
exports.registerPatternMatchFn = registerPatternMatchFn;
|
|
6737
6890
|
exports.registerPredicatePatternFactory = registerPredicatePatternFactory;
|
|
6738
6891
|
exports.registerSearchPatternFactory = registerSearchPatternFactory;
|
|
6739
6892
|
exports.registerSubjectPatternFactory = registerSubjectPatternFactory;
|