@bcts/envelope-pattern 1.0.0-alpha.16 → 1.0.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +485 -485
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +93 -29
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +93 -29
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +485 -485
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +504 -504
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -9
- package/src/format.ts +9 -9
- package/src/parse/token.ts +55 -55
- package/src/pattern/leaf/array-pattern.ts +25 -25
- package/src/pattern/leaf/bool-pattern.ts +11 -11
- package/src/pattern/leaf/byte-string-pattern.ts +14 -14
- package/src/pattern/leaf/cbor-pattern.ts +29 -29
- package/src/pattern/leaf/date-pattern.ts +8 -8
- package/src/pattern/leaf/known-value-pattern.ts +17 -17
- package/src/pattern/leaf/map-pattern.ts +13 -13
- package/src/pattern/leaf/null-pattern.ts +7 -7
- package/src/pattern/leaf/number-pattern.ts +19 -19
- package/src/pattern/leaf/tagged-pattern.ts +20 -20
- package/src/pattern/leaf/text-pattern.ts +13 -13
- package/src/pattern/meta/and-pattern.ts +11 -11
- package/src/pattern/meta/capture-pattern.ts +14 -14
- package/src/pattern/meta/group-pattern.ts +13 -13
- package/src/pattern/meta/not-pattern.ts +7 -7
- package/src/pattern/meta/or-pattern.ts +15 -15
- package/src/pattern/meta/search-pattern.ts +15 -15
- package/src/pattern/meta/traverse-pattern.ts +15 -15
- package/src/pattern/structure/assertions-pattern.ts +28 -28
- package/src/pattern/structure/digest-pattern.ts +23 -23
- package/src/pattern/structure/node-pattern.ts +17 -17
- package/src/pattern/structure/object-pattern.ts +13 -13
- package/src/pattern/structure/obscured-pattern.ts +7 -7
- package/src/pattern/structure/predicate-pattern.ts +13 -13
- package/src/pattern/structure/subject-pattern.ts +15 -15
- package/src/pattern/structure/wrapped-pattern.ts +15 -15
|
@@ -37,10 +37,10 @@ export type NodePatternType =
|
|
|
37
37
|
* Corresponds to the Rust `NodePattern` enum in node_pattern.rs
|
|
38
38
|
*/
|
|
39
39
|
export class NodePattern implements Matcher {
|
|
40
|
-
readonly
|
|
40
|
+
private readonly _pattern: NodePatternType;
|
|
41
41
|
|
|
42
42
|
private constructor(pattern: NodePatternType) {
|
|
43
|
-
this
|
|
43
|
+
this._pattern = pattern;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -76,14 +76,14 @@ export class NodePattern implements Matcher {
|
|
|
76
76
|
* Gets the pattern type.
|
|
77
77
|
*/
|
|
78
78
|
get patternType(): NodePatternType {
|
|
79
|
-
return this
|
|
79
|
+
return this._pattern;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
/**
|
|
83
83
|
* Gets the subject pattern if this is a WithSubject type, undefined otherwise.
|
|
84
84
|
*/
|
|
85
85
|
subjectPattern(): Pattern | undefined {
|
|
86
|
-
return this
|
|
86
|
+
return this._pattern.type === "WithSubject" ? this._pattern.subjectPattern : undefined;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
/**
|
|
@@ -101,12 +101,12 @@ export class NodePattern implements Matcher {
|
|
|
101
101
|
|
|
102
102
|
let isHit = false;
|
|
103
103
|
|
|
104
|
-
switch (this
|
|
104
|
+
switch (this._pattern.type) {
|
|
105
105
|
case "Any":
|
|
106
106
|
isHit = true;
|
|
107
107
|
break;
|
|
108
108
|
case "AssertionsInterval":
|
|
109
|
-
isHit = this
|
|
109
|
+
isHit = this._pattern.interval.contains(haystack.assertions().length);
|
|
110
110
|
break;
|
|
111
111
|
case "WithSubject":
|
|
112
112
|
// For WithSubject, we match if the node exists (subject pattern matching done at higher level)
|
|
@@ -138,13 +138,13 @@ export class NodePattern implements Matcher {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
toString(): string {
|
|
141
|
-
switch (this
|
|
141
|
+
switch (this._pattern.type) {
|
|
142
142
|
case "Any":
|
|
143
143
|
return "node";
|
|
144
144
|
case "AssertionsInterval":
|
|
145
|
-
return `node(${this
|
|
145
|
+
return `node(${this._pattern.interval.toString()})`;
|
|
146
146
|
case "WithSubject":
|
|
147
|
-
return `node(${(this
|
|
147
|
+
return `node(${(this._pattern.subjectPattern as unknown as { toString(): string }).toString()})`;
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
|
|
@@ -152,21 +152,21 @@ export class NodePattern implements Matcher {
|
|
|
152
152
|
* Equality comparison.
|
|
153
153
|
*/
|
|
154
154
|
equals(other: NodePattern): boolean {
|
|
155
|
-
if (this
|
|
155
|
+
if (this._pattern.type !== other._pattern.type) {
|
|
156
156
|
return false;
|
|
157
157
|
}
|
|
158
|
-
switch (this
|
|
158
|
+
switch (this._pattern.type) {
|
|
159
159
|
case "Any":
|
|
160
160
|
return true;
|
|
161
161
|
case "AssertionsInterval":
|
|
162
|
-
return this
|
|
163
|
-
(other
|
|
162
|
+
return this._pattern.interval.equals(
|
|
163
|
+
(other._pattern as { type: "AssertionsInterval"; interval: Interval }).interval,
|
|
164
164
|
);
|
|
165
165
|
case "WithSubject":
|
|
166
166
|
// Simple reference equality for pattern (could be improved with deep equality)
|
|
167
167
|
return (
|
|
168
|
-
this
|
|
169
|
-
(other
|
|
168
|
+
this._pattern.subjectPattern ===
|
|
169
|
+
(other._pattern as { type: "WithSubject"; subjectPattern: Pattern }).subjectPattern
|
|
170
170
|
);
|
|
171
171
|
}
|
|
172
172
|
}
|
|
@@ -175,12 +175,12 @@ export class NodePattern implements Matcher {
|
|
|
175
175
|
* Hash code for use in Maps/Sets.
|
|
176
176
|
*/
|
|
177
177
|
hashCode(): number {
|
|
178
|
-
switch (this
|
|
178
|
+
switch (this._pattern.type) {
|
|
179
179
|
case "Any":
|
|
180
180
|
return 0;
|
|
181
181
|
case "AssertionsInterval":
|
|
182
182
|
// Simple hash based on interval min/max
|
|
183
|
-
return this
|
|
183
|
+
return this._pattern.interval.min() * 31 + (this._pattern.interval.max() ?? 0);
|
|
184
184
|
case "WithSubject":
|
|
185
185
|
return 1;
|
|
186
186
|
}
|
|
@@ -34,10 +34,10 @@ export type ObjectPatternType =
|
|
|
34
34
|
* Corresponds to the Rust `ObjectPattern` enum in object_pattern.rs
|
|
35
35
|
*/
|
|
36
36
|
export class ObjectPattern implements Matcher {
|
|
37
|
-
readonly
|
|
37
|
+
private readonly _pattern: ObjectPatternType;
|
|
38
38
|
|
|
39
39
|
private constructor(pattern: ObjectPatternType) {
|
|
40
|
-
this
|
|
40
|
+
this._pattern = pattern;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
@@ -58,14 +58,14 @@ export class ObjectPattern implements Matcher {
|
|
|
58
58
|
* Gets the pattern type.
|
|
59
59
|
*/
|
|
60
60
|
get patternType(): ObjectPatternType {
|
|
61
|
-
return this
|
|
61
|
+
return this._pattern;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* Gets the inner pattern if this is a Pattern type, undefined otherwise.
|
|
66
66
|
*/
|
|
67
67
|
innerPattern(): Pattern | undefined {
|
|
68
|
-
return this
|
|
68
|
+
return this._pattern.type === "Pattern" ? this._pattern.pattern : undefined;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
pathsWithCaptures(haystack: Envelope): [Path[], Map<string, Path[]>] {
|
|
@@ -77,12 +77,12 @@ export class ObjectPattern implements Matcher {
|
|
|
77
77
|
|
|
78
78
|
let paths: Path[];
|
|
79
79
|
|
|
80
|
-
switch (this
|
|
80
|
+
switch (this._pattern.type) {
|
|
81
81
|
case "Any":
|
|
82
82
|
paths = [[object]];
|
|
83
83
|
break;
|
|
84
84
|
case "Pattern": {
|
|
85
|
-
const innerMatcher = this
|
|
85
|
+
const innerMatcher = this._pattern.pattern as unknown as Matcher;
|
|
86
86
|
if (innerMatcher.matches(object)) {
|
|
87
87
|
paths = [[object]];
|
|
88
88
|
} else {
|
|
@@ -117,11 +117,11 @@ export class ObjectPattern implements Matcher {
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
toString(): string {
|
|
120
|
-
switch (this
|
|
120
|
+
switch (this._pattern.type) {
|
|
121
121
|
case "Any":
|
|
122
122
|
return "obj";
|
|
123
123
|
case "Pattern":
|
|
124
|
-
return `obj(${(this
|
|
124
|
+
return `obj(${(this._pattern.pattern as unknown as { toString(): string }).toString()})`;
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
|
|
@@ -129,14 +129,14 @@ export class ObjectPattern implements Matcher {
|
|
|
129
129
|
* Equality comparison.
|
|
130
130
|
*/
|
|
131
131
|
equals(other: ObjectPattern): boolean {
|
|
132
|
-
if (this
|
|
132
|
+
if (this._pattern.type !== other._pattern.type) {
|
|
133
133
|
return false;
|
|
134
134
|
}
|
|
135
|
-
if (this
|
|
135
|
+
if (this._pattern.type === "Any") {
|
|
136
136
|
return true;
|
|
137
137
|
}
|
|
138
|
-
const thisPattern = (this
|
|
139
|
-
const otherPattern = (other
|
|
138
|
+
const thisPattern = (this._pattern as { type: "Pattern"; pattern: Pattern }).pattern;
|
|
139
|
+
const otherPattern = (other._pattern as { type: "Pattern"; pattern: Pattern }).pattern;
|
|
140
140
|
return thisPattern === otherPattern;
|
|
141
141
|
}
|
|
142
142
|
|
|
@@ -144,6 +144,6 @@ export class ObjectPattern implements Matcher {
|
|
|
144
144
|
* Hash code for use in Maps/Sets.
|
|
145
145
|
*/
|
|
146
146
|
hashCode(): number {
|
|
147
|
-
return this
|
|
147
|
+
return this._pattern.type === "Any" ? 0 : 1;
|
|
148
148
|
}
|
|
149
149
|
}
|
|
@@ -39,10 +39,10 @@ export type ObscuredPatternType =
|
|
|
39
39
|
* Corresponds to the Rust `ObscuredPattern` enum in obscured_pattern.rs
|
|
40
40
|
*/
|
|
41
41
|
export class ObscuredPattern implements Matcher {
|
|
42
|
-
readonly
|
|
42
|
+
private readonly _pattern: ObscuredPatternType;
|
|
43
43
|
|
|
44
44
|
private constructor(pattern: ObscuredPatternType) {
|
|
45
|
-
this
|
|
45
|
+
this._pattern = pattern;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
/**
|
|
@@ -77,13 +77,13 @@ export class ObscuredPattern implements Matcher {
|
|
|
77
77
|
* Gets the pattern type.
|
|
78
78
|
*/
|
|
79
79
|
get patternType(): ObscuredPatternType {
|
|
80
|
-
return this
|
|
80
|
+
return this._pattern;
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
pathsWithCaptures(haystack: Envelope): [Path[], Map<string, Path[]>] {
|
|
84
84
|
let isHit = false;
|
|
85
85
|
|
|
86
|
-
switch (this
|
|
86
|
+
switch (this._pattern.type) {
|
|
87
87
|
case "Any":
|
|
88
88
|
isHit = haystack.isObscured();
|
|
89
89
|
break;
|
|
@@ -122,7 +122,7 @@ export class ObscuredPattern implements Matcher {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
toString(): string {
|
|
125
|
-
switch (this
|
|
125
|
+
switch (this._pattern.type) {
|
|
126
126
|
case "Any":
|
|
127
127
|
return "obscured";
|
|
128
128
|
case "Elided":
|
|
@@ -138,14 +138,14 @@ export class ObscuredPattern implements Matcher {
|
|
|
138
138
|
* Equality comparison.
|
|
139
139
|
*/
|
|
140
140
|
equals(other: ObscuredPattern): boolean {
|
|
141
|
-
return this
|
|
141
|
+
return this._pattern.type === other._pattern.type;
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
145
|
* Hash code for use in Maps/Sets.
|
|
146
146
|
*/
|
|
147
147
|
hashCode(): number {
|
|
148
|
-
switch (this
|
|
148
|
+
switch (this._pattern.type) {
|
|
149
149
|
case "Any":
|
|
150
150
|
return 0;
|
|
151
151
|
case "Elided":
|
|
@@ -36,10 +36,10 @@ export type PredicatePatternType =
|
|
|
36
36
|
* Corresponds to the Rust `PredicatePattern` enum in predicate_pattern.rs
|
|
37
37
|
*/
|
|
38
38
|
export class PredicatePattern implements Matcher {
|
|
39
|
-
readonly
|
|
39
|
+
private readonly _pattern: PredicatePatternType;
|
|
40
40
|
|
|
41
41
|
private constructor(pattern: PredicatePatternType) {
|
|
42
|
-
this
|
|
42
|
+
this._pattern = pattern;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
/**
|
|
@@ -60,14 +60,14 @@ export class PredicatePattern implements Matcher {
|
|
|
60
60
|
* Gets the pattern type.
|
|
61
61
|
*/
|
|
62
62
|
get patternType(): PredicatePatternType {
|
|
63
|
-
return this
|
|
63
|
+
return this._pattern;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Gets the inner pattern if this is a Pattern type, undefined otherwise.
|
|
68
68
|
*/
|
|
69
69
|
innerPattern(): Pattern | undefined {
|
|
70
|
-
return this
|
|
70
|
+
return this._pattern.type === "Pattern" ? this._pattern.pattern : undefined;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
pathsWithCaptures(haystack: Envelope): [Path[], Map<string, Path[]>] {
|
|
@@ -79,12 +79,12 @@ export class PredicatePattern implements Matcher {
|
|
|
79
79
|
|
|
80
80
|
let paths: Path[];
|
|
81
81
|
|
|
82
|
-
switch (this
|
|
82
|
+
switch (this._pattern.type) {
|
|
83
83
|
case "Any":
|
|
84
84
|
paths = [[predicate]];
|
|
85
85
|
break;
|
|
86
86
|
case "Pattern": {
|
|
87
|
-
const innerMatcher = this
|
|
87
|
+
const innerMatcher = this._pattern.pattern as unknown as Matcher;
|
|
88
88
|
if (innerMatcher.matches(predicate)) {
|
|
89
89
|
paths = [[predicate]];
|
|
90
90
|
} else {
|
|
@@ -119,11 +119,11 @@ export class PredicatePattern implements Matcher {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
toString(): string {
|
|
122
|
-
switch (this
|
|
122
|
+
switch (this._pattern.type) {
|
|
123
123
|
case "Any":
|
|
124
124
|
return "pred";
|
|
125
125
|
case "Pattern":
|
|
126
|
-
return `pred(${(this
|
|
126
|
+
return `pred(${(this._pattern.pattern as unknown as { toString(): string }).toString()})`;
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -131,14 +131,14 @@ export class PredicatePattern implements Matcher {
|
|
|
131
131
|
* Equality comparison.
|
|
132
132
|
*/
|
|
133
133
|
equals(other: PredicatePattern): boolean {
|
|
134
|
-
if (this
|
|
134
|
+
if (this._pattern.type !== other._pattern.type) {
|
|
135
135
|
return false;
|
|
136
136
|
}
|
|
137
|
-
if (this
|
|
137
|
+
if (this._pattern.type === "Any") {
|
|
138
138
|
return true;
|
|
139
139
|
}
|
|
140
|
-
const thisPattern = (this
|
|
141
|
-
const otherPattern = (other
|
|
140
|
+
const thisPattern = (this._pattern as { type: "Pattern"; pattern: Pattern }).pattern;
|
|
141
|
+
const otherPattern = (other._pattern as { type: "Pattern"; pattern: Pattern }).pattern;
|
|
142
142
|
return thisPattern === otherPattern;
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -146,6 +146,6 @@ export class PredicatePattern implements Matcher {
|
|
|
146
146
|
* Hash code for use in Maps/Sets.
|
|
147
147
|
*/
|
|
148
148
|
hashCode(): number {
|
|
149
|
-
return this
|
|
149
|
+
return this._pattern.type === "Any" ? 0 : 1;
|
|
150
150
|
}
|
|
151
151
|
}
|
|
@@ -34,10 +34,10 @@ export type SubjectPatternType =
|
|
|
34
34
|
* Corresponds to the Rust `SubjectPattern` enum in subject_pattern.rs
|
|
35
35
|
*/
|
|
36
36
|
export class SubjectPattern implements Matcher {
|
|
37
|
-
readonly
|
|
37
|
+
private readonly _pattern: SubjectPatternType;
|
|
38
38
|
|
|
39
39
|
private constructor(pattern: SubjectPatternType) {
|
|
40
|
-
this
|
|
40
|
+
this._pattern = pattern;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
@@ -58,26 +58,26 @@ export class SubjectPattern implements Matcher {
|
|
|
58
58
|
* Gets the pattern type.
|
|
59
59
|
*/
|
|
60
60
|
get patternType(): SubjectPatternType {
|
|
61
|
-
return this
|
|
61
|
+
return this._pattern;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* Gets the inner pattern if this is a Pattern type, undefined otherwise.
|
|
66
66
|
*/
|
|
67
67
|
innerPattern(): Pattern | undefined {
|
|
68
|
-
return this
|
|
68
|
+
return this._pattern.type === "Pattern" ? this._pattern.pattern : undefined;
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
pathsWithCaptures(haystack: Envelope): [Path[], Map<string, Path[]>] {
|
|
72
72
|
const subject = haystack.subject();
|
|
73
73
|
let paths: Path[];
|
|
74
74
|
|
|
75
|
-
switch (this
|
|
75
|
+
switch (this._pattern.type) {
|
|
76
76
|
case "Any":
|
|
77
77
|
paths = [[subject]];
|
|
78
78
|
break;
|
|
79
79
|
case "Pattern": {
|
|
80
|
-
const innerMatcher = this
|
|
80
|
+
const innerMatcher = this._pattern.pattern as unknown as Matcher;
|
|
81
81
|
if (innerMatcher.matches(subject)) {
|
|
82
82
|
paths = [[subject]];
|
|
83
83
|
} else {
|
|
@@ -99,7 +99,7 @@ export class SubjectPattern implements Matcher {
|
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
compile(code: Instr[], literals: Pattern[], captures: string[]): void {
|
|
102
|
-
switch (this
|
|
102
|
+
switch (this._pattern.type) {
|
|
103
103
|
case "Any":
|
|
104
104
|
code.push({ type: "NavigateSubject" });
|
|
105
105
|
break;
|
|
@@ -108,7 +108,7 @@ export class SubjectPattern implements Matcher {
|
|
|
108
108
|
code.push({ type: "NavigateSubject" });
|
|
109
109
|
// Save the path and run the inner pattern relative to the subject
|
|
110
110
|
code.push({ type: "ExtendTraversal" });
|
|
111
|
-
(this
|
|
111
|
+
(this._pattern.pattern as unknown as Matcher).compile(code, literals, captures);
|
|
112
112
|
code.push({ type: "CombineTraversal" });
|
|
113
113
|
break;
|
|
114
114
|
}
|
|
@@ -119,11 +119,11 @@ export class SubjectPattern implements Matcher {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
toString(): string {
|
|
122
|
-
switch (this
|
|
122
|
+
switch (this._pattern.type) {
|
|
123
123
|
case "Any":
|
|
124
124
|
return "subj";
|
|
125
125
|
case "Pattern":
|
|
126
|
-
return `subj(${(this
|
|
126
|
+
return `subj(${(this._pattern.pattern as unknown as { toString(): string }).toString()})`;
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
@@ -131,15 +131,15 @@ export class SubjectPattern implements Matcher {
|
|
|
131
131
|
* Equality comparison.
|
|
132
132
|
*/
|
|
133
133
|
equals(other: SubjectPattern): boolean {
|
|
134
|
-
if (this
|
|
134
|
+
if (this._pattern.type !== other._pattern.type) {
|
|
135
135
|
return false;
|
|
136
136
|
}
|
|
137
|
-
if (this
|
|
137
|
+
if (this._pattern.type === "Any") {
|
|
138
138
|
return true;
|
|
139
139
|
}
|
|
140
140
|
// For Pattern type, compare the inner patterns
|
|
141
|
-
const thisPattern = (this
|
|
142
|
-
const otherPattern = (other
|
|
141
|
+
const thisPattern = (this._pattern as { type: "Pattern"; pattern: Pattern }).pattern;
|
|
142
|
+
const otherPattern = (other._pattern as { type: "Pattern"; pattern: Pattern }).pattern;
|
|
143
143
|
return thisPattern === otherPattern; // Reference equality for now
|
|
144
144
|
}
|
|
145
145
|
|
|
@@ -147,6 +147,6 @@ export class SubjectPattern implements Matcher {
|
|
|
147
147
|
* Hash code for use in Maps/Sets.
|
|
148
148
|
*/
|
|
149
149
|
hashCode(): number {
|
|
150
|
-
return this
|
|
150
|
+
return this._pattern.type === "Any" ? 0 : 1;
|
|
151
151
|
}
|
|
152
152
|
}
|
|
@@ -34,10 +34,10 @@ export type WrappedPatternType =
|
|
|
34
34
|
* Corresponds to the Rust `WrappedPattern` enum in wrapped_pattern.rs
|
|
35
35
|
*/
|
|
36
36
|
export class WrappedPattern implements Matcher {
|
|
37
|
-
readonly
|
|
37
|
+
private readonly _pattern: WrappedPatternType;
|
|
38
38
|
|
|
39
39
|
private constructor(pattern: WrappedPatternType) {
|
|
40
|
-
this
|
|
40
|
+
this._pattern = pattern;
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
/**
|
|
@@ -69,14 +69,14 @@ export class WrappedPattern implements Matcher {
|
|
|
69
69
|
* Gets the pattern type.
|
|
70
70
|
*/
|
|
71
71
|
get patternType(): WrappedPatternType {
|
|
72
|
-
return this
|
|
72
|
+
return this._pattern;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* Gets the inner pattern if this is an Unwrap type, undefined otherwise.
|
|
77
77
|
*/
|
|
78
78
|
innerPattern(): Pattern | undefined {
|
|
79
|
-
return this
|
|
79
|
+
return this._pattern.type === "Unwrap" ? this._pattern.pattern : undefined;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
pathsWithCaptures(haystack: Envelope): [Path[], Map<string, Path[]>] {
|
|
@@ -88,7 +88,7 @@ export class WrappedPattern implements Matcher {
|
|
|
88
88
|
|
|
89
89
|
let paths: Path[];
|
|
90
90
|
|
|
91
|
-
switch (this
|
|
91
|
+
switch (this._pattern.type) {
|
|
92
92
|
case "Any":
|
|
93
93
|
// Just match the wrapped envelope itself, don't descend
|
|
94
94
|
paths = [[haystack]];
|
|
@@ -97,7 +97,7 @@ export class WrappedPattern implements Matcher {
|
|
|
97
97
|
// Match the content of the wrapped envelope
|
|
98
98
|
const unwrapped = subject.tryUnwrap?.();
|
|
99
99
|
if (unwrapped !== undefined) {
|
|
100
|
-
const innerMatcher = this
|
|
100
|
+
const innerMatcher = this._pattern.pattern as unknown as Matcher;
|
|
101
101
|
const innerPaths = innerMatcher.paths(unwrapped);
|
|
102
102
|
paths = innerPaths.map((path) => {
|
|
103
103
|
// Add the current envelope to the path
|
|
@@ -126,7 +126,7 @@ export class WrappedPattern implements Matcher {
|
|
|
126
126
|
throw new Error("WrappedPattern factory not registered");
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
-
switch (this
|
|
129
|
+
switch (this._pattern.type) {
|
|
130
130
|
case "Any": {
|
|
131
131
|
// Just match the wrapped envelope itself, don't descend
|
|
132
132
|
const idx = literals.length;
|
|
@@ -145,7 +145,7 @@ export class WrappedPattern implements Matcher {
|
|
|
145
145
|
code.push({ type: "PushAxis", axis });
|
|
146
146
|
|
|
147
147
|
// Then match the pattern
|
|
148
|
-
const innerMatcher = this
|
|
148
|
+
const innerMatcher = this._pattern.pattern as unknown as Matcher;
|
|
149
149
|
innerMatcher.compile(code, literals, captures);
|
|
150
150
|
break;
|
|
151
151
|
}
|
|
@@ -157,12 +157,12 @@ export class WrappedPattern implements Matcher {
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
toString(): string {
|
|
160
|
-
switch (this
|
|
160
|
+
switch (this._pattern.type) {
|
|
161
161
|
case "Any":
|
|
162
162
|
return "wrapped";
|
|
163
163
|
case "Unwrap": {
|
|
164
164
|
// Check if it's the "any" pattern by string comparison
|
|
165
|
-
const patternStr = (this
|
|
165
|
+
const patternStr = (this._pattern.pattern as unknown as { toString(): string }).toString();
|
|
166
166
|
if (patternStr === "*") {
|
|
167
167
|
return "unwrap";
|
|
168
168
|
}
|
|
@@ -175,14 +175,14 @@ export class WrappedPattern implements Matcher {
|
|
|
175
175
|
* Equality comparison.
|
|
176
176
|
*/
|
|
177
177
|
equals(other: WrappedPattern): boolean {
|
|
178
|
-
if (this
|
|
178
|
+
if (this._pattern.type !== other._pattern.type) {
|
|
179
179
|
return false;
|
|
180
180
|
}
|
|
181
|
-
if (this
|
|
181
|
+
if (this._pattern.type === "Any") {
|
|
182
182
|
return true;
|
|
183
183
|
}
|
|
184
|
-
const thisPattern = (this
|
|
185
|
-
const otherPattern = (other
|
|
184
|
+
const thisPattern = (this._pattern as { type: "Unwrap"; pattern: Pattern }).pattern;
|
|
185
|
+
const otherPattern = (other._pattern as { type: "Unwrap"; pattern: Pattern }).pattern;
|
|
186
186
|
return thisPattern === otherPattern;
|
|
187
187
|
}
|
|
188
188
|
|
|
@@ -190,6 +190,6 @@ export class WrappedPattern implements Matcher {
|
|
|
190
190
|
* Hash code for use in Maps/Sets.
|
|
191
191
|
*/
|
|
192
192
|
hashCode(): number {
|
|
193
|
-
return this
|
|
193
|
+
return this._pattern.type === "Any" ? 0 : 1;
|
|
194
194
|
}
|
|
195
195
|
}
|