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

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

Introduction

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

Prototype

int getCharPositionInLine();

Source Link

Document

The index of the first character of this token relative to the beginning of the line at which it occurs, 0..n-1

Usage

From source file:ch.raffael.contracts.processor.cel.Position.java

License:Apache License

public Position(@NotNull Token token) {
    this(token.getLine(), token.getCharPositionInLine());
}

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  ww .j  av a  2s .  c  o m
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.ExceptionConvertor.java

License:Open Source License

/**
 * Returns the position information string for a parser exception.
 * @param t the token to return the information for
 * @return is a string with line and column information
 *//*w w w.java  2s .co  m*/
private static String getPositionInfo(Token t) {
    return t.getLine() > 0 && t.getCharPositionInLine() > 0
            ? " at line " + t.getLine() + " column " + t.getCharPositionInLine()
            : "";
}

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

License:Open Source License

private static String getNoAnnotation(String expression,
        List<EsperEPL2GrammarParser.AnnotationEnumContext> annos, CommonTokenStream tokens) {
    if (annos == null || annos.isEmpty()) {
        return expression;
    }/*from  w w w  .  j  a va 2s . c  om*/
    Token lastAnnotationToken = annos.get(annos.size() - 1).getStop();

    if (lastAnnotationToken == null) {
        return null;
    }

    try {
        int line = lastAnnotationToken.getLine();
        int charpos = lastAnnotationToken.getCharPositionInLine();
        int fromChar = charpos + lastAnnotationToken.getText().length();
        if (line == 1) {
            return expression.substring(fromChar).trim();
        }

        String[] lines = expression.split("\r\n|\r|\n");
        StringBuilder buf = new StringBuilder();
        buf.append(lines[line - 1].substring(fromChar));
        for (int i = line; i < lines.length; i++) {
            buf.append(lines[i]);
            if (i < lines.length - 1) {
                buf.append(newline);
            }
        }
        return buf.toString().trim();
    } catch (RuntimeException ex) {
        log.error("Error determining non-annotated expression sting: " + ex.getMessage(), ex);
    }
    return null;
}

From source file:com.facebook.presto.sql.parser.AstBuilder.java

License:Apache License

public static NodeLocation getLocation(Token token) {
    requireNonNull(token, "token is null");
    return new NodeLocation(token.getLine(), token.getCharPositionInLine());
}

From source file:com.facebook.presto.teradata.functions.dateformat.DateFormatParser.java

License:Apache License

