Search in sources :

Example 21 with LighterASTNode

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

the class TemplateToolkitParserUtil method parsePerlCode.

/**
 * Collapses perl code for lazy parsing
 *
 * @param b builder
 * @param l level
 * @return result of end parsing.
 */
public static boolean parsePerlCode(PsiBuilder b, int l, PsiBuilder.Marker outerMarker, IElementType perlTokenType, IElementType blockTokenType) {
    PsiBuilder.Marker perlMarker = b.mark();
    while (!b.eof() && !isEndTagAhead(b, l)) {
        b.advanceLexer();
    }
    boolean recoverBlock = true;
    if (isEndTagAhead(b, l)) {
        perlMarker.collapse(perlTokenType);
        perlMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
    } else {
        perlMarker.drop();
    }
    if (TemplateToolkitParser.element(b, l)) {
        LighterASTNode latestDoneMarker = b.getLatestDoneMarker();
        if (latestDoneMarker != null && latestDoneMarker.getTokenType() == END_DIRECTIVE) {
            outerMarker.done(blockTokenType);
            recoverBlock = false;
        }
    }
    if (recoverBlock) {
        while (!b.eof() || b.getTokenType() == TT2_OUTLINE_TAG || b.getTokenType() == TT2_OPEN_TAG) {
            b.advanceLexer();
        }
        outerMarker.done(blockTokenType);
        outerMarker.setCustomEdgeTokenBinders(WhitespacesBinders.GREEDY_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
        outerMarker.precede().error(PerlBundle.message("ttk2.error.unclosed.perl.block"));
    }
    return true;
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) PsiBuilder(com.intellij.lang.PsiBuilder)

Example 22 with LighterASTNode

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

the class TemplateToolkitParserUtil method parseBlockContent.

/**
 * Parses block content
 *
 * @param b builder
 * @param l level
 * @return result of end parsing.
 */
public static boolean parseBlockContent(PsiBuilder b, int l, PsiBuilder.Marker outerMarker, IElementType blockTokenType) {
    boolean r = false;
    while (!b.eof() && TemplateToolkitParser.element(b, l)) {
        LighterASTNode latestDoneMarker = b.getLatestDoneMarker();
        if (latestDoneMarker != null && latestDoneMarker.getTokenType() == END_DIRECTIVE) {
            r = true;
            break;
        }
    }
    outerMarker.done(blockTokenType);
    if (// this can happen on incomplete block, missing end
    !r) {
        outerMarker.setCustomEdgeTokenBinders(WhitespacesBinders.DEFAULT_LEFT_BINDER, WhitespacesBinders.GREEDY_RIGHT_BINDER);
        outerMarker.precede().error(PerlBundle.message("ttk2.error.unclosed.block.directive"));
    }
    return r;
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode)

Example 23 with LighterASTNode

use of com.intellij.lang.LighterASTNode 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 24 with LighterASTNode

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

the class LightTreeUtil method getChildrenOfType.

@NotNull
public static List<LighterASTNode> getChildrenOfType(@NotNull LighterAST tree, @NotNull LighterASTNode node, @NotNull TokenSet types) {
    List<LighterASTNode> children = tree.getChildren(node);
    List<LighterASTNode> result = null;
    for (int i = 0, size = children.size(); i < size; ++i) {
        LighterASTNode child = children.get(i);
        if (types.contains(child.getTokenType())) {
            if (result == null)
                result = new SmartList<>();
            result.add(child);
        }
    }
    return result != null ? result : Collections.<LighterASTNode>emptyList();
}
Also used : LighterASTNode(com.intellij.lang.LighterASTNode) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with LighterASTNode

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

the class ContractInferenceInterpreter method visitLogicalOperation.

private List<MethodContract> visitLogicalOperation(List<LighterASTNode> operands, boolean conjunction, List<ValueConstraint[]> states) {
    ValueConstraint breakValue = conjunction ? FALSE_VALUE : TRUE_VALUE;
    List<MethodContract> finalStates = ContainerUtil.newArrayList();
    for (LighterASTNode operand : operands) {
        List<PreContract> opResults = visitExpression(states, operand);
        finalStates.addAll(ContainerUtil.filter(knownContracts(opResults), contract -> contract.returnValue == breakValue));
        states = antecedentsReturning(opResults, negateConstraint(breakValue));
    }
    finalStates.addAll(toContracts(states, negateConstraint(breakValue)));
    return finalStates;
}
Also used : Arrays(java.util.Arrays) JavaTokenType(com.intellij.psi.JavaTokenType) IElementType(com.intellij.psi.tree.IElementType) Collections.emptyList(java.util.Collections.emptyList) JavaElementType(com.intellij.psi.impl.source.tree.JavaElementType) ContainerUtil(com.intellij.util.containers.ContainerUtil) JavaLightTreeUtil(com.intellij.psi.impl.source.JavaLightTreeUtil) JavaLightTreeUtil.getExpressionChildren(com.intellij.psi.impl.source.JavaLightTreeUtil.getExpressionChildren) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ElementType(com.intellij.psi.impl.source.tree.ElementType) JavaLightTreeUtil.findExpressionChild(com.intellij.psi.impl.source.JavaLightTreeUtil.findExpressionChild) ValueConstraint(com.intellij.codeInspection.dataFlow.MethodContract.ValueConstraint) LightTreeUtil.firstChildOfType(com.intellij.psi.impl.source.tree.LightTreeUtil.firstChildOfType) LighterAST(com.intellij.lang.LighterAST) LighterASTNode(com.intellij.lang.LighterASTNode) LightTreeUtil.getChildrenOfType(com.intellij.psi.impl.source.tree.LightTreeUtil.getChildrenOfType) NotNull(org.jetbrains.annotations.NotNull) LighterASTNode(com.intellij.lang.LighterASTNode) ValueConstraint(com.intellij.codeInspection.dataFlow.MethodContract.ValueConstraint)

Aggregations

LighterASTNode (com.intellij.lang.LighterASTNode)48 IElementType (com.intellij.psi.tree.IElementType)14 Nullable (org.jetbrains.annotations.Nullable)12 NotNull (org.jetbrains.annotations.NotNull)11 PsiBuilder (com.intellij.lang.PsiBuilder)6 LighterAST (com.intellij.lang.LighterAST)4 ValueConstraint (com.intellij.codeInspection.dataFlow.MethodContract.ValueConstraint)3 TypeInfo (com.intellij.psi.impl.cache.TypeInfo)3 SmartList (com.intellij.util.SmartList)3 JavaTokenType (com.intellij.psi.JavaTokenType)2 JavaLightTreeUtil (com.intellij.psi.impl.source.JavaLightTreeUtil)2 JavaLightTreeUtil.findExpressionChild (com.intellij.psi.impl.source.JavaLightTreeUtil.findExpressionChild)2 JavaLightTreeUtil.getExpressionChildren (com.intellij.psi.impl.source.JavaLightTreeUtil.getExpressionChildren)2 ElementType (com.intellij.psi.impl.source.tree.ElementType)2 JavaElementType (com.intellij.psi.impl.source.tree.JavaElementType)2 LightTreeUtil.firstChildOfType (com.intellij.psi.impl.source.tree.LightTreeUtil.firstChildOfType)2 LightTreeUtil.getChildrenOfType (com.intellij.psi.impl.source.tree.LightTreeUtil.getChildrenOfType)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 DuplicatesProfile (com.intellij.dupLocator.DuplicatesProfile)1 DuplocatorState (com.intellij.dupLocator.DuplocatorState)1