regexp-ast-analysis
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -37,3 +37,3 @@ // Generated by dts-bundle-generator v5.9.0 | ||
* - `isZeroLength(e) -> isPotentiallyZeroLength(e)` | ||
* - `isZeroLength(e) -> (getLengthRange(e) !== undefined && getLengthRange(e).max == 0)` | ||
* - `isZeroLength(e) -> getLengthRange(e).max == 0` | ||
* | ||
@@ -55,3 +55,3 @@ * @see {@link isPotentiallyZeroLength} | ||
* | ||
* - `isPotentiallyZeroLength(e) -> (getLengthRange(e) !== undefined && getLengthRange(e).min == 0)` | ||
* - `isPotentiallyZeroLength(e) -> getLengthRange(e).min == 0` | ||
* | ||
@@ -325,10 +325,7 @@ * @see {@link isZeroLength} | ||
* | ||
* If `undefined` is returned, then the given element can't consume any characters. | ||
* Note that character classes are not parsed by this function and are assumed to be non-empty. | ||
* | ||
* **Note:** `undefined` is only returned for empty alternative arrays. All characters classes/sets are assumed to | ||
* consume at least one characters and all assertions are assumed to have some accepting path. | ||
* | ||
* ## Backreferences | ||
* | ||
* While {@link isPotentiallyEmpty} generally assumes the worst-case for backreferences that references capturing group | ||
* While {@link isPotentiallyZeroLength} generally assumes the worst-case for backreferences that references capturing group | ||
* outside the given element, this function does not/cannot. The length range of a backreference only depends on the | ||
@@ -338,6 +335,8 @@ * referenced capturing group and the relative positions of the backreference and the capturing group within the | ||
* | ||
* This is an important distinction because it means that `isPotentiallyEmpty(e) -> getLengthRange(e).min == 0` is | ||
* guaranteed but `getLengthRange(e).min == 0 -> isPotentiallyEmpty(e)` is only guaranteed if `e` does not contain | ||
* This is an important distinction because it means that `isPotentiallyZeroLength(e) -> getLengthRange(e).min == 0` is | ||
* guaranteed but `getLengthRange(e).min == 0 -> isPotentiallyZeroLength(e)` is only guaranteed if `e` does not contain | ||
* backreferences. | ||
* | ||
* @throws {RangeError} if an empty array of alternatives is given. | ||
* | ||
* @see {@link isZeroLength} | ||
@@ -348,6 +347,20 @@ * @see {@link isPotentiallyZeroLength} | ||
*/ | ||
export declare function getLengthRange( | ||
element: Element | Alternative | readonly Alternative[] | ||
): LengthRange | undefined; | ||
export declare function getLengthRange(element: Element | Alternative | readonly Alternative[]): LengthRange; | ||
/** | ||
* Returns whether `getLengthRange(e).min == 0`. | ||
* | ||
* This function is slightly different from {@link isPotentiallyZeroLength} in how it handles backreferences. See the | ||
* notes on backreferences in the documentation of {@link isPotentiallyZeroLength} and {@link getLengthRange} for more | ||
* information. | ||
* | ||
* ## Relations | ||
* | ||
* - `isLengthRangeMinZero(e) <-> getLengthRange(e).min == 0` | ||
* | ||
* @throws {RangeError} if an empty array of alternatives is given. | ||
* | ||
* @see {@link getLengthRange} | ||
*/ | ||
export declare function isLengthRangeMinZero(element: Element | Alternative | readonly Alternative[]): boolean; | ||
/** | ||
* The type of the closest ancestor of two nodes with the given types. | ||
@@ -827,2 +840,18 @@ * | ||
/** | ||
* Whether only characters inside the given alternative may be considered | ||
* when creating the last character. | ||
* | ||
* This option control the behavior of {@link includeAfter}. By default, | ||
* {@link includeAfter} will also look at the characters after the | ||
* alternative to create the last character. This may be undesirable in | ||
* some case. | ||
* | ||
* The enabling this option has the following effect: If the last character | ||
* of the prefix is affected by characters outside the alternative, then | ||
* the prefix with {@link includeAfter} set to `false` will be returned. | ||
* | ||
* @default false | ||
*/ | ||
onlyInside?: boolean; | ||
/** | ||
* Whether groups will be combined more loosely. | ||
@@ -829,0 +858,0 @@ * |
@@ -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){let t=null;for(const r of e)if(null===t)t=r.parent;else if(r.parent!==t)throw new Error("Expected all alternatives to have the same parent")}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}),m=t.JS.createCharSet([{kind:"space",negate:!1}],{unicode:!0});e.space=function(e){return e.unicode?m:f}}(exports.Chars||(exports.Chars={}));const a=Array.isArray;class s{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 s(exports.Chars.empty(e))}static fromMaximum(e){return new s(t.CharSet.empty(e))}}function o(e,t){const r=e.char.intersect(t.char);return{char:r,exact:e.exact&&t.exact||r.isEmpty}}class c{constructor(e){this.count=e,this._indexes=[];for(let t=0;t<e;t++)this._indexes.push(t)}makeEqual(e,t){let r=this._indexes[e],n=this._indexes[t];for(;r!==n;)r<n?(this._indexes[t]=r,t=n,n=this._indexes[t]):(this._indexes[e]=n,e=r,r=this._indexes[e])}getEquivalenceSets(){let e=0;for(let t=0;t<this.count;t++)t===this._indexes[t]?this._indexes[t]=e++:this._indexes[t]=this._indexes[this._indexes[t]];return{count:e,indexes:this._indexes}}}function i(e,t){return a(e)?e.every(t):t(e)}function u(e,t){return a(e)?e.some(t):t(e)}function l(e){return i(e,h)}function h(e){switch(e.type){case"Alternative":return e.elements.every(h);case"Assertion":return!0;case"Character":case"CharacterClass":case"CharacterSet":return!1;case"Quantifier":return 0===e.max||h(e.element);case"Backreference":return A(e);case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(h);default:throw r(e)}}function p(e){return u(e,(e=>f(e,e)))}function f(e,t){return function e(n){switch(n.type){case"Alternative":return n.elements.every(e);case"Assertion":return!0;case"Backreference":return C(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 m(e){switch(e.type){case"Alternative":return e.elements.every(m);case"Assertion":return!1;case"Backreference":return A(e);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(m);case"Quantifier":return 0===e.max||m(e.element);default:throw r(e)}}function d(e){return function t(n){switch(n.type){case"Alternative":return n.elements.every(t);case"Assertion":return!1;case"Backreference":return C(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 C(e,t){return!!A(e)||!!g(e.resolved,(e=>e===t))&&(!F(e)||f(e.resolved,t))}function g(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 x(e,t,r){return"function"==typeof t?y(e,t,r):r?y(e,(e=>e===t),r):e===t||g(t,e)}function y(e,t,r){if(t(e))return!0;if(r&&!r(e))return!1;switch(e.type){case"Alternative":return e.elements.some((e=>y(e,t,r)));case"Assertion":return("lookahead"===e.kind||"lookbehind"===e.kind)&&e.alternatives.some((e=>y(e,t,r)));case"CapturingGroup":case"Group":case"Pattern":return e.alternatives.some((e=>y(e,t,r)));case"CharacterClass":return e.elements.some((e=>y(e,t,r)));case"CharacterClassRange":return y(e.min,t,r)||y(e.max,t,r);case"Quantifier":return y(e.element,t,r);case"RegExpLiteral":return y(e.pattern,t,r)||y(e.flags,t,r)}return!1}function k(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 v(e){let t;return g(e,(e=>"Assertion"===e.type&&(t=e,!0))),void 0===t||"lookahead"===t.kind?"ltr":"rtl"}function w(e){return"ltr"===e?"rtl":"ltr"}function S(e){return"end"===e||"lookahead"===e?"ltr":"rtl"}function A(e){const t=e.resolved,r=M(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"===v(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)||l(t)}function F(e){const t=e.resolved,r=M(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"===v(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)}function E(e){return x(e,_)}function _(e){return"CapturingGroup"===e.type}const b={min:0,max:0},L={min:1,max:1};function G(e){return a(e)?B(e):W(e)}function B(e){let t=1/0,r=0;for(const n of e){const e=W(n);e&&(t=Math.min(t,e.min),r=Math.max(r,e.max))}return t>r?void 0:{min:t,max:r}}function W(e){switch(e.type){case"Assertion":return b;case"Character":case"CharacterClass":case"CharacterSet":return L;case"Quantifier":{if(0===e.max)return b;const t=W(e.element);return t?0===t.max?b:{min:t.min*e.min,max:t.max*e.max}:0===e.min?b:void 0}case"Alternative":{let t=0,r=0;for(const n of e.elements){const e=W(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(A(e))return b;{const t=W(e.resolved);return t?t.min>0&&!F(e)?{min:0,max:t.max}:t:F(e)?b:void 0}default:throw r(e)}}function M(e,t){if(e===t)return e;if(e.parent&&e.parent===t.parent)return e.parent;{const r=O(e),n=O(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 O(e){const t=[];for(let r=e;r;r=r.parent)t.push(r);return t}function P(e){return R.from(e)}class R{constructor(e){this.toCharSet=new WeakMap,this.getFirstConsumedCharLTR=new WeakMap,this.getFirstConsumedCharRTL=new WeakMap,this.getLongestPrefix=new Map,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}static from(e){return e instanceof R?e:new R(e)}}function J(e,r){if(!a(e))return j(e,r);if(1===e.length)return j(e[0],r);const{positive:n,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?n.length?t.JS.createCharSet(q(n),r).union(...s.map((e=>t.JS.createCharSet(q(e.elements),r).negate()))):1===s.length?t.JS.createCharSet(q(s[0].elements),r).negate():exports.Chars.empty(r).union(...s.map((e=>t.JS.createCharSet(q(e.elements),r).negate()))):n.length?t.JS.createCharSet(q(n),r):exports.Chars.empty(r)}function j(e,t){if(t instanceof R){let r=t.toCharSet.get(e);return void 0===r&&(r=Q(e,t),t.toCharSet.set(e,r)),r}return Q(e,t)}function Q(e,r){if("CharacterClass"===e.type){const n=t.JS.createCharSet(q(e.elements),r);return e.negate?n.negate():n}return t.JS.createCharSet([D(e)],r)}function q(e){return e.map(D)}function D(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 T(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 I(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&&I(e.alternatives,r.alternatives)}return e.raw===r.raw}return!1}case"Backreference":{const r=t;return A(e)?A(r):T(e.resolved,r.resolved)&&F(e)==F(r)}case"Character":{const r=t;return e.value===r.value}case"CharacterClass":{const r=t;return e.negate===r.negate&&I(e.elements,r.elements)}case"CharacterClassRange":{const r=t;return T(e.min,r.min)&&T(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 I(e.alternatives,r.alternatives)}case"Quantifier":{const r=t;return e.min===r.min&&e.max===r.max&&e.greedy===r.greedy&&T(e.element,r.element)}case"RegExpLiteral":{const r=t;return T(e.flags,r.flags)&&T(e.pattern,r.pattern)}default:throw r(e)}}function I(e,t){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!T(e[r],t[r]))return!1;return!0}function z(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=S(e.kind),s=a.join(e.alternatives.map((e=>c(e,$(a,t,r),n))),n);t=l(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,$(a,t,r),r))),r);break;case"Quantifier":0===e.max||(t=0===e.min?a.join([t,o(e.element,$(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}function i(e,t,n){var s,o;const c=e.parent;if("CharacterClass"===c.type||"CharacterClassRange"===c.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"===c.type)return c.max<=1?i(c,t,n):[c,i(c,t,n)];{const a=c.elements.indexOf(e)+("ltr"===n?1:-1),s=c.elements[a];if(s)return s;{const e=c.parent;if("Pattern"===e.type)return"pattern";if("Assertion"===e.type)return u(e,t,n)?i(e,t,n):"assertion";if("CapturingGroup"===e.type||"Group"===e.type)return i(e,t,n);throw r(e)}}}function u(e,t,r){return!!a.continueOutside&&a.continueOutside(e,t,r)}function l(e,t,r){return a.endPath?a.endPath(e,t,r):e}if(s||(s=v(e)),"Alternative"===e.type)if(0===e.elements.length&&(t="next"),"enter"===t)h=e,e="ltr"===s?h.elements[0]:h.elements[h.elements.length-1];else{const t=e.parent;if("Pattern"===t.type)return l(n,s,"pattern");if("Assertion"===t.type&&!u(t,n,s))return l(n,s,"assertion");e=t}var h;return"enter"===t&&(n=o(e,n,s)),function(e,t,r){for(;;){let n=i(e,t,r);for(;Array.isArray(n);){const[e,s]=n;t=a.join([t,o(e,$(a,t,r),r)],r),n=s}if(!1===n)return t;if("assertion"===n||"pattern"===n)return l(t,r,n);t=o(n,t,r),e=n}}(e,n,s)}function $(e,t,r){return e.fork?e.fork(t,r):t}var U,N;exports.FirstLookChars=void 0,(U=exports.FirstLookChars||(exports.FirstLookChars={})).all=function(e){return{char:exports.Chars.all(e),exact:!0,edge:!0}},U.edge=function(e){return{char:exports.Chars.empty(e),exact:!0,edge:!0}},U.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,(N=exports.FirstConsumedChars||(exports.FirstConsumedChars={})).emptyConcat=function(e){return{char:exports.Chars.empty(e),exact:!0,empty:!0,look:exports.FirstLookChars.all(e)}},N.emptyUnion=function(e){return{char:exports.Chars.empty(e),exact:!0,empty:!1}},N.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}},N.union=function(e,t){const r=s.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=s.fromFlags(t);let a=!1;for(const t of n)e.add(t),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}},N.concat=function(e,t){const r=s.fromFlags(t);let n=exports.FirstLookChars.all(t);for(const t of e){if(r.add(o(t,n)),!t.empty)return{char:r.char,exact:r.exact,empty:!1};{const e=o(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}},N.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 Z{constructor(e){this._currentWordBoundaries=[],e instanceof R?(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 K(e,t,r){const n=new Z(r);return a(e)?H(e,t,r,n):V(e,t,r,n)}function H(e,t,r,n){return exports.FirstConsumedChars.union(e.map((e=>V(e,t,r,n))),r)}function V(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=te(e,w(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 S(e.kind)===t?n.multiline?c():o():s();case"lookahead":case"lookbehind":if(S(e.kind)===t){if(e.negate){if(x(e,(t=>t!==e&&"Assertion"===t.type)))return s();const r=H(e.alternatives,t,n,a),o=G(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=H(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 c(){return exports.FirstLookChars.toConsumed({char:exports.Chars.lineTerminator(n),edge:!0,exact:!0})}function i(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:J(e,n),empty:!1,exact:!0};case"Quantifier":{if(0===e.max)return exports.FirstConsumedChars.emptyConcat(n);const r=V(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 V(e,t,n,a)}(),n)}case"CapturingGroup":case"Group":return H(e.alternatives,t,n,a);case"Backreference":{if(A(e))return exports.FirstConsumedChars.emptyConcat(n);let r=V(e.resolved,t,n,a);return r.exact&&r.char.size>1&&(r=Object.assign(Object.assign({},r),{exact:!1})),F(e)?r:exports.FirstConsumedChars.makeOptional(r)}default:throw r(e)}}(e,t,n,a),a.setCached(e,t,s)),s}function X(e,t,r){return Y(e,t,r,new Z(r))}function Y(e,t,r,n){return z(e,"next",exports.FirstConsumedChars.emptyConcat(r),{join:e=>exports.FirstConsumedChars.union(e,r),enter(e,t,a){const s=V(e,a,r,n);return exports.FirstConsumedChars.concat([t,s],r)},continueInto:()=>!1,continueAfter:(e,t)=>t.empty,continueOutside:(e,t,r)=>S(e.kind)!==r},t)}function ee(e,t,r){return te(e,t,r,new Z(r))}function te(e,t,r,n){return exports.FirstConsumedChars.toLook(Y(e,t,r,n))}function re(e,t,r,n){return z(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=V(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)=>S(e.kind)!==r},t)}function ne(e,t,r,n={}){const a=R.from(r);r=a;const{includeAfter:s=!1,looseGroups:o=!1}=n,c=a.getLongestPrefix,i=`${t},${s},${o}`;let u=c.get(i);void 0===u&&(u=new WeakMap,c.set(i,u));let h=u.get(e);return void 0===h&&(h=function(e,t,r,n){const{chars:a,complete:s}=oe(e,t,r,n);for(let e=0;e<a.length;e++)if(a[e].isEmpty)return a.slice(0,e);s&&r.includeAfter&&a.push(function(e,t,r){const{elements:n}=e,a="rtl"===t?0:n.length-1,s="ltr"===t?1:-1;let o=a;for(;o>=0&&o<n.length&&l(n[o]);)o-=s;return o>=0&&o<n.length?ee(n[o],t,r):exports.FirstConsumedChars.toLook(ie(e,t,r))}(e,t,n).char);return a}(e,t,{includeAfter:s,looseGroups:o},r),u.set(e,h)),h}const ae={chars:[],complete:!0},se={chars:[],complete:!1};function oe(e,t,r,n){const{elements:a}=e,s=[],o="ltr"===t?1:-1;for(let e="ltr"===t?0:a.length-1;e>=0&&e<a.length;e+=o){const o=ce(a[e],t,r,n);if(s.push(...o.chars),!o.complete)return{chars:s,complete:!1}}return{chars:s,complete:!0}}function ce(e,t,n,a){switch(e.type){case"Assertion":return ae;case"Character":case"CharacterClass":case"CharacterSet":return{chars:[J(e,a)],complete:!0};case"CapturingGroup":case"Group":return function(e,t,r,n){const a=e.alternatives.map((e=>oe(e,t,r,n)));if(1===a.length)return a[0];const s=[];let o=!0,c=0;for(let i=0;o;i++){const u=[];let l=!1;for(const e of a)i>=e.chars.length?l=!0:(u.push(e.chars[i]),i===e.chars.length-1&&!e.complete&&r.includeAfter&&(o=!1));if(0===u.length)break;if(l){if(o=!1,!r.includeAfter)break;u.push(ee(e,t,n).char)}else if(!r.looseGroups&&(o&&u.some((e=>!e.equals(u[0])))&&c++,c>=2&&(o=!1,!r.includeAfter)))break;const h=u[0].union(...u.slice(1));s.push(h)}return{chars:s,complete:o}}(e,t,n,a);case"Quantifier":return function(e,t,r,n){if(l(e))return ae;if(p(e)){if(!r.includeAfter)return se;return{chars:[exports.FirstConsumedChars.toLook(ie(e,t,n)).char],complete:!1}}const a=ce(e.element,t,r,n);if(!a.complete)return a;if(0===a.chars.length)throw new Error(`Expected the quantifier '${e.raw}' to consume at least one character.`);const s=[];for(let t=0;t<e.min;t++)if(s.push(...a.chars),s.length>1e3)return{chars:s,complete:!1};if(e.min===e.max)return{chars:s,complete:!0};if(r.includeAfter){const r=ee(e,t,n);s.push(r.char.union(a.chars[0]))}return{chars:s,complete:!1}}(e,t,n,a);case"Backreference":if(A(e))return ae;if(F(e)){return ce(e.resolved,t,Object.assign(Object.assign({},n),{includeAfter:!1}),a)}if(!n.includeAfter)return se;return{chars:[exports.FirstConsumedChars.toLook(ie(e,t,a)).char],complete:!1};default:r(e)}}function ie(e,t,r){const n=K(e,t,r);return n.empty?exports.FirstConsumedChars.concat([n,X(e,t,r)],r):n}function ue(e,t,r){return n(e),"unknown"===t?function(e,t){const r=he(e,"ltr",t),n=he(e,"rtl",t),a=pe([...r,...n],(e=>e)),s=[];for(const e of a){const t=new Set;for(const r of e)r.forEach((e=>t.add(e)));s.push([...t])}return s}(e,r):he(e,t,r)}const le={includeAfter:!0,looseGroups:!0};function he(e,r,n){const a=function(e){function t(r){let n=t.cache.get(r);return void 0===n&&(n=e(r),t.cache.set(r,n)),n}return t.cache=new Map,t}((e=>{let t=ne(e,r,n,le),a=0;for(let e=t.length-1;e>=0&&t[e].isAll;e--)a++;return a>0&&(t=t.slice(0,t.length-a)),t})),s=new Set;for(const t of e)a(t).forEach((e=>s.add(e)));const o=new t.CharBase(s),c=[];for(const t of e)c.push({characters:a(t).map((e=>o.split(e))),alternative:t});return function e(t,r){if(t.length<2)return[t];for(const e of t)if(r>=e.characters.length)return[t];const n=pe(t,(e=>e.characters[r])),a=[];for(const t of n)a.push(...e(t,r+1));return a}(c,0).map((e=>e.map((e=>e.alternative))))}function pe(e,t){if(e.length<2)return[e];const r=new c(e.length),n=new Map;for(let a=0;a<e.length;a++){const s=e[a];for(const e of t(s)){const t=n.get(e);void 0===t?n.set(e,a):r.makeEqual(a,t)}}const a=r.getEquivalenceSets(),s=[];for(let e=0;e<a.count;e++)s.push([]);for(let t=0;t<e.length;t++)s[a.indexes[t]].push(e[t]);return s}function fe(e,t,r,n,a){const s=ue(t,r,n);return!(!a&&!function(e,t,r){let n=0,a=0;for(const r of t)E(r)&&(e.has(r)?n++:a++);if(n>1||1===n&&0!==a)return!1;if(0!==a)return r.every((t=>!t.some(E)||t.every((t=>!e.has(t)))));if(0!==n)return r.every((e=>e.length<2||!e.some(E)));return!0}(e,t,s))&&s.every((t=>t.length<2||(!!t.every((t=>!e.has(t)))||(function(e){const t=G(e);return Boolean(t&&t.min===t.max)}(t)||function(e,t,r){const n=function(e,t){const r=me(e,"ltr",t),n=me(r.rest,"rtl",t);return{left:r.prefix,right:n.prefix,rest:n.rest}}(e.map((e=>e.elements)),r),a=[];for(const e of n.rest)a.push(...e);const s=exports.Chars.empty(r).union(...a.map((e=>de(e,r))));if(("ltr"===t?n.right:n.left).some((e=>e.isDisjointWith(s))))return!0;const o=e[0].parent;if("Pattern"===o.type||"Assertion"===o.type)return!1;return ee(o,t,r).char.isDisjointWith(s)}(t,r,n)))))}function me(e,t,r){const n=function(e,t,r){const n=[];for(let a=0;;a++){let s=null;for(const o of e){const e="ltr"===t?a:o.length-1-a;if(!(a>=0&&a<o.length))return n;{const t=o[e];switch(t.type){case"Character":case"CharacterClass":case"CharacterSet":if(null===s)s=J(t,r);else if(!s.equals(J(t,r)))return n;break;default:return n}}}if(null===s)throw new Error;n.push(s)}}(e,t,r);return 0===n.length?{prefix:n,rest:e}:{prefix:n,rest:e.map((e=>{const r="ltr"===t?n.length:0,a="ltr"===t?e.length:e.length-n.length;return e.slice(r,a)}))}}function de(e,t){const r=[];return x(e,(e=>("Character"===e.type||"CharacterClass"===e.type||"CharacterSet"===e.type?r.push(J(e,t)):"Backreference"!==e.type||A(e)||r.push(de(e.resolved,t)),!1)),(e=>"Assertion"!==e.type&&"CharacterClass"!==e.type)),exports.Chars.empty(t).union(...r)}exports.canReorder=function(e,t,r={}){t=P(t);const{ignoreCapturingGroups:a=!1,matchingDirection:s}=r,o=(c=e)instanceof Set?c:new Set(c);var c;if(o.size<2)return!0;n(o);const i=function(e){if(e.size<=1)return[...e];let t;for(const r of e){t=r;break}if(!t)throw new Error;const r=t.parent.alternatives;let n=e.size,a=0;for(let t=0;t<r.length;t++){const s=r[t];e.has(s)&&(n=Math.min(n,t),a=Math.max(a,t))}return r.slice(n,a+1)}(o),u=null!=s?s:v(i[0]);return"unknown"===u?fe(o,i,"ltr",t,a)&&fe(o,i,"rtl",t,a):fe(o,i,u,t,a)},exports.canReorderDirectional=fe,exports.containsCapturingGroup=E,exports.createCache=function(e){return new R(e)},exports.followPaths=z,exports.getCapturingGroupNumber=function(t){let r=0;try{throw e.visitRegExpAST(k(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=M,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=ee,exports.getFirstCharAfterWithContributors=function(e,t,r){return function(e,t,r,n){const{char:a,contributors:s}=re(e,t,r,n);return{char:exports.FirstConsumedChars.toLook(a),contributors:s}}(e,t,r,new Z(r))},exports.getFirstConsumedChar=K,exports.getFirstConsumedCharAfter=X,exports.getFirstConsumedCharAfterWithContributors=function(e,t,r){return re(e,t,r,new Z(r))},exports.getLengthRange=G,exports.getLongestPrefix=ne,exports.getMatchingDirection=v,exports.getMatchingDirectionFromAssertionKind=S,exports.getPattern=k,exports.hasSomeAncestor=g,exports.hasSomeDescendant=x,exports.invertMatchingDirection=w,exports.isEmpty=function(e){return i(e,m)},exports.isEmptyBackreference=A,exports.isPotentiallyEmpty=function(e){return u(e,d)},exports.isPotentiallyZeroLength=p,exports.isStrictBackreference=F,exports.isZeroLength=l,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?J(e,t).isAll:"any"===e.kind&&!!t.dotAll:!(!e.negate||0!==e.elements.length)||(e.negate?J(e.elements,t).isEmpty:J(e.elements,t).isAll))},exports.matchesNoCharacters=function(e,t){return"Character"!==e.type&&"CharacterClassRange"!==e.type&&("CharacterSet"===e.type?"property"===e.kind&&J(e,t).isEmpty:!e.negate&&0===e.elements.length||(e.negate?J(e.elements,t).isAll:J(e.elements,t).isEmpty))},exports.structurallyEqual=T,exports.toCache=P,exports.toCharSet=J; | ||
"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){let t=null;for(const r of e)if(null===t)t=r.parent;else if(r.parent!==t)throw new Error("Expected all alternatives to have the same parent")}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}),f=t.JS.createCharSet([{kind:"digit",negate:!1}],{unicode:!0});e.digit=function(e){return e.unicode?f:h};const p=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:p}}(exports.Chars||(exports.Chars={}));const a=Array.isArray;class s{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 s(exports.Chars.empty(e))}static fromMaximum(e){return new s(t.CharSet.empty(e))}}function o(e,t){const r=e.char.intersect(t.char);return{char:r,exact:e.exact&&t.exact||r.isEmpty}}class c{constructor(e){this.count=e,this._indexes=[];for(let t=0;t<e;t++)this._indexes.push(t)}makeEqual(e,t){let r=this._indexes[e],n=this._indexes[t];for(;r!==n;)r<n?(this._indexes[t]=r,t=n,n=this._indexes[t]):(this._indexes[e]=n,e=r,r=this._indexes[e])}getEquivalenceSets(){let e=0;for(let t=0;t<this.count;t++)t===this._indexes[t]?this._indexes[t]=e++:this._indexes[t]=this._indexes[this._indexes[t]];return{count:e,indexes:this._indexes}}}function i(e,t){return a(e)?e.every(t):t(e)}function u(e,t){return a(e)?e.some(t):t(e)}function l(e){return i(e,h)}function h(e){switch(e.type){case"Alternative":return e.elements.every(h);case"Assertion":return!0;case"Character":case"CharacterClass":case"CharacterSet":return!1;case"Quantifier":return 0===e.max||h(e.element);case"Backreference":return S(e);case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(h);default:throw r(e)}}function f(e,t){return function e(n){switch(n.type){case"Alternative":return n.elements.every(e);case"Assertion":return!0;case"Backreference":return d(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 p(e){switch(e.type){case"Alternative":return e.elements.every(p);case"Assertion":return!1;case"Backreference":return S(e);case"Character":case"CharacterClass":case"CharacterSet":return!1;case"CapturingGroup":case"Group":return e.alternatives.length>0&&e.alternatives.every(p);case"Quantifier":return 0===e.max||p(e.element);default:throw r(e)}}function m(e){return function t(n){switch(n.type){case"Alternative":return n.elements.every(t);case"Assertion":return!1;case"Backreference":return d(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 d(e,t){return!!S(e)||!!C(e.resolved,(e=>e===t))&&(!A(e)||f(e.resolved,t))}function C(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 g(e,t,r){return"function"==typeof t?x(e,t,r):r?x(e,(e=>e===t),r):e===t||C(t,e)}function x(e,t,r){if(t(e))return!0;if(r&&!r(e))return!1;switch(e.type){case"Alternative":return e.elements.some((e=>x(e,t,r)));case"Assertion":return("lookahead"===e.kind||"lookbehind"===e.kind)&&e.alternatives.some((e=>x(e,t,r)));case"CapturingGroup":case"Group":case"Pattern":return e.alternatives.some((e=>x(e,t,r)));case"CharacterClass":return e.elements.some((e=>x(e,t,r)));case"CharacterClassRange":return x(e.min,t,r)||x(e.max,t,r);case"Quantifier":return x(e.element,t,r);case"RegExpLiteral":return x(e.pattern,t,r)||x(e.flags,t,r)}return!1}function y(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 v(e){let t;return C(e,(e=>"Assertion"===e.type&&(t=e,!0))),void 0===t||"lookahead"===t.kind?"ltr":"rtl"}function k(e){return"ltr"===e?"rtl":"ltr"}function w(e){return"end"===e||"lookahead"===e?"ltr":"rtl"}function S(e){const t=e.resolved,r=O(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"===v(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)||l(t)}function A(e){const t=e.resolved,r=O(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"===v(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)}function F(e){return g(e,E)}function E(e){return"CapturingGroup"===e.type}const b={min:0,max:0},_={min:1,max:1};function G(e){return a(e)?L(e):B(e)}function L(e){let t=1/0,r=0;for(const n of e){const e=B(n);t=Math.min(t,e.min),r=Math.max(r,e.max)}if(t>r)throw new RangeError("Expected the alternatives array to have at least one alternative.");return{min:t,max:r}}function B(e){switch(e.type){case"Assertion":return b;case"Character":case"CharacterClass":case"CharacterSet":return _;case"Quantifier":{if(0===e.max)return b;const t=B(e.element);return 0===t.max?b:{min:t.min*e.min,max:t.max*e.max}}case"Alternative":{let t=0,r=0;for(const n of e.elements){const e=B(n);t+=e.min,r+=e.max}return{min:t,max:r}}case"CapturingGroup":case"Group":return L(e.alternatives);case"Backreference":if(S(e))return b;{const t=B(e.resolved);return t.min>0&&!A(e)?{min:0,max:t.max}:t}default:throw r(e)}}function R(e){return a(e)?W(e):M(e)}function W(e){if(0===e.length)throw new RangeError("Expected the alternatives array to have at least one alternative.");return e.some(M)}function M(e){switch(e.type){case"Assertion":return!0;case"Character":case"CharacterClass":case"CharacterSet":return!1;case"Quantifier":return 0===e.min||M(e.element);case"Alternative":return e.elements.every(M);case"CapturingGroup":case"Group":return W(e.alternatives);case"Backreference":return S(e)||!A(e)||M(e.resolved);default:throw r(e)}}function O(e,t){if(e===t)return e;if(e.parent&&e.parent===t.parent)return e.parent;{const r=P(e),n=P(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 P(e){const t=[];for(let r=e;r;r=r.parent)t.push(r);return t}function J(e){return Q.from(e)}class Q{constructor(e){this.toCharSet=new WeakMap,this.getFirstConsumedCharLTR=new WeakMap,this.getFirstConsumedCharRTL=new WeakMap,this.getLongestPrefix=new Map,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}static from(e){return e instanceof Q?e:new Q(e)}}function j(e,r){if(!a(e))return q(e,r);if(1===e.length)return q(e[0],r);const{positive:n,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?n.length?t.JS.createCharSet(I(n),r).union(...s.map((e=>t.JS.createCharSet(I(e.elements),r).negate()))):1===s.length?t.JS.createCharSet(I(s[0].elements),r).negate():exports.Chars.empty(r).union(...s.map((e=>t.JS.createCharSet(I(e.elements),r).negate()))):n.length?t.JS.createCharSet(I(n),r):exports.Chars.empty(r)}function q(e,t){if(t instanceof Q){let r=t.toCharSet.get(e);return void 0===r&&(r=D(e,t),t.toCharSet.set(e,r)),r}return D(e,t)}function D(e,r){if("CharacterClass"===e.type){const n=t.JS.createCharSet(I(e.elements),r);return e.negate?n.negate():n}return t.JS.createCharSet([T(e)],r)}function I(e){return e.map(T)}function T(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 z(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 $(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&&$(e.alternatives,r.alternatives)}return e.raw===r.raw}return!1}case"Backreference":{const r=t;return S(e)?S(r):z(e.resolved,r.resolved)&&A(e)==A(r)}case"Character":{const r=t;return e.value===r.value}case"CharacterClass":{const r=t;return e.negate===r.negate&&$(e.elements,r.elements)}case"CharacterClassRange":{const r=t;return z(e.min,r.min)&&z(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 $(e.alternatives,r.alternatives)}case"Quantifier":{const r=t;return e.min===r.min&&e.max===r.max&&e.greedy===r.greedy&&z(e.element,r.element)}case"RegExpLiteral":{const r=t;return z(e.flags,r.flags)&&z(e.pattern,r.pattern)}default:throw r(e)}}function $(e,t){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!z(e[r],t[r]))return!1;return!0}function U(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=w(e.kind),s=a.join(e.alternatives.map((e=>c(e,Z(a,t,r),n))),n);t=l(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,Z(a,t,r),r))),r);break;case"Quantifier":0===e.max||(t=0===e.min?a.join([t,o(e.element,Z(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}function i(e,t,n){var s,o;const c=e.parent;if("CharacterClass"===c.type||"CharacterClassRange"===c.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"===c.type)return c.max<=1?i(c,t,n):[c,i(c,t,n)];{const a=c.elements.indexOf(e)+("ltr"===n?1:-1),s=c.elements[a];if(s)return s;{const e=c.parent;if("Pattern"===e.type)return"pattern";if("Assertion"===e.type)return u(e,t,n)?i(e,t,n):"assertion";if("CapturingGroup"===e.type||"Group"===e.type)return i(e,t,n);throw r(e)}}}function u(e,t,r){return!!a.continueOutside&&a.continueOutside(e,t,r)}function l(e,t,r){return a.endPath?a.endPath(e,t,r):e}if(s||(s=v(e)),"Alternative"===e.type)if(0===e.elements.length&&(t="next"),"enter"===t)h=e,e="ltr"===s?h.elements[0]:h.elements[h.elements.length-1];else{const t=e.parent;if("Pattern"===t.type)return l(n,s,"pattern");if("Assertion"===t.type&&!u(t,n,s))return l(n,s,"assertion");e=t}var h;return"enter"===t&&(n=o(e,n,s)),function(e,t,r){for(;;){let n=i(e,t,r);for(;Array.isArray(n);){const[e,s]=n;t=a.join([t,o(e,Z(a,t,r),r)],r),n=s}if(!1===n)return t;if("assertion"===n||"pattern"===n)return l(t,r,n);t=o(n,t,r),e=n}}(e,n,s)}function Z(e,t,r){return e.fork?e.fork(t,r):t}var N,K;exports.FirstLookChars=void 0,(N=exports.FirstLookChars||(exports.FirstLookChars={})).all=function(e){return{char:exports.Chars.all(e),exact:!0,edge:!0}},N.edge=function(e){return{char:exports.Chars.empty(e),exact:!0,edge:!0}},N.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,(K=exports.FirstConsumedChars||(exports.FirstConsumedChars={})).emptyConcat=function(e){return{char:exports.Chars.empty(e),exact:!0,empty:!0,look:exports.FirstLookChars.all(e)}},K.emptyUnion=function(e){return{char:exports.Chars.empty(e),exact:!0,empty:!1}},K.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}},K.union=function(e,t){const r=s.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=s.fromFlags(t);let a=!1;for(const t of n)e.add(t),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}},K.concat=function(e,t){const r=s.fromFlags(t);let n=exports.FirstLookChars.all(t);for(const t of e){if(r.add(o(t,n)),!t.empty)return{char:r.char,exact:r.exact,empty:!1};{const e=o(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}},K.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 H{constructor(e){this._currentWordBoundaries=[],e instanceof Q?(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 V(e,t,r){const n=new H(r);return a(e)?X(e,t,r,n):Y(e,t,r,n)}function X(e,t,r,n){return exports.FirstConsumedChars.union(e.map((e=>Y(e,t,r,n))),r)}function Y(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=ne(e,k(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 w(e.kind)===t?n.multiline?c():o():s();case"lookahead":case"lookbehind":if(w(e.kind)===t){if(e.negate){if(g(e,(t=>t!==e&&"Assertion"===t.type)))return s();const r=X(e.alternatives,t,n,a),o=G(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=X(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 c(){return exports.FirstLookChars.toConsumed({char:exports.Chars.lineTerminator(n),edge:!0,exact:!0})}function i(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:j(e,n),empty:!1,exact:!0};case"Quantifier":{if(0===e.max)return exports.FirstConsumedChars.emptyConcat(n);const r=Y(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 Y(e,t,n,a)}(),n)}case"CapturingGroup":case"Group":return X(e.alternatives,t,n,a);case"Backreference":{if(S(e))return exports.FirstConsumedChars.emptyConcat(n);let r=Y(e.resolved,t,n,a);return r.exact&&r.char.size>1&&(r=Object.assign(Object.assign({},r),{exact:!1})),A(e)?r:exports.FirstConsumedChars.makeOptional(r)}default:throw r(e)}}(e,t,n,a),a.setCached(e,t,s)),s}function ee(e,t,r){return te(e,t,r,new H(r))}function te(e,t,r,n){return U(e,"next",exports.FirstConsumedChars.emptyConcat(r),{join:e=>exports.FirstConsumedChars.union(e,r),enter(e,t,a){const s=Y(e,a,r,n);return exports.FirstConsumedChars.concat([t,s],r)},continueInto:()=>!1,continueAfter:(e,t)=>t.empty,continueOutside:(e,t,r)=>w(e.kind)!==r},t)}function re(e,t,r){return ne(e,t,r,new H(r))}function ne(e,t,r,n){return exports.FirstConsumedChars.toLook(te(e,t,r,n))}function ae(e,t,r,n){return U(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=Y(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)=>w(e.kind)!==r},t)}function se(e,t,r,n={}){const a=Q.from(r);r=a;const{includeAfter:s=!1,onlyInside:o=!1,looseGroups:c=!1}=n,i=a.getLongestPrefix,u=`${t},${s},${c}`;let h=i.get(u);void 0===h&&(h=new WeakMap,i.set(u,h));let f=h.get(e);return void 0===f&&(f=function(e,t,r,n){const{chars:a,complete:s}=ie(e,t,r,n);for(let e=0;e<a.length;e++)if(a[e].isEmpty)return a.slice(0,e);s&&r.includeAfter&&!r.onlyInside&&a.push(function(e,t,r){const{elements:n}=e,a="rtl"===t?0:n.length-1,s="ltr"===t?1:-1;let o=a;for(;o>=0&&o<n.length&&l(n[o]);)o-=s;return o>=0&&o<n.length?re(n[o],t,r):exports.FirstConsumedChars.toLook(le(e,t,r))}(e,t,n).char);return a}(e,t,{includeAfter:s,onlyInside:o,looseGroups:c,root:e},r),h.set(e,f)),f}const oe={chars:[],complete:!0},ce={chars:[],complete:!1};function ie(e,t,r,n){const{elements:a}=e,s=[],o="ltr"===t?1:-1;for(let e="ltr"===t?0:a.length-1;e>=0&&e<a.length;e+=o){const o=ue(a[e],t,r,n);if(s.push(...o.chars),!o.complete)return{chars:s,complete:!1}}return{chars:s,complete:!0}}function ue(e,t,n,a){switch(e.type){case"Assertion":return oe;case"Character":case"CharacterClass":case"CharacterSet":return{chars:[j(e,a)],complete:!0};case"CapturingGroup":case"Group":return function(e,t,r,n){const a=e.alternatives.map((e=>ie(e,t,r,n)));if(1===a.length)return a[0];const s=[];let o=!0,c=0;for(let i=0;o;i++){const u=[];let l=!1;for(const e of a)i>=e.chars.length?l=!0:(u.push(e.chars[i]),i===e.chars.length-1&&!e.complete&&r.includeAfter&&(o=!1));if(0===u.length)break;if(l){if(o=!1,!fe(e,r,t))break;u.push(re(e,t,n).char)}else if(!r.looseGroups&&(o&&u.some((e=>!e.equals(u[0])))&&c++,c>=2&&(o=!1,!r.includeAfter)))break;const h=u[0].union(...u.slice(1));s.push(h)}return{chars:s,complete:o}}(e,t,n,a);case"Quantifier":return function(e,t,r,n){if(l(e))return oe;if(R(e)){if(!he(e,r,t))return ce;return{chars:[exports.FirstConsumedChars.toLook(le(e,t,n)).char],complete:!1}}const a=ue(e.element,t,r,n);if(!a.complete)return a;if(0===a.chars.length)throw new Error(`Expected the quantifier '${e.raw}' to consume at least one character.`);const s=[];for(let t=0;t<e.min;t++)if(s.push(...a.chars),s.length>1e3)return{chars:s,complete:!1};if(e.min===e.max)return{chars:s,complete:!0};if(fe(e,r,t)){const r=re(e,t,n);s.push(r.char.union(a.chars[0]))}return{chars:s,complete:!1}}(e,t,n,a);case"Backreference":if(S(e))return oe;if(A(e)){return ue(e.resolved,t,Object.assign(Object.assign({},n),{includeAfter:!1}),a)}if(!he(e,n,t))return ce;return{chars:[exports.FirstConsumedChars.toLook(le(e,t,a)).char],complete:!1};default:r(e)}}function le(e,t,r){const n=V(e,t,r);return n.empty?exports.FirstConsumedChars.concat([n,ee(e,t,r)],r):n}function he(e,t,r){return!!t.includeAfter&&(!t.onlyInside||function(e,t,r){return!R(e)||pe(e,t,r)}(e,r,t.root))}function fe(e,t,r){return!!t.includeAfter&&(!t.onlyInside||pe(e,r,t.root))}function pe(e,t,r){const n=e.parent;if("CharacterClass"===n.type||"CharacterClassRange"===n.type)throw new Error("Expected an element outside a character class.");if("Quantifier"===n.type)return pe(n,t,r);const a="ltr"===t?1:-1;for(let t=n.elements.indexOf(e)+a;t>=0&&t<n.elements.length;t+=a){if(!R(n.elements[t]))return!0}if(n===r)return!1;const s=n.parent;if("Pattern"===s.type)throw new Error("Expected the given element to be a descendant of the root alternative.");if("Assertion"===s.type)throw new Error;return pe(s,t,r)}function me(e,t,r){return n(e),"unknown"===t?function(e,t){const r=Ce(e,"ltr",t),n=Ce(e,"rtl",t),a=ge([...r,...n],(e=>e)),s=[];for(const e of a){const t=new Set;for(const r of e)r.forEach((e=>t.add(e)));s.push([...t])}return s}(e,r):Ce(e,t,r)}const de={includeAfter:!0,looseGroups:!0};function Ce(e,r,n){const a=function(e){function t(r){let n=t.cache.get(r);return void 0===n&&(n=e(r),t.cache.set(r,n)),n}return t.cache=new Map,t}((e=>{let t=se(e,r,n,de),a=0;for(let e=t.length-1;e>=0&&t[e].isAll;e--)a++;return a>0&&(t=t.slice(0,t.length-a)),t})),s=new Set;for(const t of e)a(t).forEach((e=>s.add(e)));const o=new t.CharBase(s),c=[];for(const t of e)c.push({characters:a(t).map((e=>o.split(e))),alternative:t});return function e(t,r){if(t.length<2)return[t];for(const e of t)if(r>=e.characters.length)return[t];const n=ge(t,(e=>e.characters[r])),a=[];for(const t of n)a.push(...e(t,r+1));return a}(c,0).map((e=>e.map((e=>e.alternative))))}function ge(e,t){if(e.length<2)return[e];const r=new c(e.length),n=new Map;for(let a=0;a<e.length;a++){const s=e[a];for(const e of t(s)){const t=n.get(e);void 0===t?n.set(e,a):r.makeEqual(a,t)}}const a=r.getEquivalenceSets(),s=[];for(let e=0;e<a.count;e++)s.push([]);for(let t=0;t<e.length;t++)s[a.indexes[t]].push(e[t]);return s}function xe(e,t,r,n,a){const s=me(t,r,n);return!(!a&&!function(e,t,r){let n=0,a=0;for(const r of t)F(r)&&(e.has(r)?n++:a++);if(n>1||1===n&&0!==a)return!1;if(0!==a)return r.every((t=>!t.some(F)||t.every((t=>!e.has(t)))));if(0!==n)return r.every((e=>e.length<2||!e.some(F)));return!0}(e,t,s))&&s.every((t=>t.length<2||(!!t.every((t=>!e.has(t)))||(function(e){const t=G(e);return Boolean(t&&t.min===t.max)}(t)||function(e,t,r){const n=function(e,t){const r=ye(e,"ltr",t),n=ye(r.rest,"rtl",t);return{left:r.prefix,right:n.prefix,rest:n.rest}}(e.map((e=>e.elements)),r),a=[];for(const e of n.rest)a.push(...e);const s=exports.Chars.empty(r).union(...a.map((e=>ve(e,r))));if(("ltr"===t?n.right:n.left).some((e=>e.isDisjointWith(s))))return!0;const o=e[0].parent;if("Pattern"===o.type||"Assertion"===o.type)return!1;return re(o,t,r).char.isDisjointWith(s)}(t,r,n)))))}function ye(e,t,r){const n=function(e,t,r){const n=[];for(let a=0;;a++){let s=null;for(const o of e){const e="ltr"===t?a:o.length-1-a;if(!(a>=0&&a<o.length))return n;{const t=o[e];switch(t.type){case"Character":case"CharacterClass":case"CharacterSet":if(null===s)s=j(t,r);else if(!s.equals(j(t,r)))return n;break;default:return n}}}if(null===s)throw new Error;n.push(s)}}(e,t,r);return 0===n.length?{prefix:n,rest:e}:{prefix:n,rest:e.map((e=>{const r="ltr"===t?n.length:0,a="ltr"===t?e.length:e.length-n.length;return e.slice(r,a)}))}}function ve(e,t){const r=[];return g(e,(e=>("Character"===e.type||"CharacterClass"===e.type||"CharacterSet"===e.type?r.push(j(e,t)):"Backreference"!==e.type||S(e)||r.push(ve(e.resolved,t)),!1)),(e=>"Assertion"!==e.type&&"CharacterClass"!==e.type)),exports.Chars.empty(t).union(...r)}exports.canReorder=function(e,t,r={}){t=J(t);const{ignoreCapturingGroups:a=!1,matchingDirection:s}=r,o=(c=e)instanceof Set?c:new Set(c);var c;if(o.size<2)return!0;n(o);const i=function(e){if(e.size<=1)return[...e];let t;for(const r of e){t=r;break}if(!t)throw new Error;const r=t.parent.alternatives;let n=e.size,a=0;for(let t=0;t<r.length;t++){const s=r[t];e.has(s)&&(n=Math.min(n,t),a=Math.max(a,t))}return r.slice(n,a+1)}(o),u=null!=s?s:v(i[0]);return"unknown"===u?xe(o,i,"ltr",t,a)&&xe(o,i,"rtl",t,a):xe(o,i,u,t,a)},exports.canReorderDirectional=xe,exports.containsCapturingGroup=F,exports.createCache=function(e){return new Q(e)},exports.followPaths=U,exports.getCapturingGroupNumber=function(t){let r=0;try{throw e.visitRegExpAST(y(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=O,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=re,exports.getFirstCharAfterWithContributors=function(e,t,r){return function(e,t,r,n){const{char:a,contributors:s}=ae(e,t,r,n);return{char:exports.FirstConsumedChars.toLook(a),contributors:s}}(e,t,r,new H(r))},exports.getFirstConsumedChar=V,exports.getFirstConsumedCharAfter=ee,exports.getFirstConsumedCharAfterWithContributors=function(e,t,r){return ae(e,t,r,new H(r))},exports.getLengthRange=G,exports.getLongestPrefix=se,exports.getMatchingDirection=v,exports.getMatchingDirectionFromAssertionKind=w,exports.getPattern=y,exports.hasSomeAncestor=C,exports.hasSomeDescendant=g,exports.invertMatchingDirection=k,exports.isEmpty=function(e){return i(e,p)},exports.isEmptyBackreference=S,exports.isLengthRangeMinZero=R,exports.isPotentiallyEmpty=function(e){return u(e,m)},exports.isPotentiallyZeroLength=function(e){return u(e,(e=>f(e,e)))},exports.isStrictBackreference=A,exports.isZeroLength=l,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?j(e,t).isAll:"any"===e.kind&&!!t.dotAll:!(!e.negate||0!==e.elements.length)||(e.negate?j(e.elements,t).isEmpty:j(e.elements,t).isAll))},exports.matchesNoCharacters=function(e,t){return"Character"!==e.type&&"CharacterClassRange"!==e.type&&("CharacterSet"===e.type?"property"===e.kind&&j(e,t).isEmpty:!e.negate&&0===e.elements.length||(e.negate?j(e.elements,t).isAll:j(e.elements,t).isEmpty))},exports.structurallyEqual=z,exports.toCache=J,exports.toCharSet=j; |
{ | ||
"name": "regexp-ast-analysis", | ||
"version": "0.4.1", | ||
"version": "0.5.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
1317
78425
5