context.js
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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
"use strict";
exports.__esModule = true;
// istanbul ignore next
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _types = require("./types");
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;
// Token-specific context update code
_types.types.parenR.updateContext = _types.types.braceR.updateContext = function () {
if (this.state.context.length === 1) {
this.state.exprAllowed = true;
return;
}
var out = this.state.context.pop();
if (out === types.b_stat && this.curContext() === types.f_expr) {
this.state.context.pop();
this.state.exprAllowed = false;
} else if (out === types.b_tmpl) {
this.state.exprAllowed = true;
} else {
this.state.exprAllowed = !out.isExpr;
}
};
_types.types.braceL.updateContext = function (prevType) {
this.state.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr);
this.state.exprAllowed = true;
};
_types.types.dollarBraceL.updateContext = function () {
this.state.context.push(types.b_tmpl);
this.state.exprAllowed = true;
};
_types.types.parenL.updateContext = function (prevType) {
var statementParens = prevType === _types.types._if || prevType === _types.types._for || prevType === _types.types._with || prevType === _types.types._while;
this.state.context.push(statementParens ? types.p_stat : types.p_expr);
this.state.exprAllowed = true;
};
_types.types.incDec.updateContext = function () {
// tokExprAllowed stays unchanged
};
_types.types._function.updateContext = function () {
if (this.curContext() !== types.b_stat) {
this.state.context.push(types.f_expr);
}
this.state.exprAllowed = false;
};
_types.types.backQuote.updateContext = function () {
if (this.curContext() === types.q_tmpl) {
this.state.context.pop();
} else {
this.state.context.push(types.q_tmpl);
}
this.state.exprAllowed = false;
};