Search in sources :

Example 36 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project intellij-community by JetBrains.

the class Declaration method tryParseWithGenerics.

private static IElementType tryParseWithGenerics(@NotNull PsiBuilder builder, boolean isInClass, boolean isInAnnotation, @Nullable String typeDefinitionName, @NotNull GroovyParser parser, boolean modifiersParsed, boolean expressionPossible, boolean acceptTypeParameters) {
    if (acceptTypeParameters) {
        TypeParameters.parse(builder);
    } else {
        final PsiBuilder.Marker error = builder.mark();
        TypeParameters.parse(builder);
        error.error(GroovyBundle.message("type.parameters.are.unexpected"));
    }
    //point to begin of type or variable
    PsiBuilder.Marker checkMarker = builder.mark();
    switch(TypeSpec.parse(builder, false, expressionPossible)) {
        case PATH_REF:
        case REF_WITH_TYPE_PARAMS:
            checkMarker.drop();
            break;
        case FAIL:
            checkMarker.rollbackTo();
            break;
        case IDENTIFIER:
            // declaration name element can be parsed as type element
            IElementType result = VariableDefinitions.parseDefinitions(builder, isInClass, isInAnnotation, typeDefinitionName, modifiersParsed, false, parser);
            if (result == GroovyElementTypes.WRONGWAY) {
                checkMarker.rollbackTo();
            } else {
                checkMarker.drop();
                return result;
            }
    }
    return VariableDefinitions.parseDefinitions(builder, isInClass, isInAnnotation, typeDefinitionName, modifiersParsed, false, parser);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 37 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project intellij-community by JetBrains.

the class Declaration method parseDeclarationWithoutGenerics.

private static IElementType parseDeclarationWithoutGenerics(@NotNull PsiBuilder builder, boolean isInClass, boolean isInAnnotation, @Nullable String typeDefinitionName, @NotNull GroovyParser parser, boolean modifiersParsed, boolean expressionPossible) {
    //point to begin of type or variable
    PsiBuilder.Marker checkMarker = builder.mark();
    ReferenceElement.ReferenceElementResult typeResult = TypeSpec.parse(builder, false, expressionPossible);
    if (typeResult == ReferenceElement.ReferenceElementResult.FAIL) {
        //if type wasn't recognized trying parse VariableDeclaration
        checkMarker.rollbackTo();
        if (isInAnnotation) {
            builder.error(GroovyBundle.message("type.expected"));
        }
        //current token isn't identifier
        return VariableDefinitions.parseDefinitions(builder, isInClass, isInAnnotation, typeDefinitionName, modifiersParsed, true, parser);
    } else {
        //type was recognized, identifier here
        //starts after type
        IElementType varDeclarationTop = VariableDefinitions.parseDefinitions(builder, isInClass, isInAnnotation, typeDefinitionName, modifiersParsed, false, parser);
        if (varDeclarationTop == GroovyElementTypes.WRONGWAY) {
            if (typeResult == ReferenceElement.ReferenceElementResult.REF_WITH_TYPE_PARAMS) {
                checkMarker.drop();
                return GroovyElementTypes.VARIABLE_DEFINITION_ERROR;
            }
            checkMarker.rollbackTo();
            if (isInAnnotation) {
                builder.error(GroovyBundle.message("type.expected"));
            }
            //starts before "type" identifier, here can't be tuple, because next token is identifier (we are in "type recognized" branch)
            return VariableDefinitions.parseDefinitions(builder, isInClass, isInAnnotation, typeDefinitionName, modifiersParsed, false, parser);
        } else {
            checkMarker.drop();
            return varDeclarationTop;
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ReferenceElement(org.jetbrains.plugins.groovy.lang.parser.parsing.statements.typeDefinitions.ReferenceElement) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 38 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project intellij-community by JetBrains.

the class Declaration method parse.

public static boolean parse(@NotNull PsiBuilder builder, boolean isInClass, boolean isInAnnotation, @Nullable String typeDefinitionName, @NotNull GroovyParser parser) {
    PsiBuilder.Marker declMarker = builder.mark();
    //allows error messages
    boolean modifiersParsed = Modifiers.parse(builder, parser);
    final boolean methodStart = GroovyTokenTypes.mLT == builder.getTokenType();
    final IElementType type = parseAfterModifiers(builder, isInClass, isInAnnotation, typeDefinitionName, parser, modifiersParsed);
    if (type == GroovyElementTypes.WRONGWAY) {
        if (modifiersParsed && methodStart) {
            declMarker.error(GroovyBundle.message("method.definitions.expected"));
            return false;
        }
        declMarker.rollbackTo();
        if (modifiersParsed) {
            builder.error(GroovyBundle.message("variable.definitions.expected"));
        }
        return false;
    }
    if (type != null) {
        declMarker.done(type);
    } else {
        declMarker.drop();
    }
    return true;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 39 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project intellij-community by JetBrains.

the class VariableDefinitions method parseVar.

private static IElementType parseVar(PsiBuilder builder, boolean isInClass, boolean hasModifiers, GroovyParser parser, boolean LParenth) {
    // a = b, c = d
    PsiBuilder.Marker varAssMarker = builder.mark();
    final IElementType declarator = parseDeclarator(builder, LParenth);
    if (declarator != GroovyElementTypes.WRONGWAY) {
        final boolean wasAssignment = parseAssignment(builder, parser);
        if (declarator == GroovyElementTypes.TUPLE_DECLARATION) {
            varAssMarker.drop();
            if (!wasAssignment) {
                builder.error(GroovyBundle.message("assignment.expected"));
                return GroovyElementTypes.WRONGWAY;
            }
        } else if (isInClass) {
            // a = b, c = d
            varAssMarker.done(GroovyElementTypes.FIELD);
        } else {
            varAssMarker.done(GroovyElementTypes.VARIABLE);
        }
        if (declarator != GroovyElementTypes.TUPLE_DECLARATION) {
            while (ParserUtils.getToken(builder, GroovyTokenTypes.mCOMMA)) {
                ParserUtils.getToken(builder, GroovyTokenTypes.mNLS);
                if (GroovyElementTypes.WRONGWAY.equals(parseVariableOrField(builder, isInClass, parser)) && declarator == GroovyTokenTypes.mIDENT) {
                    //parse b = d
                    return GroovyElementTypes.VARIABLE_DEFINITION_ERROR;
                }
            }
        }
        if (isInClass && declarator == GroovyElementTypes.TUPLE_DECLARATION) {
            builder.error(GroovyBundle.message("tuple.cant.be.placed.in.class"));
        }
        return GroovyElementTypes.VARIABLE_DEFINITION;
    } else {
        varAssMarker.drop();
        builder.error(GroovyBundle.message("identifier.expected"));
        return GroovyElementTypes.WRONGWAY;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 40 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project intellij-community by JetBrains.

the class ExpressionStatement method parse.

public static Result parse(PsiBuilder builder, GroovyParser parser) {
    PsiBuilder.Marker marker = builder.mark();
    final IElementType result = parseExpressionStatement(builder, parser);
    if (result != GroovyElementTypes.CALL_EXPRESSION && result != GroovyElementTypes.PATH_METHOD_CALL) {
        marker.drop();
        return result == GroovyElementTypes.WRONGWAY ? Result.WRONG_WAY : Result.EXPRESSION;
    }
    boolean isExprStatement = result == GroovyElementTypes.CALL_EXPRESSION;
    while (true) {
        boolean nameParsed = namePartParse(builder, parser) == GroovyElementTypes.REFERENCE_EXPRESSION;
        PsiBuilder.Marker exprStatement;
        if (nameParsed) {
            exprStatement = marker.precede();
            marker.done(GroovyElementTypes.REFERENCE_EXPRESSION);
        } else {
            exprStatement = marker;
        }
        if (builder.getTokenType() == GroovyTokenTypes.mLPAREN) {
            PrimaryExpression.methodCallArgsParse(builder, parser);
            exprStatement.done(GroovyElementTypes.PATH_METHOD_CALL);
        } else if (GroovyTokenTypes.mLBRACK.equals(builder.getTokenType()) && !ParserUtils.lookAhead(builder, GroovyTokenTypes.mLBRACK, GroovyTokenTypes.mCOLON) && !ParserUtils.lookAhead(builder, GroovyTokenTypes.mLBRACK, GroovyTokenTypes.mNLS, GroovyTokenTypes.mCOLON)) {
            PathExpression.indexPropertyArgsParse(builder, parser);
            exprStatement.done(GroovyElementTypes.PATH_INDEX_PROPERTY);
            boolean isCall = false;
            if (GroovyTokenTypes.mLPAREN.equals(builder.getTokenType())) {
                PrimaryExpression.methodCallArgsParse(builder, parser);
                isCall = true;
            } else if (GroovyTokenTypes.mLCURLY.equals(builder.getTokenType())) {
                PsiBuilder.Marker argsMarker = builder.mark();
                argsMarker.done(GroovyElementTypes.ARGUMENTS);
                isCall = true;
            }
            while (GroovyTokenTypes.mLCURLY.equals(builder.getTokenType())) {
                OpenOrClosableBlock.parseClosableBlock(builder, parser);
            }
            if (isCall) {
                exprStatement = exprStatement.precede();
                exprStatement.done(GroovyElementTypes.PATH_METHOD_CALL);
            }
        } else if (nameParsed && CommandArguments.parseCommandArguments(builder, parser)) {
            isExprStatement = true;
            exprStatement.done(GroovyElementTypes.CALL_EXPRESSION);
        } else {
            exprStatement.drop();
            break;
        }
        marker = exprStatement.precede();
    }
    return isExprStatement ? Result.EXPR_STATEMENT : Result.EXPRESSION;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Aggregations

PsiBuilder (com.intellij.lang.PsiBuilder)104 IElementType (com.intellij.psi.tree.IElementType)78 Nullable (org.jetbrains.annotations.Nullable)18 NotNull (org.jetbrains.annotations.NotNull)16 Project (com.intellij.openapi.project.Project)5 GroovyElementType (org.jetbrains.plugins.groovy.lang.lexer.GroovyElementType)5 Language (com.intellij.lang.Language)3 ASTNode (com.intellij.lang.ASTNode)2 ILazyParseableElementType (com.intellij.psi.tree.ILazyParseableElementType)2 ParsingContext (com.jetbrains.python.parsing.ParsingContext)2 StatementParsing (com.jetbrains.python.parsing.StatementParsing)2 ParserDefinition (com.intellij.lang.ParserDefinition)1 HtmlParsing (com.intellij.lang.html.HtmlParsing)1 Lexer (com.intellij.lexer.Lexer)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 SmartList (com.intellij.util.SmartList)1 Stack (com.intellij.util.containers.Stack)1