@bcts/dcbor-pattern 1.0.0-alpha.11
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/LICENSE +48 -0
- package/README.md +14 -0
- package/dist/index.cjs +6561 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2732 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +2732 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.iife.js +6562 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.mjs +6244 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +85 -0
- package/src/error.ts +333 -0
- package/src/format.ts +299 -0
- package/src/index.ts +20 -0
- package/src/interval.ts +230 -0
- package/src/parse/index.ts +95 -0
- package/src/parse/meta/and-parser.ts +47 -0
- package/src/parse/meta/capture-parser.ts +56 -0
- package/src/parse/meta/index.ts +13 -0
- package/src/parse/meta/not-parser.ts +28 -0
- package/src/parse/meta/or-parser.ts +47 -0
- package/src/parse/meta/primary-parser.ts +420 -0
- package/src/parse/meta/repeat-parser.ts +133 -0
- package/src/parse/meta/search-parser.ts +56 -0
- package/src/parse/parse-registry.ts +31 -0
- package/src/parse/structure/array-parser.ts +210 -0
- package/src/parse/structure/index.ts +9 -0
- package/src/parse/structure/map-parser.ts +128 -0
- package/src/parse/structure/tagged-parser.ts +269 -0
- package/src/parse/token.ts +997 -0
- package/src/parse/value/bool-parser.ts +33 -0
- package/src/parse/value/bytestring-parser.ts +42 -0
- package/src/parse/value/date-parser.ts +24 -0
- package/src/parse/value/digest-parser.ts +24 -0
- package/src/parse/value/index.ts +14 -0
- package/src/parse/value/known-value-parser.ts +24 -0
- package/src/parse/value/null-parser.ts +19 -0
- package/src/parse/value/number-parser.ts +19 -0
- package/src/parse/value/text-parser.ts +43 -0
- package/src/pattern/index.ts +740 -0
- package/src/pattern/match-registry.ts +137 -0
- package/src/pattern/matcher.ts +388 -0
- package/src/pattern/meta/and-pattern.ts +56 -0
- package/src/pattern/meta/any-pattern.ts +43 -0
- package/src/pattern/meta/capture-pattern.ts +57 -0
- package/src/pattern/meta/index.ts +168 -0
- package/src/pattern/meta/not-pattern.ts +70 -0
- package/src/pattern/meta/or-pattern.ts +56 -0
- package/src/pattern/meta/repeat-pattern.ts +117 -0
- package/src/pattern/meta/search-pattern.ts +298 -0
- package/src/pattern/meta/sequence-pattern.ts +72 -0
- package/src/pattern/structure/array-pattern/assigner.ts +95 -0
- package/src/pattern/structure/array-pattern/backtrack.ts +240 -0
- package/src/pattern/structure/array-pattern/helpers.ts +140 -0
- package/src/pattern/structure/array-pattern/index.ts +502 -0
- package/src/pattern/structure/index.ts +122 -0
- package/src/pattern/structure/map-pattern.ts +255 -0
- package/src/pattern/structure/tagged-pattern.ts +190 -0
- package/src/pattern/value/bool-pattern.ts +67 -0
- package/src/pattern/value/bytes-utils.ts +48 -0
- package/src/pattern/value/bytestring-pattern.ts +111 -0
- package/src/pattern/value/date-pattern.ts +162 -0
- package/src/pattern/value/digest-pattern.ts +136 -0
- package/src/pattern/value/index.ts +168 -0
- package/src/pattern/value/known-value-pattern.ts +123 -0
- package/src/pattern/value/null-pattern.ts +46 -0
- package/src/pattern/value/number-pattern.ts +181 -0
- package/src/pattern/value/text-pattern.ts +82 -0
- package/src/pattern/vm.ts +619 -0
- package/src/quantifier.ts +185 -0
- package/src/reluctance.ts +65 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quantifier for pattern repetition.
|
|
3
|
+
*
|
|
4
|
+
* This module provides the Quantifier class which combines an interval
|
|
5
|
+
* (how many times to match) with a reluctance (matching strategy).
|
|
6
|
+
*
|
|
7
|
+
* @module quantifier
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Interval } from "./interval";
|
|
11
|
+
import { type Reluctance, DEFAULT_RELUCTANCE, reluctanceSuffix } from "./reluctance";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Defines how many times a pattern may or must match, with an interval and a
|
|
15
|
+
* reluctance.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* // Zero or more, greedy
|
|
20
|
+
* const star = Quantifier.zeroOrMore();
|
|
21
|
+
*
|
|
22
|
+
* // One or more, lazy
|
|
23
|
+
* const plusLazy = Quantifier.oneOrMore(Reluctance.Lazy);
|
|
24
|
+
*
|
|
25
|
+
* // Exactly 3 times
|
|
26
|
+
* const exact = Quantifier.exactly(3);
|
|
27
|
+
*
|
|
28
|
+
* // Between 2 and 5, possessive
|
|
29
|
+
* const range = Quantifier.between(2, 5, Reluctance.Possessive);
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class Quantifier {
|
|
33
|
+
readonly #interval: Interval;
|
|
34
|
+
readonly #reluctance: Reluctance;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new Quantifier.
|
|
38
|
+
*
|
|
39
|
+
* @param interval - The interval defining how many times to match
|
|
40
|
+
* @param reluctance - The matching strategy (default: Greedy)
|
|
41
|
+
*/
|
|
42
|
+
constructor(interval: Interval, reluctance: Reluctance = DEFAULT_RELUCTANCE) {
|
|
43
|
+
this.#interval = interval;
|
|
44
|
+
this.#reluctance = reluctance;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Creates a quantifier from min/max values.
|
|
49
|
+
*
|
|
50
|
+
* @param min - Minimum occurrences
|
|
51
|
+
* @param max - Maximum occurrences (undefined for unbounded)
|
|
52
|
+
* @param reluctance - The matching strategy
|
|
53
|
+
*/
|
|
54
|
+
static from(min: number, max?: number, reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
55
|
+
return new Quantifier(new Interval(min, max), reluctance);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Creates a quantifier for exactly n occurrences.
|
|
60
|
+
*/
|
|
61
|
+
static exactly(n: number, reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
62
|
+
return new Quantifier(Interval.exactly(n), reluctance);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Creates a quantifier for at least n occurrences.
|
|
67
|
+
*/
|
|
68
|
+
static atLeast(n: number, reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
69
|
+
return new Quantifier(Interval.atLeast(n), reluctance);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Creates a quantifier for at most n occurrences.
|
|
74
|
+
*/
|
|
75
|
+
static atMost(n: number, reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
76
|
+
return new Quantifier(Interval.atMost(n), reluctance);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Creates a quantifier for between min and max occurrences.
|
|
81
|
+
*/
|
|
82
|
+
static between(
|
|
83
|
+
min: number,
|
|
84
|
+
max: number,
|
|
85
|
+
reluctance: Reluctance = DEFAULT_RELUCTANCE,
|
|
86
|
+
): Quantifier {
|
|
87
|
+
return new Quantifier(new Interval(min, max), reluctance);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Creates a quantifier for zero or more occurrences (*).
|
|
92
|
+
*/
|
|
93
|
+
static zeroOrMore(reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
94
|
+
return new Quantifier(Interval.zeroOrMore(), reluctance);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Creates a quantifier for one or more occurrences (+).
|
|
99
|
+
*/
|
|
100
|
+
static oneOrMore(reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
101
|
+
return new Quantifier(Interval.oneOrMore(), reluctance);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Creates a quantifier for zero or one occurrence (?).
|
|
106
|
+
*/
|
|
107
|
+
static zeroOrOne(reluctance: Reluctance = DEFAULT_RELUCTANCE): Quantifier {
|
|
108
|
+
return new Quantifier(Interval.zeroOrOne(), reluctance);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Returns the minimum number of occurrences.
|
|
113
|
+
*/
|
|
114
|
+
min(): number {
|
|
115
|
+
return this.#interval.min();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Returns the maximum number of occurrences, or undefined if unbounded.
|
|
120
|
+
*/
|
|
121
|
+
max(): number | undefined {
|
|
122
|
+
return this.#interval.max();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns the interval.
|
|
127
|
+
*/
|
|
128
|
+
interval(): Interval {
|
|
129
|
+
return this.#interval;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Returns the reluctance (matching strategy).
|
|
134
|
+
*/
|
|
135
|
+
reluctance(): Reluctance {
|
|
136
|
+
return this.#reluctance;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Checks if the given count is within the quantifier's range.
|
|
141
|
+
*/
|
|
142
|
+
contains(count: number): boolean {
|
|
143
|
+
return this.#interval.contains(count);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Checks if the quantifier is unbounded (no maximum).
|
|
148
|
+
*/
|
|
149
|
+
isUnbounded(): boolean {
|
|
150
|
+
return this.#interval.isUnbounded();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Returns a string representation of the quantifier.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```typescript
|
|
158
|
+
* Quantifier.zeroOrMore().toString() // "*"
|
|
159
|
+
* Quantifier.zeroOrMore(Reluctance.Lazy).toString() // "*?"
|
|
160
|
+
* Quantifier.between(1, 5).toString() // "{1,5}"
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
toString(): string {
|
|
164
|
+
return `${this.#interval.shorthandNotation()}${reluctanceSuffix(this.#reluctance)}`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Checks equality with another Quantifier.
|
|
169
|
+
*/
|
|
170
|
+
equals(other: Quantifier): boolean {
|
|
171
|
+
return this.#interval.equals(other.#interval) && this.#reluctance === other.#reluctance;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Converts to an Interval (discarding reluctance).
|
|
176
|
+
*/
|
|
177
|
+
toInterval(): Interval {
|
|
178
|
+
return this.#interval;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Default quantifier is exactly 1 occurrence, greedy.
|
|
184
|
+
*/
|
|
185
|
+
export const DEFAULT_QUANTIFIER = Quantifier.exactly(1);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reluctance for quantifiers.
|
|
3
|
+
*
|
|
4
|
+
* This module defines the matching behavior for quantified patterns,
|
|
5
|
+
* controlling how greedily the pattern matcher consumes input.
|
|
6
|
+
*
|
|
7
|
+
* @module reluctance
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Reluctance for quantifiers.
|
|
12
|
+
*
|
|
13
|
+
* Controls how a quantified pattern matches:
|
|
14
|
+
* - `Greedy`: Match as many as possible, backtrack if needed
|
|
15
|
+
* - `Lazy`: Match as few as possible, add more if needed
|
|
16
|
+
* - `Possessive`: Match as many as possible, never backtrack
|
|
17
|
+
*/
|
|
18
|
+
export enum Reluctance {
|
|
19
|
+
/**
|
|
20
|
+
* Grabs as many repetitions as possible, then backtracks if the rest of
|
|
21
|
+
* the pattern cannot match.
|
|
22
|
+
*/
|
|
23
|
+
Greedy = "greedy",
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Starts with as few repetitions as possible, adding more only if the rest
|
|
27
|
+
* of the pattern cannot match.
|
|
28
|
+
*/
|
|
29
|
+
Lazy = "lazy",
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Grabs as many repetitions as possible and never backtracks; if the rest
|
|
33
|
+
* of the pattern cannot match, the whole match fails.
|
|
34
|
+
*/
|
|
35
|
+
Possessive = "possessive",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Default reluctance is Greedy.
|
|
40
|
+
*/
|
|
41
|
+
export const DEFAULT_RELUCTANCE = Reluctance.Greedy;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns the suffix character for a reluctance type.
|
|
45
|
+
*
|
|
46
|
+
* @param reluctance - The reluctance type
|
|
47
|
+
* @returns The suffix string ("" for Greedy, "?" for Lazy, "+" for Possessive)
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* reluctanceSuffix(Reluctance.Greedy) // ""
|
|
52
|
+
* reluctanceSuffix(Reluctance.Lazy) // "?"
|
|
53
|
+
* reluctanceSuffix(Reluctance.Possessive) // "+"
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const reluctanceSuffix = (reluctance: Reluctance): string => {
|
|
57
|
+
switch (reluctance) {
|
|
58
|
+
case Reluctance.Greedy:
|
|
59
|
+
return "";
|
|
60
|
+
case Reluctance.Lazy:
|
|
61
|
+
return "?";
|
|
62
|
+
case Reluctance.Possessive:
|
|
63
|
+
return "+";
|
|
64
|
+
}
|
|
65
|
+
};
|