Example usage for org.antlr.v4.runtime ParserRuleContext getParent

List of usage examples for org.antlr.v4.runtime ParserRuleContext getParent

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext getParent.

Prototype

@Override
    
    public ParserRuleContext getParent() 

Source Link

Usage

From source file:edu.clemson.resolve.misc.Utils.java

License:BSD License

/**
 * Return first ancestor node up the chain towards the root that is in {@code clazzes}. Search includes the
 * current node.// ww  w .j a va  2 s.  com
 *
 * @return the found parent, {@code null} if not found.
 */
@Nullable
public static ParserRuleContext getFirstAncestorOfType(@Nullable ParserRuleContext t,
        @NotNull List<Class<?>> clazzes) {
    while (t != null) {
        for (Class<?> clazz : clazzes) {
            if (t.getClass() == clazz) {
                return t;
            }
        }
        t = t.getParent();
    }
    return null;
}

From source file:jetbrick.template.parser.AstCodeVisitor.java

License:Open Source License

private void validateInsideOfDirectiveFor(ParserRuleContext ctx, String name) {
    ParserRuleContext p = ctx.getParent();
    while (p != null) {
        if (p instanceof Directive_forContext) {
            return;
        }/* ww  w .  j  a v a2 s. co  m*/
        if (p instanceof Directive_elseContext) {
            p = p.getParent();
        }
        p = p.getParent();
    }
    throw new SyntaxException(Errors.DIRECTIVE_OUTSIDE_OF_FOR, name).set(pos(ctx));
}

From source file:jetbrick.template.parser.JetTemplateCodeVisitor.java

License:Open Source License

private void assert_inside_of_for_directive(ParserRuleContext ctx, String name) {
    // ? forStack ??
    ParserRuleContext p = ctx.getParent();
    while (p != null) {
        if (p instanceof For_directiveContext) {
            return;
        }//from   w  w  w  . jav  a2s  . co m
        if (p instanceof Else_directiveContext) {
            // ?  for-else , ?
            //  if-else , ? #if
            p = p.getParent();
        }
        p = p.getParent();
    }
    throw reportError(name + " cannot be used outside of a #for directive", ctx);
}

From source file:languageTools.analyzer.agent.AgentValidator.java

License:Open Source License

/**
 * Delegate parsing of PARLIST terminal node to KR parser and checks whether
 * terms are variables and reports errors if this is not the case.
 *
 * @param pars/*from w  w  w.ja v a2  s  . co m*/
 *            String text from PARLIST terminal.
 * @param ctx
 *            Parser context where PARLIST was found.
 * @return List of terms.
 */
public List<Term> visitVARLIST(String pars, ParserRuleContext ctx) {
    List<Term> parameters = visitPARLIST(pars, ctx);

    for (Term term : parameters) {
        if (!term.isVar()) {
            reportError(AgentError.PARAMETER_NOT_A_VARIABLE, ctx,
                    strategy.prettyPrintRuleContext(ctx.getParent().getRuleIndex()), term.toString());
        }
    }

    return parameters;
}

From source file:notaql.parser.NotaQLErrorListener.java

License:Apache License

public static void printContextStack(ParserRuleContext ctx) {
    if (ctx.getParent() != null)
        printContextStack(ctx.getParent());
    System.err.println("context: " + ctx.getText() + " (" + ctx.getClass().toString() + ")");
}

From source file:org.ballerinalang.langserver.compiler.common.LSCustomErrorStrategy.java

License:Open Source License

@Override
protected void setErrorState(Parser parser) {
    BLangParserListener listener = getListener(parser);
    // Here the type of the exception is not important.
    ParserRuleContext context = parser.getContext();
    // Note: Here we forcefully set the exception to null, in order to avoid the callable unit body being null at
    // the run time
    if (context instanceof BallerinaParser.CallableUnitBodyContext) {
        listener.unsetErrorState();/*  w  w w. j  ava  2s  .co  m*/
        return;
    } else if (context instanceof BallerinaParser.SimpleVariableReferenceContext
            && parser.getCurrentToken().getText().equals(ACTION_INVOCATION_SYMBOL)) {
        listener.unsetErrorState();
    }

    // Note: Following check added, when the context is variable definition and the type name context is hit,
    // We need to set the error for the variable definition as well.
    if (context.getParent() instanceof BallerinaParser.VariableDefinitionStatementContext) {
        listener.setErrorState();
    } else if (context instanceof BallerinaParser.ExpressionContext) {
        setContextIfConditionalStatement(context, listener);
    } else {
        setContextIfCheckedExpression(context, listener);
    }
}

