react / wstein / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / loose / statement.js
80555 viewsimport {LooseParser} from "./state"1import {isDummy} from "./parseutil"2import {getLineInfo, tokTypes as tt} from ".."34const lp = LooseParser.prototype56lp.parseTopLevel = function() {7let node = this.startNodeAt(this.options.locations ? [0, getLineInfo(this.input, 0)] : 0)8node.body = []9while (this.tok.type !== tt.eof) node.body.push(this.parseStatement())10this.last = this.tok11if (this.options.ecmaVersion >= 6) {12node.sourceType = this.options.sourceType13}14return this.finishNode(node, "Program")15}1617lp.parseStatement = function() {18let starttype = this.tok.type, node = this.startNode()1920switch (starttype) {21case tt._break: case tt._continue:22this.next()23let isBreak = starttype === tt._break24if (this.semicolon() || this.canInsertSemicolon()) {25node.label = null26} else {27node.label = this.tok.type === tt.name ? this.parseIdent() : null28this.semicolon()29}30return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")3132case tt._debugger:33this.next()34this.semicolon()35return this.finishNode(node, "DebuggerStatement")3637case tt._do:38this.next()39node.body = this.parseStatement()40node.test = this.eat(tt._while) ? this.parseParenExpression() : this.dummyIdent()41this.semicolon()42return this.finishNode(node, "DoWhileStatement")4344case tt._for:45this.next()46this.pushCx()47this.expect(tt.parenL)48if (this.tok.type === tt.semi) return this.parseFor(node, null)49if (this.tok.type === tt._var || this.tok.type === tt._let || this.tok.type === tt._const) {50let init = this.parseVar(true)51if (init.declarations.length === 1 && (this.tok.type === tt._in || this.isContextual("of"))) {52return this.parseForIn(node, init)53}54return this.parseFor(node, init)55}56let init = this.parseExpression(true)57if (this.tok.type === tt._in || this.isContextual("of"))58return this.parseForIn(node, this.toAssignable(init))59return this.parseFor(node, init)6061case tt._function:62this.next()63return this.parseFunction(node, true)6465case tt._if:66this.next()67node.test = this.parseParenExpression()68node.consequent = this.parseStatement()69node.alternate = this.eat(tt._else) ? this.parseStatement() : null70return this.finishNode(node, "IfStatement")7172case tt._return:73this.next()74if (this.eat(tt.semi) || this.canInsertSemicolon()) node.argument = null75else { node.argument = this.parseExpression(); this.semicolon() }76return this.finishNode(node, "ReturnStatement")7778case tt._switch:79let blockIndent = this.curIndent, line = this.curLineStart80this.next()81node.discriminant = this.parseParenExpression()82node.cases = []83this.pushCx()84this.expect(tt.braceL)8586let cur87while (!this.closes(tt.braceR, blockIndent, line, true)) {88if (this.tok.type === tt._case || this.tok.type === tt._default) {89let isCase = this.tok.type === tt._case90if (cur) this.finishNode(cur, "SwitchCase")91node.cases.push(cur = this.startNode())92cur.consequent = []93this.next()94if (isCase) cur.test = this.parseExpression()95else cur.test = null96this.expect(tt.colon)97} else {98if (!cur) {99node.cases.push(cur = this.startNode())100cur.consequent = []101cur.test = null102}103cur.consequent.push(this.parseStatement())104}105}106if (cur) this.finishNode(cur, "SwitchCase")107this.popCx()108this.eat(tt.braceR)109return this.finishNode(node, "SwitchStatement")110111case tt._throw:112this.next()113node.argument = this.parseExpression()114this.semicolon()115return this.finishNode(node, "ThrowStatement")116117case tt._try:118this.next()119node.block = this.parseBlock()120node.handler = null121if (this.tok.type === tt._catch) {122let clause = this.startNode()123this.next()124this.expect(tt.parenL)125clause.param = this.toAssignable(this.parseExprAtom(), true)126this.expect(tt.parenR)127clause.guard = null128clause.body = this.parseBlock()129node.handler = this.finishNode(clause, "CatchClause")130}131node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null132if (!node.handler && !node.finalizer) return node.block133return this.finishNode(node, "TryStatement")134135case tt._var:136case tt._let:137case tt._const:138return this.parseVar()139140case tt._while:141this.next()142node.test = this.parseParenExpression()143node.body = this.parseStatement()144return this.finishNode(node, "WhileStatement")145146case tt._with:147this.next()148node.object = this.parseParenExpression()149node.body = this.parseStatement()150return this.finishNode(node, "WithStatement")151152case tt.braceL:153return this.parseBlock()154155case tt.semi:156this.next()157return this.finishNode(node, "EmptyStatement")158159case tt._class:160return this.parseClass(true)161162case tt._import:163return this.parseImport()164165case tt._export:166return this.parseExport()167168default:169let expr = this.parseExpression()170if (isDummy(expr)) {171this.next()172if (this.tok.type === tt.eof) return this.finishNode(node, "EmptyStatement")173return this.parseStatement()174} else if (starttype === tt.name && expr.type === "Identifier" && this.eat(tt.colon)) {175node.body = this.parseStatement()176node.label = expr177return this.finishNode(node, "LabeledStatement")178} else {179node.expression = expr180this.semicolon()181return this.finishNode(node, "ExpressionStatement")182}183}184}185186lp.parseBlock = function() {187let node = this.startNode()188this.pushCx()189this.expect(tt.braceL)190let blockIndent = this.curIndent, line = this.curLineStart191node.body = []192while (!this.closes(tt.braceR, blockIndent, line, true))193node.body.push(this.parseStatement())194this.popCx()195this.eat(tt.braceR)196return this.finishNode(node, "BlockStatement")197}198199lp.parseFor = function(node, init) {200node.init = init201node.test = node.update = null202if (this.eat(tt.semi) && this.tok.type !== tt.semi) node.test = this.parseExpression()203if (this.eat(tt.semi) && this.tok.type !== tt.parenR) node.update = this.parseExpression()204this.popCx()205this.expect(tt.parenR)206node.body = this.parseStatement()207return this.finishNode(node, "ForStatement")208}209210lp.parseForIn = function(node, init) {211let type = this.tok.type === tt._in ? "ForInStatement" : "ForOfStatement"212this.next()213node.left = init214node.right = this.parseExpression()215this.popCx()216this.expect(tt.parenR)217node.body = this.parseStatement()218return this.finishNode(node, type)219}220221lp.parseVar = function(noIn) {222let node = this.startNode()223node.kind = this.tok.type.keyword224this.next()225node.declarations = []226do {227let decl = this.startNode()228decl.id = this.options.ecmaVersion >= 6 ? this.toAssignable(this.parseExprAtom(), true) : this.parseIdent()229decl.init = this.eat(tt.eq) ? this.parseMaybeAssign(noIn) : null230node.declarations.push(this.finishNode(decl, "VariableDeclarator"))231} while (this.eat(tt.comma))232if (!node.declarations.length) {233let decl = this.startNode()234decl.id = this.dummyIdent()235node.declarations.push(this.finishNode(decl, "VariableDeclarator"))236}237if (!noIn) this.semicolon()238return this.finishNode(node, "VariableDeclaration")239}240241lp.parseClass = function(isStatement) {242let node = this.startNode()243this.next()244if (this.tok.type === tt.name) node.id = this.parseIdent()245else if (isStatement) node.id = this.dummyIdent()246else node.id = null247node.superClass = this.eat(tt._extends) ? this.parseExpression() : null248node.body = this.startNode()249node.body.body = []250this.pushCx()251let indent = this.curIndent + 1, line = this.curLineStart252this.eat(tt.braceL)253if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }254while (!this.closes(tt.braceR, indent, line)) {255if (this.semicolon()) continue256let method = this.startNode(), isGenerator257if (this.options.ecmaVersion >= 6) {258method.static = false259isGenerator = this.eat(tt.star)260}261this.parsePropertyName(method)262if (isDummy(method.key)) { if (isDummy(this.parseMaybeAssign())) this.next(); this.eat(tt.comma); continue }263if (method.key.type === "Identifier" && !method.computed && method.key.name === "static" &&264(this.tok.type != tt.parenL && this.tok.type != tt.braceL)) {265method.static = true266isGenerator = this.eat(tt.star)267this.parsePropertyName(method)268} else {269method.static = false270}271if (this.options.ecmaVersion >= 5 && method.key.type === "Identifier" &&272!method.computed && (method.key.name === "get" || method.key.name === "set") &&273this.tok.type !== tt.parenL && this.tok.type !== tt.braceL) {274method.kind = method.key.name275this.parsePropertyName(method)276method.value = this.parseMethod(false)277} else {278if (!method.computed && !method.static && !isGenerator && (279method.key.type === "Identifier" && method.key.name === "constructor" ||280method.key.type === "Literal" && method.key.value === "constructor")) {281method.kind = "constructor"282} else {283method.kind = "method"284}285method.value = this.parseMethod(isGenerator)286}287node.body.body.push(this.finishNode(method, "MethodDefinition"))288}289this.popCx()290if (!this.eat(tt.braceR)) {291// If there is no closing brace, make the node span to the start292// of the next token (this is useful for Tern)293this.last.end = this.tok.start294if (this.options.locations) this.last.loc.end = this.tok.loc.start295}296this.semicolon()297this.finishNode(node.body, "ClassBody")298return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")299}300301lp.parseFunction = function(node, isStatement) {302this.initFunction(node)303if (this.options.ecmaVersion >= 6) {304node.generator = this.eat(tt.star)305}306if (this.tok.type === tt.name) node.id = this.parseIdent()307else if (isStatement) node.id = this.dummyIdent()308node.params = this.parseFunctionParams()309node.body = this.parseBlock()310return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")311}312313lp.parseExport = function() {314let node = this.startNode()315this.next()316if (this.eat(tt.star)) {317node.source = this.eatContextual("from") ? this.parseExprAtom() : null318return this.finishNode(node, "ExportAllDeclaration")319}320if (this.eat(tt._default)) {321let expr = this.parseMaybeAssign()322if (expr.id) {323switch (expr.type) {324case "FunctionExpression": expr.type = "FunctionDeclaration"; break325case "ClassExpression": expr.type = "ClassDeclaration"; break326}327}328node.declaration = expr329this.semicolon()330return this.finishNode(node, "ExportDefaultDeclaration")331}332if (this.tok.type.keyword) {333node.declaration = this.parseStatement()334node.specifiers = []335node.source = null336} else {337node.declaration = null338node.specifiers = this.parseExportSpecifierList()339node.source = this.eatContextual("from") ? this.parseExprAtom() : null340this.semicolon()341}342return this.finishNode(node, "ExportNamedDeclaration")343}344345lp.parseImport = function() {346let node = this.startNode()347this.next()348if (this.tok.type === tt.string) {349node.specifiers = []350node.source = this.parseExprAtom()351node.kind = ''352} else {353let elt354if (this.tok.type === tt.name && this.tok.value !== "from") {355elt = this.startNode()356elt.local = this.parseIdent()357this.finishNode(elt, "ImportDefaultSpecifier")358this.eat(tt.comma)359}360node.specifiers = this.parseImportSpecifierList()361node.source = this.eatContextual("from") ? this.parseExprAtom() : null362if (elt) node.specifiers.unshift(elt)363}364this.semicolon()365return this.finishNode(node, "ImportDeclaration")366}367368lp.parseImportSpecifierList = function() {369let elts = []370if (this.tok.type === tt.star) {371let elt = this.startNode()372this.next()373if (this.eatContextual("as")) elt.local = this.parseIdent()374elts.push(this.finishNode(elt, "ImportNamespaceSpecifier"))375} else {376let indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart377this.pushCx()378this.eat(tt.braceL)379if (this.curLineStart > continuedLine) continuedLine = this.curLineStart380while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {381let elt = this.startNode()382if (this.eat(tt.star)) {383if (this.eatContextual("as")) elt.local = this.parseIdent()384this.finishNode(elt, "ImportNamespaceSpecifier")385} else {386if (this.isContextual("from")) break387elt.imported = this.parseIdent()388elt.local = this.eatContextual("as") ? this.parseIdent() : elt.imported389this.finishNode(elt, "ImportSpecifier")390}391elts.push(elt)392this.eat(tt.comma)393}394this.eat(tt.braceR)395this.popCx()396}397return elts398}399400lp.parseExportSpecifierList = function() {401let elts = []402let indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart403this.pushCx()404this.eat(tt.braceL)405if (this.curLineStart > continuedLine) continuedLine = this.curLineStart406while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {407if (this.isContextual("from")) break408let elt = this.startNode()409elt.local = this.parseIdent()410elt.exported = this.eatContextual("as") ? this.parseIdent() : elt.local411this.finishNode(elt, "ExportSpecifier")412elts.push(elt)413this.eat(tt.comma)414}415this.eat(tt.braceR)416this.popCx()417return elts418}419420421