BVB Source Codes

reactide Show expression.js Source code

Return Download reactide: download expression.js Source code - Download reactide Source code - Type:.js
  1. import {LooseParser} from "./state"
  2. import {isDummy} from "./parseutil"
  3. import {tokTypes as tt} from "acorn"
  4.  
  5. const lp = LooseParser.prototype
  6.  
  7. lp.checkLVal = function(expr) {
  8.   if (!expr) return expr
  9.   switch (expr.type) {
  10.   case "Identifier":
  11.   case "MemberExpression":
  12.     return expr
  13.  
  14.   case "ParenthesizedExpression":
  15.     expr.expression = this.checkLVal(expr.expression)
  16.     return expr
  17.  
  18.   default:
  19.     return this.dummyIdent()
  20.   }
  21. }
  22.  
  23. lp.parseExpression = function(noIn) {
  24.   let start = this.storeCurrentPos()
  25.   let expr = this.parseMaybeAssign(noIn)
  26.   if (this.tok.type === tt.comma) {
  27.     let node = this.startNodeAt(start)
  28.     node.expressions = [expr]
  29.     while (this.eat(tt.comma)) node.expressions.push(this.parseMaybeAssign(noIn))
  30.     return this.finishNode(node, "SequenceExpression")
  31.   }
  32.   return expr
  33. }
  34.  
  35. lp.parseParenExpression = function() {
  36.   this.pushCx()
  37.   this.expect(tt.parenL)
  38.   let val = this.parseExpression()
  39.   this.popCx()
  40.   this.expect(tt.parenR)
  41.   return val
  42. }
  43.  
  44. lp.parseMaybeAssign = function(noIn) {
  45.   if (this.toks.isContextual("yield")) {
  46.     let node = this.startNode()
  47.     this.next()
  48.     if (this.semicolon() || this.canInsertSemicolon() || (this.tok.type != tt.star && !this.tok.type.startsExpr)) {
  49.       node.delegate = false
  50.       node.argument = null
  51.     } else {
  52.       node.delegate = this.eat(tt.star)
  53.       node.argument = this.parseMaybeAssign()
  54.     }
  55.     return this.finishNode(node, "YieldExpression")
  56.   }
  57.  
  58.   let start = this.storeCurrentPos()
  59.   let left = this.parseMaybeConditional(noIn)
  60.   if (this.tok.type.isAssign) {
  61.     let node = this.startNodeAt(start)
  62.     node.operator = this.tok.value
  63.     node.left = this.tok.type === tt.eq ? this.toAssignable(left) : this.checkLVal(left)
  64.     this.next()
  65.     node.right = this.parseMaybeAssign(noIn)
  66.     return this.finishNode(node, "AssignmentExpression")
  67.   }
  68.   return left
  69. }
  70.  
  71. lp.parseMaybeConditional = function(noIn) {
  72.   let start = this.storeCurrentPos()
  73.   let expr = this.parseExprOps(noIn)
  74.   if (this.eat(tt.question)) {
  75.     let node = this.startNodeAt(start)
  76.     node.test = expr
  77.     node.consequent = this.parseMaybeAssign()
  78.     node.alternate = this.expect(tt.colon) ? this.parseMaybeAssign(noIn) : this.dummyIdent()
  79.     return this.finishNode(node, "ConditionalExpression")
  80.   }
  81.   return expr
  82. }
  83.  
  84. lp.parseExprOps = function(noIn) {
  85.   let start = this.storeCurrentPos()
  86.   let indent = this.curIndent, line = this.curLineStart
  87.   return this.parseExprOp(this.parseMaybeUnary(false), start, -1, noIn, indent, line)
  88. }
  89.  
  90. lp.parseExprOp = function(left, start, minPrec, noIn, indent, line) {
  91.   if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) return left
  92.   let prec = this.tok.type.binop
  93.   if (prec != null && (!noIn || this.tok.type !== tt._in)) {
  94.     if (prec > minPrec) {
  95.       let node = this.startNodeAt(start)
  96.       node.left = left
  97.       node.operator = this.tok.value
  98.       this.next()
  99.       if (this.curLineStart != line && this.curIndent < indent && this.tokenStartsLine()) {
  100.         node.right = this.dummyIdent()
  101.       } else {
  102.         let rightStart = this.storeCurrentPos()
  103.         node.right = this.parseExprOp(this.parseMaybeUnary(false), rightStart, prec, noIn, indent, line)
  104.       }
  105.       this.finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression")
  106.       return this.parseExprOp(node, start, minPrec, noIn, indent, line)
  107.     }
  108.   }
  109.   return left
  110. }
  111.  
  112. lp.parseMaybeUnary = function(sawUnary) {
  113.   let start = this.storeCurrentPos(), expr
  114.   if (this.tok.type.prefix) {
  115.     let node = this.startNode(), update = this.tok.type === tt.incDec
  116.     if (!update) sawUnary = true
  117.     node.operator = this.tok.value
  118.     node.prefix = true
  119.     this.next()
  120.     node.argument = this.parseMaybeUnary(true)
  121.     if (update) node.argument = this.checkLVal(node.argument)
  122.     expr = this.finishNode(node, update ? "UpdateExpression" : "UnaryExpression")
  123.   } else if (this.tok.type === tt.ellipsis) {
  124.     let node = this.startNode()
  125.     this.next()
  126.     node.argument = this.parseMaybeUnary(sawUnary)
  127.     expr = this.finishNode(node, "SpreadElement")
  128.   } else {
  129.     expr = this.parseExprSubscripts()
  130.     while (this.tok.type.postfix && !this.canInsertSemicolon()) {
  131.       let node = this.startNodeAt(start)
  132.       node.operator = this.tok.value
  133.       node.prefix = false
  134.       node.argument = this.checkLVal(expr)
  135.       this.next()
  136.       expr = this.finishNode(node, "UpdateExpression")
  137.     }
  138.   }
  139.  
  140.   if (!sawUnary && this.eat(tt.starstar)) {
  141.     let node = this.startNodeAt(start)
  142.     node.operator = "**"
  143.     node.left = expr
  144.     node.right = this.parseMaybeUnary(false)
  145.     return this.finishNode(node, "BinaryExpression")
  146.   }
  147.  
  148.   return expr
  149. }
  150.  
  151. lp.parseExprSubscripts = function() {
  152.   let start = this.storeCurrentPos()
  153.   return this.parseSubscripts(this.parseExprAtom(), start, false, this.curIndent, this.curLineStart)
  154. }
  155.  
  156. lp.parseSubscripts = function(base, start, noCalls, startIndent, line) {
  157.   for (;;) {
  158.     if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine()) {
  159.       if (this.tok.type == tt.dot && this.curIndent == startIndent)
  160.         --startIndent
  161.       else
  162.         return base
  163.     }
  164.  
  165.     if (this.eat(tt.dot)) {
  166.       let node = this.startNodeAt(start)
  167.       node.object = base
  168.       if (this.curLineStart != line && this.curIndent <= startIndent && this.tokenStartsLine())
  169.         node.property = this.dummyIdent()
  170.       else
  171.         node.property = this.parsePropertyAccessor() || this.dummyIdent()
  172.       node.computed = false
  173.       base = this.finishNode(node, "MemberExpression")
  174.     } else if (this.tok.type == tt.bracketL) {
  175.       this.pushCx()
  176.       this.next()
  177.       let node = this.startNodeAt(start)
  178.       node.object = base
  179.       node.property = this.parseExpression()
  180.       node.computed = true
  181.       this.popCx()
  182.       this.expect(tt.bracketR)
  183.       base = this.finishNode(node, "MemberExpression")
  184.     } else if (!noCalls && this.tok.type == tt.parenL) {
  185.       let node = this.startNodeAt(start)
  186.       node.callee = base
  187.       node.arguments = this.parseExprList(tt.parenR)
  188.       base = this.finishNode(node, "CallExpression")
  189.     } else if (this.tok.type == tt.backQuote) {
  190.       let node = this.startNodeAt(start)
  191.       node.tag = base
  192.       node.quasi = this.parseTemplate()
  193.       base = this.finishNode(node, "TaggedTemplateExpression")
  194.     } else {
  195.       return base
  196.     }
  197.   }
  198. }
  199.  
  200. lp.parseExprAtom = function() {
  201.   let node
  202.   switch (this.tok.type) {
  203.   case tt._this:
  204.   case tt._super:
  205.     let type = this.tok.type === tt._this ? "ThisExpression" : "Super"
  206.     node = this.startNode()
  207.     this.next()
  208.     return this.finishNode(node, type)
  209.  
  210.   case tt.name:
  211.     let start = this.storeCurrentPos()
  212.     let id = this.parseIdent()
  213.     return this.eat(tt.arrow) ? this.parseArrowExpression(this.startNodeAt(start), [id]) : id
  214.  
  215.   case tt.regexp:
  216.     node = this.startNode()
  217.     let val = this.tok.value
  218.     node.regex = {pattern: val.pattern, flags: val.flags}
  219.     node.value = val.value
  220.     node.raw = this.input.slice(this.tok.start, this.tok.end)
  221.     this.next()
  222.     return this.finishNode(node, "Literal")
  223.  
  224.   case tt.num: case tt.string:
  225.     node = this.startNode()
  226.     node.value = this.tok.value
  227.     node.raw = this.input.slice(this.tok.start, this.tok.end)
  228.     this.next()
  229.     return this.finishNode(node, "Literal")
  230.  
  231.   case tt._null: case tt._true: case tt._false:
  232.     node = this.startNode()
  233.     node.value = this.tok.type === tt._null ? null : this.tok.type === tt._true
  234.     node.raw = this.tok.type.keyword
  235.     this.next()
  236.     return this.finishNode(node, "Literal")
  237.  
  238.   case tt.parenL:
  239.     let parenStart = this.storeCurrentPos()
  240.     this.next()
  241.     let inner = this.parseExpression()
  242.     this.expect(tt.parenR)
  243.     if (this.eat(tt.arrow)) {
  244.       return this.parseArrowExpression(this.startNodeAt(parenStart), inner.expressions || (isDummy(inner) ? [] : [inner]))
  245.     }
  246.     if (this.options.preserveParens) {
  247.       let par = this.startNodeAt(parenStart)
  248.       par.expression = inner
  249.       inner = this.finishNode(par, "ParenthesizedExpression")
  250.     }
  251.     return inner
  252.  
  253.   case tt.bracketL:
  254.     node = this.startNode()
  255.     node.elements = this.parseExprList(tt.bracketR, true)
  256.     return this.finishNode(node, "ArrayExpression")
  257.  
  258.   case tt.braceL:
  259.     return this.parseObj()
  260.  
  261.   case tt._class:
  262.     return this.parseClass()
  263.  
  264.   case tt._function:
  265.     node = this.startNode()
  266.     this.next()
  267.     return this.parseFunction(node, false)
  268.  
  269.   case tt._new:
  270.     return this.parseNew()
  271.  
  272.   case tt.backQuote:
  273.     return this.parseTemplate()
  274.  
  275.   default:
  276.     return this.dummyIdent()
  277.   }
  278. }
  279.  
  280. lp.parseNew = function() {
  281.   let node = this.startNode(), startIndent = this.curIndent, line = this.curLineStart
  282.   let meta = this.parseIdent(true)
  283.   if (this.options.ecmaVersion >= 6 && this.eat(tt.dot)) {
  284.     node.meta = meta
  285.     node.property = this.parseIdent(true)
  286.     return this.finishNode(node, "MetaProperty")
  287.   }
  288.   let start = this.storeCurrentPos()
  289.   node.callee = this.parseSubscripts(this.parseExprAtom(), start, true, startIndent, line)
  290.   if (this.tok.type == tt.parenL) {
  291.     node.arguments = this.parseExprList(tt.parenR)
  292.   } else {
  293.     node.arguments = []
  294.   }
  295.   return this.finishNode(node, "NewExpression")
  296. }
  297.  
  298. lp.parseTemplateElement = function() {
  299.   let elem = this.startNode()
  300.   elem.value = {
  301.     raw: this.input.slice(this.tok.start, this.tok.end).replace(/\r\n?/g, '\n'),
  302.     cooked: this.tok.value
  303.   }
  304.   this.next()
  305.   elem.tail = this.tok.type === tt.backQuote
  306.   return this.finishNode(elem, "TemplateElement")
  307. }
  308.  
  309. lp.parseTemplate = function() {
  310.   let node = this.startNode()
  311.   this.next()
  312.   node.expressions = []
  313.   let curElt = this.parseTemplateElement()
  314.   node.quasis = [curElt]
  315.   while (!curElt.tail) {
  316.     this.next()
  317.     node.expressions.push(this.parseExpression())
  318.     if (this.expect(tt.braceR)) {
  319.       curElt = this.parseTemplateElement()
  320.     } else {
  321.       curElt = this.startNode()
  322.       curElt.value = {cooked: '', raw: ''}
  323.       curElt.tail = true
  324.       this.finishNode(curElt, "TemplateElement")
  325.     }
  326.     node.quasis.push(curElt)
  327.   }
  328.   this.expect(tt.backQuote)
  329.   return this.finishNode(node, "TemplateLiteral")
  330. }
  331.  
  332. lp.parseObj = function() {
  333.   let node = this.startNode()
  334.   node.properties = []
  335.   this.pushCx()
  336.   let indent = this.curIndent + 1, line = this.curLineStart
  337.   this.eat(tt.braceL)
  338.   if (this.curIndent + 1 < indent) { indent = this.curIndent; line = this.curLineStart }
  339.   while (!this.closes(tt.braceR, indent, line)) {
  340.     let prop = this.startNode(), isGenerator, start
  341.     if (this.options.ecmaVersion >= 6) {
  342.       start = this.storeCurrentPos()
  343.       prop.method = false
  344.       prop.shorthand = false
  345.       isGenerator = this.eat(tt.star)
  346.     }
  347.     this.parsePropertyName(prop)
  348.     if (isDummy(prop.key)) { if (isDummy(this.parseMaybeAssign())) this.next(); this.eat(tt.comma); continue }
  349.     if (this.eat(tt.colon)) {
  350.       prop.kind = "init"
  351.       prop.value = this.parseMaybeAssign()
  352.     } else if (this.options.ecmaVersion >= 6 && (this.tok.type === tt.parenL || this.tok.type === tt.braceL)) {
  353.       prop.kind = "init"
  354.       prop.method = true
  355.       prop.value = this.parseMethod(isGenerator)
  356.     } else if (this.options.ecmaVersion >= 5 && prop.key.type === "Identifier" &&
  357.                !prop.computed && (prop.key.name === "get" || prop.key.name === "set") &&
  358.                (this.tok.type != tt.comma && this.tok.type != tt.braceR)) {
  359.       prop.kind = prop.key.name
  360.       this.parsePropertyName(prop)
  361.       prop.value = this.parseMethod(false)
  362.     } else {
  363.       prop.kind = "init"
  364.       if (this.options.ecmaVersion >= 6) {
  365.         if (this.eat(tt.eq)) {
  366.           let assign = this.startNodeAt(start)
  367.           assign.operator = "="
  368.           assign.left = prop.key
  369.           assign.right = this.parseMaybeAssign()
  370.           prop.value = this.finishNode(assign, "AssignmentExpression")
  371.         } else {
  372.           prop.value = prop.key
  373.         }
  374.       } else {
  375.         prop.value = this.dummyIdent()
  376.       }
  377.       prop.shorthand = true
  378.     }
  379.     node.properties.push(this.finishNode(prop, "Property"))
  380.     this.eat(tt.comma)
  381.   }
  382.   this.popCx()
  383.   if (!this.eat(tt.braceR)) {
  384.     // If there is no closing brace, make the node span to the start
  385.     // of the next token (this is useful for Tern)
  386.     this.last.end = this.tok.start
  387.     if (this.options.locations) this.last.loc.end = this.tok.loc.start
  388.   }
  389.   return this.finishNode(node, "ObjectExpression")
  390. }
  391.  
  392. lp.parsePropertyName = function(prop) {
  393.   if (this.options.ecmaVersion >= 6) {
  394.     if (this.eat(tt.bracketL)) {
  395.       prop.computed = true
  396.       prop.key = this.parseExpression()
  397.       this.expect(tt.bracketR)
  398.       return
  399.     } else {
  400.       prop.computed = false
  401.     }
  402.   }
  403.   let key = (this.tok.type === tt.num || this.tok.type === tt.string) ? this.parseExprAtom() : this.parseIdent()
  404.   prop.key = key || this.dummyIdent()
  405. }
  406.  
  407. lp.parsePropertyAccessor = function() {
  408.   if (this.tok.type === tt.name || this.tok.type.keyword) return this.parseIdent()
  409. }
  410.  
  411. lp.parseIdent = function() {
  412.   let name = this.tok.type === tt.name ? this.tok.value : this.tok.type.keyword
  413.   if (!name) return this.dummyIdent()
  414.   let node = this.startNode()
  415.   this.next()
  416.   node.name = name
  417.   return this.finishNode(node, "Identifier")
  418. }
  419.  
  420. lp.initFunction = function(node) {
  421.   node.id = null
  422.   node.params = []
  423.   if (this.options.ecmaVersion >= 6) {
  424.     node.generator = false
  425.     node.expression = false
  426.   }
  427. }
  428.  
  429. // Convert existing expression atom to assignable pattern
  430. // if possible.
  431.  
  432. lp.toAssignable = function(node, binding) {
  433.   if (!node || node.type == "Identifier" || (node.type == "MemberExpression" && !binding)) {
  434.     // Okay
  435.   } else if (node.type == "ParenthesizedExpression") {
  436.     node.expression = this.toAssignable(node.expression, binding)
  437.   } else if (this.options.ecmaVersion < 6) {
  438.     return this.dummyIdent()
  439.   } else if (node.type == "ObjectExpression") {
  440.     node.type = "ObjectPattern"
  441.     let props = node.properties
  442.     for (let i = 0; i < props.length; i++)
  443.       props[i].value = this.toAssignable(props[i].value, binding)
  444.   } else if (node.type == "ArrayExpression") {
  445.     node.type = "ArrayPattern"
  446.     this.toAssignableList(node.elements, binding)
  447.   } else if (node.type == "SpreadElement") {
  448.     node.type = "RestElement"
  449.     node.argument = this.toAssignable(node.argument, binding)
  450.   } else if (node.type == "AssignmentExpression") {
  451.     node.type = "AssignmentPattern"
  452.     delete node.operator
  453.   } else {
  454.     return this.dummyIdent()
  455.   }
  456.   return node
  457. }
  458.  
  459. lp.toAssignableList = function(exprList, binding) {
  460.   for (let i = 0; i < exprList.length; i++)
  461.     exprList[i] = this.toAssignable(exprList[i], binding)
  462.   return exprList
  463. }
  464.  
  465. lp.parseFunctionParams = function(params) {
  466.   params = this.parseExprList(tt.parenR)
  467.   return this.toAssignableList(params, true)
  468. }
  469.  
  470. lp.parseMethod = function(isGenerator) {
  471.   let node = this.startNode()
  472.   this.initFunction(node)
  473.   node.params = this.parseFunctionParams()
  474.   node.generator = isGenerator || false
  475.   node.expression = this.options.ecmaVersion >= 6 && this.tok.type !== tt.braceL
  476.   node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
  477.   return this.finishNode(node, "FunctionExpression")
  478. }
  479.  
  480. lp.parseArrowExpression = function(node, params) {
  481.   this.initFunction(node)
  482.   node.params = this.toAssignableList(params, true)
  483.   node.expression = this.tok.type !== tt.braceL
  484.   node.body = node.expression ? this.parseMaybeAssign() : this.parseBlock()
  485.   return this.finishNode(node, "ArrowFunctionExpression")
  486. }
  487.  
  488. lp.parseExprList = function(close, allowEmpty) {
  489.   this.pushCx()
  490.   let indent = this.curIndent, line = this.curLineStart, elts = []
  491.   this.next() // Opening bracket
  492.   while (!this.closes(close, indent + 1, line)) {
  493.     if (this.eat(tt.comma)) {
  494.       elts.push(allowEmpty ? null : this.dummyIdent())
  495.       continue
  496.     }
  497.     let elt = this.parseMaybeAssign()
  498.     if (isDummy(elt)) {
  499.       if (this.closes(close, indent, line)) break
  500.       this.next()
  501.     } else {
  502.       elts.push(elt)
  503.     }
  504.     this.eat(tt.comma)
  505.   }
  506.   this.popCx()
  507.   if (!this.eat(close)) {
  508.     // If there is no closing brace, make the node span to the start
  509.     // of the next token (this is useful for Tern)
  510.     this.last.end = this.tok.start
  511.     if (this.options.locations) this.last.loc.end = this.tok.loc.start
  512.   }
  513.   return elts
  514. }
  515.  
downloadexpression.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