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

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

Introduction

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

Prototype

int getStopIndex();

Source Link

Document

The last character index of the token.

Usage

From source file:boa.compiler.listeners.ParserErrorListener.java

License:Apache License

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
        final int charPositionInLine, final String msg, final RecognitionException e) {
    final Token offendingToken = (Token) offendingSymbol;
    error("parser", ((CommonTokenStream) recognizer.getInputStream()).getTokenSource(), offendingSymbol, line,
            charPositionInLine, offendingToken.getStopIndex() - offendingToken.getStartIndex() + 1, msg, e);
}

From source file:br.beholder.memelang.model.visitor.TreePrinterListener.java

@Override
public void exitEveryRule(ParserRuleContext ctx) {
    if (ctx.getChildCount() > 0) {
        Token positionToken = ctx.getStart();
        if (positionToken != null) {
            builder.append(" [line ");
            builder.append(positionToken.getLine());
            builder.append(", offset ");
            builder.append(positionToken.getStartIndex());
            builder.append(':');
            builder.append(positionToken.getStopIndex());
            builder.append("])");
        } else {//www  .  jav  a  2s  .co m
            builder.append(')');
        }
    }
}

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  w w w.  j av a  2 s .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.basho.contact.ContactErrorListener.java

License:Apache License

protected void underlineError(Recognizer recognizer, Token offendingToken, int line, int charPositionInLine) {
    CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
    String input = tokens.getTokenSource().getInputStream().toString();
    String lines[] = input.split("\n");
    String errorLine = lines[line - 1];
    System.err.println(errorLine);
    for (int i = 0; i < charPositionInLine; i++)
        System.err.print(" ");
    int start = offendingToken.getStartIndex();
    int stop = offendingToken.getStopIndex();
    if (start >= 0 && stop >= 0) {
        for (int i = start; i <= stop; i++)
            System.err.print("^");
    }//from  w  w  w .j  a va  2  s  . co  m
    System.err.println();
}

From source file:com.bosch.example.rsql.suggestion.RsqlSuggestionHelper.java

License:Open Source License

public static SuggestionContext parse(final String rsql, final int cursorPosition) {
    CharStream inputCharStream;//from   w w  w  .  j  av  a2 s.  c  o m
    try {
        inputCharStream = new ANTLRInputStream(new StringReader(rsql));
    } catch (final IOException e) {
        throw Throwables.propagate(e);
    }
    final TokenSource tokenSource = new RsqlLexer(inputCharStream);
    final TokenStream inputTokenStream = new CommonTokenStream(tokenSource);
    final RsqlParser parser = new RsqlParser(inputTokenStream);
    final SuggestionErrorListener errorListener = new SuggestionErrorListener();
    parser.addErrorListener(errorListener);
    final SuggestionParseTreeListener parserListener = new SuggestionParseTreeListener();
    parser.addParseListener(parserListener);

    parser.expr();

    final SuggestionContext suggestionContext = new SuggestionContext();

    suggestionContext.syntaxError = errorListener.isErrorOccurred();
    if (errorListener.isErrorOccurred()) {
        final SyntaxErrorContext errorContext = new SyntaxErrorContext();
        final Token errorToken = errorListener.getErrorToken().getToken();
        errorContext.tokenStart = errorToken.getStartIndex();
        errorContext.tokenEnd = errorToken.getStopIndex();

        final IntervalSet expectedTokenType = errorListener.getErrorToken().getExpectedTokenType();
        expectedTokenType.getIntervals().forEach(interval -> {
            final int start = interval.a;
            final int end = interval.b;
            for (int index = start; index <= end; index++) {
                final String symbolicName = parser.getVocabulary().getSymbolicName(index);
                errorContext.suggestions.addAll(SuggestionMap.getSuggestions(symbolicName));
            }
        });
        suggestionContext.syntaxErrorContext = errorContext;
    }

    final Optional<Token> tokenAtCursorPosition = parserListener.getTokenAtCursorPosition(cursorPosition);
    if (tokenAtCursorPosition.isPresent()) {
        final CursorPositionSuggestionContext cursorPositionSuggestionContext = new CursorPositionSuggestionContext();
        cursorPositionSuggestionContext.currentCursor = cursorPosition;
        cursorPositionSuggestionContext.tokenStart = tokenAtCursorPosition.get().getStartIndex();
        cursorPositionSuggestionContext.tokenEnd = tokenAtCursorPosition.get().getStopIndex();
        cursorPositionSuggestionContext.suggestions = SuggestionMap
                .getSuggestions(parser.getVocabulary().getSymbolicName(tokenAtCursorPosition.get().getType()));
        suggestionContext.cursorPositionContext = cursorPositionSuggestionContext;
    }

    return suggestionContext;
}

