Example usage for org.antlr.v4.runtime Token getText

List of usage examples for org.antlr.v4.runtime Token getText

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Token getText.

Prototype

String getText();

Source Link

Document

Get the text of the token.

Usage

From source file:illarion.easynpc.parser.ParsedNpcVisitor.java

License:Open Source License

@Override
public ParsedNpcVisitor visitWalkConfiguration(@NotNull WalkConfigurationContext ctx) {
    Token startToken = ctx.getStart();
    switch (startToken.getText()) {
    case "radius":
        npc.addNpcData(new ParsedWalkingRadius(getInteger(ctx.INT())));
        break;/* ww  w  . j av a 2 s  . c o m*/
    default:
        ctx.addErrorNode(startToken);
        LOGGER.warn("Unknown walking configuration key: {}", startToken.getText());
    }
    return super.visitWalkConfiguration(ctx);
}

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

License:Open Source License

@Override
public void exitBoolean_expr(KantParser.Boolean_exprContext ctx) {
    // : boolean_product (ABELIAN_SUMOP abelian_product)*
    // | boolean_expr op=('&&' | '||' | '^' ) boolean_expr
    // | if_expr/*from   www. j  a  v  a  2  s .  c  o  m*/
    // | abelian_expr op1=('is' | 'Is') op2 =('not' | 'Not') abelian_expr
    // | abelian_expr op=('!=' | '==' | GT | LT | '>=' | '<='| (is/isnot variants)) abelian_expr
    // | <assoc=right> boolean_expr ASSIGN expr

    Expression expression = null;
    final int lineNumber = ctx.getStart().getLine();
    boolean useCompareTo = false;

    if (null != ctx.op1 && null != ctx.op2) {
        // 'is' 'not'
        Expression rhs = null, lhs = null;
        if (parsingData_.currentExpressionExists()) {
            rhs = parsingData_.popExpression();
        } else {
            rhs = new ErrorExpression(null);
        }
        if (parsingData_.currentExpressionExists()) {
            lhs = parsingData_.popExpression();
        } else {
            lhs = new ErrorExpression(null);
        }
        lhs.setResultIsConsumed(true);
        rhs.setResultIsConsumed(true);

        expression = new IdentityBooleanExpression(lhs, "is not", rhs);
    } else if ((null != ctx.abelian_expr() && ctx.abelian_expr().size() == 1) && (null != ctx.boolean_expr())
            && (null == ctx.boolean_expr() || ctx.boolean_expr().size() == 0) && null == ctx.ASSIGN()) {
        // : abelian_expr
        Expression rhs = null;
        if (parsingData_.currentExpressionExists()) {
            rhs = parsingData_.popExpression();
        } else {
            rhs = new ErrorExpression(null);
        }

        // rhs.setResultIsConsumed(true);   // done by assignmentExpr call
        expression = rhs;
        if (printProductionsDebug) {
            System.err.println("boolean_expr : abelian_expr");
        }
    } else if (null != ctx.if_expr()) {
        // | if_expr
        expression = parsingData_.popExpression();
        if (printProductionsDebug) {
            System.err.println("boolean_expr : if_expr ");
        }
    } else if (null != ctx.boolean_product() && ctx.boolean_product().size() > 1
            && null != ctx.BOOLEAN_SUMOP()) {
        // | boolean_expr op=('&&' | '||' | '^' ) boolean_expr

        // Make it left-associative
        final int booleanSumopSize = ctx.BOOLEAN_SUMOP().size();
        final Stack<Expression> exprStack = new Stack<Expression>();
        for (int i = 0; i < booleanSumopSize; i++) {
            final Expression expr2 = parsingData_.popExpression();
            exprStack.push(expr2);
        }

        if (parsingData_.currentExpressionExists()) {
            expression = parsingData_.popExpression();
            for (int i = 0; i < booleanSumopSize; i++) {
                final Expression expr2 = exprStack.pop();
                final String operatorAsString = ctx.BOOLEAN_SUMOP(i).getText();
                expression = new SumExpression(expression, operatorAsString, expr2, ctx.getStart(), this);
            }
        } else {
            expression = new ErrorExpression(null);
        }

        if (printProductionsDebug) {
            System.err.print("boolean_expr : boolean_expr ");
            for (int i = 0; i < ctx.BOOLEAN_SUMOP().size(); i++) {
                System.err.print("`");
                System.err.print(ctx.BOOLEAN_SUMOP(i).getText());
                System.err.print("' boolean_expr ");
            }
            System.err.println();
        }
    } else if (null != ctx.boolean_product() && ctx.boolean_product().size() == 1) {
        expression = parsingData_.popExpression();
        if (printProductionsDebug) {
            System.err.println("boolean_expr : boolean_product ");
        }
    } else if (null != ctx.boolean_expr() && ctx.boolean_expr().size() > 1 && null != ctx.op
            && ctx.op.getText().length() > 0) {
        //   | abelian_expr op=('&&') abelian_expr
        final Token relationalOperator = ctx.op;

        final Expression rhs = parsingData_.currentExpressionExists() ? parsingData_.popExpression()
                : new ErrorExpression(null);
        final Expression lhs = parsingData_.currentExpressionExists() ? parsingData_.popExpression()
                : new ErrorExpression(null);
        lhs.setResultIsConsumed(true);
        rhs.setResultIsConsumed(true);
        if (lhs.isntError() && rhs.isntError() && lhs.type().canBeConvertedFrom(rhs.type()) == false) {
            errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "Expression '" + rhs.getText(),
                    "' is not of the right type (", lhs.type().getText(), ").");
        }

        assert null != relationalOperator;
        final String operationAsString = relationalOperator.getText();
        expression = new RelopExpression(lhs, operationAsString, rhs);

        if (printProductionsDebug) {
            System.err.print("relop_expr : boolean_expr ");
            System.err.print(operationAsString);
            System.err.println(" boolean_expr");
        }
    } else if ((null != ctx.boolean_expr()) && (ctx.boolean_expr().size() == 1) && (ctx.expr() != null)
            && null != ctx.ASSIGN()) {
        // : lhs '=' rhs
        Expression rhs = null, lhs = null;
        if (parsingData_.currentExpressionExists()) {
            rhs = parsingData_.popExpression();
        } else {
            rhs = new ErrorExpression(null);
        }
        if (parsingData_.currentExpressionExists()) {
            lhs = parsingData_.popExpression();
        } else {
            lhs = new ErrorExpression(null);
        }
        // rhs.setResultIsConsumed(true);   // done by assignmentExpr call
        expression = this.assignmentExpr(lhs, ctx.ASSIGN().getText(), rhs, ctx);
        if (printProductionsDebug) {
            System.err.println("boolean_expr : boolean_expr ASSIGN boolean_expr");
        }
    } else if (null != ctx.abelian_expr() && ctx.abelian_expr().size() > 1 && null != ctx.op
            && ctx.op.getText().length() > 0) {
        //   | abelian_expr op=('<=' | '>=' | '<' | '>' | '==' | '!=' | (is /isnot variants)) abelian_expr
        final Token relationalOperator = ctx.op;

        Expression rhs = null, lhs = null;
        if (parsingData_.currentExpressionExists()) {
            rhs = parsingData_.popExpression();
        } else {
            rhs = new ErrorExpression(null);
        }
        if (parsingData_.currentExpressionExists()) {
            lhs = parsingData_.popExpression();
        } else {
            lhs = new ErrorExpression(null);
        }
        lhs.setResultIsConsumed(true);
        rhs.setResultIsConsumed(true);

        assert null != relationalOperator;
        final String operationAsString = relationalOperator.getText();

        if (operationAsString.equals("is") || operationAsString.equals("Is")
                || operationAsString.equals("isnot") || operationAsString.equals("IsNot")) {
            expression = new IdentityBooleanExpression(lhs, operationAsString, rhs);
        } else {
            // Refactor into method
            final Type lhsType = null == lhs ? null : lhs.type(), rhsType = null == rhs ? null : rhs.type();

            if (null != lhsType && null != rhsType) {
                if (lhsType.canBeConvertedFrom(rhsType) == false && lhs.isntError() && lhsType.isntError()
                        && rhs.isntError() && rhsType.isntError()) {
                    errorHook5p2(ErrorIncidenceType.Fatal, lineNumber, "Expression `" + rhs.getText(),
                            "' is not of the right type (", lhs.type().getText(), ").");
                }

                if (lhsType.canBeLhsOfBinaryOperatorForRhsType(operationAsString, rhsType)) {
                    ; // O.K.
                } else if (lhs.isntError() && lhs.type().isntError() && rhs.isntError()
                        && rhsType.isntError()) {
                    errorHook5p2(ErrorIncidenceType.Fatal, lineNumber,
                            "You may not apply '" + operationAsString, "' to objects of type `",
                            lhs.type().getText(), "'.");
                }
            }

            final ActualArgumentList parameterList = new ActualArgumentList();
            parameterList.addActualArgument(lhs);
            parameterList.addActualArgument(rhs);
            if (null != lhs && null != rhs && null != lhsType && (lhsType instanceof InterfaceType) == false
                    && lhs.isntError() && rhs.isntError() && lhsType.isntError() && rhsType.isntError()
                    && (operationAsString.equals("<") || operationAsString.equals(">")
                            || operationAsString.equals("<=") || operationAsString.equals(">=")
                            || operationAsString.equals("==") || operationAsString.equals("!="))
                    && null != lhsType.enclosedScope().lookupMethodDeclarationRecursive("compareTo",
                            parameterList, false)) {
                useCompareTo = true; // then, yeah...
            } else if (null != lhs && null != rhs && null != lhsType && lhsType instanceof InterfaceType
                    && (operationAsString.equals("<") || operationAsString.equals(">")
                            || operationAsString.equals("<=") || operationAsString.equals(">=")
                            || operationAsString.equals("==") || operationAsString.equals("!="))
                    && null != ((StaticInterfaceScope) lhs.type().enclosedScope())
                            .lookupMethodDeclarationWithConversionIgnoringParameter("compareTo", parameterList,
                                    false, "this")) {
                useCompareTo = true; // then, yeah...
            } else if (null != rhs && null != rhsType
                    && rhs.type().canBeRhsOfBinaryOperator(operationAsString)) {
                ; // O.K.
            } else if (rhs instanceof NullExpression) {
                ; // can always compare with NULL
            } else if (rhs.isntError() && null != rhs.type() && rhs.type().isntError()) {
                errorHook5p2(ErrorIncidenceType.Fatal, lineNumber,
                        "You may not use an object of type '"
                                + (null == rhs || null == rhs.type() ? "<unknown>" : rhs.type().getText()),
                        "' as an argument to `", operationAsString, "'.");
            }

            if (lhs.type() instanceof RoleType) {
                // We may need to convert the operator
                // to a call of compareTo
                if ((operationAsString.equals("==") || operationAsString.equals("!="))
                        && rhs.type().pathName().equals("Null")) {
                    // Comparing to NULL is O.K...  for now....
                    expression = new RelopExpression(lhs, operationAsString, rhs);
                } else {
                    expression = handleRelopCallWithRoleLHS(lhs, operationAsString, rhs, lineNumber);
                }
            } else {
                if (useCompareTo && lhs.type() instanceof BuiltInType == false
                        && rhs.type().pathName().equals("Null") == false) {
                    // We found above that things are all set up to use compareTo. Use it.
                    expression = handleRelopCall(lhs, operationAsString, rhs, lineNumber);
                } else {
                    expression = new RelopExpression(lhs, operationAsString, rhs);
                }
            }
        }

        if (printProductionsDebug) {
            System.err.print("boolean_expr : abelian_expr ");
            System.err.print(operationAsString);
            System.err.println(" abelian_expr");
        }

    } else {
        assert false;
    }

    if (null != expression) {
        // null check is error stumbling check
        parsingData_.pushExpression(expression);
    }
    if (stackSnapshotDebug)
        stackSnapshotDebug();
}

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

