Search in sources :

Example 61 with IElementType

use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.

the class KtProperty method getReceiverTypeRefByTree.

@Nullable
private KtTypeReference getReceiverTypeRefByTree() {
    ASTNode node = getNode().getFirstChildNode();
    while (node != null) {
        IElementType tt = node.getElementType();
        if (tt == KtTokens.COLON)
            break;
        if (tt == KtNodeTypes.TYPE_REFERENCE) {
            return (KtTypeReference) node.getPsi();
        }
        node = node.getTreeNext();
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable)

Example 62 with IElementType

use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.

the class KtPsiUtil method getPriority.

private static int getPriority(@NotNull KtExpression expression) {
    int maxPriority = KotlinExpressionParsing.Precedence.values().length + 1;
    // same as postfix operations
    if (expression instanceof KtPostfixExpression || expression instanceof KtQualifiedExpression || expression instanceof KtCallExpression || expression instanceof KtArrayAccessExpression || expression instanceof KtDoubleColonExpression) {
        return maxPriority - 1;
    }
    if (expression instanceof KtPrefixExpression || expression instanceof KtLabeledExpression)
        return maxPriority - 2;
    if (expression instanceof KtIfExpression) {
        return KotlinExpressionParsing.Precedence.ASSIGNMENT.ordinal();
    }
    if (expression instanceof KtSuperExpression) {
        return maxPriority;
    }
    if (expression instanceof KtDeclaration || expression instanceof KtStatementExpression) {
        return 0;
    }
    IElementType operation = getOperation(expression);
    for (KotlinExpressionParsing.Precedence precedence : KotlinExpressionParsing.Precedence.values()) {
        if (precedence != KotlinExpressionParsing.Precedence.PREFIX && precedence != KotlinExpressionParsing.Precedence.POSTFIX && precedence.getOperations().contains(operation)) {
            return maxPriority - precedence.ordinal() - 1;
        }
    }
    return maxPriority;
}
Also used : KotlinExpressionParsing(org.jetbrains.kotlin.parsing.KotlinExpressionParsing) IElementType(com.intellij.psi.tree.IElementType)

Example 63 with IElementType

use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.

the class KotlinParsing method parseTypeRefContents.

// The extraRecoverySet is needed for the foo(bar<x, 1, y>(z)) case, to tell whether we should stop
// on expression-indicating symbols or not
private PsiBuilder.Marker parseTypeRefContents(TokenSet extraRecoverySet) {
    PsiBuilder.Marker typeRefMarker = mark();
    parseTypeModifierList();
    PsiBuilder.Marker typeElementMarker = mark();
    IElementType lookahead = lookahead(1);
    IElementType lookahead2 = lookahead(2);
    boolean typeBeforeDot = true;
    if (at(IDENTIFIER) && !(lookahead == DOT && lookahead2 == IDENTIFIER) && lookahead != LT && at(DYNAMIC_KEYWORD)) {
        PsiBuilder.Marker dynamicType = mark();
        // DYNAMIC_KEYWORD
        advance();
        dynamicType.done(DYNAMIC_TYPE);
    } else if (at(IDENTIFIER) || at(PACKAGE_KEYWORD) || atParenthesizedMutableForPlatformTypes(0)) {
        parseUserType();
    } else if (at(LPAR)) {
        PsiBuilder.Marker functionOrParenthesizedType = mark();
        // This may be a function parameter list or just a prenthesized type
        // LPAR
        advance();
        // parenthesized types, no reference element around it is needed
        parseTypeRefContents(TokenSet.EMPTY).drop();
        if (at(RPAR)) {
            // RPAR
            advance();
            if (at(ARROW)) {
                // It's a function type with one parameter specified
                //    (A) -> B
                functionOrParenthesizedType.rollbackTo();
                parseFunctionType();
            } else {
                // It's a parenthesized type
                //    (A)
                functionOrParenthesizedType.drop();
            }
        } else {
            // This must be a function type
            //   (A, B) -> C
            // or
            //   (a : A) -> C
            functionOrParenthesizedType.rollbackTo();
            parseFunctionType();
        }
    } else {
        errorWithRecovery("Type expected", TokenSet.orSet(TOP_LEVEL_DECLARATION_FIRST, TokenSet.create(EQ, COMMA, GT, RBRACKET, DOT, RPAR, RBRACE, LBRACE, SEMICOLON), extraRecoverySet));
        typeBeforeDot = false;
    }
    // Disabling token merge is required for cases like
    //    Int?.(Foo) -> Bar
    myBuilder.disableJoiningComplexTokens();
    typeElementMarker = parseNullableTypeSuffix(typeElementMarker);
    myBuilder.restoreJoiningComplexTokensState();
    if (typeBeforeDot && at(DOT)) {
        // This is a receiver for a function type
        //  A.(B) -> C
        //   ^
        PsiBuilder.Marker functionType = typeElementMarker.precede();
        PsiBuilder.Marker receiverTypeRef = typeElementMarker.precede();
        PsiBuilder.Marker receiverType = receiverTypeRef.precede();
        receiverTypeRef.done(TYPE_REFERENCE);
        receiverType.done(FUNCTION_TYPE_RECEIVER);
        // DOT
        advance();
        if (at(LPAR)) {
            parseFunctionTypeContents().drop();
        } else {
            error("Expecting function type");
        }
        functionType.done(FUNCTION_TYPE);
    }
    typeElementMarker.drop();
    return typeRefMarker;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 64 with IElementType

use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.

the class KotlinParsing method parseAnnotationOrList.

/*
     * annotation
     *   : "@" (annotationUseSiteTarget ":")? unescapedAnnotation
     *   ;
     *
     * annotationList
     *   : "@" (annotationUseSiteTarget ":")? "[" unescapedAnnotation+ "]"
     *   ;
     *
     * annotationUseSiteTarget
     *   : "file"
     *   : "field"
     *   : "property"
     *   : "get"
     *   : "set"
     *   : "param"
     *   : "setparam"
     *   ;
     */
private boolean parseAnnotationOrList(AnnotationParsingMode mode) {
    if (at(AT)) {
        IElementType nextRawToken = myBuilder.rawLookup(1);
        IElementType tokenToMatch = nextRawToken;
        boolean isTargetedAnnotation = false;
        if ((nextRawToken == IDENTIFIER || ANNOTATION_TARGETS.contains(nextRawToken)) && lookahead(2) == COLON) {
            tokenToMatch = lookahead(3);
            isTargetedAnnotation = true;
        } else if (lookahead(1) == COLON) {
            // recovery for "@:ann"
            isTargetedAnnotation = true;
            tokenToMatch = lookahead(2);
        }
        if (tokenToMatch == IDENTIFIER) {
            return parseAnnotation(mode);
        } else if (tokenToMatch == LBRACKET) {
            return parseAnnotationList(mode);
        } else {
            if (isTargetedAnnotation) {
                if (lookahead(1) == COLON) {
                    // AT, COLON
                    errorAndAdvance("Expected annotation identifier after ':'", 2);
                } else {
                    // AT, (ANNOTATION TARGET KEYWORD), COLON
                    errorAndAdvance("Expected annotation identifier after ':'", 3);
                }
            } else {
                // AT
                errorAndAdvance("Expected annotation identifier after '@'", 1);
            }
        }
        return true;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType)

Example 65 with IElementType

use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.

the class KotlinParsing method parseTopLevelDeclaration.

/*
     * toplevelObject
     *   : package
     *   : class
     *   : extension
     *   : function
     *   : property
     *   : typeAlias
     *   : object
     *   ;
     */
private void parseTopLevelDeclaration() {
    if (at(SEMICOLON)) {
        // SEMICOLON
        advance();
        return;
    }
    PsiBuilder.Marker decl = mark();
    ModifierDetector detector = new ModifierDetector();
    parseModifierList(detector, DEFAULT, TokenSet.EMPTY);
    IElementType keywordToken = tt();
    IElementType declType = null;
    if (keywordToken == CLASS_KEYWORD || keywordToken == INTERFACE_KEYWORD) {
        declType = parseClass(detector.isEnumDetected());
    } else if (keywordToken == FUN_KEYWORD) {
        declType = parseFunction();
    } else if (keywordToken == VAL_KEYWORD || keywordToken == VAR_KEYWORD) {
        declType = parseProperty();
    } else if (keywordToken == TYPE_ALIAS_KEYWORD) {
        declType = parseTypeAlias();
    } else if (keywordToken == OBJECT_KEYWORD) {
        parseObject(NameParsingMode.REQUIRED, true);
        declType = OBJECT_DECLARATION;
    } else if (at(LBRACE)) {
        error("Expecting a top level declaration");
        parseBlock();
        declType = FUN;
    }
    if (declType == null) {
        errorAndAdvance("Expecting a top level declaration");
        decl.drop();
    } else {
        closeDeclarationWithCommentBinders(decl, declType, true);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Aggregations

IElementType (com.intellij.psi.tree.IElementType)843 ASTNode (com.intellij.lang.ASTNode)127 PsiBuilder (com.intellij.lang.PsiBuilder)121 Nullable (org.jetbrains.annotations.Nullable)100 NotNull (org.jetbrains.annotations.NotNull)78 PsiElement (com.intellij.psi.PsiElement)77 TextRange (com.intellij.openapi.util.TextRange)43 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)26 ArrayList (java.util.ArrayList)22 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)22 Lexer (com.intellij.lexer.Lexer)17 GrBinaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrBinaryExpression)15 NonNls (org.jetbrains.annotations.NonNls)14 Document (com.intellij.openapi.editor.Document)13 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)13 LighterASTNode (com.intellij.lang.LighterASTNode)12 BracePair (com.intellij.lang.BracePair)10 Project (com.intellij.openapi.project.Project)9 PsiFile (com.intellij.psi.PsiFile)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9