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

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

Introduction

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

Prototype

int getCharPositionInLine();

Source Link

Document

The index of the first character of this token relative to the beginning of the line at which it occurs, 0..n-1

Usage

From source file:net.sf.jame.contextfree.parser.Builder.java

License:Open Source License

protected void warning(String message, Token location) {
    System.out.println("[" + location.getLine() + ":" + location.getCharPositionInLine() + "] : " + message);
}

From source file:net.sf.jame.contextfree.parser.Builder.java

License:Open Source License

protected void error(String message, Token location) {
    System.err.println("[" + location.getLine() + ":" + location.getCharPositionInLine() + "] : " + message);
}

From source file:nl.knaw.AntlrUtils.java

License:Apache License

public static String makeTokenTable(Lexer lexer) {
    AsciiTable table = new AsciiTable().setTextAlignment(TextAlignment.LEFT);
    CWC_LongestLine cwc = new CWC_LongestLine();
    table.getRenderer().setCWC(cwc);/*from  w  w w  .ja v a2s .c om*/
    table.addRule();
    table.addRow("Pos", "Text", "Rule", "Next mode", "Token");
    table.addRule();

    Token token;
    do {
        token = lexer.nextToken();
        //      LOG.info(token.toString());
        if (token.getType() != Token.EOF) {
            String pos = token.getLine() + ":" + token.getCharPositionInLine();
            String text = "'" + token.getText() + "'";
            String rule = lexer.getRuleNames()[token.getType() - 1];
            String mode = lexer.getModeNames()[lexer._mode];
            table.addRow(pos, text, rule, mode, token);
        }
    } while (token.getType() != Token.EOF);
    table.addRule();
    table.getContext().setGrid(A7_Grids.minusBarPlusEquals());
    return table.render();
}

From source file:nl.knaw.huc.di.tag.tagml.importer.AnnotationFactory.java

License:Apache License

private String errorPrefix(ParserRuleContext ctx) {
    Token token = ctx.start;
    return format("line %d:%d :", token.getLine(), token.getCharPositionInLine() + 1);
}

From source file:nl.knaw.huc.di.tag.tagml.importer.TAGMLListener.java

License:Apache License

private String errorPrefix(ParserRuleContext ctx, boolean useStopToken) {
    Token token = useStopToken ? ctx.stop : ctx.start;
    return format("line %d:%d :", token.getLine(), token.getCharPositionInLine() + 1);
}

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);
    }/*w  ww.  j av a 2s. co  m*/
}

From source file:org.apache.lucene.expressions.js.JavascriptParserErrorStrategy.java

License:Apache License

/**
 * Ensures the ANTLR parser will throw an exception after the first error
 *
 * @param recognizer the parser being used
 * @param re the original exception from the parser
 *///from  w ww  . j  av  a 2s. c  om
@Override
public void recover(Parser recognizer, RecognitionException re) {
    Token token = re.getOffendingToken();
    String message;

    if (token == null) {
        message = "error " + getTokenErrorDisplay(token);
    } else if (re instanceof InputMismatchException) {
        message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine()
                + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of "
                + re.getExpectedTokens().toString(recognizer.getVocabulary());
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == JavascriptParser.EOF) {
            message = "unexpected end of expression";
        } else {
            message = "invalid sequence of tokens near " + getTokenErrorDisplay(token) + " on line ("
                    + token.getLine() + ") position (" + token.getCharPositionInLine() + ")";
        }
    } else {
        message = " unexpected token near " + getTokenErrorDisplay(token) + " on line (" + token.getLine()
                + ") position (" + token.getCharPositionInLine() + ")";
    }

    ParseException parseException = new ParseException(message, token.getStartIndex());
    parseException.initCause(re);
    throw new RuntimeException(parseException);
}

From source file:org.apache.lucene.expressions.js.JavascriptParserErrorStrategy.java

License:Apache License

/**
 * Ensures the ANTLR parser will throw an exception after the first error
 *
 * @param recognizer the parser being used
 * @return no actual return value//  www  . jav  a2s .  c  o  m
 * @throws RecognitionException not used as a ParseException wrapped in a RuntimeException is thrown instead
 */
@Override
public Token recoverInline(Parser recognizer) throws RecognitionException {
    Token token = recognizer.getCurrentToken();
    String message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine()
            + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of "
            + recognizer.getExpectedTokens().toString(recognizer.getVocabulary());
    ParseException parseException = new ParseException(message, token.getStartIndex());
    throw new RuntimeException(parseException);
}

From source file:org.apache.sysml.parser.common.CommonSyntacticValidator.java

License:Apache License

protected void notifyErrorListeners(String message, Token op) {
    if (!DMLScript.VALIDATOR_IGNORE_ISSUES) {
        errorListener.validationError(op.getLine(), op.getCharPositionInLine(), message);
    }//  www .  java2s  .  c om
}

From source file:org.apache.sysml.parser.common.CommonSyntacticValidator.java

License:Apache License

protected void raiseWarning(String message, Token op) {
    errorListener.validationWarning(op.getLine(), op.getCharPositionInLine(), message);
}