License:Open Source License

private void processIdentifierListRecursive(final Identifier_listContext identifier_list, final Type type,
        final int lineNumber, final AccessQualifier accessQualifier) {
    final List<ParseTree> children = identifier_list.children;
    Token tok;
    ObjectDeclaration objDecl = null;//from ww  w . j  av  a  2  s  .c o  m
    for (ParseTree pt : children) {
        if (pt instanceof TerminalNodeImpl) {
            final TerminalNodeImpl tnpt = (TerminalNodeImpl) pt;
            if (tnpt.getChildCount() == 0) {
                tok = tnpt.getSymbol();
                final String tokAsText = tok.getText();
                if (tokAsText.equals("=") == true) {
                    ; // we pick it up under the ExprContext check below
                } else if (tokAsText.equals(",") == true) {
                    ; // skip it; it separates elements
                } else {
                    this.nameCheck(tokAsText, lineNumber);
                    objDecl = this.pass1InitialDeclarationCheck(tokAsText, lineNumber);
                    if (null == objDecl) {
                        objDecl = new ObjectDeclaration(tokAsText, type, lineNumber);
                        declareObjectSuitableToPass(currentScope_, objDecl);
                        objDecl.setAccess(accessQualifier, currentScope_, lineNumber);
                    } else {
                        // Doesn't hurt to update type
                        objDecl.updateType(type);
                    }
                }
            } else {
                for (int i = 0; i < tnpt.getChildCount(); i++) {
                    final ParseTree pt2 = tnpt.getChild(i);
                    assert pt2 instanceof TerminalNodeImpl;
                    final TerminalNodeImpl tnpt2 = (TerminalNodeImpl) pt2;
                    tok = tnpt2.getSymbol();
                    final String tokAsText = tok.getText();
                    this.nameCheck(tokAsText, lineNumber);
                    if (tokAsText.equals("=") == true) {
                        ; // we get it with the ExprContext catch below
                    } else if (tokAsText.equals(",") == true) {
                        ; // skip it; it separates elements
                    } else {
                        objDecl = this.pass1InitialDeclarationCheck(tokAsText, lineNumber);
                        if (null == objDecl) {
                            objDecl = new ObjectDeclaration(tokAsText, type, lineNumber);
                            declareObjectSuitableToPass(currentScope_, objDecl);
                            objDecl.setAccess(accessQualifier, currentScope_, lineNumber);
                        } else {
                            // Doesn't hurt to update type
                            objDecl.updateType(type);
                        }
                    }
                }
            }
        } else if (pt instanceof Identifier_listContext) {
            this.processIdentifierListRecursive((Identifier_listContext) pt, type, lineNumber, accessQualifier);
            // System.err.print("Alert: ");
            // System.err.println(pt.getText());
        } else if (pt instanceof ExprContext) {
            if (parsingData_.currentExpressionExists()) {
                final Expression initializationExpr = parsingData_.popExpression();
                assert initializationExpr != null;
                assert objDecl != null;
                updateInitializationLists(initializationExpr, objDecl);
            }
        } else {
            assert false;
        }
    }
}

