use of com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree in project closure-compiler by google.
the class Parser method parseTemplateLiteral.
/**
* Constructs a template literal expression tree. "operand" is used to handle
* the case like "foo`bar`", which is a CallExpression or MemberExpression that
* calls the function foo() with the template literal as the argument (with extra
* handling). In this case, operand would be "foo", which is the callsite.
*
* <p>We store this operand in the TemplateLiteralExpressionTree and
* generate a TAGGED_TEMPLATELIT node if it's not null later when transpiling.
*
* @param operand A non-null value would represent the callsite
* @return The template literal expression
*/
private TemplateLiteralExpressionTree parseTemplateLiteral(ParseTree operand) {
SourcePosition start = operand == null ? getTreeStartLocation() : operand.location.start;
Token token = nextToken();
ImmutableList.Builder<ParseTree> elements = ImmutableList.builder();
elements.add(new TemplateLiteralPortionTree(token.location, token));
if (token.type == TokenType.NO_SUBSTITUTION_TEMPLATE) {
return new TemplateLiteralExpressionTree(getTreeLocation(start), operand, elements.build());
}
// `abc${
ParseTree expression = parseExpression();
elements.add(new TemplateSubstitutionTree(expression.location, expression));
while (!errorReporter.hadError()) {
token = nextTemplateLiteralToken();
if (token.type == TokenType.ERROR || token.type == TokenType.END_OF_FILE) {
break;
}
elements.add(new TemplateLiteralPortionTree(token.location, token));
if (token.type == TokenType.TEMPLATE_TAIL) {
break;
}
expression = parseExpression();
elements.add(new TemplateSubstitutionTree(expression.location, expression));
}
return new TemplateLiteralExpressionTree(getTreeLocation(start), operand, elements.build());
}
Aggregations