@ptolemy2002/rgx 12.7.2 → 12.7.4

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
  }
@@ -240,6 +247,9 @@ class RGXWalker {
240
247
  silent = dir === "silent";
241
248
  }
242
249
  const branchedToken = isPart ? createBranchGroups(token.token) : createBranchGroups(token);
250
+ // Still track the previous source position,
251
+ // because if we have to skip, we need to reset to it.
252
+ const prevSourcePosition = this.sourcePosition;
243
253
  let captureAttempt;
244
254
  try {
245
255
  captureAttempt = this.attemptCapture(branchedToken, isPart ? token : null);
@@ -256,7 +266,7 @@ class RGXWalker {
256
266
  if (e instanceof errors_1.RGXRegexNotMatchedAfterPositionError) {
257
267
  // If we're in infinite mode, we've reached the end before, and we're at the end now,
258
268
  // this is recoverable. Just stop the walker instead of throwing an error.
259
- if (this.infinite && this._didReachEnd && this.tokenPosition === this.tokens.length - 1) {
269
+ if (this.infinite && this._didReachEnd && this.atLastToken()) {
260
270
  this._stopped = true;
261
271
  return null;
262
272
  }
@@ -277,7 +287,7 @@ class RGXWalker {
277
287
  groups: captureAttempt.groups ?? null
278
288
  };
279
289
  if (isPart) {
280
- const dir = this.validateCapture(token, captureResult, start);
290
+ const dir = this.validateCapture(token, captureResult, prevSourcePosition);
281
291
  if (dir === "stop") {
282
292
  this._stopped = true;
283
293
  return null;
@@ -290,7 +300,7 @@ class RGXWalker {
290
300
  if (!silent)
291
301
  this.registerCapture(captureResult, token);
292
302
  if (isPart) {
293
- const dir = this.handleAfterCapture(token, captureResult, silent, start);
303
+ const dir = this.handleAfterCapture(token, captureResult, silent, prevSourcePosition);
294
304
  if (dir === "stop") {
295
305
  this.advanceToken();
296
306
  this._stopped = true;
@@ -318,7 +328,7 @@ class RGXWalker {
318
328
  stepToPart(predicate = () => true) {
319
329
  // If currently at a Part, step past it first so repeated
320
330
  // calls advance to the next Part rather than getting stuck.
321
- if (this.currentToken() instanceof part_1.RGXPart) {
331
+ if (this.hasNextToken() && this.currentToken() instanceof part_1.RGXPart) {
322
332
  this._stopped = false;
323
333
  this.step();
324
334
  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.4",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",