Search in sources :

Example 96 with SourcePosition

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

the class Parser method parseExportDeclaration.

/*
  ExportDeclaration :
    export * FromClause ;
    export ExportClause [NoReference] FromClause ;
    export ExportClause ;
    export VariableStatement
    export Declaration[Default]
    export default AssignmentExpression ;
  ExportClause [NoReference] :
    { }
    { ExportsList [?NoReference] }
    { ExportsList [?NoReference] , }
  ExportsList [NoReference] :
    ExportSpecifier [?NoReference]
    ExportsList [?NoReference] , ExportSpecifier [?NoReference]
  ExportSpecifier [NoReference] :
    [~NoReference] IdentifierReference
    [~NoReference] IdentifierReference as IdentifierName
    [+NoReference] IdentifierName
    [+NoReference] IdentifierName as IdentifierName
   */
private ParseTree parseExportDeclaration() {
    SourcePosition start = getTreeStartLocation();
    boolean isDefault = false;
    boolean isExportAll = false;
    boolean isExportSpecifier = false;
    boolean needsSemiColon = true;
    eat(TokenType.EXPORT);
    ParseTree export = null;
    ImmutableList<ParseTree> exportSpecifierList = null;
    switch(peekType()) {
        case STAR:
            isExportAll = true;
            nextToken();
            break;
        case IDENTIFIER:
            export = parseAsyncFunctionDeclaration();
            break;
        case FUNCTION:
            export = parseFunctionDeclaration();
            needsSemiColon = false;
            break;
        case CLASS:
            export = parseClassDeclaration();
            needsSemiColon = false;
            break;
        case DEFAULT:
            isDefault = true;
            nextToken();
            export = parseExpression();
            needsSemiColon = false;
            break;
        case OPEN_CURLY:
            isExportSpecifier = true;
            exportSpecifierList = parseExportSpecifierSet();
            break;
        case VAR:
        case LET:
        case CONST:
        default:
            // unreachable, parse as a var decl to get a parse error.
            export = parseVariableDeclarationList();
            break;
    }
    LiteralToken moduleSpecifier = null;
    if (isExportAll || (isExportSpecifier && peekPredefinedString(PredefinedName.FROM))) {
        eatPredefinedString(PredefinedName.FROM);
        moduleSpecifier = (LiteralToken) eat(TokenType.STRING);
    } else if (isExportSpecifier) {
        for (ParseTree tree : exportSpecifierList) {
            IdentifierToken importedName = tree.asExportSpecifier().importedName;
            if (isKeyword(importedName.value)) {
                reportError(importedName, "cannot use keyword '%s' here.", importedName.value);
            }
        }
    }
    if (needsSemiColon || peekImplicitSemiColon()) {
        eatPossiblyImplicitSemiColon();
    }
    return new ExportDeclarationTree(getTreeLocation(start), isDefault, isExportAll, export, exportSpecifierList, moduleSpecifier);
}
Also used : ExportDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 97 with SourcePosition

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

the class Parser method parse.

private ParseTree parse(Expression expressionIn) {
    SourcePosition start = getTreeStartLocation();
    ParseTree result = parseAssignment(expressionIn);
    if (peek(TokenType.COMMA) && !peek(1, TokenType.ELLIPSIS) && !peek(1, TokenType.CLOSE_PAREN)) {
        ImmutableList.Builder<ParseTree> exprs = ImmutableList.builder();
        exprs.add(result);
        while (peek(TokenType.COMMA) && !peek(1, TokenType.ELLIPSIS) && !peek(1, TokenType.CLOSE_PAREN)) {
            eat(TokenType.COMMA);
            exprs.add(parseAssignment(expressionIn));
        }
        return new CommaExpressionTree(getTreeLocation(start), exprs.build());
    }
    return result;
}
Also used : CommaExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.CommaExpressionTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 98 with SourcePosition

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

the class Parser method parseObjectPatternField.

private ParseTree parseObjectPatternField(PatternKind kind) {
    SourcePosition start = getTreeStartLocation();
    if (peekType() == TokenType.OPEN_SQUARE) {
        ParseTree key = parseComputedPropertyName();
        eat(TokenType.COLON);
        ParseTree value = parsePatternAssignmentTarget(kind);
        return new ComputedPropertyDefinitionTree(getTreeLocation(start), key, value);
    }
    Token name;
    if (peekIdOrKeyword()) {
        name = eatIdOrKeywordAsId();
        if (!peek(TokenType.COLON)) {
            IdentifierToken idToken = (IdentifierToken) name;
            if (Keywords.isKeyword(idToken.value)) {
                reportError("cannot use keyword '%s' here.", name);
            }
            if (peek(TokenType.EQUAL)) {
                IdentifierExpressionTree idTree = new IdentifierExpressionTree(getTreeLocation(start), idToken);
                eat(TokenType.EQUAL);
                ParseTree defaultValue = parseAssignmentExpression();
                return new DefaultParameterTree(getTreeLocation(start), idTree, defaultValue);
            }
            return new PropertyNameAssignmentTree(getTreeLocation(start), name, null);
        }
    } else {
        name = parseLiteralExpression().literalToken;
    }
    eat(TokenType.COLON);
    ParseTree value = parsePatternAssignmentTarget(kind);
    return new PropertyNameAssignmentTree(getTreeLocation(start), name, value);
}
Also used : ComputedPropertyDefinitionTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyDefinitionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) IdentifierExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree) PropertyNameAssignmentTree(com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 99 with SourcePosition

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

the class Parser method parseImportSpecifier.

// ImportSpecifier ::= Identifier ('as' Identifier)?
private ParseTree parseImportSpecifier() {
    SourcePosition start = getTreeStartLocation();
    IdentifierToken importedName = eatIdOrKeywordAsId();
    IdentifierToken destinationName = null;
    if (peekPredefinedString(PredefinedName.AS)) {
        eatPredefinedString(PredefinedName.AS);
        destinationName = eatId();
    } else if (isKeyword(importedName.value)) {
        reportExpectedError(null, PredefinedName.AS);
    }
    return new ImportSpecifierTree(getTreeLocation(start), importedName, destinationName);
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ImportSpecifierTree(com.google.javascript.jscomp.parsing.parser.trees.ImportSpecifierTree)

Example 100 with SourcePosition

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

the class Parser method parseUnaryExpression.

// 11.4 Unary Operator
private ParseTree parseUnaryExpression() {
    SourcePosition start = getTreeStartLocation();
    if (peekUnaryOperator()) {
        Token operator = nextToken();
        ParseTree operand = parseUnaryExpression();
        return new UnaryExpressionTree(getTreeLocation(start), operator, operand);
    } else if (peekAwaitExpression()) {
        return parseAwaitExpression();
    } else {
        return parseUpdateExpression();
    }
}
Also used : UnaryExpressionTree(com.google.javascript.jscomp.parsing.parser.trees.UnaryExpressionTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) 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