regexp-ast-analysis
Advanced tools
Comparing version 0.2.3 to 0.2.4
# Changelog | ||
## 0.2.4 (2021-08-12) | ||
### Fixed | ||
- Fixed `getFirst{Consumed,}Char*` functions taking exponential time for some word boundary assertions. | ||
## 0.2.3 (2021-07-15) | ||
@@ -4,0 +11,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,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=[]}isCurrentWordBoundary(e){return this.currentWordBoundaries.some((t=>t===e))}pushWordBoundary(e){this.currentWordBoundaries.push(e)}popWordBoundary(){this.currentWordBoundaries.pop()}}function R(e,t,r,n){return L(e.map((e=>j(e,t,r,n))),r)}function j(e,t,n,a){switch(e.type){case"Assertion":switch(e.kind){case"word":if(a.isCurrentWordBoundary(e))return s();{a.pushWordBoundary(e);const r=N(e,g(t),n,a);a.popWordBoundary();const c=exports.Chars.word(n);return r.edge?r.char.isDisjointWith(c)?o(e.negate):s():r.char.isDisjointWith(c)?o(e.negate):r.char.isSubsetOf(c)?o(!e.negate):s()}case"end":case"start":return C(e.kind)===t?n.multiline?c({char:exports.Chars.lineTerminator(n),edge:!0,exact:!0}):c(function(e){return{char:exports.Chars.empty(e),edge:!0,exact:!0}}(n)):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?c({char:r.char.negate(),edge:!0,exact:!0}):s()}return c(q(R(e.alternatives,t,n,a)))}return s();default:throw r(e)}case"Character":case"CharacterSet":case"CharacterClass":return{char:G(e,n),empty:!1,exact:!0};case"Quantifier":{if(0===e.max)return c();const r=j(e.element,t,n,a);return 0===e.min?L([c(),r],n):r}case"Alternative":{let r=e.elements;return"rtl"===t&&(r=[...r],r.reverse()),T(function*(){for(const e of r)yield j(e,t,n,a)}(),n)}case"CapturingGroup":case"Group":return R(e.alternatives,t,n,a);case"Backreference":{if(x(e))return c();const r=j(e.resolved,t,n,a);return r.exact=r.exact&&r.char.size<=1,y(e)?r:L([r,c()],n)}default:throw r(e)}function s(){return c({char:exports.Chars.all(n),edge:!0,exact:!1})}function o(e){const t=exports.Chars.word(n);return c({char:e?t.negate():t,edge:e,exact:!0})}function c(e){return O(n,e)}}function M(e){return{char:exports.Chars.all(e),edge:!0,exact:!0}}function O(e,t){return{char:exports.Chars.empty(e),empty:!0,exact:!0,look:null!=t?t:M(e)}}class D{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 D(exports.Chars.empty(e))}static emptyFromMaximum(e){return new D(t.CharSet.empty(e))}}function L(e,t){const r=D.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=D.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 T(e,t){const r=D.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 q(e){if(e.empty){const t=D.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 I(e,t,r,n){return P(e,"next",O(r),{join:e=>L(e,r),enter:(e,t,a)=>T([t,j(e,a,r,n)],r),continueInto:()=>!1,continueAfter:(e,t)=>t.empty},t)}function N(e,t,r,n){return q(I(e,t,r,n))}function U(e,t,r,n){return P(e,"next",{char:O(r),contributors:[]},{join(e){const t=new Set;return e.forEach((e=>e.contributors.forEach((e=>t.add(e))))),{char:L(e.map((e=>e.char)),r),contributors:[...t]}},enter(e,t,a){const s=j(e,a,r,n);return{char:T([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 N(e,t,r,new Q)},exports.getFirstCharAfterWithContributors=function(e,t,r){return function(e,t,r,n){const{char:a,contributors:s}=U(e,t,r,n);return{char:q(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):j(e,t,r,n)},exports.getFirstConsumedCharAfter=function(e,t,r){return I(e,t,r,new Q)},exports.getFirstConsumedCharAfterWithContributors=function(e,t,r){return U(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)}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; |
{ | ||
"name": "regexp-ast-analysis", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"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
53733
860