@zigsterz/parzing 1.0.0 → 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/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # Parzing: TypesSript Parser Combinator Library
2
2
 
3
3
  ## Overview
4
- This package implements a parser combinator system, allowing client code to easily create parsers in JavaScript or TypeScript. When used with TypeScript accurate types are computed for parsed and intermediate results, allowing quick and safe implementation and utilization of parsers.
4
+ This package is Parzing: a parser combinator library, allowing client code to easily create parsers in JavaScript or TypeScript. When used with TypeScript accurate types are computed for parsed and intermediate results, allowing easy and safe implementation and use of parsers.
5
+
6
+ This page provides instructions on how to use and customize Parzing. For more insights about the library, [see this blog](https://www.imonlydoingthis.benhaim.net/home/categories/parzing).
5
7
 
6
8
  ## Installation
7
9
 
@@ -13,7 +15,7 @@ npm install --save @zigsterz/parzing
13
15
 
14
16
  Parzing exposes a `parse` function for invoking a parser on content. To use it, we first construct a parser, and then pass the parser along with the content to parse. `parse` will either return a the result of succesfuly parsing the content, or throw an error describing a failure to parse.
15
17
 
16
- The `ParserBuilder` class exposes a set of helper factory functions for constructing parser.
18
+ The parser passed to `parse` is usually built using the `ParserBuilder` class. This class exposes a set of helper factory functions for constructing parsers.
17
19
 
18
20
  The example below demonstrates how to parse a sequence of 1 to 3 digits by first constructing a `ParserBuilder`, then using it to create an `AnyOfParser` parser and finally running the parser using `parse`.
19
21
 
@@ -29,11 +31,11 @@ assert(result == "123");
29
31
 
30
32
  ### Parsing Results
31
33
  The result of parsing content may be a value of any type. A Parzing parser has an associated result type that describes the type of the parsing result returned by that parser.
32
-
34
+ The return type from `parse` will match the result type of the parser passed to it.
33
35
 
34
36
  ### Basic Parsers
35
37
 
36
- `pb.anyOf` creates the Any Of *basic parser*. Basic parsers are the atomic building blocks for parsing. They may be combined using [*parser combinators*](#parser-combinators) to construct more complex parsers.
38
+ In the example above, `pb.anyOf` creates the Any Of *basic parser*. Basic parsers are the atomic building blocks for parsing. They may be combined using [*parser combinators*](#parser-combinators) to construct more complex parsers.
37
39
 
38
40
  Parzing offers the following basic parsers out of the box:
39
41
 
@@ -41,17 +43,18 @@ Parzing offers the following basic parsers out of the box:
41
43
  | ------------------ | ----------- | ----------- |
42
44
  | `ParserBuilder.anyOf(chars, min, max)` | Parses a minimum of *min* characters, and up to *max* characters, all out the characters listed in *chars*. | `string` |
43
45
  | `ParserBuilder.token(token)` | Parses the exact string specified by *token*. | `string` |
46
+ | `ParserBuilder.regex(regex)` | Parses the regular expression specified by *regex*. | `string` |
44
47
  | `ParserBuilder.pass()` | This is a no-op parser. It consumes no input and always succeeds. | `void` |
45
48
  | `ParserBuilder.fail(message)` | Fail parsing with the error message provided in *message*. | `void` |
46
49
 
47
- In addition to these parsers, you can create [custom parsers](#custom-parsers) to parse arbitrary complex "atoms". Customer parsers may provide any result type.
50
+ In addition to these parsers, you can create [custom parsers](#custom-parsers) to parse arbitrary complex "atoms". Custom parsers may provide any result type.
48
51
 
49
52
  ## Creating Complex Parsers
50
53
 
51
54
  ### Parser Combinators
52
- *Parser Combinators* are parsers that are constructed based on other parsers, and combine these parsers in some form to generate a more complex parser.
55
+ *Parser Combinators* are parsers constructed based on other parsers that combine these parsers in some form to generate a more complex parser.
53
56
 
54
- Perhaps the simplest example of a paser combinator is the Sequence combinator.
57
+ Perhaps the simplest example of a parser combinator is the Sequence combinator.
55
58
  The sequence combinator is constructed based on a sequence of underlying parsers, using the `ParserBuilder.sequence(...)` factory method.
56
59
  When parsing input content, the combinator will invoke each of the underlying parsers to parse consecutive fragments of the content.
57
60
  If any underlying parser fails, the sequence parser fails as well.
@@ -76,7 +79,7 @@ const result = parse(sample_parser, "1-2-3", true);
76
79
  assert.deepEqual(result, ["1", "-", "2", "-", "3"]);
77
80
  ```
78
81
 
79
- In addition to the `sequence` parser combinator, the following parser combinators offered by Parzing out-of-the box:
82
+ In addition to the `sequence` parser combinator, the following parser combinators are offered by Parzing out-of-the box:
80
83
 
81
84
  | Parser constructor | Description | Result type |
82
85
  | ------------------ | ----------- | ----------- |
@@ -91,13 +94,13 @@ In addition to the `sequence` parser combinator, the following parser combinator
91
94
 
92
95
  ### Whitespace Support
93
96
 
94
- Parsers that derive from `ParserWithInternalWhitespaceSupport` support ignoring whitespace within the content. Exactly where whitespace is ignored depends on the specific parser (see table below).
97
+ Parsers that derive from `ParserWithInternalWhitespaceSupport` support ignoring whitespace within parsed content. Exactly where whitespace is ignored depends on the specific parser as per the table below.
95
98
 
96
99
  For all of these parsers, the ignored "whitespace" is defined as content that can be parsed by the *whitespace parser*. The whitespace parser can be set by invoking `target_parser.whitespace(whitespace_parser)`.
97
- If this method is not invoked on the parser, and the parser was created using `ParserBuilder`, then the whitespace parser is set as the default whitespace parser for the builder. The default whitespace parser for a `ParserBuilder` can be set by passing it on construction.
98
-
99
100
  If there's no set whitespace parser on a `ParserWithInternalWhitespaceSupport`, no whitespace will be ignored.
100
101
 
102
+ If the `whitespace` method is not invoked on a parser, and the parser was created using `ParserBuilder`, then the whitespace parser is set as the default whitespace parser for the builder. The default whitespace parser for a `ParserBuilder` can be set by passing it on construction.
103
+
101
104
  The `WhitespaceParser` class implements a parser that accepts common whitespace patterns.
102
105
 
103
106
  The following code example illustrates a few ways to set whitespace parsers:
@@ -167,9 +170,10 @@ This parser will fail, but the failure will be reported by the `choice` combinat
167
170
  ParseError { message: 'Parser rejected input' }
168
171
  ```
169
172
 
170
- Clearly, for the input `number a123` a more approach would be to not even try the second alternative in the `choice` combinator above, and immediately bail out if we've encountered the `number` token.
171
- This kind of behavior can be achieved using cuts. A cut is a special parser, constructed using `ParserBuilder.cut`, that doesn't attempt to consume any input. Rather,
172
- When a cut 'parses', the fact that it was encountered is recored in the parsing context. Backtracking parsers, such as the ones listed above, will not attempt to backtrack parsing if a cut was encountered by one of their underlying parsers. Rather they will immediate fail with whatever failure that would have caused them to backtrack.
173
+ Clearly, for the input `number a123` a more reasonable behavior would be if the parser didn't even try the second alternative in the `choice` combinator above, and immediately bail out if we've encountered the `number` token. In Parzing, This kind of behavior can be achieved using *cuts*.
174
+
175
+ A cut is a special parser, constructed using `ParserBuilder.cut`, that doesn't attempt to consume any input. Rather,
176
+ When a cut 'parses', the fact that it was encountered is recored in the parsing context. Backtracking parsers, such as the ones listed above, will not attempt to backtrack parsing if a cut was encountered by one of their underlying parsers. Rather they will immediately fail with whatever failure that would have caused them to backtrack.
173
177
 
174
178
  Fixing the example above using cuts, we can write:
175
179
 
@@ -202,7 +206,7 @@ which would now result in the following exception:
202
206
  ParseError { message: "Expecting AnyOf 0123456789 at 7 ('a123')" }
203
207
  ```
204
208
 
205
- Clearly a more useful error message. Note that in addition to yielding clearer errors, cuts may also improve parsing performance since by preventing backtracks.
209
+ Clearly a more useful error message. Note that in addition to yielding clearer errors, cuts may also improve parsing performance by preventing backtracks.
206
210
 
207
211
  There are cases where you may want to reuse the same parser in different contexts -- where in some contexts you want the cut to appear but in others you want the cut to be ignored. This is achieved by invoking ``ParserBuilder.attempt`` on the parser which will return an ``AttemptCombinator``. This combinator parser will "swallow" any cut encountered indication within the underlying parser.
208
212
 
@@ -250,12 +254,14 @@ Parzing offers the following operators out of the box. Note that you can also cr
250
254
 
251
255
  ### Recursive Parsers
252
256
 
253
- In many cases a language may include recursive grammar definitions. Consider for example the following simple expresion parser grammer:
257
+ Grammars often include recursive definitions. Consider for example the following simple expresion parser grammer:
254
258
 
259
+ ```
255
260
  expression := addition | subtraction
256
261
  addition := term '+' term
257
262
  subtraction := term '-' term
258
263
  term := number | '(' expression ')'
264
+ ````
259
265
 
260
266
  How would we define this using Parzing?
261
267
 
@@ -289,9 +295,9 @@ const subtraction = pb.sequence(
289
295
  const expression = pb.choice(addition, subtraction);
290
296
  ```
291
297
 
292
- Note the comment "Ooops!" above. The recursive nature of the parser creates a circular declaration in Typescript, which is disallowed.
298
+ Note the comment "Ooops!" above. The recursive nature of the parser creates a circular declaration, which is disallowed in Typescript.
293
299
 
294
- The `ParserBuilder.ref` method allows coping with this situation by receiving a parameterless function returning a parser, and creating a parser that lazily resolves to the function's return value.
300
+ The `ParserBuilder.ref` method allows solving this problem by receiving a parameterless function returning a parser, and creating a parser that lazily resolves to the function's return value.
295
301
  Using this mechanism, our recursive parser becomes possible by modifying the code above as follows:
296
302
 
297
303
 
package/dist/builder.d.ts CHANGED
@@ -6,8 +6,9 @@ import { OptionalCombinator } from "./combinators/OptionalCombinator";
6
6
  import { SequenceCombinator } from "./combinators/SequenceCombinator";
7
7
  import { CutParser, FailParser, Parser, ParserType, PassParser, RefParser } from "./core";
8
8
  import { AnyOfParser } from "./parsers/AnyOfParser";
9
+ import { RegexParser } from "./parsers/RegexParser";
9
10
  import { TokenParser } from "./parsers/TokenParser";
10
- declare type WithPostfixSupport<T> = T & {
11
+ type WithPostfixSupport<T> = T & {
11
12
  _<R>(f: (target: WithPostfixSupport<T>) => R): WithPostfixSupport<R>;
12
13
  };
13
14
  export declare function addPostfixSupport<T>(who: T): WithPostfixSupport<T>;
@@ -17,7 +18,8 @@ export declare class ParserBuilder {
17
18
  parser<T extends Parser<any>>(p: T): WithPostfixSupport<T>;
18
19
  token(tok: string): WithPostfixSupport<TokenParser>;
19
20
  anyOf(alts: string, minLen?: number | null, maxLen?: number | null): WithPostfixSupport<AnyOfParser>;
20
- fail(message?: string): WithPostfixSupport<FailParser>;
21
+ regex(re: RegExp): WithPostfixSupport<RegexParser>;
22
+ fail(message: string): WithPostfixSupport<FailParser>;
21
23
  pass(): WithPostfixSupport<PassParser>;
22
24
  cut(): WithPostfixSupport<CutParser>;
23
25
  ref<T>(f: () => Parser<T>): WithPostfixSupport<RefParser<T>>;
@@ -25,7 +27,7 @@ export declare class ParserBuilder {
25
27
  map<V extends Parser<unknown>, T>(parser: V, mapper: (i: ParserType<V>) => T): WithPostfixSupport<MapParser<V, T>>;
26
28
  sequence<TS extends Parser<unknown>[]>(...s: TS): WithPostfixSupport<SequenceCombinator<TS>>;
27
29
  choice<T extends Parser<any>[]>(...parsers: T): ChooseCombinator<T>;
28
- many<T>(parser: Parser<T>, sep?: Parser<unknown> | null, min?: number, max?: number): WithPostfixSupport<ManyCombinator<T>>;
30
+ many<T>(parser: Parser<T>, sep?: Parser<unknown>, min?: number, max?: number): WithPostfixSupport<ManyCombinator<T>>;
29
31
  optional<T>(parser: Parser<T>): WithPostfixSupport<OptionalCombinator<T>>;
30
32
  private _ws;
31
33
  }
package/dist/builder.js CHANGED
@@ -1,88 +1,76 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ParserBuilder = exports.addPostfixSupport = void 0;
4
- var AttemptParser_1 = require("./combinators/AttemptParser");
5
- var ChooseCombinator_1 = require("./combinators/ChooseCombinator");
6
- var ManyCombinator_1 = require("./combinators/ManyCombinator");
7
- var MapParser_1 = require("./combinators/MapParser");
8
- var OptionalCombinator_1 = require("./combinators/OptionalCombinator");
9
- var SequenceCombinator_1 = require("./combinators/SequenceCombinator");
10
- var core_1 = require("./core");
11
- var AnyOfParser_1 = require("./parsers/AnyOfParser");
12
- var TokenParser_1 = require("./parsers/TokenParser");
3
+ exports.ParserBuilder = void 0;
4
+ exports.addPostfixSupport = addPostfixSupport;
5
+ const AttemptParser_1 = require("./combinators/AttemptParser");
6
+ const ChooseCombinator_1 = require("./combinators/ChooseCombinator");
7
+ const ManyCombinator_1 = require("./combinators/ManyCombinator");
8
+ const MapParser_1 = require("./combinators/MapParser");
9
+ const OptionalCombinator_1 = require("./combinators/OptionalCombinator");
10
+ const SequenceCombinator_1 = require("./combinators/SequenceCombinator");
11
+ const core_1 = require("./core");
12
+ const AnyOfParser_1 = require("./parsers/AnyOfParser");
13
+ const RegexParser_1 = require("./parsers/RegexParser");
14
+ const TokenParser_1 = require("./parsers/TokenParser");
13
15
  function addPostfixSupport(who) {
14
- var ret = who;
16
+ const ret = who;
15
17
  ret._ = function (f) {
16
18
  return addPostfixSupport(f(ret));
17
19
  };
18
20
  return ret;
19
21
  }
20
- exports.addPostfixSupport = addPostfixSupport;
21
- var ParserBuilder = /** @class */ (function () {
22
- function ParserBuilder(whitespaceParser) {
23
- if (whitespaceParser === void 0) { whitespaceParser = null; }
22
+ class ParserBuilder {
23
+ constructor(whitespaceParser = null) {
24
24
  this._ws = whitespaceParser;
25
25
  }
26
- ParserBuilder.prototype.postProcessParser = function (parser) {
27
- var p = parser;
26
+ postProcessParser(parser) {
27
+ let p = parser;
28
28
  if (p instanceof core_1.ParserWithInternalWhitespaceSupport && this._ws) {
29
29
  p = p.whitespace(this._ws);
30
30
  }
31
31
  return addPostfixSupport(p);
32
- };
33
- ParserBuilder.prototype.parser = function (p) {
32
+ }
33
+ parser(p) {
34
34
  return this.postProcessParser(p);
35
- };
36
- ParserBuilder.prototype.token = function (tok) {
35
+ }
36
+ token(tok) {
37
37
  return this.postProcessParser(new TokenParser_1.TokenParser(tok));
38
- };
39
- ParserBuilder.prototype.anyOf = function (alts, minLen, maxLen) {
40
- if (minLen === void 0) { minLen = 1; }
41
- if (maxLen === void 0) { maxLen = null; }
38
+ }
39
+ anyOf(alts, minLen = 1, maxLen = null) {
42
40
  return this.postProcessParser(new AnyOfParser_1.AnyOfParser(alts, minLen, maxLen));
43
- };
44
- ParserBuilder.prototype.fail = function (message) {
45
- if (message === void 0) { message = null; }
41
+ }
42
+ regex(re) {
43
+ return this.postProcessParser(new RegexParser_1.RegexParser(re));
44
+ }
45
+ fail(message) {
46
46
  return this.postProcessParser(new core_1.FailParser(message));
47
- };
48
- ParserBuilder.prototype.pass = function () {
47
+ }
48
+ pass() {
49
49
  return this.postProcessParser(new core_1.PassParser());
50
- };
51
- ParserBuilder.prototype.cut = function () {
50
+ }
51
+ cut() {
52
52
  return this.postProcessParser(new core_1.CutParser());
53
- };
54
- ParserBuilder.prototype.ref = function (f) {
53
+ }
54
+ ref(f) {
55
55
  return this.postProcessParser(new core_1.RefParser(f));
56
- };
57
- ParserBuilder.prototype.attempt = function (p) {
56
+ }
57
+ attempt(p) {
58
58
  return this.postProcessParser(new AttemptParser_1.AttemptParser(p));
59
- };
60
- ParserBuilder.prototype.map = function (parser, mapper) {
59
+ }
60
+ map(parser, mapper) {
61
61
  return this.postProcessParser(new MapParser_1.MapParser(parser, mapper));
62
- };
63
- ParserBuilder.prototype.sequence = function () {
64
- var s = [];
65
- for (var _i = 0; _i < arguments.length; _i++) {
66
- s[_i] = arguments[_i];
67
- }
62
+ }
63
+ sequence(...s) {
68
64
  return this.postProcessParser(new SequenceCombinator_1.SequenceCombinator(s));
69
- };
70
- ParserBuilder.prototype.choice = function () {
71
- var parsers = [];
72
- for (var _i = 0; _i < arguments.length; _i++) {
73
- parsers[_i] = arguments[_i];
74
- }
65
+ }
66
+ choice(...parsers) {
75
67
  return new ChooseCombinator_1.ChooseCombinator(parsers);
76
- };
77
- ParserBuilder.prototype.many = function (parser, sep, min, max) {
78
- if (sep === void 0) { sep = null; }
79
- if (min === void 0) { min = 0; }
80
- if (max === void 0) { max = 0; }
68
+ }
69
+ many(parser, sep, min = 0, max = 0) {
81
70
  return this.postProcessParser(new ManyCombinator_1.ManyCombinator(parser, sep, min, max));
82
- };
83
- ParserBuilder.prototype.optional = function (parser) {
71
+ }
72
+ optional(parser) {
84
73
  return this.postProcessParser(new OptionalCombinator_1.OptionalCombinator(parser));
85
- };
86
- return ParserBuilder;
87
- }());
74
+ }
75
+ }
88
76
  exports.ParserBuilder = ParserBuilder;
@@ -1,29 +1,18 @@
1
1
  "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
8
- }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.AstBuilder = void 0;
13
- var core_1 = require("../core");
14
- var AstBuilder = /** @class */ (function () {
15
- function AstBuilder(_parser, _ctor) {
4
+ const core_1 = require("../core");
5
+ class AstBuilder {
6
+ constructor(_parser, _ctor) {
16
7
  this._parser = _parser;
17
8
  this._ctor = _ctor;
18
9
  }
19
- AstBuilder.prototype.parse = function (parserContext) {
20
- var _a;
21
- var s = this._parser.parse(parserContext);
10
+ parse(parserContext) {
11
+ const s = this._parser.parse(parserContext);
22
12
  if (!s.successful) {
23
13
  return s;
24
14
  }
25
- return core_1.ParseResult.successful(new ((_a = this._ctor).bind.apply(_a, __spreadArray([void 0], s.result, false)))());
26
- };
27
- return AstBuilder;
28
- }());
15
+ return core_1.ParseResult.successful(new this._ctor(...s.result));
16
+ }
17
+ }
29
18
  exports.AstBuilder = AstBuilder;
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AttemptParser = void 0;
4
- var AttemptParser = /** @class */ (function () {
5
- function AttemptParser(_parser) {
4
+ class AttemptParser {
5
+ constructor(_parser) {
6
6
  this._parser = _parser;
7
7
  }
8
- AttemptParser.prototype.parse = function (parserContext) {
9
- var s = this._parser.parse(parserContext);
8
+ parse(parserContext) {
9
+ const s = this._parser.parse(parserContext);
10
10
  parserContext.cutEncountered = false;
11
11
  return s;
12
- };
13
- return AttemptParser;
14
- }());
12
+ }
13
+ }
15
14
  exports.AttemptParser = AttemptParser;
@@ -1,5 +1,5 @@
1
1
  import { Parser, ParserContext, ParseResult, ParserType } from "../core";
2
- declare type ChooseResult<E> = E extends [infer Head, ...infer Tails] ? ParserType<Head> | ChooseResult<Tails> : never;
2
+ type ChooseResult<E> = E extends [infer Head, ...infer Tails] ? ParserType<Head> | ChooseResult<Tails> : never;
3
3
  export declare class ChooseCombinator<E extends Parser<any>[]> implements Parser<ChooseResult<E>> {
4
4
  private _parsers;
5
5
  constructor(_parsers: E);
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ChooseCombinator = void 0;
4
- var core_1 = require("../core");
5
- var ChooseCombinator = /** @class */ (function () {
6
- function ChooseCombinator(_parsers) {
4
+ const core_1 = require("../core");
5
+ class ChooseCombinator {
6
+ constructor(_parsers) {
7
7
  this._parsers = _parsers;
8
8
  }
9
- ChooseCombinator.prototype.parse = function (parserContext) {
10
- var input = parserContext.input;
11
- var bm = input.getBookmark();
12
- for (var i = 0; i < this._parsers.length; i++) {
13
- var combOpt = this._parsers[i].parse(parserContext);
9
+ parse(parserContext) {
10
+ const input = parserContext.input;
11
+ const bm = input.getBookmark();
12
+ for (let i = 0; i < this._parsers.length; i++) {
13
+ const combOpt = this._parsers[i].parse(parserContext);
14
14
  if (combOpt.successful || parserContext.cutEncountered) {
15
15
  return combOpt;
16
16
  }
@@ -19,7 +19,6 @@ var ChooseCombinator = /** @class */ (function () {
19
19
  }
20
20
  }
21
21
  return core_1.ParseResult.failed(core_1.ParseError.parserRejected(this, parserContext));
22
- };
23
- return ChooseCombinator;
24
- }());
22
+ }
23
+ }
25
24
  exports.ChooseCombinator = ChooseCombinator;
@@ -1,9 +1,9 @@
1
1
  import { Parser, ParserContext, ParseResult, ParserWithInternalWhitespaceSupport } from "../core";
2
2
  export declare class ManyCombinator<T> extends ParserWithInternalWhitespaceSupport<T[]> {
3
3
  private _parser;
4
- private _sepParser;
4
+ private _sepParser?;
5
5
  private _min;
6
6
  private _max;
7
- constructor(_parser: Parser<T>, _sepParser?: Parser<unknown>, _min?: number, _max?: number);
7
+ constructor(_parser: Parser<T>, _sepParser?: Parser<unknown> | undefined, _min?: number, _max?: number);
8
8
  parse(parserContext: ParserContext): ParseResult<T[]>;
9
9
  }
@@ -1,45 +1,25 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
18
3
  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;
4
+ const core_1 = require("../core");
5
+ class ManyCombinator extends core_1.ParserWithInternalWhitespaceSupport {
6
+ constructor(_parser, _sepParser, _min = 0, _max = 0) {
7
+ super();
8
+ this._parser = _parser;
9
+ this._sepParser = _sepParser;
10
+ this._min = _min;
11
+ this._max = _max;
32
12
  }
33
- ManyCombinator.prototype.parse = function (parserContext) {
34
- var output = [];
35
- var pce = parserContext.cutEncountered;
36
- var mustParseElement = false;
13
+ parse(parserContext) {
14
+ const output = [];
15
+ const pce = parserContext.cutEncountered;
16
+ let mustParseElement = false;
37
17
  try {
38
18
  do {
39
19
  // Parse element
40
- var bm = parserContext.input.getBookmark();
20
+ let bm = parserContext.input.getBookmark();
41
21
  parserContext.cutEncountered = false;
42
- var psr = this._parser.parse(parserContext);
22
+ const psr = this._parser.parse(parserContext);
43
23
  if (psr.successful) {
44
24
  output.push(psr.result);
45
25
  }
@@ -55,7 +35,7 @@ var ManyCombinator = /** @class */ (function (_super) {
55
35
  mustParseElement = false;
56
36
  // Parse WS
57
37
  parserContext.cutEncountered = false;
58
- var wpr = this.parseWhitespace(parserContext);
38
+ let wpr = this.parseWhitespace(parserContext);
59
39
  if (!wpr.successful) {
60
40
  return wpr;
61
41
  }
@@ -63,7 +43,7 @@ var ManyCombinator = /** @class */ (function (_super) {
63
43
  if (this._sepParser) {
64
44
  parserContext.cutEncountered = false;
65
45
  bm = parserContext.input.getBookmark();
66
- var sepr = this._sepParser.parse(parserContext);
46
+ const sepr = this._sepParser.parse(parserContext);
67
47
  if (sepr.successful) {
68
48
  mustParseElement = true;
69
49
  // Parse WS post separator
@@ -84,9 +64,9 @@ var ManyCombinator = /** @class */ (function (_super) {
84
64
  }
85
65
  }
86
66
  } 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));
67
+ if (output.length < this._min ||
68
+ (this._max > 0 && output.length > this._max)) {
69
+ return core_1.ParseResult.failed(core_1.ParseError.parserRejected(this, parserContext, `Expected occurences in range {${this._min}, ${this._max}}; found ${output.length}`));
90
70
  }
91
71
  else {
92
72
  return core_1.ParseResult.successful(output);
@@ -95,7 +75,6 @@ var ManyCombinator = /** @class */ (function (_super) {
95
75
  finally {
96
76
  parserContext.cutEncountered = pce;
97
77
  }
98
- };
99
- return ManyCombinator;
100
- }(core_1.ParserWithInternalWhitespaceSupport));
78
+ }
79
+ }
101
80
  exports.ManyCombinator = ManyCombinator;
@@ -1,19 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MapParser = void 0;
4
- var core_1 = require("../core");
5
- var MapParser = /** @class */ (function () {
6
- function MapParser(_parser, _fn) {
4
+ const core_1 = require("../core");
5
+ class MapParser {
6
+ constructor(_parser, _fn) {
7
7
  this._parser = _parser;
8
8
  this._fn = _fn;
9
9
  }
10
- MapParser.prototype.parse = function (parserContext) {
11
- var s = this._parser.parse(parserContext);
10
+ parse(parserContext) {
11
+ const s = this._parser.parse(parserContext);
12
12
  if (!s.successful) {
13
13
  return s;
14
14
  }
15
15
  return core_1.ParseResult.successful(this._fn(s.result));
16
- };
17
- return MapParser;
18
- }());
16
+ }
17
+ }
19
18
  exports.MapParser = MapParser;
@@ -1,24 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OptionalCombinator = void 0;
4
- var core_1 = require("../core");
5
- var OptionalCombinator = /** @class */ (function () {
6
- function OptionalCombinator(_parser) {
4
+ const core_1 = require("../core");
5
+ class OptionalCombinator {
6
+ constructor(_parser) {
7
7
  this._parser = _parser;
8
8
  }
9
- OptionalCombinator.prototype.parse = function (parserContext) {
10
- var input = parserContext.input;
11
- var bm = input.getBookmark();
12
- var pce = parserContext.cutEncountered;
9
+ parse(parserContext) {
10
+ const input = parserContext.input;
11
+ const bm = input.getBookmark();
12
+ const pce = parserContext.cutEncountered;
13
13
  parserContext.cutEncountered = false;
14
- var ret = this._parser.parse(parserContext);
14
+ let ret = this._parser.parse(parserContext);
15
15
  if (!ret.successful && !parserContext.cutEncountered) {
16
16
  input.seekToBookmark(bm);
17
17
  ret = core_1.ParseResult.successful(null);
18
18
  }
19
19
  parserContext.cutEncountered = pce;
20
20
  return ret;
21
- };
22
- return OptionalCombinator;
23
- }());
21
+ }
22
+ }
24
23
  exports.OptionalCombinator = OptionalCombinator;
@@ -1,5 +1,5 @@
1
1
  import { Parser, ParserContext, ParseResult } from "../core";
2
- declare type ResultWithIndices<T> = {
2
+ type ResultWithIndices<T> = {
3
3
  result: T;
4
4
  start: number;
5
5
  length: number;
@@ -1,20 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ParserWithIndices = void 0;
4
- var core_1 = require("../core");
5
- var ParserWithIndices = /** @class */ (function () {
6
- function ParserWithIndices(_underlying) {
4
+ const core_1 = require("../core");
5
+ class ParserWithIndices {
6
+ constructor(_underlying) {
7
7
  this._underlying = _underlying;
8
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();
9
+ parse(parserContext) {
10
+ const startOfs = parserContext.input.tell();
11
+ const ret = this._underlying.parse(parserContext);
12
+ const endOfs = parserContext.input.tell();
13
13
  if (!ret.successful) {
14
14
  return ret;
15
15
  }
16
16
  return core_1.ParseResult.successful({ result: ret.result, start: startOfs, length: endOfs - startOfs });
17
- };
18
- return ParserWithIndices;
19
- }());
17
+ }
18
+ }
20
19
  exports.ParserWithIndices = ParserWithIndices;
@@ -1,6 +1,6 @@
1
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<{
2
+ export type FilterVoid<T> = T extends [infer Head, ...infer Rest] ? (Head extends void ? [...FilterVoid<Rest>] : [Head, ...FilterVoid<Rest>]) : T;
3
+ export type SeqType<TS extends Parser<unknown>[]> = FilterVoid<{
4
4
  [i in keyof TS]: ParserType<TS[i]>;
5
5
  }>;
6
6
  export declare class SequenceCombinator<TS extends Parser<unknown>[]> extends ParserWithInternalWhitespaceSupport<SeqType<TS>> {