Search in sources :

Example 71 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class Parser method parseRegularExpressionLiteral.

private ParseTree parseRegularExpressionLiteral() {
    SourcePosition start = getTreeStartLocation();
    LiteralToken literal = nextRegularExpressionLiteralToken();
    return new LiteralExpressionTree(getTreeLocation(start), literal);
}
Also used : ObjectLiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.ObjectLiteralExpressionTree) TemplateLiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree) ArrayLiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.ArrayLiteralExpressionTree) LiteralExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.LiteralExpressionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition)

Example 72 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class Parser method parseIdentifierExpression.

private IdentifierExpressionTree parseIdentifierExpression() {
    SourcePosition start = getTreeStartLocation();
    IdentifierToken identifier = eatId();
    return new IdentifierExpressionTree(getTreeLocation(start), identifier);
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) IdentifierExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)

Example 73 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition 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 74 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition 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 75 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition 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)

Aggregations

SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)112 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)81 ImmutableList (com.google.common.collect.ImmutableList)15 BinaryOperatorTree (com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree)13 FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)7 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)6 DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)5 ArgumentListTree (com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree)4 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)4 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)4 MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)4 OptionalMemberExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree)4 OptionalMemberLookupExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree)4 ArrayLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayLiteralExpressionTree)3 GenericTypeListTree (com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree)3 ObjectLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ObjectLiteralExpressionTree)3 OptChainCallExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree)3 TemplateLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree)3 VariableDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree)3 FormatString (com.google.errorprone.annotations.FormatString)2