tarsec 0.0.16 → 0.0.18
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.d.ts +3 -3
- package/dist/parsers.d.ts +2 -42
- package/dist/parsers.js +0 -44
- package/dist/types.d.ts +15 -28
- package/package.json +1 -1
package/dist/combinators.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CaptureParser, GeneralParser, InferManyReturnType, MergedCaptures, MergedResults, Parser, ParserResult, PickParserType,
|
|
1
|
+
import { CaptureParser, GeneralParser, InferManyReturnType, MergedCaptures, MergedResults, Parser, ParserResult, 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:
|
|
@@ -260,7 +260,7 @@ export declare function seqC<const T extends readonly GeneralParser<any, any>[]>
|
|
|
260
260
|
* @returns - true if the parser matches the input and consumes all input, false otherwise
|
|
261
261
|
*/
|
|
262
262
|
export declare function match(input: string, parser: GeneralParser<any, any>): boolean;
|
|
263
|
-
export declare function ifElse<const IF extends GeneralParser<any, any
|
|
263
|
+
export declare function ifElse<const IF extends GeneralParser<any, any>, const ELSE extends GeneralParser<any, any>, I = string>(condition: boolean, ifParser: IF, elseParser: ELSE): IF | ELSE;
|
|
264
264
|
/**
|
|
265
265
|
* Apply multiple parsers to the same input and collect all the results.
|
|
266
266
|
* Consumes no input.
|
|
@@ -275,4 +275,4 @@ export declare function manyParsers<const T extends readonly GeneralParser<any,
|
|
|
275
275
|
* @param parsers - parsers to try
|
|
276
276
|
* @returns - An array of results, or a failure.
|
|
277
277
|
*/
|
|
278
|
-
export declare function and<const
|
|
278
|
+
export declare function and<const T extends readonly GeneralParser<any, any>[]>(...parsers: T): PickParserType<T>;
|
package/dist/parsers.d.ts
CHANGED
|
@@ -148,13 +148,13 @@ export declare function set<const K extends string, const V>(key: K, value: V):
|
|
|
148
148
|
* @param value - value to succeed with
|
|
149
149
|
* @returns value
|
|
150
150
|
*/
|
|
151
|
-
export declare function succeed<T
|
|
151
|
+
export declare function succeed<T>(value: T): Parser<T>;
|
|
152
152
|
/**
|
|
153
153
|
* A parser that always fails with the given message.
|
|
154
154
|
* @param message - message to fail with
|
|
155
155
|
* @returns failure
|
|
156
156
|
*/
|
|
157
|
-
export declare function fail
|
|
157
|
+
export declare function fail(message: string): Parser<never>;
|
|
158
158
|
/**
|
|
159
159
|
* Takes a string. Succeeds if the given input contains that string.
|
|
160
160
|
* Consumes no input.
|
|
@@ -170,43 +170,3 @@ export declare function includes<const S extends string>(substr: S): Parser<S>;
|
|
|
170
170
|
* @returns - parser that succeeds if the given input contains that string
|
|
171
171
|
*/
|
|
172
172
|
export declare function iIncludes<const S extends string>(substr: S): Parser<S>;
|
|
173
|
-
/**
|
|
174
|
-
* Returns a parser that takes some input, runs the transformer function over it,
|
|
175
|
-
* and returns the result as `rest`, so it can be chained to another parser.
|
|
176
|
-
* It always returns null as its result. Always succeeds.
|
|
177
|
-
*
|
|
178
|
-
* `shape` is useful for modifying the user's input before running parsers over it.
|
|
179
|
-
* For example, here is a parser that takes in a chapter
|
|
180
|
-
* and checks that its title starts with "Once upon a time"
|
|
181
|
-
*
|
|
182
|
-
* ```ts
|
|
183
|
-
* const parser = seqR(
|
|
184
|
-
* shape((c: Chapter) => c.title),
|
|
185
|
-
* istr("Once upon a time"),
|
|
186
|
-
* )
|
|
187
|
-
* );
|
|
188
|
-
* ```
|
|
189
|
-
*
|
|
190
|
-
* Now you might be thinking, why not just use the chapter's title as input?
|
|
191
|
-
* `shape` is most useful when you want to parse multiple properties.
|
|
192
|
-
*
|
|
193
|
-
* ```ts
|
|
194
|
-
* const titleParser = seqR(
|
|
195
|
-
* shape((c: Chapter) => c.title),
|
|
196
|
-
* istr("Once upon a time"),
|
|
197
|
-
* );
|
|
198
|
-
*
|
|
199
|
-
* const textParser = seqR(
|
|
200
|
-
* shape((c: Chapter) => c.text),
|
|
201
|
-
* istr("There was a princess"),
|
|
202
|
-
* );
|
|
203
|
-
*
|
|
204
|
-
* const parser = and(titleParser, textParser);
|
|
205
|
-
* ```
|
|
206
|
-
*
|
|
207
|
-
* `parser` now takes a chapter as input and parses its title and text correctly.
|
|
208
|
-
*
|
|
209
|
-
* @param transformer - function to transform the input
|
|
210
|
-
* @returns a parser that takes some input and runs the transformer function over it
|
|
211
|
-
*/
|
|
212
|
-
export declare function shape<const X, const I>(transformer: (item: X) => I): Parser<null>;
|
package/dist/parsers.js
CHANGED
|
@@ -295,47 +295,3 @@ export function iIncludes(substr) {
|
|
|
295
295
|
return failure(`expected "${input}" to include "${substr}" (case-insensitive)`, input);
|
|
296
296
|
});
|
|
297
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/types.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/** A generic object type. */
|
|
2
2
|
export type PlainObject = Record<string, unknown>;
|
|
3
3
|
/** Represents a parse success with no captures. */
|
|
4
|
-
export type ParserSuccess<T
|
|
4
|
+
export type ParserSuccess<T> = {
|
|
5
5
|
success: true;
|
|
6
6
|
result: T;
|
|
7
|
-
rest:
|
|
7
|
+
rest: string;
|
|
8
8
|
nextParser?: Parser<any>;
|
|
9
9
|
};
|
|
10
10
|
/** Represents a parse success with captures. Notice nextParser is also a CaptureParser. */
|
|
@@ -16,25 +16,20 @@ export type CaptureParserSuccess<T, C extends PlainObject> = {
|
|
|
16
16
|
nextParser?: CaptureParser<any, any>;
|
|
17
17
|
};
|
|
18
18
|
/** Represents a parse failure. */
|
|
19
|
-
export type ParserFailure
|
|
19
|
+
export type ParserFailure = {
|
|
20
20
|
success: false;
|
|
21
|
-
rest:
|
|
21
|
+
rest: string;
|
|
22
22
|
message: string;
|
|
23
23
|
};
|
|
24
|
-
export type ParserResult<T
|
|
25
|
-
export type CaptureParserResult<T, C extends PlainObject
|
|
26
|
-
/** A parser is any function that takes
|
|
27
|
-
export type Parser<T
|
|
28
|
-
/** A
|
|
29
|
-
export type StringParser<T> = (input: string) => ParserResult<T>;
|
|
30
|
-
/** A capture parser is any function that takes an arg and returns a CaptureParserResult.
|
|
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.
|
|
31
29
|
* A CaptureParserResult is the same as a ParserResult, except it also includes captures,
|
|
32
30
|
* i.e. matches selected using `capture`. */
|
|
33
|
-
export type CaptureParser<T, C extends PlainObject
|
|
34
|
-
|
|
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>;
|
|
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>;
|
|
38
33
|
export declare function isCaptureResult<T, C extends PlainObject>(result: ParserResult<T>): result is CaptureParserSuccess<T, C>;
|
|
39
34
|
/**
|
|
40
35
|
* This typed function is helpful in filtering out the successes
|
|
@@ -50,13 +45,13 @@ export declare function isCaptureResult<T, C extends PlainObject>(result: Parser
|
|
|
50
45
|
* @param result - a parser result
|
|
51
46
|
* @returns - true if the result is a success, otherwise false
|
|
52
47
|
*/
|
|
53
|
-
export declare function isSuccess<T
|
|
48
|
+
export declare function isSuccess<T>(result: ParserResult<T>): result is ParserSuccess<T>;
|
|
54
49
|
/** Convenience function to return a ParserSuccess */
|
|
55
|
-
export declare function success<T
|
|
50
|
+
export declare function success<T>(result: T, rest: string): ParserSuccess<T>;
|
|
56
51
|
/** Convenience function to return a CaptureParserSuccess */
|
|
57
52
|
export declare function captureSuccess<T, C extends PlainObject>(result: T, rest: string, captures: C): CaptureParserSuccess<T, C>;
|
|
58
53
|
/** Convenience function to return a ParserFailure */
|
|
59
|
-
export declare function failure
|
|
54
|
+
export declare function failure(message: string, rest: string): ParserFailure;
|
|
60
55
|
/** Prettify an intersected type, to make it easier to read. */
|
|
61
56
|
export type Prettify<T> = {
|
|
62
57
|
[K in keyof T]: T[K];
|
|
@@ -91,15 +86,7 @@ export type HasCaptureParsers<T extends readonly GeneralParser<any, any>[]> = Ex
|
|
|
91
86
|
* the result and capture types. This is useful for a combinator like `or`
|
|
92
87
|
* which is not able to infer its return type correctly.
|
|
93
88
|
*/
|
|
94
|
-
export type PickParserType<T extends readonly GeneralParser<any, any>[]
|
|
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>;
|
|
89
|
+
export type PickParserType<T extends readonly GeneralParser<any, any>[]> = HasCaptureParsers<T> extends true ? CaptureParser<MergedResults<T>, UnionOfCaptures<T>> : Parser<MergedResults<T>>;
|
|
103
90
|
/** This is used to generate a return type for the `many` and `many1` combinators.
|
|
104
91
|
* Given a parser we want to apply `many` to. Suppose its type is `Parser<string>`.
|
|
105
92
|
* This will return `Parser<string[]>` as the return type.
|