use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseImportDeclaration.
private ParseTree parseImportDeclaration() {
SourcePosition start = getTreeStartLocation();
eat(TokenType.IMPORT);
// import ModuleSpecifier ;
if (peek(TokenType.STRING)) {
LiteralToken moduleSpecifier = eat(TokenType.STRING).asLiteral();
eatPossiblyImplicitSemiColon();
return new ImportDeclarationTree(getTreeLocation(start), null, null, null, moduleSpecifier);
}
// import ImportedDefaultBinding from ModuleSpecifier
// import NameSpaceImport from ModuleSpecifier
// import NamedImports from ModuleSpecifier ;
// import ImportedDefaultBinding , NameSpaceImport from ModuleSpecifier ;
// import ImportedDefaultBinding , NamedImports from ModuleSpecifier ;
IdentifierToken defaultBindingIdentifier = null;
IdentifierToken nameSpaceImportIdentifier = null;
ImmutableList<ParseTree> identifierSet = null;
boolean parseExplicitNames = true;
if (peekId()) {
defaultBindingIdentifier = eatId();
if (peek(TokenType.COMMA)) {
eat(TokenType.COMMA);
} else {
parseExplicitNames = false;
}
} else if (Keywords.isKeyword(peekType())) {
Token keyword = nextToken();
reportError(keyword, "cannot use keyword '%s' here.", keyword);
}
if (parseExplicitNames) {
if (peek(TokenType.STAR)) {
eat(TokenType.STAR);
eatPredefinedString(PredefinedName.AS);
nameSpaceImportIdentifier = eatId();
} else {
identifierSet = parseImportSpecifierSet();
}
}
eatPredefinedString(PredefinedName.FROM);
Token moduleStr = eat(TokenType.STRING);
LiteralToken moduleSpecifier = (moduleStr == null) ? null : moduleStr.asLiteral();
eatPossiblyImplicitSemiColon();
return new ImportDeclarationTree(getTreeLocation(start), defaultBindingIdentifier, identifierSet, nameSpaceImportIdentifier, moduleSpecifier);
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree 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();
if (!(token instanceof TemplateLiteralToken)) {
reportError(token, "Unexpected template literal token %s.", token.type.toString());
}
boolean isTaggedTemplate = operand != null;
TemplateLiteralToken templateToken = (TemplateLiteralToken) token;
if (!isTaggedTemplate) {
reportTemplateErrorIfPresent(templateToken);
}
ImmutableList.Builder<ParseTree> elements = ImmutableList.builder();
elements.add(new TemplateLiteralPortionTree(templateToken.location, templateToken));
if (templateToken.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()) {
templateToken = nextTemplateLiteralToken();
if (templateToken.type == TokenType.ERROR || templateToken.type == TokenType.END_OF_FILE) {
break;
}
if (!isTaggedTemplate) {
reportTemplateErrorIfPresent(templateToken);
}
elements.add(new TemplateLiteralPortionTree(templateToken.location, templateToken));
if (templateToken.type == TokenType.TEMPLATE_TAIL) {
break;
}
expression = parseExpression();
elements.add(new TemplateSubstitutionTree(expression.location, expression));
}
return new TemplateLiteralExpressionTree(getTreeLocation(start), operand, elements.build());
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseParameter.
private ParseTree parseParameter() {
SourcePosition start = getTreeStartLocation();
ParseTree parameter = null;
if (peek(TokenType.ELLIPSIS)) {
parameter = parseIterRest(PatternKind.INITIALIZER);
} else if (peekId()) {
parameter = parseIdentifierExpression();
} else if (peekPatternStart()) {
parameter = parsePattern(PatternKind.INITIALIZER);
} else {
throw new IllegalStateException("parseParameterCalled() without confirming a parameter exists.");
}
if (!parameter.isRestParameter() && peek(TokenType.EQUAL)) {
eat(TokenType.EQUAL);
ParseTree defaultValue = parseAssignmentExpression();
parameter = new DefaultParameterTree(getTreeLocation(start), parameter, defaultValue);
}
return parameter;
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseFormalParameterList.
private FormalParameterListTree parseFormalParameterList() {
SourcePosition listStart = getTreeStartLocation();
eat(TokenType.OPEN_PAREN);
ImmutableList.Builder<ParseTree> result = ImmutableList.builder();
boolean trailingComma = false;
ImmutableList.Builder<SourcePosition> commaPositions = ImmutableList.builder();
while (peekParameter()) {
result.add(parseParameter());
if (!peek(TokenType.CLOSE_PAREN)) {
Token comma = eat(TokenType.COMMA);
if (comma != null) {
commaPositions.add(comma.getStart());
} else {
// semi-arbitrary comma position in case the code is syntactially invalid & missing one
commaPositions.add(getTreeEndLocation());
}
if (peek(TokenType.CLOSE_PAREN)) {
recordFeatureUsed(Feature.TRAILING_COMMA_IN_PARAM_LIST);
if (!config.atLeast8) {
reportError(comma, "Invalid trailing comma in formal parameter list");
}
trailingComma = true;
}
}
}
eat(TokenType.CLOSE_PAREN);
return new FormalParameterListTree(getTreeLocation(listStart), result.build(), trailingComma, commaPositions.build());
}
use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.
the class Parser method parseComputedPropertyName.
private ParseTree parseComputedPropertyName() {
eat(TokenType.OPEN_SQUARE);
ParseTree assign = parseAssignmentExpression();
eat(TokenType.CLOSE_SQUARE);
return assign;
}
Aggregations