Search in sources :

Example 76 with SourcePosition

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

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

the class Parser method parseReturnStatement.

// 12.9 The return Statement
private ParseTree parseReturnStatement() {
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.RETURN);
    ParseTree expression = null;
    if (!peekImplicitSemiColon()) {
        expression = parseExpression();
    }
    eatPossiblyImplicitSemiColon();
    return new ReturnStatementTree(getTreeLocation(start), expression);
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ReturnStatementTree(com.google.javascript.jscomp.parsing.parser.trees.ReturnStatementTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 78 with SourcePosition

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

the class Parser method parseExponentiationExpression.

private ParseTree parseExponentiationExpression() {
    SourcePosition start = getTreeStartLocation();
    ParseTree left = parseUnaryExpression();
    if (peek(TokenType.STAR_STAR)) {
        // -x**y is a syntax error
        if (left.type == ParseTreeType.UNARY_EXPRESSION) {
            reportError("Unary operator '%s' requires parentheses before '**'", left.asUnaryExpression().operator);
        }
        Token operator = nextToken();
        ParseTree right = parseExponentiationExpression();
        return new BinaryOperatorTree(getTreeLocation(start), left, operator, right);
    } else {
        return left;
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) BinaryOperatorTree(com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 79 with SourcePosition

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

the class Scanner method nextTemplateLiteralTokenShared.

private TemplateLiteralToken nextTemplateLiteralTokenShared(TokenType endType, TokenType middleType) {
    int beginIndex = index;
    // Save the starting position to use with the multi-line safe version of getTokenRange().
    SourcePosition startingPosition = getPosition(beginIndex);
    SkipTemplateCharactersResult skipTemplateCharactersResult = skipTemplateCharacters();
    if (isAtEnd()) {
        reportError(getPosition(beginIndex), "Unterminated template literal");
    }
    String value = getTokenString(beginIndex);
    switch(peekChar()) {
        case '`':
            nextChar();
            return new TemplateLiteralToken(endType, value, skipTemplateCharactersResult.getErrorMessage(), skipTemplateCharactersResult.getPosition(), getTokenRange(startingPosition));
        case '$':
            // $
            nextChar();
            // {
            nextChar();
            return new TemplateLiteralToken(middleType, value, skipTemplateCharactersResult.getErrorMessage(), skipTemplateCharactersResult.getPosition(), getTokenRange(beginIndex - 1));
        default:
            // Should have reported error already
            return new TemplateLiteralToken(endType, value, skipTemplateCharactersResult.getErrorMessage(), skipTemplateCharactersResult.getPosition(), getTokenRange(beginIndex - 1));
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) FormatString(com.google.errorprone.annotations.FormatString)

Example 80 with SourcePosition

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

the class Parser method parsePropertyAssignmentGenerator.

private ParseTree parsePropertyAssignmentGenerator() {
    TokenType type = peekType(1);
    if (type == TokenType.STRING || type == TokenType.NUMBER || type == TokenType.IDENTIFIER || Keywords.isKeyword(type)) {
        // parseMethodDeclaration will consume the '*'.
        return parseMethodDeclaration();
    } else {
        SourcePosition start = getTreeStartLocation();
        eat(TokenType.STAR);
        ParseTree name = parseComputedPropertyName();
        FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(FunctionDeclarationTree.Kind.EXPRESSION);
        parseFunctionTail(builder, FunctionFlavor.GENERATOR);
        ParseTree value = builder.build(getTreeLocation(start));
        return new ComputedPropertyMethodTree(getTreeLocation(start), name, value);
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) 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