tarsec 0.0.16 → 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/package.json +1 -1
- package/dist/combinators/seq.d.ts +0 -1
- package/dist/combinators/seq.js +0 -1
- package/dist/combinators.d.ts +0 -278
- package/dist/combinators.js +0 -664
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -4
- package/dist/parsers/within.d.ts +0 -2
- package/dist/parsers/within.js +0 -37
- package/dist/parsers.d.ts +0 -212
- package/dist/parsers.js +0 -341
- package/dist/trace.d.ts +0 -99
- package/dist/trace.js +0 -187
- package/dist/types.d.ts +0 -141
- package/dist/types.js +0 -57
- package/dist/utils.d.ts +0 -8
- package/dist/utils.js +0 -57
package/dist/trace.js
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import { escape, round, shorten } from "./utils.js";
|
|
2
|
-
import process from "process";
|
|
3
|
-
const STEP = 2;
|
|
4
|
-
/**
|
|
5
|
-
* This function is used internally by the `trace` function to create the string for each step.
|
|
6
|
-
* @param name - debug name for parser
|
|
7
|
-
* @param result - parser result
|
|
8
|
-
* @returns - A formatted string that describes the parser's result
|
|
9
|
-
*/
|
|
10
|
-
export function resultToString(name, result) {
|
|
11
|
-
if (result.success) {
|
|
12
|
-
return `✅ ${name} -- match: ${escape(result.result)}, rest: ${escape(result.rest)}`;
|
|
13
|
-
}
|
|
14
|
-
return `❌ ${name} -- message: ${escape(result.message)}, rest: ${escape(result.rest)}`;
|
|
15
|
-
}
|
|
16
|
-
let level = 0;
|
|
17
|
-
let counts = {};
|
|
18
|
-
let times = {};
|
|
19
|
-
let debugFlag = !!process.env.DEBUG;
|
|
20
|
-
let stepCount = 0;
|
|
21
|
-
let stepLimit = -1;
|
|
22
|
-
/**
|
|
23
|
-
* This function is used internally with debug mode. Given a parser and a debug name for it,
|
|
24
|
-
* when the parser is called, `trace` will:
|
|
25
|
-
* 1. Print a line when the parser starts
|
|
26
|
-
* 2. print a line when the parser ends, indicating success or failure.
|
|
27
|
-
* 3. If the parser returns any captures, print the captures.
|
|
28
|
-
* 4. Count the number of times this parser has been run.
|
|
29
|
-
* 5. Track the total time this parser has taken.
|
|
30
|
-
* 6. Track the total number of steps your parser has taken (a step equals one parser invocation).
|
|
31
|
-
* So, for example, you may find out that your parser to parse Markdown has taken 50 steps to parse that file.
|
|
32
|
-
*
|
|
33
|
-
* All this happens only if debug mode is on, which you can turn on by using `parserDebug`, or setting the env var `DEBUG` to `1`.
|
|
34
|
-
*
|
|
35
|
-
* Caveat: If you have debug mode on through an environment variable, `trace` will capture counts and times
|
|
36
|
-
* for all parsers across your entire application. If you want to profile just a particular section of code, use `parserDebug` instead.
|
|
37
|
-
* If you *do* want to track constant times for all parsers, don't use `parserDebug` as it will reset those.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* `trace` works with tarsec's built-in parsers out of the box. You can easily set it up to work with your custom parser too.
|
|
41
|
-
*
|
|
42
|
-
* For example, if your parser looks like this:
|
|
43
|
-
*
|
|
44
|
-
* ```ts
|
|
45
|
-
* const myParser = (input:string) => {
|
|
46
|
-
* if (input === "hello") {
|
|
47
|
-
* return { success: true, result: "hello", rest: "" };
|
|
48
|
-
* }
|
|
49
|
-
* return { success: false, message: "expected hello", rest: input };
|
|
50
|
-
* }
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* You can wrap it in `trace` like this:
|
|
54
|
-
*
|
|
55
|
-
* ```ts
|
|
56
|
-
* const myParser = trace("myParser", (input:string) => {
|
|
57
|
-
* if (input === "hello") {
|
|
58
|
-
* return { success: true, result: "hello", rest: "" };
|
|
59
|
-
* }
|
|
60
|
-
* return { success: false, message: "expected hello", rest: input };
|
|
61
|
-
* });
|
|
62
|
-
* ```
|
|
63
|
-
*
|
|
64
|
-
* Now, when you run `myParser("hello")` with debug mode on,
|
|
65
|
-
* you will see the debug output.
|
|
66
|
-
*
|
|
67
|
-
* Some parsers, like `seq`, are very general. You might have a few parser that use `seq`.
|
|
68
|
-
* So when you see `seq` debug output, you might not know which `seq` parser that means.
|
|
69
|
-
* ou can pass `seq` a debug name as an optional third argument to be used in the debug output.
|
|
70
|
-
* This name is used to track count and time, so using this name will also mean this `seq` parser's
|
|
71
|
-
* count and time are tracked separately from the other `seq` parsers, which might be useful.
|
|
72
|
-
*
|
|
73
|
-
* @param name - debug name for parser
|
|
74
|
-
* @param parser - parser to run
|
|
75
|
-
* @returns
|
|
76
|
-
*/
|
|
77
|
-
export function trace(name, parser) {
|
|
78
|
-
if (stepLimit > 0 && stepCount > stepLimit) {
|
|
79
|
-
throw new Error(`parser step limit of ${stepLimit} exceeded, parser may be in an infinite loop`);
|
|
80
|
-
}
|
|
81
|
-
return (input) => {
|
|
82
|
-
if (debugFlag) {
|
|
83
|
-
console.log(" ".repeat(level) + `🔍 ${name} -- input: ${shorten(escape(input))}`);
|
|
84
|
-
let result;
|
|
85
|
-
const time = parserTime(() => {
|
|
86
|
-
level += STEP;
|
|
87
|
-
result = parser(input);
|
|
88
|
-
level -= STEP;
|
|
89
|
-
});
|
|
90
|
-
counts[name] = counts[name] ? counts[name] + 1 : 1;
|
|
91
|
-
stepCount += 1;
|
|
92
|
-
if (time) {
|
|
93
|
-
times[name] = times[name] ? times[name] + time : time;
|
|
94
|
-
}
|
|
95
|
-
console.log(" ".repeat(level) + resultToString(name, result));
|
|
96
|
-
if (result.success && result.captures) {
|
|
97
|
-
console.log(" ".repeat(level) +
|
|
98
|
-
`⭐ ${name} -- captures: ${JSON.stringify(result.captures)}`);
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
return parser(input);
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Utility timing function. Given a callback, it times the callback
|
|
109
|
-
* and returns its runtime in milliseconds. It uses `performance.now()` to do this.
|
|
110
|
-
* If `performance.now()` is not available, it returns null.
|
|
111
|
-
*
|
|
112
|
-
* @param callback - callback to time
|
|
113
|
-
* @returns - time in milliseconds
|
|
114
|
-
*/
|
|
115
|
-
export function parserTime(callback) {
|
|
116
|
-
if (performance && performance.now) {
|
|
117
|
-
const start = performance.now();
|
|
118
|
-
callback();
|
|
119
|
-
const end = performance.now();
|
|
120
|
-
return end - start;
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
console.error("performance.now not available");
|
|
124
|
-
callback();
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Wrapper for parser time. Instead of returning the time in milliseconds,
|
|
130
|
-
* it console.logs it, in a nicely formatted string.
|
|
131
|
-
* @param name - debug name for timing
|
|
132
|
-
* @param callback - callback to time
|
|
133
|
-
*/
|
|
134
|
-
export function printTime(name, callback) {
|
|
135
|
-
const time = parserTime(callback);
|
|
136
|
-
if (time) {
|
|
137
|
-
console.log(`⏱ ${name} -- time: ${round(time)}ms`);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* This is the recommended way to run a parser in debug mode.
|
|
142
|
-
* Takes a callback and turns debug mode on just for the callback.
|
|
143
|
-
* This enables `trace` to capture all sorts of information
|
|
144
|
-
* about any executed parsers and print them to console.log.
|
|
145
|
-
* `trace` tracks counts and times but they don't actually get reset to zero
|
|
146
|
-
* unless you use this function to wrap your code.
|
|
147
|
-
*
|
|
148
|
-
* @param name - debug name
|
|
149
|
-
* @param callback - callback to run in debug mode
|
|
150
|
-
*/
|
|
151
|
-
export function parserDebug(name, callback) {
|
|
152
|
-
debugFlag = true;
|
|
153
|
-
stepCount = 0;
|
|
154
|
-
counts = {};
|
|
155
|
-
times = {};
|
|
156
|
-
printTime(name, callback);
|
|
157
|
-
debugFlag = false;
|
|
158
|
-
console.log("\n");
|
|
159
|
-
console.log(`📊 ${name} -- counts:`);
|
|
160
|
-
const sorted = Object.entries(counts).sort((a, b) => b[1] - a[1]);
|
|
161
|
-
for (const [name, count] of sorted) {
|
|
162
|
-
console.log(` ${name}: ${count}`);
|
|
163
|
-
}
|
|
164
|
-
console.log("\n");
|
|
165
|
-
console.log(`📊 ${name} -- times:`);
|
|
166
|
-
const sortedTimes = Object.entries(times).sort((a, b) => b[1] - a[1]);
|
|
167
|
-
for (const [name, time] of sortedTimes) {
|
|
168
|
-
console.log(` ${name}: ${round(time)}ms`);
|
|
169
|
-
}
|
|
170
|
-
console.log("\n");
|
|
171
|
-
console.log(`📊 ${name} -- step count: ${stepCount}`);
|
|
172
|
-
console.log("\n\n");
|
|
173
|
-
stepCount = 0;
|
|
174
|
-
counts = {};
|
|
175
|
-
times = {};
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Utility function to limit the number of steps a parser can take.
|
|
179
|
-
* This is useful for avoiding infinite loops in your parser.
|
|
180
|
-
* @param limit - number of steps to limit the parser to
|
|
181
|
-
* @param callback - callback to run
|
|
182
|
-
*/
|
|
183
|
-
export function limitSteps(limit, callback) {
|
|
184
|
-
stepLimit = limit;
|
|
185
|
-
callback();
|
|
186
|
-
stepLimit = -1;
|
|
187
|
-
}
|
package/dist/types.d.ts
DELETED
|
@@ -1,141 +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, R = string> = {
|
|
5
|
-
success: true;
|
|
6
|
-
result: T;
|
|
7
|
-
rest: R;
|
|
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<R = string> = {
|
|
20
|
-
success: false;
|
|
21
|
-
rest: R;
|
|
22
|
-
message: string;
|
|
23
|
-
};
|
|
24
|
-
export type ParserResult<T, R = string> = ParserSuccess<T, R> | ParserFailure<R>;
|
|
25
|
-
export type CaptureParserResult<T, C extends PlainObject, R = string> = CaptureParserSuccess<T, C> | ParserFailure<R>;
|
|
26
|
-
/** A parser is any function that takes an arg and returns a ParserResult. */
|
|
27
|
-
export type Parser<T, I = string> = (input: I) => ParserResult<T>;
|
|
28
|
-
/** A string parser is any function that takes a string and returns a ParserResult. */
|
|
29
|
-
export type StringParser<T> = (input: string) => ParserResult<T>;
|
|
30
|
-
/** A capture parser is any function that takes an arg and returns a CaptureParserResult.
|
|
31
|
-
* A CaptureParserResult is the same as a ParserResult, except it also includes captures,
|
|
32
|
-
* i.e. matches selected using `capture`. */
|
|
33
|
-
export type CaptureParser<T, C extends PlainObject, I = string> = (input: I) => CaptureParserResult<T, C>;
|
|
34
|
-
/** A string capture parser is any function that takes a string and returns a CaptureParserResult. */
|
|
35
|
-
export type StringCaptureParser<T, C extends PlainObject> = (input: string) => CaptureParserResult<T, C>;
|
|
36
|
-
export type GeneralParser<T, C extends PlainObject, I = string> = Parser<T, I> | CaptureParser<T, C, I>;
|
|
37
|
-
export type GeneralStringParser<T, C extends PlainObject> = Parser<T, string> | CaptureParser<T, C, string>;
|
|
38
|
-
export declare function isCaptureResult<T, C extends PlainObject>(result: ParserResult<T>): result is CaptureParserSuccess<T, C>;
|
|
39
|
-
/**
|
|
40
|
-
* This typed function is helpful in filtering out the successes
|
|
41
|
-
* from an array of results while preserving type information. For example:
|
|
42
|
-
*
|
|
43
|
-
* ```
|
|
44
|
-
* // type is ParserSuccess[]
|
|
45
|
-
* results.filter(isSuccess);
|
|
46
|
-
*
|
|
47
|
-
* // type is ParserResult[]
|
|
48
|
-
* results.filter(r => r.success);
|
|
49
|
-
* ```
|
|
50
|
-
* @param result - a parser result
|
|
51
|
-
* @returns - true if the result is a success, otherwise false
|
|
52
|
-
*/
|
|
53
|
-
export declare function isSuccess<T, R = string>(result: ParserResult<T, R>): result is ParserSuccess<T, R>;
|
|
54
|
-
/** Convenience function to return a ParserSuccess */
|
|
55
|
-
export declare function success<T, R = string>(result: T, rest: R): ParserSuccess<T, R>;
|
|
56
|
-
/** Convenience function to return a CaptureParserSuccess */
|
|
57
|
-
export declare function captureSuccess<T, C extends PlainObject>(result: T, rest: string, captures: C): CaptureParserSuccess<T, C>;
|
|
58
|
-
/** Convenience function to return a ParserFailure */
|
|
59
|
-
export declare function failure<R = string>(message: string, rest: R): ParserFailure<R>;
|
|
60
|
-
/** Prettify an intersected type, to make it easier to read. */
|
|
61
|
-
export type Prettify<T> = {
|
|
62
|
-
[K in keyof T]: T[K];
|
|
63
|
-
} & {};
|
|
64
|
-
/** see <https://stackoverflow.com/a/50375286/3625> */
|
|
65
|
-
export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
66
|
-
/** Convenience type to get the result type out of a parser. */
|
|
67
|
-
type ExtractResults<T> = T extends Parser<infer U> ? U : never;
|
|
68
|
-
/** Convenience type to get the capture type out of a capture parser. */
|
|
69
|
-
type ExtractCaptures<T> = T extends CaptureParser<any, infer U> ? U : never;
|
|
70
|
-
/** Convenience type where given an array of parsers and capture parsers,
|
|
71
|
-
* it returns the types of the capture parsers, like
|
|
72
|
-
* CaptureParser<string, { name: string }> | CaptureParser<number, { age: number }>
|
|
73
|
-
*/
|
|
74
|
-
type ExtractCaptureParsers<T extends readonly GeneralParser<any, any>[]> = Extract<T[number], CaptureParser<any, any>>;
|
|
75
|
-
/** Convenience type where given an array of parsers and capture parsers,
|
|
76
|
-
* it returns a single capture type that merges all the capture types. */
|
|
77
|
-
export type MergedCaptures<T extends readonly GeneralParser<any, any>[]> = Prettify<UnionToIntersection<UnionOfCaptures<T>>>;
|
|
78
|
-
/** Convenience type where given an array of parsers and capture parsers,
|
|
79
|
-
* it first gets the capture parsers, then extracts the capture types,
|
|
80
|
-
* and returns a union of all the capture types. Example:
|
|
81
|
-
* { name: string } | { age: number }
|
|
82
|
-
*/
|
|
83
|
-
export type UnionOfCaptures<T extends readonly GeneralParser<any, any>[]> = Prettify<ExtractCaptures<ExtractCaptureParsers<T>>>;
|
|
84
|
-
/** Convenience type where given an array of parsers and capture parsers,
|
|
85
|
-
* it returns true if there are any capture parsers, otherwise false. */
|
|
86
|
-
export type HasCaptureParsers<T extends readonly GeneralParser<any, any>[]> = ExtractCaptureParsers<T> extends never ? false : true;
|
|
87
|
-
/**
|
|
88
|
-
* For a given array of GeneralParsers, if any of them is a CaptureParser,
|
|
89
|
-
* PickParserType says the array is an array of CaptureParsers,
|
|
90
|
-
* otherwise it's an array of Parsers. It also correctly merges
|
|
91
|
-
* the result and capture types. This is useful for a combinator like `or`
|
|
92
|
-
* which is not able to infer its return type correctly.
|
|
93
|
-
*/
|
|
94
|
-
export type PickParserType<T extends readonly GeneralParser<any, any>[], I = string> = I extends string ? PickParserTypeString<T> : PickParserTypeGeneral<T, I>;
|
|
95
|
-
/** split out to make types more readable */
|
|
96
|
-
export type PickParserTypeGeneral<T extends readonly GeneralParser<any, any>[], I = string> = HasCaptureParsers<T> extends true ? CaptureParser<MergedResults<T>, UnionOfCaptures<T>, I> : Parser<MergedResults<T>, I>;
|
|
97
|
-
export type PickParserTypeString<T extends readonly GeneralParser<any, any>[]> = HasCaptureParsers<T> extends true ? StringCaptureParser<MergedResults<T>, UnionOfCaptures<T>> : StringParser<MergedResults<T>>;
|
|
98
|
-
/**
|
|
99
|
-
* Like `PickParserType`, but the result is an array of the result types,
|
|
100
|
-
* instead of just a union.
|
|
101
|
-
*/
|
|
102
|
-
export type PickParserTypeArray<T extends readonly GeneralParser<any, any>[], I = string> = HasCaptureParsers<T> extends true ? CaptureParser<MergedResults<T>, UnionOfCaptures<T>, I> : Parser<MergedResults<T>[], I>;
|
|
103
|
-
/** This is used to generate a return type for the `many` and `many1` combinators.
|
|
104
|
-
* Given a parser we want to apply `many` to. Suppose its type is `Parser<string>`.
|
|
105
|
-
* This will return `Parser<string[]>` as the return type.
|
|
106
|
-
*
|
|
107
|
-
* Suppose the parser is a `CaptureParser<string, { name: string }>`.
|
|
108
|
-
* This will return `CaptureParser<string[], { captures: { name: string }[] }>` as the return type.
|
|
109
|
-
*/
|
|
110
|
-
export type InferManyReturnType<T extends GeneralParser<any, any>> = T extends CaptureParser<infer R, infer C> ? CaptureParser<R[], {
|
|
111
|
-
captures: C[];
|
|
112
|
-
}> : T extends Parser<infer R> ? Parser<R[]> : never;
|
|
113
|
-
export type MergedResults<T extends readonly GeneralParser<any, any>[]> = ExtractResults<T[number]>;
|
|
114
|
-
/** Used to create a parser tree for backtracking. */
|
|
115
|
-
export type Node = ParserNode | EmptyNode;
|
|
116
|
-
export type ParserNode = {
|
|
117
|
-
parent: Node;
|
|
118
|
-
parser: GeneralParser<any, any> | null;
|
|
119
|
-
input?: string;
|
|
120
|
-
child: Node;
|
|
121
|
-
closed: boolean;
|
|
122
|
-
};
|
|
123
|
-
export type EmptyNode = null;
|
|
124
|
-
/** Convenience function to create a ParserNode. */
|
|
125
|
-
export declare function createNode(parent: Node | null, parser: GeneralParser<any, any>): ParserNode;
|
|
126
|
-
/** Convenience function where, given an array of parsers, it creates a tree we can use for backtracking.
|
|
127
|
-
* This tree is what `seq` use. It's used to keep track of the parsers we've tried so far,
|
|
128
|
-
* so we can backtrack if we need to.
|
|
129
|
-
*/
|
|
130
|
-
export declare function createTree(parsers: readonly GeneralParser<any, any>[]): Node;
|
|
131
|
-
/** Used by `within`. */
|
|
132
|
-
export type Matched = {
|
|
133
|
-
type: "matched";
|
|
134
|
-
value: string;
|
|
135
|
-
};
|
|
136
|
-
export type Unmatched = {
|
|
137
|
-
type: "unmatched";
|
|
138
|
-
value: string;
|
|
139
|
-
};
|
|
140
|
-
export type WithinResult = Matched | Unmatched;
|
|
141
|
-
export {};
|
package/dist/types.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
export function isCaptureResult(result) {
|
|
2
|
-
return "captures" in result;
|
|
3
|
-
}
|
|
4
|
-
/**
|
|
5
|
-
* This typed function is helpful in filtering out the successes
|
|
6
|
-
* from an array of results while preserving type information. For example:
|
|
7
|
-
*
|
|
8
|
-
* ```
|
|
9
|
-
* // type is ParserSuccess[]
|
|
10
|
-
* results.filter(isSuccess);
|
|
11
|
-
*
|
|
12
|
-
* // type is ParserResult[]
|
|
13
|
-
* results.filter(r => r.success);
|
|
14
|
-
* ```
|
|
15
|
-
* @param result - a parser result
|
|
16
|
-
* @returns - true if the result is a success, otherwise false
|
|
17
|
-
*/
|
|
18
|
-
export function isSuccess(result) {
|
|
19
|
-
return result.success;
|
|
20
|
-
}
|
|
21
|
-
/** Convenience function to return a ParserSuccess */
|
|
22
|
-
export function success(result, rest) {
|
|
23
|
-
return { success: true, result, rest };
|
|
24
|
-
}
|
|
25
|
-
/** Convenience function to return a CaptureParserSuccess */
|
|
26
|
-
export function captureSuccess(result, rest, captures) {
|
|
27
|
-
return { success: true, result, rest, captures };
|
|
28
|
-
}
|
|
29
|
-
/** Convenience function to return a ParserFailure */
|
|
30
|
-
export function failure(message, rest) {
|
|
31
|
-
return { success: false, message, rest };
|
|
32
|
-
}
|
|
33
|
-
/** Convenience function to create a ParserNode. */
|
|
34
|
-
export function createNode(parent, parser) {
|
|
35
|
-
return {
|
|
36
|
-
parent,
|
|
37
|
-
parser,
|
|
38
|
-
child: null,
|
|
39
|
-
closed: false,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/** Convenience function where, given an array of parsers, it creates a tree we can use for backtracking.
|
|
43
|
-
* This tree is what `seq` use. It's used to keep track of the parsers we've tried so far,
|
|
44
|
-
* so we can backtrack if we need to.
|
|
45
|
-
*/
|
|
46
|
-
export function createTree(parsers) {
|
|
47
|
-
if (parsers.length === 0) {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
const rootNode = createNode(null, parsers[0]);
|
|
51
|
-
let currentNode = rootNode;
|
|
52
|
-
for (let i = 1; i < parsers.length; i++) {
|
|
53
|
-
currentNode.child = createNode(currentNode, parsers[i]);
|
|
54
|
-
currentNode = currentNode.child;
|
|
55
|
-
}
|
|
56
|
-
return rootNode;
|
|
57
|
-
}
|
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
|
-
}
|