public static DateTimeFormatter createDateTimeFormatter(String format) {
    DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
    for (Token token : tokenize(format)) {
        switch (token.getType()) {
        case DateFormat.TEXT:
            builder.appendLiteral(token.getText());
            break;
        case DateFormat.DD:
            builder.appendDayOfMonth(2);
            break;
        case DateFormat.HH24:
            builder.appendHourOfDay(2);/*from  w  w w .j  a  va  2s . co  m*/
            break;
        case DateFormat.HH:
            builder.appendHourOfHalfday(2);
            break;
        case DateFormat.MI:
            builder.appendMinuteOfHour(2);
            break;
        case DateFormat.MM:
            builder.appendMonthOfYear(2);
            break;
        case DateFormat.SS:
            builder.appendSecondOfMinute(2);
            break;
        case DateFormat.YY:
            builder.appendTwoDigitYear(2050);
            break;
        case DateFormat.YYYY:
            builder.appendYear(4, 4);
            break;
        case DateFormat.UNRECOGNIZED:
        default:
            throw new PrestoException(StandardErrorCode.INVALID_FUNCTION_ARGUMENT,
                    String.format("Failed to tokenize string [%s] at offset [%d]", token.getText(),
                            token.getCharPositionInLine()));
        }
    }

    try {
        return builder.toFormatter();
    } catch (UnsupportedOperationException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e);
    }
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public Template visitBlock(final BlockContext ctx) {
    SexprContext sexpr = ctx.sexpr();// w  w w  .j  a v a  2 s  .c om
    Token nameStart = sexpr.QID().getSymbol();
    String name = nameStart.getText();
    String nameEnd = ctx.nameEnd.getText();
    if (!name.equals(nameEnd)) {
        reportError(null, ctx.nameEnd.getLine(), ctx.nameEnd.getCharPositionInLine(),
                String.format("found: '%s', expected: '%s'", nameEnd, name));
    }

    hasTag(true);
    Block block = new Block(handlebars, name, false, params(sexpr.param()), hash(sexpr.hash()));
    block.filename(source.filename());
    block.position(nameStart.getLine(), nameStart.getCharPositionInLine());
    String startDelim = ctx.start.getText();
    startDelim = startDelim.substring(0, startDelim.length() - 1);
    block.startDelimiter(startDelim);
    block.endDelimiter(ctx.stop.getText());

    Template body = visitBody(ctx.thenBody);
    if (body != null) {
        block.body(body);
    }
    ElseBlockContext elseBlock = ctx.elseBlock();
    if (elseBlock != null) {
        Template unless = visitBody(elseBlock.unlessBody);
        if (unless != null) {
            String inverseLabel = elseBlock.inverseToken.getText();
            if (inverseLabel.startsWith(startDelim)) {
                inverseLabel = inverseLabel.substring(startDelim.length());
            }
            block.inverse(inverseLabel, unless);
        }
    }
    hasTag(true);
    return block;
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public Object visitEscape(final EscapeContext ctx) {
    Token token = ctx.ESC_VAR().getSymbol();
    String text = token.getText().substring(1);
    line.append(text);//from   w  w w . j a  v a 2 s  . c  om
    return new Text(text, "\\").filename(source.filename()).position(token.getLine(),
            token.getCharPositionInLine());
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

/**
 * Build a new {@link Variable}.//from   w ww  . j a  va 2 s .  c om
 *
 * @param name The var's name.
 * @param varType The var's type.
 * @param params The var params.
 * @param hash The var hash.
 * @param startDelimiter The current start delimiter.
 * @param endDelimiter The current end delimiter.
 * @return A new {@link Variable}.
 */
private Template newVar(final Token name, final TagType varType, final List<Object> params,
        final Map<String, Object> hash, final String startDelimiter, final String endDelimiter) {
    String varName = name.getText();
    Helper<Object> helper = handlebars.helper(varName);
    if (helper == null && ((params.size() > 0 || hash.size() > 0) || varType == TagType.SUB_EXPRESSION)) {
        Helper<Object> helperMissing = handlebars.helper(HelperRegistry.HELPER_MISSING);
        if (helperMissing == null) {
            reportError(null, name.getLine(), name.getCharPositionInLine(),
                    "could not find helper: '" + varName + "'");
        }
    }
    return new Variable(handlebars, varName, varType, params, hash).startDelimiter(startDelimiter)
            .endDelimiter(endDelimiter).filename(source.filename())
            .position(name.getLine(), name.getCharPositionInLine());
}

From source file:com.github.jknack.handlebars.internal.TemplateBuilder.java

License:Apache License

@Override
public Template visitPartial(final PartialContext ctx) {
    hasTag(true);//from www .j  ava 2s . c o  m
    Token pathToken = ctx.PATH().getSymbol();
    String uri = pathToken.getText();
    if (uri.startsWith("[") && uri.endsWith("]")) {
        uri = uri.substring(1, uri.length() - 1);
    }

    if (uri.startsWith("/")) {
        String message = "found: '/', partial shouldn't start with '/'";
        reportError(null, pathToken.getLine(), pathToken.getCharPositionInLine(), message);
    }

    String indent = line.toString();
    if (hasTag()) {
        if (isEmpty(indent) || !isEmpty(indent.trim())) {
            indent = null;
        }
    } else {
        indent = null;
    }

    TerminalNode partialContext = ctx.QID();
    String startDelim = ctx.start.getText();
    Template partial = new Partial(handlebars, uri, partialContext != null ? partialContext.getText() : null)
            .startDelimiter(startDelim.substring(0, startDelim.length() - 1)).endDelimiter(ctx.stop.getText())
            .indent(indent).filename(source.filename())
            .position(pathToken.getLine(), pathToken.getCharPositionInLine());

    return partial;
}