@ptolemy2002/rgx 6.2.0 → 7.1.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
|
@@ -86,6 +86,7 @@ type RGXCapture<T = unknown> = {
|
|
|
86
86
|
value: T;
|
|
87
87
|
start: number;
|
|
88
88
|
end: number;
|
|
89
|
+
ownerId: string | null;
|
|
89
90
|
};
|
|
90
91
|
|
|
91
92
|
type RGXPartOptions<R, T=string> = {
|
|
@@ -663,8 +664,8 @@ constructor(source: string, tokens: RGXTokenCollectionInput, options?: RGXWalker
|
|
|
663
664
|
- `tokens` (`RGXTokenCollection`): The internal collection of tokens in 'concat' mode (readonly).
|
|
664
665
|
- `tokenPosition` (`number`): The current index in the token collection. Setting this validates that the value is >= 0 and <= `tokens.length`, throwing `RGXOutOfBoundsError` if not.
|
|
665
666
|
- `reduced` (`R`): A user-defined accumulator value, typically updated by `RGXPart` callbacks during walking.
|
|
666
|
-
- `captures` (`RGXCapture[]`): An array of structured capture results recorded during walking. Each entry has a `raw` string
|
|
667
|
-
- `namedCaptures` (`Record<string, RGXCapture>`): An object mapping capture IDs to their corresponding `RGXCapture` results. Only Parts with non-null IDs are included.
|
|
667
|
+
- `captures` (`RGXCapture[]`): An array of structured capture results recorded during walking. Each entry has a `raw` string, a `value` (the transform result for Parts, or the raw string for plain tokens), `start` and `end` indices in the source string, and an `ownerId` that is the `id` of the Part that produced it (or `null` for captures from plain tokens or parts without ids).
|
|
668
|
+
- `namedCaptures` (`Record<string, RGXCapture[]>`): An object mapping capture IDs to their corresponding `RGXCapture` results. Only Parts with non-null IDs are included. The captures occur in the same order as they appear in the `captures` array.
|
|
668
669
|
- `stopped` (`boolean`, readonly): Whether the walker has been stopped, either by a Part's `beforeCapture` returning `"stop"` or by calling `stop()` in an `afterCapture` callback.
|
|
669
670
|
|
|
670
671
|
#### Methods
|
|
@@ -1353,6 +1354,8 @@ A pre-built `RegExpFlagTransformer` that makes a regex pattern accent-insensitiv
|
|
|
1353
1354
|
- `o` / `O`: ó, ò, ö, ô, õ / Ó, Ò, Ö, Ô, Õ
|
|
1354
1355
|
- `u` / `U`: ú, ù, ü, û / Ú, Ù, Ü, Û
|
|
1355
1356
|
|
|
1357
|
+
Note that this transformer intentionally excludes replacing characters preceded by an odd number of backslashes, to allow for escaping. For example, in the pattern `\\a`, the `a` is preceded by two backslashes (an even number), so it will be replaced with `(a|á|à|ä|â|ã)`. In the pattern `\a`, the `a` is preceded by one backslash (an odd number), so it will not be replaced.
|
|
1358
|
+
|
|
1356
1359
|
#### Parameters
|
|
1357
1360
|
- `exp` (`RegExp`): The regular expression to transform.
|
|
1358
1361
|
|
|
@@ -1659,6 +1662,11 @@ Since these are defined as native tokens (strings), they are automatically wrapp
|
|
|
1659
1662
|
| `"non-word-char"` | `\W` | Any non-word character |
|
|
1660
1663
|
| `"backspace"` | `[\b]` | Backspace character |
|
|
1661
1664
|
|
|
1665
|
+
### Complex Constructs
|
|
1666
|
+
| Name | Resolves To | Description |
|
|
1667
|
+
| --- | --- | --- |
|
|
1668
|
+
| `"non-escape-bound"` | `(?<=(?<!\\)(?:\\\\)*)(?=[^\\]\|$)` | Matches a position that is not preceded by an odd number of backslashes, i.e., the next character is not escaped. Note that this doesn't match when the next character is a backslash, since allowing it to do that would cause non-escaped backslashes within a series of backslashes to be treated as escaped. For example, in the string `\\\a`, the first and third backslashes would be treated as escaped. |
|
|
1669
|
+
|
|
1662
1670
|
## Peer Dependencies
|
|
1663
1671
|
- `@ptolemy2002/immutability-utils` ^2.0.0
|
|
1664
1672
|
- `@ptolemy2002/js-utils` ^3.2.2
|
package/dist/constants.js
CHANGED
|
@@ -191,3 +191,12 @@ defineRGXConstant("backspace", {
|
|
|
191
191
|
return /[\b]/;
|
|
192
192
|
}
|
|
193
193
|
});
|
|
194
|
+
// Complex Constructs
|
|
195
|
+
// Put this before any pattern to ensure that the pattern is not escaped, i.e., not preceded by an odd number of backslashes.
|
|
196
|
+
defineRGXConstant("non-escape-bound", {
|
|
197
|
+
rgxGroupWrap: false,
|
|
198
|
+
rgxIsRepeatable: false,
|
|
199
|
+
toRgx() {
|
|
200
|
+
return /(?<=(?<!\\)(?:\\\\)*)(?=[^\\]|$)/;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.accentInsensitiveFlagTransformer = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const resolve_1 = require("../resolve");
|
|
4
6
|
const accentPatterns = [
|
|
5
7
|
"(a|á|à|ä|â|ã)", "(A|Á|À|Ä|Â|Ã)",
|
|
6
8
|
"(e|é|è|ë|ê)", "(E|É|È|Ë|Ê)",
|
|
@@ -11,9 +13,10 @@ const accentPatterns = [
|
|
|
11
13
|
const accentInsensitiveFlagTransformer = function (exp) {
|
|
12
14
|
let source = exp.source;
|
|
13
15
|
const flags = exp.flags;
|
|
16
|
+
const nonEscapeBound = (0, resolve_1.resolveRGXToken)((0, constants_1.rgxConstant)("non-escape-bound"));
|
|
14
17
|
accentPatterns.forEach((pattern) => {
|
|
15
18
|
// Replace any of the characters in the pattern with the pattern itself
|
|
16
|
-
source = source.replaceAll(new RegExp(pattern, "g"), pattern);
|
|
19
|
+
source = source.replaceAll(new RegExp(nonEscapeBound + pattern, "g"), pattern);
|
|
17
20
|
});
|
|
18
21
|
return new RegExp(source, flags);
|
|
19
22
|
};
|
package/dist/walker/base.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export declare class RGXWalker<R> implements RGXConvertibleToken {
|
|
|
13
13
|
_tokenPosition: number;
|
|
14
14
|
reduced: R;
|
|
15
15
|
captures: RGXCapture[];
|
|
16
|
-
namedCaptures: Record<string, RGXCapture>;
|
|
16
|
+
namedCaptures: Record<string, RGXCapture[]>;
|
|
17
17
|
private _stopped;
|
|
18
18
|
static check: (value: unknown) => value is RGXWalker<unknown>;
|
|
19
19
|
static assert: (value: unknown) => asserts value is RGXWalker<unknown>;
|
package/dist/walker/base.js
CHANGED
|
@@ -98,7 +98,7 @@ class RGXWalker {
|
|
|
98
98
|
const raw = this.capture(token);
|
|
99
99
|
const end = this.sourcePosition;
|
|
100
100
|
const value = isPart ? token.transform(raw) : raw;
|
|
101
|
-
const captureResult = { raw, value, start, end };
|
|
101
|
+
const captureResult = { raw, value, start, end, ownerId: isPart && token.hasId() ? token.id : null };
|
|
102
102
|
// Validate the part. If validation fails, it will throw an error, so nothing below will run.
|
|
103
103
|
if (isPart) {
|
|
104
104
|
token.validate(captureResult, this);
|
|
@@ -107,7 +107,9 @@ class RGXWalker {
|
|
|
107
107
|
if (!silent) {
|
|
108
108
|
this.captures.push(captureResult);
|
|
109
109
|
if (isPart && token.hasId()) {
|
|
110
|
-
|
|
110
|
+
if (!(token.id in this.namedCaptures))
|
|
111
|
+
this.namedCaptures[token.id] = [];
|
|
112
|
+
this.namedCaptures[token.id].push(captureResult);
|
|
111
113
|
}
|
|
112
114
|
}
|
|
113
115
|
// Notify Part after capture
|
package/dist/walker/part.d.ts
CHANGED