From source file:io.crate.sql.parser.AstBuilder.java

License:Apache License

private String getObjectType(Token type) {
    if (type == null)
        return null;
    switch (type.getType()) {
    case SqlBaseLexer.DYNAMIC:
        return type.getText().toLowerCase(Locale.ENGLISH);
    case SqlBaseLexer.STRICT:
        return type.getText().toLowerCase(Locale.ENGLISH);
    case SqlBaseLexer.IGNORED:
        return type.getText().toLowerCase(Locale.ENGLISH);
    }/*from  w  w w  .jav a  2s  .c  o  m*/

    throw new UnsupportedOperationException("Unsupported object type: " + type.getText());
}

From source file:io.crate.sql.parser.AstBuilder.java

License:Apache License

private static ArithmeticExpression.Type getArithmeticBinaryOperator(Token operator) {
    switch (operator.getType()) {
    case SqlBaseLexer.PLUS:
        return ArithmeticExpression.Type.ADD;
    case SqlBaseLexer.MINUS:
        return ArithmeticExpression.Type.SUBTRACT;
    case SqlBaseLexer.ASTERISK:
        return ArithmeticExpression.Type.MULTIPLY;
    case SqlBaseLexer.SLASH:
        return ArithmeticExpression.Type.DIVIDE;
    case SqlBaseLexer.PERCENT:
        return ArithmeticExpression.Type.MODULUS;
    }//from   ww w  .  j a v a 2  s  .com

    throw new UnsupportedOperationException("Unsupported operator: " + operator.getText());
}

