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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export * from "./parsers";
2
- export * from "./combinators";
3
- export * from "./trace";
4
- export * from "./types";
1
+ export * from "./parsers.js";
2
+ export * from "./combinators.js";
3
+ export * from "./trace.js";
4
+ export * from "./types.js";
package/dist/index.js CHANGED
@@ -1,30 +1,4 @@
1
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
- if (k2 === undefined) k2 = k;
3
- var desc = Object.getOwnPropertyDescriptor(m, k);
4
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
- desc = { enumerable: true, get: function() { return m[k]; } };
6
- }
7
- Object.defineProperty(o, k2, desc);
8
- }) : (function(o, m, k, k2) {
9
- if (k2 === undefined) k2 = k;
10
- o[k2] = m[k];
11
- }));
12
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
13
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
14
- };
15
- (function (factory) {
16
- if (typeof module === "object" && typeof module.exports === "object") {
17
- var v = factory(require, exports);
18
- if (v !== undefined) module.exports = v;
19
- }
20
- else if (typeof define === "function" && define.amd) {
21
- define(["require", "exports", "./parsers", "./combinators", "./trace", "./types"], factory);
22
- }
23
- })(function (require, exports) {
24
- "use strict";
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- __exportStar(require("./parsers"), exports);
27
- __exportStar(require("./combinators"), exports);
28
- __exportStar(require("./trace"), exports);
29
- __exportStar(require("./types"), exports);
30
- });
1
+ export * from "./parsers.js";
2
+ export * from "./combinators.js";
3
+ export * from "./trace.js";
4
+ export * from "./types.js";
@@ -1,2 +1,2 @@
1
- import { WithinResult, Parser } from "../types";
1
+ import { WithinResult, Parser } from "../types.js";
2
2
  export declare function within(parser: Parser<string>): Parser<WithinResult[]>;
