import{MATCH as e,MISMATCH as t,DISALLOW_EMPTY as n}from"./match-graph.mjs";import{Comma as a,Ident as l,WhiteSpace as r,Comment as c,Function as s,LeftParenthesis as u,LeftSquareBracket as o,LeftCurlyBracket as p,RightParenthesis as i,RightSquareBracket as f,RightCurlyBracket as h,Delim as k}from"../tokenizer/types.mjs";const{hasOwnProperty:y}=Object.prototype,m="Match";function x(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++){const a=t.charCodeAt(n);let l=e.charCodeAt(n);if(l>=65&&l<=90&&(l|=32),l!==a)return!1}return!0}function d(e){return null===e||(e.type===a||e.type===s||e.type===u||e.type===o||e.type===p||function(e){return e.type===k&&"?"!==e.value}(e))}function b(e){return null===e||(e.type===i||e.type===f||e.type===h||e.type===k&&"/"===e.value)}function v(s,u,o){const p=function(s,u,o){function p(){do{I++,C=I<s.length?s[I]:null}while(null!==C&&(C.type===r||C.type===c))}function i(e){const t=I+e;return t<s.length?s[t]:null}function f(e,t){return{nextState:e,matchStack:E,syntaxStack:M,thenStack:w,tokenIndex:I,prev:t}}function h(e){w={nextState:e,matchStack:E,syntaxStack:M,prev:w}}function k(e){O=f(e,O)}function v(){E={type:1,syntax:u.syntax,token:C,prev:E},p(),T=null,I>j&&(j=I)}function g(){M={syntax:u.syntax,opts:u.syntax.opts||null!==M&&M.opts||null,prev:M},E={type:2,syntax:u.syntax,token:E.token,prev:E}}function S(){E=2===E.type?E.prev:{type:3,syntax:M.syntax,token:E.token,prev:E},M=M.prev}let M=null,w=null,O=null,T=null,A=0,B=null,C=null,I=-1,j=0,E={type:0,syntax:null,token:null,prev:null};for(p();null===B&&++A<15e3;)switch(u.type){case"Match":if(null===w){if(null!==C&&(I!==s.length-1||"\\0"!==C.value&&"\\9"!==C.value)){u=t;break}B=m;break}if((u=w.nextState)===n){if(w.matchStack===E){u=t;break}u=e}for(;w.syntaxStack!==M;)S();w=w.prev;break;case"Mismatch":if(null!==T&&!1!==T)(null===O||I>O.tokenIndex)&&(O=T,T=!1);else if(null===O){B="Mismatch";break}u=O.nextState,w=O.thenStack,M=O.syntaxStack,E=O.matchStack,I=O.tokenIndex,C=I<s.length?s[I]:null,O=O.prev;break;case"MatchGraph":u=u.match;break;case"If":u.else!==t&&k(u.else),u.then!==e&&h(u.then),u=u.match;break;case"MatchOnce":u={type:"MatchOnceBuffer",syntax:u,index:0,mask:0};break;case"MatchOnceBuffer":{const n=u.syntax.terms;if(u.index===n.length){if(0===u.mask||u.syntax.all){u=t;break}u=e;break}if(u.mask===(1<<n.length)-1){u=e;break}for(;u.index<n.length;u.index++){const e=1<<u.index;if(0===(u.mask&e)){k(u),h({type:"AddMatchOnce",syntax:u.syntax,mask:u.mask|e}),u=n[u.index++];break}}break}case"AddMatchOnce":u={type:"MatchOnceBuffer",syntax:u.syntax,index:0,mask:u.mask};break;case"Enum":if(null!==C){let e=C.value.toLowerCase();if(-1!==e.indexOf("\\")&&(e=e.replace(/\\[09].*$/,"")),y.call(u.map,e)){u=u.map[e];break}}u=t;break;case"Generic":{const n=null!==M?M.opts:null,a=I+Math.floor(u.fn(C,i,n));if(!isNaN(a)&&a>I){for(;I<a;)v();u=e}else u=t;break}case"Type":case"Property":{const e="Type"===u.type?"types":"properties",n=y.call(o,e)?o[e][u.name]:null;if(!n||!n.match)throw new Error("Bad syntax reference: "+("Type"===u.type?"<"+u.name+">":"<'"+u.name+"'>"));if(!1!==T&&null!==C&&"Type"===u.type&&("custom-ident"===u.name&&C.type===l||"length"===u.name&&"0"===C.value)){null===T&&(T=f(u,O)),u=t;break}g(),u=n.matchRef||n.match;break}case"Keyword":{const n=u.name;if(null!==C){let t=C.value;if(-1!==t.indexOf("\\")&&(t=t.replace(/\\[09].*$/,"")),x(t,n)){v(),u=e;break}}u=t;break}case"AtKeyword":case"Function":if(null!==C&&x(C.value,u.name)){v(),u=e;break}u=t;break;case"Token":if(null!==C&&C.value===u.value){v(),u=e;break}u=t;break;case"Comma":null!==C&&C.type===a?d(E.token)?u=t:(v(),u=b(C)?t:e):u=d(E.token)||b(C)?e:t;break;case"String":let r="",c=I;for(;c<s.length&&r.length<u.value.length;c++)r+=s[c].value;if(x(r,u.value)){for(;I<c;)v();u=e}else u=t;break;default:throw new Error("Unknown node type: "+u.type)}switch(B){case null:B="Maximum iteration number exceeded (please fill an issue on https://github.com/csstree/csstree/issues)",E=null;break;case m:for(;null!==M;)S();break;default:E=null}return{tokens:s,reason:B,iterations:A,match:E,longestMatch:j}}(s,u,o||{});if(null===p.match)return p;let i=p.match,f=p.match={syntax:u.syntax||null,match:[]};const h=[f];for(i=function(e){let t=null,n=null,a=e;for(;null!==a;)n=a.prev,a.prev=t,t=a,a=n;return t}(i).prev;null!==i;){switch(i.type){case 2:f.match.push(f={syntax:i.syntax,match:[]}),h.push(f);break;case 3:h.pop(),f=h[h.length-1];break;default:f.match.push({syntax:i.syntax||null,token:i.token.value,node:i.token.node})}i=i.prev}return p}export{v as matchAsTree};import g from"node:url";import S from"node:path";import M from"node:module";const w=g.fileURLToPath(import.meta.url);S.dirname(w),M.createRequire(import.meta.url);