Java Code Examples for org.antlr.v4.runtime.CharStream#seek()

The following examples show how to use org.antlr.v4.runtime.CharStream#seek() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Session.java    From antsdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void skipComments(CharStream cs) {
    int idx = cs.index();
    if (cs.LA(1) == '/') {
        cs.consume();
        if (cs.LA(1) == '*') {
            cs.consume();
            for (;;) {
                int ch = cs.LA(1);
                cs.consume();
                if (ch == IntStream.EOF) break;
                if (ch == '/') {
                    break;
                }
            }
            return;
        }
    }
    cs.seek(idx);
}
 
Example 2
Source File: ANTLRLexerAdaptor.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) {
	this.buffer = buffer;
	this.endOffset = endOffset;

	CharStream in = new CharSequenceCharStream(buffer, endOffset, IntStream.UNKNOWN_SOURCE_NAME);
	in.seek(startOffset);

	ANTLRLexerState state;
	if (startOffset == 0 && initialState == 0) {
		state = getInitialState();
	} else {
		state = toLexerState(initialState);
	}

	applyLexerState(in, state);
	advance();
}
 
Example 3
Source File: ContextDependentFEELLexer.java    From jdmn with Apache License 2.0 5 votes vote down vote up
private void rewind(CharStream inputTape, int offset) {
    if (offset < 0) {
        throw new IllegalArgumentException(String.format("Offset should be positive. Got %d", offset));
    } else if (offset > 0) {
        int rewindIndex = inputTape.index() - offset;
        inputTape.seek(rewindIndex);
    }
}
 
Example 4
Source File: ExprGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
static String getString(TerminalNode rule, Decoder decoder) {
    Token token = rule.getSymbol();
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);
    int len = token.getStopIndex() - token.getStartIndex() - 1;
    IntSupplier supplier = new IntSupplier() {
        int i = 0;
        
        @Override
        public int getAsInt() {
            if (i >= len) {
                return -1;
            }
            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';
                }
            }
            i++;
            return ch;
        }
    };
    String result = decoder.toString(supplier);
    cs.seek(pos);
    return result;
}
 
Example 5
Source File: ExprGenerator.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static byte[] getBytes(TerminalNode rule) {
    Token token = rule.getSymbol();
    byte[] bytes = new byte[token.getStopIndex() - token.getStartIndex() - 1];
    CharStream cs = token.getInputStream();
    int pos = cs.index();
    cs.seek(token.getStartIndex() + 1);
    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;
}
 
Example 6
Source File: GroovyLangLexer.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected void resetAcceptPosition(CharStream input, int index, int line, int charPositionInLine) {
    input.seek(index);
    this.line = line;
    this.charPositionInLine = charPositionInLine;
    this.consume(input);
}
 
Example 7
Source File: PositionAdjustingLexerATNSimulator.java    From beakerx with Apache License 2.0 4 votes vote down vote up
protected void resetAcceptPosition(CharStream input, int index, int line, int charPositionInLine) {
    input.seek(index);
    this.line = line;
    this.charPositionInLine = charPositionInLine;
    consume(input);
}