tarsec 0.0.19 → 0.0.21
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/combinators.js +7 -5
- package/dist/parsers.d.ts +1 -1
- package/dist/parsers.js +3 -2
- package/dist/trace.d.ts +3 -2
- package/dist/trace.js +4 -56
- package/package.json +1 -1
package/dist/combinators.js
CHANGED
|
@@ -15,7 +15,7 @@ import { escape, findAncestorWithNextParser, popMany } from "./utils.js";
|
|
|
15
15
|
* and returns the result as an array
|
|
16
16
|
*/
|
|
17
17
|
export function many(parser) {
|
|
18
|
-
|
|
18
|
+
const _parser = (input) => {
|
|
19
19
|
let results = [];
|
|
20
20
|
let captures = [];
|
|
21
21
|
let rest = input;
|
|
@@ -44,7 +44,8 @@ export function many(parser) {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
}
|
|
47
|
+
};
|
|
48
|
+
return trace("many", _parser);
|
|
48
49
|
}
|
|
49
50
|
/**
|
|
50
51
|
* Same as `many`, but fails if the parser doesn't match at least once.
|
|
@@ -392,13 +393,14 @@ export function capture(parser, name) {
|
|
|
392
393
|
* @returns - the parser's result set as the captures object
|
|
393
394
|
*/
|
|
394
395
|
export function captureCaptures(parser) {
|
|
395
|
-
|
|
396
|
+
const _parser = (input) => {
|
|
396
397
|
let result = parser(input);
|
|
397
398
|
if (result.success) {
|
|
398
399
|
return Object.assign(Object.assign({}, result), { captures: result.result });
|
|
399
400
|
}
|
|
400
401
|
return result;
|
|
401
|
-
}
|
|
402
|
+
};
|
|
403
|
+
return trace(`captureCaptures()`, _parser);
|
|
402
404
|
}
|
|
403
405
|
/**
|
|
404
406
|
* Returns a parser that consumes input till the given parser succeeds.
|
|
@@ -545,7 +547,7 @@ export function search(parser) {
|
|
|
545
547
|
.join(" ");
|
|
546
548
|
return success(result, rest);
|
|
547
549
|
}
|
|
548
|
-
return success(
|
|
550
|
+
return success([], input);
|
|
549
551
|
});
|
|
550
552
|
}
|
|
551
553
|
/*
|
package/dist/parsers.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export declare function noneOf(chars: string): Parser<string>;
|
|
|
43
43
|
* @param input - input string
|
|
44
44
|
* @returns - ParserResult
|
|
45
45
|
*/
|
|
46
|
-
export declare const anyChar:
|
|
46
|
+
export declare const anyChar: Parser<string>;
|
|
47
47
|
/** A parser that matches one of " \t\n\r". */
|
|
48
48
|
export declare const space: Parser<string>;
|
|
49
49
|
/** A parser that matches one or more spaces. */
|
package/dist/parsers.js
CHANGED
|
@@ -175,7 +175,7 @@ export function captureRegex(str, options = "", ...captureNames) {
|
|
|
175
175
|
else {
|
|
176
176
|
re = str;
|
|
177
177
|
}
|
|
178
|
-
|
|
178
|
+
const _parser = (input) => {
|
|
179
179
|
const match = input.match(re);
|
|
180
180
|
if (match) {
|
|
181
181
|
if (match.slice(1).length > captureNames.length) {
|
|
@@ -188,7 +188,8 @@ export function captureRegex(str, options = "", ...captureNames) {
|
|
|
188
188
|
return success(captures, input.slice(match[0].length));
|
|
189
189
|
}
|
|
190
190
|
return failure(`expected ${str}, got ${input.slice(0, 10)}`, input);
|
|
191
|
-
}
|
|
191
|
+
};
|
|
192
|
+
return trace(`captureRegex(${str})`, _parser);
|
|
192
193
|
}
|
|
193
194
|
/**
|
|
194
195
|
* Return a parser that takes a key and a value.
|
package/dist/trace.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ParserResult } from "./types.js";
|
|
1
|
+
import { ParserResult, Parser, PlainObject, CaptureParser } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* This function is used internally by the `trace` function to create the string for each step.
|
|
4
4
|
* @param name - debug name for parser
|
|
@@ -61,7 +61,8 @@ export declare function resultToString<T>(name: string, result: ParserResult<T>)
|
|
|
61
61
|
* @param parser - parser to run
|
|
62
62
|
* @returns
|
|
63
63
|
*/
|
|
64
|
-
export declare function trace(name: string, parser:
|
|
64
|
+
export declare function trace<T, C extends PlainObject>(name: string, parser: CaptureParser<T, C>): CaptureParser<T, C>;
|
|
65
|
+
export declare function trace<T>(name: string, parser: Parser<T>): Parser<T>;
|
|
65
66
|
/**
|
|
66
67
|
* Utility timing function. Given a callback, it times the callback
|
|
67
68
|
* and returns its runtime in milliseconds. It uses `performance.now()` to do this.
|
package/dist/trace.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { escape, round, shorten } from "./utils.js";
|
|
2
2
|
import process from "process";
|
|
3
|
+
const isNode = typeof process !== "undefined" &&
|
|
4
|
+
process.versions != null &&
|
|
5
|
+
process.versions.node != null;
|
|
3
6
|
const STEP = 2;
|
|
4
7
|
/**
|
|
5
8
|
* This function is used internally by the `trace` function to create the string for each step.
|
|
@@ -16,64 +19,9 @@ export function resultToString(name, result) {
|
|
|
16
19
|
let level = 0;
|
|
17
20
|
let counts = {};
|
|
18
21
|
let times = {};
|
|
19
|
-
let debugFlag = !!process.env.DEBUG;
|
|
22
|
+
let debugFlag = isNode ? !!process.env.DEBUG : false;
|
|
20
23
|
let stepCount = 0;
|
|
21
24
|
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
25
|
export function trace(name, parser) {
|
|
78
26
|
if (stepLimit > 0 && stepCount > stepLimit) {
|
|
79
27
|
throw new Error(`parser step limit of ${stepLimit} exceeded, parser may be in an infinite loop`);
|