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:com.espertech.esper.core.deploy.EPLModuleUtil.java

License:Open Source License

public static ParseNode getModule(EPLModuleParseItem item, String resourceName)
        throws ParseException, IOException {
    CharStream input = new NoCaseSensitiveStream(new StringReader(item.getExpression()));

    EsperEPL2GrammarLexer lex = ParseHelper.newLexer(input);
    CommonTokenStream tokenStream = new CommonTokenStream(lex);
    tokenStream.fill();// w ww.  j  a va  2s.  c o  m

    List tokens = tokenStream.getTokens();
    int beginIndex = 0;
    boolean isMeta = false;
    boolean isModule = false;
    boolean isUses = false;
    boolean isExpression = false;

    while (beginIndex < tokens.size()) {
        Token t = (Token) tokens.get(beginIndex);
        if (t.getType() == EsperEPL2GrammarParser.EOF) {
            break;
        }
        if ((t.getType() == EsperEPL2GrammarParser.WS) || (t.getType() == EsperEPL2GrammarParser.SL_COMMENT)
                || (t.getType() == EsperEPL2GrammarParser.ML_COMMENT)) {
            beginIndex++;
            continue;
        }
        String tokenText = t.getText().trim().toLowerCase();
        if (tokenText.equals("module")) {
            isModule = true;
            isMeta = true;
        } else if (tokenText.equals("uses")) {
            isUses = true;
            isMeta = true;
        } else if (tokenText.equals("import")) {
            isMeta = true;
        } else {
            isExpression = true;
            break;
        }
        beginIndex++;
        beginIndex++; // skip space
        break;
    }

    if (isExpression) {
        return new ParseNodeExpression(item);
    }
    if (!isMeta) {
        return new ParseNodeComment(item);
    }

    // check meta tag (module, uses, import)
    StringWriter buffer = new StringWriter();
    for (int i = beginIndex; i < tokens.size(); i++) {
        Token t = (Token) tokens.get(i);
        if (t.getType() == EsperEPL2GrammarParser.EOF) {
            break;
        }
        if ((t.getType() != EsperEPL2GrammarParser.IDENT) && (t.getType() != EsperEPL2GrammarParser.DOT)
                && (t.getType() != EsperEPL2GrammarParser.STAR) && (!t.getText().matches("[a-zA-Z]*"))) {
            throw getMessage(isModule, isUses, resourceName, t.getType());
        }
        buffer.append(t.getText().trim());
    }

    String result = buffer.toString().trim();
    if (result.length() == 0) {
        throw getMessage(isModule, isUses, resourceName, -1);
    }

    if (isModule) {
        return new ParseNodeModule(item, result);
    } else if (isUses) {
        return new ParseNodeUses(item, result);
    }
    return new ParseNodeImport(item, result);
}

From source file:com.espertech.esper.core.deploy.EPLModuleUtil.java

License:Open Source License

public static List<EPLModuleParseItem> parse(String module) throws ParseException {

    CharStream input;/*from www  .j a  v  a 2 s  . c om*/
    try {
        input = new NoCaseSensitiveStream(new StringReader(module));
    } catch (IOException ex) {
        log.error("Exception reading module expression: " + ex.getMessage(), ex);
        return null;
    }

    EsperEPL2GrammarLexer lex = ParseHelper.newLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lex);
    try {
        tokens.fill();
    } catch (RuntimeException ex) {
        String message = "Unexpected exception recognizing module text";
        if (ex instanceof LexerNoViableAltException) {
            if (ParseHelper.hasControlCharacters(module)) {
                message = "Unrecognized control characters found in text, failed to parse text";
            } else {
                message += ", recognition failed for " + ex.toString();
            }
        } else if (ex instanceof RecognitionException) {
            RecognitionException recog = (RecognitionException) ex;
            message += ", recognition failed for " + recog.toString();
        } else if (ex.getMessage() != null) {
            message += ": " + ex.getMessage();
        }
        message += " [" + module + "]";
        log.error(message, ex);
        throw new ParseException(message);
    }

    List<EPLModuleParseItem> statements = new ArrayList<EPLModuleParseItem>();
    StringWriter current = new StringWriter();
    Integer lineNum = null;
    int charPosStart = 0;
    int charPos = 0;
    List<Token> tokenList = tokens.getTokens();
    Set<Integer> skippedSemicolonIndexes = getSkippedSemicolons(tokenList);
    int index = -1;
    for (Object token : tokenList) // Call getTokens first before invoking tokens.size! ANTLR problem
    {
        index++;
        Token t = (Token) token;
        boolean semi = t.getType() == EsperEPL2GrammarLexer.SEMI && !skippedSemicolonIndexes.contains(index);
        if (semi) {
            if (current.toString().trim().length() > 0) {
                statements.add(new EPLModuleParseItem(current.toString().trim(), lineNum == null ? 0 : lineNum,
                        charPosStart, charPos));
                lineNum = null;
            }
            current = new StringWriter();
        } else {
            if ((lineNum == null) && (t.getType() != EsperEPL2GrammarParser.WS)) {
                lineNum = t.getLine();
                charPosStart = charPos;
            }
            if (t.getType() != EsperEPL2GrammarLexer.EOF) {
                current.append(t.getText());
                charPos += t.getText().length();
            }
        }
    }

    if (current.toString().trim().length() > 0) {
        statements.add(new EPLModuleParseItem(current.toString().trim(), lineNum == null ? 0 : lineNum, 0, 0));
    }
    return statements;
}

