/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import{CommentHandler as t}from"./comment-handler.js";import{ErrorHandler as e,formatErrorDescription as r}from"./error-handler.js";import{Scanner as i}from"./scanner.js";import{OperatorPrecedence as n,TokenType as s,ParsingErrorCodes as a,ParsingError as o,Syntax as h,TokenNames as c,UpdateOperators as p,UnaryOperators as l,LogicalOperators as u,AssignmentOperators as m,isEmptyStatement as d}from"./types.js";var x,k;function w(t,e=0){let r=t.start-t.lineStart,i=t.lineNumber;return r<0&&(r+=e,i--),{index:t.start,line:i,column:r}}function g(t){return[{index:t.range[0],...t.loc.start},{index:t.range[1],...t.loc.end}]}function y(t){return n[t]??0}!function(t){t[t.None=0]="None",t[t.Function=1]="Function",t[t.IfClause=2]="IfClause",t[t.ForLoop=4]="ForLoop",t[t.WhileLoop=8]="WhileLoop"}(x||(x={})),function(t){t[t.AsObject=0]="AsObject",t[t.Automatic=1]="Automatic"}(k||(k={}));class T{constructor(r,n={},a){this.delegate=a,this.hasLineTerminator=!1,this.options={tokens:"boolean"==typeof n.tokens&&n.tokens,comments:"boolean"==typeof n.comments&&n.comments,tolerant:"boolean"==typeof n.tolerant&&n.tolerant},this.options.comments&&(this.commentHandler=new t),this.errorHandler=new e(this.options.tolerant),this.scanner=new i(r,this.errorHandler),this.context={isAssignmentTarget:!1,blockContext:x.None,curlyParsingType:k.AsObject},this.rawToken={type:s.EOF,value:"",lineNumber:this.scanner.lineNumber,lineStart:0,start:0,end:0},this.tokens=[],this.startMarker={index:0,line:this.scanner.lineNumber,column:0},this.endMarker={index:0,line:this.scanner.lineNumber,column:0},this.readNextRawToken(),this.endMarker={index:this.scanner.index,line:this.scanner.lineNumber,column:this.scanner.index-this.scanner.lineStart}}throwIfInvalidType(t,e,{validTypes:r,invalidTypes:i}){r?.some((e=>t.type===e))||i?.some((e=>t.type===e))&&this.throwError(a.InvalidExpression,e)}throwError(t,e,r=this.endMarker){const{index:i,line:n,column:s}=e,a=r.index-i-1;this.errorHandler.throwError({code:t,index:i,line:n,column:s+1,len:a})}tolerateError(t,e){throw new Error("######################################### !!!")}unexpectedTokenError(t={}){const{rawToken:e}=t;let i,{code:n,data:h}=t;if(e){if(!n)switch(e.type){case s.EOF:n=a.UnexpectedEndOfScript;break;case s.Identifier:n=a.UnexpectedIdentifier;break;case s.NumericLiteral:n=a.UnexpectedNumber;break;case s.StringLiteral:n=a.UnexpectedString;break;case s.Template:n=a.UnexpectedTemplate}i=e.value.toString()}else i="ILLEGAL";n=n??a.UnexpectedToken,h||(h={value:i});const c=r(n,h);if(e){const t=e.start,r=e.lineNumber,i=e.start-e.lineStart+1;return new o({code:n,index:t,line:r,column:i,len:e.end-e.start-1,data:h,description:c})}const{index:p,line:l}=this.endMarker;return new o({code:n,index:p,line:l,column:this.endMarker.column+1,data:h,description:c})}throwUnexpectedToken(t={}){throw t.rawToken=t.rawToken??this.rawToken,this.unexpectedTokenError(t)}collectComments(t){const{commentHandler:e}=this;e&&t?.length&&t.forEach((t=>{const r={type:t.multiLine?h.BlockComment:h.LineComment,value:this.getSourceValue(t),range:t.range,loc:t.loc};e.collectComment(r)}))}peekAhead(t){const e=()=>(this.scanner.scanComments(),this.scanner.lex()),r=this.scanner.saveState(),i=t.call(this,e);return this.scanner.restoreState(r),i}getSourceValue(t){return this.scanner.source.slice(t.start,t.end)}convertToToken(t){return{type:c[t.type],value:this.getSourceValue(t),range:[t.start,t.end],loc:{start:{line:this.startMarker.line,column:this.startMarker.column},end:{line:this.scanner.lineNumber,column:this.scanner.index-this.scanner.lineStart}}}}readNextRawToken(){this.endMarker.index=this.scanner.index,this.endMarker.line=this.scanner.lineNumber,this.endMarker.column=this.scanner.index-this.scanner.lineStart;const t=this.rawToken;this.collectComments(this.scanner.scanComments()),this.scanner.index!==this.startMarker.index&&(this.startMarker.index=this.scanner.index,this.startMarker.line=this.scanner.lineNumber,this.startMarker.column=this.scanner.index-this.scanner.lineStart),this.rawToken=this.scanner.lex(),this.hasLineTerminator=t.lineNumber!==this.rawToken.lineNumber,this.options.tokens&&this.rawToken.type!==s.EOF&&this.tokens.push(this.convertToToken(this.rawToken))}captureStartMarker(){return{index:this.startMarker.index,line:this.startMarker.line,column:this.startMarker.column}}getItemLocation(t){return{range:[t.index,this.endMarker.index],loc:{start:{line:t.line,column:t.column},end:{line:this.endMarker.line,column:this.endMarker.column}}}}finalize(t){return(this.delegate||this.commentHandler)&&(this.commentHandler?.attachComments(t),this.delegate?.(t)),t}expectPunctuator(t){const e=this.rawToken;this.matchPunctuator(t)?this.readNextRawToken():this.throwUnexpectedToken({rawToken:e,code:a.PunctuatorExpected,data:{value:t}})}expectKeyword(t){this.rawToken.type!==s.Keyword||this.rawToken.value.toLowerCase()!==t?this.throwUnexpectedToken({rawToken:this.rawToken}):this.readNextRawToken()}expectContextualKeyword(t){this.rawToken.type!==s.Identifier||this.rawToken.value.toLowerCase()!==t?this.throwUnexpectedToken({rawToken:this.rawToken}):this.readNextRawToken()}matchKeyword(t){return this.rawToken.type===s.Keyword&&this.rawToken.value.toLowerCase()===t}matchContextualKeyword(t){return this.rawToken.type===s.Identifier&&this.rawToken.value===t}matchPunctuator(t){return this.rawToken.type===s.Punctuator&&this.rawToken.value===t}getMatchingPunctuator(t){if("string"==typeof t&&(t=t.split("")),this.rawToken.type===s.Punctuator&&t?.length)return t.find(this.matchPunctuator,this)}isolateCoverGrammar(t){const e=this.context.isAssignmentTarget;this.context.isAssignmentTarget=!0;const r=t.call(this);return this.context.isAssignmentTarget=e,r}inheritCoverGrammar(t){const e=this.context.isAssignmentTarget;this.context.isAssignmentTarget=!0;const r=t.call(this);return this.context.isAssignmentTarget=this.context.isAssignmentTarget&&e,r}withBlockContext(t,e){const r=this.context.blockContext;this.context.blockContext=this.context.blockContext|t;const i=this.context.curlyParsingType;this.context.curlyParsingType=k.Automatic;const n=e.call(this);return this.context.blockContext=r,this.context.curlyParsingType=i,n}consumeSemicolon(){if(this.matchPunctuator(";"))this.readNextRawToken();else if(!this.hasLineTerminator)return this.rawToken.type===s.EOF||this.matchPunctuator("}")?(this.endMarker.index=this.startMarker.index,this.endMarker.line=this.startMarker.line,void(this.endMarker.column=this.startMarker.column)):void this.throwUnexpectedToken({rawToken:this.rawToken})}parsePrimaryExpression(){const t=this.captureStartMarker(),e=this.rawToken;switch(e.type){case s.Identifier:return this.readNextRawToken(),this.finalize({type:h.Identifier,name:e.value,...this.getItemLocation(t)});case s.NumericLiteral:case s.StringLiteral:return this.context.isAssignmentTarget=!1,this.readNextRawToken(),this.finalize({type:h.Literal,value:e.value,raw:this.getSourceValue(e),isString:"string"==typeof e.value,...this.getItemLocation(t)});case s.BooleanLiteral:return this.context.isAssignmentTarget=!1,this.readNextRawToken(),this.finalize({type:h.Literal,value:"true"===e.value.toLowerCase(),raw:this.getSourceValue(e),isString:!1,...this.getItemLocation(t)});case s.NullLiteral:return this.context.isAssignmentTarget=!1,this.readNextRawToken(),this.finalize({type:h.Literal,value:null,raw:this.getSourceValue(e),isString:!1,...this.getItemLocation(t)});case s.Template:return this.parseTemplateLiteral();case s.Punctuator:switch(e.value){case"(":return this.inheritCoverGrammar(this.parseGroupExpression);case"[":return this.inheritCoverGrammar(this.parseArrayInitializer);case"{":return this.inheritCoverGrammar(this.parseObjectExpression);default:return this.throwUnexpectedToken({rawToken:this.rawToken})}case s.Keyword:return this.context.isAssignmentTarget=!1,this.throwUnexpectedToken({rawToken:this.rawToken});default:return this.throwUnexpectedToken({rawToken:this.rawToken})}}parseArrayInitializer(){const t=this.captureStartMarker();this.expectPunctuator("[");const e=[];for(;!this.matchPunctuator("]");)this.matchPunctuator(",")?(this.readNextRawToken(),e.push(null)):(e.push(this.inheritCoverGrammar(this.parseAssignmentExpression)),this.matchPunctuator("]")||this.expectPunctuator(","));return this.expectPunctuator("]"),this.finalize({type:h.ArrayExpression,elements:e,...this.getItemLocation(t)})}parseObjectPropertyKey(){const t=this.captureStartMarker(),e=this.rawToken;switch(e.type){case s.StringLiteral:return this.readNextRawToken(),this.finalize({type:h.Literal,value:e.value,raw:this.getSourceValue(e),isString:!0,...this.getItemLocation(t)});case s.Identifier:case s.BooleanLiteral:case s.NullLiteral:case s.Keyword:return this.readNextRawToken(),this.finalize({type:h.Identifier,name:e.value,...this.getItemLocation(t)});default:this.throwError(a.KeyMustBeString,t)}}parseObjectProperty(){const t=this.rawToken,e=this.captureStartMarker(),r=this.parseObjectPropertyKey();let i=!1,n=null;return this.matchPunctuator(":")?(this.readNextRawToken(),n=this.inheritCoverGrammar(this.parseAssignmentExpression)):t.type===s.Identifier?(i=!0,n=this.finalize({type:h.Identifier,name:t.value,...this.getItemLocation(e)})):this.throwUnexpectedToken({rawToken:this.rawToken}),this.finalize({type:h.Property,kind:"init",key:r,value:n,shorthand:i,...this.getItemLocation(e)})}parseObjectExpression(){const t=this.captureStartMarker();this.expectPunctuator("{");const e=[];for(;!this.matchPunctuator("}");)e.push(this.parseObjectProperty()),this.matchPunctuator("}")||this.expectPunctuator(",");return this.expectPunctuator("}"),this.finalize({type:h.ObjectExpression,properties:e,...this.getItemLocation(t)})}parseTemplateElement(t=!1){const e=this.rawToken;e.type!==s.Template&&this.throwUnexpectedToken({rawToken:e}),t&&!e.head&&this.throwUnexpectedToken({code:a.InvalidTemplateHead,rawToken:e});const r=this.captureStartMarker();this.readNextRawToken();const{value:i,cooked:n,tail:o}=e,c=this.finalize({type:h.TemplateElement,value:{raw:i,cooked:n},tail:o,...this.getItemLocation(r)});return c.loc.start.column++,c.loc.end.column=c.loc.end.column-(o?1:2),c}parseTemplateLiteral(){const t=this.captureStartMarker(),e=[],r=[];let i=this.parseTemplateElement(!0);for(r.push(i);!i.tail;)e.push(this.parseExpression()),i=this.parseTemplateElement(),r.push(i);return this.finalize({type:h.TemplateLiteral,quasis:r,expressions:e,...this.getItemLocation(t)})}parseGroupExpression(){this.expectPunctuator("(");const t=this.inheritCoverGrammar(this.parseAssignmentExpression);return this.expectPunctuator(")"),t}parseArguments(){this.expectPunctuator("(");const t=[];if(!this.matchPunctuator(")"))for(;;){const e=this.isolateCoverGrammar(this.parseAssignmentExpression);if(t.push(e),this.matchPunctuator(")"))break;if(this.expectPunctuator(","),this.matchPunctuator(")"))break}return this.expectPunctuator(")"),t}parseMemberName(){const t=this.rawToken,e=this.captureStartMarker();return this.readNextRawToken(),t.type!==s.NullLiteral&&t.type!==s.Identifier&&t.type!==s.Keyword&&t.type!==s.BooleanLiteral&&this.throwUnexpectedToken({rawToken:t}),this.finalize({type:h.Identifier,name:t.value,...this.getItemLocation(e)})}parseLeftHandSideExpression(){const t=this.captureStartMarker();let e=this.inheritCoverGrammar(this.parsePrimaryExpression);const r=this.captureStartMarker();let i;for(;i=this.getMatchingPunctuator("([.");)switch(i){case"(":{this.context.isAssignmentTarget=!1,e.type!==h.Identifier&&e.type!==h.MemberExpression&&this.throwError(a.IdentiferExpected,t,r);const i=this.parseArguments();e=this.finalize({type:h.CallExpression,callee:e,arguments:i,...this.getItemLocation(t)});continue}case"[":{this.context.isAssignmentTarget=!0,this.expectPunctuator("[");const r=this.isolateCoverGrammar(this.parseExpression);this.expectPunctuator("]"),e=this.finalize({type:h.MemberExpression,computed:!0,object:e,property:r,...this.getItemLocation(t)});continue}case".":{this.context.isAssignmentTarget=!0,this.expectPunctuator(".");const r=this.parseMemberName();e=this.finalize({type:h.MemberExpression,computed:!1,object:e,property:r,...this.getItemLocation(t)});continue}}return e}parseUpdateExpression(){const t=this.captureStartMarker();let e=this.getMatchingPunctuator(p);if(e){this.readNextRawToken();const r=this.captureStartMarker(),i=this.inheritCoverGrammar(this.parseUnaryExpression);return i.type!==h.Identifier&&i.type!==h.MemberExpression&&i.type!==h.CallExpression&&this.throwError(a.InvalidExpression,r),this.context.isAssignmentTarget||this.tolerateError(a.InvalidLeftHandSideInAssignment,t),this.context.isAssignmentTarget=!1,this.finalize({type:h.UpdateExpression,operator:e,argument:i,prefix:!0,...this.getItemLocation(t)})}const r=this.captureStartMarker(),i=this.inheritCoverGrammar(this.parseLeftHandSideExpression),n=this.captureStartMarker();return this.hasLineTerminator?i:(e=this.getMatchingPunctuator(p),e?(i.type!==h.Identifier&&i.type!==h.MemberExpression&&this.throwError(a.InvalidExpression,r,n),this.context.isAssignmentTarget||this.tolerateError(a.InvalidLeftHandSideInAssignment,t),this.readNextRawToken(),this.context.isAssignmentTarget=!1,this.finalize({type:h.UpdateExpression,operator:e,argument:i,prefix:!1,...this.getItemLocation(t)})):i)}parseUnaryExpression(){const t=this.getMatchingPunctuator(l);if(t){const e=this.captureStartMarker();this.readNextRawToken();const r=this.inheritCoverGrammar(this.parseUnaryExpression);return this.context.isAssignmentTarget=!1,this.finalize({type:h.UnaryExpression,operator:t,argument:r,prefix:!0,...this.getItemLocation(e)})}return this.parseUpdateExpression()}parseBinaryExpression(){const t=this.rawToken;let e=this.inheritCoverGrammar(this.parseUnaryExpression);if(this.rawToken.type!==s.Punctuator)return e;const r=this.rawToken.value;let i=y(r);if(0===i)return e;this.readNextRawToken(),this.context.isAssignmentTarget=!1;const n=[t,this.rawToken];let a=e,o=this.inheritCoverGrammar(this.parseUnaryExpression);const h=[a,r,o],c=[i];for(;this.rawToken.type===s.Punctuator&&(i=y(this.rawToken.value))>0;){for(;h.length>2&&i<=c[c.length-1];){o=h.pop();const t=h.pop();c.pop(),a=h.pop(),n.pop();const e=n[n.length-1],r=w(e,e.lineStart);h.push(this.finalize(this.createBinaryOrLogicalExpression(r,t,a,o)))}h.push(this.rawToken.value),c.push(i),n.push(this.rawToken),this.readNextRawToken(),h.push(this.inheritCoverGrammar(this.parseUnaryExpression))}let p=h.length-1;e=h[p];let l=n.pop();for(;p>1;){const t=n.pop();if(!t)break;const r=l?.lineStart,i=w(t,r),s=h[p-1];e=this.finalize(this.createBinaryOrLogicalExpression(i,s,h[p-2],e)),p-=2,l=t}return e}createBinaryOrLogicalExpression(t,e,r,i){const n=u.includes(e)?h.LogicalExpression:h.BinaryExpression;return n===h.BinaryExpression||(r.type!==h.AssignmentExpression&&r.type!==h.UpdateExpression||this.throwError(a.InvalidExpression,...g(r)),i.type!==h.AssignmentExpression&&i.type!==h.UpdateExpression||this.throwError(a.InvalidExpression,...g(r))),{type:n,operator:e,left:r,right:i,...this.getItemLocation(t)}}parseAssignmentExpression(){const t=this.captureStartMarker(),e=this.inheritCoverGrammar(this.parseBinaryExpression),r=this.captureStartMarker(),i=this.getMatchingPunctuator(m);if(!i)return e;e.type!==h.Identifier&&e.type!==h.MemberExpression&&this.throwError(a.InvalidExpression,t,r),this.context.isAssignmentTarget||this.tolerateError(a.InvalidLeftHandSideInAssignment,t),this.matchPunctuator("=")||(this.context.isAssignmentTarget=!1),this.readNextRawToken();const n=this.isolateCoverGrammar(this.parseAssignmentExpression);return this.finalize({type:h.AssignmentExpression,left:e,operator:i,right:n,...this.getItemLocation(t)})}parseExpression(){return this.isolateCoverGrammar(this.parseAssignmentExpression)}parseStatements(t=null){const e=[];for(;this.rawToken.type!==s.EOF&&!this.matchPunctuator(t);){const t=this.parseStatementListItem();d(t)||e.push(t)}return e}parseStatementListItem(){return this.context.isAssignmentTarget=!0,this.matchKeyword("function")?this.parseFunctionDeclaration():this.matchKeyword("export")?this.parseExportDeclaration():this.matchKeyword("import")?this.parseImportDeclaration():this.parseStatement()}parseBlock(){const t=this.captureStartMarker();this.expectPunctuator("{");const e=this.parseStatements("}");return this.expectPunctuator("}"),this.finalize({type:h.BlockStatement,body:e,...this.getItemLocation(t)})}parseObjectStatement(){const t=this.captureStartMarker(),e=this.parseObjectExpression();return this.finalize({type:h.ExpressionStatement,expression:e,...this.getItemLocation(t)})}parseBlockOrObjectStatement(){if(this.context.curlyParsingType===k.AsObject)return this.parseObjectStatement();return this.peekAhead((t=>{let e=t();return(e.type===s.Identifier||e.type===s.StringLiteral)&&(e=t(),e.type===s.Punctuator&&":"===e.value)}))?this.parseObjectStatement():this.parseBlock()}parseIdentifier(){const t=this.rawToken;if(t.type!==s.Identifier)return null;const e=this.captureStartMarker();return this.readNextRawToken(),this.finalize({type:h.Identifier,name:t.value,...this.getItemLocation(e)})}parseVariableDeclarator(){const t=this.captureStartMarker(),e=this.parseIdentifier();e||this.throwUnexpectedToken({code:a.IdentiferExpected});let r=null;if(this.matchPunctuator("=")){this.readNextRawToken();const t=this.rawToken;try{r=this.isolateCoverGrammar(this.parseAssignmentExpression)}catch(i){this.throwUnexpectedToken({rawToken:t,code:a.InvalidVariableAssignment})}}return this.finalize({type:h.VariableDeclarator,id:e,init:r,...this.getItemLocation(t)})}parseVariableDeclarationList(){const t=[this.parseVariableDeclarator()];for(;this.matchPunctuator(",");)this.readNextRawToken(),t.push(this.parseVariableDeclarator());return t}parseVariableDeclaration(){const t=this.captureStartMarker();this.expectKeyword("var");const e=this.parseVariableDeclarationList();return this.consumeSemicolon(),this.finalize({type:h.VariableDeclaration,declarations:e,kind:"var",...this.getItemLocation(t)})}parseEmptyStatement(){const t=this.captureStartMarker();return this.expectPunctuator(";"),this.finalize({type:h.EmptyStatement,...this.getItemLocation(t)})}parseExpressionStatement(){const t=this.captureStartMarker(),e=this.parseExpression();return this.consumeSemicolon(),this.finalize({type:h.ExpressionStatement,expression:e,...this.getItemLocation(t)})}parseIfClause(){return this.withBlockContext(x.IfClause,this.parseStatement)}parseIfStatement(){const t=this.captureStartMarker();this.expectKeyword("if"),this.expectPunctuator("(");const e=this.captureStartMarker(),r=this.parseExpression(),i=this.captureStartMarker();this.expectPunctuator(")"),r.type!==h.AssignmentExpression&&r.type!==h.UpdateExpression||this.throwError(a.InvalidExpression,e,i);const n=this.parseIfClause();let s=null;return this.matchKeyword("else")&&(this.readNextRawToken(),s=this.parseIfClause()),this.finalize({type:h.IfStatement,test:r,consequent:n,alternate:s,...this.getItemLocation(t)})}parseWhileStatement(){const t=this.captureStartMarker();this.expectKeyword("while"),this.expectPunctuator("(");const e=this.captureStartMarker(),r=this.parseExpression(),i=this.captureStartMarker();this.expectPunctuator(")"),r.type!==h.AssignmentExpression&&r.type!==h.UpdateExpression||this.throwError(a.InvalidExpression,e,i);const n=this.withBlockContext(x.WhileLoop,this.parseStatement);return this.finalize({type:h.WhileStatement,test:r,body:n,...this.getItemLocation(t)})}parseForStatement(){let t=null,e=null,r=null,i=null,n=null;const s=this.captureStartMarker();if(this.expectKeyword("for"),this.expectPunctuator("("),this.matchPunctuator(";"))this.readNextRawToken();else if(this.matchKeyword("var")){const e=this.captureStartMarker();this.readNextRawToken();const r=this.parseVariableDeclarationList();if(1===r.length&&this.matchKeyword("in")){r[0].init&&this.throwError(a.ForInOfLoopInitializer,e),i=this.finalize({type:h.VariableDeclaration,declarations:r,kind:"var",...this.getItemLocation(e)}),this.readNextRawToken(),n=this.parseExpression()}else this.matchKeyword("in")&&this.throwError(a.InvalidLeftHandSideInForIn,e),t=this.finalize({type:h.VariableDeclaration,declarations:r,kind:"var",...this.getItemLocation(e)}),this.expectPunctuator(";")}else{const e=this.context.isAssignmentTarget,r=this.captureStartMarker();t=this.inheritCoverGrammar(this.parseAssignmentExpression),this.matchKeyword("in")?(this.context.isAssignmentTarget||this.tolerateError(a.InvalidLeftHandSideInForIn,r),t.type!==h.Identifier&&this.throwError(a.InvalidLeftHandSideInForIn,r),this.readNextRawToken(),i=t,n=this.parseExpression(),t=null):(this.context.isAssignmentTarget=e,this.expectPunctuator(";"))}i||(this.matchPunctuator(";")||(e=this.isolateCoverGrammar(this.parseExpression)),this.expectPunctuator(";"),this.matchPunctuator(")")||(r=this.isolateCoverGrammar(this.parseExpression))),this.expectPunctuator(")");const o=this.withBlockContext(x.ForLoop,(()=>this.isolateCoverGrammar(this.parseStatement)));return i&&n?this.finalize({type:h.ForInStatement,left:i,right:n,body:o,...this.getItemLocation(s)}):this.finalize({type:h.ForStatement,init:t,test:e,update:r,body:o,...this.getItemLocation(s)})}parseContinueStatement(){const t=this.captureStartMarker();return this.expectKeyword("continue"),this.consumeSemicolon(),this.finalize({type:h.ContinueStatement,...this.getItemLocation(t)})}parseBreakStatement(){const t=this.captureStartMarker();return this.expectKeyword("break"),this.consumeSemicolon(),this.finalize({type:h.BreakStatement,...this.getItemLocation(t)})}parseReturnStatement(){const t=this.captureStartMarker();this.expectKeyword("return");const e=!this.matchPunctuator(";")&&!this.matchPunctuator("}")&&!this.hasLineTerminator&&this.rawToken.type!==s.EOF||this.rawToken.type===s.StringLiteral||this.rawToken.type===s.Template?this.parseExpression():null;return this.consumeSemicolon(),this.finalize({type:h.ReturnStatement,argument:e,...this.getItemLocation(t)})}parseStatement(){switch(this.rawToken.type){case s.BooleanLiteral:case s.NullLiteral:case s.NumericLiteral:case s.StringLiteral:case s.Template:case s.Identifier:return this.parseExpressionStatement();case s.Punctuator:return"{"===this.rawToken.value?this.parseBlockOrObjectStatement():"("===this.rawToken.value?this.parseExpressionStatement():";"===this.rawToken.value?this.parseEmptyStatement():this.parseExpressionStatement();case s.Keyword:switch(this.rawToken.value.toLowerCase()){case"break":return this.parseBreakStatement();case"continue":return this.parseContinueStatement();case"for":return this.parseForStatement();case"function":return this.parseFunctionDeclaration();case"if":return this.parseIfStatement();case"return":return this.parseReturnStatement();case"var":return this.parseVariableDeclaration();case"while":return this.parseWhileStatement();default:return this.parseExpressionStatement()}default:return this.throwUnexpectedToken({rawToken:this.rawToken})}}parseFormalParameters(){const t=[];if(this.expectPunctuator("("),!this.matchPunctuator(")"))for(;this.rawToken.type!==s.EOF;){const e=this.parseIdentifier();if(e||this.throwUnexpectedToken({rawToken:this.rawToken,code:a.IdentiferExpected}),t.push(e),this.matchPunctuator(")"))break;if(this.expectPunctuator(","),this.matchPunctuator(")"))break}return this.expectPunctuator(")"),t}parseFunctionDeclaration(){(this.context.blockContext&x.Function)===x.Function&&this.throwUnexpectedToken({code:a.NoFunctionInsideFunction}),(this.context.blockContext&x.WhileLoop)!==x.WhileLoop&&(this.context.blockContext&x.IfClause)!==x.IfClause||this.throwUnexpectedToken({code:a.NoFunctionInsideBlock});const t=this.captureStartMarker();this.expectKeyword("function");const e=this.parseIdentifier();e||this.throwUnexpectedToken({code:a.InvalidFunctionIdentifier});const r=this.parseFormalParameters(),i=this.context.blockContext;this.context.blockContext=this.context.blockContext|x.Function;const n=this.parseBlock();return this.context.blockContext=i,this.finalize({type:h.FunctionDeclaration,id:e,params:r,body:n,...this.getItemLocation(t)})}parseScript(){const t=this.captureStartMarker(),e=this.parseStatements(),r=this.finalize({type:h.Program,body:e,...this.getItemLocation(t)});return this.options.tokens&&(r.tokens=this.tokens),this.options.tolerant&&(r.errors=this.errorHandler.errors),r}parseExportDeclaration(){this.context.blockContext!==x.None&&this.throwUnexpectedToken({code:a.ModuleExportRootOnly});let t=null;const e=this.captureStartMarker();return this.expectKeyword("export"),this.matchKeyword("var")?t=this.parseVariableDeclaration():this.matchKeyword("function")?t=this.parseFunctionDeclaration():this.throwUnexpectedToken({code:a.InvalidExpression}),this.finalize({type:h.ExportNamedDeclaration,declaration:t,specifiers:[],source:null,...this.getItemLocation(e)})}parseModuleSpecifier(){const t=this.captureStartMarker(),e=this.rawToken;if(e.type===s.StringLiteral)return this.readNextRawToken(),this.finalize({type:h.Literal,value:e.value,raw:this.getSourceValue(e),isString:!0,...this.getItemLocation(t)});this.throwError(a.InvalidModuleUri,t)}parseDefaultSpecifier(){const t=this.captureStartMarker(),e=this.parseIdentifier();return e||this.throwUnexpectedToken({code:a.InvalidFunctionIdentifier}),this.finalize({type:h.ImportDefaultSpecifier,local:e,...this.getItemLocation(t)})}parseImportDeclaration(){this.context.blockContext!==x.None&&this.throwUnexpectedToken({code:a.ModuleImportRootOnly});const t=this.captureStartMarker();this.expectKeyword("import");const e=this.parseDefaultSpecifier();this.expectContextualKeyword("from");const r=this.parseModuleSpecifier();return this.finalize({type:h.ImportDeclaration,specifiers:[e],source:r,...this.getItemLocation(t)})}}export{T as Parser,y as binaryOperatorPrecedence};