From source file:io.crate.sql.parser.AstBuilder.java

License:Apache License

private static ComparisonExpression.Type getComparisonOperator(Token symbol) {
    switch (symbol.getType()) {
    case SqlBaseLexer.EQ:
        return ComparisonExpression.Type.EQUAL;
    case SqlBaseLexer.NEQ:
        return ComparisonExpression.Type.NOT_EQUAL;
    case SqlBaseLexer.LT:
        return ComparisonExpression.Type.LESS_THAN;
    case SqlBaseLexer.LTE:
        return ComparisonExpression.Type.LESS_THAN_OR_EQUAL;
    case SqlBaseLexer.GT:
        return ComparisonExpression.Type.GREATER_THAN;
    case SqlBaseLexer.GTE:
        return ComparisonExpression.Type.GREATER_THAN_OR_EQUAL;
    case SqlBaseLexer.REGEX_MATCH:
        return ComparisonExpression.Type.REGEX_MATCH;
    case SqlBaseLexer.REGEX_NO_MATCH:
        return ComparisonExpression.Type.REGEX_NO_MATCH;
    case SqlBaseLexer.REGEX_MATCH_CI:
        return ComparisonExpression.Type.REGEX_MATCH_CI;
    case SqlBaseLexer.REGEX_NO_MATCH_CI:
        return ComparisonExpression.Type.REGEX_NO_MATCH_CI;
    }/*from   www  .j  a v a 2s.  c  o  m*/

    throw new IllegalArgumentException("Unsupported operator: " + symbol.getText());
}

