Example usage for org.antlr.v4.runtime.tree TerminalNode getSourceInterval

List of usage examples for org.antlr.v4.runtime.tree TerminalNode getSourceInterval

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.tree TerminalNode getSourceInterval.

Prototype

Interval getSourceInterval();

Source Link

Document

Return an Interval indicating the index in the TokenStream of the first and last token associated with this subtree.

Usage

From source file:info.fulloo.trygve.parser.Pass1Listener.java

License:Open Source License

private Expression processIndexedArrayElement(final ParseTree arrayExprCtx,
        final KantParser.ExprContext sexpCtx, final TerminalNode ABELIAN_INCREMENT_OPCtx) {
    // | abelian_expr '[' expr ']' ABELIAN_INCREMENT_OP
    // | ABELIAN_INCREMENT_OP expr '[' expr ']'

    Expression retval = null;//from  w  w w.  j  av  a  2s.  co m
    final Expression indexExpr = parsingData_.popExpression();
    indexExpr.setResultIsConsumed(true);

    final Expression rawArrayBase = parsingData_.popExpression();
    final Type rawArrayBaseType = rawArrayBase.type();
    assert rawArrayBaseType instanceof ArrayType;
    final ArrayType arrayType = (ArrayType) rawArrayBaseType;
    final Type baseType = arrayType.baseType();
    final ArrayExpression arrayBase = new ArrayExpression(rawArrayBase, baseType);
    arrayBase.setResultIsConsumed(true);

    final Interval JavaIDInterval = arrayExprCtx.getSourceInterval();
    final Interval OperatorInterval = ABELIAN_INCREMENT_OPCtx.getSourceInterval();
    final UnaryopExpressionWithSideEffect.PreOrPost preOrPost = JavaIDInterval.startsAfter(OperatorInterval)
            ? UnaryopExpressionWithSideEffect.PreOrPost.Pre
            : UnaryopExpressionWithSideEffect.PreOrPost.Post;
    retval = new ArrayIndexExpressionUnaryOp(arrayBase, indexExpr, ABELIAN_INCREMENT_OPCtx.getText(), preOrPost,
            sexpCtx.getStart().getLine());
    return retval;
}

From source file:info.fulloo.trygve.parser.Pass1Listener.java

License:Open Source License

