Java Code Examples for org.antlr.v4.runtime.CharStream#LA

The following examples show how to use org.antlr.v4.runtime.CharStream#LA . 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: Session.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Script parse0(CharBuffer cbuf) {
    if (this.isClosed.get()) {
        throw new OrcaException("session is closed");
    }

    CharStream cs = new CharBufferStream(cbuf);
    startTrx();
    
    // check the global statement cache
    Script script = this.getOrca().getCachedStatement(cs.toString());

    // skip leading comments
    skipComments(cs);
    skipSpaces(cs);
    
    // parse it for real if it is not cached
    if (script == null) {
        try {
            if (cs.LA(1) == '.') {
                cs.consume();
                script = this.fishParser.parse(this, cs);
            }
            else {
                script = this.fac.parse(this, cs);
            }
        }
        catch (CompileDdlException x) {
            script = new Script(null, x.nParameters, 100, cs.toString());
        }
        if (_log.isTraceEnabled()) {
            _log.trace("{}-{}: {}", getId(), script.hashCode(), cs.toString());
        }
        if (script.worthCache()) {
            this.getOrca().cacheStatement(script);
        }
    }
    return script;
}
 
Example 3
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 4
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 5
Source File: SemanticPredicates.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static boolean isFollowedByWhiteSpaces(CharStream cs) {
    for (int index = 1, c = cs.LA(index); !('\r' == c || '\n' == c || CharStream.EOF == c); index++, c = cs.LA(index)) {
        if (matches(String.valueOf((char) c), NONSPACES_PATTERN)) {
            return false;
        }
    }

    return true;
}
 
Example 6
Source File: SemanticPredicates.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static boolean isFollowedBy(CharStream cs, char... chars) {
    int c1 = cs.LA(1);

    for (char c : chars) {
        if (c1 == c) {
            return true;
        }
    }

    return false;
}
 
Example 7
Source File: SemanticPredicates.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static boolean isFollowedByJavaLetterInGString(CharStream cs) {
    int c1 = cs.LA(1);

    if ('$' == c1) { // single $ is not a valid identifier
        return false;
    }

    String str1 = String.valueOf((char) c1);

    if (matches(str1, LETTER_AND_LEFTCURLY_PATTERN)) {
        return true;
    }

    if (matches(str1, NONSURROGATE_PATTERN)
            && Character.isJavaIdentifierPart(c1)) {
        return true;
    }

    int c2 = cs.LA(2);
    String str2 = String.valueOf((char) c2);

    if (matches(str1, SURROGATE_PAIR1_PATTERN)
            && matches(str2, SURROGATE_PAIR2_PATTERN)
            && Character.isJavaIdentifierPart(Character.toCodePoint((char) c1, (char) c2))) {

        return true;
    }

    return false;
}