tarsec 0.0.13 → 0.0.15
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 +10 -2
- package/dist/combinators/seq.js +1 -12
- package/dist/combinators.d.ts +116 -5
- package/dist/combinators.js +593 -495
- 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 +85 -7
- package/dist/parsers.js +239 -142
- package/dist/trace.d.ts +94 -1
- package/dist/trace.js +177 -94
- package/dist/types.d.ts +1 -1
- package/dist/types.js +40 -59
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +53 -66
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -40,8 +40,16 @@ parser("hello there"); // failure
|
|
|
40
40
|
|
|
41
41
|
## Features
|
|
42
42
|
- tarsec is entirely TypeScript. There's nothing to compile.
|
|
43
|
-
- [Debug mode](/tutorials/debugging.md) that prints what's happening step-by-step
|
|
44
43
|
- Derived types: tarsec will generate TypeScript types for your parser
|
|
44
|
+
- [Debug mode](/tutorials/debugging.md) that prints what's happening step-by-step
|
|
45
|
+
- Tools to debug your parser's [performance](/tutorials/performance.md)
|
|
45
46
|
- Partial [backtracking](/tutorials/backtracking.md) support
|
|
47
|
+
- A way to make your parser more [secure](/tutorials/security.md).
|
|
48
|
+
|
|
49
|
+
## Examples
|
|
50
|
+
- [A markdown parser](/tests/examples/markdown.ts)
|
|
51
|
+
|
|
52
|
+
Read more about [use cases for tarsec](/tutorials/use-case.md).
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
## Contributing
|
|
55
|
+
PRs for documentation, tests, and bug fixes are welcome.
|
package/dist/combinators/seq.js
CHANGED
|
@@ -1,12 +1 @@
|
|
|
1
|
-
|
|
2
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
-
var v = factory(require, exports);
|
|
4
|
-
if (v !== undefined) module.exports = v;
|
|
5
|
-
}
|
|
6
|
-
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports"], factory);
|
|
8
|
-
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
});
|
|
1
|
+
export {};
|
package/dist/combinators.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CaptureParser, GeneralParser, InferManyReturnType, MergedCaptures, MergedResults, Parser, PickParserType, PlainObject } from "./types";
|
|
1
|
+
import { CaptureParser, GeneralParser, InferManyReturnType, MergedCaptures, MergedResults, Parser, PickParserType, PlainObject } from "./types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Takes a parser and runs it zero or more times, returning the results as an array.
|
|
4
4
|
* If the parser is a capture parser, it returns the captures as an array in this form:
|
|
@@ -7,7 +7,6 @@ import { CaptureParser, GeneralParser, InferManyReturnType, MergedCaptures, Merg
|
|
|
7
7
|
* { captures: <array of captures> }
|
|
8
8
|
* ```
|
|
9
9
|
*
|
|
10
|
-
* Fails on empty strings
|
|
11
10
|
* @param parser - parser to run
|
|
12
11
|
* @returns - parser that runs the given parser zero to many times,
|
|
13
12
|
* and returns the result as an array
|
|
@@ -20,6 +19,12 @@ export declare function many<const T extends GeneralParser<any, any>>(parser: T)
|
|
|
20
19
|
* @returns a parser that runs the given parser one to many times,
|
|
21
20
|
*/
|
|
22
21
|
export declare function many1<const T extends GeneralParser<any, any>>(parser: T): InferManyReturnType<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Takes a parser, runs it, and returns the number of times it succeeded.
|
|
24
|
+
* @param parser - parser to run
|
|
25
|
+
* @returns - the number of times the parser succeeded.
|
|
26
|
+
*/
|
|
27
|
+
export declare function count<T>(parser: Parser<T>): Parser<number>;
|
|
23
28
|
/**
|
|
24
29
|
* Takes a parser, runs it n times, and returns the results as an array.
|
|
25
30
|
* If it cannot run the parser n times, it fails without consuming input.
|
|
@@ -27,7 +32,7 @@ export declare function many1<const T extends GeneralParser<any, any>>(parser: T
|
|
|
27
32
|
* @param parser - parser to run
|
|
28
33
|
* @returns - parser that runs the given parser `num` times and returns an array of the results
|
|
29
34
|
*/
|
|
30
|
-
export declare function
|
|
35
|
+
export declare function exactly<T>(num: number, parser: Parser<T>): Parser<T[]>;
|
|
31
36
|
/**
|
|
32
37
|
* Same as `many`, but joins the results into a single string.
|
|
33
38
|
*
|
|
@@ -103,9 +108,37 @@ export declare function not(parser: Parser<any>): Parser<null>;
|
|
|
103
108
|
* @returns a parser that returns the result of `parser`.
|
|
104
109
|
*/
|
|
105
110
|
export declare function between<O, C, P>(open: Parser<O>, close: Parser<C>, parser: Parser<P>): Parser<P>;
|
|
111
|
+
/**
|
|
112
|
+
* Parses many instances of the parser separated by separator.
|
|
113
|
+
* @param separator
|
|
114
|
+
* @param parser
|
|
115
|
+
* @returns a parser that runs the given parser zero to many times, separated by the separator parser.
|
|
116
|
+
*/
|
|
106
117
|
export declare function sepBy<S, P>(separator: Parser<S>, parser: Parser<P>): Parser<P[]>;
|
|
118
|
+
/**
|
|
119
|
+
* Convenience function to use as the second argument to `seq` to get all the results from `seq`
|
|
120
|
+
* @param results
|
|
121
|
+
* @param captures
|
|
122
|
+
* @returns `results`
|
|
123
|
+
*/
|
|
107
124
|
export declare function getResults<R, C>(results: R, captures: C): R;
|
|
125
|
+
/**
|
|
126
|
+
* Convenience function to use as the second argument to seq to get all the captures.
|
|
127
|
+
* @param results
|
|
128
|
+
* @param captures
|
|
129
|
+
* @returns `captures`
|
|
130
|
+
*/
|
|
108
131
|
export declare function getCaptures<R, C>(results: R, captures: C): C;
|
|
132
|
+
/**
|
|
133
|
+
* `capture` is the only way to create a capture. Given a parser and a name,
|
|
134
|
+
* `capture` runs the parser and saves its result in a captures object
|
|
135
|
+
* with the given name as the key. It returns the result from the parser,
|
|
136
|
+
* and attaches the captures object along with it.
|
|
137
|
+
*
|
|
138
|
+
* @param parser - parser to run
|
|
139
|
+
* @param name - name of the capture
|
|
140
|
+
* @returns - the results of the parser, with the captures object attached.
|
|
141
|
+
*/
|
|
109
142
|
export declare function capture<T, const S extends string>(parser: Parser<T>, name: S): CaptureParser<T, Record<S, T>>;
|
|
110
143
|
/**
|
|
111
144
|
* Returns a parser that consumes input till the given parser succeeds.
|
|
@@ -115,9 +148,73 @@ export declare function capture<T, const S extends string>(parser: Parser<T>, na
|
|
|
115
148
|
* The stop parser's match is not included in the result.
|
|
116
149
|
*/
|
|
117
150
|
export declare function manyTill<T>(parser: Parser<T>): Parser<string>;
|
|
151
|
+
/**
|
|
152
|
+
* Just like `manyTill`, but fails unless at least one character of input is consumed.
|
|
153
|
+
* @param parser - the stop parser
|
|
154
|
+
* @returns a parser that consumes the input string until the stop parser succeeds.
|
|
155
|
+
*/
|
|
118
156
|
export declare function many1Till<T>(parser: Parser<T>): Parser<string>;
|
|
119
|
-
|
|
120
|
-
|
|
157
|
+
/**
|
|
158
|
+
* `manyTillOneOf` is an optimized version of `manyTill`.
|
|
159
|
+
* The `manyTill` combinator is slow because it runs the given parser
|
|
160
|
+
* on every character of the string until it succeeds. However, if you
|
|
161
|
+
* just want to consume input until you get to a substring,
|
|
162
|
+
* use `manyTillOneOf`. It uses `indexOf`, which is significantly faster
|
|
163
|
+
* than running a parser over every character.
|
|
164
|
+
*
|
|
165
|
+
* Given an array of strings, this parser consumes input until it hits one of those strings.
|
|
166
|
+
* If none of the strings is found, the parser will consume all input and return success.
|
|
167
|
+
*
|
|
168
|
+
* @param str - the string to stop at
|
|
169
|
+
* @param options - object of optional parameters. { insensitive: boolean }
|
|
170
|
+
* @returns a parser that consumes the input string until one of the given strings is found.
|
|
171
|
+
*/
|
|
172
|
+
export declare function manyTillOneOf(stops: string[], { insensitive }?: {
|
|
173
|
+
insensitive?: boolean;
|
|
174
|
+
}): Parser<string>;
|
|
175
|
+
/**
|
|
176
|
+
* `manyTillStr` is an optimized version of `manyTill`.
|
|
177
|
+
* The `manyTill` combinator is slow because it runs the given parser
|
|
178
|
+
* on every character of the string until it succeeds. However, if you
|
|
179
|
+
* just want to consume input until you get to a substring,
|
|
180
|
+
* use `manyTillStr`. It uses `indexOf`, which is significantly faster
|
|
181
|
+
* than running a parser over every character.
|
|
182
|
+
*
|
|
183
|
+
* @param str - the string to stop at
|
|
184
|
+
* @param options - object of optional parameters. { insensitive: boolean }
|
|
185
|
+
* @returns a parser that consumes the input string until the given string is found.
|
|
186
|
+
*/
|
|
187
|
+
export declare function manyTillStr(str: string, { insensitive }?: {
|
|
188
|
+
insensitive?: boolean;
|
|
189
|
+
}): Parser<string>;
|
|
190
|
+
/**
|
|
191
|
+
* Like `manyTillStr`, but case insensitive.
|
|
192
|
+
* @param str - the string to stop at
|
|
193
|
+
* @returns a parser that consumes the input string until the given string is found.
|
|
194
|
+
*/
|
|
195
|
+
export declare function iManyTillStr(str: string): Parser<string>;
|
|
196
|
+
/**
|
|
197
|
+
* `map` is a parser combinator that takes a parser and a mapper function.
|
|
198
|
+
* If the parser succeeds, it maps its result using the mapper function.
|
|
199
|
+
* You can think of map as a general `map`, like for functors, applied to a parser.
|
|
200
|
+
* Since `map` itself is a parser, you can use it in `seq` or other combinators.
|
|
201
|
+
*
|
|
202
|
+
* @param parser - parser to run
|
|
203
|
+
* @param mapperFunc - function to map the result of the parser
|
|
204
|
+
* @returns
|
|
205
|
+
*/
|
|
206
|
+
export declare function map<R, C extends PlainObject, X>(parser: GeneralParser<R, C>, mapperFunc: (x: R) => X): GeneralParser<X, C>;
|
|
207
|
+
/**
|
|
208
|
+
* Given a parser that returns a string, `search` looks for all substrings in a string that match that parser.
|
|
209
|
+
* For example, given a parser that matches quoted strings, `search` will return an array of all the quoted strings
|
|
210
|
+
* it finds in the input, as an array.
|
|
211
|
+
*
|
|
212
|
+
* The rest of the input that isn't part of the result is simply joined together and returned as a string.
|
|
213
|
+
* If you need a more structured result + rest, you can use `within` instead.
|
|
214
|
+
*
|
|
215
|
+
* @param parser - a parser that returns a string
|
|
216
|
+
* @returns - a parser that returns an array of strings
|
|
217
|
+
*/
|
|
121
218
|
export declare function search(parser: Parser<string>): Parser<string[]>;
|
|
122
219
|
/**
|
|
123
220
|
* seq takes an array of parsers and runs them sequentially.
|
|
@@ -146,6 +243,20 @@ export declare function search(parser: Parser<string>): Parser<string[]>;
|
|
|
146
243
|
* @returns
|
|
147
244
|
*/
|
|
148
245
|
export declare function seq<const T extends readonly GeneralParser<any, any>[], U>(parsers: T, transform: (results: MergedResults<T>[], captures: MergedCaptures<T>) => U, debugName?: string): Parser<U>;
|
|
246
|
+
/** Just like seq except it returns the results.
|
|
247
|
+
* It's like using `seq([parsers], getResults)`.
|
|
248
|
+
*/
|
|
149
249
|
export declare function seqR<const T extends readonly GeneralParser<any, any>[]>(...parsers: T): Parser<MergedResults<T>[]>;
|
|
250
|
+
/** Just like seq except it returns the captures.
|
|
251
|
+
* It's like using `seq([parsers], getCaptures)`.
|
|
252
|
+
*/
|
|
150
253
|
export declare function seqC<const T extends readonly GeneralParser<any, any>[]>(...parsers: T): Parser<MergedCaptures<T>>;
|
|
254
|
+
/**
|
|
255
|
+
* Match takes an input string and a parser. If the parser matches the input string
|
|
256
|
+
* and consumes the entire input string, `match` returns `true`. Otherwise it returns `false`.
|
|
257
|
+
*
|
|
258
|
+
* @param input - input string
|
|
259
|
+
* @param parser - parser to match input against
|
|
260
|
+
* @returns - true if the parser matches the input and consumes all input, false otherwise
|
|
261
|
+
*/
|
|
151
262
|
export declare function match(input: string, parser: GeneralParser<any, any>): boolean;
|