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:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java

License:Open Source License

public void enterE23(@NotNull RParser.E23Context ctx) {

    Interval sourceInterval = ctx.getSourceInterval();
    Token firstToken = tokens.get(sourceInterval.a);
    int lineStart = firstToken.getStartIndex();
    Token lastToken = tokens.get(sourceInterval.b);
    int lineEnd = lastToken.getStopIndex() + 1 - lineStart;
    // Add to the editor folding action if enabled in the preferences!
    if (store.getBoolean("FOR_LOOP_FOLDING")) {
        startStop.add(lineStart + "," + lineEnd);
    }/*from   ww  w .j a v a 2s  .  c  o m*/
}

From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java

License:Open Source License

public void enterE24(@NotNull RParser.E24Context ctx) {

    Interval sourceInterval = ctx.getSourceInterval();
    Token firstToken = tokens.get(sourceInterval.a);
    int lineStart = firstToken.getStartIndex();
    Token lastToken = tokens.get(sourceInterval.b);
    int lineEnd = lastToken.getStopIndex() + 1 - lineStart;
    // Add to the editor folding action if enabled in the preferences!
    if (store.getBoolean("WHILE_LOOP_FOLDING")) {
        startStop.add(lineStart + "," + lineEnd);
    }/*from   www .  ja  v  a2s  . c om*/
}

From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.editor.RBaseListen.java

License:Open Source License

public void enterE25(@NotNull RParser.E25Context ctx) {

    Interval sourceInterval = ctx.getSourceInterval();
    Token firstToken = tokens.get(sourceInterval.a);
    int lineStart = firstToken.getStartIndex();
    Token lastToken = tokens.get(sourceInterval.b);
    int lineEnd = lastToken.getStopIndex() + 1 - lineStart;
    // Add to the editor folding action if enabled in the preferences!
    if (store.getBoolean("REPEAT_LOOP_FOLDING")) {
        startStop.add(lineStart + "," + lineEnd);
    }/* www .  ja va  2s .  co  m*/
}

From source file:nl.lxtreme.libtdl.grammar.adv.AdvTdlSemanticAnalyzer.java

License:Apache License

private void validateDeclaredTerm(Token term) {
    String name = normalizeName(term.getText());
    if (!m_declarations.containsKey(name)) {
        int offset = term.getStartIndex();
        int length = term.getStopIndex() - offset;
        String msg = name + " is not declared";

        Marker marker = new MarkerBuilder().setCategory(Category.SEMANTIC).setType(Type.ERROR) //
                .setLocation(offset, length, term.getLine(), term.getCharPositionInLine()) //
                .setDescription(msg).build();

        m_problemReporter.report(marker);
    }/*from  www  . ja  v  a 2  s .c  om*/
}

From source file:no.ssb.vtl.script.support.SyntaxErrorListener.java

License:Apache License

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int startLine, int startColumn,
        String msg, RecognitionException e) {
    VTLScriptException vtlScriptException;
    // Use the context from the RecognitionException if available.
    if (e != null && e.getCtx() != null) {
        vtlScriptException = new VTLScriptException(msg, (ParserRuleContext) e.getCtx());
    } else {//from  w  ww .  j  a  va2 s  .c o m
        int stopColumn = startColumn;
        if (offendingSymbol instanceof Token) {
            Token symbol = (Token) offendingSymbol;
            int start = symbol.getStartIndex();
            int stop = symbol.getStopIndex();
            if (start >= 0 && stop >= 0) {
                stopColumn = startColumn + (stop - start) + 1;
            }
            vtlScriptException = new VTLScriptException(msg, startLine, startColumn, startLine, stopColumn);
        } else {
            vtlScriptException = new VTLScriptException(msg, startLine, startColumn);
        }
    }
    errorConsumer.accept(vtlScriptException);
}

From source file:no.ssb.vtl.test.junit.GrammarRule.java

License:Apache License

