support const (#4190)

This commit is contained in:
Alex Lam S.L
2020-10-11 18:18:57 +01:00
committed by GitHub
parent ffcce28ce1
commit 55451e7b78
15 changed files with 1265 additions and 302 deletions

View File

@@ -832,6 +832,12 @@ function parse($TEXT, options) {
next();
return break_cont(AST_Break);
case "const":
next();
var node = const_();
semicolon();
return node;
case "continue":
next();
return break_cont(AST_Continue);
@@ -988,7 +994,9 @@ function parse($TEXT, options) {
expect("(");
var init = null;
if (!is("punc", ";")) {
init = is("keyword", "var")
init = is("keyword", "const")
? (next(), const_(true))
: is("keyword", "var")
? (next(), var_(true))
: expression(true, true);
if (is("operator", "in")) {
@@ -1161,13 +1169,22 @@ function parse($TEXT, options) {
});
}
function vardefs(no_in) {
function vardefs(type, no_in, must_init) {
var a = [];
for (;;) {
var start = S.token;
var name = as_symbol(type);
var value = null;
if (is("operator", "=")) {
next();
value = expression(false, no_in);
} else if (must_init) {
croak("Missing initializer in declaration");
}
a.push(new AST_VarDef({
start : S.token,
name : as_symbol(AST_SymbolVar),
value : is("operator", "=") ? (next(), expression(false, no_in)) : null,
start : start,
name : name,
value : value,
end : prev()
}));
if (!is("punc", ","))
@@ -1177,10 +1194,18 @@ function parse($TEXT, options) {
return a;
}
var const_ = function(no_in) {
return new AST_Const({
start : prev(),
definitions : vardefs(AST_SymbolConst, no_in, true),
end : prev()
});
};
var var_ = function(no_in) {
return new AST_Var({
start : prev(),
definitions : vardefs(no_in),
definitions : vardefs(AST_SymbolVar, no_in),
end : prev()
});
};