From source file:io.crate.sql.parser.AstBuilder.java

License:Apache License

private static CurrentTime.Type getDateTimeFunctionType(Token token) {
    switch (token.getType()) {
    case SqlBaseLexer.CURRENT_DATE:
        return CurrentTime.Type.DATE;
    case SqlBaseLexer.CURRENT_TIME:
        return CurrentTime.Type.TIME;
    case SqlBaseLexer.CURRENT_TIMESTAMP:
        return CurrentTime.Type.TIMESTAMP;
    }//from ww  w.  j av a 2  s .  c o m

    throw new IllegalArgumentException("Unsupported special function: " + token.getText());
}

From source file:io.crate.sql.parser.AstBuilder.java

License:Apache License

private static ArrayComparisonExpression.Quantifier getComparisonQuantifier(Token symbol) {
    switch (symbol.getType()) {
    case SqlBaseLexer.ALL:
        return ArrayComparisonExpression.Quantifier.ALL;
    case SqlBaseLexer.ANY:
        return ArrayComparisonExpression.Quantifier.ANY;
    case SqlBaseLexer.SOME:
        return ArrayComparisonExpression.Quantifier.ANY;
    }/*from w  ww  .  j  a v  a2  s  . c  o m*/

    throw new IllegalArgumentException("Unsupported quantifier: " + symbol.getText());
}

From source file:io.mxnet.caffetranslator.CreateModelListener.java

License:Apache License

private String getPrototxt(TokenStream stream, int start, int end) {
    StringBuilder prototxt = new StringBuilder();
    for (int i = start; i <= end; i++) {
        Token token = stream.get(i);
        prototxt.append(token.getText());
    }/*from  ww w  .j  a  v  a  2 s.com*/
    String strPrototxt = prototxt.toString();
    return strPrototxt.replaceAll(" +num_examples:.*\\s", "");
}

From source file:io.prestosql.sql.parser.AstBuilder.java

License:Apache License

private static ArithmeticBinaryExpression.Operator getArithmeticBinaryOperator(Token operator) {
    switch (operator.getType()) {
    case SqlBaseLexer.PLUS:
        return ArithmeticBinaryExpression.Operator.ADD;
    case SqlBaseLexer.MINUS:
        return ArithmeticBinaryExpression.Operator.SUBTRACT;
    case SqlBaseLexer.ASTERISK:
        return ArithmeticBinaryExpression.Operator.MULTIPLY;
    case SqlBaseLexer.SLASH:
        return ArithmeticBinaryExpression.Operator.DIVIDE;
    case SqlBaseLexer.PERCENT:
        return ArithmeticBinaryExpression.Operator.MODULUS;
    }/*from   w w w.  j av a 2  s  . com*/

    throw new UnsupportedOperationException("Unsupported operator: " + operator.getText());
}