@ptolemy2002/rgx 8.1.0 → 9.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.
package/README.md CHANGED
@@ -17,6 +17,7 @@ Because there is so much to document, it has been broken up into multiple files.
17
17
  - [escapeRegex](./docs/util/escapeRegex.md) - The `escapeRegex` function, which escapes special regex characters in a given string and assures you that the result is valid Regex.
18
18
  - [regexMatchAtPosition](./docs/util/regexMatchAtPosition.md) - The `regexMatchAtPosition` function and related functions, which attempt to match a given regular expression at a specific position in a string.
19
19
  - [regexWithFlags](./docs/util/regexWithFlags.md) - The `regexWithFlags` function, which creates a new regular expression with the same source as a given regular expression but with different flags.
20
+ - [createRGXClassGuardFunction](./docs/util/createRGXClassGuardFunction.md) - The `createRGXClassGuardFunction` and `createAssertRGXClassGuardFunction` utilities for creating type guard and assertion functions for class instances.
20
21
  - `class` - A directory containing documentation for all classes in the library.
21
22
  - [collection](./docs/class/collection.md) - The `RGXTokenCollection` class, which is a collection of tokens.
22
23
  - [RGXError](./docs/class/RGXError.md) - Details on all custom error classes and the base `RGXError` class.
@@ -4,6 +4,7 @@ exports.rgxGroup = exports.RGXGroupToken = void 0;
4
4
  const collection_1 = require("../collection");
5
5
  const base_1 = require("./base");
6
6
  const internal_1 = require("../internal");
7
+ const utils_1 = require("../utils");
7
8
  const typeGuards_1 = require("../typeGuards");
8
9
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
9
10
  class RGXGroupToken extends base_1.RGXClassToken {
@@ -60,6 +61,6 @@ class RGXGroupToken extends base_1.RGXClassToken {
60
61
  }
61
62
  }
62
63
  exports.RGXGroupToken = RGXGroupToken;
63
- RGXGroupToken.check = (0, internal_1.createClassGuardFunction)(RGXGroupToken);
64
- RGXGroupToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXGroupToken);
64
+ RGXGroupToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXGroupToken);
65
+ RGXGroupToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXGroupToken);
65
66
  exports.rgxGroup = (0, internal_1.createConstructFunction)(RGXGroupToken);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rgxLookahead = exports.RGXLookaheadToken = void 0;
4
4
  const internal_1 = require("../internal");
5
+ const utils_1 = require("../utils");
5
6
  const lookaround_1 = require("./lookaround");
6
7
  const lookbehind_1 = require("./lookbehind");
7
8
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
@@ -27,6 +28,6 @@ class RGXLookaheadToken extends lookaround_1.RGXLookaroundToken {
27
28
  }
28
29
  }
29
30
  exports.RGXLookaheadToken = RGXLookaheadToken;
30
- RGXLookaheadToken.check = (0, internal_1.createClassGuardFunction)(RGXLookaheadToken);
31
- RGXLookaheadToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXLookaheadToken);
31
+ RGXLookaheadToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXLookaheadToken);
32
+ RGXLookaheadToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXLookaheadToken);
32
33
  exports.rgxLookahead = (0, internal_1.createConstructFunction)(RGXLookaheadToken);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rgxLookbehind = exports.RGXLookbehindToken = void 0;
4
4
  const internal_1 = require("../internal");
5
+ const utils_1 = require("../utils");
5
6
  const lookahead_1 = require("./lookahead");
6
7
  const lookaround_1 = require("./lookaround");
7
8
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
@@ -27,6 +28,6 @@ class RGXLookbehindToken extends lookaround_1.RGXLookaroundToken {
27
28
  }
28
29
  }
29
30
  exports.RGXLookbehindToken = RGXLookbehindToken;
30
- RGXLookbehindToken.check = (0, internal_1.createClassGuardFunction)(RGXLookbehindToken);
31
- RGXLookbehindToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXLookbehindToken);
31
+ RGXLookbehindToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXLookbehindToken);
32
+ RGXLookbehindToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXLookbehindToken);
32
33
  exports.rgxLookbehind = (0, internal_1.createConstructFunction)(RGXLookbehindToken);
@@ -7,6 +7,7 @@ const typeGuards_1 = require("../typeGuards");
7
7
  const errors_1 = require("../errors");
8
8
  const resolve_1 = require("../resolve");
9
9
  const internal_1 = require("../internal");
10
+ const utils_1 = require("../utils");
10
11
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
11
12
  const clone_1 = require("../clone");
12
13
  class RGXRepeatToken extends base_1.RGXClassToken {
@@ -98,6 +99,6 @@ class RGXRepeatToken extends base_1.RGXClassToken {
98
99
  }
99
100
  }
100
101
  exports.RGXRepeatToken = RGXRepeatToken;
