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

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

Introduction

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

Prototype

int getStartIndex();

Source Link

Document

The starting character index of the token This method is optional; return -1 if not implemented.

Usage

From source file:controle.grammar.tools.UnderlineListener.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];
    String msgTextArea = Editor.jTextArea1.getText();
    String erro = errorLine + "\n";
    for (int i = 0; i < charPositionInLine; i++) {
        erro = erro + "  ";
    }// w ww .j  av  a  2  s .c  om
    int start = offendingToken.getStartIndex();
    int stop = offendingToken.getStopIndex();
    if (start >= 0 && stop >= 0) {
        for (int i = start; i <= stop; i++) {
            erro = erro + "^";
        }
    }
    // Editor.jTextArea1.setText(msgTextArea + "\n" + erro);
    System.err.println();
}

From source file:de.adrodoc55.minecraft.mpl.ide.autocompletion.AutoCompletionAction.java

License:Open Source License

public AutoCompletionAction(int startIndex, @Nullable Token token) {
    this.startIndex = token != null ? token.getStartIndex() : startIndex;
    this.token = token;
}

From source file:de.adrodoc55.minecraft.mpl.ide.autocompletion.AutoCompletionListener.java

License:Open Source License

protected void visitNode(TerminalNode node) {
    Token token = node.getSymbol();
    if (token == null || index > token.getStopIndex() + 1)
        return;/*from   w  w w.  j  a va  2 s. c  om*/
    if (index < token.getStartIndex() || token.getType() == MplLexer.EOF) {
        token = null;
    }
    result.setToken(token);
    throw new ResultException(result);
}

From source file:de.adrodoc55.minecraft.mpl.ide.gui.MplSyntaxFilter.java

License:Open Source License

public void colorTokens(String text) {
    MplLexer lexer = new MplLexer(new ANTLRInputStream(text));
    loop: while (true) {
        Token token = lexer.nextToken();
        switch (token.getType()) {
        case MplLexer.EOF:
            break loop;
        case MplLexer.ALWAYS_ACTIVE:
        case MplLexer.UNCONDITIONAL:
            styleToken(token, getLowFocusKeywordStyle());
            break;
        case MplLexer.BREAKPOINT:
        case MplLexer.CONDITIONAL:
        case MplLexer.ELSE:
        case MplLexer.IF:
        case MplLexer.IMPORT:
        case MplLexer.INCLUDE:
        case MplLexer.INSTALL:
        case MplLexer.INTERCEPT:
        case MplLexer.INVERT:
        case MplLexer.NOT:
        case MplLexer.NOTIFY:
        case MplLexer.ORIENTATION:
        case MplLexer.PROCESS:
        case MplLexer.PROJECT:
        case MplLexer.SKIP_TOKEN:
        case MplLexer.START:
        case MplLexer.STOP:
        case MplLexer.THEN:
        case MplLexer.UNINSTALL:
        case MplLexer.WAITFOR:
            styleToken(token, getHighFocusKeywordStyle());
            break;
        case MplLexer.IMPULSE:
            styleToken(token, getImpulseStyle());
            break;
        case MplLexer.CHAIN:
            styleToken(token, getChainStyle());
            break;
        case MplLexer.BREAK:
        case MplLexer.CONTINUE:
        case MplLexer.DO:
        case MplLexer.REPEAT:
        case MplLexer.WHILE:
            styleToken(token, getRepeatStyle());
            break;
        case MplLexer.NEEDS_REDSTONE:
            styleToken(token, getNeedsRedstoneStyle());
            break;
        case MplLexer.IDENTIFIER:
            styleToken(token, getIdentifierStyle());
            break;
        case MplLexer.COMMAND:
            styleToken(token, getDefaultStyle());
            Matcher insert = INSERT_PATTERN.matcher(token.getText());
            while (insert.find()) {
                int tokenStart = token.getStartIndex();
                int start = tokenStart + insert.start();
                int stop = token.getStartIndex() + insert.end();
                styleToken(start, stop, getInsertStyle());
            }//  www .jav  a 2s.c om
            break;
        case MplLexer.COMMENT:
            styleToken(token, getCommentStyle());
            break;
        default:
            styleToken(token, getDefaultStyle());
        }
    }
}

