regex-recursion
Advanced tools
Comparing version 5.0.0 to 5.1.0
@@ -1,2 +0,17 @@ | ||
var Regex;(Regex||={}).plugins=(()=>{var A=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var H=Object.prototype.hasOwnProperty;var B=(e,t)=>{for(var r in t)A(e,r,{get:t[r],enumerable:!0})},j=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of _(t))!H.call(e,n)&&n!==r&&A(e,n,{get:()=>t[n],enumerable:!(o=P(t,n))||o.enumerable});return e};var z=e=>j(A({},"__esModule",{value:!0}),e);var K={};B(K,{recursion:()=>J});var f=Object.freeze({DEFAULT:"DEFAULT",CHAR_CLASS:"CHAR_CLASS"});function x(e,t,r,o){let n=new RegExp(String.raw`${t}|(?<$skip>\[\^?|\\?.)`,"gsu"),s=[!1],u=0,c="";for(let i of e.matchAll(n)){let{0:l,groups:{$skip:a}}=i;if(!a&&(!o||o===f.DEFAULT==!u)){r instanceof Function?c+=r(i,{context:u?f.CHAR_CLASS:f.DEFAULT,negated:s[s.length-1]}):c+=r;continue}l[0]==="["?(u++,s.push(l[1]==="^")):l==="]"&&u&&(u--,s.pop()),c+=l}return c}function I(e,t,r,o){x(e,t,r,o)}function q(e,t,r=0,o){if(!new RegExp(t,"su").test(e))return null;let n=new RegExp(`${t}|(?<$skip>\\\\?.)`,"gsu");n.lastIndex=r;let s=0,u;for(;u=n.exec(e);){let{0:c,groups:{$skip:i}}=u;if(!i&&(!o||o===f.DEFAULT==!s))return u;c==="["?s++:c==="]"&&s&&s--,n.lastIndex==u.index&&n.lastIndex++}return null}function C(e,t,r){return!!q(e,t,0,r)}function T(e,t){let r=/\\?./gsu;r.lastIndex=t;let o=e.length,n=0,s=1,u;for(;u=r.exec(e);){let[c]=u;if(c==="[")n++;else if(n)c==="]"&&n--;else if(c==="(")s++;else if(c===")"&&(s--,!s)){o=u.index;break}}return e.slice(t,o)}var h=String.raw,y=h`\\g<(?<gRNameOrNum>[^>&]+)&R=(?<gRDepth>[^>]+)>`,b=h`\(\?R=(?<rDepth>[^\)]+)\)|${y}`,R=h`\(\?<(?![=!])(?<captureName>[^>]+)>`,$=new RegExp(h`${R}|${b}|\(\?|\\?.`,"gsu"),N="Cannot use multiple overlapping recursions",S="$E$";function J(e,t){if(!new RegExp(b,"su").test(e))return e;if(C(e,h`\(\?\(DEFINE\)`,f.DEFAULT))throw new Error("DEFINE groups cannot be used with recursion");let r=!!t?.useEmulationGroups,o=C(e,h`\\[1-9]`,f.DEFAULT),n=new Map,s=[],u=!1,c=0,i=0,l;for($.lastIndex=0;l=$.exec(e);){let{0:a,groups:{captureName:m,rDepth:g,gRNameOrNum:p,gRDepth:D}}=l;if(a==="[")c++;else if(c)a==="]"&&c--;else if(g){if(O(g),u)throw new Error(N);if(o)throw new Error("Numbered backrefs cannot be used with global recursion");let d=e.slice(0,l.index),w=e.slice($.lastIndex);if(C(w,b,f.DEFAULT))throw new Error(N);return G(d,w,+g,!1,r)}else if(p){O(D);let d=!1;for(let E of s)if(E.name===p||E.num===+p){if(d=!0,E.hasRecursedWithin)throw new Error(N);break}if(!d)throw new Error(h`Recursive \g cannot be used outside the referenced group "\g<${p}&R=${D}>"`);let w=n.get(p),U=T(e,w);if(o&&C(U,h`${R}|\((?!\?)`,f.DEFAULT))throw new Error("Numbered backrefs cannot be used with recursion of capturing groups");let k=e.slice(w,l.index),F=U.slice(k.length+a.length),L=G(k,F,+D,!0,r),v=e.slice(0,w),W=e.slice(w+U.length);e=`${v}${L}${W}`,$.lastIndex+=L.length-a.length-k.length-F.length,s.forEach(E=>E.hasRecursedWithin=!0),u=!0}else if(m)i++,n.set(String(i),$.lastIndex),n.set(m,$.lastIndex),s.push({num:i,name:m});else if(a.startsWith("(")){let d=a==="(";d&&(i++,n.set(String(i),$.lastIndex)),s.push(d?{num:i}:{})}else a===")"&&s.pop()}return e}function O(e){let t=`Max depth must be integer between 2 and 100; used ${e}`;if(!/^[1-9]\d*$/.test(e))throw new Error(t);if(e=+e,e<2||e>100)throw new Error(t)}function G(e,t,r,o,n){let s=new Set;o&&I(e+t,R,({groups:{captureName:c}})=>{s.add(c)},f.DEFAULT);let u=r-1;return`${e}${M(`(?:${e}`,u,o?s:null,"forward",n)}(?:)${M(`${t})`,u,o?s:null,"backward",n)}${t}`}function M(e,t,r,o,n){let u=i=>o==="backward"?t-i+2-1:i+2,c="";for(let i=0;i<t;i++){let l=u(i);c+=x(e,h`${R}|\\k<(?<backref>[^>]+)>${n?h`|\((?!\?)`:""}`,({0:a,groups:{captureName:m,backref:g}})=>{if(g&&r&&!r.has(g))return a;if(a==="(")return`(${S}`;let p=`_$${l}`;return m?`(?<${m}${p}>${n?S:""}`:h`\k<${g}${p}>`},f.DEFAULT)}return c}return z(K);})(); | ||
var Regex;(Regex||={}).plugins=(()=>{var N=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var H=(e,t)=>{for(var n in t)N(e,n,{get:t[n],enumerable:!0})},Q=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of q(t))!z.call(e,r)&&r!==n&&N(e,r,{get:()=>t[r],enumerable:!(a=W(t,r))||a.enumerable});return e};var Z=e=>Q(N({},"__esModule",{value:!0}),e);var X={};H(X,{recursion:()=>V});var l=Object.freeze({DEFAULT:"DEFAULT",CHAR_CLASS:"CHAR_CLASS"});function k(e,t,n,a){let r=new RegExp(String.raw`${t}|(?<$skip>\[\^?|\\?.)`,"gsu"),s=[!1],u=0,o="";for(let c of e.matchAll(r)){let{0:p,groups:{$skip:f}}=c;if(!f&&(!a||a===l.DEFAULT==!u)){n instanceof Function?o+=n(c,{context:u?l.CHAR_CLASS:l.DEFAULT,negated:s[s.length-1]}):o+=n;continue}p[0]==="["?(u++,s.push(p[1]==="^")):p==="]"&&u&&(u--,s.pop()),o+=p}return o}function F(e,t,n,a){k(e,t,n,a)}function j(e,t,n=0,a){if(!new RegExp(t,"su").test(e))return null;let r=new RegExp(`${t}|(?<$skip>\\\\?.)`,"gsu");r.lastIndex=n;let s=0,u;for(;u=r.exec(e);){let{0:o,groups:{$skip:c}}=u;if(!c&&(!a||a===l.DEFAULT==!s))return u;o==="["?s++:o==="]"&&s&&s--,r.lastIndex==u.index&&r.lastIndex++}return null}function A(e,t,n){return!!j(e,t,0,n)}function b(e,t){let n=/\\?./gsu;n.lastIndex=t;let a=e.length,r=0,s=1,u;for(;u=n.exec(e);){let[o]=u;if(o==="[")r++;else if(r)o==="]"&&r--;else if(o==="(")s++;else if(o===")"&&(s--,!s)){a=u.index;break}}return e.slice(t,a)}var E="$E$";var T=String.raw`\(\?(?:[:=!>A-Za-z\-]|<[=!]|\(DEFINE\))`;var ce=new RegExp(String.raw`(?<noncapturingStart>${T})|(?<capturingStart>\((?:\?<[^>]+>)?)|\\?.`,"gsu");var J=String.raw`(?:[?*+]|\{\d+(?:,\d*)?\})`,le=new RegExp(String.raw` | ||
\\(?: \d+ | ||
| c[A-Za-z] | ||
| [gk]<[^>]+> | ||
| [pPu]\{[^\}]+\} | ||
| u[A-Fa-f\d]{4} | ||
| x[A-Fa-f\d]{2} | ||
) | ||
| \((?: \? (?: [:=!>] | ||
| <(?:[=!]|[^>]+>) | ||
| [A-Za-z\-]+: | ||
| \(DEFINE\) | ||
))? | ||
| (?<qBase>${J})(?<qMod>[?+]?)(?<invalidQ>[?*+\{]?) | ||
| \\?. | ||
`.replace(/\s+/g,""),"gsu");var i=String.raw,K=i`\\g<(?<gRNameOrNum>[^>&]+)&R=(?<gRDepth>[^>]+)>`,U=i`\(\?R=(?<rDepth>[^\)]+)\)|${K}`,R=i`\(\?<(?![=!])(?<captureName>[^>]+)>`,d=new RegExp(i`${R}|${U}|\(\?|\\?.`,"gsu"),G="Cannot use multiple overlapping recursions";function V(e,t){if(!new RegExp(U,"su").test(e))return e;if(A(e,i`\(\?\(DEFINE\)`,l.DEFAULT))throw new Error("DEFINE groups cannot be used with recursion");let n=!!t?.useEmulationGroups,a=A(e,i`\\[1-9]`,l.DEFAULT),r=new Map,s=[],u=!1,o=0,c=0,p;for(d.lastIndex=0;p=d.exec(e);){let{0:f,groups:{captureName:C,rDepth:$,gRNameOrNum:h,gRDepth:w}}=p;if(f==="[")o++;else if(o)f==="]"&&o--;else if($){if(L($),u)throw new Error(G);if(a)throw new Error("Numbered backrefs cannot be used with global recursion");let g=e.slice(0,p.index),m=e.slice(d.lastIndex);if(A(m,U,l.DEFAULT))throw new Error(G);return _(g,m,+$,!1,n)}else if(h){L(w);let g=!1;for(let I of s)if(I.name===h||I.num===+h){if(g=!0,I.hasRecursedWithin)throw new Error(G);break}if(!g)throw new Error(i`Recursive \g cannot be used outside the referenced group "\g<${h}&R=${w}>"`);let m=r.get(h),x=b(e,m);if(a&&A(x,i`${R}|\((?!\?)`,l.DEFAULT))throw new Error("Numbered backrefs cannot be used with recursion of capturing groups");let D=e.slice(m,p.index),M=x.slice(D.length+f.length),S=_(D,M,+w,!0,n),y=e.slice(0,m),P=e.slice(m+x.length);e=`${y}${S}${P}`,d.lastIndex+=S.length-f.length-D.length-M.length,s.forEach(I=>I.hasRecursedWithin=!0),u=!0}else if(C)c++,r.set(String(c),d.lastIndex),r.set(C,d.lastIndex),s.push({num:c,name:C});else if(f.startsWith("(")){let g=f==="(";g&&(c++,r.set(String(c),d.lastIndex+(n?O(e,d.lastIndex):0))),s.push(g?{num:c}:{})}else f===")"&&s.pop()}return e}function L(e){let t=`Max depth must be integer between 2 and 100; used ${e}`;if(!/^[1-9]\d*$/.test(e))throw new Error(t);if(e=+e,e<2||e>100)throw new Error(t)}function _(e,t,n,a,r){let s=new Set;a&&F(e+t,R,({groups:{captureName:o}})=>{s.add(o)},l.DEFAULT);let u=n-1;return`${e}${v(`(?:${e}`,u,a?s:null,"forward",r)}(?:)${v(`${t})`,u,a?s:null,"backward",r)}${t}`}function v(e,t,n,a,r){let u=c=>a==="backward"?t-c+2-1:c+2,o="";for(let c=0;c<t;c++){let p=u(c);o+=k(e,i`${R}|\\k<(?<backref>[^>]+)>${r?i`|\((?!\?)`:""}`,({0:f,index:C,groups:{captureName:$,backref:h}})=>{if(h&&n&&!n.has(h))return f;if(f==="(")return`(${O(e,C+1)?"":E}`;let w=`_$${p}`;return $?`(?<${$}${w}>${r?E:""}`:i`\k<${h}${w}>`},l.DEFAULT)}return o}var B=new RegExp(i`(?:\$[1-9]\d*)?${E.replace(/\$/g,i`\$`)}`,"y");function O(e,t){B.lastIndex=t;let n=B.exec(e);return n?n[0].length:0}return Z(X);})(); | ||
//# sourceMappingURL=regex-recursion.min.js.map |
{ | ||
"name": "regex-recursion", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Recursive matching plugin for Regex+", | ||
@@ -40,10 +40,10 @@ "author": "Steven Levithan", | ||
"dependencies": { | ||
"regex": "^5.1.0", | ||
"regex-utilities": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"esbuild": "^0.24.0", | ||
"esbuild": "^0.24.2", | ||
"jasmine": "^5.5.0", | ||
"regex": "^5.0.2", | ||
"typescript": "^5.7.2" | ||
"typescript": "~5.7.2" | ||
} | ||
} |
@@ -33,4 +33,4 @@ # regex-recursion | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/regex@5.0.2/dist/regex.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/regex-recursion@5.0.0/dist/regex-recursion.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/regex@5.1.0/dist/regex.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/regex-recursion@5.1.0/dist/regex-recursion.min.js"></script> | ||
<script> | ||
@@ -37,0 +37,0 @@ const {regex} = Regex; |
import {Context, forEachUnescaped, getGroupContents, hasUnescaped, replaceUnescaped} from 'regex-utilities'; | ||
import {emulationGroupMarker} from 'regex/internals'; | ||
@@ -9,4 +10,2 @@ const r = String.raw; | ||
const overlappingRecursionMsg = 'Cannot use multiple overlapping recursions'; | ||
// See <github.com/slevithan/regex/blob/main/src/subclass.js> | ||
const emulationGroupMarker = '$E$'; | ||
@@ -116,3 +115,6 @@ /** | ||
numCaptures++; | ||
groupContentsStartPos.set(String(numCaptures), token.lastIndex); | ||
groupContentsStartPos.set( | ||
String(numCaptures), | ||
token.lastIndex + (useEmulationGroups ? emulationGroupMarkerLength(expression, token.lastIndex) : 0) | ||
); | ||
} | ||
@@ -189,3 +191,3 @@ openGroups.push(isUnnamedCapture ? {num: numCaptures} : {}); | ||
r`${namedCapturingDelim}|\\k<(?<backref>[^>]+)>${useEmulationGroups ? r`|\((?!\?)` : ''}`, | ||
({0: m, groups: {captureName, backref}}) => { | ||
({0: m, index, groups: {captureName, backref}}) => { | ||
if (backref && namesInRecursed && !namesInRecursed.has(backref)) { | ||
@@ -195,4 +197,6 @@ // Don't alter backrefs to groups outside the recursed subpattern | ||
} | ||
// `(` only matched if `useEmulationGroups` | ||
if (m === '(') { | ||
return `(${emulationGroupMarker}`; | ||
// Add an emulation group marker if there isn't one already | ||
return `(${emulationGroupMarkerLength(expression, index + 1) ? '' : emulationGroupMarker}`; | ||
} | ||
@@ -209,1 +213,9 @@ const suffix = `_$${captureNum}`; | ||
} | ||
const emulationGroupMarkerRe = new RegExp(r`(?:\$[1-9]\d*)?${emulationGroupMarker.replace(/\$/g, r`\$`)}`, 'y'); | ||
function emulationGroupMarkerLength(expression, index) { | ||
emulationGroupMarkerRe.lastIndex = index; | ||
const match = emulationGroupMarkerRe.exec(expression); | ||
return match ? match[0].length : 0; | ||
} |
Sorry, the diff of this file is not supported yet
55227
3
250
2
+ Addedregex@^5.1.0
+ Addedregex@5.1.1(transitive)