Search in sources :

Example 61 with ParseTree

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);
}
Also used : ImportDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ImportDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 62 with ParseTree

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());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) TemplateLiteralPortionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralPortionTree) TemplateLiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) TemplateSubstitutionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateSubstitutionTree)

Example 63 with ParseTree

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;
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 64 with ParseTree

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());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) FormalParameterListTree(com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)

Example 65 with ParseTree

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;
}
Also used : ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)100 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)81 ImmutableList (com.google.common.collect.ImmutableList)15 BinaryOperatorTree (com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree)14 FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)9 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)6 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)5 DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)5 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)5 ArgumentListTree (com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree)4 ComputedPropertyMethodTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree)4 OptionalMemberExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree)4 OptionalMemberLookupExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree)4 ComputedPropertyMemberVariableTree (com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree)3 GenericTypeListTree (com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree)3 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)3 NullTree (com.google.javascript.jscomp.parsing.parser.trees.NullTree)3 OptChainCallExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree)3 CommaExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree)2 DoWhileStatementTree (com.google.javascript.jscomp.parsing.parser.trees.DoWhileStatementTree)2