101
- RGXRepeatToken.check = (0, internal_1.createClassGuardFunction)(RGXRepeatToken);
102
- RGXRepeatToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXRepeatToken);
102
+ RGXRepeatToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXRepeatToken);
103
+ RGXRepeatToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXRepeatToken);
103
104
  exports.rgxRepeat = (0, internal_1.createConstructFunction)(RGXRepeatToken);
@@ -5,6 +5,7 @@ const base_1 = require("./base");
5
5
  const typeGuards_1 = require("../typeGuards");
6
6
  const errors_1 = require("../errors");
7
7
  const internal_1 = require("../internal");
8
+ const utils_1 = require("../utils");
8
9
  class RGXSubpatternToken extends base_1.RGXClassToken {
9
10
  get pattern() {
10
11
  return this._pattern;
@@ -38,6 +39,6 @@ class RGXSubpatternToken extends base_1.RGXClassToken {
38
39
  }
39
40
  }
40
41
  exports.RGXSubpatternToken = RGXSubpatternToken;
41
- RGXSubpatternToken.check = (0, internal_1.createClassGuardFunction)(RGXSubpatternToken);
42
- RGXSubpatternToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXSubpatternToken);
42
+ RGXSubpatternToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXSubpatternToken);
43
+ RGXSubpatternToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXSubpatternToken);
43
44
  exports.rgxSubpattern = (0, internal_1.createConstructFunction)(RGXSubpatternToken);
@@ -7,6 +7,7 @@ const internal_1 = require("../internal");
7
7
  const collection_1 = require("../collection");
8
8
  const base_1 = require("./base");
