react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / acorn / src / parseutil.js
80559 viewsimport {types as tt} from "./tokentype"1import {Parser} from "./state"2import {lineBreak} from "./whitespace"34const pp = Parser.prototype56// ## Parser utilities78// Test whether a statement node is the string literal `"use strict"`.910pp.isUseStrict = function(stmt) {11return this.options.ecmaVersion >= 5 && stmt.type === "ExpressionStatement" &&12stmt.expression.type === "Literal" && stmt.expression.value === "use strict"13}1415// Predicate that tests whether the next token is of the given16// type, and if yes, consumes it as a side effect.1718pp.eat = function(type) {19if (this.type === type) {20this.next()21return true22} else {23return false24}25}2627// Tests whether parsed token is a contextual keyword.2829pp.isContextual = function(name) {30return this.type === tt.name && this.value === name31}3233// Consumes contextual keyword if possible.3435pp.eatContextual = function(name) {36return this.value === name && this.eat(tt.name)37}3839// Asserts that following token is given contextual keyword.4041pp.expectContextual = function(name) {42if (!this.eatContextual(name)) this.unexpected()43}4445// Test whether a semicolon can be inserted at the current position.4647pp.canInsertSemicolon = function() {48return this.type === tt.eof ||49this.type === tt.braceR ||50lineBreak.test(this.input.slice(this.lastTokEnd, this.start))51}5253pp.insertSemicolon = function() {54if (this.canInsertSemicolon()) {55if (this.options.onInsertedSemicolon)56this.options.onInsertedSemicolon(this.lastTokEnd, this.lastTokEndLoc)57return true58}59}6061// Consume a semicolon, or, failing that, see if we are allowed to62// pretend that there is a semicolon at this position.6364pp.semicolon = function() {65if (!this.eat(tt.semi) && !this.insertSemicolon()) this.unexpected()66}6768pp.afterTrailingComma = function(tokType) {69if (this.type == tokType) {70if (this.options.onTrailingComma)71this.options.onTrailingComma(this.lastTokStart, this.lastTokStartLoc)72this.next()73return true74}75}7677// Expect a token of a given type. If found, consume it, otherwise,78// raise an unexpected token error.7980pp.expect = function(type) {81this.eat(type) || this.unexpected()82}8384// Raise an unexpected token error.8586pp.unexpected = function(pos) {87this.raise(pos != null ? pos : this.start, "Unexpected token")88}899091