From source file:org.ballerinalang.langserver.compiler.common.LSCustomErrorStrategy.java

License:Open Source License

/**
 * Check the context and identify if the particular context is a child of a connector init and set the exception.
 *
 * @param context       current parser rule context
 * @param listener      Parser Listener instance
 *///from  w w  w . j a  va  2 s .  c  o m
private void setContextIfCheckedExpression(ParserRuleContext context, BLangParserListener listener) {
    ParserRuleContext parentContext = context.getParent();
    if (parentContext != null && parentContext instanceof BallerinaParser.CheckedExpressionContext) {
        listener.setErrorState();
    } else if (parentContext instanceof BallerinaParser.VariableReferenceExpressionContext
            && parentContext.getParent() instanceof BallerinaParser.CheckedExpressionContext) {
        listener.setErrorState();
    }
}

From source file:org.ballerinalang.langserver.compiler.common.LSCustomErrorStrategy.java

License:Open Source License

/**
 * Set the context if the statement is a conditional statement such as if-else, while or catch.
 *
 * @param context       Current parser rule context
 * @param listener      Parser Listener instance
 *//*from  w ww  .j  a v a2  s  .  c o m*/
private void setContextIfConditionalStatement(ParserRuleContext context, BLangParserListener listener) {
    ParserRuleContext conditionalContext = context.getParent();
    if (conditionalContext == null) {
        return;
    }
    if (conditionalContext instanceof BallerinaParser.IfClauseContext) {
        listener.setErrorState();
    } else if (conditionalContext instanceof BallerinaParser.WhileStatementContext
            || conditionalContext instanceof BallerinaParser.TypeConversionExpressionContext) {
        listener.setErrorState();
    } else if (conditionalContext instanceof BallerinaParser.BinaryEqualExpressionContext) {
        setContextIfConditionalStatement(conditionalContext, listener);
    } else if (conditionalContext instanceof BallerinaParser.CheckedExpressionContext) {
        setContextIfCheckedExpression(context, listener);
    } else if (conditionalContext instanceof BallerinaParser.ThrowStatementContext) {
        // TODO: need to migrate this check to a top layer
        listener.setErrorState();
    }
}

From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java

License:Open Source License

@Override
protected void setContextException(Parser parser) {
    // Here the type of the exception is not important.
    InputMismatchException e = new InputMismatchException(parser);
    ParserRuleContext context = parser.getContext();
    // Note: Here we forcefully set the exception to null, in order to avoid the callable unit body being null at
    // the run time
    if (context instanceof BallerinaParser.CallableUnitBodyContext) {
        context.exception = null;/* w w w . j a  va2s.co m*/
        return;
    }
    context.exception = e;
    // Note: Following check added, when the context is variable definition and the type name context is hit,
    // We need to set the error for the variable definition as well.
    if (context.getParent() instanceof BallerinaParser.VariableDefinitionStatementContext) {
        context.getParent().exception = e;
        return;
    }

    if (context instanceof BallerinaParser.NameReferenceContext) {
        setContextIfConnectorInit(context, e);
    }
}

From source file:org.ballerinalang.langserver.completions.BallerinaCustomErrorStrategy.java

License:Open Source License

/**
 * Check the context and identify if the particular context is a child of a connector init and set the exception.
 * @param context   current parser rule context
 * @param e         exception to set/*ww  w.jav  a  2 s . c  om*/
 */
private void setContextIfConnectorInit(ParserRuleContext context, InputMismatchException e) {
    ParserRuleContext connectorInitContext = context.getParent().getParent().getParent();
    if (connectorInitContext instanceof BallerinaParser.ConnectorInitExpressionContext) {
        ParserRuleContext tempContext = context;
        while (true) {
            tempContext.exception = e;
            tempContext = tempContext.getParent();
            if (tempContext.equals(connectorInitContext)) {
                tempContext.getParent().exception = e;
                break;
            }
        }
        connectorInitContext.getParent().exception = e;
    }
}