regexp-ast-analysis
Advanced tools
Comparing version 0.2.4 to 0.3.0
# Changelog | ||
## 0.3.0 (2021-09-04) | ||
### Breaking | ||
- The `FirstLookChar`, `FirstFullyConsumedChar`, and `FirstPartiallyConsumedChar` interfaces are now immutable. | ||
### Added | ||
- Added transparent caching for all functions taking flags. | ||
- Added `FirstConsumedChars` and `FirstLookChars` namespaces. They contain methods for working with `FirstLookChar`s and `FirstConsumedChar`s. | ||
- Added `FollowOperations.continueOutside` to improve analysis inside lookaround assertions. | ||
- Added `FollowEndReason` type. | ||
- `followPaths` now supports alternatives as the start element. | ||
- The `getFirst{Consumed,}Char*` functions now supports alternatives as the after-this element. | ||
### Improved | ||
- The `getFirst{Consumed,}Char*` functions can now look outside of lookarounds to return better results. | ||
- Improved how the `exact` property of `FirstConsumedChar`s is determined. `getFirstConsumedChar` will now return better results. | ||
- Improved documentation. | ||
- Lots of internal refactoring and improvements. | ||
### Fixed | ||
- Fixed that `getFirstConsumedChar` sometimes returned partially consumed chars with trivially rejecting looks instead of a fully consumed char. | ||
## 0.2.4 (2021-08-12) | ||
@@ -4,0 +31,0 @@ |
267
index.d.ts
@@ -396,2 +396,67 @@ // Generated by dts-bundle-generator v5.9.0 | ||
/** | ||
* A cache that functions may use to store results. | ||
* | ||
* A cache implements the {@link ReadonlyFlags} interface. All functions that take a {@link ReadonlyFlags} objects can | ||
* be given a cache instead to utilize the cache. Example: | ||
* | ||
* ```js | ||
* const flags: ReadonlyFlags = getFlags(); | ||
* const cache = toCache(flags); | ||
* | ||
* toCharSet(element, flags); // uncached | ||
* toCharSet(element, cache); // cached | ||
* ``` | ||
* | ||
* Whether the cache is actually utilized depends on the implementation of the function. | ||
* | ||
* To get a cache for some flags, use the {@link toCache} function. | ||
* | ||
* ### Assumption | ||
* | ||
* Caches assume that the regexpp AST of cached nodes is immutable. If this assumption is broken, then the cache may | ||
* return old or incorrect results. | ||
* | ||
* The AST may be changed before the cache first sees a node of the AST and after the cached last sees a node of the | ||
* AST. Changes are allowed as long as the AST appears to be immutable from the perspective of the cache. | ||
* | ||
* ### Memory | ||
* | ||
* The cache uses regexpp `Node` objects as keys in | ||
* [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)s internally. | ||
* They will not cause memory leaks. | ||
* | ||
* This means that caches may out-live the nodes they cache information for. | ||
* | ||
* @see {@link toCache} | ||
* @see {@link createCache} | ||
*/ | ||
export interface Cache extends Required<ReadonlyFlags> { | ||
/** @internal */ | ||
readonly __cache?: never; | ||
} | ||
/** | ||
* This will create a new cache instance for the given flags. | ||
* | ||
* This operation will always create a new cache. If you want to transparently reuse cache instances, use | ||
* {@link toCache} instead. | ||
* | ||
* See {@link Cache} from more information about using caches. | ||
* | ||
* @see {@link Cache} | ||
* @see {@link toCache} | ||
*/ | ||
export declare function createCache(flags: ReadonlyFlags): Cache; | ||
/** | ||
* Returns a cache instance for the given flags. | ||
* | ||
* If the given flags are a cache instance, the cache instance will be returned. Otherwise a new cache instance will | ||
* be created using {@link createCache}. | ||
* | ||
* See {@link Cache} from more information about using caches. | ||
* | ||
* @see {@link Cache} | ||
* @see {@link createCache} | ||
*/ | ||
export declare function toCache(flags: ReadonlyFlags): Cache; | ||
/** | ||
* All possible element types that are accepted by {@link toCharSet}. | ||
@@ -471,2 +536,16 @@ * | ||
/** | ||
* The reason a path ends. | ||
* | ||
* Paths generally end because: | ||
* | ||
* 1. the {@link FollowOperations} do not wish to continue or | ||
* 2. because paths cannot be followed further because of the structure of the regex. | ||
* | ||
* This type describes the reasons for the second option. | ||
* | ||
* @see {@link FollowOperations} | ||
* @see {@link FollowOperations.endPath} | ||
*/ | ||
export declare type FollowEndReason = "pattern" | "assertion"; | ||
/** | ||
* A set of operations that determine how state is propagated and changed. | ||
@@ -495,10 +574,43 @@ * | ||
/** | ||
* This function is called when dealing to general lookarounds (it will __not__ be called for predefined assertion - | ||
* `^`, `$`, `\b`, `\B`). | ||
* This function is called when dealing with lookarounds. | ||
* | ||
* It will __not__ be called for predefined assertion - `^`, `$`, `\b`, `\B`. Use {@link FollowOperations.enter} or | ||
* {@link FollowOperations.leave} for predefined assertions instead. | ||
* | ||
* @default x => x | ||
*/ | ||
assert?: (state: S, direction: MatchingDirection, assertion: S, assertionDirection: MatchingDirection) => S; | ||
/** | ||
* This function is called when entering an element. | ||
* | ||
* Operations for elements are called in the following order: | ||
* | ||
* 1. {@link FollowOperations.enter} | ||
* 2. if {@link FollowOperations.continueInto} return `true` | ||
* 1. Element-specific operations (if any) that can change the current state. | ||
* 3. {@link FollowOperations.leave} | ||
* 4. {@link FollowOperations.continueAfter} (optional; might not be called for every element) | ||
* | ||
* @default (_, x) => x | ||
*/ | ||
enter?: (element: Element, state: S, direction: MatchingDirection) => S; | ||
/** | ||
* This function is called when leaving an element. | ||
* | ||
* See the documentation on {@link FollowOperations.enter} for more details. | ||
* | ||
* @default (_, x) => x | ||
*/ | ||
leave?: (element: Element, state: S, direction: MatchingDirection) => S; | ||
endPath?: (state: S, direction: MatchingDirection, reason: "pattern" | "assertion") => S; | ||
/** | ||
* This function is called when a path ends. | ||
* | ||
* Paths end at the end the patterns and assertions. It means that there is no element after the pattern/assertion | ||
* in that direction. | ||
* | ||
* @default x => x | ||
* @see {@link FollowEndReason} | ||
*/ | ||
endPath?: (state: S, direction: MatchingDirection, reason: FollowEndReason) => S; | ||
/** | ||
* Whether the current path should go into the given element (return `true`) or whether it should be skipped | ||
@@ -508,3 +620,7 @@ * (return `false`). If the element is skipped, the given state will not be changed and passed as-is to the `leave` | ||
* | ||
* You shouldn't modify state in this function. Modify state in the `enter` function instead. | ||
* You shouldn't modify state in this function. Modify state in {@link FollowOperations.enter} instead. | ||
* | ||
* See the documentation on {@link FollowOperations.enter} for more details. | ||
* | ||
* @default () => true | ||
*/ | ||
@@ -519,5 +635,30 @@ continueInto?: (element: Element, state: S, direction: MatchingDirection) => boolean; | ||
* | ||
* You shouldn't modify state in this function. Modify state in the `leave` function instead. | ||
* You shouldn't modify state in this function. Modify state in {@link FollowOperations.leave} instead. | ||
* | ||
* See the documentation on {@link FollowOperations.enter} for more details. | ||
* | ||
* @default () => true | ||
*/ | ||
continueAfter?: (element: Element, state: S, direction: MatchingDirection) => boolean; | ||
/** | ||
* Whether the current path should continue outside the given lookaround assertion. | ||
* | ||
* Paths that leave a lookaround assertions (= go outside of it) generally can't be followed. However, for some | ||
* operations it makes sense to do it anyway. | ||
* | ||
* It usually makes sense to follow paths outside of assertions if | ||
* `getMatchingDirectionFromAssertionKind(element.kind) !== direction`. This condition ensure that lookbehinds only | ||
* follow paths going out to the right (e.g. `(?<=a)->b`) and lookaheads only follow paths going out to the left | ||
* (e.g. `b<-(?=a)`). | ||
* | ||
* If this function returns `false`, {@link FollowOperations.endPath} is guaranteed to be called next. | ||
* If this function returns `true`, {@link FollowOperations.continueAfter} is guaranteed to be called next for the | ||
* lookaround assertion. | ||
* | ||
* You shouldn't modify state in this function. Modify state in {@link FollowOperations.endPath} or | ||
* {@link FollowOperations.enter} instead. | ||
* | ||
* @default () => false | ||
*/ | ||
continueOutside?: (element: LookaroundAssertion, state: S, direction: MatchingDirection) => boolean; | ||
} | ||
@@ -624,3 +765,3 @@ /** | ||
export declare function followPaths<S>( | ||
start: Element, | ||
start: Element | Alternative, | ||
startMode: "enter" | "next", | ||
@@ -659,8 +800,10 @@ initialState: S, | ||
* | ||
* - Accept all: The instance `{ char: all, edge: true }` (`edge` doesn't matter) is guaranteed to be equivalent to an | ||
* - Accept all: The instance `{ char: all, exact: true, edge: true }` is guaranteed to be equivalent to an | ||
* assertion that accepts all input strings (`(?=[\s\S]|$)`). | ||
* - Reject all: The instance `{ char: empty, edge: false }` (`edge` doesn't matter) is guaranteed to be equivalent to | ||
* - Reject all: The instance `{ char: empty, edge: false }` (`exact` doesn't matter) is guaranteed to be equivalent to | ||
* an assertion that rejects all input strings (`(?=[])`). | ||
* - Edge assertion: The instance `{ char: empty, edge: true }` (`edge` doesn't matter) is guaranteed to be equivalent | ||
* - Edge assertion: The instance `{ char: empty, edge: true }` (`exact` doesn't matter) is guaranteed to be equivalent | ||
* to an edge assertion (either `^` or `$`). | ||
* | ||
* @see {@link FirstLookChars} | ||
*/ | ||
@@ -674,20 +817,47 @@ export interface FirstLookChar { | ||
*/ | ||
char: CharSet; | ||
readonly char: CharSet; | ||
/** | ||
* If `true`, then the first character can be the start/end of the string. | ||
*/ | ||
edge: boolean; | ||
readonly edge: boolean; | ||
/** | ||
* If `true`, then `char` is guaranteed to be exactly the first character and not just a super set of it. | ||
*/ | ||
exact: boolean; | ||
readonly exact: boolean; | ||
} | ||
/** | ||
* This namespace contains methods for working with {@link FirstLookChar}s. | ||
*/ | ||
export declare namespace FirstLookChars { | ||
/** | ||
* Returns a {@link FirstLookChar} that is equivalent to a trivially accepting lookaround. | ||
* | ||
* The returned look is semantically equivalent to `(?=)` == `(?=[^]|$)` or `(?<=)` == `(?<=[^]|^)`. | ||
*/ | ||
function all(flags: ReadonlyFlags): FirstLookChar; | ||
/** | ||
* Returns a {@link FirstLookChar} that is equivalent to an assertion that only accepts the start/end of the input | ||
* string. | ||
* | ||
* The returned look is semantically equivalent to `$` == `(?=[]|$)` or `^` == `(?<=[]|^)`. | ||
*/ | ||
function edge(flags: ReadonlyFlags): FirstLookChar; | ||
/** | ||
* Converts the given {@link FirstLookChar} to a {@link FirstConsumedChar}. | ||
* | ||
* This is semantically equivalent to `(?=b|$)` -> `[]|(?=b|$)`. | ||
* | ||
* Note: This operation will typically return a {@link FirstPartiallyConsumedChar}. It will only return a | ||
* {@link FirstFullyConsumedChar} if the given `char` is empty and `edge: false`. This is because | ||
* `(?=[])` -> `[]|(?=[])` == `[]`. | ||
*/ | ||
function toConsumed(look: FirstLookChar): FirstConsumedChar; | ||
} | ||
/** | ||
* The first character consumed by some element. | ||
* | ||
* The first character can either be fully consumed or partially consumed. A fully consumed character means that all | ||
* input strings accepted by the element must start with this character. A partially consumed character means that the | ||
* element might not consumed characters. | ||
* The first character can either be fully consumed or partially consumed. | ||
* | ||
* @see {@link getFirstConsumedChar} | ||
* @see {@link FirstConsumedChars} | ||
*/ | ||
@@ -697,2 +867,4 @@ export declare type FirstConsumedChar = FirstFullyConsumedChar | FirstPartiallyConsumedChar; | ||
* This is equivalent to a regex fragment `[char]`. | ||
* | ||
* @see {@link FirstConsumedChar} | ||
*/ | ||
@@ -706,15 +878,17 @@ export interface FirstFullyConsumedChar { | ||
*/ | ||
char: CharSet; | ||
readonly char: CharSet; | ||
/** | ||
* If `true`, then the first character also includes the empty word. | ||
*/ | ||
empty: false; | ||
readonly empty: false; | ||
/** | ||
* If `true`, then `char` is guaranteed to be exactly the first character and not just a super set of it. | ||
*/ | ||
exact: boolean; | ||
readonly exact: boolean; | ||
} | ||
/** | ||
* This is equivalent to a regex fragment `[char]|(?=[look.char])` or `[char]|(?=[look.char]|$)` depending on | ||
* `look.edge`. | ||
* {@link FirstLookChar.edge}. | ||
* | ||
* @see {@link FirstConsumedChar} | ||
*/ | ||
@@ -728,17 +902,56 @@ export interface FirstPartiallyConsumedChar { | ||
*/ | ||
char: CharSet; | ||
readonly char: CharSet; | ||
/** | ||
* If `true`, then the first character also includes the empty word. | ||
*/ | ||
empty: true; | ||
readonly empty: true; | ||
/** | ||
* If `true`, then `char` is guaranteed to be exactly the first character and not just a super set of it. | ||
*/ | ||
exact: boolean; | ||
readonly exact: boolean; | ||
/** | ||
* A set of characters that may come after the consumed character | ||
*/ | ||
look: FirstLookChar; | ||
readonly look: FirstLookChar; | ||
} | ||
/** | ||
* This namespace contains methods for working with {@link FirstConsumedChar}s. | ||
*/ | ||
export declare namespace FirstConsumedChars { | ||
/** | ||
* Returns a {@link FirstConsumedChar} that is equivalent to the empty concatenation. | ||
*/ | ||
function emptyConcat(flags: ReadonlyFlags): FirstPartiallyConsumedChar; | ||
/** | ||
* Returns a {@link FirstConsumedChar} that is equivalent to the empty union (or empty set). | ||
*/ | ||
function emptyUnion(flags: ReadonlyFlags): FirstFullyConsumedChar; | ||
/** | ||
* Converts the given {@link FirstConsumedChar} to a {@link FirstLookChar}. | ||
* | ||
* This is conceptually equivalent to wrapping the given consumed character into a lookaround. | ||
* | ||
* This is semantically equivalent to `a|(?=b|$)` -> `(?=a|(?=b|$))` == `(?=[ab]|$)`. | ||
*/ | ||
function toLook(consumed: FirstConsumedChar): FirstLookChar; | ||
/** | ||
* Creates the union of all the given {@link FirstConsumedChar}s. | ||
* | ||
* The result is independent of the order in which the characters are given. | ||
*/ | ||
function union(chars: Iterable<FirstConsumedChar>, flags: ReadonlyFlags): FirstConsumedChar; | ||
/** | ||
* Creates the concatenation of all the given {@link FirstConsumedChar}s. | ||
* | ||
* The given char iterable is evaluated **lazily**. The implementation will try to iterate as few chars as possible. | ||
*/ | ||
function concat(chars: Iterable<FirstConsumedChar>, flags: ReadonlyFlags): FirstConsumedChar; | ||
/** | ||
* Makes the given consumed character optional. | ||
* | ||
* This is semantically equivalent to `a|(?=b|$)` -> `a?`. | ||
*/ | ||
function makeOptional(consumed: FirstConsumedChar): FirstPartiallyConsumedChar; | ||
} | ||
/** | ||
* If a character is returned, it guaranteed to be a super set of the actual character. If the given element is | ||
@@ -764,3 +977,3 @@ * always of zero length, then the empty character set will be returned. | ||
export declare function getFirstConsumedCharAfter( | ||
afterThis: Element, | ||
afterThis: Element | Alternative, | ||
direction: MatchingDirection, | ||
@@ -776,3 +989,3 @@ flags: ReadonlyFlags | ||
export declare function getFirstCharAfter( | ||
afterThis: Element, | ||
afterThis: Element | Alternative, | ||
direction: MatchingDirection, | ||
@@ -796,3 +1009,3 @@ flags: ReadonlyFlags | ||
export declare function getFirstConsumedCharAfterWithContributors( | ||
afterThis: Element, | ||
afterThis: Element | Alternative, | ||
direction: MatchingDirection, | ||
@@ -806,3 +1019,3 @@ flags: ReadonlyFlags | ||
export declare function getFirstCharAfterWithContributors( | ||
afterThis: Element, | ||
afterThis: Element | Alternative, | ||
direction: MatchingDirection, | ||
@@ -809,0 +1022,0 @@ flags: ReadonlyFlags |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("regexpp"),t=require("refa");function r(e,t){throw new Error(t||e)}function n(e,t){if(Array.isArray(e)){const r=e;return r.length>0&&r.every(t)}return t(e)}function a(e,t){return Array.isArray(e)?e.some(t):t(e)}function s(e){return n(e,o)}function o(e){switch(e.type){case"Alternative":return e.elements.every(o);case"Assertion":return!0;case"Character":case"CharacterClass":case"CharacterSet":return!1;case"Quantifier":return 0===e.max||o(e.element);case"Backreference":return x(e);case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(o);default:throw r(e)}}function c(e,t){return function e(n){switch(n.type){case"Alternative":return n.elements.every(e);case"Assertion":return!0;case"Backreference":return l(n,t);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return n.alternatives.some(e);case"Quantifier":return 0===n.min||e(n.element);default:throw r(n)}}(e)}function i(e){switch(e.type){case"Alternative":return e.elements.every(i);case"Assertion":return!1;case"Backreference":return x(e);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(i);case"Quantifier":return 0===e.max||i(e.element);default:throw r(e)}}function u(e){return function t(n){switch(n.type){case"Alternative":return n.elements.every(t);case"Assertion":return!1;case"Backreference":return l(n,e);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return n.alternatives.some(t);case"Quantifier":return 0===n.min||t(n.element);default:throw r(n)}}(e)}function l(e,t){return!!x(e)||!!h(e.resolved,(e=>e===t))&&(!y(e)||c(e.resolved,t))}function h(e,t){return"function"==typeof t?function(e,t){let r=e.parent;for(;r;){if(t(r))return!0;r=r.parent}return!1}(e,t):function(e,t){let r=e.parent;for(;r;){if(r===t)return!0;r=r.parent}return!1}(e,t)}function p(e,t,r){return"function"==typeof t?f(e,t,r):r?f(e,(e=>e===t),r):e===t||h(t,e)}function f(e,t,r){if(t(e))return!0;if(r&&!r(e))return!1;switch(e.type){case"Alternative":return e.elements.some((e=>f(e,t,r)));case"Assertion":return("lookahead"===e.kind||"lookbehind"===e.kind)&&e.alternatives.some((e=>f(e,t,r)));case"CapturingGroup":case"Group":case"Pattern":return e.alternatives.some((e=>f(e,t,r)));case"CharacterClass":return e.elements.some((e=>f(e,t,r)));case"CharacterClassRange":return f(e.min,t,r)||f(e.max,t,r);case"Quantifier":return f(e.element,t,r);case"RegExpLiteral":return f(e.pattern,t,r)||f(e.flags,t,r)}return!1}function d(e){switch(e.type){case"RegExpLiteral":return e.pattern;case"Pattern":return e;case"Flags":if(e.parent)return e.parent.pattern;throw new Error("Unable to find the pattern of flags without a RegExp literal.");default:{let t=e.parent;for(;"Pattern"!==t.type;)t=t.parent;return t}}}function m(e){let t;return h(e,(e=>"Assertion"===e.type&&(t=e,!0))),void 0===t||"lookahead"===t.kind?"ltr":"rtl"}function g(e){return"ltr"===e?"rtl":"ltr"}function C(e){return"end"===e||"lookahead"===e?"ltr":"rtl"}function x(e){const t=e.resolved,r=b(e,t);if(r===t)return!0;if("Alternative"!==r.type)return!0;const n=new Set;for(let t=e;t;t=t.parent)n.add(t);return!function e(t){const r=t.parent;switch(r.type){case"Alternative":{const a=r.elements.indexOf(t);let s;if(s="ltr"===m(t)?r.elements.slice(a+1):r.elements.slice(0,a),s.some((e=>n.has(e))))return!0;const o=r.parent;return"Pattern"!==o.type&&(("Assertion"!==o.type||!o.negate)&&e(o))}case"Quantifier":return e(r);default:throw new Error("What happened?")}}(t)||s(t)}function y(e){const t=e.resolved,r=b(e,t);if(r===t)return!1;if("Alternative"!==r.type)return!1;const n=new Set;for(let t=e;t;t=t.parent)n.add(t);return function e(t){const r=t.parent;switch(r.type){case"Alternative":{const a=r.elements.indexOf(t);let s;if(s="ltr"===m(t)?r.elements.slice(a+1):r.elements.slice(0,a),s.some((e=>n.has(e))))return!0;const o=r.parent;return"Pattern"!==o.type&&(("Assertion"!==o.type||!o.negate)&&(!(o.alternatives.length>1)&&e(o)))}case"Quantifier":return 0!==r.min&&e(r);default:throw new Error("What happened?")}}(t)}const v={min:0,max:0},k={min:1,max:1};function w(e){return Array.isArray(e)?A(e):S(e)}function A(e){let t=1/0,r=0;for(const n of e){const e=S(n);e&&(t=Math.min(t,e.min),r=Math.max(r,e.max))}return t>r?void 0:{min:t,max:r}}function S(e){switch(e.type){case"Assertion":return v;case"Character":case"CharacterClass":case"CharacterSet":return k;case"Quantifier":{if(0===e.max)return v;const t=S(e.element);return t?0===t.max?v:{min:t.min*e.min,max:t.max*e.max}:0===e.min?v:void 0}case"Alternative":{let t=0,r=0;for(const n of e.elements){const e=S(n);if(!e)return;t+=e.min,r+=e.max}return{min:t,max:r}}case"CapturingGroup":case"Group":return A(e.alternatives);case"Backreference":if(x(e))return v;{const t=S(e.resolved);return t?t.min>0&&!y(e)?{min:0,max:t.max}:t:y(e)?v:void 0}default:throw r(e)}}function b(e,t){if(e===t)return e;if(e.parent&&e.parent===t.parent)return e.parent;{const r=E(e),n=E(t);for(;;){if(0===r.length)return e;if(0===n.length)return t;if(r[r.length-1]!==n[n.length-1])break;r.pop(),n.pop()}const a=r[r.length-1].parent;if(a)return a;throw new Error("The two nodes are not part of the same tree.")}}function E(e){const t=[];for(let r=e;r;r=r.parent)t.push(r);return t}function G(e,r){const{positive:n,negated:a}=function(e){if(Array.isArray(e)){const t=e;if(function(e){for(let t=0,r=e.length;t<r;t++)if("CharacterClass"===e[t].type)return!1;return!0}(t))return{positive:t,negated:void 0};if(function(e){for(let t=0,r=e.length;t<r;t++){const r=e[t];if("CharacterClass"!==r.type||!r.negate)return!1}return!0}(t))return{positive:void 0,negated:t};{const e=[],r=[];for(let n=0,a=t.length;n<a;n++){const a=t[n];"CharacterClass"===a.type?a.negate?r.push(a):e.push(...a.elements):e.push(a)}return{positive:e,negated:r}}}{const t=e;return"CharacterClass"===t.type?t.negate?{positive:void 0,negated:[t]}:{positive:t.elements,negated:void 0}:{positive:[t],negated:void 0}}}(e);return a?n?t.JS.createCharSet(B(n),r).union(...a.map((e=>t.JS.createCharSet(B(e.elements),r).negate()))):1===a.length?t.JS.createCharSet(B(a[0].elements),r).negate():exports.Chars.empty(r).union(...a.map((e=>t.JS.createCharSet(B(e.elements),r).negate()))):n?t.JS.createCharSet(B(n),r):exports.Chars.empty(r)}function B(e){return e.map((e=>{switch(e.type){case"Character":return e.value;case"CharacterClassRange":return{min:e.min.value,max:e.max.value};case"CharacterSet":return e;default:throw r(e)}}))}function F(e,t){if(e==t)return!0;if(!e||!t||e.type!=t.type)return!1;switch(e.type){case"Alternative":{const r=t;return J(e.elements,r.elements)}case"Assertion":{const r=t;if(e.kind===r.kind){if("lookahead"===e.kind||"lookbehind"===e.kind){const r=t;return e.negate===r.negate&&J(e.alternatives,r.alternatives)}return e.raw===r.raw}return!1}case"Backreference":{const r=t;return x(e)?x(r):F(e.resolved,r.resolved)&&y(e)==y(r)}case"Character":{const r=t;return e.value===r.value}case"CharacterClass":{const r=t;return e.negate===r.negate&&J(e.elements,r.elements)}case"CharacterClassRange":{const r=t;return F(e.min,r.min)&&F(e.max,r.max)}case"CharacterSet":{const r=t;return"property"===e.kind&&"property"===r.kind?e.negate===r.negate&&e.key===r.key:e.raw===r.raw}case"Flags":{const r=t;return e.dotAll===r.dotAll&&e.global===r.global&&e.ignoreCase===r.ignoreCase&&e.multiline===r.multiline&&e.sticky===r.sticky&&e.unicode===r.unicode}case"CapturingGroup":case"Group":case"Pattern":{const r=t;return J(e.alternatives,r.alternatives)}case"Quantifier":{const r=t;return e.min===r.min&&e.max===r.max&&e.greedy===r.greedy&&F(e.element,r.element)}case"RegExpLiteral":{const r=t;return F(e.flags,r.flags)&&F(e.pattern,r.pattern)}default:throw r(e)}}function J(e,t){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!F(e[r],t[r]))return!1;return!0}function P(e,t,n,a,s){function o(e,t,r){var n,s;a.enter&&(t=a.enter(e,t,r));if(null===(s=null===(n=a.continueInto)||void 0===n?void 0:n.call(a,e,t,r))||void 0===s||s)switch(e.type){case"Assertion":if("lookahead"===e.kind||"lookbehind"===e.kind){const n=C(e.kind),s=a.join(e.alternatives.map((e=>c(e,W(a,t,r),n))),n);a.endPath&&(t=a.endPath(t,n,"assertion")),a.assert&&(t=a.assert(t,r,s,n))}break;case"Group":case"CapturingGroup":t=a.join(e.alternatives.map((e=>c(e,W(a,t,r),r))),r);break;case"Quantifier":0===e.max||(t=0===e.min?a.join([t,o(e.element,W(a,t,r),r)],r):o(e.element,t,r))}return a.leave&&(t=a.leave(e,t,r)),t}function c(e,t,r){var n,s;let c="ltr"===r?0:e.elements.length-1;const i="ltr"===r?1:-1;let u;for(;u=e.elements[c];c+=i){t=o(u,t,r);if(!(null===(s=null===(n=a.continueAfter)||void 0===n?void 0:n.call(a,u,t,r))||void 0===s||s))break}return t}return s||(s=m(e)),"enter"===t&&(n=o(e,n,s)),function(e,t,n){function s(e){var o,c;const i=e.parent;if("CharacterClass"===i.type||"CharacterClassRange"===i.type)throw new Error("The given element cannot be part of a character class.");if(!(null===(c=null===(o=a.continueAfter)||void 0===o?void 0:o.call(a,e,t,n))||void 0===c||c))return!1;if("Quantifier"===i.type)return i.max<=1?s(i):[i,s(i)];{const t=i.elements.indexOf(e)+("ltr"===n?1:-1),a=i.elements[t];if(a)return a;{const e=i.parent;if("Pattern"===e.type)return"pattern";if("Assertion"===e.type)return"assertion";if("CapturingGroup"===e.type||"Group"===e.type)return s(e);throw r(e)}}}for(;;){let r=s(e);for(;Array.isArray(r);){const[e,s]=r;t=a.join([t,o(e,W(a,t,n),n)],n),r=s}if(!1===r)return t;if("assertion"===r||"pattern"===r)return a.endPath&&(t=a.endPath(t,n,r)),t;t=o(r,t,n),e=r}}(e,n,s)}function W(e,t,r){return e.fork?e.fork(t,r):t}exports.Chars=void 0,function(e){const r=t.CharSet.empty(65535),n=t.CharSet.empty(1114111);e.empty=function(e){return e.unicode?n:r};const a=t.CharSet.all(65535),s=t.CharSet.all(1114111);e.all=function(e){return e.unicode?s:a};const o=t.JS.createCharSet([{kind:"any"}],{unicode:!1}).negate(),c=t.JS.createCharSet([{kind:"any"}],{unicode:!0}).negate();e.lineTerminator=function(e){return e.unicode?c:o};const i=t.JS.createCharSet([{kind:"word",negate:!1}],{unicode:!1}),u=t.JS.createCharSet([{kind:"word",negate:!1}],{unicode:!0,ignoreCase:!1}),l=t.JS.createCharSet([{kind:"word",negate:!1}],{unicode:!0,ignoreCase:!0});e.word=function(e){return e.unicode?e.ignoreCase?l:u:i};const h=t.JS.createCharSet([{kind:"digit",negate:!1}],{unicode:!1}),p=t.JS.createCharSet([{kind:"digit",negate:!1}],{unicode:!0});e.digit=function(e){return e.unicode?p:h};const f=t.JS.createCharSet([{kind:"space",negate:!1}],{unicode:!1}),d=t.JS.createCharSet([{kind:"space",negate:!1}],{unicode:!0});e.space=function(e){return e.unicode?d:f}}(exports.Chars||(exports.Chars={}));class Q{constructor(){this._currentWordBoundaries=[],this._ltrCache=new Map,this._rtlCache=new Map}isCurrentWordBoundary(e){return this._currentWordBoundaries.some((t=>t===e))}pushWordBoundary(e){this._currentWordBoundaries.push(e)}popWordBoundary(){this._currentWordBoundaries.pop()}getCached(e,t){return"ltr"===t?this._ltrCache.get(e):this._rtlCache.get(e)}setCached(e,t,r){"ltr"===t?this._ltrCache.set(e,r):this._rtlCache.set(e,r)}}function R(e,t,r,n){return D(e.map((e=>_(e,t,r,n))),r)}function _(e,t,n,a){let s=a.getCached(e,t);return void 0===s&&(s=function(e,t,n,a){switch(e.type){case"Assertion":return function(e,t,n,a){switch(e.kind){case"word":if(a.isCurrentWordBoundary(e))return s();{a.pushWordBoundary(e);const r=I(e,g(t),n,a);a.popWordBoundary();const o=exports.Chars.word(n);return r.edge?r.char.isDisjointWith(o)?i(e.negate):s():r.char.isDisjointWith(o)?i(e.negate):r.char.isSubsetOf(o)?i(!e.negate):s()}case"end":case"start":return C(e.kind)===t?n.multiline?c():o():s();case"lookahead":case"lookbehind":if(C(e.kind)===t){if(e.negate){if(p(e,(t=>t!==e&&"Assertion"===t.type)))return s();const r=R(e.alternatives,t,n,a),o=w(e.alternatives);return r.empty||!o?{char:exports.Chars.empty(n),empty:!1,exact:!0}:r.exact&&1===o.max?u({char:r.char.negate(),edge:!0,exact:!0}):s()}return u(T(R(e.alternatives,t,n,a)))}return s();default:throw r(e)}function s(){return u({char:exports.Chars.all(n),edge:!0,exact:!1})}function o(){return u(function(e){return{char:exports.Chars.empty(e),edge:!0,exact:!0}}(n))}function c(){return u({char:exports.Chars.lineTerminator(n),edge:!0,exact:!0})}function i(e){const t=exports.Chars.word(n);return u({char:e?t.negate():t,edge:e,exact:!0})}function u(e){return j(n,e)}}(e,t,n,a);case"Character":case"CharacterSet":case"CharacterClass":return{char:G(e,n),empty:!1,exact:!0};case"Quantifier":{if(0===e.max)return s();const r=_(e.element,t,n,a);return 0===e.min?D([s(),r],n):r}case"Alternative":{let r=e.elements;return"rtl"===t&&(r=[...r],r.reverse()),L(function*(){for(const e of r)yield _(e,t,n,a)}(),n)}case"CapturingGroup":case"Group":return R(e.alternatives,t,n,a);case"Backreference":{if(x(e))return s();const r=_(e.resolved,t,n,a);return r.exact=r.exact&&r.char.size<=1,y(e)?r:D([r,s()],n)}default:throw r(e)}function s(e){return j(n,e)}}(e,t,n,a),a.setCached(e,t,s)),s}function M(e){return{char:exports.Chars.all(e),edge:!0,exact:!0}}function j(e,t){return{char:exports.Chars.empty(e),empty:!0,exact:!0,look:null!=t?t:M(e)}}class O{constructor(e){this.char=e,this.exact=!0}add(e,t){!this.exact||t||this.char.isSupersetOf(e)?!this.exact&&t&&e.isSupersetOf(this.char)&&(this.exact=!0):this.exact=!1,this.char=this.char.union(e)}static emptyFromFlags(e){return new O(exports.Chars.empty(e))}static emptyFromMaximum(e){return new O(t.CharSet.empty(e))}}function D(e,t){const r=O.emptyFromFlags(t),n=[];for(const t of e)r.add(t.char,t.exact),t.empty&&n.push(t.look);if(n.length>0){const e=O.emptyFromFlags(t);let a=!1;for(const t of n)e.add(t.char,t.exact),a=a||t.edge;return{char:r.char,exact:r.exact,empty:!0,look:{char:e.char,exact:e.exact,edge:a}}}return{char:r.char,exact:r.exact,empty:!1}}function L(e,t){const r=O.emptyFromFlags(t);let n=M(t);for(const t of e){if(r.add(t.char.intersect(n.char),n.exact&&t.exact),!t.empty)return{char:r.char,exact:r.exact,empty:!1};{const e=n.char.intersect(t.look.char);n={char:e,exact:n.exact&&t.look.exact||e.isEmpty,edge:n.edge&&t.look.edge}}}return{char:r.char,exact:r.exact,empty:!0,look:n}}function T(e){if(e.empty){const t=O.emptyFromMaximum(e.char.maximum);return t.add(e.char,e.exact),t.add(e.look.char,e.look.exact),{char:t.char,exact:t.exact,edge:e.look.edge}}return{char:e.char,exact:e.exact,edge:!1}}function q(e,t,r,n){return P(e,"next",j(r),{join:e=>D(e,r),enter:(e,t,a)=>L([t,_(e,a,r,n)],r),continueInto:()=>!1,continueAfter:(e,t)=>t.empty},t)}function I(e,t,r,n){return T(q(e,t,r,n))}function N(e,t,r,n){return P(e,"next",{char:j(r),contributors:[]},{join(e){const t=new Set;return e.forEach((e=>e.contributors.forEach((e=>t.add(e))))),{char:D(e.map((e=>e.char)),r),contributors:[...t]}},enter(e,t,a){const s=_(e,a,r,n);return{char:L([t.char,s],r),contributors:[...t.contributors,e]}},continueInto:()=>!1,continueAfter:(e,t)=>t.char.empty},t)}exports.followPaths=P,exports.getCapturingGroupNumber=function(t){let r=0;try{throw e.visitRegExpAST(d(t),{onCapturingGroupEnter(e){if(r++,e===t)throw new Error}}),new Error("Unable to find the given capturing group in its parent pattern.")}catch(e){return r}},exports.getClosestAncestor=b,exports.getEffectiveMaximumRepetition=function(e){let t=1;for(let r=e.parent;r;r=r.parent)if("Quantifier"===r.type){if(t*=r.max,0===t)return 0}else if("Assertion"===r.type)break;return t},exports.getFirstCharAfter=function(e,t,r){return I(e,t,r,new Q)},exports.getFirstCharAfterWithContributors=function(e,t,r){return function(e,t,r,n){const{char:a,contributors:s}=N(e,t,r,n);return{char:T(a),contributors:s}}(e,t,r,new Q)},exports.getFirstConsumedChar=function(e,t,r){const n=new Q;return Array.isArray(e)?R(e,t,r,n):_(e,t,r,n)},exports.getFirstConsumedCharAfter=function(e,t,r){return q(e,t,r,new Q)},exports.getFirstConsumedCharAfterWithContributors=function(e,t,r){return N(e,t,r,new Q)},exports.getLengthRange=w,exports.getMatchingDirection=m,exports.getMatchingDirectionFromAssertionKind=C,exports.getPattern=d,exports.hasSomeAncestor=h,exports.hasSomeDescendant=p,exports.invertMatchingDirection=g,exports.isEmpty=function(e){return n(e,i)},exports.isEmptyBackreference=x,exports.isPotentiallyEmpty=function(e){return a(e,u)},exports.isPotentiallyZeroLength=function(e){return a(e,(e=>c(e,e)))},exports.isStrictBackreference=y,exports.isZeroLength=s,exports.matchesAllCharacters=function(e,r){return"Character"!==e.type&&("CharacterClassRange"===e.type?0===e.min.value&&e.max.value===(r.unicode?1114111:65535):"CharacterSet"===e.type?"property"===e.kind?t.JS.createCharSet([e],r).isAll:"any"===e.kind&&!!r.dotAll:!(!e.negate||0!==e.elements.length)||(e.negate?G(e.elements,r).isEmpty:G(e.elements,r).isAll))},exports.matchesNoCharacters=function(e,r){return"Character"!==e.type&&"CharacterClassRange"!==e.type&&("CharacterSet"===e.type?"property"===e.kind&&t.JS.createCharSet([e],r).isEmpty:!e.negate&&0===e.elements.length||(e.negate?G(e.elements,r).isAll:G(e.elements,r).isEmpty))},exports.structurallyEqual=F,exports.toCharSet=G; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("regexpp"),t=require("refa");function r(e,t){throw new Error(t||e)}exports.Chars=void 0,function(e){const r=t.CharSet.empty(65535),n=t.CharSet.empty(1114111);e.empty=function(e){return e.unicode?n:r};const a=t.CharSet.all(65535),s=t.CharSet.all(1114111);e.all=function(e){return e.unicode?s:a};const o=t.JS.createCharSet([{kind:"any"}],{unicode:!1}).negate(),i=t.JS.createCharSet([{kind:"any"}],{unicode:!0}).negate();e.lineTerminator=function(e){return e.unicode?i:o};const c=t.JS.createCharSet([{kind:"word",negate:!1}],{unicode:!1}),u=t.JS.createCharSet([{kind:"word",negate:!1}],{unicode:!0,ignoreCase:!1}),h=t.JS.createCharSet([{kind:"word",negate:!1}],{unicode:!0,ignoreCase:!0});e.word=function(e){return e.unicode?e.ignoreCase?h:u:c};const l=t.JS.createCharSet([{kind:"digit",negate:!1}],{unicode:!1}),p=t.JS.createCharSet([{kind:"digit",negate:!1}],{unicode:!0});e.digit=function(e){return e.unicode?p:l};const f=t.JS.createCharSet([{kind:"space",negate:!1}],{unicode:!1}),m=t.JS.createCharSet([{kind:"space",negate:!1}],{unicode:!0});e.space=function(e){return e.unicode?m:f}}(exports.Chars||(exports.Chars={}));const n=Array.isArray;class a{constructor(e){this._exactChars=e,this._inexactChars=e}get char(){return this._exactChars.union(this._inexactChars)}get exact(){return this._exactChars.isSupersetOf(this._inexactChars)}add(e){e.exact?this._exactChars=this._exactChars.union(e.char):this._inexactChars=this._inexactChars.union(e.char)}static fromFlags(e){return new a(exports.Chars.empty(e))}static fromMaximum(e){return new a(t.CharSet.empty(e))}}function s(e,t){const r=e.char.intersect(t.char);return{char:r,exact:e.exact&&t.exact||r.isEmpty}}function o(e,t){return n(e)?e.every(t):t(e)}function i(e,t){return n(e)?e.some(t):t(e)}function c(e){return o(e,u)}function u(e){switch(e.type){case"Alternative":return e.elements.every(u);case"Assertion":return!0;case"Character":case"CharacterClass":case"CharacterSet":return!1;case"Quantifier":return 0===e.max||u(e.element);case"Backreference":return v(e);case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(u);default:throw r(e)}}function h(e,t){return function e(n){switch(n.type){case"Alternative":return n.elements.every(e);case"Assertion":return!0;case"Backreference":return f(n,t);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return n.alternatives.some(e);case"Quantifier":return 0===n.min||e(n.element);default:throw r(n)}}(e)}function l(e){switch(e.type){case"Alternative":return e.elements.every(l);case"Assertion":return!1;case"Backreference":return v(e);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(l);case"Quantifier":return 0===e.max||l(e.element);default:throw r(e)}}function p(e){return function t(n){switch(n.type){case"Alternative":return n.elements.every(t);case"Assertion":return!1;case"Backreference":return f(n,e);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return n.alternatives.some(t);case"Quantifier":return 0===n.min||t(n.element);default:throw r(n)}}(e)}function f(e,t){return!!v(e)||!!m(e.resolved,(e=>e===t))&&(!w(e)||h(e.resolved,t))}function m(e,t){return"function"==typeof t?function(e,t){let r=e.parent;for(;r;){if(t(r))return!0;r=r.parent}return!1}(e,t):function(e,t){let r=e.parent;for(;r;){if(r===t)return!0;r=r.parent}return!1}(e,t)}function d(e,t,r){return"function"==typeof t?C(e,t,r):r?C(e,(e=>e===t),r):e===t||m(t,e)}function C(e,t,r){if(t(e))return!0;if(r&&!r(e))return!1;switch(e.type){case"Alternative":return e.elements.some((e=>C(e,t,r)));case"Assertion":return("lookahead"===e.kind||"lookbehind"===e.kind)&&e.alternatives.some((e=>C(e,t,r)));case"CapturingGroup":case"Group":case"Pattern":return e.alternatives.some((e=>C(e,t,r)));case"CharacterClass":return e.elements.some((e=>C(e,t,r)));case"CharacterClassRange":return C(e.min,t,r)||C(e.max,t,r);case"Quantifier":return C(e.element,t,r);case"RegExpLiteral":return C(e.pattern,t,r)||C(e.flags,t,r)}return!1}function x(e){switch(e.type){case"RegExpLiteral":return e.pattern;case"Pattern":return e;case"Flags":if(e.parent)return e.parent.pattern;throw new Error("Unable to find the pattern of flags without a RegExp literal.");default:{let t=e.parent;for(;"Pattern"!==t.type;)t=t.parent;return t}}}function g(e){let t;return m(e,(e=>"Assertion"===e.type&&(t=e,!0))),void 0===t||"lookahead"===t.kind?"ltr":"rtl"}function y(e){return"ltr"===e?"rtl":"ltr"}function k(e){return"end"===e||"lookahead"===e?"ltr":"rtl"}function v(e){const t=e.resolved,r=E(e,t);if(r===t)return!0;if("Alternative"!==r.type)return!0;const n=new Set;for(let t=e;t;t=t.parent)n.add(t);return!function e(t){const r=t.parent;switch(r.type){case"Alternative":{const a=r.elements.indexOf(t);let s;if(s="ltr"===g(t)?r.elements.slice(a+1):r.elements.slice(0,a),s.some((e=>n.has(e))))return!0;const o=r.parent;return"Pattern"!==o.type&&(("Assertion"!==o.type||!o.negate)&&e(o))}case"Quantifier":return e(r)}}(t)||c(t)}function w(e){const t=e.resolved,r=E(e,t);if(r===t)return!1;if("Alternative"!==r.type)return!1;const n=new Set;for(let t=e;t;t=t.parent)n.add(t);return function e(t){const r=t.parent;switch(r.type){case"Alternative":{const a=r.elements.indexOf(t);let s;if(s="ltr"===g(t)?r.elements.slice(a+1):r.elements.slice(0,a),s.some((e=>n.has(e))))return!0;const o=r.parent;return"Pattern"!==o.type&&(("Assertion"!==o.type||!o.negate)&&(!(o.alternatives.length>1)&&e(o)))}case"Quantifier":return 0!==r.min&&e(r)}}(t)}const S={min:0,max:0},A={min:1,max:1};function F(e){return n(e)?b(e):L(e)}function b(e){let t=1/0,r=0;for(const n of e){const e=L(n);e&&(t=Math.min(t,e.min),r=Math.max(r,e.max))}return t>r?void 0:{min:t,max:r}}function L(e){switch(e.type){case"Assertion":return S;case"Character":case"CharacterClass":case"CharacterSet":return A;case"Quantifier":{if(0===e.max)return S;const t=L(e.element);return t?0===t.max?S:{min:t.min*e.min,max:t.max*e.max}:0===e.min?S:void 0}case"Alternative":{let t=0,r=0;for(const n of e.elements){const e=L(n);if(!e)return;t+=e.min,r+=e.max}return{min:t,max:r}}case"CapturingGroup":case"Group":return b(e.alternatives);case"Backreference":if(v(e))return S;{const t=L(e.resolved);return t?t.min>0&&!w(e)?{min:0,max:t.max}:t:w(e)?S:void 0}default:throw r(e)}}function E(e,t){if(e===t)return e;if(e.parent&&e.parent===t.parent)return e.parent;{const r=_(e),n=_(t);for(;;){if(0===r.length)return e;if(0===n.length)return t;if(r[r.length-1]!==n[n.length-1])break;r.pop(),n.pop()}const a=r[r.length-1].parent;if(a)return a;throw new Error("The two nodes are not part of the same tree.")}}function _(e){const t=[];for(let r=e;r;r=r.parent)t.push(r);return t}function G(e){return new B(e)}class B{constructor(e){this.toCharSet=new WeakMap,this.getFirstConsumedCharLTR=new WeakMap,this.getFirstConsumedCharRTL=new WeakMap,this.dotAll=!!e.dotAll,this.global=!!e.global,this.hasIndices=!!e.hasIndices,this.ignoreCase=!!e.ignoreCase,this.multiline=!!e.multiline,this.sticky=!!e.sticky,this.unicode=!!e.unicode}}function W(e,r){if(!n(e))return O(e,r);if(1===e.length)return O(e[0],r);const{positive:a,negated:s}=function(e){const t=[],r=[];for(const n of e)"CharacterClass"===n.type?n.negate?r.push(n):t.push(...n.elements):t.push(n);return{positive:t,negated:r}}(e);return s.length?a.length?t.JS.createCharSet(J(a),r).union(...s.map((e=>t.JS.createCharSet(J(e.elements),r).negate()))):1===s.length?t.JS.createCharSet(J(s[0].elements),r).negate():exports.Chars.empty(r).union(...s.map((e=>t.JS.createCharSet(J(e.elements),r).negate()))):a.length?t.JS.createCharSet(J(a),r):exports.Chars.empty(r)}function O(e,t){if(t instanceof B){let r=t.toCharSet.get(e);return void 0===r&&(r=R(e,t),t.toCharSet.set(e,r)),r}return R(e,t)}function R(e,r){if("CharacterClass"===e.type){const n=t.JS.createCharSet(J(e.elements),r);return e.negate?n.negate():n}return t.JS.createCharSet([P(e)],r)}function J(e){return e.map(P)}function P(e){switch(e.type){case"Character":return e.value;case"CharacterClassRange":return{min:e.min.value,max:e.max.value};case"CharacterSet":return e;default:throw r(e)}}function M(e,t){if(e==t)return!0;if(!e||!t||e.type!=t.type)return!1;switch(e.type){case"Alternative":{const r=t;return Q(e.elements,r.elements)}case"Assertion":{const r=t;if(e.kind===r.kind){if("lookahead"===e.kind||"lookbehind"===e.kind){const r=t;return e.negate===r.negate&&Q(e.alternatives,r.alternatives)}return e.raw===r.raw}return!1}case"Backreference":{const r=t;return v(e)?v(r):M(e.resolved,r.resolved)&&w(e)==w(r)}case"Character":{const r=t;return e.value===r.value}case"CharacterClass":{const r=t;return e.negate===r.negate&&Q(e.elements,r.elements)}case"CharacterClassRange":{const r=t;return M(e.min,r.min)&&M(e.max,r.max)}case"CharacterSet":{const r=t;return"property"===e.kind&&"property"===r.kind?e.negate===r.negate&&e.key===r.key:e.raw===r.raw}case"Flags":{const r=t;return e.dotAll===r.dotAll&&e.global===r.global&&e.ignoreCase===r.ignoreCase&&e.multiline===r.multiline&&e.sticky===r.sticky&&e.unicode===r.unicode}case"CapturingGroup":case"Group":case"Pattern":{const r=t;return Q(e.alternatives,r.alternatives)}case"Quantifier":{const r=t;return e.min===r.min&&e.max===r.max&&e.greedy===r.greedy&&M(e.element,r.element)}case"RegExpLiteral":{const r=t;return M(e.flags,r.flags)&&M(e.pattern,r.pattern)}default:throw r(e)}}function Q(e,t){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!M(e[r],t[r]))return!1;return!0}function j(e,t,n,a,s){function o(e,t,r){var n,s;a.enter&&(t=a.enter(e,t,r));if(null===(s=null===(n=a.continueInto)||void 0===n?void 0:n.call(a,e,t,r))||void 0===s||s)switch(e.type){case"Assertion":if("lookahead"===e.kind||"lookbehind"===e.kind){const n=k(e.kind),s=a.join(e.alternatives.map((e=>i(e,T(a,t,r),n))),n);t=h(t,n,"assertion"),a.assert&&(t=a.assert(t,r,s,n))}break;case"Group":case"CapturingGroup":t=a.join(e.alternatives.map((e=>i(e,T(a,t,r),r))),r);break;case"Quantifier":0===e.max||(t=0===e.min?a.join([t,o(e.element,T(a,t,r),r)],r):o(e.element,t,r))}return a.leave&&(t=a.leave(e,t,r)),t}function i(e,t,r){var n,s;let i="ltr"===r?0:e.elements.length-1;const c="ltr"===r?1:-1;let u;for(;u=e.elements[i];i+=c){t=o(u,t,r);if(!(null===(s=null===(n=a.continueAfter)||void 0===n?void 0:n.call(a,u,t,r))||void 0===s||s))break}return t}function c(e,t,n){var s,o;const i=e.parent;if("CharacterClass"===i.type||"CharacterClassRange"===i.type)throw new Error("The given element cannot be part of a character class.");if(!(null===(o=null===(s=a.continueAfter)||void 0===s?void 0:s.call(a,e,t,n))||void 0===o||o))return!1;if("Quantifier"===i.type)return i.max<=1?c(i,t,n):[i,c(i,t,n)];{const a=i.elements.indexOf(e)+("ltr"===n?1:-1),s=i.elements[a];if(s)return s;{const e=i.parent;if("Pattern"===e.type)return"pattern";if("Assertion"===e.type)return u(e,t,n)?c(e,t,n):"assertion";if("CapturingGroup"===e.type||"Group"===e.type)return c(e,t,n);throw r(e)}}}function u(e,t,r){return!!a.continueOutside&&a.continueOutside(e,t,r)}function h(e,t,r){return a.endPath?a.endPath(e,t,r):e}if(s||(s=g(e)),"Alternative"===e.type)if(0===e.elements.length&&(t="next"),"enter"===t)l=e,e="ltr"===s?l.elements[0]:l.elements[l.elements.length-1];else{const t=e.parent;if("Pattern"===t.type)return h(n,s,"pattern");if("Assertion"===t.type&&!u(t,n,s))return h(n,s,"assertion");e=t}var l;return"enter"===t&&(n=o(e,n,s)),function(e,t,r){for(;;){let n=c(e,t,r);for(;Array.isArray(n);){const[e,s]=n;t=a.join([t,o(e,T(a,t,r),r)],r),n=s}if(!1===n)return t;if("assertion"===n||"pattern"===n)return h(t,r,n);t=o(n,t,r),e=n}}(e,n,s)}function T(e,t,r){return e.fork?e.fork(t,r):t}var D,I;exports.FirstLookChars=void 0,(D=exports.FirstLookChars||(exports.FirstLookChars={})).all=function(e){return{char:exports.Chars.all(e),exact:!0,edge:!0}},D.edge=function(e){return{char:exports.Chars.empty(e),exact:!0,edge:!0}},D.toConsumed=function(e){return!e.edge&&e.char.isEmpty?{char:t.CharSet.empty(e.char.maximum),exact:!0,empty:!1}:{char:t.CharSet.empty(e.char.maximum),exact:!0,empty:!0,look:e}},exports.FirstConsumedChars=void 0,(I=exports.FirstConsumedChars||(exports.FirstConsumedChars={})).emptyConcat=function(e){return{char:exports.Chars.empty(e),exact:!0,empty:!0,look:exports.FirstLookChars.all(e)}},I.emptyUnion=function(e){return{char:exports.Chars.empty(e),exact:!0,empty:!1}},I.toLook=function(e){if(e.empty){const t=function(e,t){const r=e.char.union(t.char);let n;return n=e.exact?!!t.exact||e.char.isSupersetOf(t.char):!!t.exact&&t.char.isSupersetOf(e.char),{char:r,exact:n}}(e,e.look);return{char:t.char,exact:t.exact,edge:e.look.edge}}return{char:e.char,exact:e.exact,edge:!1}},I.union=function(e,t){const r=a.fromFlags(t),n=[];for(const t of e)r.add(t),t.empty&&n.push(t.look);if(n.length>0){if(1===n.length)return{char:r.char,exact:r.exact,empty:!0,look:n[0]};const e=a.fromFlags(t);let s=!1;for(const t of n)e.add(t),s=s||t.edge;return{char:r.char,exact:r.exact,empty:!0,look:{char:e.char,exact:e.exact,edge:s}}}return{char:r.char,exact:r.exact,empty:!1}},I.concat=function(e,t){const r=a.fromFlags(t);let n=exports.FirstLookChars.all(t);for(const t of e){if(r.add(s(t,n)),!t.empty)return{char:r.char,exact:r.exact,empty:!1};{const e=s(n,t.look);if(n={char:e.char,exact:e.exact,edge:n.edge&&t.look.edge},!n.edge&&n.char.isEmpty)return{char:r.char,exact:r.exact,empty:!1}}}return{char:r.char,exact:r.exact,empty:!0,look:n}},I.makeOptional=function(e){return{char:e.char,exact:e.exact,empty:!0,look:{char:t.CharSet.all(e.char.maximum),exact:!0,edge:!0}}};class q{constructor(e){this._currentWordBoundaries=[],e instanceof B?(this._ltrCache=e.getFirstConsumedCharLTR,this._rtlCache=e.getFirstConsumedCharRTL):(this._ltrCache=new WeakMap,this._rtlCache=new WeakMap)}isCurrentWordBoundary(e){return this._currentWordBoundaries.some((t=>t===e))}pushWordBoundary(e){this._currentWordBoundaries.push(e)}popWordBoundary(){this._currentWordBoundaries.pop()}getCached(e,t){return"ltr"===t?this._ltrCache.get(e):this._rtlCache.get(e)}setCached(e,t,r){"ltr"===t?this._ltrCache.set(e,r):this._rtlCache.set(e,r)}}function U(e,t,r,n){return exports.FirstConsumedChars.union(e.map((e=>N(e,t,r,n))),r)}function N(e,t,n,a){let s=a.getCached(e,t);return void 0===s&&(s=function(e,t,n,a){switch(e.type){case"Assertion":return function(e,t,n,a){switch(e.kind){case"word":if(a.isCurrentWordBoundary(e))return s();{a.pushWordBoundary(e);const r=z(e,y(t),n,a);a.popWordBoundary();const o=exports.Chars.word(n);return r.edge?r.char.isDisjointWith(o)?c(e.negate):s():r.char.isDisjointWith(o)?c(e.negate):r.char.isSubsetOf(o)?c(!e.negate):s()}case"end":case"start":return k(e.kind)===t?n.multiline?i():o():s();case"lookahead":case"lookbehind":if(k(e.kind)===t){if(e.negate){if(d(e,(t=>t!==e&&"Assertion"===t.type)))return s();const r=U(e.alternatives,t,n,a),o=F(e.alternatives);return r.empty||!o?{char:exports.Chars.empty(n),empty:!1,exact:!0}:r.exact&&1===o.max?exports.FirstLookChars.toConsumed({char:r.char.negate(),edge:!0,exact:!0}):s()}{const r=U(e.alternatives,t,n,a);return exports.FirstLookChars.toConsumed(exports.FirstConsumedChars.toLook(r))}}return s();default:throw r(e)}function s(){return exports.FirstLookChars.toConsumed({char:exports.Chars.all(n),edge:!0,exact:!1})}function o(){return exports.FirstLookChars.toConsumed(exports.FirstLookChars.edge(n))}function i(){return exports.FirstLookChars.toConsumed({char:exports.Chars.lineTerminator(n),edge:!0,exact:!0})}function c(e){const t=exports.Chars.word(n);return exports.FirstLookChars.toConsumed({char:e?t.negate():t,edge:e,exact:!0})}}(e,t,n,a);case"Character":case"CharacterSet":case"CharacterClass":return{char:W(e,n),empty:!1,exact:!0};case"Quantifier":{if(0===e.max)return exports.FirstConsumedChars.emptyConcat(n);const r=N(e.element,t,n,a);return 0===e.min?exports.FirstConsumedChars.makeOptional(r):r}case"Alternative":{let r=e.elements;return"rtl"===t&&(r=[...r],r.reverse()),exports.FirstConsumedChars.concat(function*(){for(const e of r)yield N(e,t,n,a)}(),n)}case"CapturingGroup":case"Group":return U(e.alternatives,t,n,a);case"Backreference":{if(v(e))return exports.FirstConsumedChars.emptyConcat(n);let r=N(e.resolved,t,n,a);return r.exact&&r.char.size>1&&(r=Object.assign(Object.assign({},r),{exact:!1})),w(e)?r:exports.FirstConsumedChars.makeOptional(r)}default:throw r(e)}}(e,t,n,a),a.setCached(e,t,s)),s}function Z(e,t,r,n){return j(e,"next",exports.FirstConsumedChars.emptyConcat(r),{join:e=>exports.FirstConsumedChars.union(e,r),enter(e,t,a){const s=N(e,a,r,n);return exports.FirstConsumedChars.concat([t,s],r)},continueInto:()=>!1,continueAfter:(e,t)=>t.empty,continueOutside:(e,t,r)=>k(e.kind)!==r},t)}function z(e,t,r,n){return exports.FirstConsumedChars.toLook(Z(e,t,r,n))}function K(e,t,r,n){return j(e,"next",{char:exports.FirstConsumedChars.emptyConcat(r),contributors:[]},{join(e){const t=new Set;return e.forEach((e=>e.contributors.forEach((e=>t.add(e))))),{char:exports.FirstConsumedChars.union(e.map((e=>e.char)),r),contributors:[...t]}},enter(e,t,a){const s=N(e,a,r,n);return{char:exports.FirstConsumedChars.concat([t.char,s],r),contributors:[...t.contributors,e]}},continueInto:()=>!1,continueAfter:(e,t)=>t.char.empty,continueOutside:(e,t,r)=>k(e.kind)!==r},t)}exports.createCache=G,exports.followPaths=j,exports.getCapturingGroupNumber=function(t){let r=0;try{throw e.visitRegExpAST(x(t),{onCapturingGroupEnter(e){if(r++,e===t)throw new Error}}),new Error("Unable to find the given capturing group in its parent pattern.")}catch(e){return r}},exports.getClosestAncestor=E,exports.getEffectiveMaximumRepetition=function(e){let t=1;for(let r=e.parent;r;r=r.parent)if("Quantifier"===r.type){if(t*=r.max,0===t)return 0}else if("Assertion"===r.type)break;return t},exports.getFirstCharAfter=function(e,t,r){return z(e,t,r,new q(r))},exports.getFirstCharAfterWithContributors=function(e,t,r){return function(e,t,r,n){const{char:a,contributors:s}=K(e,t,r,n);return{char:exports.FirstConsumedChars.toLook(a),contributors:s}}(e,t,r,new q(r))},exports.getFirstConsumedChar=function(e,t,r){const a=new q(r);return n(e)?U(e,t,r,a):N(e,t,r,a)},exports.getFirstConsumedCharAfter=function(e,t,r){return Z(e,t,r,new q(r))},exports.getFirstConsumedCharAfterWithContributors=function(e,t,r){return K(e,t,r,new q(r))},exports.getLengthRange=F,exports.getMatchingDirection=g,exports.getMatchingDirectionFromAssertionKind=k,exports.getPattern=x,exports.hasSomeAncestor=m,exports.hasSomeDescendant=d,exports.invertMatchingDirection=y,exports.isEmpty=function(e){return o(e,l)},exports.isEmptyBackreference=v,exports.isPotentiallyEmpty=function(e){return i(e,p)},exports.isPotentiallyZeroLength=function(e){return i(e,(e=>h(e,e)))},exports.isStrictBackreference=w,exports.isZeroLength=c,exports.matchesAllCharacters=function(e,t){return"Character"!==e.type&&("CharacterClassRange"===e.type?0===e.min.value&&e.max.value===(t.unicode?1114111:65535):"CharacterSet"===e.type?"property"===e.kind?W(e,t).isAll:"any"===e.kind&&!!t.dotAll:!(!e.negate||0!==e.elements.length)||(e.negate?W(e.elements,t).isEmpty:W(e.elements,t).isAll))},exports.matchesNoCharacters=function(e,t){return"Character"!==e.type&&"CharacterClassRange"!==e.type&&("CharacterSet"===e.type?"property"===e.kind&&W(e,t).isEmpty:!e.negate&&0===e.elements.length||(e.negate?W(e.elements,t).isAll:W(e.elements,t).isEmpty))},exports.structurallyEqual=M,exports.toCache=function(e){return e instanceof B?e:G(e)},exports.toCharSet=W; |
{ | ||
"name": "regexp-ast-analysis", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "A library for analysing JS RegExp", | ||
@@ -5,0 +5,0 @@ "main": "index", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65138
1082