react / wstein / node_modules / browserify / node_modules / module-deps / node_modules / detective / node_modules / acorn / src / tokencontext.js
80559 views// The algorithm used to determine whether a regexp can appear at a1// given point in the program is loosely based on sweet.js' approach.2// See https://github.com/mozilla/sweet.js/wiki/design34import {Parser} from "./state"5import {types as tt} from "./tokentype"6import {lineBreak} from "./whitespace"78export class TokContext {9constructor(token, isExpr, preserveSpace, override) {10this.token = token11this.isExpr = isExpr12this.preserveSpace = preserveSpace13this.override = override14}15}1617export const types = {18b_stat: new TokContext("{", false),19b_expr: new TokContext("{", true),20b_tmpl: new TokContext("${", true),21p_stat: new TokContext("(", false),22p_expr: new TokContext("(", true),23q_tmpl: new TokContext("`", true, true, p => p.readTmplToken()),24f_expr: new TokContext("function", true)25}2627const pp = Parser.prototype2829pp.initialContext = function() {30return [types.b_stat]31}3233pp.braceIsBlock = function(prevType) {34let parent35if (prevType === tt.colon && (parent = this.curContext()).token == "{")36return !parent.isExpr37if (prevType === tt._return)38return lineBreak.test(this.input.slice(this.lastTokEnd, this.start))39if (prevType === tt._else || prevType === tt.semi || prevType === tt.eof)40return true41if (prevType == tt.braceL)42return this.curContext() === types.b_stat43return !this.exprAllowed44}4546pp.updateContext = function(prevType) {47let update, type = this.type48if (type.keyword && prevType == tt.dot)49this.exprAllowed = false50else if (update = type.updateContext)51update.call(this, prevType)52else53this.exprAllowed = type.beforeExpr54}5556// Token-specific context update code5758tt.parenR.updateContext = tt.braceR.updateContext = function() {59if (this.context.length == 1) {60this.exprAllowed = true61return62}63let out = this.context.pop()64if (out === types.b_stat && this.curContext() === types.f_expr) {65this.context.pop()66this.exprAllowed = false67} else if (out === types.b_tmpl) {68this.exprAllowed = true69} else {70this.exprAllowed = !out.isExpr71}72}7374tt.braceL.updateContext = function(prevType) {75this.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr)76this.exprAllowed = true77}7879tt.dollarBraceL.updateContext = function() {80this.context.push(types.b_tmpl)81this.exprAllowed = true82}8384tt.parenL.updateContext = function(prevType) {85let statementParens = prevType === tt._if || prevType === tt._for || prevType === tt._with || prevType === tt._while86this.context.push(statementParens ? types.p_stat : types.p_expr)87this.exprAllowed = true88}8990tt.incDec.updateContext = function() {91// tokExprAllowed stays unchanged92}9394tt._function.updateContext = function() {95if (this.curContext() !== types.b_stat)96this.context.push(types.f_expr)97this.exprAllowed = false98}99100tt.backQuote.updateContext = function() {101if (this.curContext() === types.q_tmpl)102this.context.pop()103else104this.context.push(types.q_tmpl)105this.exprAllowed = false106}107108109