9
9
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
10
+ const utils_1 = require("../utils");
10
11
  class RGXClassUnionToken extends base_1.RGXClassToken {
11
12
  constructor(tokens = []) {
12
13
  super();
@@ -52,8 +53,8 @@ class RGXClassUnionToken extends base_1.RGXClassToken {
52
53
  }
53
54
  }
54
55
  exports.RGXClassUnionToken = RGXClassUnionToken;
55
- RGXClassUnionToken.check = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
56
- RGXClassUnionToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
56
+ RGXClassUnionToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXClassUnionToken);
57
+ RGXClassUnionToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXClassUnionToken);
57
58
  function expandRgxUnionTokens(...tokens) {
58
59
  const result = new collection_1.RGXTokenCollection();
59
60
  for (const token of tokens) {
@@ -6,6 +6,7 @@ const typeGuards_1 = require("../typeGuards");
6
6
  const internal_1 = require("../internal");
7
7
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
8
8
  const clone_1 = require("../clone");
9
+ const utils_1 = require("../utils");
9
10
  class RGXClassWrapperToken extends base_1.RGXClassToken {
10
11
  constructor(token) {
11
12
  super();
@@ -33,6 +34,6 @@ class RGXClassWrapperToken extends base_1.RGXClassToken {
33
34
  }
34
35
  }
35
36
  exports.RGXClassWrapperToken = RGXClassWrapperToken;
36
- RGXClassWrapperToken.check = (0, internal_1.createClassGuardFunction)(RGXClassWrapperToken);
37
- RGXClassWrapperToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXClassWrapperToken);
37
+ RGXClassWrapperToken.check = (0, utils_1.createRGXClassGuardFunction)(RGXClassWrapperToken);
38
+ RGXClassWrapperToken.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXClassWrapperToken);
38
39
  exports.rgxClassWrapper = (0, internal_1.createConstructFunction)(RGXClassWrapperToken);
@@ -5,6 +5,7 @@ const resolve_1 = require("./resolve");
5
5
  const concat_1 = require("./concat");
6
6
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
7
7
  const internal_1 = require("./internal");
8
+ const utils_1 = require("./utils");
8
9
  class RGXTokenCollection {
9
10
  constructor(tokens = [], mode = 'concat') {
10
11
  this.tokens = [];
@@ -167,6 +168,6 @@ class RGXTokenCollection {
167
168
  }
168
169
  }
169
170
  exports.RGXTokenCollection = RGXTokenCollection;
170
- RGXTokenCollection.check = (0, internal_1.createClassGuardFunction)(RGXTokenCollection);
171
- RGXTokenCollection.assert = (0, internal_1.createAssertClassGuardFunction)(RGXTokenCollection);
171
+ RGXTokenCollection.check = (0, utils_1.createRGXClassGuardFunction)(RGXTokenCollection);
172
+ RGXTokenCollection.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXTokenCollection);
172
173
  exports.rgxTokenCollection = (0, internal_1.createConstructFunction)(RGXTokenCollection);
@@ -1,4 +1,4 @@
1
- export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'CONSTANT_CONFLICT' | 'INVALID_CONSTANT_KEY' | 'INSERTION_REJECTED' | 'REGEX_NOT_MATCHED_AT_POSITION' | 'PART_VALIDATION_FAILED' | 'INVALID_LEXER_MODE' | 'LEXEME_NOT_MATCHED_AT_POSITION' | 'INVALID_RGX_LEXER';
1
+ export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'CONSTANT_CONFLICT' | 'INVALID_CONSTANT_KEY' | 'INSERTION_REJECTED' | 'REGEX_NOT_MATCHED_AT_POSITION' | 'PART_VALIDATION_FAILED' | 'INVALID_LEXER_MODE' | 'LEXEME_NOT_MATCHED_AT_POSITION' | 'INVALID_RGX_LEXER' | 'INVALID_RGX_WALKER' | 'INVALID_RGX_PART';
2
2
  export declare class RGXError extends Error {
3
3
  _message: string;
4
4
  code: RGXErrorCode;
@@ -17,3 +17,5 @@ export * from './partValidationFailed';
17
17
  export * from './invalidLexerMode';
18
18
  export * from './lexemeNotMatchedAtPosition';
19
19
  export * from './invalidLexer';
20
+ export * from './invalidWalker';
21
+ export * from './invalidPart';
@@ -33,3 +33,5 @@ __exportStar(require("./partValidationFailed"), exports);
33
33
  __exportStar(require("./invalidLexerMode"), exports);
34
34
  __exportStar(require("./lexemeNotMatchedAtPosition"), exports);
35
35
  __exportStar(require("./invalidLexer"), exports);
36
+ __exportStar(require("./invalidWalker"), exports);
37
+ __exportStar(require("./invalidPart"), exports);
@@ -0,0 +1,7 @@
1
+ import { RGXError } from "./";
2
+ export declare class RGXInvalidPartError extends RGXError {
3
+ constructorName: string;
4
+ got: unknown;
5
+ constructor(message: string, got: unknown, constructorName?: string);
6
+ calcMessage(message: string): string;
7
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXInvalidPartError = void 0;
4
+ const errors_1 = require("./");
5
+ const class_1 = require("../class");
6
+ class RGXInvalidPartError extends errors_1.RGXError {
7
+ constructor(message, got, constructorName = "RGXPart") {
8
+ super(message, 'INVALID_RGX_PART');
9
+ this.name = 'RGXInvalidPartError';
10
+ this.got = got;
11
+ this.constructorName = constructorName;
12
+ }
13
+ calcMessage(message) {
14
+ const gotString = class_1.RGXClassToken.check(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
15
+ return `${message}; Expected: [instance of ${this.constructorName}]; Got: [${gotString}]`;
16
+ }
17
+ }
18
+ exports.RGXInvalidPartError = RGXInvalidPartError;
@@ -0,0 +1,7 @@
1
+ import { RGXError } from "./";
2
+ export declare class RGXInvalidWalkerError extends RGXError {
3
+ constructorName: string;
4
+ got: unknown;
5
+ constructor(message: string, got: unknown, constructorName?: string);
6
+ calcMessage(message: string): string;
7
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXInvalidWalkerError = void 0;
4
+ const errors_1 = require("./");
5
+ const class_1 = require("../class");
6
+ class RGXInvalidWalkerError extends errors_1.RGXError {
7
+ constructor(message, got, constructorName = "RGXWalker") {
8
+ super(message, 'INVALID_RGX_WALKER');
9
+ this.name = 'RGXInvalidWalkerError';
10
+ this.got = got;
11
+ this.constructorName = constructorName;
12
+ }
13
+ calcMessage(message) {
14
+ const gotString = class_1.RGXClassToken.check(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
15
+ return `${message}; Expected: [instance of ${this.constructorName}]; Got: [${gotString}]`;
16
+ }
17
+ }
18
+ exports.RGXInvalidWalkerError = RGXInvalidWalkerError;
package/dist/index.d.ts CHANGED
@@ -17,5 +17,5 @@ export * from "./walker";
17
17
  export * from "./lexer";
18
18
  export declare function rgxa(tokens: t.RGXToken[], flags?: string): ExtRegExp;
19
19
  export default function rgx(flags?: string, multiline?: boolean): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => ExtRegExp;
20
- export declare function rgxwa<R = unknown, T = unknown>(source: string, tokens: RGXTokenOrPart<R, T>[], options?: Omit<t.RGXWOptions<R>, "multiline">): RGXWalker<R>;
21
- export declare function rgxw<R = unknown, T = unknown>(source: string, { multiline, ...options }?: t.RGXWOptions<R>): (strings: TemplateStringsArray, ...tokens: RGXTokenOrPart<R, T>[]) => RGXWalker<R>;
20
+ export declare function rgxwa<R = unknown, S = unknown, T = unknown>(source: string, tokens: RGXTokenOrPart<R, S, T>[], options?: Omit<t.RGXWOptions<R, S>, "multiline">): RGXWalker<R, S>;
21
+ export declare function rgxw<R = unknown, S = unknown, T = unknown>(source: string, { multiline, ...options }?: t.RGXWOptions<R, S>): (strings: TemplateStringsArray, ...tokens: RGXTokenOrPart<R, S, T>[]) => RGXWalker<R, S>;
@@ -1,5 +1,4 @@
1
1
  export * from "./createConstructFunction";
2
- export * from "./createClassGuardFunction";
3
2
  export * from "./taggedTemplateToArray";
4
3
  export * from "./isConstructor";
5
4
  export * from "./getProxy";
@@ -15,7 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./createConstructFunction"), exports);
18
- __exportStar(require("./createClassGuardFunction"), exports);
19
18
  __exportStar(require("./taggedTemplateToArray"), exports);
20
19
  __exportStar(require("./isConstructor"), exports);
21
20
  __exportStar(require("./getProxy"), exports);
@@ -1,13 +1,14 @@
1
1
  import { RGXToken } from "../types";
2
2
  import { RGXTokenOrPart, RGXWalkerOptions } from "../walker";
3
- export type RGXLexemeDefinition<Data> = Readonly<({
3
+ export type RGXLexemeDefinition<Data, Share = unknown> = Readonly<({
4
4
  type: "resolve";
5
5
  token: RGXToken;
6
6
  } | {
7
7
  type: "walk";
8
- tokens: RGXTokenOrPart<Data>[];
9
- options?: Omit<RGXWalkerOptions<Data>, "startingSourcePosition" | "reduced"> & {
8
+ tokens: RGXTokenOrPart<Data, Share>[];
9
+ options?: Omit<RGXWalkerOptions<Data, Share>, "startingSourcePosition" | "reduced" | "share"> & {
10
10
  reduced?: (() => Data) | null;
11
+ share?: (() => Share) | null;
11
12
  };
12
13
  }) & {
13
14
  id: string;
@@ -20,23 +21,23 @@ export type RGXLexeme<Data> = {
20
21
  end: RGXLexemeLocation;
21
22
  data?: Data;
22
23
  };
23
- export type RGXLexemeDefinitions<Data> = Readonly<Record<string, ReadonlyArray<RGXLexemeDefinition<Data>>>>;
24
+ export type RGXLexemeDefinitions<Data, Share = unknown> = Readonly<Record<string, ReadonlyArray<RGXLexemeDefinition<Data, Share>>>>;
24
25
  export type RGXLexemeLocation = {
25
26
  index: number;
26
27
  line: number;
27
28
  column: number;
28
29
  };
29
30
  export declare function rgxLexemeLocationFromIndex(source: string, index: number): RGXLexemeLocation;
30
- export declare class RGXLexer<Data> {
31
+ export declare class RGXLexer<Data, Share = unknown> {
31
32
  readonly source: string;
32
33
  _position: number;
33
- readonly lexemeDefinitions: RGXLexemeDefinitions<Data>;
34
+ readonly lexemeDefinitions: RGXLexemeDefinitions<Data, Share>;
34
35
  readonly matched: RGXLexeme<Data>[];
35
- static check: (value: unknown) => value is RGXLexer<unknown>;
36
- static assert: (value: unknown) => asserts value is RGXLexer<unknown>;
36
+ static check: (value: unknown) => value is RGXLexer<unknown, unknown>;
37
+ static assert: (value: unknown) => asserts value is RGXLexer<unknown, unknown>;
37
38
  get position(): number;
38
39
  set position(value: number);
39
- constructor(source: string, lexemeDefinitions?: RGXLexemeDefinitions<Data>, startingPosition?: number);
40
+ constructor(source: string, lexemeDefinitions?: RGXLexemeDefinitions<Data, Share>, startingPosition?: number);
40
41
  private matchDefinition;
41
42
  private next;
42
43
  private expect;
@@ -56,4 +57,4 @@ export declare class RGXLexer<Data> {
56
57
  consumeWhile(predicate: (lexeme: RGXLexeme<Data>) => boolean, mode?: string, skipWhitespace?: boolean): RGXLexeme<Data>[];
57
58
  consumeUntil(predicate: (lexeme: RGXLexeme<Data>) => boolean, mode?: string, skipWhitespace?: boolean): RGXLexeme<Data>[];
58
59
  }
59
- export declare const rgxLexer: <Data>(...args: ConstructorParameters<typeof RGXLexer<Data>>) => RGXLexer<Data>;
60
+ export declare const rgxLexer: <Data, Share = unknown>(...args: ConstructorParameters<typeof RGXLexer<Data, Share>>) => RGXLexer<Data, Share>;
@@ -6,7 +6,7 @@ const resolve_1 = require("../resolve");
6
6
  const errors_1 = require("../errors");
7
7
  const walker_1 = require("../walker");
8
8
  const utils_1 = require("../utils");
9
- const internal_1 = require("../internal");
9
+ const utils_2 = require("../utils");
10
10
  const constants_1 = require("../constants");
11
11
  function rgxLexemeLocationFromIndex(source, index) {
12
12
  (0, errors_1.assertInRange)(index, { min: 0, max: source.length }, "index is outside the bounds of the source string");
@@ -52,10 +52,11 @@ class RGXLexer {
52
52
  // No data part for token lexemes.
53
53
  }
54
54
  else {
55
- const { tokens, options: { reduced, ...options } = {} } = lexemeDefinition;
55
+ const { tokens, options: { reduced, share, ...options } = {} } = lexemeDefinition;
56
56
  const walker = new walker_1.RGXWalker(this.source, tokens, {
57
57
  ...options,
58
58
  reduced: reduced?.() ?? undefined,
59
+ share: share?.() ?? undefined,
59
60
  startingSourcePosition: startPosition
60
61
  });
61
62
  // Since the walker will throw an error if it fails to match to the end of its tokens (assuming it isn't stopped before then), this is safe.
@@ -192,7 +193,7 @@ class RGXLexer {
192
193
  }
193
194
  }
194
195
  exports.RGXLexer = RGXLexer;
195
- RGXLexer.check = (0, internal_1.createClassGuardFunction)(RGXLexer);
196
- RGXLexer.assert = (0, internal_1.createAssertClassGuardFunction)(RGXLexer, (value, c) => new errors_1.RGXInvalidLexerError("Invalid Lexer", value, c.name));
196
+ RGXLexer.check = (0, utils_2.createRGXClassGuardFunction)(RGXLexer);
197
+ RGXLexer.assert = (0, utils_2.createAssertRGXClassGuardFunction)(RGXLexer, (value, c) => new errors_1.RGXInvalidLexerError("Invalid Lexer", value, c.name));
197
198
  const rgxLexer = (...args) => new RGXLexer(...args);
198
199
  exports.rgxLexer = rgxLexer;
package/dist/types.d.ts CHANGED
@@ -49,6 +49,6 @@ export type ValidRegexFlags = Branded<string, [ValidRegexFlagsBrandSymbol]> | Va
49
49
  export declare const validIdentifierSymbol: unique symbol;
50
50
  export type ValidIdentifierBrandSymbol = typeof validIdentifierSymbol;
51
51
  export type ValidIdentifier = Branded<string, [ValidIdentifierBrandSymbol]>;
52
- export type RGXWOptions<R = unknown> = RGXWalkerOptions<R> & {
52
+ export type RGXWOptions<R = unknown, S = unknown> = RGXWalkerOptions<R, S> & {
53
53
  multiline?: boolean;
54
54
  };
@@ -0,0 +1,3 @@
1
+ import { RGXError } from "../errors";
2
+ export declare function createRGXClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T): (value: unknown) => value is InstanceType<T>;
3
+ export declare function createAssertRGXClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T, constructError?: (value: unknown, constructor: T) => RGXError): (value: unknown) => asserts value is InstanceType<T>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRGXClassGuardFunction = createRGXClassGuardFunction;
4
+ exports.createAssertRGXClassGuardFunction = createAssertRGXClassGuardFunction;
5
+ const errors_1 = require("../errors");
6
+ function createRGXClassGuardFunction(constructor) {
7
+ return (value) => value instanceof constructor;
8
+ }
9
+ function createAssertRGXClassGuardFunction(constructor, constructError = (value, constructor) => new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: [`instance of ${constructor.name}`] }, value)) {
10
+ return (value) => {
11
+ if (!(value instanceof constructor)) {
12
+ throw constructError(value, constructor);
13
+ }
14
+ };
15
+ }
@@ -1,3 +1,4 @@
1
1
  export * from "./regexMatchAtPosition";
2
2
  export * from "./regexWithFlags";
3
3
  export * from "./normalizeRegexFlags";
4
+ export * from "./createRGXClassGuardFunction";
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./regexMatchAtPosition"), exports);
18
18
  __exportStar(require("./regexWithFlags"), exports);
19
19
  __exportStar(require("./normalizeRegexFlags"), exports);
20
+ __exportStar(require("./createRGXClassGuardFunction"), exports);
@@ -1,46 +1,48 @@
1
1
  import { CloneDepth } from "@ptolemy2002/immutability-utils";
2
2
  import { RGXToken } from "../types";
3
3
  import { RGXPart, RGXCapture } from "./part";
4
- export type RGXWalkerOptions<R> = {
4
+ export type RGXWalkerOptions<R, S = unknown> = {
5
5
  startingSourcePosition?: number;
6
6
  reduced?: R;
7
+ share?: S;
7
8
  infinite?: boolean;
8
9
  looping?: boolean;
9
10
  };
10
- export type RGXTokenOrPart<R, T = unknown> = RGXToken | RGXPart<R, T>;
11
- export declare class RGXWalker<R> {
11
+ export type RGXTokenOrPart<R, S = unknown, T = unknown> = RGXToken | RGXPart<R, S, T>;
12
+ export declare class RGXWalker<R, S = unknown> {
12
13
  readonly source: string;
13
14
  _sourcePosition: number;
14
- readonly tokens: RGXTokenOrPart<R>[];
15
+ readonly tokens: RGXTokenOrPart<R, S>[];
15
16
  _tokenPosition: number;
16
17
  reduced: R;
18
+ share: S;
17
19
  captures: RGXCapture[];
18
20
  namedCaptures: Record<string, RGXCapture[]>;
19
21
  infinite: boolean;
20
22
  looping: boolean;
21
23
  private _stopped;
22
- static check: (value: unknown) => value is RGXWalker<unknown>;
23
- static assert: (value: unknown) => asserts value is RGXWalker<unknown>;
24
+ static check: (value: unknown) => value is RGXWalker<unknown, unknown>;
25
+ static assert: (value: unknown) => asserts value is RGXWalker<unknown, unknown>;
24
26
  get sourcePosition(): number;
25
27
  set sourcePosition(value: number);
26
28
  get tokenPosition(): number;
27
29
  set tokenPosition(value: number);
28
30
  get stopped(): boolean;
29
- constructor(source: string, tokens: RGXTokenOrPart<R>[], options?: RGXWalkerOptions<R>);
31
+ constructor(source: string, tokens: RGXTokenOrPart<R, S>[], options?: RGXWalkerOptions<R, S>);
30
32
  stop(): this;
31
33
  atTokenEnd(): boolean;
32
- hasNextToken(predicate?: (token: RGXToken | RGXPart<R>) => boolean): boolean;
34
+ hasNextToken(predicate?: (token: RGXToken | RGXPart<R, S>) => boolean): boolean;
33
35
  atSourceEnd(): boolean;
34
36
  hasNextSource(predicate?: (rest: string) => boolean): boolean;
35
37
  lastCapture(): RGXCapture | null;
36
- currentToken(): RGXTokenOrPart<R, unknown>;
38
+ currentToken(): RGXTokenOrPart<R, S, unknown>;
37
39
  remainingSource(): string | null;
38
- capture(token: RGXTokenOrPart<R>, includeMatch: true): RegExpExecArray;
39
- capture(token: RGXTokenOrPart<R>, includeMatch?: false): string;
40
+ capture(token: RGXTokenOrPart<R, S>, includeMatch: true): RegExpExecArray;
41
+ capture(token: RGXTokenOrPart<R, S>, includeMatch?: false): string;
40
42
  step(): RGXCapture | null;
41
43
  stepToToken(predicate: (token: RGXTokenOrPart<R>) => boolean): this;
42
- stepToPart(predicate?: (part: RGXPart<R, unknown>) => boolean): this;
44
+ stepToPart(predicate?: (part: RGXPart<R, S, unknown>) => boolean): this;
43
45
  walk(): this;
44
- clone(depth?: CloneDepth): RGXWalker<R>;
46
+ clone(depth?: CloneDepth): RGXWalker<R, S>;
45
47
  }
46
- export declare function rgxWalker<R>(...args: ConstructorParameters<typeof RGXWalker<R>>): RGXWalker<R>;
48
+ export declare function rgxWalker<R, S = unknown>(...args: ConstructorParameters<typeof RGXWalker<R, S>>): RGXWalker<R, S>;
@@ -10,7 +10,7 @@ const resolve_1 = require("../resolve");
10
10
  const utils_1 = require("../utils");
11
11
  const typeGuards_1 = require("../typeGuards");
12
12
  const class_1 = require("../class");
13
- const internal_1 = require("../internal");
13
+ const utils_2 = require("../utils");
14
14
  function createBranchGroups(tokens) {
15
15
  if ((tokens instanceof collection_1.RGXTokenCollection && tokens.mode === "union") ||
16
16
  class_1.RGXClassUnionToken.check(tokens))
@@ -52,6 +52,7 @@ class RGXWalker {
52
52
  this.tokens = tokens;
53
53
  this.tokenPosition = 0;
54
54
  this.reduced = options.reduced ?? null;
55
+ this.share = options.share ?? null;
55
56
  this.infinite = options.infinite ?? false;
56
57
  this.looping = options.looping ?? false;
57
58
  }
@@ -107,7 +108,7 @@ class RGXWalker {
107
108
  let silent = false;
108
109
  // Ask Part what to do — control flow via return values, not flags.
109
110
  if (isPart) {
110
- const control = token.beforeCapture?.(token, this);
111
+ const control = token.beforeCapture?.(this.share, token, this);
111
112
  if (control === "stop") {
112
113
  this._stopped = true;
113
114
  return null;
@@ -127,7 +128,16 @@ class RGXWalker {
127
128
  branchedToken = createBranchGroups(token.token);
128
129
  else
129
130
  branchedToken = createBranchGroups(token);
130
- const capture = this.capture(branchedToken, true);
131
+ let capture;
132
+ try {
133
+ capture = this.capture(branchedToken, true);
134
+ }
135
+ catch (e) {
136
+ if (isPart && e instanceof errors_1.RGXRegexNotMatchedAtPositionError) {
137
+ token.afterFailure?.(e, this.share, token, this);
138
+ }
139
+ throw e;
140
+ }
131
141
  const raw = isPart ? token.rawTransform(capture[0]) : capture[0];
132
142
  const end = this.sourcePosition;
133
143
  const value = isPart ? token.transform(raw) : raw;
@@ -148,7 +158,15 @@ class RGXWalker {
148
158
  };
149
159
  // Validate the part. If validation fails, it will throw an error, so nothing below will run.
150
160
  if (isPart) {
151
- token.validate(captureResult, this);
161
+ try {
162
+ token.validate(captureResult, this.share, this);
163
+ }
164
+ catch (e) {
165
+ if (e instanceof errors_1.RGXPartValidationFailedError) {
166
+ token.afterValidationFailure?.(e, this.share, token, this);
167
+ }
168
+ throw e;
169
+ }
152
170
  }
153
171
  // Skip adding the capture if in silent mode.
154
172
  if (!silent) {
@@ -161,7 +179,7 @@ class RGXWalker {
161
179
  }
162
180
  // Notify Part after capture
163
181
  if (isPart) {
164
- token.afterCapture?.(captureResult, token, this);
182
+ token.afterCapture?.(captureResult, this.share, token, this);
165
183
  }
166
184
  if (!this.infinite || this.tokenPosition < this.tokens.length - 1) {
167
185
  this.tokenPosition++;
@@ -204,6 +222,7 @@ class RGXWalker {
204
222
  const clone = new RGXWalker(this.source, (0, immutability_utils_1.extClone)(this.tokens, (0, immutability_utils_1.depthDecrement)(1)), {
205
223
  startingSourcePosition: this.sourcePosition,
206
224
  reduced: (0, immutability_utils_1.extClone)(this.reduced, (0, immutability_utils_1.depthDecrement)(1)),
225
+ share: (0, immutability_utils_1.extClone)(this.share, (0, immutability_utils_1.depthDecrement)(1)),
207
226
  infinite: this.infinite,
208
227
  looping: this.looping
209
228
  });
@@ -214,8 +233,8 @@ class RGXWalker {
214
233
  }
215
234
  }
216
235
  exports.RGXWalker = RGXWalker;
217
- RGXWalker.check = (0, internal_1.createClassGuardFunction)(RGXWalker);
218
- RGXWalker.assert = (0, internal_1.createAssertClassGuardFunction)(RGXWalker);
236
+ RGXWalker.check = (0, utils_2.createRGXClassGuardFunction)(RGXWalker);
237
+ RGXWalker.assert = (0, utils_2.createAssertRGXClassGuardFunction)(RGXWalker, (value, constructor) => new errors_1.RGXInvalidWalkerError("Invalid Walker", value, constructor.name));
219
238
  function rgxWalker(...args) {
220
239
  return new RGXWalker(...args);
221
240
  }
@@ -1,6 +1,7 @@
1
1
  import { RGXToken } from "../types";
2
2
  import type { RGXWalker } from "./base";
3
3
  import { CloneDepth } from "@ptolemy2002/immutability-utils";
4
+ import { RGXPartValidationFailedError, RGXRegexNotMatchedAtPositionError } from "../errors";
4
5
  export type RGXPartControl = "skip" | "stop" | "silent" | void;
5
6
  export type RGXCapture<T = unknown> = {
6
7
  raw: string;
@@ -11,29 +12,33 @@ export type RGXCapture<T = unknown> = {
11
12
  branch: number;
12
13
  groups: Record<string, string> | null;
13
14
  };
14
- export type RGXPartOptions<R, T = string> = {
15
+ export type RGXPartOptions<R, S = unknown, T = string> = {
15
16
  id: string;
16
17
  rawTransform: (captured: string) => string;
17
18
  transform: (captured: string) => T;
18
- validate: (captured: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => boolean | string;
19
- beforeCapture: ((part: RGXPart<R, T>, walker: RGXWalker<R>) => RGXPartControl) | null;
20
- afterCapture: ((capture: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => void) | null;
19
+ validate: (captured: RGXCapture<T>, share: S, part: RGXPart<R, S, T>, walker: RGXWalker<R, S>) => boolean | string;
20
+ beforeCapture: ((share: S, part: RGXPart<R, S, T>, walker: RGXWalker<R, S>) => RGXPartControl) | null;
21
+ afterCapture: ((capture: RGXCapture<T>, share: S, part: RGXPart<R, S, T>, walker: RGXWalker<R, S>) => void) | null;
22
+ afterFailure: ((e: RGXRegexNotMatchedAtPositionError, share: S, part: RGXPart<R, S, T>, walker: RGXWalker<R, S>) => void) | null;
23
+ afterValidationFailure: ((e: RGXPartValidationFailedError, share: S, part: RGXPart<R, S, T>, walker: RGXWalker<R, S>) => void) | null;
21
24
  };
22
- export declare class RGXPart<R, T = string> {
25
+ export declare class RGXPart<R, S = unknown, T = string> {
23
26
  id: string | null;
24
27
  token: RGXToken;
25
- readonly rawTransform: RGXPartOptions<R, T>["rawTransform"];
26
- readonly transform: RGXPartOptions<R, T>["transform"];
28
+ readonly rawTransform: RGXPartOptions<R, S, T>["rawTransform"];
29
+ readonly transform: RGXPartOptions<R, S, T>["transform"];
27
30
  private readonly _validate;
28
- readonly beforeCapture: RGXPartOptions<R, T>["beforeCapture"];
29
- readonly afterCapture: RGXPartOptions<R, T>["afterCapture"];
30
- static check: (value: unknown) => value is RGXPart<unknown, unknown>;
31
- static assert: (value: unknown) => asserts value is RGXPart<unknown, unknown>;
32
- constructor(token: RGXToken, options?: Partial<RGXPartOptions<R, T>>);
33
- hasId(): this is RGXPart<R, T> & {
31
+ readonly beforeCapture: RGXPartOptions<R, S, T>["beforeCapture"];
32
+ readonly afterCapture: RGXPartOptions<R, S, T>["afterCapture"];
33
+ readonly afterFailure: RGXPartOptions<R, S, T>["afterFailure"];
34
+ readonly afterValidationFailure: RGXPartOptions<R, S, T>["afterValidationFailure"];
35
+ static check: (value: unknown) => value is RGXPart<unknown, unknown, unknown>;
36
+ static assert: (value: unknown) => asserts value is RGXPart<unknown, unknown, unknown>;
37
+ constructor(token: RGXToken, options?: Partial<RGXPartOptions<R, S, T>>);
38
+ hasId(): this is RGXPart<R, S, T> & {
34
39
  id: string;
35
40
  };
36
- validate(capture: RGXCapture<T>, walker: RGXWalker<R>): void;
37
- clone(depth?: CloneDepth): RGXPart<R, T>;
41
+ validate(capture: RGXCapture<T>, share: S, walker: RGXWalker<R, S>): void;
42
+ clone(depth?: CloneDepth): RGXPart<R, S, T>;
38
43
  }
39
- export declare function rgxPart<R, T = string>(...args: ConstructorParameters<typeof RGXPart<R, T>>): RGXPart<R, T>;
44
+ export declare function rgxPart<R, S = unknown, T = string>(...args: ConstructorParameters<typeof RGXPart<R, S, T>>): RGXPart<R, S, T>;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RGXPart = void 0;
4
4
  exports.rgxPart = rgxPart;
5
- const internal_1 = require("../internal");
5
+ const utils_1 = require("../utils");
6
6
  const clone_1 = require("../clone");
7
7
  const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
8
8
  const errors_1 = require("../errors");
@@ -15,12 +15,14 @@ class RGXPart {
15
15
  this._validate = options.validate ?? (() => true);
16
16
  this.beforeCapture = options.beforeCapture ?? null;
17
17
  this.afterCapture = options.afterCapture ?? null;
18
+ this.afterFailure = options.afterFailure ?? null;
19
+ this.afterValidationFailure = options.afterValidationFailure ?? null;
18
20
  }
19
21
  hasId() {
20
22
  return this.id !== null;
21
23
  }
22
- validate(capture, walker) {
23
- const result = this._validate(capture, this, walker);
24
+ validate(capture, share, walker) {
25
+ const result = this._validate(capture, share, this, walker);
24
26
  if (result === true)
25
27
  return;
26
28
  const message = typeof result === "string" ? result : "Part Validation Failed";
@@ -40,8 +42,8 @@ class RGXPart {
40
42
  }
41
43
  }
42
44
  exports.RGXPart = RGXPart;
43
- RGXPart.check = (0, internal_1.createClassGuardFunction)(RGXPart);
44
- RGXPart.assert = (0, internal_1.createAssertClassGuardFunction)(RGXPart);
45
+ RGXPart.check = (0, utils_1.createRGXClassGuardFunction)(RGXPart);
46
+ RGXPart.assert = (0, utils_1.createAssertRGXClassGuardFunction)(RGXPart, (value, constructor) => new errors_1.RGXInvalidPartError("Invalid Part", value, constructor.name));
45
47
  function rgxPart(...args) {
46
48
  return new RGXPart(...args);
47
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "8.1.0",
3
+ "version": "9.0.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,3 +0,0 @@
1
- import { RGXError } from "../errors";
2
- export declare function createClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T): (value: unknown) => value is InstanceType<T>;
3
- export declare function createAssertClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T, constructError?: (value: unknown, constructor: T) => RGXError): (value: unknown) => asserts value is InstanceType<T>;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createClassGuardFunction = createClassGuardFunction;
4
- exports.createAssertClassGuardFunction = createAssertClassGuardFunction;
5
- const errors_1 = require("../errors");
6
- function createClassGuardFunction(constructor) {
7
- return (value) => value instanceof constructor;
8
- }
9
- function createAssertClassGuardFunction(constructor, constructError = (value, constructor) => new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: [`instance of ${constructor.name}`] }, value)) {
10
- return (value) => {
11
- if (!(value instanceof constructor)) {
12
- throw constructError(value, constructor);
13
- }
14
- };
15
- }