Example usage for org.antlr.v4.runtime CharStream index

List of usage examples for org.antlr.v4.runtime CharStream index

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CharStream index.

Prototype

int index();

Source Link

Document

Return the index into the stream of the input symbol referred to by LA(1) .

Usage

From source file:com.antsdb.saltedfish.sql.mysql.ExprGenerator.java

License:Open Source License

private static byte[] getBytes(Literal_value_binaryContext rule) {
    Token token = rule.STRING_LITERAL().getSymbol();
    byte[] bytes = new byte[token.getStopIndex() - token.getStartIndex() - 1];
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);/*from  www.  j a va  2s.c  o m*/
    int j = 0;
    for (int i = 0; i < bytes.length; i++) {
        int ch = cs.LA(i + 1);
        if (ch == '\\') {
            i++;
            ch = cs.LA(i + 1);
            if (ch == '0') {
                ch = 0;
            } else if (ch == 'n') {
                ch = '\n';
            } else if (ch == 'r') {
                ch = '\r';
            } else if (ch == 'Z') {
                ch = '\032';
            }
        }
        bytes[j] = (byte) ch;
        j++;
    }
    cs.seek(pos);
    if (j != bytes.length) {
        // esacpe characters
        byte[] old = bytes;
        bytes = new byte[j];
        System.arraycopy(old, 0, bytes, 0, j);
    }
    return bytes;
}

From source file:com.antsdb.saltedfish.sql.mysql.MysqlParserFactory.java

License:Open Source License

/**
 * mysql executes statement in a specially formatted comment like
 * / *!40101 SET NAMES utf8 * /;//from   w  w  w .  ja  v a2s  .  c o  m
 * @param cs
 * @return
 */
private static boolean isCommtedStatement(CharStream cs) {
    int idx = cs.index();
    if (cs.LA(idx + 1) != '/') {
        return false;
    }
    if (cs.LA(idx + 2) != '*') {
        return false;
    }
    if (cs.LA(idx + 3) != '!') {
        return false;
    }
    if (cs.LA(idx + 4) != '4') {
        return false;
    }
    return true;
}

From source file:com.tunnelvisionlabs.postgresql.PostgreSqlLexerAtnSimulator.java

License:Open Source License

@Override
protected int execATN(CharStream input, DFAState ds0) {
    // This works around bug #688 in the ANTLR 4 runtime where zero-length
    // tokens are not recognized.
    if (ds0.isAcceptState) {
        captureSimState(prevAccept, new InputWithFixedIndex(input.index() - 1), ds0);
    }/*w ww .ja v  a2 s .  c  o m*/

    return super.execATN(input, ds0);
}

From source file:org.apache.lucene.expressions.js.JavascriptErrorHandlingLexer.java

License:Apache License

/**
 * Ensures the ANTLR lexer will throw an exception after the first error
 * @param lnvae the lexer exception//from www .j av a 2 s .co  m
 */
@Override
public void recover(LexerNoViableAltException lnvae) {
    CharStream charStream = lnvae.getInputStream();
    int startIndex = lnvae.getStartIndex();
    String text = charStream.getText(Interval.of(startIndex, charStream.index()));

    ParseException parseException = new ParseException("unexpected character '" + getErrorDisplay(text) + "'"
            + " on line (" + _tokenStartLine + ") position (" + _tokenStartCharPositionInLine + ")",
            _tokenStartCharIndex);
    parseException.initCause(lnvae);
    throw new RuntimeException(parseException);
}

From source file:org.elasticsearch.painless.ErrorHandlingLexer.java

License:Apache License

@Override
public void recover(LexerNoViableAltException lnvae) {
    CharStream charStream = lnvae.getInputStream();
    int startIndex = lnvae.getStartIndex();
    String text = charStream.getText(Interval.of(startIndex, charStream.index()));

    ParseException parseException = new ParseException("Error [" + _tokenStartLine + ":"
            + _tokenStartCharPositionInLine + "]: unexpected character [" + getErrorDisplay(text) + "].",
            _tokenStartCharIndex);/*from w  ww  . j a  v a 2s. com*/
    parseException.initCause(lnvae);
    throw new RuntimeException(parseException);
}