Search in sources :

Example 71 with PsiBuilder

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

the class JavaParsingTestCase method createBuilder.

private static PsiBuilder createBuilder(final ASTNode chameleon) {
    final PsiBuilder builder = JavaParserUtil.createBuilder(chameleon);
    builder.setDebugMode(true);
    return builder;
}
Also used : PsiBuilder(com.intellij.lang.PsiBuilder)

Example 72 with PsiBuilder

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

the class ReferenceParser method parseReferenceParameterList.

public boolean parseReferenceParameterList(final PsiBuilder builder, final boolean wildcard, final boolean diamonds) {
    final PsiBuilder.Marker list = builder.mark();
    if (!expect(builder, JavaTokenType.LT)) {
        list.done(JavaElementType.REFERENCE_PARAMETER_LIST);
        return false;
    }
    int flags = set(set(EAT_LAST_DOT, WILDCARD, wildcard), DIAMONDS, diamonds);
    boolean isOk = true;
    while (true) {
        if (parseTypeInfo(builder, flags, true) == null) {
            error(builder, JavaErrorMessages.message("expected.identifier"));
        } else {
            IElementType tokenType = builder.getTokenType();
            if (WILDCARD_KEYWORD_SET.contains(tokenType)) {
                parseReferenceList(builder, tokenType, null, JavaTokenType.AND);
            }
        }
        if (expect(builder, JavaTokenType.GT)) {
            break;
        } else if (!expectOrError(builder, JavaTokenType.COMMA, "expected.gt.or.comma")) {
            isOk = false;
            break;
        }
        flags = set(flags, DIAMONDS, false);
    }
    list.done(JavaElementType.REFERENCE_PARAMETER_LIST);
    return isOk;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 73 with PsiBuilder

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

the class StatementParser method parseStatement.

@Nullable
public PsiBuilder.Marker parseStatement(final PsiBuilder builder) {
    final IElementType tokenType = builder.getTokenType();
    if (tokenType == JavaTokenType.IF_KEYWORD) {
        return parseIfStatement(builder);
    } else if (tokenType == JavaTokenType.WHILE_KEYWORD) {
        return parseWhileStatement(builder);
    } else if (tokenType == JavaTokenType.FOR_KEYWORD) {
        return parseForStatement(builder);
    } else if (tokenType == JavaTokenType.DO_KEYWORD) {
        return parseDoWhileStatement(builder);
    } else if (tokenType == JavaTokenType.SWITCH_KEYWORD) {
        return parseSwitchStatement(builder);
    } else if (tokenType == JavaTokenType.CASE_KEYWORD || tokenType == JavaTokenType.DEFAULT_KEYWORD) {
        return parseSwitchLabelStatement(builder);
    } else if (tokenType == JavaTokenType.BREAK_KEYWORD) {
        return parseBreakStatement(builder);
    } else if (tokenType == JavaTokenType.CONTINUE_KEYWORD) {
        return parseContinueStatement(builder);
    } else if (tokenType == JavaTokenType.RETURN_KEYWORD) {
        return parseReturnStatement(builder);
    } else if (tokenType == JavaTokenType.THROW_KEYWORD) {
        return parseThrowStatement(builder);
    } else if (tokenType == JavaTokenType.SYNCHRONIZED_KEYWORD) {
        return parseSynchronizedStatement(builder);
    } else if (tokenType == JavaTokenType.TRY_KEYWORD) {
        return parseTryStatement(builder);
    } else if (tokenType == JavaTokenType.ASSERT_KEYWORD) {
        return parseAssertStatement(builder);
    } else if (tokenType == JavaTokenType.LBRACE) {
        return parseBlockStatement(builder);
    } else if (tokenType instanceof ILazyParseableElementType) {
        builder.advanceLexer();
        return null;
    } else if (tokenType == JavaTokenType.SEMICOLON) {
        final PsiBuilder.Marker empty = builder.mark();
        builder.advanceLexer();
        done(empty, JavaElementType.EMPTY_STATEMENT);
        return empty;
    } else if (tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.AT) {
        final PsiBuilder.Marker refPos = builder.mark();
        myParser.getDeclarationParser().parseAnnotations(builder);
        skipQualifiedName(builder);
        final IElementType suspectedLT = builder.getTokenType(), next = builder.lookAhead(1);
        refPos.rollbackTo();
        if (suspectedLT == JavaTokenType.LT || suspectedLT == JavaTokenType.DOT && next == JavaTokenType.AT) {
            final PsiBuilder.Marker declStatement = builder.mark();
            final PsiBuilder.Marker decl = myParser.getDeclarationParser().parse(builder, DeclarationParser.Context.CODE_BLOCK);
            if (decl == null) {
                PsiBuilder.Marker marker = myParser.getReferenceParser().parseType(builder, 0);
                error(builder, JavaErrorMessages.message("expected.identifier"));
                if (marker == null)
                    builder.advanceLexer();
            }
            done(declStatement, JavaElementType.DECLARATION_STATEMENT);
            return declStatement;
        }
    }
    final PsiBuilder.Marker pos = builder.mark();
    final PsiBuilder.Marker expr = myParser.getExpressionParser().parse(builder);
    if (expr != null) {
        int count = 1;
        final PsiBuilder.Marker list = expr.precede();
        final PsiBuilder.Marker statement = list.precede();
        while (builder.getTokenType() == JavaTokenType.COMMA) {
            final PsiBuilder.Marker commaPos = builder.mark();
            builder.advanceLexer();
            final PsiBuilder.Marker expr1 = myParser.getExpressionParser().parse(builder);
            if (expr1 == null) {
                commaPos.rollbackTo();
                break;
            }
            commaPos.drop();
            count++;
        }
        if (count > 1) {
            pos.drop();
            done(list, JavaElementType.EXPRESSION_LIST);
            semicolon(builder);
            done(statement, JavaElementType.EXPRESSION_LIST_STATEMENT);
            return statement;
        }
        if (exprType(expr) != JavaElementType.REFERENCE_EXPRESSION) {
            drop(list, pos);
            semicolon(builder);
            done(statement, JavaElementType.EXPRESSION_STATEMENT);
            return statement;
        }
        pos.rollbackTo();
    } else {
        pos.drop();
    }
    final PsiBuilder.Marker decl = myParser.getDeclarationParser().parse(builder, DeclarationParser.Context.CODE_BLOCK);
    if (decl != null) {
        final PsiBuilder.Marker statement = decl.precede();
        done(statement, JavaElementType.DECLARATION_STATEMENT);
        return statement;
    }
    if (builder.getTokenType() == JavaTokenType.IDENTIFIER && builder.lookAhead(1) == JavaTokenType.COLON) {
        final PsiBuilder.Marker statement = builder.mark();
        advance(builder, 2);
        parseStatement(builder);
        done(statement, JavaElementType.LABELED_STATEMENT);
        return statement;
    }
    if (expr != null) {
        final PsiBuilder.Marker statement = builder.mark();
        myParser.getExpressionParser().parse(builder);
        semicolon(builder);
        done(statement, JavaElementType.EXPRESSION_STATEMENT);
        return statement;
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ILazyParseableElementType(com.intellij.psi.tree.ILazyParseableElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with PsiBuilder

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

the class StatementParser method parseStatements.

private void parseStatements(final PsiBuilder builder, @Nullable final BraceMode braceMode) {
    while (builder.getTokenType() != null) {
        final PsiBuilder.Marker statement = parseStatement(builder);
        if (statement != null)
            continue;
        final IElementType tokenType = builder.getTokenType();
        if (tokenType == JavaTokenType.RBRACE) {
            if (braceMode == BraceMode.TILL_FIRST) {
                return;
            } else if (braceMode == BraceMode.TILL_LAST) {
                if (builder.lookAhead(1) == null) {
                    return;
                }
            }
        }
        final PsiBuilder.Marker error = builder.mark();
        builder.advanceLexer();
        if (tokenType == JavaTokenType.ELSE_KEYWORD) {
            error.error(JavaErrorMessages.message("else.without.if"));
        } else if (tokenType == JavaTokenType.CATCH_KEYWORD) {
            error.error(JavaErrorMessages.message("catch.without.try"));
        } else if (tokenType == JavaTokenType.FINALLY_KEYWORD) {
            error.error(JavaErrorMessages.message("finally.without.try"));
        } else {
            error.error(JavaErrorMessages.message("unexpected.token"));
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 75 with PsiBuilder

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

the class StatementParser method parseCodeBlock.

@Nullable
public PsiBuilder.Marker parseCodeBlock(final PsiBuilder builder, final boolean isStatement) {
    if (builder.getTokenType() != JavaTokenType.LBRACE)
        return null;
    else if (isStatement && isParseStatementCodeBlocksDeep(builder))
        return parseCodeBlockDeep(builder, false);
    final PsiBuilder.Marker codeBlock = builder.mark();
    builder.advanceLexer();
    boolean greedyBlock = false;
    int braceCount = 1;
    while (true) {
        final IElementType tokenType = builder.getTokenType();
        if (tokenType == null) {
            greedyBlock = true;
            break;
        }
        if (tokenType == JavaTokenType.LBRACE) {
            braceCount++;
        } else if (tokenType == JavaTokenType.RBRACE) {
            braceCount--;
        }
        builder.advanceLexer();
        if (braceCount == 0) {
            break;
        } else if (braceCount == 1 && (tokenType == JavaTokenType.SEMICOLON || tokenType == JavaTokenType.RBRACE)) {
            final PsiBuilder.Marker position = builder.mark();
            final List<IElementType> list = new SmartList<>();
            while (true) {
                final IElementType type = builder.getTokenType();
                if (ElementType.PRIMITIVE_TYPE_BIT_SET.contains(type) || ElementType.MODIFIER_BIT_SET.contains(type) || (type == JavaTokenType.IDENTIFIER && list.size() > 0) || type == JavaTokenType.LT || type == JavaTokenType.GT || type == JavaTokenType.GTGT || type == JavaTokenType.GTGTGT || type == JavaTokenType.COMMA || type == JavaTokenType.DOT || type == JavaTokenType.EXTENDS_KEYWORD || type == JavaTokenType.IMPLEMENTS_KEYWORD) {
                    list.add(type);
                    builder.advanceLexer();
                } else {
                    break;
                }
            }
            if (builder.getTokenType() == JavaTokenType.LPARENTH && list.size() >= 2) {
                final IElementType last = list.get(list.size() - 1);
                final IElementType prevLast = list.get(list.size() - 2);
                if (last == JavaTokenType.IDENTIFIER && (prevLast == JavaTokenType.IDENTIFIER || ElementType.PRIMITIVE_TYPE_BIT_SET.contains(prevLast))) {
                    position.rollbackTo();
                    greedyBlock = true;
                    break;
                }
            }
            position.drop();
        }
    }
    codeBlock.collapse(JavaElementType.CODE_BLOCK);
    if (greedyBlock) {
        codeBlock.setCustomEdgeTokenBinders(null, WhitespacesBinders.GREEDY_RIGHT_BINDER);
    }
    return codeBlock;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) List(java.util.List) SmartList(com.intellij.util.SmartList) 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