BVB Source Codes

reactide Show statement.js Source code

Return Download reactide: download statement.js Source code - Download reactide Source code - Type:.js
  1. import {LooseParser} from "./state"
  2. import {isDummy} from "./parseutil"
  3. import {getLineInfo, tokTypes as tt} from "acorn"
  4.  
  5. const lp = LooseParser.prototype
  6.  
  7. lp.parseTopLevel = function() {
  8.   let node = this.startNodeAt(this.options.locations ? [0, getLineInfo(this.input, 0)] : 0)
  9.   node.body = []
  10.   while (this.tok.type !== tt.eof) node.body.push(this.parseStatement())
  11.   this.last = this.tok
  12.   if (this.options.ecmaVersion >= 6) {
  13.     node.sourceType = this.options.sourceType
  14.   }
  15.   return this.finishNode(node, "Program")
  16. }
  17.  
  18. lp.parseStatement = function() {
  19.   let starttype = this.tok.type, node = this.startNode(), kind
  20.  
  21.   if (this.toks.isLet()) {
  22.     starttype = tt._var
  23.     kind = "let"
  24.   }
  25.  
  26.   switch (starttype) {
  27.   case tt._break: case tt._continue:
  28.     this.next()
  29.     let isBreak = starttype === tt._break
  30.     if (this.semicolon() || this.canInsertSemicolon()) {
  31.       node.label = null
  32.     } else {
  33.       node.label = this.tok.type === tt.name ? this.parseIdent() : null
  34.       this.semicolon()
  35.     }
  36.     return this.finishNode(node, isBreak ? "BreakStatement" : "ContinueStatement")
  37.  
  38.   case tt._debugger:
  39.     this.next()
  40.     this.semicolon()
  41.     return this.finishNode(node, "DebuggerStatement")
  42.  
  43.   case tt._do:
  44.     this.next()
  45.     node.body = this.parseStatement()
  46.     node.test = this.eat(tt._while) ? this.parseParenExpression() : this.dummyIdent()
  47.     this.semicolon()
  48.     return this.finishNode(node, "DoWhileStatement")
  49.  
  50.   case tt._for:
  51.     this.next()
  52.     this.pushCx()
  53.     this.expect(tt.parenL)
  54.     if (this.tok.type === tt.semi) return this.parseFor(node, null)
  55.     let isLet = this.toks.isLet()
  56.     if (isLet || this.tok.type === tt._var || this.tok.type === tt._const) {
  57.       let init = this.parseVar(true, isLet ? "let" : this.tok.value)
  58.       if (init.declarations.length === 1 && (this.tok.type === tt._in || this.isContextual("of"))) {
  59.         return this.parseForIn(node, init)
  60.       }
  61.       return this.parseFor(node, init)
  62.     }
  63.     let init = this.parseExpression(true)
  64.     if (this.tok.type === tt._in || this.isContextual("of"))
  65.       return this.parseForIn(node, this.toAssignable(init))
  66.     return this.parseFor(node, init)
  67.  
  68.   case tt._function:
  69.     this.next()
  70.     return this.parseFunction(node, true)
  71.  
  72.   case tt._if:
  73.     this.next()
  74.     node.test = this.parseParenExpression()
  75.     node.consequent = this.parseStatement()
  76.     node.alternate = this.eat(tt._else) ? this.parseStatement() : null
  77.     return this.finishNode(node, "IfStatement")
  78.  
  79.   case tt._return:
  80.     this.next()
  81.     if (this.eat(tt.semi) || this.canInsertSemicolon()) node.argument = null
  82.     else { node.argument = this.parseExpression(); this.semicolon() }
  83.     return this.finishNode(node, "ReturnStatement")
  84.  
  85.   case tt._switch:
  86.     let blockIndent = this.curIndent, line = this.curLineStart
  87.     this.next()
  88.     node.discriminant = this.parseParenExpression()
  89.     node.cases = []
  90.     this.pushCx()
  91.     this.expect(tt.braceL)
  92.  
  93.     let cur
  94.     while (!this.closes(tt.braceR, blockIndent, line, true)) {
  95.       if (this.tok.type === tt._case || this.tok.type === tt._default) {
  96.         let isCase = this.tok.type === tt._case
  97.         if (cur) this.finishNode(cur, "SwitchCase")
  98.         node.cases.push(cur = this.startNode())
  99.         cur.consequent = []
  100.         this.next()
  101.         if (isCase) cur.test = this.parseExpression()
  102.         else cur.test = null
  103.         this.expect(tt.colon)
  104.       } else {
  105.         if (!cur) {
  106.           node.cases.push(cur = this.startNode())
  107.           cur.consequent = []
  108.           cur.test = null
  109.         }
  110.         cur.consequent.push(this.parseStatement())
  111.       }
  112.     }
  113.     if (cur) this.finishNode(cur, "SwitchCase")
  114.     this.popCx()
  115.     this.eat(tt.braceR)
  116.     return this.finishNode(node, "SwitchStatement")
  117.  
  118.   case tt._throw:
  119.     this.next()
  120.     node.argument = this.parseExpression()
  121.     this.semicolon()
  122.     return this.finishNode(node, "ThrowStatement")
  123.  
  124.   case tt._try:
  125.     this.next()
  126.     node.block = this.parseBlock()
  127.     node.handler = null
  128.     if (this.tok.type === tt._catch) {
  129.       let clause = this.startNode()
  130.       this.next()
  131.       this.expect(tt.parenL)
  132.       clause.param = this.toAssignable(this.parseExprAtom(), true)
  133.       this.expect(tt.parenR)
  134.       clause.body = this.parseBlock()
  135.       node.handler = this.finishNode(clause, "CatchClause")
  136.     }
  137.     node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null
  138.     if (!node.handler && !node.finalizer) return node.block
  139.     return this.finishNode(node, "TryStatement")
  140.  
  141.   case tt._var:
  142.   case tt._const:
  143.     return this.parseVar(false, kind || this.tok.value)
  144.  
  145.   case tt._while:
  146.     this.next()
  147.     node.test = this.parseParenExpression()
  148.     node.body = this.parseStatement()
  149.     return this.finishNode(node, "WhileStatement")
  150.  
  151.   case tt._with:
  152.     this.next()
  153.     node.object = this.parseParenExpression()
  154.     node.body = this.parseStatement()
  155.     return this.finishNode(node, "WithStatement")
  156.  
  157.   case tt.braceL:
  158.     return this.parseBlock()
  159.  
  160.   case tt.semi:
  161.     this.next()
  162.     return this.finishNode(node, "EmptyStatement")
  163.  
  164.   case tt._class:
  165.     return this.parseClass(true)
  166.  
  167.   case tt._import:
  168.     return this.parseImport()
  169.  
  170.   case tt._export:
  171.     return this.parseExport()
  172.  
  173.   default:
  174.     let expr = this.parseExpression()
  175.     if (isDummy(expr)) {
  176.       this.next()
  177.       if (this.tok.type === tt.eof) return this.finishNode(node, "EmptyStatement")
  178.       return this.parseStatement()
  179.     } else if (starttype === tt.name && expr.type === "Identifier" && this.eat(tt.colon)) {
  180.       node.body = this.parseStatement()
  181.       node.label = expr
  182.       return this.finishNode(node, "LabeledStatement")
  183.     } else {
  184.       node.expression = expr
  185.       this.semicolon()
  186.       return this.finishNode(node, "ExpressionStatement")
  187.     }
  188.   }
  189. }
  190.  
  191. lp.parseBlock = function() {
  192.   let node = this.startNode()
  193.   this.pushCx()
  194.   this.expect(tt.braceL)
  195.   let blockIndent = this.curIndent, line = this.curLineStart
  196.   node.body = []
  197.   while (!this.closes(tt.braceR, blockIndent, line, true))
  198.     node.body.push(this.parseStatement())
  199.   this.popCx()
  200.   this.eat(tt.braceR)
  201.   return this.finishNode(node, "BlockStatement")
  202. }
  203.  
  204. lp.parseFor = function(node, init) {
  205.   node.init = init
  206.   node.test = node.update = null
  207.   if (this.eat(tt.semi) && this.tok.type !== tt.semi) node.test = this.parseExpression()
  208.   if (this.eat(tt.semi) && this.tok.type !== tt.parenR) node.update = this.parseExpression()
  209.   this.popCx()
  210.   this.expect(tt.parenR)
  211.   node.body = this.parseStatement()
  212.   return this.finishNode(node, "ForStatement")
  213. }
  214.  
  215. lp.parseForIn = function(node, init) {
  216.   let type = this.tok.type === tt._in ? "ForInStatement" : "ForOfStatement"
  217.   this.next()
  218.   node.left = init
  219.   node.right = this.parseExpression()
  220.   this.popCx()
  221.   this.expect(tt.parenR)
  222.   node.body = this.parseStatement()
  223.   return this.finishNode(node, type)
  224. }
  225.  
  226. lp.parseVar = function(noIn, kind) {
  227.   let node = this.startNode()
  228.   node.kind = kind
  229.   this.next()
  230.   node.declarations = []
  231.   do {
  232.     let decl = this.startNode()
  233.     decl.id = this.options.ecmaVersion >= 6 ? this.toAssignable(this.parseExprAtom(), true) : this.parseIdent()
  234.     decl.init = this.eat(tt.eq) ? this.parseMaybeAssign(noIn) : null
  235.     node.declarations.push(this.finishNode(decl, "VariableDeclarator"))
  236.   } while (this.eat(tt.comma))
  237.   if (!node.declarations.length) {
  238.     let decl = this.startNode()
  239.     decl.id = this.dummyIdent()
  240.     node.declarations.push(this.finishNode(decl, "VariableDeclarator"))
  241.   }
  242.   if (!noIn) this.semicolon()
  243.   return this.finishNode(node, "VariableDeclaration")
  244. }
  245.  
  246. lp.parseClass = function(isStatement) {
  247.   let node = this.startNode()
  248.   this.next()
  249.   if (this.tok.type === tt.name) node.id = this.parseIdent()
  250.   else if (isStatement) node.id = this.dummyIdent()
  251.   else node.id = null
  252.   node.superClass = this.eat(tt._extends) ? this.parseExpression() : null
  253.   node.body = this.startNode()
  254.   node.body.body = []
  255.   this.pushCx()
  256.   let indent = this.curIndent + 1, line = this.curLineStart
  257.   this.eat(tt.braceL)
  258.   if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
  259.   while (!this.closes(tt.braceR, indent, line)) {
  260.     if (this.semicolon()) continue
  261.     let method = this.startNode(), isGenerator
  262.     if (this.options.ecmaVersion >= 6) {
  263.       method.static = false
  264.       isGenerator = this.eat(tt.star)
  265.     }
  266.     this.parsePropertyName(method)
  267.     if (isDummy(method.key)) { if (isDummy(this.parseMaybeAssign())) this.next(); this.eat(tt.comma); continue }
  268.     if (method.key.type === "Identifier" && !method.computed && method.key.name === "static" &&
  269.         (this.tok.type != tt.parenL && this.tok.type != tt.braceL)) {
  270.       method.static = true
  271.       isGenerator = this.eat(tt.star)
  272.       this.parsePropertyName(method)
  273.     } else {
  274.       method.static = false
  275.     }
  276.     if (this.options.ecmaVersion >= 5 && method.key.type === "Identifier" &&
  277.         !method.computed && (method.key.name === "get" || method.key.name === "set") &&
  278.         this.tok.type !== tt.parenL && this.tok.type !== tt.braceL) {
  279.       method.kind = method.key.name
  280.       this.parsePropertyName(method)
  281.       method.value = this.parseMethod(false)
  282.     } else {
  283.       if (!method.computed && !method.static && !isGenerator && (
  284.         method.key.type === "Identifier" && method.key.name === "constructor" ||
  285.           method.key.type === "Literal" && method.key.value === "constructor")) {
  286.         method.kind = "constructor"
  287.       } else {
  288.         method.kind =  "method"
  289.       }
  290.       method.value = this.parseMethod(isGenerator)
  291.     }
  292.     node.body.body.push(this.finishNode(method, "MethodDefinition"))
  293.   }
  294.   this.popCx()
  295.   if (!this.eat(tt.braceR)) {
  296.     // If there is no closing brace, make the node span to the start
  297.     // of the next token (this is useful for Tern)
  298.     this.last.end = this.tok.start
  299.     if (this.options.locations) this.last.loc.end = this.tok.loc.start
  300.   }
  301.   this.semicolon()
  302.   this.finishNode(node.body, "ClassBody")
  303.   return this.finishNode(node, isStatement ? "ClassDeclaration" : "ClassExpression")
  304. }
  305.  
  306. lp.parseFunction = function(node, isStatement) {
  307.   this.initFunction(node)
  308.   if (this.options.ecmaVersion >= 6) {
  309.     node.generator = this.eat(tt.star)
  310.   }
  311.   if (this.tok.type === tt.name) node.id = this.parseIdent()
  312.   else if (isStatement) node.id = this.dummyIdent()
  313.   node.params = this.parseFunctionParams()
  314.   node.body = this.parseBlock()
  315.   return this.finishNode(node, isStatement ? "FunctionDeclaration" : "FunctionExpression")
  316. }
  317.  
  318. lp.parseExport = function() {
  319.   let node = this.startNode()
  320.   this.next()
  321.   if (this.eat(tt.star)) {
  322.     node.source = this.eatContextual("from") ? this.parseExprAtom() : this.dummyString()
  323.     return this.finishNode(node, "ExportAllDeclaration")
  324.   }
  325.   if (this.eat(tt._default)) {
  326.     let expr = this.parseMaybeAssign()
  327.     if (expr.id) {
  328.       switch (expr.type) {
  329.       case "FunctionExpression": expr.type = "FunctionDeclaration"; break
  330.       case "ClassExpression": expr.type = "ClassDeclaration"; break
  331.       }
  332.     }
  333.     node.declaration = expr
  334.     this.semicolon()
  335.     return this.finishNode(node, "ExportDefaultDeclaration")
  336.   }
  337.   if (this.tok.type.keyword || this.toks.isLet()) {
  338.     node.declaration = this.parseStatement()
  339.     node.specifiers = []
  340.     node.source = null
  341.   } else {
  342.     node.declaration = null
  343.     node.specifiers = this.parseExportSpecifierList()
  344.     node.source = this.eatContextual("from") ? this.parseExprAtom() : null
  345.     this.semicolon()
  346.   }
  347.   return this.finishNode(node, "ExportNamedDeclaration")
  348. }
  349.  
  350. lp.parseImport = function() {
  351.   let node = this.startNode()
  352.   this.next()
  353.   if (this.tok.type === tt.string) {
  354.     node.specifiers = []
  355.     node.source = this.parseExprAtom()
  356.     node.kind = ''
  357.   } else {
  358.     let elt
  359.     if (this.tok.type === tt.name && this.tok.value !== "from") {
  360.       elt = this.startNode()
  361.       elt.local = this.parseIdent()
  362.       this.finishNode(elt, "ImportDefaultSpecifier")
  363.       this.eat(tt.comma)
  364.     }
  365.     node.specifiers = this.parseImportSpecifierList()
  366.     node.source = this.eatContextual("from") && this.tok.type == tt.string ? this.parseExprAtom() : this.dummyString()
  367.     if (elt) node.specifiers.unshift(elt)
  368.   }
  369.   this.semicolon()
  370.   return this.finishNode(node, "ImportDeclaration")
  371. }
  372.  
  373. lp.parseImportSpecifierList = function() {
  374.   let elts = []
  375.   if (this.tok.type === tt.star) {
  376.     let elt = this.startNode()
  377.     this.next()
  378.     elt.local = this.eatContextual("as") ? this.parseIdent() : this.dummyIdent()
  379.     elts.push(this.finishNode(elt, "ImportNamespaceSpecifier"))
  380.   } else {
  381.     let indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart
  382.     this.pushCx()
  383.     this.eat(tt.braceL)
  384.     if (this.curLineStart > continuedLine) continuedLine = this.curLineStart
  385.     while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
  386.       let elt = this.startNode()
  387.       if (this.eat(tt.star)) {
  388.         elt.local = this.eatContextual("as") ? this.parseIdent() : this.dummyIdent()
  389.         this.finishNode(elt, "ImportNamespaceSpecifier")
  390.       } else {
  391.         if (this.isContextual("from")) break
  392.         elt.imported = this.parseIdent()
  393.         if (isDummy(elt.imported)) break
  394.         elt.local = this.eatContextual("as") ? this.parseIdent() : elt.imported
  395.         this.finishNode(elt, "ImportSpecifier")
  396.       }
  397.       elts.push(elt)
  398.       this.eat(tt.comma)
  399.     }
  400.     this.eat(tt.braceR)
  401.     this.popCx()
  402.   }
  403.   return elts
  404. }
  405.  
  406. lp.parseExportSpecifierList = function() {
  407.   let elts = []
  408.   let indent = this.curIndent, line = this.curLineStart, continuedLine = this.nextLineStart
  409.   this.pushCx()
  410.   this.eat(tt.braceL)
  411.   if (this.curLineStart > continuedLine) continuedLine = this.curLineStart
  412.   while (!this.closes(tt.braceR, indent + (this.curLineStart <= continuedLine ? 1 : 0), line)) {
  413.     if (this.isContextual("from")) break
  414.     let elt = this.startNode()
  415.     elt.local = this.parseIdent()
  416.     if (isDummy(elt.local)) break
  417.     elt.exported = this.eatContextual("as") ? this.parseIdent() : elt.local
  418.     this.finishNode(elt, "ExportSpecifier")
  419.     elts.push(elt)
  420.     this.eat(tt.comma)
  421.   }
  422.   this.eat(tt.braceR)
  423.   this.popCx()
  424.   return elts
  425. }
  426.  
downloadstatement.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top