tarsec 0.0.15 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tarsec",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "A parser combinator library for TypeScript, inspired by Parsec.",
5
5
  "homepage": "https://github.com/egonSchiele/tarsec",
6
6
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "require": "./dist/index.js"
22
22
  }
23
23
  },
24
+ "type": "module",
24
25
  "types": "./dist/index.d.ts",
25
26
  "keywords": ["parser", "parser combinator", "parsec"],
26
27
  "author": "",
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,262 +0,0 @@
1
- import { CaptureParser, GeneralParser, InferManyReturnType, MergedCaptures, MergedResults, Parser, PickParserType, PlainObject } from "./types.js";
2
- /**
3
- * Takes a parser and runs it zero or more times, returning the results as an array.
4
- * If the parser is a capture parser, it returns the captures as an array in this form:
5
- *
6
- * ```ts
7
- * { captures: <array of captures> }
8
- * ```
9
- *
10
- * @param parser - parser to run
11
- * @returns - parser that runs the given parser zero to many times,
12
- * and returns the result as an array
13
- */
14
- export declare function many<const T extends GeneralParser<any, any>>(parser: T): InferManyReturnType<T>;
15
- /**
16
- * Same as `many`, but fails if the parser doesn't match at least once.
17
- *
18
- * @param parser - parser to run
19
- * @returns a parser that runs the given parser one to many times,
20
- */
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>;
28
- /**
29
- * Takes a parser, runs it n times, and returns the results as an array.
30
- * If it cannot run the parser n times, it fails without consuming input.
31
- * @param num - number of times to run the parser
32
- * @param parser - parser to run
33
- * @returns - parser that runs the given parser `num` times and returns an array of the results
34
- */
35
- export declare function exactly<T>(num: number, parser: Parser<T>): Parser<T[]>;
36
- /**
37
- * Same as `many`, but joins the results into a single string.
38
- *
39
- * @param parser - parser to run. The parser must return a string as its result.
40
- * @returns - parser that runs the given parser zero to many times,
41
- * and returns the result as a single string
42
- */
43
- export declare function manyWithJoin<const T extends GeneralParser<string, any>>(parser: T): GeneralParser<string, any>;
44
- /**
45
- * Same as `many1`, but joins the results into a single string.
46
- *
47
- * @param parser - parser to run. The parser must return a string as its result.
48
- * @returns - parser that runs the given parser one to many times,
49
- * and returns the result as a single string
50
- */
51
- export declare function many1WithJoin(parser: Parser<string>): Parser<string>;
52
- /**
53
- * `or` takes an array of parsers and runs them sequentially.
54
- * It returns the results of the first parser that succeeds.
55
- * You can use `capture` in an `or`:
56
- *
57
- * ```ts
58
- * const parser = or(capture(digit, "num"), capture(word, "name"));
59
- * ```
60
- *
61
- * `or` supports backtracking by returning a `nextParser`:
62
- *
63
- * ```ts
64
- * const parser = or(str("hello"), str("hello!"));
65
- *
66
- * // this will match the first parser
67
- * const result = parser("hello");
68
- *
69
- * // but or returns the untried parsers as a new parser
70
- * result.nextParser("hello!"); // works
71
- *
72
- * // result.nextParser is the same as or(str("hello!"))
73
- * ```
74
- *
75
- * @param parsers - parsers to try
76
- * @returns - a parser that tries each parser in order. Returns the result of the first parser that succeeds.
77
- */
78
- export declare function or<const T extends readonly GeneralParser<any, any>[]>(...parsers: T): PickParserType<T>;
79
- /**
80
- * Takes a parser and runs it. If the parser fails,
81
- * optional returns a success with a null result.
82
- *
83
- * @param parser - parser to run
84
- * @returns - a parser that runs the given parser.
85
- * If it fails, returns a success with a null result.
86
- */
87
- export declare function optional<T>(parser: Parser<T>): Parser<T | null>;
88
- /**
89
- * Takes a parser and runs it. If the parser fails,
90
- * `not` returns a success with a `null` result.
91
- * If the parser succeeds, `not` returns a failure.
92
- *
93
- * @param parser - parser to run
94
- * @returns - a parser that runs the given parser.
95
- * If it fails, returns a success with a `null` result.
96
- * If it succeeds, returns a failure.
97
- */
98
- export declare function not(parser: Parser<any>): Parser<null>;
99
- /**
100
- * Takes three parsers, `open`, `close`, and `parser`.
101
- * `between` matches something that matches `parser`,
102
- * surrounded by `open` and `close`. It returns the result of `parser`.
103
- * If any of the parsers fail, `between` fails.
104
- *
105
- * @param open - parser for the opening delimiter
106
- * @param close - parser for the closing delimiter
107
- * @param parser - parser for the content
108
- * @returns a parser that returns the result of `parser`.
109
- */
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
- */
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
- */
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
- */
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
- */
142
- export declare function capture<T, const S extends string>(parser: Parser<T>, name: S): CaptureParser<T, Record<S, T>>;
143
- /**
144
- * Returns a parser that consumes input till the given parser succeeds.
145
- * @param parser - the stop parser
146
- * @returns a parser that consumes the input string until the stop parser succeeds.
147
- * Then it returns the consumed input as a string.
148
- * The stop parser's match is not included in the result.
149
- */
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
- */
156
- export declare function many1Till<T>(parser: Parser<T>): Parser<string>;
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
- */
218
- export declare function search(parser: Parser<string>): Parser<string[]>;
219
- /**
220
- * seq takes an array of parsers and runs them sequentially.
221
- * If any of the parsers fail, seq fails without consuming any input.
222
- *
223
- * The second argument to seq is a function.
224
- * The first argument of that function is an array of results:
225
- * one result from each of the parsers you gave to seq.
226
- * The second is an object containing any captures.
227
- * You can use this second argument, the transformer function,
228
- * to transform these however you want and return a result
229
- *
230
- * Tarsec includes the utility functions `getResults` and `getCaptures`
231
- * to just return the results array or captures object respectively for you.
232
- *
233
- * Finally, you don't need to use seq at all. You can just hand write the logic.
234
- * But you'll need to do the error handling
235
- * and pass the remaining input to the next parser yourself.
236
- * seq also does some backtracking for you that you will need to do yourself.
237
- *
238
- * Also see `seqR` and `seqC` for convenience functions that return the results or captures respectively.
239
- *
240
- * @param parsers - parsers to run sequentially
241
- * @param transform - function to transform the results and captures. The params are the results and captures
242
- * @param debugName - optional name for trace debugging
243
- * @returns
244
- */
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
- */
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
- */
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
- */
262
- export declare function match(input: string, parser: GeneralParser<any, any>): boolean;