private ExpressionStackAPI exprFromExprDotJAVA_ID(final TerminalNode ctxJAVA_ID, final Token ctxGetStart,
        final TerminalNode ctxABELIAN_INCREMENT_OP) {
    // Certified Pass 1 version ;-) There is no longer any Pass 2+ version
    UnaryopExpressionWithSideEffect.PreOrPost preOrPost;
    Type type = null;/*  w w w.  j ava 2s .c o  m*/
    final ExpressionStackAPI qualifier = parsingData_.popRawExpression();
    Expression expression = null;
    final String javaIdString = ctxJAVA_ID.getText();
    preOrPost = UnaryopExpressionWithSideEffect.PreOrPost.Post;

    if (null != ctxABELIAN_INCREMENT_OP) {
        final Interval JavaIDInterval = ctxJAVA_ID.getSourceInterval();
        final Interval OperatorInterval = ctxABELIAN_INCREMENT_OP.getSourceInterval();
        preOrPost = JavaIDInterval.startsAfter(OperatorInterval) ? UnaryopExpressionWithSideEffect.PreOrPost.Pre
                : UnaryopExpressionWithSideEffect.PreOrPost.Post;
    }

    assert null != qualifier;
    if (null != qualifier.type() && qualifier.type().name().equals("Class")) {
        // This is where we handle types like "System" for System.out.print*
        // Now we need to get the actual class of that name
        final Type rawClass = currentScope_.lookupTypeDeclarationRecursive(qualifier.name());
        assert rawClass instanceof ClassType;
        final ClassType theClass = (ClassType) rawClass;

        final ObjectDeclaration odecl = theClass.type().enclosedScope().lookupObjectDeclaration(javaIdString);
        if (null != odecl) {
            // It must be static
            final ObjectDeclaration odecl2 = theClass.type().enclosedScope()
                    .lookupStaticDeclaration(javaIdString);
            if (null == odecl2) {
                errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(),
                        "Attempt to access instance member `", javaIdString,
                        "' as a member of class `" + theClass.name(), "'.");
                type = new ErrorType();
            } else {
                type = odecl.type();
            }
            assert type != null;

            if (null != ctxABELIAN_INCREMENT_OP) {
                expression = new QualifiedClassMemberExpressionUnaryOp(theClass, javaIdString, type,
                        ctxABELIAN_INCREMENT_OP.getText(), preOrPost);
            } else {
                expression = new QualifiedClassMemberExpression(theClass, javaIdString, type);
            }
        } else {
            errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Member `", javaIdString,
                    "' of `" + qualifier.name(), "' is not defined.");
            expression = new ErrorExpression(null);
        }
    } else {
        final Expression object = (Expression) qualifier;
        object.setResultIsConsumed(true);
        final Type objectType = object.type();
        Declaration odecl = null;
        if (objectType.isntError()) {
            odecl = objectType.enclosedScope().lookupObjectDeclarationRecursive(javaIdString);
        }

        if (null != odecl) {
            type = odecl.type();
            assert type != null;

            if (null != ctxABELIAN_INCREMENT_OP) {
                expression = new QualifiedIdentifierExpressionUnaryOp(object, javaIdString, type,
                        ctxABELIAN_INCREMENT_OP.getText(), preOrPost);
            } else {
                expression = new QualifiedIdentifierExpression(object, javaIdString, type);
            }

            if (odecl instanceof ObjectDeclaration) {
                final ObjectDeclaration odeclAsOdecl = (ObjectDeclaration) odecl;
                final boolean isAccessible = currentScope_.canAccessDeclarationWithAccessibility(odeclAsOdecl,
                        odeclAsOdecl.accessQualifier_, ctxGetStart.getLine());
                if (isAccessible == false) {
                    errorHook6p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Cannot access expression `",
                            expression.getText(), "' with `", odeclAsOdecl.accessQualifier_.asString(),
                            "' access qualifier.", "");
                }
            } else if (odecl.isntError()) {
                errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Cannot access expression `",
                        expression.getText(), "' with non-object type declaration", "");
            }
        } else if (null == (expression = degenerateProcedureCheck(qualifier, objectType, javaIdString,
                ctxGetStart.getLine()))) {
            if (object.isntError() && object.type().isntError()) {
                errorHook5p2(ErrorIncidenceType.Fatal, ctxGetStart.getLine(), "Field `", javaIdString,
                        "' not found as member of `", object.type().name() + "'.");
            }
            type = new ErrorType();
            odecl = new ErrorDeclaration("");
            expression = new ErrorExpression(null);
        }
    }

    return null == expression ? new ErrorExpression(null) : expression;
}

From source file:io.proleap.cobol.preprocessor.sub.document.impl.CobolDocumentParserListenerImpl.java

License:Open Source License

@Override
public void visitTerminal(final TerminalNode node) {
    final int tokPos = node.getSourceInterval().a;
    context().write(TokenUtils.getHiddenTokensToLeft(tokPos, tokens));

    if (!TokenUtils.isEOF(node)) {
        final String text = node.getText();
        context().write(text);//from  w  w  w.j av  a 2 s  . c  o  m
    }
}

From source file:io.proleap.cobol.preprocessor.sub.document.impl.CobolHiddenTokenCollectorListenerImpl.java

License:Open Source License

@Override
public void visitTerminal(final TerminalNode node) {
    if (!firstTerminal) {
        final int tokPos = node.getSourceInterval().a;
        outputBuffer.append(TokenUtils.getHiddenTokensToLeft(tokPos, tokens));
    }/*from  w w  w .j a v a 2  s. co  m*/

    if (!TokenUtils.isEOF(node)) {
        final String text = node.getText();
        outputBuffer.append(text);
    }

    firstTerminal = false;
}

