parser-combinators 1.1.2 → 1.2.0
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/LICENSE +12 -0
- package/README.md +49 -4
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/{parser.d.ts → dist/parser.d.ts} +9 -16
- package/{parser.js → dist/parser.js} +21 -34
- package/{parsers → dist/parsers}/any.d.ts +16 -15
- package/dist/parsers/any.js +47 -0
- package/dist/parsers/anyString.d.ts +15 -0
- package/dist/parsers/anyString.js +85 -0
- package/{parsers → dist/parsers}/between.d.ts +5 -5
- package/{parsers → dist/parsers}/between.js +20 -21
- package/{parsers → dist/parsers}/exhaust.d.ts +5 -5
- package/{parsers → dist/parsers}/exhaust.js +25 -27
- package/{parsers → dist/parsers}/index.d.ts +12 -11
- package/{parsers → dist/parsers}/index.js +31 -23
- package/{parsers → dist/parsers}/many.d.ts +17 -17
- package/{parsers → dist/parsers}/many.js +61 -61
- package/{parsers → dist/parsers}/map.d.ts +5 -5
- package/{parsers → dist/parsers}/map.js +21 -22
- package/{parsers → dist/parsers}/opt.d.ts +5 -5
- package/{parsers → dist/parsers}/opt.js +16 -17
- package/dist/parsers/recovery.d.ts +14 -0
- package/dist/parsers/recovery.js +53 -0
- package/{parsers → dist/parsers}/regex.d.ts +5 -5
- package/{parsers → dist/parsers}/regex.js +20 -21
- package/{parsers → dist/parsers}/seq.d.ts +16 -15
- package/{parsers → dist/parsers}/seq.js +17 -18
- package/dist/parsers/str.d.ts +17 -0
- package/dist/parsers/str.js +36 -0
- package/dist/parsers/utilities.d.ts +33 -0
- package/dist/parsers/utilities.js +96 -0
- package/{parsers → dist/parsers}/values.d.ts +31 -28
- package/{parsers → dist/parsers}/values.js +48 -36
- package/{types.d.ts → dist/types.d.ts} +40 -33
- package/{types.js → dist/types.js} +47 -47
- package/package.json +22 -24
- package/parsers/any.js +0 -20
- package/parsers/str.d.ts +0 -5
- package/parsers/str.js +0 -18
- package/parsers/utilities.d.ts +0 -9
- package/parsers/utilities.js +0 -30
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
import { Parser } from '../types';
|
|
2
|
-
/** Parses 0 or more spaces
|
|
3
|
-
*/
|
|
4
|
-
export declare const spaces: Parser<string>;
|
|
5
|
-
/** Parses 1 or more spaces
|
|
6
|
-
*/
|
|
7
|
-
export declare const spacesPlus: Parser<string>;
|
|
8
|
-
/** Parses 0 or more whitespace characters
|
|
9
|
-
*/
|
|
10
|
-
export declare const wspaces: Parser<string | null>;
|
|
11
|
-
/** Parses a boolean (true or false) and returns a string
|
|
12
|
-
*/
|
|
13
|
-
export declare const bool: Parser<'true' | 'false'>;
|
|
14
|
-
/** Parses a boolean (true or false) and returns a boolean
|
|
15
|
-
*/
|
|
16
|
-
export declare const boolP: Parser<boolean>;
|
|
17
|
-
/** Parses an integer and returns a string
|
|
18
|
-
*/
|
|
19
|
-
export declare const int: Parser<string>;
|
|
20
|
-
/** Parses an integer and returns a number
|
|
21
|
-
*/
|
|
22
|
-
export declare const intP: Parser<number>;
|
|
23
|
-
/** Parses a standard real number (X.X) and returns a string
|
|
24
|
-
*/
|
|
25
|
-
export declare const real: Parser<`${number}.${number}`>;
|
|
26
|
-
/** Parses a standard real number (X.X) and returns a number
|
|
27
|
-
*/
|
|
28
|
-
export declare const realP: Parser<number>;
|
|
1
|
+
import { Parser } from '../types';
|
|
2
|
+
/** Parses 0 or more spaces
|
|
3
|
+
*/
|
|
4
|
+
export declare const spaces: Parser<string>;
|
|
5
|
+
/** Parses 1 or more spaces
|
|
6
|
+
*/
|
|
7
|
+
export declare const spacesPlus: Parser<string>;
|
|
8
|
+
/** Parses 0 or more whitespace characters
|
|
9
|
+
*/
|
|
10
|
+
export declare const wspaces: Parser<string | null>;
|
|
11
|
+
/** Parses a boolean (true or false) and returns a string
|
|
12
|
+
*/
|
|
13
|
+
export declare const bool: Parser<'true' | 'false'>;
|
|
14
|
+
/** Parses a boolean (true or false) and returns a boolean
|
|
15
|
+
*/
|
|
16
|
+
export declare const boolP: Parser<boolean>;
|
|
17
|
+
/** Parses an integer and returns a string
|
|
18
|
+
*/
|
|
19
|
+
export declare const int: Parser<string>;
|
|
20
|
+
/** Parses an integer and returns a number
|
|
21
|
+
*/
|
|
22
|
+
export declare const intP: Parser<number>;
|
|
23
|
+
/** Parses a standard real number (X.X) and returns a string
|
|
24
|
+
*/
|
|
25
|
+
export declare const real: Parser<`${number}.${number}`>;
|
|
26
|
+
/** Parses a standard real number (X.X) and returns a number
|
|
27
|
+
*/
|
|
28
|
+
export declare const realP: Parser<number>;
|
|
29
|
+
/** Parses an end of text/file. Fails if the parser is not at the end.
|
|
30
|
+
*/
|
|
31
|
+
export declare const eof: Parser<void>;
|
|
@@ -1,36 +1,48 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.realP = exports.real = exports.intP = exports.int = exports.boolP = exports.bool = exports.wspaces = exports.spacesPlus = exports.spaces = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.eof = exports.realP = exports.real = exports.intP = exports.int = exports.boolP = exports.bool = exports.wspaces = exports.spacesPlus = exports.spaces = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const map_1 = require("./map");
|
|
6
|
+
const opt_1 = require("./opt");
|
|
7
|
+
const regex_1 = require("./regex");
|
|
8
|
+
const seq_1 = require("./seq");
|
|
9
|
+
const str_1 = require("./str");
|
|
10
|
+
const utilities_1 = require("./utilities");
|
|
11
|
+
/** Parses 0 or more spaces
|
|
12
|
+
*/
|
|
13
|
+
exports.spaces = (0, regex_1.regex)(/ */, 'spaces');
|
|
14
|
+
/** Parses 1 or more spaces
|
|
15
|
+
*/
|
|
16
|
+
exports.spacesPlus = (0, regex_1.regex)(/ +/, 'spaces');
|
|
17
|
+
/** Parses 0 or more whitespace characters
|
|
18
|
+
*/
|
|
19
|
+
exports.wspaces = (0, opt_1.opt)((0, regex_1.regex)(/(?:\s|\t|\n|\r)+/, 'whitespace characters'));
|
|
20
|
+
/** Parses a boolean (true or false) and returns a string
|
|
21
|
+
*/
|
|
22
|
+
exports.bool = (0, regex_1.regex)(/true|false/, 'boolean');
|
|
23
|
+
/** Parses a boolean (true or false) and returns a boolean
|
|
24
|
+
*/
|
|
25
|
+
exports.boolP = (0, map_1.map)(exports.bool, val => val === 'true');
|
|
26
|
+
/** Parses an integer and returns a string
|
|
27
|
+
*/
|
|
28
|
+
exports.int = (0, regex_1.regex)(/\d+/, 'integer');
|
|
29
|
+
/** Parses an integer and returns a number
|
|
30
|
+
*/
|
|
31
|
+
exports.intP = (0, map_1.map)(exports.int, seq => parseInt(seq, 10));
|
|
32
|
+
/** Parses a standard real number (X.X) and returns a string
|
|
33
|
+
*/
|
|
34
|
+
exports.real = (0, utilities_1.expect)((0, map_1.map)((0, seq_1.seq)(exports.int, (0, str_1.str)('.'), exports.int), ([intPart, , decimalPart]) => `${intPart}.${decimalPart}`), 'real');
|
|
35
|
+
/** Parses a standard real number (X.X) and returns a number
|
|
36
|
+
*/
|
|
37
|
+
exports.realP = (0, map_1.map)(exports.real, (seq) => parseFloat(seq));
|
|
38
|
+
/** Parses an end of text/file. Fails if the parser is not at the end.
|
|
39
|
+
*/
|
|
40
|
+
const eof = (ctx) => {
|
|
41
|
+
if (ctx.index === ctx.text.length) {
|
|
42
|
+
return (0, types_1.success)(ctx, void 0);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
return (0, types_1.failure)(ctx, "End Of File", ["EOF"]);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.eof = eof;
|
|
@@ -1,33 +1,40 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
text: string;
|
|
4
|
-
path: string;
|
|
5
|
-
index: number;
|
|
6
|
-
}>;
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
success: true;
|
|
10
|
-
value: T;
|
|
11
|
-
ctx: Context;
|
|
12
|
-
}>;
|
|
13
|
-
export
|
|
14
|
-
success: false;
|
|
15
|
-
expected: string;
|
|
16
|
-
ctx: Context;
|
|
17
|
-
history: string[];
|
|
18
|
-
}>;
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
export
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
export type Parser<T> = (ctx: Context) => Result<T>;
|
|
2
|
+
export type Context = Readonly<{
|
|
3
|
+
text: string;
|
|
4
|
+
path: string;
|
|
5
|
+
index: number;
|
|
6
|
+
}>;
|
|
7
|
+
export type Result<T> = Success<T> | Failure;
|
|
8
|
+
export type Success<T> = Readonly<{
|
|
9
|
+
success: true;
|
|
10
|
+
value: T;
|
|
11
|
+
ctx: Context;
|
|
12
|
+
}>;
|
|
13
|
+
export type Failure = Readonly<{
|
|
14
|
+
success: false;
|
|
15
|
+
expected: string;
|
|
16
|
+
ctx: Context;
|
|
17
|
+
history: string[];
|
|
18
|
+
}>;
|
|
19
|
+
export type TokenRange = {
|
|
20
|
+
start: number;
|
|
21
|
+
end: number;
|
|
22
|
+
};
|
|
23
|
+
export type Token<T> = TokenRange & {
|
|
24
|
+
value: T;
|
|
25
|
+
};
|
|
26
|
+
export declare function isFailure(input: unknown): input is Failure;
|
|
27
|
+
export declare function success<T>(ctx: Context, value: T): Success<T>;
|
|
28
|
+
export declare function failure(ctx: Context, expected: string, history: string[]): Failure;
|
|
29
|
+
export declare function result<T>(value: T): Parser<T>;
|
|
30
|
+
export declare function fail<T>(reason: string): Parser<T>;
|
|
31
|
+
export declare class ParseError extends Error {
|
|
32
|
+
readonly index: number;
|
|
33
|
+
readonly history: string[];
|
|
34
|
+
readonly line: string;
|
|
35
|
+
readonly column: number;
|
|
36
|
+
readonly row: number;
|
|
37
|
+
constructor(message: string, input: string, index: number, history: string[]);
|
|
38
|
+
private getInputData;
|
|
39
|
+
getPrettyErrorMessage(): string;
|
|
40
|
+
}
|
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ParseError =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function
|
|
13
|
-
return { success:
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
class ParseError extends Error {
|
|
25
|
-
constructor(message, input, index, history) {
|
|
26
|
-
super(message);
|
|
27
|
-
this.index = index;
|
|
28
|
-
this.history = history;
|
|
29
|
-
[this.line, this.column, this.row] = this.getInputData(input, index);
|
|
30
|
-
}
|
|
31
|
-
getInputData(input, index) {
|
|
32
|
-
const lines = input.split('\n');
|
|
33
|
-
let row = 0;
|
|
34
|
-
while (index > 0) {
|
|
35
|
-
if (lines[row].length > index) {
|
|
36
|
-
return [lines[row], index + 1, row + 1];
|
|
37
|
-
}
|
|
38
|
-
index -= lines[row].length + 1;
|
|
39
|
-
row += 1;
|
|
40
|
-
}
|
|
41
|
-
return [lines[row], index + 1, row + 1];
|
|
42
|
-
}
|
|
43
|
-
getPrettyErrorMessage() {
|
|
44
|
-
return `${this.message} (line ${this.row}, col ${this.column})
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.ParseError = ParseError;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ParseError = void 0;
|
|
4
|
+
exports.isFailure = isFailure;
|
|
5
|
+
exports.success = success;
|
|
6
|
+
exports.failure = failure;
|
|
7
|
+
exports.result = result;
|
|
8
|
+
exports.fail = fail;
|
|
9
|
+
function isFailure(input) {
|
|
10
|
+
return !input.success;
|
|
11
|
+
}
|
|
12
|
+
function success(ctx, value) {
|
|
13
|
+
return { success: true, value, ctx };
|
|
14
|
+
}
|
|
15
|
+
function failure(ctx, expected, history) {
|
|
16
|
+
return { success: false, expected, ctx, history };
|
|
17
|
+
}
|
|
18
|
+
function result(value) {
|
|
19
|
+
return (ctx) => success(ctx, value);
|
|
20
|
+
}
|
|
21
|
+
function fail(reason) {
|
|
22
|
+
return (ctx) => failure(ctx, reason, [reason]);
|
|
23
|
+
}
|
|
24
|
+
class ParseError extends Error {
|
|
25
|
+
constructor(message, input, index, history) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.index = index;
|
|
28
|
+
this.history = history;
|
|
29
|
+
[this.line, this.column, this.row] = this.getInputData(input, index);
|
|
30
|
+
}
|
|
31
|
+
getInputData(input, index) {
|
|
32
|
+
const lines = input.split('\n');
|
|
33
|
+
let row = 0;
|
|
34
|
+
while (index > 0) {
|
|
35
|
+
if (lines[row].length > index) {
|
|
36
|
+
return [lines[row], index + 1, row + 1];
|
|
37
|
+
}
|
|
38
|
+
index -= lines[row].length + 1;
|
|
39
|
+
row += 1;
|
|
40
|
+
}
|
|
41
|
+
return [lines[row], index + 1, row + 1];
|
|
42
|
+
}
|
|
43
|
+
getPrettyErrorMessage() {
|
|
44
|
+
return `${this.message} (line ${this.row}, col ${this.column}):\n${this.line}\n${(this.column > 0 ? '-'.repeat(this.column - 1) : '') + '^'}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.ParseError = ParseError;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "parser-combinators",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"maintainers": [
|
|
6
6
|
"Micha_i"
|
|
7
7
|
],
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/michalusio/Parser.git"
|
|
10
|
+
"url": "git+https://github.com/michalusio/Parser.git"
|
|
11
11
|
},
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/michalusio/Parser/issues"
|
|
@@ -16,33 +16,31 @@
|
|
|
16
16
|
"description": "A library of parser combinators, with which you can create your own parsers. The library will be continuously improved in time.",
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsc",
|
|
19
|
-
"publishing:prepare": "copy package.json dist && copy README.md dist",
|
|
20
|
-
"publishing:patch": "npm version patch && npm run publishing:prepare",
|
|
21
|
-
"publishing:minor": "npm version minor && npm run publishing:prepare",
|
|
22
|
-
"publishing:major": "npm version major && npm run publishing:prepare",
|
|
23
19
|
"start": "tsc && node .",
|
|
24
20
|
"lint": "eslint . --ext .ts",
|
|
25
|
-
"test:
|
|
26
|
-
"test": "
|
|
27
|
-
"test
|
|
21
|
+
"test:mutate": "stryker run",
|
|
22
|
+
"test:unit": "mocha",
|
|
23
|
+
"test": "nyc -e '.ts' --r html -r lcov -r text npm run test:unit"
|
|
28
24
|
},
|
|
29
25
|
"author": "Micha_i <isalski.michal@gmail.com> (https://github.com/michalusio)",
|
|
30
|
-
"main": "./
|
|
31
|
-
"types": "./
|
|
32
|
-
"
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
33
31
|
"devDependencies": {
|
|
34
|
-
"@
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
37
|
-
"@
|
|
38
|
-
"
|
|
39
|
-
"cross-env": "
|
|
40
|
-
"eslint": "
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"typescript": "
|
|
32
|
+
"@stryker-mutator/core": "^9.4.0",
|
|
33
|
+
"@stryker-mutator/mocha-runner": "^9.4.0",
|
|
34
|
+
"@stryker-mutator/typescript-checker": "^9.4.0",
|
|
35
|
+
"@types/mocha": "10.0.10",
|
|
36
|
+
"@types/node": "25.0.6",
|
|
37
|
+
"cross-env": "10.1.0",
|
|
38
|
+
"eslint": "9.39.2",
|
|
39
|
+
"mocha": "11.7.5",
|
|
40
|
+
"nyc": "17.1.0",
|
|
41
|
+
"ts-node": "10.9.2",
|
|
42
|
+
"typescript": "5.9.3",
|
|
43
|
+
"typescript-eslint": "8.52.0"
|
|
46
44
|
},
|
|
47
45
|
"keywords": [
|
|
48
46
|
"parser",
|
package/parsers/any.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.any = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
function any(...parsers) {
|
|
6
|
-
return (ctx) => {
|
|
7
|
-
const expected = [];
|
|
8
|
-
for (const parser of parsers) {
|
|
9
|
-
const res = parser(ctx);
|
|
10
|
-
if ((0, types_1.isFailure)(res)) {
|
|
11
|
-
expected.push(res);
|
|
12
|
-
}
|
|
13
|
-
else
|
|
14
|
-
return res;
|
|
15
|
-
}
|
|
16
|
-
const longest = expected.reduce((a, b) => a.ctx.index > b.ctx.index ? a : b);
|
|
17
|
-
return (0, types_1.failure)(longest.ctx, longest.expected, ['any', ...longest.history]);
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
exports.any = any;
|
package/parsers/str.d.ts
DELETED
package/parsers/str.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.str = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
/** Parses a string and returns it as a result of the parse.
|
|
6
|
-
* @returns A parser parsing a given string.
|
|
7
|
-
*/
|
|
8
|
-
function str(match) {
|
|
9
|
-
return (ctx) => {
|
|
10
|
-
if (ctx.text.startsWith(match, ctx.index)) {
|
|
11
|
-
return (0, types_1.success)({ ...ctx, index: ctx.index + match.length }, match);
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
return (0, types_1.failure)(ctx, match, [match]);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
exports.str = str;
|
package/parsers/utilities.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Parser } from '../types';
|
|
2
|
-
/** Allows to make a condition on the result of the parsing function.
|
|
3
|
-
* @returns A parser returning the same but also performing a given check on the result.
|
|
4
|
-
*/
|
|
5
|
-
export declare function ref<T>(parser: Parser<T>, check: ((p: T) => boolean), expected: string): Parser<T>;
|
|
6
|
-
/** Changes the expected value for when the parser fails.
|
|
7
|
-
* @returns A parser returning the same but with a different expected value.
|
|
8
|
-
*/
|
|
9
|
-
export declare function expect<T>(parser: Parser<T>, expected: string): Parser<T>;
|
package/parsers/utilities.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.expect = exports.ref = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
/** Allows to make a condition on the result of the parsing function.
|
|
6
|
-
* @returns A parser returning the same but also performing a given check on the result.
|
|
7
|
-
*/
|
|
8
|
-
function ref(parser, check, expected) {
|
|
9
|
-
return (ctx) => {
|
|
10
|
-
const res = parser(ctx);
|
|
11
|
-
if (!(0, types_1.isFailure)(res) && !check(res.value)) {
|
|
12
|
-
return (0, types_1.failure)(res.ctx, expected, [`ref: ${expected}`]);
|
|
13
|
-
}
|
|
14
|
-
return res;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
exports.ref = ref;
|
|
18
|
-
/** Changes the expected value for when the parser fails.
|
|
19
|
-
* @returns A parser returning the same but with a different expected value.
|
|
20
|
-
*/
|
|
21
|
-
function expect(parser, expected) {
|
|
22
|
-
return (ctx) => {
|
|
23
|
-
const res = parser(ctx);
|
|
24
|
-
if ((0, types_1.isFailure)(res)) {
|
|
25
|
-
return (0, types_1.failure)(res.ctx, expected, [expected, ...res.history]);
|
|
26
|
-
}
|
|
27
|
-
return res;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
exports.expect = expect;
|