Search in sources :

Example 81 with PsiBuilder

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

the class DeclarationParser method eatBrackets.

private boolean eatBrackets(PsiBuilder builder, @Nullable @PropertyKey(resourceBundle = JavaErrorMessages.BUNDLE) String errorKey) {
    IElementType tokenType = builder.getTokenType();
    if (tokenType != JavaTokenType.LBRACKET && tokenType != JavaTokenType.AT)
        return true;
    PsiBuilder.Marker marker = builder.mark();
    int count = 0;
    while (true) {
        parseAnnotations(builder);
        if (!expect(builder, JavaTokenType.LBRACKET)) {
            break;
        }
        ++count;
        if (!expect(builder, JavaTokenType.RBRACKET)) {
            break;
        }
        ++count;
    }
    if (count == 0) {
        // just annotation, most probably belongs to a next declaration
        marker.rollbackTo();
        return true;
    }
    if (errorKey != null) {
        marker.error(JavaErrorMessages.message(errorKey));
    } else {
        marker.drop();
    }
    boolean paired = count % 2 == 0;
    if (!paired) {
        error(builder, JavaErrorMessages.message("expected.rbracket"));
    }
    return paired;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 82 with PsiBuilder

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

the class DeclarationParser method parseAnnotationParameterList.

@NotNull
private PsiBuilder.Marker parseAnnotationParameterList(final PsiBuilder builder) {
    PsiBuilder.Marker list = builder.mark();
    if (!expect(builder, JavaTokenType.LPARENTH)) {
        done(list, JavaElementType.ANNOTATION_PARAMETER_LIST);
        return list;
    }
    if (expect(builder, JavaTokenType.RPARENTH)) {
        done(list, JavaElementType.ANNOTATION_PARAMETER_LIST);
        return list;
    }
    final boolean isFirstParamNamed = parseAnnotationParameter(builder, true);
    boolean isFirstParamWarned = false;
    boolean afterBad = false;
    while (true) {
        final IElementType tokenType = builder.getTokenType();
        if (tokenType == null) {
            error(builder, JavaErrorMessages.message("expected.parameter"));
            break;
        } else if (expect(builder, JavaTokenType.RPARENTH)) {
            break;
        } else if (tokenType == JavaTokenType.COMMA) {
            final PsiBuilder.Marker errorStart = builder.mark();
            final PsiBuilder.Marker errorEnd = builder.mark();
            builder.advanceLexer();
            final boolean hasParamName = parseAnnotationParameter(builder, false);
            if (!isFirstParamNamed && hasParamName && !isFirstParamWarned) {
                errorStart.errorBefore(JavaErrorMessages.message("annotation.name.is.missing"), errorEnd);
                isFirstParamWarned = true;
            } else {
                errorStart.drop();
            }
            errorEnd.drop();
        } else if (!afterBad) {
            error(builder, JavaErrorMessages.message("expected.comma.or.rparen"));
            builder.advanceLexer();
            afterBad = true;
        } else {
            afterBad = false;
            parseAnnotationParameter(builder, false);
        }
    }
    done(list, JavaElementType.ANNOTATION_PARAMETER_LIST);
    return list;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 83 with PsiBuilder

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

the class ExpressionParser method parseUnary.

@Nullable
private PsiBuilder.Marker parseUnary(final PsiBuilder builder) {
    final IElementType tokenType = builder.getTokenType();
    if (PREFIX_OPS.contains(tokenType)) {
        final PsiBuilder.Marker unary = builder.mark();
        builder.advanceLexer();
        final PsiBuilder.Marker operand = parseUnary(builder);
        if (operand == null) {
            error(builder, JavaErrorMessages.message("expected.expression"));
        }
        unary.done(JavaElementType.PREFIX_EXPRESSION);
        return unary;
    } else if (tokenType == JavaTokenType.LPARENTH) {
        final PsiBuilder.Marker typeCast = builder.mark();
        builder.advanceLexer();
        ReferenceParser.TypeInfo typeInfo = myParser.getReferenceParser().parseTypeInfo(builder, ReferenceParser.EAT_LAST_DOT | ReferenceParser.WILDCARD | ReferenceParser.CONJUNCTIONS | ReferenceParser.INCOMPLETE_ANNO);
        if (typeInfo == null || !expect(builder, JavaTokenType.RPARENTH)) {
            typeCast.rollbackTo();
            return parsePostfix(builder);
        }
        if (PREF_ARITHMETIC_OPS.contains(builder.getTokenType()) && !typeInfo.isPrimitive) {
            typeCast.rollbackTo();
            return parsePostfix(builder);
        }
        final PsiBuilder.Marker expr = parseUnary(builder);
        if (expr == null) {
            if (!typeInfo.isParameterized) {
                // cannot parse correct parenthesized expression after correct parameterized type
                typeCast.rollbackTo();
                return parsePostfix(builder);
            } else {
                error(builder, JavaErrorMessages.message("expected.expression"));
            }
        }
        typeCast.done(JavaElementType.TYPE_CAST_EXPRESSION);
        return typeCast;
    } else {
        return parsePostfix(builder);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 84 with PsiBuilder

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

the class ExpressionParser method parseArrayInitializer.

@NotNull
public PsiBuilder.Marker parseArrayInitializer(@NotNull PsiBuilder builder, @NotNull IElementType type, @NotNull Function<PsiBuilder, Boolean> elementParser, @NotNull @PropertyKey(resourceBundle = BUNDLE) String missingElementKey) {
    final PsiBuilder.Marker arrayInit = builder.mark();
    builder.advanceLexer();
    boolean first = true;
    while (true) {
        if (builder.getTokenType() == JavaTokenType.RBRACE) {
            builder.advanceLexer();
            break;
        }
        if (builder.getTokenType() == null) {
            error(builder, JavaErrorMessages.message("expected.rbrace"));
            break;
        }
        if (!elementParser.fun(builder)) {
            if (builder.getTokenType() == JavaTokenType.COMMA) {
                if (first && builder.lookAhead(1) == JavaTokenType.RBRACE) {
                    advance(builder, 2);
                    break;
                }
                builder.error(JavaErrorMessages.message(missingElementKey));
            } else if (builder.getTokenType() != JavaTokenType.RBRACE) {
                error(builder, JavaErrorMessages.message("expected.rbrace"));
                break;
            }
        }
        first = false;
        final IElementType tokenType = builder.getTokenType();
        if (tokenType == JavaTokenType.COMMA) {
            builder.advanceLexer();
        } else if (tokenType != JavaTokenType.RBRACE) {
            error(builder, JavaErrorMessages.message("expected.comma"));
        }
    }
    arrayInit.done(type);
    return arrayInit;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with PsiBuilder

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

the class ExpressionParser method parseRelational.

@Nullable
private PsiBuilder.Marker parseRelational(final PsiBuilder builder) {
    PsiBuilder.Marker left = parseExpression(builder, ExprType.SHIFT);
    if (left == null)
        return null;
    IElementType tokenType;
    while ((tokenType = getGtTokenType(builder)) != null) {
        final IElementType toCreate;
        final ExprType toParse;
        if (RELATIONAL_OPS.contains(tokenType)) {
            toCreate = JavaElementType.BINARY_EXPRESSION;
            toParse = ExprType.SHIFT;
        } else if (tokenType == JavaTokenType.INSTANCEOF_KEYWORD) {
            toCreate = JavaElementType.INSTANCE_OF_EXPRESSION;
            toParse = ExprType.TYPE;
        } else {
            break;
        }
        final PsiBuilder.Marker expression = left.precede();
        advanceGtToken(builder, tokenType);
        final PsiBuilder.Marker right = parseExpression(builder, toParse);
        if (right == null) {
            error(builder, JavaErrorMessages.message(toParse == ExprType.TYPE ? "expected.type" : "expected.expression"));
            expression.done(toCreate);
            return expression;
        }
        expression.done(toCreate);
        left = expression;
    }
    return left;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiBuilder (com.intellij.lang.PsiBuilder)124 IElementType (com.intellij.psi.tree.IElementType)90 Nullable (org.jetbrains.annotations.Nullable)18 NotNull (org.jetbrains.annotations.NotNull)16 Project (com.intellij.openapi.project.Project)7 LighterASTNode (com.intellij.lang.LighterASTNode)6 GroovyElementType (org.jetbrains.plugins.groovy.lang.lexer.GroovyElementType)5 Language (com.intellij.lang.Language)3 ASTNode (com.intellij.lang.ASTNode)2 PsiElement (com.intellij.psi.PsiElement)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 PsiParser (com.intellij.lang.PsiParser)1 HtmlParsing (com.intellij.lang.html.HtmlParsing)1 Lexer (com.intellij.lexer.Lexer)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1