@ptolemy2002/rgx 12.7.2 → 12.7.3

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.
@@ -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' | 'REGEX_NOT_MATCHED_AFTER_POSITION' | 'PART_VALIDATION_FAILED' | 'INVALID_LEXER_MODE' | 'LEXEME_NOT_MATCHED_AT_POSITION' | 'INVALID_RGX_LEXER' | 'INVALID_RGX_WALKER' | 'INVALID_RGX_PART' | 'NOT_DIRECT_REGEXP';
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' | 'REGEX_NOT_MATCHED_AFTER_POSITION' | 'PART_VALIDATION_FAILED' | 'INVALID_LEXER_MODE' | 'LEXEME_NOT_MATCHED_AT_POSITION' | 'INVALID_RGX_LEXER' | 'INVALID_RGX_WALKER' | 'INVALID_RGX_PART' | 'NOT_DIRECT_REGEXP' | 'CURRENT_TOKEN_NOT_FOUND';
2
2
  export declare class RGXError extends Error {
3
3
  _message: string;
4
4
  code: RGXErrorCode;
@@ -0,0 +1,4 @@
1
+ import { RGXError } from "./base";
2
+ export declare class RGXCurrentTokenNotFoundError extends RGXError {
3
+ constructor(message: string);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXCurrentTokenNotFoundError = void 0;
4
+ const base_1 = require("./base");
5
+ class RGXCurrentTokenNotFoundError extends base_1.RGXError {
6
+ constructor(message) {
7
+ super(message, 'CURRENT_TOKEN_NOT_FOUND');
8
+ this.name = 'RGXCurrentTokenNotFoundError';
9
+ }
10
+ }
11
+ exports.RGXCurrentTokenNotFoundError = RGXCurrentTokenNotFoundError;
@@ -21,3 +21,4 @@ export * from './invalidLexer';
21
21
  export * from './invalidWalker';
22
22
  export * from './invalidPart';
23
23
  export * from './notDirectRegexp';
24
+ export * from './currentTokenNotFound';
@@ -37,3 +37,4 @@ __exportStar(require("./invalidLexer"), exports);
37
37
  __exportStar(require("./invalidWalker"), exports);
38
38
  __exportStar(require("./invalidPart"), exports);
39
39
  __exportStar(require("./notDirectRegexp"), exports);
40
+ __exportStar(require("./currentTokenNotFound"), exports);
@@ -35,7 +35,8 @@ export declare class RGXWalker<R, S = unknown> {
35
35
  constructor(source: string, tokens: RGXTokenOrPart<R, S>[], options?: RGXWalkerOptions<R, S>);
36
36
  stop(): this;
37
37
  atTokenEnd(): boolean;
38
- hasNextToken(predicate?: (token: RGXToken | RGXPart<R, S, unknown>) => boolean): boolean;
38
+ atLastToken(): boolean;
39
+ hasNextToken(predicate?: (token: RGXTokenOrPart<R, S, unknown>) => boolean): boolean;
39
40
  atSourceEnd(): boolean;
40
41
  hasNextSource(predicate?: (rest: string) => boolean): boolean;
41
42
  lastCapture(): RGXCapture | null;
@@ -11,6 +11,7 @@ const utils_1 = require("../utils");
11
11
  const typeGuards_1 = require("../typeGuards");
12
12
  const class_1 = require("../class");
13
13
  const utils_2 = require("../utils");
14
+ const currentTokenNotFound_1 = require("../errors/currentTokenNotFound");
14
15
  function createBranchGroups(tokens) {
15
16
  if ((tokens instanceof collection_1.RGXTokenCollection && tokens.mode === "union") ||
16
17
  class_1.RGXClassUnionToken.check(tokens))
@@ -71,6 +72,9 @@ class RGXWalker {
71
72
  atTokenEnd() {
72
73
  return this.tokenPosition >= this.tokens.length;
73
74
  }
75
+ atLastToken() {
76
+ return this.tokenPosition === this.tokens.length - 1;
77
+ }
74
78
  hasNextToken(predicate = () => true) {
75
79
  return !this.atTokenEnd() && predicate(this.currentToken());
76
80
  }
@@ -86,8 +90,11 @@ class RGXWalker {
86
90
  return this.captures[this.captures.length - 1];
87
91
  }
88
92
  currentToken() {
93
+ // Because an RGXToken might be null or undefined, this error is used
94
+ // to indicate there is no current token when one is expected.
95
+ // Previously, we just returned null.
89
96
  if (this.atTokenEnd())
90
- return null;
97
+ throw new currentTokenNotFound_1.RGXCurrentTokenNotFoundError("Token position is at the end of the token collection, no current token exists");
91
98
  return this.tokens[this.tokenPosition];
92
99
  }
93
100
  remainingSource() {
@@ -113,9 +120,9 @@ class RGXWalker {
113
120
  return includeMatch ? match : match[0];
114
121
  }
115
122
  advanceToken() {
116
- if (this.tokenPosition === this.tokens.length - 1)
123
+ if (this.atLastToken())
117
124
  this._didReachEnd = true;
118
- if (!this.infinite || this.tokenPosition < this.tokens.length - 1) {
125
+ if (!this.infinite || !this.atLastToken()) {
119
126
  this.tokenPosition++;
120
127
  if (this.looping && this.atTokenEnd()) {
121
128
  this.tokenPosition = 0;
@@ -123,7 +130,7 @@ class RGXWalker {
123
130
  }
124
131
  }
125
132
  determineBranch(capture) {
126
- for (let i = 0; i < capture.length - 1; i++) {
133
+ for (let i = 0; i < capture.length; i++) {
127
134
  if (capture.groups?.[`rgx_branch_${i}`] !== undefined)
128
135
  return i;
129
136
  }
@@ -256,7 +263,7 @@ class RGXWalker {
256
263
  if (e instanceof errors_1.RGXRegexNotMatchedAfterPositionError) {
257
264
  // If we're in infinite mode, we've reached the end before, and we're at the end now,
258
265
  // this is recoverable. Just stop the walker instead of throwing an error.
259
- if (this.infinite && this._didReachEnd && this.tokenPosition === this.tokens.length - 1) {
266
+ if (this.infinite && this._didReachEnd && this.atLastToken()) {
260
267
  this._stopped = true;
261
268
  return null;
262
269
  }
@@ -318,7 +325,7 @@ class RGXWalker {
318
325
  stepToPart(predicate = () => true) {
319
326
  // If currently at a Part, step past it first so repeated
320
327
  // calls advance to the next Part rather than getting stuck.
321
- if (this.currentToken() instanceof part_1.RGXPart) {
328
+ if (this.hasNextToken() && this.currentToken() instanceof part_1.RGXPart) {
322
329
  this._stopped = false;
323
330
  this.step();
324
331
  if (this._stopped)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "12.7.2",
3
+ "version": "12.7.3",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",