Search in sources :

Example 66 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project idea-handlebars by dmarcotte.

the class HbParsing method parseStatement.

/**
 * statement
 * : block
 * | mustache (HB_CUSTOMIZATION we check `block` before `mustache` because our custom "{{else" gets incorrectly parsed as a broken
 *             mustache if we parse this first)
 * | rawBlock
 * | partial
 * | ESCAPE_CHAR (HB_CUSTOMIZATION the official Handlebars lexer just throws out the escape char;
 *                it's convenient for us to keep it so that we can highlight it)
 * | CONTENT
 * | COMMENT
 * ;
 */
private boolean parseStatement(PsiBuilder builder) {
    IElementType tokenType = builder.getTokenType();
    /**
     * block
     * : openBlock program inverseAndChain? closeBlock
     * | openInverse program inverseAndProgram? closeBlock
     */
    {
        if (builder.getTokenType() == OPEN_INVERSE) {
            if (builder.lookAhead(1) == CLOSE) {
                // this is actually a `{{^}}` simple inverse.  Bail out.  It gets parsed outside of `statement`
                return false;
            }
            PsiBuilder.Marker blockMarker = builder.mark();
            if (parseOpenInverse(builder)) {
                parseProgram(builder);
                parseInverseAndProgram(builder);
                parseCloseBlock(builder);
                blockMarker.done(HbTokenTypes.BLOCK_WRAPPER);
            } else {
                return false;
            }
            return true;
        }
        if (tokenType == OPEN_BLOCK) {
            PsiBuilder.Marker blockMarker = builder.mark();
            if (parseOpenBlock(builder)) {
                parseProgram(builder);
                parseInverseChain(builder);
                parseCloseBlock(builder);
                blockMarker.done(HbTokenTypes.BLOCK_WRAPPER);
            } else {
                return false;
            }
            return true;
        }
    }
    /**
     * mustache
     * : OPEN sexpr CLOSE
     * | OPEN_UNESCAPED sexpr CLOSE_UNESCAPED
     * ;
     */
    {
        if (tokenType == OPEN) {
            if (builder.lookAhead(1) == ELSE) {
                // this is actually an `{{else` expression, not a mustache.
                return false;
            }
            parseMustache(builder, OPEN, CLOSE);
            return true;
        }
        if (tokenType == OPEN_UNESCAPED) {
            parseMustache(builder, OPEN_UNESCAPED, CLOSE_UNESCAPED);
            return true;
        }
    }
    /**
     * rawBlock
     * : openRawBlock CONTENT endRawBlock
     */
    if (tokenType == OPEN_RAW_BLOCK) {
        PsiBuilder.Marker blockMarker = builder.mark();
        if (parseOpenRawBlock(builder)) {
            if (builder.getTokenType() == CONTENT) {
                // eat non-HB content
                builder.advanceLexer();
            }
            parseCloseRawBlock(builder);
            blockMarker.done(HbTokenTypes.BLOCK_WRAPPER);
        } else {
            return false;
        }
        return true;
    }
    if (tokenType == OPEN_PARTIAL) {
        parsePartial(builder);
        return true;
    }
    if (tokenType == ESCAPE_CHAR) {
        // ignore the escape character
        builder.advanceLexer();
        return true;
    }
    if (tokenType == CONTENT) {
        // eat non-HB content
        builder.advanceLexer();
        return true;
    }
    if (tokenType == COMMENT) {
        parseLeafToken(builder, COMMENT);
        return true;
    }
    // HB_CUSTOMIZATION: we lex UNCLOSED_COMMENT sections specially so that we can coherently mark them as errors
    if (tokenType == UNCLOSED_COMMENT) {
        PsiBuilder.Marker unclosedCommentMarker = builder.mark();
        parseLeafToken(builder, UNCLOSED_COMMENT);
        unclosedCommentMarker.error(HbBundle.message("hb.parsing.comment.unclosed"));
        return true;
    }
    return false;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 67 with PsiBuilder

use of com.intellij.lang.PsiBuilder in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoParserUtil method keyOrValueExpression.

public static boolean keyOrValueExpression(@NotNull PsiBuilder builder_, int level) {
    PsiBuilder.Marker m = enter_section_(builder_);
    boolean r = GoParser.Expression(builder_, level + 1, -1);
    if (!r)
        r = GoParser.LiteralValue(builder_, level + 1);
    IElementType type = r && builder_.getTokenType() == GoTypes.COLON ? GoTypes.KEY : GoTypes.VALUE;
    exit_section_(builder_, m, type, r);
    return r;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 68 with PsiBuilder

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

the class RegExpParser method parseGroup.

/**
   * GROUP  ::= "(" PATTERN ")" | TERM
   * TERM   ::= "." | "$" | "^" | CHAR | CLASS | BACKREF
   */
@Nullable
private PsiBuilder.Marker parseGroup(PsiBuilder builder) {
    final IElementType type = builder.getTokenType();
    final PsiBuilder.Marker marker = builder.mark();
    if (RegExpTT.GROUPS.contains(type)) {
        builder.advanceLexer();
        parseGroupEnd(builder);
        marker.done(RegExpElementTypes.GROUP);
    } else if (type == RegExpTT.SET_OPTIONS) {
        builder.advanceLexer();
        if (builder.getTokenType() == RegExpTT.OPTIONS_ON) {
            final PsiBuilder.Marker o = builder.mark();
            builder.advanceLexer();
            o.done(RegExpElementTypes.OPTIONS);
        }
        if (builder.getTokenType() == RegExpTT.OPTIONS_OFF) {
            final PsiBuilder.Marker o = builder.mark();
            builder.advanceLexer();
            o.done(RegExpElementTypes.OPTIONS);
        }
        if (builder.getTokenType() == RegExpTT.COLON) {
            builder.advanceLexer();
            parseGroupEnd(builder);
            marker.done(RegExpElementTypes.GROUP);
        } else {
            checkMatches(builder, RegExpTT.GROUP_END, "Unclosed options group");
            marker.done(RegExpElementTypes.SET_OPTIONS);
        }
    } else if (RegExpTT.CHARACTERS.contains(type) || type == RegExpTT.NAMED_CHARACTER) {
        marker.drop();
        parseCharacter(builder);
    } else if (RegExpTT.BOUNDARIES.contains(type)) {
        builder.advanceLexer();
        marker.done(RegExpElementTypes.BOUNDARY);
    } else if (type == RegExpTT.BACKREF) {
        builder.advanceLexer();
        marker.done(RegExpElementTypes.BACKREF);
    } else if (type == RegExpTT.PYTHON_NAMED_GROUP || type == RegExpTT.RUBY_NAMED_GROUP || type == RegExpTT.RUBY_QUOTED_NAMED_GROUP) {
        builder.advanceLexer();
        checkMatches(builder, RegExpTT.NAME, "Group name expected");
        checkMatches(builder, type == RegExpTT.RUBY_QUOTED_NAMED_GROUP ? RegExpTT.QUOTE : RegExpTT.GT, "Unclosed group name");
        parseGroupEnd(builder);
        marker.done(RegExpElementTypes.GROUP);
    } else if (type == RegExpTT.PYTHON_NAMED_GROUP_REF) {
        parseNamedGroupRef(builder, marker, RegExpTT.GROUP_END);
    } else if (type == RegExpTT.RUBY_NAMED_GROUP_REF || type == RegExpTT.RUBY_NAMED_GROUP_CALL) {
        parseNamedGroupRef(builder, marker, RegExpTT.GT);
    } else if (type == RegExpTT.RUBY_QUOTED_NAMED_GROUP_REF || type == RegExpTT.RUBY_QUOTED_NAMED_GROUP_CALL) {
        parseNamedGroupRef(builder, marker, RegExpTT.QUOTE);
    } else if (type == RegExpTT.PYTHON_COND_REF) {
        builder.advanceLexer();
        if (builder.getTokenType() == RegExpTT.NAME || builder.getTokenType() == RegExpTT.NUMBER) {
            builder.advanceLexer();
        } else {
            builder.error("Group name or number expected");
        }
        checkMatches(builder, RegExpTT.GROUP_END, "Unclosed group reference");
        if (!parseBranch(builder)) {
            patternExpected(builder);
        } else {
            if (builder.getTokenType() == RegExpTT.UNION) {
                builder.advanceLexer();
                if (!parseBranch(builder)) {
                    patternExpected(builder);
                }
            }
            checkMatches(builder, RegExpTT.GROUP_END, "Unclosed group");
        }
        marker.done(RegExpElementTypes.PY_COND_REF);
    } else if (type == RegExpTT.PROPERTY) {
        marker.drop();
        parseProperty(builder);
    } else if (type == RegExpTT.DOT || type == RegExpTT.CHAR_CLASS) {
        builder.advanceLexer();
        marker.done(RegExpElementTypes.SIMPLE_CLASS);
    } else if (type == RegExpTT.CLASS_BEGIN) {
        marker.drop();
        return parseClass(builder);
    } else {
        marker.drop();
        return null;
    }
    return marker;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 69 with PsiBuilder

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

the class CfscriptParser method parseImport.

private static void parseImport(PsiBuilder myBuilder) {
    if (myBuilder.getTokenType() != IMPORT_KEYWORD) {
        return;
    }
    PsiBuilder.Marker marker = myBuilder.mark();
    myBuilder.advanceLexer();
    IElementType tokenType = myBuilder.getTokenType();
    if (tokenType == CfmlTokenTypes.SINGLE_QUOTE || tokenType == CfmlTokenTypes.DOUBLE_QUOTE) {
        (new CfmlExpressionParser(myBuilder)).parseString();
    } else if (tokenType == IDENTIFIER) {
        (new CfmlExpressionParser(myBuilder)).parseComponentReference();
    }
    marker.done(CfmlElementTypes.IMPORTEXPRESSION);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 70 with PsiBuilder

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

the class GherkinParser method parseStep.

private static void parseStep(PsiBuilder builder) {
    final PsiBuilder.Marker marker = builder.mark();
    builder.advanceLexer();
    int prevTokenEnd = -1;
    while (builder.getTokenType() == GherkinTokenTypes.TEXT || builder.getTokenType() == GherkinTokenTypes.STEP_PARAMETER_BRACE || builder.getTokenType() == GherkinTokenTypes.STEP_PARAMETER_TEXT) {
        String tokenText = builder.getTokenText();
        if (hadLineBreakBefore(builder, prevTokenEnd)) {
            break;
        }
        prevTokenEnd = builder.getCurrentOffset() + getTokenLength(tokenText);
        if (!parseStepParameter(builder)) {
            builder.advanceLexer();
        }
    }
    final IElementType tokenTypeAfterName = builder.getTokenType();
    if (tokenTypeAfterName == GherkinTokenTypes.PIPE) {
        parseTable(builder);
    } else if (tokenTypeAfterName == GherkinTokenTypes.PYSTRING) {
        parsePystring(builder);
    }
    marker.done(GherkinElementTypes.STEP);
}
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