tarsec 0.0.15 → 0.0.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/types.d.ts DELETED
@@ -1,113 +0,0 @@
1
- /** A generic object type. */
2
- export type PlainObject = Record<string, unknown>;
3
- /** Represents a parse success with no captures. */
4
- export type ParserSuccess<T> = {
5
- success: true;
6
- result: T;
7
- rest: string;
8
- nextParser?: Parser<any>;
9
- };
10
- /** Represents a parse success with captures. Notice nextParser is also a CaptureParser. */
11
- export type CaptureParserSuccess<T, C extends PlainObject> = {
12
- success: true;
13
- result: T;
14
- rest: string;
15
- captures: C;
16
- nextParser?: CaptureParser<any, any>;
17
- };
18
- /** Represents a parse failure. */
19
- export type ParserFailure = {
20
- success: false;
21
- rest: string;
22
- message: string;
23
- };
24
- export type ParserResult<T> = ParserSuccess<T> | ParserFailure;
25
- export type CaptureParserResult<T, C extends PlainObject> = CaptureParserSuccess<T, C> | ParserFailure;
26
- /** A parser is any function that takes a string and returns a ParserResult. */
27
- export type Parser<T> = (input: string) => ParserResult<T>;
28
- /** A capture parser is any function that takes a string and returns a CaptureParserResult.
29
- * A CaptureParserResult is the same as a ParserResult, except it also includes captures,
30
- * i.e. matches selected using `capture`. */
31
- export type CaptureParser<T, C extends PlainObject> = (input: string) => CaptureParserResult<T, C>;
32
- export type GeneralParser<T, C extends PlainObject> = Parser<T> | CaptureParser<T, C>;
33
- export declare function isCaptureResult<T, C extends PlainObject>(result: ParserResult<T>): result is CaptureParserSuccess<T, C>;
34
- /** Convenience function to return a ParserSuccess */
35
- export declare function success<T>(result: T, rest: string): ParserSuccess<T>;
36
- /** Convenience function to return a CaptureParserSuccess */
37
- export declare function captureSuccess<T, C extends PlainObject>(result: T, rest: string, captures: C): CaptureParserSuccess<T, C>;
38
- /** Convenience function to return a ParserFailure */
39
- export declare function failure(message: string, rest: string): ParserFailure;
40
- /** Prettify an intersected type, to make it easier to read. */
41
- export type Prettify<T> = {
42
- [K in keyof T]: T[K];
43
- } & {};
44
- /** see <https://stackoverflow.com/a/50375286/3625> */
45
- export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
46
- /** Convenience type to get the result type out of a parser. */
47
- type ExtractResults<T> = T extends Parser<infer U> ? U : never;
48
- /** Convenience type to get the capture type out of a capture parser. */
49
- type ExtractCaptures<T> = T extends CaptureParser<any, infer U> ? U : never;
50
- /** Convenience type where given an array of parsers and capture parsers,
51
- * it returns the types of the capture parsers, like
52
- * CaptureParser<string, { name: string }> | CaptureParser<number, { age: number }>
53
- */
54
- type ExtractCaptureParsers<T extends readonly GeneralParser<any, any>[]> = Extract<T[number], CaptureParser<any, any>>;
55
- /** Convenience type where given an array of parsers and capture parsers,
56
- * it returns a single capture type that merges all the capture types. */
57
- export type MergedCaptures<T extends readonly GeneralParser<any, any>[]> = Prettify<UnionToIntersection<UnionOfCaptures<T>>>;
58
- /** Convenience type where given an array of parsers and capture parsers,
59
- * it first gets the capture parsers, then extracts the capture types,
60
- * and returns a union of all the capture types. Example:
61
- * { name: string } | { age: number }
62
- */
63
- export type UnionOfCaptures<T extends readonly GeneralParser<any, any>[]> = Prettify<ExtractCaptures<ExtractCaptureParsers<T>>>;
64
- /** Convenience type where given an array of parsers and capture parsers,
65
- * it returns true if there are any capture parsers, otherwise false. */
66
- export type HasCaptureParsers<T extends readonly GeneralParser<any, any>[]> = ExtractCaptureParsers<T> extends never ? false : true;
67
- /**
68
- * For a given array of GeneralParsers, if any of them is a CaptureParser,
69
- * PickParserType says the array is an array of CaptureParsers,
70
- * otherwise it's an array of Parsers. It also correctly merges
71
- * the result and capture types. This is useful for a combinator like `or`
72
- * which is not able to infer its return type correctly.
73
- */
74
- export type PickParserType<T extends readonly GeneralParser<any, any>[]> = HasCaptureParsers<T> extends true ? CaptureParser<MergedResults<T>, UnionOfCaptures<T>> : Parser<MergedResults<T>>;
75
- /** This is used to generate a return type for the `many` and `many1` combinators.
76
- * Given a parser we want to apply `many` to. Suppose its type is `Parser<string>`.
77
- * This will return `Parser<string[]>` as the return type.
78
- *
79
- * Suppose the parser is a `CaptureParser<string, { name: string }>`.
80
- * This will return `CaptureParser<string[], { captures: { name: string }[] }>` as the return type.
81
- */
82
- export type InferManyReturnType<T extends GeneralParser<any, any>> = T extends CaptureParser<infer R, infer C> ? CaptureParser<R[], {
83
- captures: C[];
84
- }> : T extends Parser<infer R> ? Parser<R[]> : never;
85
- export type MergedResults<T extends readonly GeneralParser<any, any>[]> = ExtractResults<T[number]>;
86
- /** Used to create a parser tree for backtracking. */
87
- export type Node = ParserNode | EmptyNode;
88
- export type ParserNode = {
89
- parent: Node;
90
- parser: GeneralParser<any, any> | null;
91
- input?: string;
92
- child: Node;
93
- closed: boolean;
94
- };
95
- export type EmptyNode = null;
96
- /** Convenience function to create a ParserNode. */
97
- export declare function createNode(parent: Node | null, parser: GeneralParser<any, any>): ParserNode;
98
- /** Convenience function where, given an array of parsers, it creates a tree we can use for backtracking.
99
- * This tree is what `seq` use. It's used to keep track of the parsers we've tried so far,
100
- * so we can backtrack if we need to.
101
- */
102
- export declare function createTree(parsers: readonly GeneralParser<any, any>[]): Node;
103
- /** Used by `within`. */
104
- export type Matched = {
105
- type: "matched";
106
- value: string;
107
- };
108
- export type Unmatched = {
109
- type: "unmatched";
110
- value: string;
111
- };
112
- export type WithinResult = Matched | Unmatched;
113
- export {};
package/dist/types.js DELETED
@@ -1,40 +0,0 @@
1
- export function isCaptureResult(result) {
2
- return "captures" in result;
3
- }
4
- /** Convenience function to return a ParserSuccess */
5
- export function success(result, rest) {
6
- return { success: true, result, rest };
7
- }
8
- /** Convenience function to return a CaptureParserSuccess */
9
- export function captureSuccess(result, rest, captures) {
10
- return { success: true, result, rest, captures };
11
- }
12
- /** Convenience function to return a ParserFailure */
13
- export function failure(message, rest) {
14
- return { success: false, message, rest };
15
- }
16
- /** Convenience function to create a ParserNode. */
17
- export function createNode(parent, parser) {
18
- return {
19
- parent,
20
- parser,
21
- child: null,
22
- closed: false,
23
- };
24
- }
25
- /** Convenience function where, given an array of parsers, it creates a tree we can use for backtracking.
26
- * This tree is what `seq` use. It's used to keep track of the parsers we've tried so far,
27
- * so we can backtrack if we need to.
28
- */
29
- export function createTree(parsers) {
30
- if (parsers.length === 0) {
31
- return null;
32
- }
33
- const rootNode = createNode(null, parsers[0]);
34
- let currentNode = rootNode;
35
- for (let i = 1; i < parsers.length; i++) {
36
- currentNode.child = createNode(currentNode, parsers[i]);
37
- currentNode = currentNode.child;
38
- }
39
- return rootNode;
40
- }
package/dist/utils.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import { Node } from "./types.js";
2
- export declare function escape(str: any): string;
3
- export declare function merge(a: any | any[], b: any | any[]): any[];
4
- export declare function mergeCaptures(a: Record<string, any>, b: Record<string, any>): Record<string, any>;
5
- export declare function findAncestorWithNextParser(node: Node, count?: number): [Node, number];
6
- export declare function popMany(arr: any[], count: number): void;
7
- export declare function round(num: number, places?: number): number;
8
- export declare function shorten(str: string, length?: number): string;
package/dist/utils.js DELETED
@@ -1,57 +0,0 @@
1
- export function escape(str) {
2
- return JSON.stringify(str);
3
- }
4
- export function merge(a, b) {
5
- if (Array.isArray(a) && Array.isArray(b)) {
6
- return [...a, ...b];
7
- }
8
- else if (Array.isArray(a)) {
9
- return [...a, b];
10
- }
11
- else if (Array.isArray(b)) {
12
- return [a, ...b];
13
- }
14
- else {
15
- return [a, b];
16
- }
17
- }
18
- export function mergeCaptures(a, b) {
19
- const result = {};
20
- Object.keys(a).forEach((key) => {
21
- result[key] = a[key];
22
- });
23
- Object.keys(b).forEach((key) => {
24
- if (result[key]) {
25
- result[key] = merge(result[key], b[key]);
26
- }
27
- else {
28
- result[key] = b[key];
29
- }
30
- });
31
- return result;
32
- }
33
- export function findAncestorWithNextParser(node, count = 0) {
34
- if (node === null)
35
- return [null, count];
36
- if (!node.closed) {
37
- return [node, count];
38
- }
39
- if (node.parent) {
40
- return findAncestorWithNextParser(node.parent, count + 1);
41
- }
42
- return [null, count];
43
- }
44
- export function popMany(arr, count) {
45
- for (let i = 0; i < count; i++) {
46
- arr.pop();
47
- }
48
- }
49
- export function round(num, places = 2) {
50
- return Math.round(num * 10 ** places) / 10 ** places;
51
- }
52
- export function shorten(str, length = 250) {
53
- if (str.length > length) {
54
- return str.substring(0, length) + "...";
55
- }
56
- return str;
57
- }