Files
Library/node_modules/oniguruma-parser/dist/tokenizer/tokenize.js

35 lines
12 KiB
JavaScript
Raw Normal View History

2026-01-09 23:05:52 -05:00
"use strict";import{cpOf as h,PosixClassNames as G,r as p,throwIfNullish as N}from"../utils.js";const m=p`\[\^?`,b=`c.? | C(?:-.?)?|${p`[pP]\{(?:\^?[-\x20_]*[A-Za-z][-\x20\w]*\})?`}|${p`x[89A-Fa-f]\p{AHex}(?:\\x[89A-Fa-f]\p{AHex})*`}|${p`u(?:\p{AHex}{4})? | x\{[^\}]*\}? | x\p{AHex}{0,2}`}|${p`o\{[^\}]*\}?`}|${p`\d{1,3}`}`,y=/[?*+][?+]?|\{(?:\d+(?:,\d*)?|,\d+)\}\??/,C=new RegExp(p`
\\ (?:
${b}
| [gk]<[^>]*>?
| [gk]'[^']*'?
| .
)
| \( (?:
\? (?:
[:=!>({]
| <[=!]
| <[^>]*>
| '[^']*'
| ~\|?
| #(?:[^)\\]|\\.?)*
| [^:)]*[:)]
)?
| \*[^\)]*\)?
)?
| (?:${y.source})+
| ${m}
| .
`.replace(/\s+/g,""),"gsu"),T=new RegExp(p`
\\ (?:
${b}
| .
)
| \[:(?:\^?\p{Alpha}+|\^):\]
| ${m}
| &&
| .
`.replace(/\s+/g,""),"gsu");function M(e,n={}){const t={flags:"",...n,rules:{captureGroup:!1,singleline:!1,...n.rules}};if(typeof e!="string")throw new Error("String expected as pattern");const o=Y(t.flags),s=[o.extended],a={captureGroup:t.rules.captureGroup,getCurrentModX(){return s.at(-1)},numOpenGroups:0,popModX(){s.pop()},pushModX(u){s.push(u)},replaceCurrentModX(u){s[s.length-1]=u},singleline:t.rules.singleline};let r=[],i;for(C.lastIndex=0;i=C.exec(e);){const u=F(a,e,i[0],C.lastIndex);u.tokens?r.push(...u.tokens):u.token&&r.push(u.token),u.lastIndex!==void 0&&(C.lastIndex=u.lastIndex)}const l=[];let c=0;r.filter(u=>u.type==="GroupOpen").forEach(u=>{u.kind==="capturing"?u.number=++c:u.raw==="("&&l.push(u)}),c||l.forEach((u,S)=>{u.kind="capturing",u.number=S+1});const g=c||l.length;return{tokens:r.map(u=>u.type==="EscapedNumber"?ee(u,g):u).flat(),flags:o}}function F(e,n,t,o){const[s,a]=t;if(t==="["||t==="[^"){const r=K(n,t,o);return{tokens:r.tokens,lastIndex:r.lastIndex}}if(s==="\\"){if("AbBGyYzZ".includes(a))return{token:w(t,t)};if(/^\\g[<']/.test(t)){if(!/^\\g(?:<[^>]+>|'[^']+')$/.test(t))throw new Error(`Invalid group name "${t}"`);return{token:R(t)}}if(/^\\k[<']/.test(t)){if(!/^\\k(?:<[^>]+>|'[^']+')$/.test(t))throw new Error(`Invalid group name "${t}"`);return{token:A(t)}}if(a==="K")return{token:I("keep",t)};if(a==="N"||a==="R")return{token:k("newline",t,{negate:a==="N"})};if(a==="O")return{token:k("any",t)};if(a==="X")return{token:k("text_segment",t)};const r=x(t,{inCharClass:!1});return Array.isArray(r)?{tokens:r}:{token:r}}if(s==="("){if(a==="*")return{token:j(t)};if(t==="(?{")throw new Error(`Unsupported callout "${t}"`);if(t.startsWith("(?#")){if(n[o]!==")")throw new Error('Unclosed comment group "(?#"');return{lastIndex:o+1}}if(/^\(\?[-imx]+[:)]$/.test(t))return{token:L(t,e)};if(e.pushModX(e.getCurrentModX()),e.numOpenGroups++,t==="("&&!e.captureGroup||t==="(?:")return{token:f("group",t)};if(t==="(?>")return{token:f("atomic",t)};if(t==="(?="||t==="(?!"||t==="(?<="||t==="(?<!")return{token:f(t[2]==="<"?"lookbehind":"lookahead",t,{negate:t.endsWith("!")})};if(t==="("&&e.captureGroup||t.startsWith("(?<")&&t.endsWith(">")||t.startsWith("(?'")&&t.endsWith("'"))return{token:f("capturing",t,{...t!=="("&&{name:t.slice(3,-1)}})};if(t.startsWith("(?~")){if(t==="(?~|")throw new Error(`Unsupported absence function kind "${t}"`);return{token:f("absence_repeater",t)}}throw t==="(?("?new Error(`Unsupported conditional "${t}"`):new Error(`Invalid or unsupported group option "${t}"`)}if(t===")"){if(e.popModX(),e.numOpenGroups--,e.numOpenGroups<0)throw new Error('Unmatched ")"');return{token:Q(t)}}if(e.getCurrentModX()){if(t==="#"){const r=n.indexOf(`
`,o);return{lastIndex:r===-1?n.length:r}}if(/^\s$/.test(t)){const r=/\s+/y;return r.lastIndex=o,{lastIndex:r.exec(n)?r.lastIndex:o}}}if(t===".")return{token:k("dot",t)};if(t==="^"||t==="$"){const r=e.singleline?{"^":p`\A`,$:p`\Z`}[t]:t;return{token:w(r,t)}}return t==="|"?{token:P(t)}:y.test(t)?{tokens:te(t)}:{token:d(h(t),t)}}function K(e,n,t){const o=[E(n[1]==="^",n)];let s=1,a;for(T.lastIndex=t;a=T.exec(e);){const r=a[0];if(r[0]==="["&&r[1]!==":")s++,o.push(E(r[1]==="^",r));else if(r==="]"){if(o.at(-1).type==="CharacterClassOpen")o.push(d(93,r));else if(s--,o.push(z(r)),!s)break}else{const i=X(r);Array.isArray(i)?o.push(...i):o.push(i)}}return{tokens:o,lastIndex:T.lastIndex||e.length}}function X(e){if(e[0]==="\\")return x(e,{inCharClass:!0});if(e[0]==="["){const n=/\[:(?<negate>\^?)(?<name>[a-z]+):\]/.exec(e);if(!n||!G.has(n.groups.name))throw new Error(`Invalid POSIX class "${e}"`);return k("posix",e,{value:n.groups.name,negate:!!n.groups.negate})}return e==="-"?U(e):e==="&&"?H(e):d(h(e),e)}function x(e,{inCharClass:n}){const t=e[1];if(t==="c"||t==="C")return Z(e);if("dDhHsSwW".includes(t))return q(e);if(e.startsWith(p`\o{`))throw new Error(`Incomplete, invalid, or unsupported octal code point "${e}"`);if(/^\\[pP]\{/.test(e)){if(e.length===3)throw new Error(`Incomplete or invalid Unicode property "${e}"`);return V(e)}if(/^\\x[89A-Fa-f]\p{AHex}/u.test(e))try{const o=e.split(/\\x/).slice(1).map(i=>parseInt(i,16)),s=new TextDecoder("utf-8",{ignoreBOM:!0,fatal:!0}).decode(new Uint8Array(o)),a=new TextEncoder;return[...s].map(i=>{const l=[...a.encode(i)].map(c=>`\\x${c.toString(16)}`).join("");return d(h(i),l)})}catch{throw new Error(`Multibyte code "${e}" incomplete or invalid in Oniguruma`)}if(t==="u"||t==="x")return d(J(e),e);if($.has(t))return d($.get(t),e);if(/\d/.test(t))return W(n,e);if(e==="\\")throw new Error(p`Incomplete escape "\"`);if(t==="M")throw new Error(`Unsupported meta "${e}"`);if([...e].length===2)return d(e.codePointAt(1),e);throw new Error(`Unexpected escape "${e}"`)}function P(e){return{type:"Alternator",raw:e}}function w(e,n){return{type:"Assertion",kind:e,raw:n}}function A(e){return{type:"Backreference",raw:e}}function d(e,n){return{type:"Character",value:e,raw:n}}function z(e){return{type:"CharacterClassClose",raw:e}}function U(e){return{type:"CharacterClassHyphen",raw:e}}function H(e){return{type:"CharacterClassIntersector",raw:e}}function E(e,n){return{type:"CharacterClassOpen",negate:e,raw:n}}function k(e,n,t={}){return{type:"CharacterSet",kind:e,...t,raw:n}}function I(e,n,t={}){return e==="keep"?{type:"Directive",kind:e,raw:n}:{type:"Directive",kind:e,flags:N(t.flags),raw:n}}function W(e,n){return{type:"EscapedNumber",inCharClass:e,raw:n}}function Q(e){return{type:"GroupClose",raw:e}}function f(e,n,t={}){return{type:"GroupOpen",kind:e,...t,raw:n}}function D(e,n,t,o){return{type:"NamedCallout",kind:e,tag:n,arguments:t,raw:o}}function _(e,n,t,o){return{type:"Quantifier",kind:e,min:n,max:t,raw:o}}function R(e){return{type:"Subroutine",raw:e}}const B=new Set(["COUNT","CMP","ERROR","FAIL","MAX","MISMATCH","SKIP","TOTAL_COUNT"]),$=new Map([["a",7],["b",8],["e",27],["f",12],["n",10],["r",13],["t",9],["v",11]]);function Z(e){const n=e[1]==="c"?e[2]:e[3];if(!n||!/[A-Za-z]/.test(n))throw new Error(`Unsupported control character "${e}"`);return d(h(n.toUpperCase())-64,e)}function L(e,n){let{on:t,off:o}=/^\(\?(?<on>[imx]*)(?:-(?<off>[-imx]*))?/.exec(e).groups;o??="";const s=(n.getCurrentModX()||t.includes("x"))&&!o.includes("x"),a=v(t),r=v(o),i={};if(a&&(i.enable=a),r&&(i.disable=r),e.endsWith(")"))return n.replaceCurrentModX(s),I("flags",e,{flags:i});if(e.endsWith(":"))return n.pushModX(s),n.numOpenGroups++,f("group",e,{...(a||r)&&{flags:i}});throw new Error(`Unexpected flag modifier "${e}"`)}function j(e){const n=/\(\*(?<name>[A-Za-z_]\w*)?(?:\[(?<tag>(?:[A-Za-z_]\w*)?)\])?(?:\{(?<args>[^}]*)\})?\)/.exec(e);if(!n)throw new Error(`Incomplete or invalid named callout "${e}"`);const{name:t,tag:o,args:s}=n.groups;if(!t)throw new Error(`Invalid named callout "${e}"`);if(o==="")throw new Error(`Named callout
//# sourceMappingURL=tokenize.js.map