BVB Source Codes

reactide Show state.js Source code

Return Download reactide: download state.js Source code - Download reactide Source code - Type:.js
  1. import {tokenizer, SourceLocation, tokTypes as tt, Node, lineBreak, isNewLine} from "acorn"
  2.  
  3. // Registered plugins
  4. export const pluginsLoose = {}
  5.  
  6. export class LooseParser {
  7.   constructor(input, options = {}) {
  8.     this.toks = tokenizer(input, options)
  9.     this.options = this.toks.options
  10.     this.input = this.toks.input
  11.     this.tok = this.last = {type: tt.eof, start: 0, end: 0}
  12.     if (this.options.locations) {
  13.       let here = this.toks.curPosition()
  14.       this.tok.loc = new SourceLocation(this.toks, here, here)
  15.     }
  16.     this.ahead = [] // Tokens ahead
  17.     this.context = [] // Indentation contexted
  18.     this.curIndent = 0
  19.     this.curLineStart = 0
  20.     this.nextLineStart = this.lineEnd(this.curLineStart) + 1
  21.     // Load plugins
  22.     this.options.pluginsLoose = options.pluginsLoose || {}
  23.     this.loadPlugins(this.options.pluginsLoose)
  24.   }
  25.  
  26.   startNode() {
  27.     return new Node(this.toks, this.tok.start, this.options.locations ? this.tok.loc.start : null)
  28.   }
  29.  
  30.   storeCurrentPos() {
  31.     return this.options.locations ? [this.tok.start, this.tok.loc.start] : this.tok.start
  32.   }
  33.  
  34.   startNodeAt(pos) {
  35.     if (this.options.locations) {
  36.       return new Node(this.toks, pos[0], pos[1])
  37.     } else {
  38.       return new Node(this.toks, pos)
  39.     }
  40.   }
  41.  
  42.   finishNode(node, type) {
  43.     node.type = type
  44.     node.end = this.last.end
  45.     if (this.options.locations)
  46.       node.loc.end = this.last.loc.end
  47.     if (this.options.ranges)
  48.       node.range[1] = this.last.end
  49.     return node
  50.   }
  51.  
  52.   dummyNode(type) {
  53.     let dummy = this.startNode()
  54.     dummy.type = type
  55.     dummy.end = dummy.start
  56.     if (this.options.locations)
  57.       dummy.loc.end = dummy.loc.start
  58.     if (this.options.ranges)
  59.       dummy.range[1] = dummy.start
  60.     this.last = {type: tt.name, start: dummy.start, end: dummy.start, loc: dummy.loc}
  61.     return dummy
  62.   }
  63.  
  64.   dummyIdent() {
  65.     let dummy = this.dummyNode("Identifier")
  66.     dummy.name = "鉁?
  67.    return dummy
  68.  }
  69.  
  70.  dummyString() {
  71.    let dummy = this.dummyNode("Literal")
  72.    dummy.value = dummy.raw = "?
  73.     return dummy
  74.   }
  75.  
  76.   eat(type) {
  77.     if (this.tok.type === type) {
  78.       this.next()
  79.       return true
  80.     } else {
  81.       return false
  82.     }
  83.   }
  84.  
  85.   isContextual(name) {
  86.     return this.tok.type === tt.name && this.tok.value === name
  87.   }
  88.  
  89.   eatContextual(name) {
  90.     return this.tok.value === name && this.eat(tt.name)
  91.   }
  92.  
  93.   canInsertSemicolon() {
  94.     return this.tok.type === tt.eof || this.tok.type === tt.braceR ||
  95.       lineBreak.test(this.input.slice(this.last.end, this.tok.start))
  96.   }
  97.  
  98.   semicolon() {
  99.     return this.eat(tt.semi)
  100.   }
  101.  
  102.   expect(type) {
  103.     if (this.eat(type)) return true
  104.     for (let i = 1; i <= 2; i++) {
  105.       if (this.lookAhead(i).type == type) {
  106.         for (let j = 0; j < i; j++) this.next()
  107.         return true
  108.       }
  109.     }
  110.   }
  111.  
  112.   pushCx() {
  113.     this.context.push(this.curIndent)
  114.   }
  115.  
  116.   popCx() {
  117.     this.curIndent = this.context.pop()
  118.   }
  119.  
  120.   lineEnd(pos) {
  121.     while (pos < this.input.length && !isNewLine(this.input.charCodeAt(pos))) ++pos
  122.     return pos
  123.   }
  124.  
  125.   indentationAfter(pos) {
  126.     for (let count = 0;; ++pos) {
  127.       let ch = this.input.charCodeAt(pos)
  128.       if (ch === 32) ++count
  129.       else if (ch === 9) count += this.options.tabSize
  130.       else return count
  131.     }
  132.   }
  133.  
  134.   closes(closeTok, indent, line, blockHeuristic) {
  135.     if (this.tok.type === closeTok || this.tok.type === tt.eof) return true
  136.     return line != this.curLineStart && this.curIndent < indent && this.tokenStartsLine() &&
  137.       (!blockHeuristic || this.nextLineStart >= this.input.length ||
  138.        this.indentationAfter(this.nextLineStart) < indent)
  139.   }
  140.  
  141.   tokenStartsLine() {
  142.     for (let p = this.tok.start - 1; p >= this.curLineStart; --p) {
  143.       let ch = this.input.charCodeAt(p)
  144.       if (ch !== 9 && ch !== 32) return false
  145.     }
  146.     return true
  147.   }
  148.  
  149.   extend(name, f) {
  150.     this[name] = f(this[name])
  151.   }
  152.  
  153.   loadPlugins(pluginConfigs) {
  154.     for (let name in pluginConfigs) {
  155.       let plugin = pluginsLoose[name]
  156.       if (!plugin) throw new Error("Plugin '" + name + "' not found")
  157.       plugin(this, pluginConfigs[name])
  158.     }
  159.   }
  160. }
  161.  
downloadstate.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