@@ -1,51 +1,37 @@
1
- (function (factory) {
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", "../trace", "../types"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.within = void 0;
13
- const trace_1 = require("../trace");
14
- const types_1 = require("../types");
15
- function within(parser) {
16
- return (0, trace_1.trace)("within", (input) => {
17
- let start = 0;
18
- let current = 0;
19
- const results = [];
20
- while (current < input.length) {
21
- const parsed = parser(input.slice(current));
22
- if (parsed.success) {
23
- const unmatchedValue = input.slice(start, current);
24
- if (unmatchedValue.length > 0) {
25
- results.push({
26
- type: "unmatched",
27
- value: unmatchedValue,
28
- });
29
- }
1
+ import { trace } from "../trace.js";
2
+ import { success } from "../types.js";
3
+ export function within(parser) {
4
+ return trace("within", (input) => {
5
+ let start = 0;
6
+ let current = 0;
7
+ const results = [];
8
+ while (current < input.length) {
9
+ const parsed = parser(input.slice(current));
10
+ if (parsed.success) {
11
+ const unmatchedValue = input.slice(start, current);
12
+ if (unmatchedValue.length > 0) {
30
13
  results.push({
31
- type: "matched",
32
- value: parsed.result,
14
+ type: "unmatched",
15
+ value: unmatchedValue,
33
16
  });
34
- current += parsed.result.length;
35
- start = current;
36
17
  }
37
- else {
38
- current += 1;
39
- }
40
- }
41
- if (start < current) {
42
18
  results.push({
43
- type: "unmatched",
44
- value: input.slice(start, current),
19
+ type: "matched",
20
+ value: parsed.result,
45
21
  });
22
+ current += parsed.result.length;
23
+ start = current;
24
+ }
25
+ else {
26
+ current += 1;
46
27
  }
47
- return (0, types_1.success)(results, "");
48
- });
49
- }
50
- exports.within = within;
51
- });
28
+ }
29
+ if (start < current) {
30
+ results.push({
31
+ type: "unmatched",
32
+ value: input.slice(start, current),
33
+ });
34
+ }
35
+ return success(results, "");
36
+ });
37
+ }
package/dist/parsers.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Parser } from "./types";
2
- export { within as betweenWithin } from "./parsers/within";
1
+ import { CaptureParser, Parser, Prettify } from "./types.js";
2
+ export { within as betweenWithin } from "./parsers/within.js";
3
3
  /**
4
4
  * Takes a character. Returns a parser that parses that character.
5
5
  *
@@ -80,3 +80,133 @@ export declare const quotedString: Parser<string>;
80
80
  * @returns - parser that matches the given regex
81
81
  */
82
82
  export declare function regexParser(str: string | RegExp, options?: string): Parser<string>;
83
+ /**
84
+ * Like `regexParser`, but you can name your capture groups
85
+ * and get them back as the result instead.
86
+ * Fails if it doesn't have the same number of names as capture groups.
87
+ *
88
+ * @param str - regex string or RegExp instance to match
89
+ * @param options - string of regex options (i = ignore case, g = global, m = multiline, u = unicode)
90
+ * @param captureNames - names of the captures
91
+ * @returns - parser that matches the given regex
92
+ */
93
+ export declare function captureRegex<const T extends string[]>(str: string | RegExp, options?: string, ...captureNames: T): Parser<Prettify<Record<(typeof captureNames)[number], string>>>;
94
+ /**
95
+ * Return a parser that takes a key and a value.
96
+ * The parser consumes no input and always succeeds,
97
+ * and returns `null` as the result. It also returns a captures object
98
+ * with that key-value pair set. This is useful when you need to inject
99
+ * a key-value pair into captures for a `seq`.
100
+ *
101
+ * For example, here is a Markdown heading parser.
102
+
103
+ * ```ts
104
+ * export const headingParser: Parser<Heading> = seqC(
105
+ * capture(count(char("#")), "level"),
106
+ * spaces,
107
+ * capture(many1Till(or(char("\n"), eof)), "content")
108
+ * );
109
+ ```
110
+ *
111
+ * This parser returns
112
+ *
113
+ * ```ts
114
+ * {
115
+ * level: number,
116
+ * content: string
117
+ * }
118
+ * ```
119
+ * but the type of heading is actually
120
+ *
121
+ * ```ts
122
+ * type Heading = {
123
+ * type: "heading";
124
+ * level: number;
125
+ * content: string;
126
+ * };
127
+ * ```
128
+ *
129
+ * The `type` key is missing. You can use `set` to inject the `type`
130
+ * key-value pair into captures:
131
+ *
132
+ * ```ts
133
+ * export const headingParser: Parser<Heading> = seqC(
134
+ * set("type", "heading"),
135
+ * capture(count(char("#")), "level"),
136
+ * spaces,
137
+ * capture(many1Till(or(char("\n"), eof)), "content")
138
+ * );
139
+ * ```
140
+ *
141
+ * @param key - key to set on captures object
142
+ * @param value - value to set on captures object
143
+ * @returns
144
+ */
145
+ export declare function set<const K extends string, const V>(key: K, value: V): CaptureParser<null, Record<K, V>>;
146
+ /**
147
+ * A parser that always succeeds with the given value.
148
+ * @param value - value to succeed with
149
+ * @returns value
150
+ */
151
+ export declare function succeed<T, I = string>(value: T): Parser<T>;
152
+ /**
153
+ * A parser that always fails with the given message.
154
+ * @param message - message to fail with
155
+ * @returns failure
156
+ */
157
+ export declare function fail<I = string>(message: string): Parser<never>;
158
+ /**
159
+ * Takes a string. Succeeds if the given input contains that string.
160
+ * Consumes no input.
161
+ *
162
+ * @param substr - substring to find
163
+ * @returns - parser that succeeds if the given input contains that string
164
+ */
165
+ export declare function includes<const S extends string>(substr: S): Parser<S>;
166
+ /**
167
+ * Like `includes`, but case-insensitive.
168
+ *
169
+ * @param substr - substring to find
170
+ * @returns - parser that succeeds if the given input contains that string
171
+ */
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>;