Files
JiboViteDocs/node_modules/oniguruma-parser/dist/parser/parse.js

3 lines
10 KiB
JavaScript
Raw Normal View History

2026-05-03 12:12:57 -04:00
"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
//# sourceMappingURL=parse.js.map