From source file:net.klazz.symboliclua.conv.Converter.java

License:Open Source License

@Override
public String visitTerminal(TerminalNode node) {
    return mStream.getText(node.getSourceInterval());
}

From source file:org.icesquirrel.interpreter.SquirrelInterpretedProcessor.java

License:Open Source License

/**
 * Evaluate an increment expression/*from w ww  .  j a va  2 s.c  o  m*/
 * 
 * @param incexp
 *            increment expression
 */
private Object evaluate(IncexpContext incexp) {
    // : ( ('++' | '--' ) var ) | ( var ( '++' | '--' ) ) | ( var '+=' exp )
    // | ( var '-=' exp )
    final ExpContext exp = incexp.exp();
    TerminalNode inc = incexp.INC();
    TerminalNode dec = incexp.DEC();
    TerminalNode decval = incexp.DECVAL();
    TerminalNode incval = incexp.INCVAL();
    TerminalNode op = null;
    int n = 1;
    if (exp != null) {
        n = ((Number) evaluate(exp)).intValue();
    }
    if (dec != null || decval != null) {
        n *= -1;
        op = dec == null ? decval : dec;
    } else {
        op = inc == null ? incval : inc;
    }
    VarContext varc = incexp.var();
    Key var = resolveVar(varc);
    Object val = get((SquirrelTable) var.getKey(), var.getValue());
    Object newVal = SquirrelExecutionContext.get().getRuntime().getClass(val).add(val, n);
    ((SquirrelTable) var.getKey()).put(var.getValue(), newVal);
    return op.getSourceInterval().startsBeforeDisjoint(varc.getSourceInterval()) ? val : newVal;
}

From source file:org.icesquirrel.interpreter.SquirrelInterpretedProcessor.java

License:Open Source License

/**
 * Process a for loop.//from   w ww.j  a va 2  s.c om
 * 
 * @param forLoop
 *            loop
 */
private void processForLoop(ForloopContext forLoop) {
    // : 'for' '(' ( localvar | assignlist ) ';' exp? ';' exp ')' block

    // First either declare new variables or assign values to exist ones
    LocalvarContext lvc = forLoop.localvar();
    if (lvc != null) {
        // Create a new table for the scope of the loop, delegating to the
        // current environment
        SquirrelExecution execution = SquirrelExecutionContext.get().execution();
        SquirrelExecution newExecution = SquirrelExecutionContext.get().newExecution(execution.getEnvironment(),
                forLoop.getStart().getLine());
        newExecution.addDelegate(execution);
        processDeclareList(newExecution, lvc.declarelist());
    } else {
        AssignlistContext ass = forLoop.assignlist();
        if (ass != null) {
            processAssignList(ass);
        }
    }

    // Find the condition expression and the adjust expression
    List<ExpContext> exps = forLoop.exp();
    ExpContext condition = null;
    ExpContext loop = null;
    if (exps.size() > 0) {
        final List<TerminalNode> seps = forLoop.STATSEP();
        TerminalNode s1 = seps.get(1);
        if (exps.size() == 1) {
            ExpContext e1 = exps.get(0);
            if (e1.getSourceInterval().startsBeforeNonDisjoint(s1.getSourceInterval())) {
                condition = e1;
            } else {
                loop = e1;
            }
        } else {
            condition = exps.get(0);
            loop = exps.get(1);
        }
    }

    //
    while (condition == null || SquirrelMath.bool(evaluate(condition))) {
        try {
            processStatement(forLoop.stat());
        } catch (Break be) {
            break;
        } catch (Continue c) {
            continue;
        }
        if (loop != null) {
            evaluate(loop);
        }
    }

    if (lvc != null) {
        SquirrelExecutionContext.get().popExecution();
    }
}