|
|
- import{Parser,NodeSet,NodeType,DefaultBufferLength,NodeProp,Tree,IterMode}from"@lezer/common";class Stack{constructor(p,stack,state,reducePos,pos,score,buffer,bufferBase,curContext,lookAhead=0,parent){this.p=p;this.stack=stack;this.state=state;this.reducePos=reducePos;this.pos=pos;this.score=score;this.buffer=buffer;this.bufferBase=bufferBase;this.curContext=curContext;this.lookAhead=lookAhead;this.parent=parent}toString(){return`[${this.stack.filter((_,i)=>i%3==0).concat(this.state)}]@${this.pos}${this.score?"!"+this.score:""}`}static start(p,state,pos=0){let cx=p.parser.context;return new Stack(p,[],state,pos,pos,0,[],0,cx?new StackContext(cx,cx.start):null,0,null)}get context(){return this.curContext?this.curContext.context:null}pushState(state,start){this.stack.push(this.state,start,this.bufferBase+this.buffer.length);this.state=state}reduce(action){let depth=action>>19,type=action&65535;let{parser}=this.p;let dPrec=parser.dynamicPrecedence(type);if(dPrec)this.score+=dPrec;if(depth==0){this.pushState(parser.getGoto(this.state,type,true),this.reducePos);if(type<parser.minRepeatTerm)this.storeNode(type,this.reducePos,this.reducePos,4,true);this.reduceContext(type,this.reducePos);return}let base=this.stack.length-(depth-1)*3-(action&262144?6:0);let start=this.stack[base-2];let bufferBase=this.stack[base-1],count=this.bufferBase+this.buffer.length-bufferBase;if(type<parser.minRepeatTerm||action&131072){let pos=parser.stateFlag(this.state,1)?this.pos:this.reducePos;this.storeNode(type,start,pos,count+4,true)}if(action&262144){this.state=this.stack[base]}else{let baseStateID=this.stack[base-3];this.state=parser.getGoto(baseStateID,type,true)}while(this.stack.length>base)this.stack.pop();this.reduceContext(type,start)}storeNode(term,start,end,size=4,isReduce=false){if(term==0&&(!this.stack.length||this.stack[this.stack.length-1]<this.buffer.length+this.bufferBase)){let cur=this,top=this.buffer.length;if(top==0&&cur.parent){top=cur.bufferBase-cur.parent.bufferBase;cur=cur.parent}if(top>0&&cur.buffer[top-4]==0&&cur.buffer[top-1]>-1){if(start==end)return;if(cur.buffer[top-2]>=start){cur.buffer[top-2]=end;return}}}if(!isReduce||this.pos==end){this.buffer.push(term,start,end,size)}else{let index=this.buffer.length;if(index>0&&this.buffer[index-4]!=0)while(index>0&&this.buffer[index-2]>end){this.buffer[index]=this.buffer[index-4];this.buffer[index+1]=this.buffer[index-3];this.buffer[index+2]=this.buffer[index-2];this.buffer[index+3]=this.buffer[index-1];index-=4;if(size>4)size-=4}this.buffer[index]=term;this.buffer[index+1]=start;this.buffer[index+2]=end;this.buffer[index+3]=size}}shift(action,next,nextEnd){let start=this.pos;if(action&131072){this.pushState(action&65535,this.pos)}else if((action&262144)==0){let nextState=action,{parser}=this.p;if(nextEnd>this.pos||next<=parser.maxNode){this.pos=nextEnd;if(!parser.stateFlag(nextState,1))this.reducePos=nextEnd}this.pushState(nextState,start);this.shiftContext(next,start);if(next<=parser.maxNode)this.buffer.push(next,start,nextEnd,4)}else{this.pos=nextEnd;this.shiftContext(next,start);if(next<=this.p.parser.maxNode)this.buffer.push(next,start,nextEnd,4)}}apply(action,next,nextEnd){if(action&65536)this.reduce(action);else this.shift(action,next,nextEnd)}useNode(value,next){let index=this.p.reused.length-1;if(index<0||this.p.reused[index]!=value){this.p.reused.push(value);index++}let start=this.pos;this.reducePos=this.pos=start+value.length;this.pushState(next,start);this.buffer.push(index,start,this.reducePos,-1);if(this.curContext)this.updateContext(this.curContext.tracker.reuse(this.curContext.context,value,this,this.p.stream.reset(this.pos-value.length)))}split(){let parent=this;let off=parent.buffer.length;while(off>0&&parent.buffer[off-2]>parent.reducePos)off-=4;let buffer=parent.buffer.slice(off),base=parent.bufferBase+off;while(parent&&base==parent.bufferBase)parent=parent.parent;return new Stack(this.p,this.stack.slice(),this.state,this.reducePos,this.pos,this.score,buffer,base,this.curContext,this.lookAhead,parent)}recoverByDelete(next,nextEnd){let isNode=next<=this.p.pars
|