/**
 * Parse an expression starting from the given <b>ANTLR rule</b>
 * <p>/*  w w  w .  j  a  v  a  2s .c o m*/
 * In order to get the Rule, use the {@link #withRule(String)} method.
 *
 * @param expression the expression to parse.
 * @param rule       the rule to start from.
 *                   @param diagnostic {@link DiagnosticErrorListener} will be used if true.
 * @return the resulting parse tree.
 * @throws Exception if the expression failed to parse.
 */
public ParserRuleContext parse(String expression, Rule rule, boolean diagnostic) throws Exception {
    Multimap<Integer, String> messages = LinkedListMultimap.create();

    LexerInterpreter lexerInterpreter = grammar.createLexerInterpreter(new ANTLRInputStream(expression));
    GrammarParserInterpreter parserInterpreter = grammar
            .createGrammarParserInterpreter(new CommonTokenStream(lexerInterpreter));

    BaseErrorListener errorListener;
    if (diagnostic) {
        errorListener = new DiagnosticErrorListener();
    } else {
        errorListener = new ConsoleErrorListener();
    }

    BaseErrorListener ruleErrorReporter = new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, org.antlr.v4.runtime.RecognitionException e) {
            int startLine = line, stopLine = line;
            int startColumn = charPositionInLine, stopColumn = charPositionInLine;
            if (offendingSymbol instanceof Token) {
                Token symbol = (Token) offendingSymbol;
                int start = symbol.getStartIndex();
                int stop = symbol.getStopIndex();
                if (start >= 0 && stop >= 0) {
                    stopColumn = startColumn + (stop - start) + 1;
                }
            }

            messages.put(stopLine,
                    String.format("at [%4s:%6s]:\t%s (%s)\n", String.format("%d,%d", startLine, stopLine),
                            String.format("%d,%d", startColumn, stopColumn), msg,
                            Optional.ofNullable(e).map(ex -> ex.getClass().getSimpleName()).orElse("null")));
        }
    };

    parserInterpreter.setErrorHandler(new GrammarParserInterpreter.BailButConsumeErrorStrategy());
    lexerInterpreter.removeErrorListeners();
    parserInterpreter.removeErrorListeners();

    lexerInterpreter.addErrorListener(errorListener);
    parserInterpreter.addErrorListener(errorListener);
    lexerInterpreter.addErrorListener(ruleErrorReporter);
    parserInterpreter.addErrorListener(ruleErrorReporter);

    ParserRuleContext parse = parserInterpreter.parse(rule.index);

    if (!messages.isEmpty()) {

        StringBuilder expressionWithErrors = new StringBuilder();
        LineNumberReader expressionReader = new LineNumberReader(new StringReader(expression));
        String line;
        while ((line = expressionReader.readLine()) != null) {
            int lineNumber = expressionReader.getLineNumber();
            expressionWithErrors.append(String.format("\t%d:%s%n", lineNumber, line));
            if (messages.containsKey(lineNumber)) {
                expressionWithErrors.append(String.format("%n"));
                for (String message : messages.get(lineNumber)) {
                    expressionWithErrors.append(message);
                }
            }
        }
        throw new Exception(
                String.format("errors parsing expression:%n%n%s%n", expressionWithErrors.toString()));
    }

    return parse;
}

From source file:org.apache.hive.hplsql.Exec.java

License:Apache License

String getText(ParserRuleContext ctx, Token start, Token stop) {
    return ctx.start.getInputStream()
            .getText(new org.antlr.v4.runtime.misc.Interval(start.getStartIndex(), stop.getStopIndex()));
}

From source file:org.apache.hive.hplsql.Exec.java

License:Apache License

/**
 * Append the text preserving the formatting (space symbols) between tokens
 *//*from   ww w.  j av a2  s. c  o  m*/
void append(StringBuilder str, String appendStr, Token start, Token stop) {
    String spaces = start.getInputStream()
            .getText(new org.antlr.v4.runtime.misc.Interval(start.getStartIndex(), stop.getStopIndex()));
    spaces = spaces.substring(start.getText().length(), spaces.length() - stop.getText().length());
    str.append(spaces);
    str.append(appendStr);
}

From source file:org.eclipse.titan.common.parsers.cfg.CfgLocation.java

License:Open Source License

/**
 * Constructor for ANTLR v4 tokens/*from   w  w w  . ja  v a 2  s. c om*/
 * @param aFile the parsed file
 * @param aStartToken the 1st token, its line and start position will be used for the location
 *                  NOTE: start position is the column index of the tokens 1st character.
 *                        Column index starts with 0.
 * @param aEndToken the last token, its end position will be used for the location.
 *                  NOTE: end position is the column index after the token's last character.
 */
public CfgLocation(final IFile aFile, final Token aStartToken, final Token aEndToken) {
    setLocation(aFile, aStartToken.getLine(), aStartToken.getStartIndex(), aEndToken.getStopIndex() + 1);
}

From source file:org.eclipse.titan.designer.AST.ASN1.Block.java

License:Open Source License

public Block(final Token token) {
    if (token instanceof TokenWithIndexAndSubTokens) {
        tokenList = ((TokenWithIndexAndSubTokens) token).getSubTokens();
        final IFile sourceFile = ((TokenWithIndexAndSubTokens) token).getSourceFile();
        setLocation(new Location(sourceFile, token.getLine(), token.getStartIndex(), token.getStopIndex()));
    } else {//www . j a va  2s .c  o  m
        setLocation(NULL_Location.INSTANCE);
        tokenList = ((TokenWithIndexAndSubTokens) token).getSubTokens();
    }
}