From source file:de.adrodoc55.minecraft.mpl.ide.gui.MplSyntaxFilter.java

License:Open Source License

private void styleToken(Token token, AttributeSet style, boolean replace) {
    styleToken(token.getStartIndex(), token.getStopIndex() + 1, style, replace);
}

From source file:de.huberlin.wbi.cuneiform.cfide.editor.SyntaxListener.java

License:Apache License

private void mark(Token t, String styleName) {

    Style style;/*from w ww  . j a  va2 s .c om*/
    int start, len;
    StyledDocument doc;

    if (t == null)
        return;

    if (styleName == null)
        throw new NullPointerException("Style name must not be null.");

    if (styleName.isEmpty())
        throw new RuntimeException("Style name must not be empty.");

    doc = getDoc();

    style = doc.getStyle(styleName);
    if (style == null)
        throw new NullPointerException("A style with name '" + styleName + "' has never been defined.");

    start = t.getStartIndex();
    if (start < 0)
        return;

    len = t.getStopIndex() - start + 1;
    if (len <= 0)
        return;

    doc.setCharacterAttributes(start, len, style, false);
}

From source file:de.interactive_instruments.ShapeChange.SBVR.SbvrErrorInfo.java

License:Open Source License

public void setMetadataFromToken(Token token) {

    this.offendingTextStartIndex = token.getStartIndex();
    this.offendingTextStopIndex = token.getStopIndex();
}

From source file:de.interactive_instruments.ShapeChange.SBVR.SbvrErrorInfo.java

License:Open Source License

public void setMetadataFromContext(ParserRuleContext ctx) {

    Token start = ctx.start;
    Token stop = ctx.stop;//from   w w w .  j a v  a2 s  . c o m

    this.offendingTextStartIndex = start.getStartIndex();
    this.offendingTextStopIndex = stop.getStopIndex();
}

From source file:de.interactive_instruments.ShapeChange.SBVR.SbvrErrorListener.java

License:Open Source License

protected <T extends Token> void underlineError(Recognizer<T, ?> 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.out.println(errorLine);

    for (int i = 0; i < charPositionInLine; i++)
        System.out.print(" ");

    int start = offendingToken.getStartIndex();
    int stop = offendingToken.getStopIndex();

    if (start >= 0 && stop >= 0) {
        for (int i = start; i <= stop; i++)
            System.out.print("^");
    }//from  w ww .ja v a 2 s.  com
    System.out.println();
}

From source file:groovy.ui.text.SmartDocumentFilter.java

License:Apache License

private void parseDocument() throws BadLocationException {
    GroovyLangLexer lexer;/*from ww w.jav a  2  s.c  o  m*/
    try {
        lexer = createLexer(styledDocument.getText(0, styledDocument.getLength()));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    CommonTokenStream tokenStream = new CommonTokenStream(lexer);

    try {
        tokenStream.fill();
    } catch (LexerNoViableAltException | GroovySyntaxError e) {
        // ignore
        return;
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    List<Token> tokenList = tokenStream.getTokens();
    List<Token> tokenListToRender = findTokensToRender(tokenList);

    for (Token token : tokenListToRender) {
        int tokenType = token.getType();

        //                if (token instanceof CommonToken) {
        //                    System.out.println(((CommonToken) token).toString(lexer));
        //                }

        if (EOF == tokenType) {
            continue;
        }

        int tokenStartIndex = token.getStartIndex();
        int tokenStopIndex = token.getStopIndex();
        int tokenLength = tokenStopIndex - tokenStartIndex + 1;

        styledDocument.setCharacterAttributes(tokenStartIndex, tokenLength, findStyleByTokenType(tokenType),
                true);

        if (GStringBegin == tokenType || GStringPart == tokenType) {
            styledDocument.setCharacterAttributes(tokenStartIndex + tokenLength - 1, 1, defaultStyle, true);
        }
    }

    this.latestTokenList = tokenList;
}