Search in sources :

Example 16 with PsiBuilder

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

the class ModuleParser method parse.

@Nullable
public PsiBuilder.Marker parse(@NotNull PsiBuilder builder) {
    PsiBuilder.Marker module = builder.mark();
    PsiBuilder.Marker firstAnnotation = myParser.getDeclarationParser().parseAnnotations(builder);
    IElementType type = builder.getTokenType();
    String text = type == JavaTokenType.IDENTIFIER ? builder.getTokenText() : null;
    if (!(PsiKeyword.OPEN.equals(text) || PsiKeyword.MODULE.equals(text))) {
        module.rollbackTo();
        return null;
    }
    PsiBuilder.Marker modifierList = firstAnnotation != null ? firstAnnotation.precede() : builder.mark();
    if (PsiKeyword.OPEN.equals(text)) {
        mapAndAdvance(builder, JavaTokenType.OPEN_KEYWORD);
        text = builder.getTokenText();
    }
    JavaParserUtil.done(modifierList, JavaElementType.MODIFIER_LIST);
    if (PsiKeyword.MODULE.equals(text)) {
        mapAndAdvance(builder, JavaTokenType.MODULE_KEYWORD);
    } else {
        module.drop();
        parseExtras(builder, JavaErrorMessages.message("expected.module.declaration"));
        return module;
    }
    if (parseName(builder) == null) {
        module.drop();
        if (builder.getTokenType() != null) {
            parseExtras(builder, JavaErrorMessages.message("expected.module.declaration"));
        } else {
            error(builder, JavaErrorMessages.message("expected.identifier"));
        }
        return module;
    }
    if (!expect(builder, JavaTokenType.LBRACE)) {
        if (builder.getTokenType() != null) {
            parseExtras(builder, JavaErrorMessages.message("expected.module.declaration"));
        } else {
            error(builder, JavaErrorMessages.message("expected.lbrace"));
        }
    } else {
        parseModuleContent(builder);
    }
    JavaParserUtil.done(module, JavaElementType.MODULE);
    if (builder.getTokenType() != null) {
        parseExtras(builder, JavaErrorMessages.message("unexpected.tokens"));
    }
    return module;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with PsiBuilder

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

the class ModuleParser method parseModuleContent.

private void parseModuleContent(PsiBuilder builder) {
    IElementType token;
    PsiBuilder.Marker invalid = null;
    while ((token = builder.getTokenType()) != null) {
        if (token == JavaTokenType.RBRACE) {
            break;
        }
        if (token == JavaTokenType.SEMICOLON) {
            if (invalid != null) {
                invalid.error(JavaErrorMessages.message("expected.module.statement"));
                invalid = null;
            }
            builder.advanceLexer();
            continue;
        }
        PsiBuilder.Marker statement = parseStatement(builder);
        if (statement == null) {
            if (invalid == null)
                invalid = builder.mark();
            builder.advanceLexer();
        } else if (invalid != null) {
            invalid.errorBefore(JavaErrorMessages.message("expected.module.statement"), statement);
            invalid = null;
        }
    }
    if (invalid != null) {
        invalid.error(JavaErrorMessages.message("expected.module.statement"));
    }
    if (!expect(builder, JavaTokenType.RBRACE) && invalid == null) {
        error(builder, JavaErrorMessages.message("expected.rbrace"));
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 18 with PsiBuilder

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

the class ModuleParser method parseName.

private static PsiBuilder.Marker parseName(PsiBuilder builder) {
    PsiBuilder.Marker nameElement = builder.mark();
    boolean empty = true;
    boolean idExpected = true;
    while (true) {
        IElementType t = builder.getTokenType();
        if (t == JavaTokenType.IDENTIFIER) {
            if (!idExpected)
                error(builder, JavaErrorMessages.message("expected.dot"));
            idExpected = false;
        } else if (t == JavaTokenType.DOT) {
            if (idExpected)
                error(builder, JavaErrorMessages.message("expected.identifier"));
            idExpected = true;
        } else
            break;
        builder.advanceLexer();
        empty = false;
    }
    if (!empty) {
        if (idExpected)
            error(builder, JavaErrorMessages.message("expected.identifier"));
        nameElement.done(JavaElementType.MODULE_REFERENCE);
        return nameElement;
    } else {
        nameElement.drop();
        return null;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 19 with PsiBuilder

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

the class ReferenceParser method parseTypeInfo.

@Nullable
private TypeInfo parseTypeInfo(PsiBuilder builder, int flags, boolean badWildcard) {
    if (builder.getTokenType() == null)
        return null;
    final TypeInfo typeInfo = new TypeInfo();
    PsiBuilder.Marker type = builder.mark();
    PsiBuilder.Marker anno = myParser.getDeclarationParser().parseAnnotations(builder);
    final IElementType tokenType = builder.getTokenType();
    if (expect(builder, ElementType.PRIMITIVE_TYPE_BIT_SET)) {
        typeInfo.isPrimitive = true;
    } else if ((isSet(flags, WILDCARD) || badWildcard) && (tokenType == JavaTokenType.QUEST || isKeywordAny(builder))) {
        if (tokenType == JavaTokenType.QUEST) {
            builder.advanceLexer();
        } else {
            dummy(builder);
        }
        completeWildcardType(builder, isSet(flags, WILDCARD), type);
        typeInfo.marker = type;
        return typeInfo;
    } else if (tokenType == JavaTokenType.IDENTIFIER) {
        parseJavaCodeReference(builder, isSet(flags, EAT_LAST_DOT), true, false, false, false, isSet(flags, DIAMONDS), typeInfo);
    } else if (isSet(flags, DIAMONDS) && tokenType == JavaTokenType.GT) {
        if (anno == null) {
            emptyElement(builder, JavaElementType.DIAMOND_TYPE);
        } else {
            error(builder, JavaErrorMessages.message("expected.identifier"));
            typeInfo.hasErrors = true;
        }
        type.done(JavaElementType.TYPE);
        typeInfo.marker = type;
        return typeInfo;
    } else {
        type.drop();
        if (anno != null && isSet(flags, INCOMPLETE_ANNO)) {
            error(builder, JavaErrorMessages.message("expected.type"));
            typeInfo.hasErrors = true;
            return typeInfo;
        }
        return null;
    }
    while (true) {
        type.done(JavaElementType.TYPE);
        myParser.getDeclarationParser().parseAnnotations(builder);
        final PsiBuilder.Marker bracket = builder.mark();
        if (!expect(builder, JavaTokenType.LBRACKET)) {
            bracket.drop();
            break;
        }
        if (!expect(builder, JavaTokenType.RBRACKET)) {
            bracket.rollbackTo();
            break;
        }
        bracket.drop();
        typeInfo.isArray = true;
        type = type.precede();
    }
    if (isSet(flags, ELLIPSIS) && builder.getTokenType() == JavaTokenType.ELLIPSIS) {
        type = type.precede();
        builder.advanceLexer();
        type.done(JavaElementType.TYPE);
        typeInfo.isVarArg = true;
    }
    typeInfo.marker = type;
    return typeInfo;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with PsiBuilder

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

the class ExpressionParser method parseNew.

@NotNull
private PsiBuilder.Marker parseNew(PsiBuilder builder, @Nullable PsiBuilder.Marker start) {
    PsiBuilder.Marker newExpr = (start != null ? start.precede() : builder.mark());
    builder.advanceLexer();
    myParser.getReferenceParser().parseReferenceParameterList(builder, false, true);
    PsiBuilder.Marker refOrType;
    PsiBuilder.Marker anno = myParser.getDeclarationParser().parseAnnotations(builder);
    IElementType tokenType = builder.getTokenType();
    if (tokenType == JavaTokenType.IDENTIFIER) {
        rollbackTo(anno);
        refOrType = myParser.getReferenceParser().parseJavaCodeReference(builder, true, true, true, true);
        if (refOrType == null) {
            error(builder, JavaErrorMessages.message("expected.identifier"));
            newExpr.done(JavaElementType.NEW_EXPRESSION);
            return newExpr;
        }
    } else if (ElementType.PRIMITIVE_TYPE_BIT_SET.contains(tokenType)) {
        refOrType = null;
        builder.advanceLexer();
    } else {
        error(builder, JavaErrorMessages.message("expected.identifier"));
        newExpr.done(JavaElementType.NEW_EXPRESSION);
        return newExpr;
    }
    if (refOrType != null && builder.getTokenType() == JavaTokenType.LPARENTH) {
        parseArgumentList(builder);
        if (builder.getTokenType() == JavaTokenType.LBRACE) {
            final PsiBuilder.Marker classElement = refOrType.precede();
            myParser.getDeclarationParser().parseClassBodyWithBraces(builder, false, false);
            classElement.done(JavaElementType.ANONYMOUS_CLASS);
        }
        newExpr.done(JavaElementType.NEW_EXPRESSION);
        return newExpr;
    }
    anno = myParser.getDeclarationParser().parseAnnotations(builder);
    if (builder.getTokenType() != JavaTokenType.LBRACKET) {
        rollbackTo(anno);
        error(builder, JavaErrorMessages.message(refOrType == null ? "expected.lbracket" : "expected.lparen.or.lbracket"));
        newExpr.done(JavaElementType.NEW_EXPRESSION);
        return newExpr;
    }
    int bracketCount = 0;
    int dimCount = 0;
    while (true) {
        anno = myParser.getDeclarationParser().parseAnnotations(builder);
        if (builder.getTokenType() != JavaTokenType.LBRACKET) {
            rollbackTo(anno);
            break;
        }
        builder.advanceLexer();
        if (bracketCount == dimCount) {
            final PsiBuilder.Marker dimExpr = parse(builder);
            if (dimExpr != null) {
                dimCount++;
            }
        }
        bracketCount++;
        if (!expectOrError(builder, JavaTokenType.RBRACKET, "expected.rbracket")) {
            newExpr.done(JavaElementType.NEW_EXPRESSION);
            return newExpr;
        }
    }
    if (dimCount == 0) {
        if (builder.getTokenType() == JavaTokenType.LBRACE) {
            parseArrayInitializer(builder);
        } else {
            error(builder, JavaErrorMessages.message("expected.array.initializer"));
        }
    }
    newExpr.done(JavaElementType.NEW_EXPRESSION);
    return newExpr;
}
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