From source file:com.espertech.esper.epl.db.DatabasePollingViewableFactory.java

License:Open Source License

/**
 * Lexes the sample SQL and inserts a "where 1=0" where-clause.
 * @param querySQL to inspect using lexer
 * @return sample SQL with where-clause inserted
 * @throws ExprValidationException to indicate a lexer problem
 *//*from  w  w  w .j a v  a  2s  .c  om*/
protected static String lexSampleSQL(String querySQL) throws ExprValidationException {
    querySQL = querySQL.replaceAll("\\s\\s+|\\n|\\r", " ");
    StringReader reader = new StringReader(querySQL);
    CharStream input;
    try {
        input = new NoCaseSensitiveStream(reader);
    } catch (IOException ex) {
        throw new ExprValidationException("IOException lexing query SQL '" + querySQL + '\'', ex);
    }

    int whereIndex = -1;
    int groupbyIndex = -1;
    int havingIndex = -1;
    int orderByIndex = -1;
    List<Integer> unionIndexes = new ArrayList<Integer>();

    EsperEPL2GrammarLexer lex = ParseHelper.newLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lex);
    tokens.fill();
    List tokenList = tokens.getTokens();

    for (int i = 0; i < tokenList.size(); i++) {
        Token token = (Token) tokenList.get(i);
        if ((token == null) || token.getText() == null) {
            break;
        }
        String text = token.getText().toLowerCase().trim();
        if (text.equals("where")) {
            whereIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("group")) {
            groupbyIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("having")) {
            havingIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("order")) {
            orderByIndex = token.getCharPositionInLine() + 1;
        }
        if (text.equals("union")) {
            unionIndexes.add(token.getCharPositionInLine() + 1);
        }
    }

    // If we have a union, break string into subselects and process each
    if (unionIndexes.size() != 0) {
        StringWriter changedSQL = new StringWriter();
        int lastIndex = 0;
        for (int i = 0; i < unionIndexes.size(); i++) {
            int index = unionIndexes.get(i);
            String fragment;
            if (i > 0) {
                fragment = querySQL.substring(lastIndex + 5, index - 1);
            } else {
                fragment = querySQL.substring(lastIndex, index - 1);
            }
            String lexedFragment = lexSampleSQL(fragment);

            if (i > 0) {
                changedSQL.append("union ");
            }
            changedSQL.append(lexedFragment);
            lastIndex = index - 1;
        }

        // last part after last union
        String fragment = querySQL.substring(lastIndex + 5, querySQL.length());
        String lexedFragment = lexSampleSQL(fragment);
        changedSQL.append("union ");
        changedSQL.append(lexedFragment);

        return changedSQL.toString();
    }

    // Found a where clause, simplest cases
    if (whereIndex != -1) {
        StringWriter changedSQL = new StringWriter();
        String prefix = querySQL.substring(0, whereIndex + 5);
        String suffix = querySQL.substring(whereIndex + 5, querySQL.length());
        changedSQL.write(prefix);
        changedSQL.write("1=0 and ");
        changedSQL.write(suffix);
        return changedSQL.toString();
    }

    // No where clause, find group-by
    int insertIndex;
    if (groupbyIndex != -1) {
        insertIndex = groupbyIndex;
    } else if (havingIndex != -1) {
        insertIndex = havingIndex;
    } else if (orderByIndex != -1) {
        insertIndex = orderByIndex;
    } else {
        StringWriter changedSQL = new StringWriter();
        changedSQL.write(querySQL);
        changedSQL.write(" where 1=0 ");
        return changedSQL.toString();
    }

    try {
        StringWriter changedSQL = new StringWriter();
        String prefix = querySQL.substring(0, insertIndex - 1);
        changedSQL.write(prefix);
        changedSQL.write("where 1=0 ");
        String suffix = querySQL.substring(insertIndex - 1, querySQL.length());
        changedSQL.write(suffix);
        return changedSQL.toString();
    } catch (Exception ex) {
        String text = "Error constructing sample SQL to retrieve metadata for JDBC-drivers that don't support metadata, consider using the "
                + SAMPLE_WHERECLAUSE_PLACEHOLDER + " placeholder or providing a sample SQL";
        log.error(text, ex);
        throw new ExprValidationException(text, ex);
    }
}

