@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,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bool pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/bool-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
import { anyBool, bool } from "../../pattern";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse a boolean pattern from the `bool` keyword.
|
|
15
|
+
*/
|
|
16
|
+
export const parseBool = (_lexer: Lexer): Result<Pattern> => {
|
|
17
|
+
// `bool` keyword was already consumed, just return the pattern
|
|
18
|
+
return Ok(anyBool());
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Parse a `true` literal.
|
|
23
|
+
*/
|
|
24
|
+
export const parseBoolTrue = (_lexer: Lexer): Result<Pattern> => {
|
|
25
|
+
return Ok(bool(true));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Parse a `false` literal.
|
|
30
|
+
*/
|
|
31
|
+
export const parseBoolFalse = (_lexer: Lexer): Result<Pattern> => {
|
|
32
|
+
return Ok(bool(false));
|
|
33
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ByteString pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/bytestring-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
import { anyByteString, byteString, byteStringRegex } from "../../pattern";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse a bytestring pattern from the `bytes` keyword.
|
|
15
|
+
*/
|
|
16
|
+
export const parseByteString = (_lexer: Lexer): Result<Pattern> => {
|
|
17
|
+
// `bytes` keyword was already consumed
|
|
18
|
+
return Ok(anyByteString());
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Parse a hex string token result into a pattern.
|
|
23
|
+
*/
|
|
24
|
+
export const parseHexStringToken = (hexResult: Result<Uint8Array>): Result<Pattern> => {
|
|
25
|
+
if (!hexResult.ok) {
|
|
26
|
+
return hexResult;
|
|
27
|
+
}
|
|
28
|
+
return Ok(byteString(hexResult.value));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parse a hex regex token result into a pattern.
|
|
33
|
+
*
|
|
34
|
+
* In TypeScript, binary regex matching is implemented by converting bytes to Latin-1 strings.
|
|
35
|
+
* This mimics Rust's regex::bytes::Regex behavior where each byte 0-255 maps to a character.
|
|
36
|
+
*/
|
|
37
|
+
export const parseHexRegexToken = (regexResult: Result<RegExp>): Result<Pattern> => {
|
|
38
|
+
if (!regexResult.ok) {
|
|
39
|
+
return regexResult;
|
|
40
|
+
}
|
|
41
|
+
return Ok(byteStringRegex(regexResult.value));
|
|
42
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Date pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/date-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
|
|
12
|
+
// Import date pattern constructor
|
|
13
|
+
import { datePatternAny } from "../../pattern/value/date-pattern";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parse a date pattern from the `date` keyword.
|
|
17
|
+
*/
|
|
18
|
+
export const parseDate = (_lexer: Lexer): Result<Pattern> => {
|
|
19
|
+
// `date` keyword was already consumed
|
|
20
|
+
return Ok({
|
|
21
|
+
kind: "Value",
|
|
22
|
+
pattern: { type: "Date", pattern: datePatternAny() },
|
|
23
|
+
});
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Digest pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/digest-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
|
|
12
|
+
// Import digest pattern constructor
|
|
13
|
+
import { digestPatternAny } from "../../pattern/value/digest-pattern";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parse a digest pattern from the `digest` keyword.
|
|
17
|
+
*/
|
|
18
|
+
export const parseDigest = (_lexer: Lexer): Result<Pattern> => {
|
|
19
|
+
// `digest` keyword was already consumed
|
|
20
|
+
return Ok({
|
|
21
|
+
kind: "Value",
|
|
22
|
+
pattern: { type: "Digest", pattern: digestPatternAny() },
|
|
23
|
+
});
|
|
24
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Value parsers for dCBOR patterns.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export * from "./bool-parser";
|
|
8
|
+
export * from "./null-parser";
|
|
9
|
+
export * from "./number-parser";
|
|
10
|
+
export * from "./text-parser";
|
|
11
|
+
export * from "./bytestring-parser";
|
|
12
|
+
export * from "./date-parser";
|
|
13
|
+
export * from "./digest-parser";
|
|
14
|
+
export * from "./known-value-parser";
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KnownValue pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/known-value-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
|
|
12
|
+
// Import known value pattern constructor
|
|
13
|
+
import { knownValuePatternAny } from "../../pattern/value/known-value-pattern";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Parse a known value pattern from the `known` keyword.
|
|
17
|
+
*/
|
|
18
|
+
export const parseKnownValue = (_lexer: Lexer): Result<Pattern> => {
|
|
19
|
+
// `known` keyword was already consumed
|
|
20
|
+
return Ok({
|
|
21
|
+
kind: "Value",
|
|
22
|
+
pattern: { type: "KnownValue", pattern: knownValuePatternAny() },
|
|
23
|
+
});
|
|
24
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Null pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/null-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
import { nullPattern } from "../../pattern";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse a null pattern from the `null` keyword.
|
|
15
|
+
*/
|
|
16
|
+
export const parseNull = (_lexer: Lexer): Result<Pattern> => {
|
|
17
|
+
// `null` keyword was already consumed
|
|
18
|
+
return Ok(nullPattern());
|
|
19
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Number pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/number-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok } from "../../error";
|
|
11
|
+
import { anyNumber } from "../../pattern";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse a number pattern from the `number` keyword.
|
|
15
|
+
*/
|
|
16
|
+
export const parseNumber = (_lexer: Lexer): Result<Pattern> => {
|
|
17
|
+
// `number` keyword was already consumed
|
|
18
|
+
return Ok(anyNumber());
|
|
19
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text pattern parser.
|
|
3
|
+
*
|
|
4
|
+
* @module parse/value/text-parser
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Lexer } from "../token";
|
|
8
|
+
import type { Pattern } from "../../pattern";
|
|
9
|
+
import type { Result } from "../../error";
|
|
10
|
+
import { Ok, Err } from "../../error";
|
|
11
|
+
import { anyText, text, textRegex } from "../../pattern";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse a text pattern from the `text` keyword.
|
|
15
|
+
*/
|
|
16
|
+
export const parseText = (lexer: Lexer): Result<Pattern> => {
|
|
17
|
+
// `text` keyword was already consumed
|
|
18
|
+
// Check if followed by a quoted value
|
|
19
|
+
const peeked = lexer.peekToken();
|
|
20
|
+
if (peeked?.ok === true) {
|
|
21
|
+
const token = peeked.value;
|
|
22
|
+
if (token.type === "SingleQuoted") {
|
|
23
|
+
lexer.next(); // consume the token
|
|
24
|
+
// Check if it's a regex (starts and ends with /)
|
|
25
|
+
if (token.value.startsWith("/") && token.value.endsWith("/") && token.value.length > 2) {
|
|
26
|
+
const regexBody = token.value.slice(1, -1);
|
|
27
|
+
try {
|
|
28
|
+
const regex = new RegExp(regexBody);
|
|
29
|
+
return Ok(textRegex(regex));
|
|
30
|
+
} catch {
|
|
31
|
+
return Err({ type: "InvalidRegex", span: lexer.span() });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Otherwise it's a literal string match
|
|
35
|
+
return Ok(text(token.value));
|
|
36
|
+
}
|
|
37
|
+
if (token.type === "StringLiteral") {
|
|
38
|
+
lexer.next(); // consume the token
|
|
39
|
+
return Ok(text(token.value));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Ok(anyText());
|
|
43
|
+
};
|