react / wstein / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / loose / parseutil.js
80555 viewsimport {LooseParser} from "./state"1import {Node, SourceLocation, lineBreak, isNewLine, tokTypes as tt} from ".."23const lp = LooseParser.prototype45lp.startNode = function() {6let node = new Node7node.start = this.tok.start8if (this.options.locations)9node.loc = new SourceLocation(this.toks, this.tok.loc.start)10if (this.options.directSourceFile)11node.sourceFile = this.options.directSourceFile12if (this.options.ranges)13node.range = [this.tok.start, 0]14return node15}1617lp.storeCurrentPos = function() {18return this.options.locations ? [this.tok.start, this.tok.loc.start] : this.tok.start19}2021lp.startNodeAt = function(pos) {22let node = new Node23if (this.options.locations) {24node.start = pos[0]25node.loc = new SourceLocation(this.toks, pos[1])26pos = pos[0]27} else {28node.start = pos29}30if (this.options.directSourceFile)31node.sourceFile = this.options.directSourceFile32if (this.options.ranges)33node.range = [pos, 0]34return node35}3637lp.finishNode = function(node, type) {38node.type = type39node.end = this.last.end40if (this.options.locations)41node.loc.end = this.last.loc.end42if (this.options.ranges)43node.range[1] = this.last.end44return node45}4647lp.dummyIdent = function() {48let dummy = this.startNode()49dummy.name = "✖"50return this.finishNode(dummy, "Identifier")51}5253export function isDummy(node) { return node.name == "✖" }5455lp.eat = function(type) {56if (this.tok.type === type) {57this.next()58return true59} else {60return false61}62}6364lp.isContextual = function(name) {65return this.tok.type === tt.name && this.tok.value === name66}6768lp.eatContextual = function(name) {69return this.tok.value === name && this.eat(tt.name)70}7172lp.canInsertSemicolon = function() {73return this.tok.type === tt.eof || this.tok.type === tt.braceR ||74lineBreak.test(this.input.slice(this.last.end, this.tok.start))75}7677lp.semicolon = function() {78return this.eat(tt.semi)79}8081lp.expect = function(type) {82if (this.eat(type)) return true83for (let i = 1; i <= 2; i++) {84if (this.lookAhead(i).type == type) {85for (let j = 0; j < i; j++) this.next()86return true87}88}89}9091lp.pushCx = function() {92this.context.push(this.curIndent)93}94lp.popCx = function() {95this.curIndent = this.context.pop()96}9798lp.lineEnd = function(pos) {99while (pos < this.input.length && !isNewLine(this.input.charCodeAt(pos))) ++pos100return pos101}102103lp.indentationAfter = function(pos) {104for (let count = 0;; ++pos) {105let ch = this.input.charCodeAt(pos)106if (ch === 32) ++count107else if (ch === 9) count += this.options.tabSize108else return count109}110}111112lp.closes = function(closeTok, indent, line, blockHeuristic) {113if (this.tok.type === closeTok || this.tok.type === tt.eof) return true114return line != this.curLineStart && this.curIndent < indent && this.tokenStartsLine() &&115(!blockHeuristic || this.nextLineStart >= this.input.length ||116this.indentationAfter(this.nextLineStart) < indent)117}118119lp.tokenStartsLine = function() {120for (let p = this.tok.start - 1; p >= this.curLineStart; --p) {121let ch = this.input.charCodeAt(p)122if (ch !== 9 && ch !== 32) return false123}124return true125}126127128