From source file:com.espertech.esper.epl.parse.ASTContextHelper.java

License:Open Source License

private static boolean checkNow(Token i) {
    if (i == null) {
        return false;
    }//from   www.  j  a  v  a  2 s .c  o m
    String ident = i.getText();
    if (!ident.toLowerCase().equals("now")) {
        throw ASTWalkException.from("Expected 'now' keyword after '@', found '" + ident + "' instead");
    }
    return true;
}

From source file:com.espertech.esper.epl.parse.ASTCreateSchemaHelper.java

License:Open Source License

protected static boolean validateIsPrimitiveArray(Token p) {
    if (p != null) {
        if (!p.getText().toLowerCase().equals("primitive")) {
            throw ASTWalkException
                    .from("Column type keyword '" + p.getText() + "' not recognized, expecting '[primitive]'");
        }/*from   w w w  .  j ava2 s .  c o  m*/
        return true;
    }
    return false;
}

From source file:com.espertech.esper.epl.parse.ASTExprHelper.java

License:Open Source License

public static void addOptionalSimpleProperty(ExprNode exprNode, Token token, VariableService variableService,
        StatementSpecRaw spec) {//w w  w.j a  va  2  s  .  com
    if (token == null) {
        return;
    }
    ExprNode node = ASTExprHelper.resolvePropertyOrVariableIdentifier(token.getText(), variableService, spec);
    exprNode.addChildNode(node);
}

From source file:com.espertech.esper.epl.parse.ASTOutputLimitHelper.java

License:Open Source License

private static Object parseNumOrVariableIdent(EsperEPL2GrammarParser.NumberconstantContext num, Token ident) {
    if (ident != null) {
        return ident.getText();
    } else {//w w w  .j  a  va  2s  . c o m
        return ASTConstantHelper.parse(num);
    }
}

From source file:com.espertech.esper.epl.parse.ASTUtil.java

License:Open Source License

/**
 * Print the token stream to the logger.
 * @param tokens to print//from www  .j  a va  2s .  co  m
 */
public static void printTokens(CommonTokenStream tokens) {
    if (log.isDebugEnabled()) {
        List tokenList = tokens.getTokens();

        StringWriter writer = new StringWriter();
        PrintWriter printer = new PrintWriter(writer);
        for (int i = 0; i < tokens.size(); i++) {
            Token t = (Token) tokenList.get(i);
            String text = t.getText();
            if (text.trim().length() == 0) {
                printer.print("'" + text + "'");
            } else {
                printer.print(text);
            }
            printer.print('[');
            printer.print(t.getType());
            printer.print(']');
            printer.print(" ");
        }
        printer.println();
        log.debug("Tokens: " + writer.toString());
    }
}

From source file:com.espertech.esper.epl.parse.ASTWalkException.java

License:Open Source License

public static ASTWalkException from(String message, Token token) {
    return new ASTWalkException(message + " in text '" + token.getText() + "'");
}

From source file:com.espertech.esper.epl.parse.EPLTreeWalkerListener.java

License:Open Source License

private UniformPair<String> getOnExprWindowName(EsperEPL2GrammarParser.OnExprContext ctx) {
    if (ctx.onDeleteExpr() != null) {
        return getOnExprWindowName(ctx.onDeleteExpr().onExprFrom());
    }//  w  w w .  j a  v a2s. c o m
    if (ctx.onSelectExpr() != null && ctx.onSelectExpr().onExprFrom() != null) {
        return getOnExprWindowName(ctx.onSelectExpr().onExprFrom());
    }
    if (ctx.onUpdateExpr() != null) {
        Token alias = ctx.onUpdateExpr().i;
        return new UniformPair<String>(ctx.onUpdateExpr().n.getText(), alias != null ? alias.getText() : null);
    }
    return null;
}