parser.js 23 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.24/esri/copyright.txt for details.
  4. */
  5. import{assert as e}from"./assert.js";import{ErrorHandler as t}from"./error-handler.js";import{Messages as i}from"./messages.js";import{Comment as s,AssignmentOperators as n,Literal as r,Identifier as a,ArrayExpression as o,Property as h,ObjectExpression as c,TemplateElement as l,TemplateLiteral as m,CallExpression as p,ComputedMemberExpression as x,StaticMemberExpression as d,UpdateExpression as u,UnaryExpression as k,BinaryExpression as f,AssignmentExpression as g,BlockStatement as w,ExpressionStatement as T,VariableDeclarator as E,VariableDeclaration as y,EmptyStatement as v,IfStatement as b,ForInStatement as I,ForStatement as N,ContinueStatement as S,BreakStatement as z,ReturnStatement as C,FunctionDeclaration as A,Program as L}from"./nodes.js";import{Scanner as B}from"./scanner.js";import{Syntax as U}from"./syntax.js";import{TokenType as M,TokenName as P}from"./token.js";class O{constructor(e,i={},s){this.config={range:"boolean"==typeof i.range&&i.range,loc:"boolean"==typeof i.loc&&i.loc,source:null,tokens:"boolean"==typeof i.tokens&&i.tokens,comment:"boolean"==typeof i.comment&&i.comment,tolerant:"boolean"==typeof i.tolerant&&i.tolerant,globalReturn:!!i.globalReturn},this.config.loc&&i.source&&null!==i.source&&(this.config.source=String(i.source)),this.delegate=s,this.errorHandler=new t,this.errorHandler.tolerant=this.config.tolerant,this.scanner=new B(e,this.errorHandler),this.scanner.trackComment=this.config.comment,this.operatorPrecedence={")":0,";":0,",":0,"=":0,"]":0,"||":1,"&&":2,"|":3,"^":4,"&":5,"==":6,"!=":6,"===":6,"!==":6,"<":7,">":7,"<=":7,">=":7,"<<":8,">>":8,">>>":8,"+":9,"-":9,"*":11,"/":11,"%":11},this.lookahead={type:M.EOF,value:"",lineNumber:this.scanner.lineNumber,lineStart:0,start:0,end:0},this.hasLineTerminator=!1,this.context={allowIn:!0,firstCoverInitializedNameError:null,isAssignmentTarget:!1,isBindingElement:!1,inFunctionBody:!1,inIteration:!1,curlyParsing:"asObject"},this.tokens=[],this.startMarker={index:0,line:this.scanner.lineNumber,column:0},this.lastMarker={index:0,line:this.scanner.lineNumber,column:0},this.nextToken(),this.lastMarker={index:this.scanner.index,line:this.scanner.lineNumber,column:this.scanner.index-this.scanner.lineStart}}tolerateError(t,...i){const s=i.slice(),n=t.replace(/%(\d)/g,((t,i)=>(e(i<s.length,"Message reference must be in range"),s[i]))),r=this.lastMarker.index,a=this.scanner.lineNumber,o=this.lastMarker.column+1;this.errorHandler.tolerateError(r,a,o,n)}unexpectedTokenError(e,t){let s,n=t||i.UnexpectedToken;if(e?(t||(n=e.type===M.EOF?i.UnexpectedEOS:e.type===M.Identifier?i.UnexpectedIdentifier:e.type===M.NumericLiteral?i.UnexpectedNumber:e.type===M.StringLiteral?i.UnexpectedString:e.type===M.Template?i.UnexpectedTemplate:i.UnexpectedToken),s=e.value.toString()):s="ILLEGAL",n=n.replace("%0",s),e&&"number"==typeof e.lineNumber){const t=e.start,i=e.lineNumber,s=this.lastMarker.index-this.lastMarker.column,r=e.start-s+1;return this.errorHandler.createError(t,i,r,n)}const r=this.lastMarker.index,a=this.lastMarker.line,o=this.lastMarker.column+1;return this.errorHandler.createError(r,a,o,n)}throwUnexpectedToken(e,t){throw this.unexpectedTokenError(e,t)}tolerateUnexpectedToken(e,t){this.errorHandler.tolerate(this.unexpectedTokenError(e,t))}collectComments(){if(this.config.comment){const e=this.scanner.scanComments();if(e&&e.length>0&&this.delegate)for(let t=0;t<e.length;++t){const i=e[t],n=new s(i.multiLine?"Block":"Line",this.scanner.source.slice(i.slice[0],i.slice[1]));this.config.range&&(n.range=i.range),this.config.loc&&(n.loc=i.loc);const r={start:{line:i.loc.start.line,column:i.loc.start.column,offset:i.range[0]},end:{line:i.loc.end.line,column:i.loc.end.column,offset:i.range[1]}};this.delegate(n,r)}}else this.scanner.scanComments()}peekAhead(e){const t=()=>(this.scanner.scanComments(),this.scanner.lex()),i=this.scanner.saveState();e.call(this,t),this.scanner.restoreState(i)}getTokenRaw(e){return this.scanner.source.slice(e.start,e.end)}convertToken(e){const t={type:P[e.type],value:this.getTokenRaw(e)};if(this.config.range&&(t.range=[e.start,e.end]),this.config.loc&&(t.loc={start:{line:this.startMarker.line,column:this.startMarker.column},end:{line:this.scanner.lineNumber,column:this.scanner.index-this.scanner.lineStart}}),e.type===M.RegularExpression){const i=e.pattern,s=e.flags;t.regex={pattern:i,flags:s}}return t}nextToken(){const e=this.lookahead;this.lastMarker.index=this.scanner.index,this.lastMarker.line=this.scanner.lineNumber,this.lastMarker.column=this.scanner.index-this.scanner.lineStart,this.collectComments(),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);const t=this.scanner.lex();return this.hasLineTerminator=e.lineNumber!==t.lineNumber,this.lookahead=t,this.config.tokens&&t.type!==M.EOF&&this.tokens.push(this.convertToken(t)),e}createNode(){return{index:this.startMarker.index,line:this.startMarker.line,column:this.startMarker.column}}startNode(e,t=0){let i=e.start-e.lineStart,s=e.lineNumber;return i<0&&(i+=t,s--),{index:e.start,line:s,column:i}}finalize(e,t){if(this.config.range&&(t.range=[e.index,this.lastMarker.index]),this.config.loc&&(t.loc={start:{line:e.line,column:e.column},end:{line:this.lastMarker.line,column:this.lastMarker.column}},this.config.source&&(t.loc.source=this.config.source)),this.delegate){const i={start:{line:e.line,column:e.column,offset:e.index},end:{line:this.lastMarker.line,column:this.lastMarker.column,offset:this.lastMarker.index}};this.delegate(t,i)}return t}expect(e){const t=this.nextToken();t.type===M.Punctuator&&t.value===e||this.throwUnexpectedToken(t)}expectCommaSeparator(){if(this.config.tolerant){const e=this.lookahead;e.type===M.Punctuator&&","===e.value?this.nextToken():e.type===M.Punctuator&&";"===e.value?(this.nextToken(),this.tolerateUnexpectedToken(e)):this.tolerateUnexpectedToken(e,i.UnexpectedToken)}else this.expect(",")}expectKeyword(e){const t=this.nextToken();t.type===M.Keyword&&t.value.toString().toLowerCase()===e.toLowerCase()||this.throwUnexpectedToken(t)}match(e){return this.lookahead.type===M.Punctuator&&this.lookahead.value===e}matchKeyword(e){return this.lookahead.type===M.Keyword&&this.lookahead.value.toLowerCase()===e.toLowerCase()}matchContextualKeyword(e){return this.lookahead.type===M.Identifier&&this.lookahead.value===e}matchAssign(){if(this.lookahead.type!==M.Punctuator)return!1;const e=this.lookahead.value;return n.includes(e)}isolateCoverGrammar(e){const t=this.context.isBindingElement,i=this.context.isAssignmentTarget,s=this.context.firstCoverInitializedNameError;this.context.isBindingElement=!0,this.context.isAssignmentTarget=!0,this.context.firstCoverInitializedNameError=null;const n=e.call(this);return null!==this.context.firstCoverInitializedNameError&&this.throwUnexpectedToken(this.context.firstCoverInitializedNameError),this.context.isBindingElement=t,this.context.isAssignmentTarget=i,this.context.firstCoverInitializedNameError=s,n}inheritCoverGrammar(e){const t=this.context.isBindingElement,i=this.context.isAssignmentTarget,s=this.context.firstCoverInitializedNameError;this.context.isBindingElement=!0,this.context.isAssignmentTarget=!0,this.context.firstCoverInitializedNameError=null;const n=e.call(this);return this.context.isBindingElement=this.context.isBindingElement&&t,this.context.isAssignmentTarget=this.context.isAssignmentTarget&&i,this.context.firstCoverInitializedNameError=s||this.context.firstCoverInitializedNameError,n}consumeSemicolon(){this.match(";")?this.nextToken():this.hasLineTerminator||(this.lookahead.type===M.EOF||this.match("}")||this.throwUnexpectedToken(this.lookahead),this.lastMarker.index=this.startMarker.index,this.lastMarker.line=this.startMarker.line,this.lastMarker.column=this.startMarker.column)}parsePrimaryExpression(){const e=this.createNode();let t,i,s;switch(this.lookahead.type){case M.Identifier:t=this.finalize(e,new a(this.nextToken().value));break;case M.NumericLiteral:case M.StringLiteral:this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1,i=this.nextToken(),s=this.getTokenRaw(i),t=this.finalize(e,new r(i.value,s));break;case M.BooleanLiteral:this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1,i=this.nextToken(),s=this.getTokenRaw(i),t=this.finalize(e,new r("true"===i.value.toString().toLowerCase(),s));break;case M.NullLiteral:this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1,i=this.nextToken(),s=this.getTokenRaw(i),t=this.finalize(e,new r(null,s));break;case M.Template:t=this.parseTemplateLiteral();break;case M.Punctuator:switch(this.lookahead.value){case"(":this.context.isBindingElement=!1,t=this.inheritCoverGrammar(this.parseGroupExpression);break;case"[":t=this.inheritCoverGrammar(this.parseArrayInitializer);break;case"{":t=this.inheritCoverGrammar(this.parseObjectInitializer);break;default:t=this.throwUnexpectedToken(this.nextToken())}break;case M.Keyword:this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1,t=this.throwUnexpectedToken(this.nextToken());break;default:t=this.throwUnexpectedToken(this.nextToken())}return t}parseArrayInitializer(){const e=this.createNode(),t=[];for(this.expect("[");!this.match("]");)this.match(",")?(this.nextToken(),t.push(null)):(t.push(this.inheritCoverGrammar(this.parseAssignmentExpression)),this.match("]")||this.expect(","));return this.expect("]"),this.finalize(e,new o(t))}parseObjectPropertyKey(){const e=this.createNode(),t=this.nextToken();let i;switch(t.type){case M.StringLiteral:case M.NumericLiteral:i=this.finalize(e,new r(t.value,this.getTokenRaw(t)));break;case M.Identifier:case M.BooleanLiteral:case M.NullLiteral:case M.Keyword:i=this.finalize(e,new a(t.value));break;default:i=this.throwUnexpectedToken(t)}return i}parseObjectProperty(){const e=this.createNode(),t=this.lookahead;let i=!1;const s=!1;let n=!1,r=null;if(t.type===M.Identifier){const s=t.value;this.nextToken(),i=this.match("["),r=this.finalize(e,new a(s))}else this.match("*")?this.nextToken():(i=this.match("["),r=this.parseObjectPropertyKey());r||this.throwUnexpectedToken(this.lookahead);let o=null;const c="init";if(this.match(":"))this.nextToken(),o=this.inheritCoverGrammar(this.parseAssignmentExpression);else if(t.type===M.Identifier){n=!0,o=this.finalize(e,new a(t.value))}else this.throwUnexpectedToken(this.nextToken());return this.finalize(e,new h(c,r,i,o,s,n))}parseObjectInitializer(){const e=this.createNode();this.expect("{");const t=[];for(;!this.match("}");)t.push(this.parseObjectProperty()),this.match("}")||this.expectCommaSeparator();return this.expect("}"),this.finalize(e,new c(t))}parseTemplateHead(){e(this.lookahead.head,"Template literal must start with a template head");const t=this.createNode(),i=this.nextToken(),s=i.value,n=i.cooked;return this.finalize(t,new l({raw:s,cooked:n},i.tail))}parseTemplateElement(){this.lookahead.type!==M.Template&&this.throwUnexpectedToken();const e=this.createNode(),t=this.nextToken(),i=t.value,s=t.cooked;return this.finalize(e,new l({raw:i,cooked:s},t.tail))}parseTemplateLiteral(){const e=this.createNode(),t=[],i=[];let s=this.parseTemplateHead();for(i.push(s);!s.tail;)t.push(this.parseExpression()),s=this.parseTemplateElement(),i.push(s);return this.finalize(e,new m(i,t))}parseGroupExpression(){this.expect("("),this.context.isBindingElement=!0;const e=this.inheritCoverGrammar(this.parseAssignmentExpression);return this.expect(")"),this.context.isBindingElement=!1,e}parseArguments(){this.expect("(");const e=[];if(!this.match(")"))for(;;){const t=this.isolateCoverGrammar(this.parseAssignmentExpression);if(e.push(t),this.match(")"))break;if(this.expectCommaSeparator(),this.match(")"))break}return this.expect(")"),e}isIdentifierName(e){return e.type===M.Identifier||e.type===M.Keyword||e.type===M.BooleanLiteral||e.type===M.NullLiteral}parseIdentifierName(){const e=this.createNode(),t=this.nextToken();return this.isIdentifierName(t)||this.throwUnexpectedToken(t),this.finalize(e,new a(t.value))}parseLeftHandSideExpressionAllowCall(){const e=this.lookahead,t=this.context.allowIn;this.context.allowIn=!0;let i=this.inheritCoverGrammar(this.parsePrimaryExpression);for(;;)if(this.match("(")){this.context.isBindingElement=!1,this.context.isAssignmentTarget=!1;const t=this.parseArguments();i=this.finalize(this.startNode(e),new p(i,t))}else if(this.match("[")){this.context.isBindingElement=!1,this.context.isAssignmentTarget=!0,this.expect("[");const t=this.isolateCoverGrammar(this.parseExpression);this.expect("]"),i=this.finalize(this.startNode(e),new x(i,t))}else{if(!this.match("."))break;{this.context.isBindingElement=!1,this.context.isAssignmentTarget=!0,this.expect(".");const t=this.parseIdentifierName();i=this.finalize(this.startNode(e),new d(i,t))}}return this.context.allowIn=t,i}parseLeftHandSideExpression(){e(this.context.allowIn,"callee of new expression always allow in keyword.");const t=this.startNode(this.lookahead);let i=this.inheritCoverGrammar(this.parsePrimaryExpression);for(;;)if(this.match("[")){this.context.isBindingElement=!1,this.context.isAssignmentTarget=!0,this.expect("[");const e=this.isolateCoverGrammar(this.parseExpression);this.expect("]"),i=this.finalize(t,new x(i,e))}else{if(!this.match("."))break;{this.context.isBindingElement=!1,this.context.isAssignmentTarget=!0,this.expect(".");const e=this.parseIdentifierName();i=this.finalize(t,new d(i,e))}}return i}parseUpdateExpression(){let e;const t=this.lookahead;if(this.match("++")||this.match("--")){const s=this.startNode(t),n=this.nextToken();e=this.inheritCoverGrammar(this.parseUnaryExpression),this.context.isAssignmentTarget||this.tolerateError(i.InvalidLHSInAssignment);const r=!0;e=this.finalize(s,new u(n.value,e,r)),this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1}else if(e=this.inheritCoverGrammar(this.parseLeftHandSideExpressionAllowCall),!this.hasLineTerminator&&this.lookahead.type===M.Punctuator&&(this.match("++")||this.match("--"))){this.context.isAssignmentTarget||this.tolerateError(i.InvalidLHSInAssignment),this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1;const s=this.nextToken().value,n=!1;e=this.finalize(this.startNode(t),new u(s,e,n))}return e}parseUnaryExpression(){let e;if(this.match("+")||this.match("-")||this.match("~")||this.match("!")){const t=this.startNode(this.lookahead),i=this.nextToken();e=this.inheritCoverGrammar(this.parseUnaryExpression),e=this.finalize(t,new k(i.value,e)),this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1}else e=this.parseUpdateExpression();return e}binaryPrecedence(e){const t=e.value;let i;return i=e.type===M.Punctuator?this.operatorPrecedence[t]||0:e.type===M.Keyword&&this.context.allowIn&&"in"===t?12:0,i}parseBinaryExpression(){const e=this.lookahead;let t=this.inheritCoverGrammar(this.parseUnaryExpression);const i=this.lookahead;let s=this.binaryPrecedence(i);if(s>0){this.nextToken(),this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1;const n=[e,this.lookahead];let r=t,a=this.inheritCoverGrammar(this.parseUnaryExpression);const o=[r,i.value,a],h=[s];for(;s=this.binaryPrecedence(this.lookahead),!(s<=0);){for(;o.length>2&&s<=h[h.length-1];){a=o.pop();const e=o.pop();h.pop(),r=o.pop(),n.pop();const t=n[n.length-1],i=this.startNode(t,t.lineStart);o.push(this.finalize(i,new f(e,r,a)))}o.push(this.nextToken().value),h.push(s),n.push(this.lookahead),o.push(this.inheritCoverGrammar(this.parseUnaryExpression))}let c=o.length-1;t=o[c];let l=n.pop();for(;c>1;){const e=n.pop();if(!e)break;const i=l&&l.lineStart,s=this.startNode(e,i),r=o[c-1];t=this.finalize(s,new f(r,o[c-2],t)),c-=2,l=e}}return t}parseAssignmentExpression(){const e=this.lookahead;let t=e,s=this.inheritCoverGrammar(this.parseBinaryExpression);if(this.matchAssign()){this.context.isAssignmentTarget||this.tolerateError(i.InvalidLHSInAssignment),this.match("=")||(this.context.isAssignmentTarget=!1,this.context.isBindingElement=!1),t=this.nextToken();const n=t.value,r=this.isolateCoverGrammar(this.parseAssignmentExpression);s=this.finalize(this.startNode(e),new g(n,s,r)),this.context.firstCoverInitializedNameError=null}return s}parseExpression(){return this.isolateCoverGrammar(this.parseAssignmentExpression)}parseStatementListItem(){let e;if(this.context.isAssignmentTarget=!0,this.context.isBindingElement=!0,this.lookahead.type===M.Keyword)if("function"===this.lookahead.value)e=this.parseFunctionDeclaration();else e=this.parseStatement();else e=this.parseStatement();return e}parseBlock(){const e=this.createNode();this.expect("{");const t=[];for(;!this.match("}");)t.push(this.parseStatementListItem());return this.expect("}"),this.finalize(e,new w(t))}parseObjectStatement(){const e=this.createNode(),t=this.parseObjectInitializer();return this.finalize(e,new T(t))}parseBlockOrObjectStatement(){let e="asObject"===this.context.curlyParsing;return"asObjectOrBlock"===this.context.curlyParsing&&this.peekAhead((t=>{let i=t();i.type!==M.Identifier&&i.type!==M.StringLiteral||(i=t(),i.type===M.Punctuator&&":"===i.value&&(e=!0))})),e?this.parseObjectStatement():this.parseBlock()}parseVariableIdentifier(){const e=this.createNode(),t=this.nextToken();return t.type!==M.Identifier&&this.throwUnexpectedToken(t,i.IdentiferExpected),this.finalize(e,new a(t.value))}parseVariableDeclaration(e){const t=this.createNode(),i=this.parseVariableIdentifier();let s=null;return this.match("=")?(this.nextToken(),s=this.isolateCoverGrammar(this.parseAssignmentExpression)):i.type===U.Identifier||e.inFor||this.expect("="),this.finalize(t,new E(i,s))}parseVariableDeclarationList(e){const t=[this.parseVariableDeclaration(e)];for(;this.match(",");)this.nextToken(),t.push(this.parseVariableDeclaration(e));return t}parseVariableStatement(){const e=this.createNode();this.expectKeyword("var");const t=this.parseVariableDeclarationList({inFor:!1});return this.consumeSemicolon(),this.finalize(e,new y(t,"var"))}parseEmptyStatement(){const e=this.createNode();return this.expect(";"),this.finalize(e,new v)}parseExpressionStatement(){const e=this.createNode(),t=this.parseExpression();return this.consumeSemicolon(),this.finalize(e,new T(t))}parseIfClause(){const e=this.context.curlyParsing;this.context.curlyParsing="asObjectOrBlock";const t=this.parseStatement();return this.context.curlyParsing=e,t}parseIfStatement(){const e=this.createNode();let t,i=null;this.expectKeyword("if"),this.expect("(");const s=this.parseExpression();return!this.match(")")&&this.config.tolerant?(this.tolerateUnexpectedToken(this.nextToken()),t=this.finalize(this.createNode(),new v)):(this.expect(")"),t=this.parseIfClause(),this.matchKeyword("else")&&(this.nextToken(),i=this.parseIfClause())),this.finalize(e,new b(s,t,i))}parseForStatement(){let e=null,t=null,s=null,n=null,r=null;const a=this.createNode();if(this.expectKeyword("for"),this.expect("("),this.match(";"))this.nextToken();else if(this.matchKeyword("var")){const t=this.createNode();this.nextToken();const s=this.context.allowIn;this.context.allowIn=!1;const a=this.parseVariableDeclarationList({inFor:!0});if(this.context.allowIn=s,1===a.length&&this.matchKeyword("in")){a[0].init&&this.tolerateError(i.ForInOfLoopInitializer,"for-in"),n=this.finalize(t,new y(a,"var")),this.nextToken(),r=this.parseExpression()}else e=this.finalize(t,new y(a,"var")),this.expect(";")}else{const t=this.context.isBindingElement,s=this.context.isAssignmentTarget,a=this.context.firstCoverInitializedNameError,o=this.context.allowIn;this.context.allowIn=!1,e=this.inheritCoverGrammar(this.parseAssignmentExpression),this.context.allowIn=o,this.matchKeyword("in")?(this.context.isAssignmentTarget&&e.type!==U.AssignmentExpression||this.tolerateError(i.InvalidLHSInForIn),this.nextToken(),n=e,r=this.parseExpression(),e=null):(this.context.isBindingElement=t,this.context.isAssignmentTarget=s,this.context.firstCoverInitializedNameError=a,this.expect(";"))}let o;if(n||(this.match(";")||(t=this.isolateCoverGrammar(this.parseExpression)),this.expect(";"),this.match(")")||(s=this.isolateCoverGrammar(this.parseExpression))),!this.match(")")&&this.config.tolerant)this.tolerateUnexpectedToken(this.nextToken()),o=this.finalize(this.createNode(),new v);else{this.expect(")");const e=this.context.inIteration,t=this.context.curlyParsing;this.context.inIteration=!0,this.context.curlyParsing="asObjectOrBlock",o=this.isolateCoverGrammar(this.parseStatement),this.context.curlyParsing=t,this.context.inIteration=e}return n&&r?this.finalize(a,new I(n,r,o)):this.finalize(a,new N(e,t,s,o))}parseContinueStatement(){const e=this.createNode();return this.expectKeyword("continue"),this.consumeSemicolon(),this.finalize(e,new S)}parseBreakStatement(){const e=this.createNode();return this.expectKeyword("break"),this.consumeSemicolon(),this.finalize(e,new z)}parseReturnStatement(){this.config.globalReturn||this.context.inFunctionBody||this.tolerateError(i.IllegalReturn);const e=this.createNode();this.expectKeyword("return");const t=!this.match(";")&&!this.match("}")&&!this.hasLineTerminator&&this.lookahead.type!==M.EOF||this.lookahead.type===M.StringLiteral||this.lookahead.type===M.Template?this.parseExpression():null;return this.consumeSemicolon(),this.finalize(e,new C(t))}parseStatement(){let e;switch(this.lookahead.type){case M.BooleanLiteral:case M.NullLiteral:case M.NumericLiteral:case M.StringLiteral:case M.Template:case M.Identifier:e=this.parseExpressionStatement();break;case M.Punctuator:{const t=this.lookahead.value;e="{"===t?this.parseBlockOrObjectStatement():"("===t?this.parseExpressionStatement():";"===t?this.parseEmptyStatement():this.parseExpressionStatement();break}case M.Keyword:switch(this.lookahead.value.toLowerCase()){case"break":e=this.parseBreakStatement();break;case"continue":e=this.parseContinueStatement();break;case"for":e=this.parseForStatement();break;case"function":e=this.parseFunctionDeclaration();break;case"if":e=this.parseIfStatement();break;case"return":e=this.parseReturnStatement();break;case"var":e=this.parseVariableStatement();break;default:e=this.parseExpressionStatement()}break;default:e=this.throwUnexpectedToken(this.lookahead)}return e}parseFunctionSourceElements(){const e=this.createNode();this.expect("{");const t=this.context.inIteration,i=this.context.inFunctionBody;this.context.inIteration=!1,this.context.inFunctionBody=!0;const s=[];for(;this.lookahead.type!==M.EOF&&!this.match("}");)s.push(this.parseStatementListItem());return this.expect("}"),this.context.inIteration=t,this.context.inFunctionBody=i,this.finalize(e,new w(s))}parseFormalParameters(){const e=[];if(this.expect("("),!this.match(")"))for(;this.lookahead.type!==M.EOF&&(e.push(this.parseVariableIdentifier()),!this.match(")"))&&(this.expect(","),!this.match(")")););return this.expect(")"),e}parseFunctionDeclaration(){const e=this.createNode();this.expectKeyword("function");const t=this.parseVariableIdentifier(),i=this.parseFormalParameters(),s=this.parseFunctionSourceElements();return this.finalize(e,new A(t,i,s))}parseScript(){const e=this.createNode(),t=[];for(;this.lookahead.type!==M.EOF;)t.push(this.parseStatementListItem());return this.finalize(e,new L(t))}}export{O as Parser};