Files

3 lines
10 KiB
JavaScript

"use strict";import{tokenize as q}from"../tokenizer/tokenize.js";import{cpOf as H,getOrInsert as Z,PosixClassNames as x,r as y,throwIfNullish as g}from"../utils.js";import{hasOnlyChild as Y,isAlternativeContainer as j,isQuantifiable as S}from"./node-utils.js";function J(e,r={}){const n={flags:"",normalizeUnknownPropertyNames:!1,skipBackrefValidation:!1,skipLookbehindValidation:!1,skipPropertyNameValidation:!1,unicodePropertyMap:null,...r,rules:{captureGroup:!1,singleline:!1,...r.rules}},o=q(e,{flags:n.flags,rules:{captureGroup:n.rules.captureGroup,singleline:n.rules.singleline}}),i=(p,N)=>{const u=o.tokens[t.nextIndex];switch(t.parent=p,t.nextIndex++,u.type){case"Alternator":return b();case"Assertion":return W(u);case"Backreference":return X(u,t);case"Character":return m(u.value,{useLastValid:!!N.isCheckingRangeEnd});case"CharacterClassHyphen":return ee(u,t,N);case"CharacterClassOpen":return re(u,t,N);case"CharacterSet":return ne(u,t);case"Directive":return I(u.kind,{flags:u.flags});case"GroupOpen":return te(u,t,N);case"NamedCallout":return U(u.kind,u.tag,u.arguments);case"Quantifier":return oe(u,t);case"Subroutine":return ae(u,t);default:throw new Error(`Unexpected token type "${u.type}"`)}},t={capturingGroups:[],hasNumberedRef:!1,namedGroupsByName:new Map,nextIndex:0,normalizeUnknownPropertyNames:n.normalizeUnknownPropertyNames,parent:null,skipBackrefValidation:n.skipBackrefValidation,skipLookbehindValidation:n.skipLookbehindValidation,skipPropertyNameValidation:n.skipPropertyNameValidation,subroutines:[],tokens:o.tokens,unicodePropertyMap:n.unicodePropertyMap,walk:i},d=B(T(o.flags));let s=d.body[0];for(;t.nextIndex<o.tokens.length;){const p=i(s,{});p.type==="Alternative"?(d.body.push(p),s=p):s.body.push(p)}const{capturingGroups:a,hasNumberedRef:l,namedGroupsByName:c,subroutines:f}=t;if(l&&c.size&&!n.rules.captureGroup)throw new Error("Numbered backref/subroutine not allowed when using named capture");for(const{ref:p}of f)if(typeof p=="number"){if(p>a.length)throw new Error("Subroutine uses a group number that's not defined");p&&(a[p-1].isSubroutined=!0)}else if(c.has(p)){if(c.get(p).length>1)throw new Error(y`Subroutine uses a duplicate group name "\g<${p}>"`);c.get(p)[0].isSubroutined=!0}else throw new Error(y`Subroutine uses a group name that's not defined "\g<${p}>"`);return d}function W({kind:e}){return F(g({"^":"line_start",$:"line_end","\\A":"string_start","\\b":"word_boundary","\\B":"word_boundary","\\G":"search_start","\\y":"text_segment_boundary","\\Y":"text_segment_boundary","\\z":"string_end","\\Z":"string_end_newline"}[e],`Unexpected assertion kind "${e}"`),{negate:e===y`\B`||e===y`\Y`})}function X({raw:e},r){const n=/^\\k[<']/.test(e),o=n?e.slice(3,-1):e.slice(1),i=(t,d=!1)=>{const s=r.capturingGroups.length;let a=!1;if(t>s)if(r.skipBackrefValidation)a=!0;else throw new Error(`Not enough capturing groups defined to the left "${e}"`);return r.hasNumberedRef=!0,k(d?s+1-t:t,{orphan:a})};if(n){const t=/^(?<sign>-?)0*(?<num>[1-9]\d*)$/.exec(o);if(t)return i(+t.groups.num,!!t.groups.sign);if(/[-+]/.test(o))throw new Error(`Invalid backref name "${e}"`);if(!r.namedGroupsByName.has(o))throw new Error(`Group name not defined to the left "${e}"`);return k(o)}return i(+o)}function ee(e,r,n){const{tokens:o,walk:i}=r,t=r.parent,d=t.body.at(-1),s=o[r.nextIndex];if(!n.isCheckingRangeEnd&&d&&d.type!=="CharacterClass"&&d.type!=="CharacterClassRange"&&s&&s.type!=="CharacterClassOpen"&&s.type!=="CharacterClassClose"&&s.type!=="CharacterClassIntersector"){const a=i(t,{...n,isCheckingRangeEnd:!0});if(d.type==="Character"&&a.type==="Character")return t.body.pop(),L(d,a);throw new Error("Invalid character class range")}return m(H("-"))}function re({negate:e},r,n){const{tokens:o,walk:i}=r,t=[C()],d=o[r.nextIndex];let s=z(d);for(;s.type!=="CharacterClassClose";){if(s.type==="CharacterClassIntersector")t.push(C()),r.nextIndex++;else{const l=t.at(-1);l.body.push(i(l,n))}s=z(o[r.nextIndex],d)}const a=C({negate:e});return t.length===1?a.body=t[0].body:(a.kind="intersection",a.body=t.map(l=>l.body.length===1?l.body[0]:l)),r.nextIndex++,a}function ne({kind:e,negate:r,value:n},o){const{normalizeUnknownPropertyNames:i,skipPropertyNameValidation:t,unicodePropertyMap:d}=o;if(e==="property"){const s=w(n);if(x.has(s)&&!d?.has(s))e="posix",n=s;else return Q(n,{negate:r,normalizeUnknownPropertyNames:i,skipPropertyNameValidation:t,unicodePropertyMap:d})}return e==="posix"?R(n,{negate:r}):E(e,{negate:r})}function te(e,r,n){const{tokens:o,capturingGroups:i,namedGroupsByName:t,skipLookbehindValidation:d,walk:s}=r,a=ie(e),l=a.type==="AbsenceFunction",c=$(a),f=c&&a.negate;if(a.type==="CapturingGroup"&&(i.push(a),a.name&&Z(t,a.name,[]).push(a)),l&&n.isInAbsenceFunction)throw new Error("Nested absence function not supported by Oniguruma");let p=D(o[r.nextIndex]);for(;p.type!=="GroupClose";){if(p.type==="Alternator")a.body.push(b()),r.nextIndex++;else{const N=a.body.at(-1),u=s(N,{...n,isInAbsenceFunction:n.isInAbsenceFunction||l,isInLookbehind:n.isInLookbehind||c,isInNegLookbehind:n.isInNegLookbehind||f});if(N.body.push(u),(c||n.isInLookbehind)&&!d){const v="Lookbehind includes a pattern not allowed by Oniguruma";if(f||n.isInNegLookbehind){if(M(u)||u.type==="CapturingGroup")throw new Error(v)}else if(M(u)||$(u)&&u.negate)throw new Error(v)}}p=D(o[r.nextIndex])}return r.nextIndex++,a}function oe({kind:e,min:r,max:n},o){const i=o.parent,t=i.body.at(-1);if(!t||!S(t))throw new Error("Quantifier requires a repeatable token");const d=_(e,r,n,t);return i.body.pop(),d}function ae({raw:e},r){const{capturingGroups:n,subroutines:o}=r;let i=e.slice(3,-1);const t=/^(?<sign>[-+]?)0*(?<num>[1-9]\d*)$/.exec(i);if(t){const s=+t.groups.num,a=n.length;if(r.hasNumberedRef=!0,i={"":s,"+":a+s,"-":a+1-s}[t.groups.sign],i<1)throw new Error("Invalid subroutine number")}else i==="0"&&(i=0);const d=O(i);return o.push(d),d}function G(e,r){if(e!=="repeater")throw new Error(`Unexpected absence function kind "${e}"`);return{type:"AbsenceFunction",kind:e,body:h(r?.body)}}function b(e){return{type:"Alternative",body:V(e?.body)}}function F(e,r){const n={type:"Assertion",kind:e};return(e==="word_boundary"||e==="text_segment_boundary")&&(n.negate=!!r?.negate),n}function k(e,r){const n=!!r?.orphan;return{type:"Backreference",ref:e,...n&&{orphan:n}}}function P(e,r){const n={name:void 0,isSubroutined:!1,...r};if(n.name!==void 0&&!se(n.name))throw new Error(`Group name "${n.name}" invalid in Oniguruma`);return{type:"CapturingGroup",number:e,...n.name&&{name:n.name},...n.isSubroutined&&{isSubroutined:n.isSubroutined},body:h(r?.body)}}function m(e,r){const n={useLastValid:!1,...r};if(e>1114111){const o=e.toString(16);if(n.useLastValid)e=1114111;else throw e>1310719?new Error(`Invalid code point out of range "\\x{${o}}"`):new Error(`Invalid code point out of range in JS "\\x{${o}}"`)}return{type:"Character",value:e}}function C(e){const r={kind:"union",negate:!1,...e};return{type:"CharacterClass",kind:r.kind,negate:r.negate,body:V(e?.body)}}function L(e,r){if(r.value<e.value)throw new Error("Character class range out of order");return{type:"CharacterClassRange",min:e,max:r}}function E(e,r){const n=!!r?.negate,o={type:"CharacterSet",kind:e};return(e==="digit"||e==="hex"||e==="newline"||e==="space"||e==="word")&&(o.negate=n),(e==="text_segment"||e==="newline"&&!n)&&(o.variableLength=!0),o}function I(e,r={}){if(e==="keep")return{type:"Directive",kind:e};if(e==="flags")return{type:"Directive",kind:e,flags:g(r.flags)};throw new Error(`Unexpected directive kind "${e}"`)}function T(e){return{type:"Flags",...e}}function A(e){const r=e?.atomic,n=e?.flags;if(r&&n)throw new Error("Atomic group cannot have flags");return{type:"Group",...r&&{atomic:r},...n&&{flags:n},body:h(e?.body)}}function K(e){const r={behind:!1,negate:!1,...e};return{type:"LookaroundAssertion",kind:r.behind?"lookbehind":"lookahead",negate:r.negate,body:h(e?.body)}}function U(e,r,n){return{type:"NamedCallout",kind:e,tag:r,arguments:n}}function R(e,r){const n=!!r?.negate;if(!x.has(e))throw new Error(`Invalid POSIX class "${e}"`);return{type:"CharacterSet",kind:"posix",value:e,negate:n}}function _(e,r,n,o){if(r>n)throw new Error("Invalid reversed quantifier range");return{type:"Quantifier",kind:e,min:r,max:n,body:o}}function B(e,r){return{type:"Regex",body:h(r?.body),flags:e}}function O(e){return{type:"Subroutine",ref:e}}function Q(e,r){const n={negate:!1,normalizeUnknownPropertyNames:!1,skipPropertyNameValidation:!1,unicodePropertyMap:null,...r};let o=n.unicodePropertyMap?.get(w(e));if(!o){if(n.normalizeUnknownPropertyNames)o=de(e);else if(n.unicodePropertyMap&&!n.skipPropertyNameValidation)throw new Error(y`Invalid Unicode property "\p{${e}}"`)}return{type:"CharacterSet",kind:"property",value:o??e,negate:n.negate}}function ie({flags:e,kind:r,name:n,negate:o,number:i}){switch(r){case"absence_repeater":return G("repeater");case"atomic":return A({atomic:!0});case"capturing":return P(i,{name:n});case"group":return A({flags:e});case"lookahead":case"lookbehind":return K({behind:r==="lookbehind",negate:o});default:throw new Error(`Unexpected group kind "${r}"`)}}function h(e){if(e===void 0)e=[b()];else if(!Array.isArray(e)||!e.length||!e.every(r=>r.type==="Alternative"))throw new Error("Invalid body; expected array of one or more Alternative nodes");return e}function V(e){if(e===void 0)e=[];else if(!Array.isArray(e)||!e.every(r=>!!r.type))throw new Error("Invalid body; expected array of nodes");return e}function M(e){return e.type==="LookaroundAssertion"&&e.kind==="lookahead"}function $(e){return e.type==="LookaroundAssertion"&&e.kind==="lookbehind"}function se(e){return/^[\p{Alpha}\p{Pc}][^)]*$/u.test(e)}function de(e){return e.trim().replace(/[- _]+/g,"_").replace(/[A-Z][a-z]+(?=[A-Z])/g,"$&_").replace(/[A-Za-z]+/g,r=>r[0].toUpperCase()+r.slice(1).toLowerCase())}function w(e){return e.replace(/[- _]+/g,"").toLowerCase()}function z(e,r){const n=r;return g(e,`Unclosed character class${n?.type==="Character"&&n.value===93&&n.raw==="]"?' (started with "]")':""}`)}function D(e){return g(e,"Unclosed group")}export{G as createAbsenceFunction,b as createAlternative,F as createAssertion,k as createBackreference,P as createCapturingGroup,m as createCharacter,C as createCharacterClass,L as createCharacterClassRange,E as createCharacterSet,I as createDirective,T as createFlags,A as createGroup,K as createLookaroundAssertion,U as createNamedCallout,R as createPosixClass,_ as createQuantifier,B as createRegex,O as createSubroutine,Q as createUnicodeProperty,Y as hasOnlyChild,j as isAlternativeContainer,S as isQuantifiable,J as parse,w as slug};
//# sourceMappingURL=parse.js.map