react / wstein / node_modules / browserify / node_modules / syntax-error / node_modules / acorn / src / loose / tokenize.js
80555 viewsimport {tokTypes as tt, Token, isNewLine, SourceLocation, getLineInfo, lineBreakG} from ".."1import {LooseParser} from "./state"23const lp = LooseParser.prototype45function isSpace(ch) {6return (ch < 14 && ch > 8) || ch === 32 || ch === 160 || isNewLine(ch)7}89lp.next = function() {10this.last = this.tok11if (this.ahead.length)12this.tok = this.ahead.shift()13else14this.tok = this.readToken()1516if (this.tok.start >= this.nextLineStart) {17while (this.tok.start >= this.nextLineStart) {18this.curLineStart = this.nextLineStart19this.nextLineStart = this.lineEnd(this.curLineStart) + 120}21this.curIndent = this.indentationAfter(this.curLineStart)22}23}2425lp.readToken = function() {26for (;;) {27try {28this.toks.next()29if (this.toks.type === tt.dot &&30this.input.substr(this.toks.end, 1) === "." &&31this.options.ecmaVersion >= 6) {32this.toks.end++33this.toks.type = tt.ellipsis34}35return new Token(this.toks)36} catch(e) {37if (!(e instanceof SyntaxError)) throw e3839// Try to skip some text, based on the error message, and then continue40let msg = e.message, pos = e.raisedAt, replace = true41if (/unterminated/i.test(msg)) {42pos = this.lineEnd(e.pos + 1)43if (/string/.test(msg)) {44replace = {start: e.pos, end: pos, type: tt.string, value: this.input.slice(e.pos + 1, pos)}45} else if (/regular expr/i.test(msg)) {46let re = this.input.slice(e.pos, pos)47try { re = new RegExp(re) } catch(e) {}48replace = {start: e.pos, end: pos, type: tt.regexp, value: re}49} else if (/template/.test(msg)) {50replace = {start: e.pos, end: pos,51type: tt.template,52value: this.input.slice(e.pos, pos)}53} else {54replace = false55}56} else if (/invalid (unicode|regexp|number)|expecting unicode|octal literal|is reserved|directly after number|expected number in radix/i.test(msg)) {57while (pos < this.input.length && !isSpace(this.input.charCodeAt(pos))) ++pos58} else if (/character escape|expected hexadecimal/i.test(msg)) {59while (pos < this.input.length) {60let ch = this.input.charCodeAt(pos++)61if (ch === 34 || ch === 39 || isNewLine(ch)) break62}63} else if (/unexpected character/i.test(msg)) {64pos++65replace = false66} else if (/regular expression/i.test(msg)) {67replace = true68} else {69throw e70}71this.resetTo(pos)72if (replace === true) replace = {start: pos, end: pos, type: tt.name, value: "✖"}73if (replace) {74if (this.options.locations)75replace.loc = new SourceLocation(76this.toks,77getLineInfo(this.input, replace.start),78getLineInfo(this.input, replace.end))79return replace80}81}82}83}8485lp.resetTo = function(pos) {86this.toks.pos = pos87let ch = this.input.charAt(pos - 1)88this.toks.exprAllowed = !ch || /[\[\{\(,;:?\/*=+\-~!|&%^<>]/.test(ch) ||89/[enwfd]/.test(ch) &&90/\b(keywords|case|else|return|throw|new|in|(instance|type)of|delete|void)$/.test(this.input.slice(pos - 10, pos))9192if (this.options.locations) {93this.toks.curLine = 194this.toks.lineStart = lineBreakG.lastIndex = 095let match96while ((match = lineBreakG.exec(this.input)) && match.index < pos) {97++this.toks.curLine98this.toks.lineStart = match.index + match[0].length99}100}101}102103lp.lookAhead = function(n) {104while (n > this.ahead.length)105this.ahead.push(this.readToken())106return this.ahead[n - 1]107}108109110