Search in sources :

Example 56 with ParseTree

use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.

the class Parser method parseInterfaceElement.

private ParseTree parseInterfaceElement() {
    SourcePosition start = getTreeStartLocation();
    boolean isGenerator = eatOpt(TokenType.STAR) != null;
    IdentifierToken name = null;
    TokenType type = peekType();
    if (type == TokenType.NEW) {
        // ConstructSignature
        return parseCallSignature(true);
    } else if (type == TokenType.IDENTIFIER || Keywords.isKeyword(type)) {
        name = eatIdOrKeywordAsId();
    } else if (type == TokenType.OPEN_SQUARE) {
        // IndexSignature
        return parseIndexSignature();
    } else if (type == TokenType.OPEN_ANGLE || type == TokenType.OPEN_PAREN) {
        // CallSignature
        return parseCallSignature(false);
    }
    boolean isOptional = false;
    if (peek(TokenType.QUESTION)) {
        eat(TokenType.QUESTION);
        isOptional = true;
    }
    if (peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
        // Method signature.
        ParseTree function = parseMethodSignature(start, name, false, isGenerator, isOptional, null);
        return function;
    } else {
        // Property signature.
        ParseTree declaredType = maybeParseColonType();
        return new MemberVariableTree(getTreeLocation(start), name, false, isOptional, null, declaredType);
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) ComputedPropertyMemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree) MemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree)

Example 57 with ParseTree

use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.

the class Parser method parseAmbientDeclarationHelper.

private ParseTree parseAmbientDeclarationHelper() {
    ParseTree declare;
    switch(peekType()) {
        case FUNCTION:
            declare = parseAmbientFunctionDeclaration();
            eatPossibleImplicitSemiColon();
            break;
        case CLASS:
            declare = parseClassDeclaration(true);
            break;
        case ENUM:
            declare = parseEnumDeclaration();
            break;
        case MODULE:
        case NAMESPACE:
            declare = parseNamespaceDeclaration(true);
            break;
        case VAR:
        case LET:
        case CONST:
        default:
            // unreachable, parse as a var decl to get a parse error.
            declare = parseAmbientVariableDeclarationList();
            eatPossibleImplicitSemiColon();
            break;
    }
    return declare;
}
Also used : ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 58 with ParseTree

use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.

the class Parser method parseClassMemberDeclaration.

private ParseTree parseClassMemberDeclaration(PartialClassElement partial) {
    boolean isGenerator = eatOpt(TokenType.STAR) != null;
    ParseTree nameExpr;
    IdentifierToken name;
    if (peekPropertyName(0)) {
        if (peekIdOrKeyword()) {
            nameExpr = null;
            name = eatIdOrKeywordAsId();
            if (Keywords.isKeyword(name.value)) {
                features = features.with(Feature.KEYWORDS_AS_PROPERTIES);
            }
        } else {
            // { 'str'() {} }
            // { 123() {} }
            // Treat these as if they were computed properties.
            name = null;
            nameExpr = parseLiteralExpression();
        }
    } else {
        if (config.parseTypeSyntax && peekIndexSignature()) {
            ParseTree indexSignature = parseIndexSignature();
            eatPossibleImplicitSemiColon();
            return indexSignature;
        }
        nameExpr = parseComputedPropertyName();
        name = null;
    }
    // Member variables are supported only when parsing type syntax
    if (!config.parseTypeSyntax || peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
        // Member function.
        FunctionDeclarationTree.Kind kind;
        TokenType accessOnFunction;
        if (nameExpr == null) {
            kind = FunctionDeclarationTree.Kind.MEMBER;
            accessOnFunction = partial.accessModifier;
        } else {
            kind = FunctionDeclarationTree.Kind.EXPRESSION;
            // Accessibility modifier goes on the ComputedPropertyMethodTree
            accessOnFunction = null;
        }
        ParseTree function;
        if (partial.isAmbient) {
            function = parseMethodSignature(partial, name, isGenerator, /* isOptional */
            false);
            eatPossibleImplicitSemiColon();
        } else {
            FunctionDeclarationTree.Builder builder = FunctionDeclarationTree.builder(kind).setName(name).setStatic(partial.isStatic).setAccess(accessOnFunction);
            parseFunctionTail(builder, isGenerator ? FunctionFlavor.GENERATOR : FunctionFlavor.NORMAL);
            function = builder.build(getTreeLocation(partial.start));
        }
        if (kind == FunctionDeclarationTree.Kind.MEMBER) {
            return function;
        } else {
            return new ComputedPropertyMethodTree(getTreeLocation(partial.start), partial.accessModifier, nameExpr, function);
        }
    } else {
        // Member variable.
        if (isGenerator) {
            reportError("Member variable cannot be prefixed by '*' (generator function)");
        }
        ParseTree declaredType = maybeParseColonType();
        if (peek(TokenType.EQUAL)) {
            reportError("Member variable initializers ('=') are not supported");
        }
        eatPossibleImplicitSemiColon();
        if (nameExpr == null) {
            return new MemberVariableTree(getTreeLocation(partial.start), name, partial.isStatic, false, partial.accessModifier, declaredType);
        } else {
            return new ComputedPropertyMemberVariableTree(getTreeLocation(partial.start), nameExpr, partial.isStatic, partial.accessModifier, declaredType);
        }
    }
}
Also used : FunctionDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree) ComputedPropertyMemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree) ComputedPropertyMethodTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMethodTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) ComputedPropertyMemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree) MemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree)

