import{adoptBuffer as t}from"./adopt-buffer.mjs";import{cmpStr as e}from"./utils.mjs";import s from"./names.mjs";import{RightParenthesis as n,Function as o,LeftParenthesis as i,RightSquareBracket as h,LeftSquareBracket as r,RightCurlyBracket as f,LeftCurlyBracket as k,EOF as u,WhiteSpace as a,Comment as d,Delim as p}from"./types.mjs";const l=16777215,c=24,y=new Uint8Array(32);function T(t){return 0!==y[t]}y[o]=n,y[i]=n,y[r]=h,y[k]=f;class C{constructor(t,e){this.setSource(t,e)}reset(){this.eof=!1,this.tokenIndex=-1,this.tokenType=0,this.tokenStart=this.firstCharOffset,this.tokenEnd=this.firstCharOffset}setSource(e="",s=()=>{}){const n=(e=String(e||"")).length,o=t(this.offsetAndType,e.length+1),i=t(this.balance,e.length+1);let h=0,r=-1,f=0,k=e.length;this.offsetAndType=null,this.balance=null,i.fill(0),s(e,(t,e,s)=>{const n=h++;if(o[n]=t<<c|s,-1===r&&(r=e),i[n]=k,t===f){const t=i[k];i[k]=n,k=t,f=y[o[t]>>c]}else T(t)&&(k=n,f=y[t])}),o[h]=u<<c|n,i[h]=h;for(let t=0;t<h;t++){const e=i[t];if(e<=t){const s=i[e];s!==t&&(i[t]=s)}else e>h&&(i[t]=h)}this.source=e,this.firstCharOffset=-1===r?0:r,this.tokenCount=h,this.offsetAndType=o,this.balance=i,this.reset(),this.next()}lookupType(t){return(t+=this.tokenIndex)<this.tokenCount?this.offsetAndType[t]>>c:u}lookupTypeNonSC(t){for(let e=this.tokenIndex;e<this.tokenCount;e++){const s=this.offsetAndType[e]>>c;if(s!==a&&s!==d&&0===t--)return s}return u}lookupOffset(t){return(t+=this.tokenIndex)<this.tokenCount?this.offsetAndType[t-1]&l:this.source.length}lookupOffsetNonSC(t){for(let e=this.tokenIndex;e<this.tokenCount;e++){const s=this.offsetAndType[e]>>c;if(s!==a&&s!==d&&0===t--)return e-this.tokenIndex}return u}lookupValue(t,s){return(t+=this.tokenIndex)<this.tokenCount&&e(this.source,this.offsetAndType[t-1]&l,this.offsetAndType[t]&l,s)}getTokenStart(t){return t===this.tokenIndex?this.tokenStart:t>0?t<this.tokenCount?this.offsetAndType[t-1]&l:this.offsetAndType[this.tokenCount]&l:this.firstCharOffset}substrToCursor(t){return this.source.substring(t,this.tokenStart)}isBalanceEdge(t){return this.balance[this.tokenIndex]<t}isDelim(t,e){return e?this.lookupType(e)===p&&this.source.charCodeAt(this.lookupOffset(e))===t:this.tokenType===p&&this.source.charCodeAt(this.tokenStart)===t}skip(t){let e=this.tokenIndex+t;e<this.tokenCount?(this.tokenIndex=e,this.tokenStart=this.offsetAndType[e-1]&l,e=this.offsetAndType[e],this.tokenType=e>>c,this.tokenEnd=e&l):(this.tokenIndex=this.tokenCount,this.next())}next(){let t=this.tokenIndex+1;t<this.tokenCount?(this.tokenIndex=t,this.tokenStart=this.tokenEnd,t=this.offsetAndType[t],this.tokenType=t>>c,this.tokenEnd=t&l):(this.eof=!0,this.tokenIndex=this.tokenCount,this.tokenType=u,this.tokenStart=this.tokenEnd=this.source.length)}skipSC(){for(;this.tokenType===a||this.tokenType===d;)this.next()}skipUntilBalanced(t,e){let s=t,n=0,o=0;t:for(;s<this.tokenCount&&(n=this.balance[s],!(n<t));s++)switch(o=s>0?this.offsetAndType[s-1]&l:this.firstCharOffset,e(this.source.charCodeAt(o))){case 1:break t;case 2:s++;break t;default:T(this.offsetAndType[s]>>c)&&(s=n)}this.skip(s-this.tokenIndex)}forEachToken(t){for(let e=0,s=this.firstCharOffset;e<this.tokenCount;e++){const n=s,o=this.offsetAndType[e],i=o&l;s=i,t(o>>c,n,i,e)}}dump(){const t=new Array(this.tokenCount);return this.forEachToken((e,n,o,i)=>{t[i]={idx:i,type:s[e],chunk:this.source.substring(n,o),balance:this.balance[i]}}),t}}export{C as TokenStream};