tarsec 0.0.14 → 0.0.16
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/README.md +5 -1
- package/dist/combinators/seq.js +1 -12
- package/dist/combinators.d.ts +39 -5
- package/dist/combinators.js +640 -600
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -30
- package/dist/parsers/within.d.ts +1 -1
- package/dist/parsers/within.js +31 -45
- package/dist/parsers.d.ts +132 -2
- package/dist/parsers.js +329 -168
- package/dist/trace.d.ts +8 -1
- package/dist/trace.js +177 -180
- package/dist/types.d.ts +43 -15
- package/dist/types.js +57 -59
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +53 -66
- package/package.json +2 -1
package/dist/parsers.js
CHANGED
|
@@ -1,180 +1,341 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { many1WithJoin, manyWithJoin, seq } from "./combinators.js";
|
|
2
|
+
import { trace } from "./trace.js";
|
|
3
|
+
import { captureSuccess, failure, success, } from "./types.js";
|
|
4
|
+
import { escape } from "./utils.js";
|
|
5
|
+
export { within as betweenWithin } from "./parsers/within.js";
|
|
6
|
+
/**
|
|
7
|
+
* Takes a character. Returns a parser that parses that character.
|
|
8
|
+
*
|
|
9
|
+
* @param c - character to parse
|
|
10
|
+
* @returns - parser that parses the given character
|
|
11
|
+
*/
|
|
12
|
+
export function char(c) {
|
|
13
|
+
return trace(`char(${escape(c)})`, (input) => {
|
|
14
|
+
if (input.length === 0) {
|
|
15
|
+
return {
|
|
16
|
+
success: false,
|
|
17
|
+
rest: input,
|
|
18
|
+
message: "unexpected end of input",
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (input[0] === c) {
|
|
22
|
+
return success(c, input.slice(1));
|
|
23
|
+
}
|
|
24
|
+
return failure(`expected ${escape(c)}, got ${escape(input[0])}`, input);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Takes a string. Returns a parser that parses that string.
|
|
29
|
+
*
|
|
30
|
+
* @param s - string to match on
|
|
31
|
+
* @returns - parser that parses the given string
|
|
32
|
+
*/
|
|
33
|
+
export function str(s) {
|
|
34
|
+
return trace(`str(${escape(s)})`, (input) => {
|
|
35
|
+
if (input.substring(0, s.length) === s) {
|
|
36
|
+
return success(s, input.slice(s.length));
|
|
37
|
+
}
|
|
38
|
+
return failure(`expected ${s}, got ${input.substring(0, s.length)}`, input);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Like `str`, but case insensitive.
|
|
43
|
+
* @param s - string to match on, case insensitive
|
|
44
|
+
* @returns - parser that matches the given string, case insensitive
|
|
45
|
+
*/
|
|
46
|
+
export function istr(s) {
|
|
47
|
+
return trace(`istr(${escape(s)})`, (input) => {
|
|
48
|
+
if (input.substring(0, s.length).toLocaleLowerCase() === s.toLocaleLowerCase()) {
|
|
49
|
+
return success(input.substring(0, s.length), input.slice(s.length));
|
|
50
|
+
}
|
|
51
|
+
return failure(`expected ${s}, got ${input.substring(0, s.length)}`, input);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Takes a string. Returns a parser that parses
|
|
56
|
+
* one of the characters in that string.
|
|
57
|
+
*
|
|
58
|
+
* @param chars - string of possible characters
|
|
59
|
+
* @returns - parser that parses one of the given characters
|
|
60
|
+
*/
|
|
61
|
+
export function oneOf(chars) {
|
|
62
|
+
return trace(`oneOf(${escape(chars)})`, (input) => {
|
|
63
|
+
if (input.length === 0) {
|
|
64
|
+
return failure("unexpected end of input", input);
|
|
65
|
+
}
|
|
66
|
+
const c = input[0];
|
|
67
|
+
if (chars.includes(c)) {
|
|
68
|
+
return char(c)(input);
|
|
69
|
+
}
|
|
70
|
+
return failure(`expected one of ${escape(chars)}, got ${c}`, input);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Takes a string. Returns a parser that parses one character
|
|
75
|
+
* that's not any of the characters in the given string
|
|
76
|
+
*
|
|
77
|
+
* @param chars - string of characters to avoid
|
|
78
|
+
* @returns - parser that parses a character that is not in the given string
|
|
79
|
+
*/
|
|
80
|
+
export function noneOf(chars) {
|
|
81
|
+
return trace(`noneOf(${escape(chars)})`, (input) => {
|
|
82
|
+
if (input.length === 0) {
|
|
83
|
+
return failure("unexpected end of input", input);
|
|
84
|
+
}
|
|
85
|
+
if (chars.includes(input[0])) {
|
|
86
|
+
return failure(`expected none of ${escape(chars)}, got ${input[0]}`, input);
|
|
87
|
+
}
|
|
88
|
+
return char(input[0])(input);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A parser that parses any one character.
|
|
93
|
+
* Fails on empty strings, succeeds otherwise.
|
|
94
|
+
*
|
|
95
|
+
* @param input - input string
|
|
96
|
+
* @returns - ParserResult
|
|
97
|
+
*/
|
|
98
|
+
export const anyChar = trace("anyChar", (input) => {
|
|
99
|
+
if (input.length === 0) {
|
|
100
|
+
return failure("unexpected end of input", input);
|
|
5
101
|
}
|
|
6
|
-
|
|
7
|
-
|
|
102
|
+
return success(input[0], input.slice(1));
|
|
103
|
+
});
|
|
104
|
+
/** A parser that matches one of " \t\n\r". */
|
|
105
|
+
export const space = oneOf(" \t\n\r");
|
|
106
|
+
/** A parser that matches one or more spaces. */
|
|
107
|
+
export const spaces = many1WithJoin(space);
|
|
108
|
+
/** A parser that matches one digit. */
|
|
109
|
+
export const digit = oneOf("0123456789");
|
|
110
|
+
/** A parser that matches one letter, case insensitive. */
|
|
111
|
+
export const letter = oneOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
|
112
|
+
/** A parser that matches one digit or letter, case insensitive. */
|
|
113
|
+
export const alphanum = oneOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
|
|
114
|
+
/** A parser that matches one word, case insensitive. */
|
|
115
|
+
export const word = regexParser("^[a-z]+", "ui");
|
|
116
|
+
/** A parser that matches one or more digits. */
|
|
117
|
+
export const num = regexParser("^[0-9]+");
|
|
118
|
+
/** A parser that matches one single or double quote. */
|
|
119
|
+
export const quote = oneOf(`'"`);
|
|
120
|
+
/** A parser that matches one tab character. */
|
|
121
|
+
export const tab = char("\t");
|
|
122
|
+
/** A parser that matches one newline ("\n" only) character. */
|
|
123
|
+
export const newline = char("\n");
|
|
124
|
+
/** A parser that succeeds on an empty string. Returns `null` as the result. */
|
|
125
|
+
export const eof = (input) => {
|
|
126
|
+
if (input === "") {
|
|
127
|
+
return success(null, input);
|
|
8
128
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
success: false,
|
|
30
|
-
rest: input,
|
|
31
|
-
message: "unexpected end of input",
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
if (input[0] === c) {
|
|
35
|
-
return (0, types_1.success)(c, input.slice(1));
|
|
36
|
-
}
|
|
37
|
-
return (0, types_1.failure)(`expected ${(0, utils_1.escape)(c)}, got ${(0, utils_1.escape)(input[0])}`, input);
|
|
38
|
-
});
|
|
129
|
+
return failure("expected end of input", input);
|
|
130
|
+
};
|
|
131
|
+
/** A parser that matches a quoted string, in single or double quotes.
|
|
132
|
+
* Returns the string as the result, including the quotes.
|
|
133
|
+
*/
|
|
134
|
+
export const quotedString = seq([quote, manyWithJoin(noneOf(`"'`)), quote], (results) => results.join(""));
|
|
135
|
+
/**
|
|
136
|
+
* Returns a parser that matches a regex. If you pass in a string,
|
|
137
|
+
* it will get converted to a regex. The regex should always match from the start of the input.
|
|
138
|
+
* If you pass in a string, a `^` will get prepended to it.
|
|
139
|
+
*
|
|
140
|
+
* @param str - regex string or RegExp instance to match
|
|
141
|
+
* @param options - regex options (i = ignore case, g = global, m = multiline, u = unicode)
|
|
142
|
+
* @returns - parser that matches the given regex
|
|
143
|
+
*/
|
|
144
|
+
export function regexParser(str, options = "") {
|
|
145
|
+
let re;
|
|
146
|
+
if (typeof str === "string") {
|
|
147
|
+
re = new RegExp(str.startsWith("^") ? str : `^${str}`, options);
|
|
39
148
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* Takes a string. Returns a parser that parses that string.
|
|
43
|
-
*
|
|
44
|
-
* @param s - string to match on
|
|
45
|
-
* @returns - parser that parses the given string
|
|
46
|
-
*/
|
|
47
|
-
function str(s) {
|
|
48
|
-
return (0, trace_1.trace)(`str(${(0, utils_1.escape)(s)})`, (input) => {
|
|
49
|
-
if (input.substring(0, s.length) === s) {
|
|
50
|
-
return (0, types_1.success)(s, input.slice(s.length));
|
|
51
|
-
}
|
|
52
|
-
return (0, types_1.failure)(`expected ${s}, got ${input.substring(0, s.length)}`, input);
|
|
53
|
-
});
|
|
149
|
+
else {
|
|
150
|
+
re = str;
|
|
54
151
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
152
|
+
return trace(`regex(${str})`, (input) => {
|
|
153
|
+
const match = input.match(re);
|
|
154
|
+
if (match) {
|
|
155
|
+
return success(match[0], input.slice(match[0].length));
|
|
156
|
+
}
|
|
157
|
+
return failure(`expected ${str}, got ${input.slice(0, 10)}`, input);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Like `regexParser`, but you can name your capture groups
|
|
162
|
+
* and get them back as the result instead.
|
|
163
|
+
* Fails if it doesn't have the same number of names as capture groups.
|
|
164
|
+
*
|
|
165
|
+
* @param str - regex string or RegExp instance to match
|
|
166
|
+
* @param options - string of regex options (i = ignore case, g = global, m = multiline, u = unicode)
|
|
167
|
+
* @param captureNames - names of the captures
|
|
168
|
+
* @returns - parser that matches the given regex
|
|
169
|
+
*/
|
|
170
|
+
export function captureRegex(str, options = "", ...captureNames) {
|
|
171
|
+
let re;
|
|
172
|
+
if (typeof str === "string") {
|
|
173
|
+
re = new RegExp(str.startsWith("^") ? str : `^${str}`, options);
|
|
68
174
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
* Takes a string. Returns a parser that parses
|
|
72
|
-
* one of the characters in that string.
|
|
73
|
-
*
|
|
74
|
-
* @param chars - string of possible characters
|
|
75
|
-
* @returns - parser that parses one of the given characters
|
|
76
|
-
*/
|
|
77
|
-
function oneOf(chars) {
|
|
78
|
-
return (0, trace_1.trace)(`oneOf(${(0, utils_1.escape)(chars)})`, (input) => {
|
|
79
|
-
if (input.length === 0) {
|
|
80
|
-
return (0, types_1.failure)("unexpected end of input", input);
|
|
81
|
-
}
|
|
82
|
-
const c = input[0];
|
|
83
|
-
if (chars.includes(c)) {
|
|
84
|
-
return char(c)(input);
|
|
85
|
-
}
|
|
86
|
-
return (0, types_1.failure)(`expected one of ${(0, utils_1.escape)(chars)}, got ${c}`, input);
|
|
87
|
-
});
|
|
175
|
+
else {
|
|
176
|
+
re = str;
|
|
88
177
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
* @param chars - string of characters to avoid
|
|
95
|
-
* @returns - parser that parses a character that is not in the given string
|
|
96
|
-
*/
|
|
97
|
-
function noneOf(chars) {
|
|
98
|
-
return (0, trace_1.trace)(`noneOf(${(0, utils_1.escape)(chars)})`, (input) => {
|
|
99
|
-
if (input.length === 0) {
|
|
100
|
-
return (0, types_1.failure)("unexpected end of input", input);
|
|
178
|
+
return trace(`captureRegex(${str})`, (input) => {
|
|
179
|
+
const match = input.match(re);
|
|
180
|
+
if (match) {
|
|
181
|
+
if (match.slice(1).length > captureNames.length) {
|
|
182
|
+
return failure(`more capture groups than names. ${match.slice(1).length} capture groups, ${captureNames.length} names`, input);
|
|
101
183
|
}
|
|
102
|
-
if (
|
|
103
|
-
return
|
|
184
|
+
if (match.slice(1).length < captureNames.length) {
|
|
185
|
+
return failure(`fewer capture groups than names. ${match.slice(1).length} capture groups, ${captureNames.length} names`, input);
|
|
104
186
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
exports.noneOf = noneOf;
|
|
109
|
-
/**
|
|
110
|
-
* A parser that parses any one character.
|
|
111
|
-
* Fails on empty strings, succeeds otherwise.
|
|
112
|
-
*
|
|
113
|
-
* @param input - input string
|
|
114
|
-
* @returns - ParserResult
|
|
115
|
-
*/
|
|
116
|
-
exports.anyChar = (0, trace_1.trace)("anyChar", (input) => {
|
|
117
|
-
if (input.length === 0) {
|
|
118
|
-
return (0, types_1.failure)("unexpected end of input", input);
|
|
187
|
+
const captures = Object.assign({}, Object.fromEntries(match.slice(1).map((value, index) => [captureNames[index], value])));
|
|
188
|
+
return success(captures, input.slice(match[0].length));
|
|
119
189
|
}
|
|
120
|
-
return (
|
|
190
|
+
return failure(`expected ${str}, got ${input.slice(0, 10)}`, input);
|
|
121
191
|
});
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Return a parser that takes a key and a value.
|
|
195
|
+
* The parser consumes no input and always succeeds,
|
|
196
|
+
* and returns `null` as the result. It also returns a captures object
|
|
197
|
+
* with that key-value pair set. This is useful when you need to inject
|
|
198
|
+
* a key-value pair into captures for a `seq`.
|
|
199
|
+
*
|
|
200
|
+
* For example, here is a Markdown heading parser.
|
|
201
|
+
|
|
202
|
+
* ```ts
|
|
203
|
+
* export const headingParser: Parser<Heading> = seqC(
|
|
204
|
+
* capture(count(char("#")), "level"),
|
|
205
|
+
* spaces,
|
|
206
|
+
* capture(many1Till(or(char("\n"), eof)), "content")
|
|
207
|
+
* );
|
|
208
|
+
```
|
|
209
|
+
*
|
|
210
|
+
* This parser returns
|
|
211
|
+
*
|
|
212
|
+
* ```ts
|
|
213
|
+
* {
|
|
214
|
+
* level: number,
|
|
215
|
+
* content: string
|
|
216
|
+
* }
|
|
217
|
+
* ```
|
|
218
|
+
* but the type of heading is actually
|
|
219
|
+
*
|
|
220
|
+
* ```ts
|
|
221
|
+
* type Heading = {
|
|
222
|
+
* type: "heading";
|
|
223
|
+
* level: number;
|
|
224
|
+
* content: string;
|
|
225
|
+
* };
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* The `type` key is missing. You can use `set` to inject the `type`
|
|
229
|
+
* key-value pair into captures:
|
|
230
|
+
*
|
|
231
|
+
* ```ts
|
|
232
|
+
* export const headingParser: Parser<Heading> = seqC(
|
|
233
|
+
* set("type", "heading"),
|
|
234
|
+
* capture(count(char("#")), "level"),
|
|
235
|
+
* spaces,
|
|
236
|
+
* capture(many1Till(or(char("\n"), eof)), "content")
|
|
237
|
+
* );
|
|
238
|
+
* ```
|
|
239
|
+
*
|
|
240
|
+
* @param key - key to set on captures object
|
|
241
|
+
* @param value - value to set on captures object
|
|
242
|
+
* @returns
|
|
243
|
+
*/
|
|
244
|
+
export function set(key, value) {
|
|
245
|
+
return trace(`set(${key}, ${value})`, (input) => {
|
|
246
|
+
return captureSuccess(null, input, { [key]: value });
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* A parser that always succeeds with the given value.
|
|
251
|
+
* @param value - value to succeed with
|
|
252
|
+
* @returns value
|
|
253
|
+
*/
|
|
254
|
+
export function succeed(value) {
|
|
255
|
+
return trace(`succeed(${value})`, (input) => {
|
|
256
|
+
return success(value, input);
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* A parser that always fails with the given message.
|
|
261
|
+
* @param message - message to fail with
|
|
262
|
+
* @returns failure
|
|
263
|
+
*/
|
|
264
|
+
export function fail(message) {
|
|
265
|
+
return trace(`fail(${message})`, (input) => {
|
|
266
|
+
return failure(message, input);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Takes a string. Succeeds if the given input contains that string.
|
|
271
|
+
* Consumes no input.
|
|
272
|
+
*
|
|
273
|
+
* @param substr - substring to find
|
|
274
|
+
* @returns - parser that succeeds if the given input contains that string
|
|
275
|
+
*/
|
|
276
|
+
export function includes(substr) {
|
|
277
|
+
return trace(`includes(${substr})`, (input) => {
|
|
278
|
+
if (input.includes(substr)) {
|
|
279
|
+
return success(substr, input);
|
|
167
280
|
}
|
|
168
|
-
|
|
169
|
-
|
|
281
|
+
return failure(`expected ${escape(input)} to include ${escape(substr)}`, input);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Like `includes`, but case-insensitive.
|
|
286
|
+
*
|
|
287
|
+
* @param substr - substring to find
|
|
288
|
+
* @returns - parser that succeeds if the given input contains that string
|
|
289
|
+
*/
|
|
290
|
+
export function iIncludes(substr) {
|
|
291
|
+
return trace(`iIncludes(${substr})`, (input) => {
|
|
292
|
+
if (input.toLowerCase().includes(substr.toLowerCase())) {
|
|
293
|
+
return success(substr, input);
|
|
170
294
|
}
|
|
171
|
-
return (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
295
|
+
return failure(`expected "${input}" to include "${substr}" (case-insensitive)`, input);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Returns a parser that takes some input, runs the transformer function over it,
|
|
300
|
+
* and returns the result as `rest`, so it can be chained to another parser.
|
|
301
|
+
* It always returns null as its result. Always succeeds.
|
|
302
|
+
*
|
|
303
|
+
* `shape` is useful for modifying the user's input before running parsers over it.
|
|
304
|
+
* For example, here is a parser that takes in a chapter
|
|
305
|
+
* and checks that its title starts with "Once upon a time"
|
|
306
|
+
*
|
|
307
|
+
* ```ts
|
|
308
|
+
* const parser = seqR(
|
|
309
|
+
* shape((c: Chapter) => c.title),
|
|
310
|
+
* istr("Once upon a time"),
|
|
311
|
+
* )
|
|
312
|
+
* );
|
|
313
|
+
* ```
|
|
314
|
+
*
|
|
315
|
+
* Now you might be thinking, why not just use the chapter's title as input?
|
|
316
|
+
* `shape` is most useful when you want to parse multiple properties.
|
|
317
|
+
*
|
|
318
|
+
* ```ts
|
|
319
|
+
* const titleParser = seqR(
|
|
320
|
+
* shape((c: Chapter) => c.title),
|
|
321
|
+
* istr("Once upon a time"),
|
|
322
|
+
* );
|
|
323
|
+
*
|
|
324
|
+
* const textParser = seqR(
|
|
325
|
+
* shape((c: Chapter) => c.text),
|
|
326
|
+
* istr("There was a princess"),
|
|
327
|
+
* );
|
|
328
|
+
*
|
|
329
|
+
* const parser = and(titleParser, textParser);
|
|
330
|
+
* ```
|
|
331
|
+
*
|
|
332
|
+
* `parser` now takes a chapter as input and parses its title and text correctly.
|
|
333
|
+
*
|
|
334
|
+
* @param transformer - function to transform the input
|
|
335
|
+
* @returns a parser that takes some input and runs the transformer function over it
|
|
336
|
+
*/
|
|
337
|
+
export function shape(transformer) {
|
|
338
|
+
return trace(`shape()`, (_input) => {
|
|
339
|
+
return success(null, transformer(_input));
|
|
340
|
+
});
|
|
341
|
+
}
|
package/dist/trace.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ParserResult } from "./types";
|
|
1
|
+
import { ParserResult } 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
|
|
@@ -90,3 +90,10 @@ export declare function printTime(name: string, callback: Function): void;
|
|
|
90
90
|
* @param callback - callback to run in debug mode
|
|
91
91
|
*/
|
|
92
92
|
export declare function parserDebug(name: string, callback: Function): void;
|
|
93
|
+
/**
|
|
94
|
+
* Utility function to limit the number of steps a parser can take.
|
|
95
|
+
* This is useful for avoiding infinite loops in your parser.
|
|
96
|
+
* @param limit - number of steps to limit the parser to
|
|
97
|
+
* @param callback - callback to run
|
|
98
|
+
*/
|
|
99
|
+
export declare function limitSteps(limit: number, callback: Function): void;
|