117 lines
3.9 KiB
JavaScript
117 lines
3.9 KiB
JavaScript
/* */
|
|
"format cjs";
|
|
"use strict";
|
|
|
|
var _babelToolsProtectJs2 = require("./../../babel/tools/protect.js");
|
|
|
|
var _babelToolsProtectJs3 = _interopRequireDefault(_babelToolsProtectJs2);
|
|
|
|
exports.__esModule = true;
|
|
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
|
|
// The algorithm used to determine whether a regexp can appear at a
|
|
// given point in the program is loosely based on sweet.js' approach.
|
|
// See https://github.com/mozilla/sweet.js/wiki/design
|
|
|
|
var _state = require("./state");
|
|
|
|
var _tokentype = require("./tokentype");
|
|
|
|
var _whitespace = require("./whitespace");
|
|
|
|
_babelToolsProtectJs3["default"](module);
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
|
|
var TokContext = function TokContext(token, isExpr, preserveSpace, override) {
|
|
_classCallCheck(this, TokContext);
|
|
|
|
this.token = token;
|
|
this.isExpr = isExpr;
|
|
this.preserveSpace = preserveSpace;
|
|
this.override = override;
|
|
};
|
|
|
|
exports.TokContext = TokContext;
|
|
var types = {
|
|
b_stat: new TokContext("{", false),
|
|
b_expr: new TokContext("{", true),
|
|
b_tmpl: new TokContext("${", true),
|
|
p_stat: new TokContext("(", false),
|
|
p_expr: new TokContext("(", true),
|
|
q_tmpl: new TokContext("`", true, true, function (p) {
|
|
return p.readTmplToken();
|
|
}),
|
|
f_expr: new TokContext("function", true)
|
|
};
|
|
|
|
exports.types = types;
|
|
var pp = _state.Parser.prototype;
|
|
|
|
pp.initialContext = function () {
|
|
return [types.b_stat];
|
|
};
|
|
|
|
pp.braceIsBlock = function (prevType) {
|
|
var parent = undefined;
|
|
if (prevType === _tokentype.types.colon && (parent = this.curContext()).token == "{") return !parent.isExpr;
|
|
if (prevType === _tokentype.types._return) return _whitespace.lineBreak.test(this.input.slice(this.lastTokEnd, this.start));
|
|
if (prevType === _tokentype.types._else || prevType === _tokentype.types.semi || prevType === _tokentype.types.eof) return true;
|
|
if (prevType == _tokentype.types.braceL) return this.curContext() === types.b_stat;
|
|
return !this.exprAllowed;
|
|
};
|
|
|
|
pp.updateContext = function (prevType) {
|
|
var update = undefined,
|
|
type = this.type;
|
|
if (type.keyword && prevType == _tokentype.types.dot) this.exprAllowed = false;else if (update = type.updateContext) update.call(this, prevType);else this.exprAllowed = type.beforeExpr;
|
|
};
|
|
|
|
// Token-specific context update code
|
|
|
|
_tokentype.types.parenR.updateContext = _tokentype.types.braceR.updateContext = function () {
|
|
if (this.context.length == 1) {
|
|
this.exprAllowed = true;
|
|
return;
|
|
}
|
|
var out = this.context.pop();
|
|
if (out === types.b_stat && this.curContext() === types.f_expr) {
|
|
this.context.pop();
|
|
this.exprAllowed = false;
|
|
} else if (out === types.b_tmpl) {
|
|
this.exprAllowed = true;
|
|
} else {
|
|
this.exprAllowed = !out.isExpr;
|
|
}
|
|
};
|
|
|
|
_tokentype.types.braceL.updateContext = function (prevType) {
|
|
this.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr);
|
|
this.exprAllowed = true;
|
|
};
|
|
|
|
_tokentype.types.dollarBraceL.updateContext = function () {
|
|
this.context.push(types.b_tmpl);
|
|
this.exprAllowed = true;
|
|
};
|
|
|
|
_tokentype.types.parenL.updateContext = function (prevType) {
|
|
var statementParens = prevType === _tokentype.types._if || prevType === _tokentype.types._for || prevType === _tokentype.types._with || prevType === _tokentype.types._while;
|
|
this.context.push(statementParens ? types.p_stat : types.p_expr);
|
|
this.exprAllowed = true;
|
|
};
|
|
|
|
_tokentype.types.incDec.updateContext = function () {};
|
|
|
|
_tokentype.types._function.updateContext = function () {
|
|
if (this.curContext() !== types.b_stat) this.context.push(types.f_expr);
|
|
this.exprAllowed = false;
|
|
};
|
|
|
|
_tokentype.types.backQuote.updateContext = function () {
|
|
if (this.curContext() === types.q_tmpl) this.context.pop();else this.context.push(types.q_tmpl);
|
|
this.exprAllowed = false;
|
|
};
|
|
|
|
// tokExprAllowed stays unchanged
|