support template literals (#4601)
This commit is contained in:
54
lib/parse.js
54
lib/parse.js
@@ -113,7 +113,7 @@ var OPERATORS = makePredicate([
|
||||
var NEWLINE_CHARS = "\n\r\u2028\u2029";
|
||||
var OPERATOR_CHARS = "+-*&%=<>!?|~^";
|
||||
var PUNC_BEFORE_EXPRESSION = "[{(,;:";
|
||||
var PUNC_CHARS = PUNC_BEFORE_EXPRESSION + ")}]";
|
||||
var PUNC_CHARS = PUNC_BEFORE_EXPRESSION + "`)}]";
|
||||
var WHITESPACE_CHARS = NEWLINE_CHARS + " \u00a0\t\f\u000b\u200b\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\uFEFF";
|
||||
var NON_IDENTIFIER_CHARS = makePredicate(characters("./'\"" + OPERATOR_CHARS + PUNC_CHARS + WHITESPACE_CHARS));
|
||||
|
||||
@@ -191,7 +191,28 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
|
||||
regex_allowed : false,
|
||||
comments_before : [],
|
||||
directives : {},
|
||||
directive_stack : []
|
||||
directive_stack : [],
|
||||
read_template : with_eof_error("Unterminated template literal", function(strings) {
|
||||
var s = "";
|
||||
for (;;) {
|
||||
var ch = next(true, true);
|
||||
switch (ch) {
|
||||
case "\\":
|
||||
ch += next(true, true);
|
||||
break;
|
||||
case "`":
|
||||
strings.push(s);
|
||||
return;
|
||||
case "$":
|
||||
if (peek() == "{") {
|
||||
next();
|
||||
strings.push(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
s += ch;
|
||||
}
|
||||
}),
|
||||
};
|
||||
var prev_was_dot = false;
|
||||
|
||||
@@ -816,6 +837,7 @@ function parse($TEXT, options) {
|
||||
});
|
||||
case "[":
|
||||
case "(":
|
||||
case "`":
|
||||
return simple_statement();
|
||||
case ";":
|
||||
S.in_directives = false;
|
||||
@@ -1401,6 +1423,11 @@ function parse($TEXT, options) {
|
||||
var start = S.token;
|
||||
if (is("punc")) {
|
||||
switch (start.value) {
|
||||
case "`":
|
||||
var tmpl = template(null);
|
||||
tmpl.start = start;
|
||||
tmpl.end = prev();
|
||||
return subscripts(tmpl, allow_calls);
|
||||
case "(":
|
||||
next();
|
||||
if (is("punc", ")")) {
|
||||
@@ -1771,6 +1798,23 @@ function parse($TEXT, options) {
|
||||
}
|
||||
}
|
||||
|
||||
function template(tag) {
|
||||
var read = S.input.context().read_template;
|
||||
var strings = [];
|
||||
var expressions = [];
|
||||
while (read(strings)) {
|
||||
next();
|
||||
expressions.push(expression());
|
||||
if (!is("punc", "}")) unexpected();
|
||||
}
|
||||
next();
|
||||
return new AST_Template({
|
||||
expressions: expressions,
|
||||
strings: strings,
|
||||
tag: tag,
|
||||
});
|
||||
}
|
||||
|
||||
var subscripts = function(expr, allow_calls) {
|
||||
var start = expr.start;
|
||||
if (is("punc", ".")) {
|
||||
@@ -1804,6 +1848,12 @@ function parse($TEXT, options) {
|
||||
mark_pure(call);
|
||||
return subscripts(call, true);
|
||||
}
|
||||
if (is("punc", "`")) {
|
||||
var tmpl = template(expr);
|
||||
tmpl.start = expr.start;
|
||||
tmpl.end = prev();
|
||||
return subscripts(tmpl, allow_calls);
|
||||
}
|
||||
return expr;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user