Example 59 with ParseTree

use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree in project closure-compiler by google.

the class Parser method parseType.

private ParseTree parseType() {
    SourcePosition start = getTreeStartLocation();
    if (!peekId() && !EnumSet.of(TokenType.VOID, TokenType.OPEN_PAREN, TokenType.OPEN_CURLY, TokenType.TYPEOF).contains(peekType())) {
        reportError("Unexpected token '%s' in type expression", peekType());
        return new TypeNameTree(getTreeLocation(start), ImmutableList.of("error"));
    }
    ParseTree typeExpression = parseFunctionTypeExpression();
    if (!peek(TokenType.BAR)) {
        return typeExpression;
    }
    ImmutableList.Builder<ParseTree> unionType = ImmutableList.builder();
    unionType.add(typeExpression);
    do {
        eat(TokenType.BAR);
        unionType.add(parseArrayTypeExpression());
    } while (peek(TokenType.BAR));
    return new UnionTypeTree(getTreeLocation(start), unionType.build());
}
Also used : TypeNameTree(com.google.javascript.jscomp.parsing.parser.trees.TypeNameTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) UnionTypeTree(com.google.javascript.jscomp.parsing.parser.trees.UnionTypeTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 60 with ParseTree

use of com.google.javascript.jscomp.parsing.parser.trees.ParseTree 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(boolean isAmbient) {
    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 FUNCTION:
            // TODO(bradfordcsmith): handle async functions here
            export = isAmbient ? parseAmbientFunctionDeclaration() : parseFunctionDeclaration();
            needsSemiColon = isAmbient;
            break;
        case CLASS:
            export = parseClassDeclaration(isAmbient);
            needsSemiColon = false;
            break;
        case INTERFACE:
            export = parseInterfaceDeclaration();
            needsSemiColon = false;
            break;
        case ENUM:
            export = parseEnumDeclaration();
            needsSemiColon = false;
            break;
        case MODULE:
        case NAMESPACE:
            export = parseNamespaceDeclaration(isAmbient);
            needsSemiColon = false;
            break;
        case DECLARE:
            export = parseAmbientDeclaration();
            needsSemiColon = false;
            break;
        case DEFAULT:
            isDefault = true;
            nextToken();
            export = parseExpression();
            needsSemiColon = false;
            break;
        case OPEN_CURLY:
            isExportSpecifier = true;
            exportSpecifierList = parseExportSpecifierSet();
            break;
        case TYPE:
            export = parseTypeAlias();
            break;
        case VAR:
        case LET:
        case CONST:
        default:
            // unreachable, parse as a var decl to get a parse error.
            export = isAmbient ? parseAmbientVariableDeclarationList() : 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()) {
        eatPossibleImplicitSemiColon();
    }
    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)

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