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

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

Introduction

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

Prototype

void seek(int index);

Source Link

Document

Set the input cursor to the position indicated by index .

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);
    int j = 0;/*from   www.  j  a va2  s. c om*/
    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.twosigma.beaker.groovy.autocomplete.PositionAdjustingLexerATNSimulator.java

License:Apache License

protected void resetAcceptPosition(CharStream input, int index, int line, int charPositionInLine) {
    input.seek(index);
    this.line = line;
    this.charPositionInLine = charPositionInLine;
    consume(input);//from w w w .  j a  v  a  2s .co m
}

From source file:org.tvl.goworks.editor.go.parser.CurrentMemberContextParserTask.java

License:Open Source License

@Override
@RuleDependency(recognizer = GoParser.class, rule = GoParser.RULE_topLevelDecl, version = 0)
public void parse(ParserTaskManager taskManager, ParseContext parseContext, DocumentSnapshot snapshot,
        Collection<? extends ParserDataDefinition<?>> requestedData, ParserResultHandler results)
        throws InterruptedException, ExecutionException {

    if (requestedData.contains(GoParserDataDefinitions.CURRENT_DECLARATION_CONTEXT)) {
        CurrentDeclarationContextData data = null;
        SnapshotPosition position = parseContext.getPosition();
        if (position != null) {
            int caretOffset = position.getOffset();

            Future<ParserData<List<Anchor>>> result = taskManager.getData(snapshot,
                    GoParserDataDefinitions.DYNAMIC_ANCHOR_POINTS, EnumSet.of(ParserDataOptions.SYNCHRONOUS));

            ParserData<List<Anchor>> anchorsData = result != null ? result.get() : null;
            List<Anchor> anchors = anchorsData.getData();

            TopLevelDeclContext context = null;

            if (anchors != null) {
                Anchor enclosing = null;

                /*/* w ww  . j  av a2 s. co m*/
                * parse the current rule
                */
                for (Anchor anchor : anchors) {
                    if (anchor.getSpan().getStartPosition(snapshot).getOffset() <= caretOffset
                            && anchor.getSpan().getEndPosition(snapshot).getOffset() > caretOffset) {
                        enclosing = anchor;
                    } else if (anchor.getSpan().getStartPosition(snapshot).getOffset() > caretOffset) {
                        break;
                    }
                }

                if (enclosing != null) {
                    CharStream input = new DocumentSnapshotCharStream(snapshot);
                    input.seek(enclosing.getSpan().getStartPosition(snapshot).getOffset());
                    GoLexer lexer = new GoLexer(input);
                    CommonTokenStream tokens = new TaskTokenStream(lexer);
                    final GoParser parser = GoParserFactory.DEFAULT.getParser(tokens,
                            ParserConfiguration.PRECISE);
                    parser.removeErrorListeners();
                    parser.setBuildParseTree(true);
                    context = parser.topLevelDecl();
                }
            }

            data = new CurrentDeclarationContextData(snapshot, context);
        }

        results.addResult(new BaseParserData<>(parseContext,
                GoParserDataDefinitions.CURRENT_DECLARATION_CONTEXT, snapshot, data));
    }
}