Search in sources :

Example 56 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project Perl5-IDEA by Camelcade.

the class TemplateToolkitParserUtil method parseIfSequence.

public static boolean parseIfSequence(PsiBuilder b, int l, PsiBuilder.Marker branchMarker, IElementType branchTokenType) {
    while (!b.eof()) {
        PsiBuilder.Marker currentMarker = b.mark();
        if (TemplateToolkitParser.element(b, l)) {
            LighterASTNode latestDoneMarker = b.getLatestDoneMarker();
            if (latestDoneMarker != null) {
                if (latestDoneMarker.getTokenType() == END_DIRECTIVE) {
                    branchMarker.doneBefore(branchTokenType, currentMarker);
                    branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
                    currentMarker.drop();
                    branchMarker = null;
                    break;
                } else if (latestDoneMarker.getTokenType() == ELSIF_DIRECTIVE) {
                    branchMarker.doneBefore(branchTokenType, currentMarker);
                    branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
                    branchMarker = currentMarker.precede();
                    branchTokenType = ELSIF_BRANCH;
                } else if (latestDoneMarker.getTokenType() == ELSE_DIRECTIVE) {
                    branchMarker.doneBefore(branchTokenType, currentMarker);
                    branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
                    branchMarker = currentMarker.precede();
                    branchTokenType = ELSE_BRANCH;
                } else {
                    currentMarker.error(PerlBundle.message("ttk2.else.elsif.end.expected"));
                }
                currentMarker.drop();
            }
        } else {
            b.advanceLexer();
            currentMarker.error(PerlBundle.message("ttk2.unexpected.token"));
        }
    }
    if (branchMarker != null) {
        branchMarker.done(branchTokenType);
        branchMarker.precede().error(PerlBundle.message("ttk2.error.unclosed.block.directive"));
        branchMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
    }
    return true;
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 57 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project Perl5-IDEA by Camelcade.

the class PodParserUtil method collapseNonSpaceTo.

public static boolean collapseNonSpaceTo(PsiBuilder b, int l, IElementType targetElement) {
    IElementType tokenType = b.getTokenType();
    if (tokenType == POD_IDENTIFIER) {
        PsiBuilder.Marker m = b.mark();
        while (!b.eof() && !PodParserDefinition.ALL_WHITE_SPACES.contains(b.rawLookup(1))) {
            b.advanceLexer();
        }
        b.advanceLexer();
        m.collapse(targetElement);
        return true;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 58 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project Perl5-IDEA by Camelcade.

the class PerlParserUtil method validateAnonHashSuffix.

/**
 * Checks if anon hash has proper suffix
 *
 * @param b PerlBuilder
 * @param l parsing level
 * @return chack result.
 */
public static boolean validateAnonHashSuffix(PsiBuilder b, int l) {
    IElementType tokenType = b.getTokenType();
    if (tokenType == null || ((PerlBuilder) b).getPerlParser().getAnonHashSuffixTokens().contains(tokenType)) {
        return true;
    } else {
        PsiBuilder.Marker m = b.mark();
        boolean r = PerlParserImpl.statement_modifier(b, l);
        r = r && (b.getTokenType() != LEFT_BRACE);
        m.rollbackTo();
        return r;
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 59 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project Perl5-IDEA by Camelcade.

the class PerlParserUtil method parseBadCharacters.

/**
 * Consuming unexpected token
 *
 * @param b perlbuilder
 * @param l parsing level
 * @return true
 */
public static boolean parseBadCharacters(PsiBuilder b, @SuppressWarnings("unused") int l) {
    IElementType tokenType = b.getTokenType();
    if (tokenType == null || ((PerlBuilder) b).getPerlParser().getBadCharacterForbiddenTokens().contains(tokenType)) {
        return false;
    }
    PsiBuilder.Marker m = b.mark();
    b.advanceLexer();
    if (tokenType == TokenType.BAD_CHARACTER) {
        while (b.getTokenType() == TokenType.BAD_CHARACTER) {
            b.advanceLexer();
        }
        m.error("Unexpected tokens, plugin currently supports only ASCII identifiers");
    } else if (tokenType == RIGHT_PAREN) {
        m.error("Unopened closing parenthesis");
    } else if (tokenType == RIGHT_BRACKET) {
        m.error("Unopened closing bracket");
    } else {
        m.error("Unexpected token");
    }
    return true;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 60 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project Perl5-IDEA by Camelcade.

the class HTMLMasonParserImpl method parseStatement.

@Override
public boolean parseStatement(PsiBuilder b, int l) {
    IElementType tokenType = b.getTokenType();
    boolean r = false;
    PsiBuilder.Marker m = b.mark();
    if (tokenType == HTML_MASON_BLOCK_OPENER) {
        PsiBuilder.Marker statementMarker = b.mark();
        b.advanceLexer();
        if (PerlParserImpl.expr(b, l, -1)) {
            // parseStatement filter
            if (PerlParserUtil.consumeToken(b, HTML_MASON_EXPR_FILTER_PIPE)) {
                while ((tokenType = b.getTokenType()) == HTML_MASON_DEFAULT_ESCAPER_NAME || tokenType == HTML_MASON_ESCAPER_NAME) {
                    b.advanceLexer();
                    while (PerlParserUtil.consumeToken(b, HTML_MASON_DEFAULT_ESCAPER_NAME)) {
                        ;
                    }
                    if (!PerlParserUtil.consumeToken(b, COMMA)) {
                        break;
                    }
                }
            }
        }
        if (r = MasonParserUtil.endOrRecover(b, HTML_MASON_BLOCK_CLOSER)) {
            statementMarker.done(STATEMENT);
        }
    } else if (tokenType == HTML_MASON_CALL_OPENER) {
        PsiBuilder.Marker statementMarker = b.mark();
        b.advanceLexer();
        PerlParserImpl.expr(b, l, -1);
        if (r = MasonParserUtil.endOrRecover(b, HTML_MASON_CALL_CLOSER)) {
            statementMarker.done(HTML_MASON_CALL_STATEMENT);
        }
    } else if (tokenType == HTML_MASON_CALL_FILTERING_OPENER) {
        PsiBuilder.Marker statementMarker = b.mark();
        b.advanceLexer();
        PerlParserImpl.expr(b, l, -1);
        if (MasonParserUtil.endOrRecover(b, HTML_MASON_CALL_CLOSER_UNMATCHED)) {
            statementMarker.done(HTML_MASON_CALL_STATEMENT);
        }
        PsiBuilder.Marker blockMarker = b.mark();
        PerlParserImpl.block_content(b, l);
        if (// need recover
        b.getTokenType() != HTML_MASON_CALL_CLOSE_TAG_START) {
            while (!b.eof() && b.getTokenType() != HTML_MASON_CALL_CLOSE_TAG_START) {
                b.advanceLexer();
            }
        }
        blockMarker.done(HTML_MASON_FILTERED_BLOCK);
        blockMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
        if (b.eof()) {
            b.mark().error("Unclosed filtering block");
        } else {
            PsiBuilder.Marker tagMarker = b.mark();
            if (PerlParserUtil.consumeToken(b, HTML_MASON_CALL_CLOSE_TAG_START)) {
                string_bare(b, l);
                if (!PerlParserUtil.consumeToken(b, HTML_MASON_TAG_CLOSER)) {
                    b.mark().error("Incomplete close tag");
                }
                tagMarker.done(HTML_MASON_CALL_CLOSE_TAG);
            } else {
                tagMarker.drop();
            }
        }
        r = true;
    } else if (tokenType == HTML_MASON_ONCE_OPENER) {
        r = MasonParserUtil.parsePerlBlock(b, l, HTML_MASON_ONCE_CLOSER, HTML_MASON_ONCE_BLOCK);
    } else if (tokenType == HTML_MASON_SHARED_OPENER) {
        r = MasonParserUtil.parsePerlBlock(b, l, HTML_MASON_SHARED_CLOSER, HTML_MASON_SHARED_BLOCK);
    } else if (tokenType == HTML_MASON_CLEANUP_OPENER) {
        r = MasonParserUtil.parsePerlBlock(b, l, HTML_MASON_CLEANUP_CLOSER, HTML_MASON_CLEANUP_BLOCK);
    } else if (tokenType == HTML_MASON_INIT_OPENER) {
        r = MasonParserUtil.parsePerlBlock(b, l, HTML_MASON_INIT_CLOSER, HTML_MASON_INIT_BLOCK);
    } else if (tokenType == HTML_MASON_FILTER_OPENER) {
        r = MasonParserUtil.parsePerlBlock(b, l, HTML_MASON_FILTER_CLOSER, HTML_MASON_FILTER_BLOCK);
    } else if (tokenType == HTML_MASON_ARGS_OPENER) {
        r = parseArgsBlock(b, l);
    } else if (tokenType == HTML_MASON_FLAGS_OPENER) {
        PsiBuilder.Marker statementMarker = b.mark();
        b.advanceLexer();
        while (!b.eof() && b.getTokenType() != HTML_MASON_FLAGS_CLOSER) {
            if (!PerlParserImpl.expr(b, l, -1)) {
                break;
            }
        }
        r = MasonParserUtil.endOrRecover(b, HTML_MASON_FLAGS_CLOSER);
        statementMarker.done(HTML_MASON_FLAGS_STATEMENT);
    } else if (tokenType == HTML_MASON_ATTR_OPENER) {
        PsiBuilder.Marker statementMarker = b.mark();
        b.advanceLexer();
        while (!b.eof() && b.getTokenType() != HTML_MASON_ATTR_CLOSER) {
            if (!PerlParserImpl.expr(b, l, -1) && !parseHardNewline(b)) {
                break;
            }
        }
        r = MasonParserUtil.endOrRecover(b, HTML_MASON_ATTR_CLOSER);
        statementMarker.done(HTML_MASON_ATTR_BLOCK);
    } else if (tokenType == HTML_MASON_DOC_OPENER) {
        b.advanceLexer();
        PerlParserUtil.consumeToken(b, COMMENT_BLOCK);
        r = MasonParserUtil.endOrRecover(b, HTML_MASON_DOC_CLOSER);
    } else if (tokenType == HTML_MASON_TEXT_OPENER) {
        PsiBuilder.Marker stringMarker = b.mark();
        b.advanceLexer();
        PerlParserUtil.consumeToken(b, STRING_CONTENT);
        r = MasonParserUtil.endOrRecover(b, HTML_MASON_TEXT_CLOSER);
        stringMarker.done(HTML_MASON_TEXT_BLOCK);
    } else if (tokenType == HTML_MASON_METHOD_OPENER) {
        r = parseMasonNamedBlock(b, l, HTML_MASON_METHOD_CLOSER, HTML_MASON_METHOD_DEFINITION);
    } else if (tokenType == HTML_MASON_DEF_OPENER) {
        r = parseMasonNamedBlock(b, l, HTML_MASON_DEF_CLOSER, HTML_MASON_SUBCOMPONENT_DEFINITION);
    }
    if (r) {
        m.drop();
    } else {
        m.rollbackTo();
    }
    return r || super.parseStatement(b, l);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

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