Search in sources :

Example 21 with PsiBuilder

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

the class ExpressionParser method parseArgumentList.

@NotNull
public PsiBuilder.Marker parseArgumentList(final PsiBuilder builder) {
    final PsiBuilder.Marker list = builder.mark();
    builder.advanceLexer();
    boolean first = true;
    while (true) {
        final IElementType tokenType = builder.getTokenType();
        if (first && (ARGS_LIST_END.contains(tokenType) || builder.eof()))
            break;
        if (!first && !ARGS_LIST_CONTINUE.contains(tokenType))
            break;
        boolean hasError = false;
        if (!first) {
            if (builder.getTokenType() == JavaTokenType.COMMA) {
                builder.advanceLexer();
            } else {
                hasError = true;
                error(builder, JavaErrorMessages.message("expected.comma.or.rparen"));
                emptyExpression(builder);
            }
        }
        first = false;
        final PsiBuilder.Marker arg = parse(builder);
        if (arg == null) {
            if (!hasError) {
                error(builder, JavaErrorMessages.message("expected.expression"));
                emptyExpression(builder);
            }
            if (!ARGS_LIST_CONTINUE.contains(builder.getTokenType()))
                break;
            if (builder.getTokenType() != JavaTokenType.COMMA && !builder.eof()) {
                builder.advanceLexer();
            }
        }
    }
    final boolean closed = expectOrError(builder, JavaTokenType.RPARENTH, "expected.rparen");
    list.done(JavaElementType.EXPRESSION_LIST);
    if (!closed) {
        list.setCustomEdgeTokenBinders(null, WhitespacesBinders.GREEDY_RIGHT_BINDER);
    }
    return list;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with PsiBuilder

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

the class ExpressionParser method parseBinary.

@Nullable
private PsiBuilder.Marker parseBinary(final PsiBuilder builder, final ExprType type, final TokenSet ops) {
    PsiBuilder.Marker result = parseExpression(builder, type);
    if (result == null)
        return null;
    int operandCount = 1;
    IElementType tokenType = getGtTokenType(builder);
    IElementType currentExprTokenType = tokenType;
    while (true) {
        if (tokenType == null || !ops.contains(tokenType))
            break;
        advanceGtToken(builder, tokenType);
        final PsiBuilder.Marker right = parseExpression(builder, type);
        operandCount++;
        tokenType = getGtTokenType(builder);
        if (tokenType == null || !ops.contains(tokenType) || tokenType != currentExprTokenType || right == null) {
            // save
            result = result.precede();
            if (right == null) {
                error(builder, JavaErrorMessages.message("expected.expression"));
            }
            result.done(operandCount > 2 ? JavaElementType.POLYADIC_EXPRESSION : JavaElementType.BINARY_EXPRESSION);
            if (right == null)
                break;
            currentExprTokenType = tokenType;
            operandCount = 1;
        }
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 23 with PsiBuilder

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

the class ExpressionParser method parsePrimary.

@Nullable
private PsiBuilder.Marker parsePrimary(PsiBuilder builder, @Nullable BreakPoint breakPoint, int breakOffset) {
    PsiBuilder.Marker startMarker = builder.mark();
    PsiBuilder.Marker expr = parsePrimaryExpressionStart(builder);
    if (expr == null) {
        startMarker.drop();
        return null;
    }
    while (true) {
        final IElementType tokenType = builder.getTokenType();
        if (tokenType == JavaTokenType.DOT) {
            final PsiBuilder.Marker dotPos = builder.mark();
            final int dotOffset = builder.getCurrentOffset();
            builder.advanceLexer();
            IElementType dotTokenType = builder.getTokenType();
            if (dotTokenType == JavaTokenType.AT) {
                myParser.getDeclarationParser().parseAnnotations(builder);
                dotTokenType = builder.getTokenType();
            }
            if (dotTokenType == JavaTokenType.CLASS_KEYWORD && exprType(expr) == JavaElementType.REFERENCE_EXPRESSION) {
                if (breakPoint == BreakPoint.P1 && builder.getCurrentOffset() == breakOffset) {
                    error(builder, JavaErrorMessages.message("expected.identifier"));
                    drop(startMarker, dotPos);
                    return expr;
                }
                final PsiBuilder.Marker copy = startMarker.precede();
                final int offset = builder.getCurrentOffset();
                startMarker.rollbackTo();
                final PsiBuilder.Marker classObjAccess = parseClassAccessOrMethodReference(builder);
                if (classObjAccess == null || builder.getCurrentOffset() < offset) {
                    copy.rollbackTo();
                    return parsePrimary(builder, BreakPoint.P1, offset);
                }
                startMarker = copy;
                expr = classObjAccess;
            } else if (dotTokenType == JavaTokenType.NEW_KEYWORD) {
                dotPos.drop();
                expr = parseNew(builder, expr);
            } else if (dotTokenType == JavaTokenType.SUPER_KEYWORD && builder.lookAhead(1) == JavaTokenType.LPARENTH) {
                dotPos.drop();
                PsiBuilder.Marker refExpr = expr.precede();
                builder.mark().done(JavaElementType.REFERENCE_PARAMETER_LIST);
                builder.advanceLexer();
                refExpr.done(JavaElementType.REFERENCE_EXPRESSION);
                expr = refExpr;
            } else if (THIS_OR_SUPER.contains(dotTokenType) && exprType(expr) == JavaElementType.REFERENCE_EXPRESSION) {
                if (breakPoint == BreakPoint.P2 && builder.getCurrentOffset() == breakOffset) {
                    dotPos.rollbackTo();
                    startMarker.drop();
                    return expr;
                }
                PsiBuilder.Marker copy = startMarker.precede();
                int offset = builder.getCurrentOffset();
                startMarker.rollbackTo();
                PsiBuilder.Marker ref = myParser.getReferenceParser().parseJavaCodeReference(builder, false, true, false, false);
                if (ref == null || builder.getTokenType() != JavaTokenType.DOT || builder.getCurrentOffset() != dotOffset) {
                    copy.rollbackTo();
                    return parsePrimary(builder, BreakPoint.P2, offset);
                }
                builder.advanceLexer();
                if (builder.getTokenType() != dotTokenType) {
                    copy.rollbackTo();
                    return parsePrimary(builder, BreakPoint.P2, offset);
                }
                builder.advanceLexer();
                startMarker = copy;
                expr = ref.precede();
                expr.done(dotTokenType == JavaTokenType.THIS_KEYWORD ? JavaElementType.THIS_EXPRESSION : JavaElementType.SUPER_EXPRESSION);
            } else {
                PsiBuilder.Marker refExpr = expr.precede();
                myParser.getReferenceParser().parseReferenceParameterList(builder, false, false);
                if (!expect(builder, ID_OR_SUPER)) {
                    dotPos.rollbackTo();
                    builder.advanceLexer();
                    myParser.getReferenceParser().parseReferenceParameterList(builder, false, false);
                    error(builder, JavaErrorMessages.message("expected.identifier"));
                    refExpr.done(JavaElementType.REFERENCE_EXPRESSION);
                    startMarker.drop();
                    return refExpr;
                }
                dotPos.drop();
                refExpr.done(JavaElementType.REFERENCE_EXPRESSION);
                expr = refExpr;
            }
        } else if (tokenType == JavaTokenType.LPARENTH) {
            if (exprType(expr) != JavaElementType.REFERENCE_EXPRESSION) {
                startMarker.drop();
                return expr;
            }
            PsiBuilder.Marker callExpr = expr.precede();
            parseArgumentList(builder);
            callExpr.done(JavaElementType.METHOD_CALL_EXPRESSION);
            expr = callExpr;
        } else if (tokenType == JavaTokenType.LBRACKET) {
            if (breakPoint == BreakPoint.P4) {
                startMarker.drop();
                return expr;
            }
            builder.advanceLexer();
            if (builder.getTokenType() == JavaTokenType.RBRACKET && exprType(expr) == JavaElementType.REFERENCE_EXPRESSION) {
                final int pos = builder.getCurrentOffset();
                final PsiBuilder.Marker copy = startMarker.precede();
                startMarker.rollbackTo();
                final PsiBuilder.Marker classObjAccess = parseClassAccessOrMethodReference(builder);
                if (classObjAccess == null || builder.getCurrentOffset() <= pos) {
                    copy.rollbackTo();
                    return parsePrimary(builder, BreakPoint.P4, -1);
                }
                startMarker = copy;
                expr = classObjAccess;
            } else {
                final PsiBuilder.Marker arrayAccess = expr.precede();
                final PsiBuilder.Marker index = parse(builder);
                if (index == null) {
                    error(builder, JavaErrorMessages.message("expected.expression"));
                    arrayAccess.done(JavaElementType.ARRAY_ACCESS_EXPRESSION);
                    startMarker.drop();
                    return arrayAccess;
                }
                if (builder.getTokenType() != JavaTokenType.RBRACKET) {
                    error(builder, JavaErrorMessages.message("expected.rbracket"));
                    arrayAccess.done(JavaElementType.ARRAY_ACCESS_EXPRESSION);
                    startMarker.drop();
                    return arrayAccess;
                }
                builder.advanceLexer();
                arrayAccess.done(JavaElementType.ARRAY_ACCESS_EXPRESSION);
                expr = arrayAccess;
            }
        } else if (tokenType == JavaTokenType.DOUBLE_COLON) {
            return parseMethodReference(builder, startMarker);
        } else {
            startMarker.drop();
            return expr;
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 24 with PsiBuilder

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

the class FileParser method parseImportStatement.

@Nullable
private PsiBuilder.Marker parseImportStatement(PsiBuilder builder) {
    if (builder.getTokenType() != JavaTokenType.IMPORT_KEYWORD)
        return null;
    final PsiBuilder.Marker statement = builder.mark();
    builder.advanceLexer();
    final boolean isStatic = expect(builder, JavaTokenType.STATIC_KEYWORD);
    final IElementType type = isStatic ? JavaElementType.IMPORT_STATIC_STATEMENT : JavaElementType.IMPORT_STATEMENT;
    final boolean isOk = myParser.getReferenceParser().parseImportCodeReference(builder, isStatic);
    if (isOk) {
        semicolon(builder);
    }
    done(statement, type);
    return statement;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with PsiBuilder

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

the class FileParser method parseImportList.

@NotNull
private Pair<PsiBuilder.Marker, Boolean> parseImportList(PsiBuilder builder, Predicate<PsiBuilder> stopper) {
    PsiBuilder.Marker list = builder.mark();
    IElementType tokenType = builder.getTokenType();
    boolean isEmpty = tokenType != JavaTokenType.IMPORT_KEYWORD && tokenType != JavaTokenType.SEMICOLON;
    if (!isEmpty) {
        PsiBuilder.Marker invalidElements = null;
        while (!builder.eof()) {
            if (stopper.test(builder)) {
                break;
            } else if (builder.getTokenType() == JavaTokenType.SEMICOLON) {
                builder.advanceLexer();
                continue;
            }
            final PsiBuilder.Marker statement = parseImportStatement(builder);
            if (statement != null) {
                if (invalidElements != null) {
                    invalidElements.errorBefore(JavaErrorMessages.message("unexpected.token"), statement);
                    invalidElements = null;
                }
                continue;
            }
            if (invalidElements == null) {
                invalidElements = builder.mark();
            }
            builder.advanceLexer();
        }
        if (invalidElements != null) {
            invalidElements.error(JavaErrorMessages.message("unexpected.token"));
        }
    }
    done(list, JavaElementType.IMPORT_LIST);
    return Pair.create(list, isEmpty);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) NotNull(org.jetbrains.annotations.NotNull)

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