From source file:com.cisco.yangide.core.parser.YangParserModelListener.java

License:Open Source License

private void updateNamedNode(ASTNamedNode astNode, ParseTree treeNode) {
    updateNodePosition(astNode, treeNode);
    for (int i = 0; i < treeNode.getChildCount(); ++i) {
        if (treeNode.getChild(i) instanceof StringContext) {
            final StringContext context = (StringContext) treeNode.getChild(i);
            if (context != null) {
                Token token = context.getStart();
                astNode.setNameStartPosition(token.getStartIndex());
                astNode.setNameLength(token.getStopIndex() - token.getStartIndex() + 1);
                astNode.setLineNumber(token.getLine());
                astNode.setName(stringFromStringContext(context));
            }/* ww w. ja v a2 s .c o  m*/
        }
    }
}

From source file:com.cisco.yangide.core.parser.YangParserUtil.java

License:Open Source License

public static void validateYangContext(YangContext context, IYangValidationListener validationListener) {
    final ParseTreeWalker walker = new ParseTreeWalker();
    final YangModelBasicValidationListener yangModelParser = new YangModelBasicValidationListener();
    try {//  w  ww.  jav a2  s  .co m
        walker.walk(yangModelParser, context);
    } catch (YangValidationException e) {
        if (validationListener != null) {
            int lineNumber = -1;
            int charStart = 0;
            int charEnd = 0;
            if (e.getContext() instanceof ParserRuleContext) {
                Token token = ((ParserRuleContext) e.getContext()).getStart();
                lineNumber = token.getLine();
                charStart = token.getStartIndex();
                charEnd = token.getStopIndex() + 1;
            }
            validationListener.validationError(e.getMessage(), lineNumber, charStart, charEnd);
        }
    }
}

From source file:com.github.drrb.rust.netbeans.parsing.RustLexUtils.java

License:Open Source License

public static OffsetRange offsetRangeBetween(Token start, Token end) {
    return range(start.getStartIndex(), end.getStopIndex() + 1);
}

From source file:com.jeroensteenbeeke.andalite.core.Location.java

License:Open Source License

@Nonnull
public static Location from(@Nonnull TerminalNode node) {
    Token symbol = node.getSymbol();

    return new Location(symbol.getStartIndex(), symbol.getStopIndex() + 1);
}

From source file:com.koltem.filetype.verilog.editor.VerilogErrorListener.java

protected void underlineError(Recognizer recognizer, Token offendingToken, int line, int charPositionInLine) {
    CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
    String input = tokens.getTokenSource().getInputStream().toString();
    String[] lines = input.split("\n");
    String errorLine = lines[line - 1];
    System.err.println(errorLine);
    for (int i = 0; i < charPositionInLine; i++) {
        System.err.print(" ");
    }/*from   w w  w . j ava 2 s. c  o  m*/
    int start = offendingToken.getStartIndex();
    int stop = offendingToken.getStopIndex();
    if (start >= 0 && stop >= 0) {
        for (int i = start; i <= stop; i++) {
            System.err.print("^");
        }
    }
    System.err.println();
}