@zigsterz/parzing 1.0.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.
@@ -0,0 +1,9 @@
1
+ import { Parser, ParserContext, ParseResult, ParserWithInternalWhitespaceSupport } from "../core";
2
+ export declare class ManyCombinator<T> extends ParserWithInternalWhitespaceSupport<T[]> {
3
+ private _parser;
4
+ private _sepParser;
5
+ private _min;
6
+ private _max;
7
+ constructor(_parser: Parser<T>, _sepParser?: Parser<unknown>, _min?: number, _max?: number);
8
+ parse(parserContext: ParserContext): ParseResult<T[]>;
9
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.ManyCombinator = void 0;
19
+ var core_1 = require("../core");
20
+ var ManyCombinator = /** @class */ (function (_super) {
21
+ __extends(ManyCombinator, _super);
22
+ function ManyCombinator(_parser, _sepParser, _min, _max) {
23
+ if (_sepParser === void 0) { _sepParser = null; }
24
+ if (_min === void 0) { _min = 0; }
25
+ if (_max === void 0) { _max = 0; }
26
+ var _this = _super.call(this) || this;
27
+ _this._parser = _parser;
28
+ _this._sepParser = _sepParser;
29
+ _this._min = _min;
30
+ _this._max = _max;
31
+ return _this;
32
+ }
33
+ ManyCombinator.prototype.parse = function (parserContext) {
34
+ var output = [];
35
+ var pce = parserContext.cutEncountered;
36
+ var mustParseElement = false;
37
+ try {
38
+ do {
39
+ // Parse element
40
+ var bm = parserContext.input.getBookmark();
41
+ parserContext.cutEncountered = false;
42
+ var psr = this._parser.parse(parserContext);
43
+ if (psr.successful) {
44
+ output.push(psr.result);
45
+ }
46
+ else {
47
+ if (mustParseElement || parserContext.cutEncountered) {
48
+ return psr;
49
+ }
50
+ else {
51
+ parserContext.input.seekToBookmark(bm);
52
+ break;
53
+ }
54
+ }
55
+ mustParseElement = false;
56
+ // Parse WS
57
+ parserContext.cutEncountered = false;
58
+ var wpr = this.parseWhitespace(parserContext);
59
+ if (!wpr.successful) {
60
+ return wpr;
61
+ }
62
+ // Attempt parse sep
63
+ if (this._sepParser) {
64
+ parserContext.cutEncountered = false;
65
+ bm = parserContext.input.getBookmark();
66
+ var sepr = this._sepParser.parse(parserContext);
67
+ if (sepr.successful) {
68
+ mustParseElement = true;
69
+ // Parse WS post separator
70
+ parserContext.cutEncountered = false;
71
+ wpr = this.parseWhitespace(parserContext);
72
+ if (!wpr.successful) {
73
+ return wpr;
74
+ }
75
+ }
76
+ else {
77
+ if (parserContext.cutEncountered) {
78
+ return sepr;
79
+ }
80
+ else {
81
+ parserContext.input.seekToBookmark(bm);
82
+ break;
83
+ }
84
+ }
85
+ }
86
+ } while (true);
87
+ if ((output.length < this._min) ||
88
+ ((this._max > 0) && output.length > this._max)) {
89
+ return core_1.ParseResult.failed(core_1.ParseError.parserRejected(this, parserContext, "Expected occurences in range {" + this._min + ", " + this._max + "}; found " + output.length));
90
+ }
91
+ else {
92
+ return core_1.ParseResult.successful(output);
93
+ }
94
+ }
95
+ finally {
96
+ parserContext.cutEncountered = pce;
97
+ }
98
+ };
99
+ return ManyCombinator;
100
+ }(core_1.ParserWithInternalWhitespaceSupport));
101
+ exports.ManyCombinator = ManyCombinator;
@@ -0,0 +1,7 @@
1
+ import { Parser, ParserContext, ParseResult, ParserType } from "../core";
2
+ export declare class MapParser<V extends Parser<unknown>, T> implements Parser<T> {
3
+ private _parser;
4
+ private _fn;
5
+ constructor(_parser: V, _fn: (i: ParserType<V>) => T);
6
+ parse(parserContext: ParserContext): ParseResult<T>;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MapParser = void 0;
4
+ var core_1 = require("../core");
5
+ var MapParser = /** @class */ (function () {
6
+ function MapParser(_parser, _fn) {
7
+ this._parser = _parser;
8
+ this._fn = _fn;
9
+ }
10
+ MapParser.prototype.parse = function (parserContext) {
11
+ var s = this._parser.parse(parserContext);
12
+ if (!s.successful) {
13
+ return s;
14
+ }
15
+ return core_1.ParseResult.successful(this._fn(s.result));
16
+ };
17
+ return MapParser;
18
+ }());
19
+ exports.MapParser = MapParser;
@@ -0,0 +1,6 @@
1
+ import { Parser, ParserContext, ParseResult } from "../core";
2
+ export declare class OptionalCombinator<T> implements Parser<T | null> {
3
+ private _parser;
4
+ constructor(_parser: Parser<T>);
5
+ parse(parserContext: ParserContext): ParseResult<T | null>;
6
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OptionalCombinator = void 0;
4
+ var core_1 = require("../core");
5
+ var OptionalCombinator = /** @class */ (function () {
6
+ function OptionalCombinator(_parser) {
7
+ this._parser = _parser;
8
+ }
9
+ OptionalCombinator.prototype.parse = function (parserContext) {
10
+ var input = parserContext.input;
11
+ var bm = input.getBookmark();
12
+ var pce = parserContext.cutEncountered;
13
+ parserContext.cutEncountered = false;
14
+ var ret = this._parser.parse(parserContext);
15
+ if (!ret.successful && !parserContext.cutEncountered) {
16
+ input.seekToBookmark(bm);
17
+ ret = core_1.ParseResult.successful(null);
18
+ }
19
+ parserContext.cutEncountered = pce;
20
+ return ret;
21
+ };
22
+ return OptionalCombinator;
23
+ }());
24
+ exports.OptionalCombinator = OptionalCombinator;
@@ -0,0 +1,12 @@
1
+ import { Parser, ParserContext, ParseResult } from "../core";
2
+ declare type ResultWithIndices<T> = {
3
+ result: T;
4
+ start: number;
5
+ length: number;
6
+ };
7
+ export declare class ParserWithIndices<T> implements Parser<ResultWithIndices<T>> {
8
+ private _underlying;
9
+ constructor(_underlying: Parser<T>);
10
+ parse(parserContext: ParserContext): ParseResult<ResultWithIndices<T>>;
11
+ }
12
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParserWithIndices = void 0;
4
+ var core_1 = require("../core");
5
+ var ParserWithIndices = /** @class */ (function () {
6
+ function ParserWithIndices(_underlying) {
7
+ this._underlying = _underlying;
8
+ }
9
+ ParserWithIndices.prototype.parse = function (parserContext) {
10
+ var startOfs = parserContext.input.tell();
11
+ var ret = this._underlying.parse(parserContext);
12
+ var endOfs = parserContext.input.tell();
13
+ if (!ret.successful) {
14
+ return ret;
15
+ }
16
+ return core_1.ParseResult.successful({ result: ret.result, start: startOfs, length: endOfs - startOfs });
17
+ };
18
+ return ParserWithIndices;
19
+ }());
20
+ exports.ParserWithIndices = ParserWithIndices;
@@ -0,0 +1,10 @@
1
+ import { Parser, ParserContext, ParseResult, ParserType, ParserWithInternalWhitespaceSupport } from "../core";
2
+ export declare type FilterVoid<T> = T extends [infer Head, ...infer Rest] ? (Head extends void ? [...FilterVoid<Rest>] : [Head, ...FilterVoid<Rest>]) : T;
3
+ export declare type SeqType<TS extends Parser<unknown>[]> = FilterVoid<{
4
+ [i in keyof TS]: ParserType<TS[i]>;
5
+ }>;
6
+ export declare class SequenceCombinator<TS extends Parser<unknown>[]> extends ParserWithInternalWhitespaceSupport<SeqType<TS>> {
7
+ private _parsers;
8
+ constructor(_parsers: TS);
9
+ parse(parserContext: ParserContext): ParseResult<SeqType<TS>>;
10
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.SequenceCombinator = void 0;
19
+ var core_1 = require("../core");
20
+ var SequenceCombinator = /** @class */ (function (_super) {
21
+ __extends(SequenceCombinator, _super);
22
+ function SequenceCombinator(_parsers) {
23
+ var _this = _super.call(this) || this;
24
+ _this._parsers = _parsers;
25
+ return _this;
26
+ }
27
+ SequenceCombinator.prototype.parse = function (parserContext) {
28
+ var results = [];
29
+ for (var i = 0; i < this._parsers.length; i++) {
30
+ if (i) {
31
+ var wsr = this.parseWhitespace(parserContext);
32
+ if (!wsr.successful) {
33
+ return wsr;
34
+ }
35
+ }
36
+ var psr = this._parsers[i].parse(parserContext);
37
+ if (psr.successful) {
38
+ if (psr.result !== undefined) {
39
+ results.push(psr.result);
40
+ }
41
+ }
42
+ else {
43
+ return psr;
44
+ }
45
+ }
46
+ return core_1.ParseResult.successful(results);
47
+ };
48
+ return SequenceCombinator;
49
+ }(core_1.ParserWithInternalWhitespaceSupport));
50
+ exports.SequenceCombinator = SequenceCombinator;
package/dist/core.d.ts ADDED
@@ -0,0 +1,89 @@
1
+ export interface ParserInputBookmark {
2
+ }
3
+ export interface ParserInput {
4
+ read(readLen: number): string;
5
+ peek(peekLen: number): string;
6
+ getBookmark(): ParserInputBookmark;
7
+ seekToBookmark(bm: ParserInputBookmark): any;
8
+ eof(): boolean;
9
+ tell(): number;
10
+ }
11
+ export declare class StringParserInput implements ParserInput {
12
+ private _text;
13
+ private _index;
14
+ constructor(_text: String);
15
+ read(readLen: number): string;
16
+ peek(readLen: number): string;
17
+ getBookmark(): ParserInputBookmark;
18
+ seekToBookmark(bm: ParserInputBookmark): void;
19
+ eof(): boolean;
20
+ remainder(): string;
21
+ skip(howMuch: number): void;
22
+ tell(): number;
23
+ }
24
+ export declare class ParserContext {
25
+ private _input;
26
+ private _whitespaceParser;
27
+ constructor(_input: ParserInput, _whitespaceParser?: Parser<unknown> | null);
28
+ parseWhitespace(): void;
29
+ get input(): ParserInput;
30
+ cutEncountered: boolean;
31
+ }
32
+ export declare type ParseResult<T> = {
33
+ successful: true;
34
+ failed: false;
35
+ result: T;
36
+ } | {
37
+ successful: false;
38
+ failed: true;
39
+ parseError: ParseError;
40
+ };
41
+ export declare namespace ParseResult {
42
+ function successful<T>(r: T): ParseResult<T>;
43
+ function voidSuccessful(): ParseResult<void>;
44
+ function failed<T>(r: ParseError): ParseResult<T>;
45
+ function resultOrThrow<T>(p: ParseResult<T>): T;
46
+ }
47
+ export interface Parser<T> {
48
+ parse(parserContext: ParserContext): ParseResult<T>;
49
+ }
50
+ export declare function isParser(p: any): p is Parser<unknown>;
51
+ export declare class FailParser implements Parser<void> {
52
+ private _message;
53
+ constructor(_message?: string);
54
+ parse(parserContext: ParserContext): any;
55
+ }
56
+ export declare class PassParser implements Parser<void> {
57
+ parse(parserContext: ParserContext): ParseResult<void>;
58
+ }
59
+ export declare class CutParser implements Parser<void> {
60
+ parse(parserContext: ParserContext): ParseResult<void>;
61
+ }
62
+ export declare class RefParser<T> implements Parser<T> {
63
+ private _parserProvider;
64
+ constructor(_parserProvider: () => Parser<T>);
65
+ parse(parserContext: ParserContext): ParseResult<T>;
66
+ private _parser;
67
+ }
68
+ export declare type ParserType<pt> = pt extends Parser<infer T> ? T : never;
69
+ export declare class ParserWithInternalWhitespaceSupport<T> implements Parser<T> {
70
+ parse(parserContext: ParserContext): ParseResult<T>;
71
+ whitespace(whitespaceParser: Parser<unknown> | null): ParserWithInternalWhitespaceSupport<T>;
72
+ protected parseWhitespace(parserContext: ParserContext): {
73
+ successful: false;
74
+ failed: true;
75
+ parseError: ParseError;
76
+ } | {
77
+ successful: true;
78
+ failed: false;
79
+ result: unknown;
80
+ };
81
+ private _whitespace;
82
+ }
83
+ export declare function parse<T>(parser: Parser<T>, input: ParserInput | string, allowPartial?: boolean): T;
84
+ export declare class ParseError {
85
+ message: string;
86
+ constructor(input: ParserInput, bookmark: ParserInputBookmark | null, parser: Parser<unknown> | null, contentMessage: string);
87
+ toString(): string;
88
+ static parserRejected(parser: Parser<unknown>, context: ParserContext, message?: string): ParseError;
89
+ }
package/dist/core.js ADDED
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParseError = exports.parse = exports.ParserWithInternalWhitespaceSupport = exports.RefParser = exports.CutParser = exports.PassParser = exports.FailParser = exports.isParser = exports.ParseResult = exports.ParserContext = exports.StringParserInput = void 0;
4
+ var StringParserInput = /** @class */ (function () {
5
+ function StringParserInput(_text) {
6
+ this._text = _text;
7
+ this._index = 0;
8
+ }
9
+ StringParserInput.prototype.read = function (readLen) {
10
+ if (this._index + readLen > this._text.length) {
11
+ this._index = this._text.length;
12
+ throw new ParseError(this, this.getBookmark(), null, "Attempt to read beyond EOF");
13
+ }
14
+ var ret = this._text.substr(this._index, readLen);
15
+ this._index += readLen;
16
+ return ret;
17
+ };
18
+ StringParserInput.prototype.peek = function (readLen) {
19
+ return this._text.substr(this._index, readLen);
20
+ };
21
+ StringParserInput.prototype.getBookmark = function () {
22
+ return this._index;
23
+ };
24
+ StringParserInput.prototype.seekToBookmark = function (bm) {
25
+ this._index = bm;
26
+ };
27
+ StringParserInput.prototype.eof = function () {
28
+ return this._index >= this._text.length;
29
+ };
30
+ StringParserInput.prototype.remainder = function () {
31
+ return this._text.substr(this._index);
32
+ };
33
+ StringParserInput.prototype.skip = function (howMuch) {
34
+ this._index += howMuch;
35
+ };
36
+ StringParserInput.prototype.tell = function () {
37
+ return this._index;
38
+ };
39
+ return StringParserInput;
40
+ }());
41
+ exports.StringParserInput = StringParserInput;
42
+ var ParserContext = /** @class */ (function () {
43
+ function ParserContext(_input, _whitespaceParser) {
44
+ if (_whitespaceParser === void 0) { _whitespaceParser = null; }
45
+ this._input = _input;
46
+ this._whitespaceParser = _whitespaceParser;
47
+ this.cutEncountered = false;
48
+ }
49
+ ParserContext.prototype.parseWhitespace = function () {
50
+ if (this._whitespaceParser) {
51
+ this._whitespaceParser.parse(this);
52
+ }
53
+ };
54
+ Object.defineProperty(ParserContext.prototype, "input", {
55
+ get: function () {
56
+ return this._input;
57
+ },
58
+ enumerable: false,
59
+ configurable: true
60
+ });
61
+ return ParserContext;
62
+ }());
63
+ exports.ParserContext = ParserContext;
64
+ var ParseResult;
65
+ (function (ParseResult) {
66
+ function successful(r) {
67
+ return { successful: true, failed: false, result: r };
68
+ }
69
+ ParseResult.successful = successful;
70
+ function voidSuccessful() {
71
+ return { successful: true, failed: false, result: undefined };
72
+ }
73
+ ParseResult.voidSuccessful = voidSuccessful;
74
+ function failed(r) {
75
+ return { successful: false, failed: true, parseError: r };
76
+ }
77
+ ParseResult.failed = failed;
78
+ function resultOrThrow(p) {
79
+ if (p.successful) {
80
+ return p.result;
81
+ }
82
+ else if (p.failed) {
83
+ throw p.parseError;
84
+ }
85
+ }
86
+ ParseResult.resultOrThrow = resultOrThrow;
87
+ })(ParseResult = exports.ParseResult || (exports.ParseResult = {}));
88
+ ;
89
+ function isParser(p) {
90
+ return 'parse' in p;
91
+ }
92
+ exports.isParser = isParser;
93
+ var FailParser = /** @class */ (function () {
94
+ function FailParser(_message) {
95
+ if (_message === void 0) { _message = null; }
96
+ this._message = _message;
97
+ }
98
+ FailParser.prototype.parse = function (parserContext) {
99
+ return ParseResult.failed(ParseError.parserRejected(this, parserContext, this._message));
100
+ };
101
+ return FailParser;
102
+ }());
103
+ exports.FailParser = FailParser;
104
+ var PassParser = /** @class */ (function () {
105
+ function PassParser() {
106
+ }
107
+ PassParser.prototype.parse = function (parserContext) {
108
+ return ParseResult.voidSuccessful();
109
+ };
110
+ return PassParser;
111
+ }());
112
+ exports.PassParser = PassParser;
113
+ var CutParser = /** @class */ (function () {
114
+ function CutParser() {
115
+ }
116
+ CutParser.prototype.parse = function (parserContext) {
117
+ parserContext.cutEncountered = true;
118
+ return ParseResult.voidSuccessful();
119
+ };
120
+ return CutParser;
121
+ }());
122
+ exports.CutParser = CutParser;
123
+ var RefParser = /** @class */ (function () {
124
+ function RefParser(_parserProvider) {
125
+ this._parserProvider = _parserProvider;
126
+ }
127
+ RefParser.prototype.parse = function (parserContext) {
128
+ if (!this._parser) {
129
+ this._parser = this._parserProvider();
130
+ }
131
+ return this._parser.parse(parserContext);
132
+ };
133
+ return RefParser;
134
+ }());
135
+ exports.RefParser = RefParser;
136
+ var ParserWithInternalWhitespaceSupport = /** @class */ (function () {
137
+ function ParserWithInternalWhitespaceSupport() {
138
+ this._whitespace = null;
139
+ }
140
+ ParserWithInternalWhitespaceSupport.prototype.parse = function (parserContext) {
141
+ throw new Error('Method not implemented');
142
+ };
143
+ ParserWithInternalWhitespaceSupport.prototype.whitespace = function (whitespaceParser) {
144
+ this._whitespace = whitespaceParser;
145
+ return this;
146
+ };
147
+ ParserWithInternalWhitespaceSupport.prototype.parseWhitespace = function (parserContext) {
148
+ if (this._whitespace) {
149
+ return this._whitespace.parse(parserContext);
150
+ }
151
+ else {
152
+ return ParseResult.voidSuccessful();
153
+ }
154
+ };
155
+ return ParserWithInternalWhitespaceSupport;
156
+ }());
157
+ exports.ParserWithInternalWhitespaceSupport = ParserWithInternalWhitespaceSupport;
158
+ function parse(parser, input, allowPartial) {
159
+ if (allowPartial === void 0) { allowPartial = false; }
160
+ if (typeof (input) === 'string') {
161
+ input = new StringParserInput(input);
162
+ }
163
+ var context = new ParserContext(input);
164
+ var ret = parser.parse(context);
165
+ if (!allowPartial && !input.eof()) {
166
+ throw new ParseError(input, input.getBookmark(), null, "End of input expected");
167
+ }
168
+ return ParseResult.resultOrThrow(ret);
169
+ }
170
+ exports.parse = parse;
171
+ var ParseError = /** @class */ (function () {
172
+ function ParseError(input, bookmark, parser, contentMessage) {
173
+ this.message = contentMessage;
174
+ if (bookmark) {
175
+ this.message = this.message + " at " + bookmark + " ('" + input.peek(5) + "')";
176
+ }
177
+ }
178
+ ParseError.prototype.toString = function () {
179
+ return "Error: " + this.message;
180
+ };
181
+ ParseError.parserRejected = function (parser, context, message) {
182
+ return new ParseError(context.input, context.input.getBookmark(), parser, message || "Parser rejected input");
183
+ };
184
+ return ParseError;
185
+ }());
186
+ exports.ParseError = ParseError;
@@ -0,0 +1,13 @@
1
+ import { AstBuilder } from "./combinators/AstBuilder";
2
+ import { MapParser } from "./combinators/MapParser";
3
+ import { OptionalCombinator } from "./combinators/OptionalCombinator";
4
+ import { ParserWithIndices } from "./combinators/ParserWithIndices";
5
+ import { Parser, ParserWithInternalWhitespaceSupport } from "./core";
6
+ export declare namespace ParserOperators {
7
+ function map<S, T>(mapper: (s: S) => T): (p: Parser<S>) => MapParser<Parser<S>, T>;
8
+ function optional<T>(): (p: Parser<T>) => OptionalCombinator<T>;
9
+ function build<Args extends [...any], Ctor extends new (...args: Args) => any>(ctor: Ctor): (p: Parser<Args>) => AstBuilder<Args, Ctor>;
10
+ function omit(): (p: Parser<unknown>) => MapParser<Parser<unknown>, void>;
11
+ function whitespace<T>(ws: Parser<unknown>): (p: ParserWithInternalWhitespaceSupport<T>) => ParserWithInternalWhitespaceSupport<T>;
12
+ function withIndices<T>(): (p: Parser<T>) => ParserWithIndices<T>;
13
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParserOperators = void 0;
4
+ var AstBuilder_1 = require("./combinators/AstBuilder");
5
+ var MapParser_1 = require("./combinators/MapParser");
6
+ var OptionalCombinator_1 = require("./combinators/OptionalCombinator");
7
+ var ParserWithIndices_1 = require("./combinators/ParserWithIndices");
8
+ var ParserOperators;
9
+ (function (ParserOperators) {
10
+ function map(mapper) {
11
+ return function (p) {
12
+ return new MapParser_1.MapParser(p, mapper);
13
+ };
14
+ }
15
+ ParserOperators.map = map;
16
+ function optional() {
17
+ return function (p) {
18
+ return new OptionalCombinator_1.OptionalCombinator(p);
19
+ };
20
+ }
21
+ ParserOperators.optional = optional;
22
+ function build(ctor) {
23
+ return function (p) {
24
+ return new AstBuilder_1.AstBuilder(p, ctor);
25
+ };
26
+ }
27
+ ParserOperators.build = build;
28
+ function omit() {
29
+ return function (p) {
30
+ return new MapParser_1.MapParser(p, function (a) { });
31
+ };
32
+ }
33
+ ParserOperators.omit = omit;
34
+ function whitespace(ws) {
35
+ return function (p) {
36
+ return p.whitespace(ws);
37
+ };
38
+ }
39
+ ParserOperators.whitespace = whitespace;
40
+ function withIndices() {
41
+ return function (p) {
42
+ return new ParserWithIndices_1.ParserWithIndices(p);
43
+ };
44
+ }
45
+ ParserOperators.withIndices = withIndices;
46
+ })(ParserOperators = exports.ParserOperators || (exports.ParserOperators = {}));
@@ -0,0 +1,9 @@
1
+ import { Parser, ParserContext, ParseResult } from "../core";
2
+ export declare class AnyOfParser implements Parser<string> {
3
+ private _characters;
4
+ private _minLen;
5
+ private _maxLen;
6
+ _charBitmap: number[] | null;
7
+ constructor(_characters: string, _minLen?: number | null, _maxLen?: number | null);
8
+ parse(parserContext: ParserContext): ParseResult<string>;
9
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnyOfParser = void 0;
4
+ var core_1 = require("../core");
5
+ var AnyOfParser = /** @class */ (function () {
6
+ function AnyOfParser(_characters, _minLen, _maxLen) {
7
+ if (_minLen === void 0) { _minLen = 1; }
8
+ if (_maxLen === void 0) { _maxLen = null; }
9
+ this._characters = _characters;
10
+ this._minLen = _minLen;
11
+ this._maxLen = _maxLen;
12
+ var charBitmap = [];
13
+ this._charBitmap = charBitmap;
14
+ for (var i = 0; i < _characters.length; i++) {
15
+ var ccode = _characters.charCodeAt(i);
16
+ if (ccode < 256) {
17
+ charBitmap[ccode >> 3] = (charBitmap[ccode >> 3] || 0) | (1 << (ccode & 7));
18
+ }
19
+ else {
20
+ this._charBitmap = null;
21
+ break;
22
+ }
23
+ }
24
+ }
25
+ AnyOfParser.prototype.parse = function (parserContext) {
26
+ var ret = [];
27
+ var cont = true;
28
+ var input = parserContext.input;
29
+ while (cont && !input.eof()) {
30
+ var str = input.peek(1);
31
+ if (this._charBitmap) {
32
+ var ccode = str.charCodeAt(0);
33
+ cont = (this._charBitmap[ccode >> 3] & (1 << (ccode & 7))) != 0;
34
+ }
35
+ else {
36
+ cont = this._characters.indexOf(str) >= 0;
37
+ }
38
+ if (cont) {
39
+ ret.push(str);
40
+ input.read(1);
41
+ }
42
+ }
43
+ if ((this._maxLen != null && ret.length > this._maxLen) ||
44
+ (this._minLen != null && ret.length < this._minLen)) {
45
+ return core_1.ParseResult.failed(core_1.ParseError.parserRejected(this, parserContext, "Expecting AnyOf " + this._characters));
46
+ }
47
+ return core_1.ParseResult.successful(ret.join(''));
48
+ };
49
+ return AnyOfParser;
50
+ }());
51
+ exports.AnyOfParser = AnyOfParser;
@@ -0,0 +1,6 @@
1
+ import { Parser, ParserContext, ParseResult } from "../core";
2
+ export declare class TokenParser implements Parser<string> {
3
+ private _token;
4
+ constructor(_token: String);
5
+ parse(parserContext: ParserContext): ParseResult<string>;
6
+ }