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

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

Introduction

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

Prototype

public <T extends ParserRuleContext> List<T> getRuleContexts(Class<? extends T> ctxType) 

Source Link

Usage

From source file:com.satisfyingstructures.J2S.J2SConverter.java

License:Open Source License

private void convertVariableDeclaration(ParserRuleContext declarationCtx) {
    // Read comments in exitVariableDeclaratorId just above!

    int declarationRuleIndex = declarationCtx.getRuleIndex();
    Class<? extends ParserRuleContext> modifierContextClass = Java8Parser.VariableModifierContext.class;
    Constness constness = Constness.unknown;
    boolean isOptional = false;
    boolean hasDeclaratorList = false;
    boolean enhancedFor = false;

    switch (declarationRuleIndex) {
    case Java8Parser.RULE_constantDeclaration:
        modifierContextClass = Java8Parser.ConstantModifierContext.class;
        hasDeclaratorList = true;/*from www  .j  av a  2s  .co m*/
        constness = Constness.explicit;
        break;
    case Java8Parser.RULE_fieldDeclaration:
        modifierContextClass = Java8Parser.FieldModifierContext.class;
        hasDeclaratorList = true;
        break;
    case Java8Parser.RULE_localVariableDeclaration:
        hasDeclaratorList = true;
        break;
    case Java8Parser.RULE_resource:
        constness = Constness.explicit;
        break;
    case Java8Parser.RULE_formalParameter:
    case Java8Parser.RULE_lastFormalParameter:
        constness = Constness.implicit;
        break;
    case Java8Parser.RULE_catchFormalParameter:
        constness = Constness.explicit;
        break;
    case Java8Parser.RULE_enhancedForStatement:
    case Java8Parser.RULE_enhancedForStatementNoShortIf:
        enhancedFor = true;
        constness = Constness.implicit;
        break;
    default:
        return; // not our expected parameter type
    }

    // Look for and remove 'final', '@NonNull' and '@Nullable' in modifiers
    for (ParserRuleContext modifierCtx : declarationCtx.getRuleContexts(modifierContextClass)) {
        TerminalNode tn = modifierCtx.getChild(TerminalNode.class, 0);
        if (null == tn) {
            String annotationText = modifierCtx.getText();
            switch (annotationText) {
            // some spelling variations here...
            case "@Nonnull": // javax.annotation.NotNull
            case "@NonNull": // android.support.annotation.NonNull
                             // edu.umd.cs.findbugs.annotations.NonNull
            case "@NotNull": // org.jetbrains.annotations.NotNull
                isOptional = false;
                break;
            case "@Nullable":
                isOptional = true;
                break;
            default:
                continue;
            }
        } else {
            Token token = tn.getSymbol();
            mapModifierToken(token);
            switch (token.getType()) {
            case Java8Parser.FINAL:
                if (constness == Constness.unknown)
                    constness = Constness.explicit;
                break;
            default:
                continue;
            }
        }
        rewriter.deleteAndAdjustWhitespace(modifierCtx);
    }

    // Move trailing dimensions to wrap the type. First any dimensions binding to the declarator id and
    // then any dimensions binding to the right of the type.
    // a) start by finding the type context that will be wrapped.
    Java8Parser.UnannTypeContext unannTypeCtx = null;
    Java8Parser.UnannReferenceTypeContext unannReferenceTypeCtx = null;
    Java8Parser.UnannArrayTypeContext unannArrayTypeCtx = null;
    Java8Parser.DimsContext outerDimsCtx = null;
    ParserRuleContext typeCtx = null;
    if (declarationRuleIndex == Java8Parser.RULE_catchFormalParameter)
        typeCtx = declarationCtx.getChild(Java8Parser.CatchTypeContext.class, 0);
    else
        typeCtx = unannTypeCtx = declarationCtx.getChild(Java8Parser.UnannTypeContext.class, 0);
    if (null != unannTypeCtx)
        if (null != (unannReferenceTypeCtx = unannTypeCtx.unannReferenceType())
                && null != (unannArrayTypeCtx = unannReferenceTypeCtx.unannArrayType())) {
            typeCtx = unannArrayTypeCtx.getChild(ParserRuleContext.class, 0);
            outerDimsCtx = unannArrayTypeCtx.dims();
        }
    // b) process dimensions attached to declarator ids
    // ...process inside blocks below
    // c) process dimensions attached to type
    // ...process inside blocks below

    // Now insert unannTypeText at end of each variableDeclaratorId if necessary:
    ParserRuleContext ctx, varInitCtx;
    Java8Parser.VariableDeclaratorIdContext varIdCtx;
    Java8Parser.DimsContext innerDimsCtx = null;
    String unannTypeText;
    if (hasDeclaratorList) {
        // Iterate over the list of declarator-initialiser pairs backwards so that variable lists without
        // intialisers and with explicit enough types, just pick up the type from the end of the list, i.e.
        // so that we generate var a, b, c: Int, and not var a: Int, b: Int, c: Int.
        ListIterator<Java8Parser.VariableDeclaratorContext> iter;
        List<Java8Parser.VariableDeclaratorContext> list;
        String followingUnannTypeText = null;
        boolean followingVarHasExplicitType = false;
        boolean hasInitialiser;

        ctx = declarationCtx.getChild(Java8Parser.VariableDeclaratorListContext.class, 0);
        list = ctx.getRuleContexts(Java8Parser.VariableDeclaratorContext.class);
        iter = list.listIterator(list.size());
        unannTypeText = null;

        while (iter.hasPrevious()) {
            ctx = iter.previous();
            varIdCtx = ctx.getRuleContext(Java8Parser.VariableDeclaratorIdContext.class, 0);

            // Wrap the inner type string with array dimensions if we have them. Have to do this for each variable,
            // because they can have different dimensionality.
            followingUnannTypeText = unannTypeText;
            unannTypeText = rewriter.getText(typeCtx);
            if (null != (innerDimsCtx = varIdCtx.dims())) {
                unannTypeText = wrapTypeStringWithDims(unannTypeText, innerDimsCtx);
                rewriter.delete(innerDimsCtx);
            }
            if (null != outerDimsCtx)
                unannTypeText = wrapTypeStringWithDims(unannTypeText, outerDimsCtx);

            varInitCtx = ctx.getRuleContext(Java8Parser.VariableInitializerContext.class, 0);
            if (null != varInitCtx)
                varInitCtx = varInitCtx.getChild(ParserRuleContext.class, 0); // expression or arrayInitializer
            hasInitialiser = null != varInitCtx;

            // In the basic case, we have to qualify the variable with its type, but we can omit this if it has an
            // initialiser that completely implies the type, or it has no initialiser and has the same type as the
            // a contiguously following variable with explicit type.
            if (hasInitialiser
                    ? !isVariableTypeCompletelyImpliedByInitializer(unannTypeCtx, varInitCtx,
                            /*inEnhancedFor:*/false)
                    : !followingVarHasExplicitType || null == followingUnannTypeText
                            || !unannTypeText.equals(followingUnannTypeText)) {
                rewriter.insertAfter(varIdCtx, ": " + unannTypeText + (isOptional ? "?" : ""));
                followingVarHasExplicitType = !hasInitialiser;
            }
        }
    } else {
        varIdCtx = declarationCtx.getRuleContext(Java8Parser.VariableDeclaratorIdContext.class, 0);
        unannTypeText = rewriter.getText(typeCtx);
        if (null != (innerDimsCtx = varIdCtx.dims())) {
            unannTypeText = wrapTypeStringWithDims(unannTypeText, innerDimsCtx);
            rewriter.delete(innerDimsCtx);
        }
        if (null != outerDimsCtx)
            unannTypeText = wrapTypeStringWithDims(unannTypeText, outerDimsCtx);

        varInitCtx = null;
        if (declarationRuleIndex == Java8Parser.RULE_resource
                || declarationRuleIndex == Java8Parser.RULE_enhancedForStatement
                || declarationRuleIndex == Java8Parser.RULE_enhancedForStatementNoShortIf)
            varInitCtx = declarationCtx.getRuleContext(Java8Parser.ExpressionContext.class, 0);

        if (declarationRuleIndex == Java8Parser.RULE_catchFormalParameter)
            rewriter.insertAfter(varIdCtx, " as " + unannTypeText);
        else if (!isVariableTypeCompletelyImpliedByInitializer(unannTypeCtx, varInitCtx, enhancedFor))
            rewriter.insertAfter(varIdCtx, ": " + unannTypeText + (isOptional ? "?" : ""));

        // In parameter lists, add an anonymizing argument label, as argument labels not used in java method/function calls
        if (declarationRuleIndex == Java8Parser.RULE_formalParameter
                || declarationRuleIndex == Java8Parser.RULE_lastFormalParameter)
            rewriter.insertBefore(varIdCtx, "_ ");
    }

    // Finally replace the complete type context with let/var/-
    // in an enhancedForStatement, the loop var is implicitly const, but can be made variable with var if it is
    // to be modified inside the loop; we could check for this, but its a rare scenario, and a lot of work, so no.
    if (null != unannTypeCtx)
        typeCtx = unannTypeCtx;
    switch (constness) {
    case explicit:
        rewriter.replace(typeCtx, "let");
        break;
    case implicit:
        rewriter.deleteAndAdjustWhitespace(typeCtx);
        break;
    case variable:
        rewriter.replace(typeCtx, "var");
        break;
    // if still unknown, then assume variable...
    default:
        rewriter.replace(typeCtx, "var");
        break;
    }
}

From source file:org.onos.yangtools.yang.parser.impl.ParserListenerUtils.java

License:Open Source License

public static String getArgumentString(final org.antlr.v4.runtime.ParserRuleContext ctx) {
    List<StringContext> potentialValues = ctx.getRuleContexts(StringContext.class);
    checkState(!potentialValues.isEmpty());
    return ParserListenerUtils.stringFromStringContext(potentialValues.get(0));
}

From source file:org.onos.yangtools.yang.parser.impl.ParserListenerUtils.java

License:Open Source License

public static <T extends ParserRuleContext> Optional<T> getFirstContext(final ParserRuleContext context,
        final Class<T> contextType) {
    List<T> potential = context.getRuleContexts(contextType);
    if (potential.isEmpty()) {
        return Optional.absent();
    }//from w  w  w  .  j  a  v a  2  s .c  o m
    return Optional.of(potential.get(0));
}