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

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

Introduction

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

Prototype

int getLine();

Source Link

Document

The line number on which the 1st character of this token was matched, line=1..n

Usage

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:net.sourceforge.pmd.cpd.SwiftTokenizer.java

License:BSD License

@Override
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
    StringBuilder buffer = sourceCode.getCodeBuffer();

    try {/* ww w .  j av  a  2s  .co  m*/
        ANTLRInputStream ais = new ANTLRInputStream(buffer.toString());
        SwiftLexer lexer = new SwiftLexer(ais);

        lexer.removeErrorListeners();
        lexer.addErrorListener(new ErrorHandler());
        Token token = lexer.nextToken();

        while (token.getType() != Token.EOF) {
            if (token.getChannel() != Lexer.HIDDEN) {
                TokenEntry tokenEntry = new TokenEntry(token.getText(), sourceCode.getFileName(),
                        token.getLine());

                tokenEntries.add(tokenEntry);
            }
            token = lexer.nextToken();
        }
    } catch (ANTLRSyntaxError err) {
        // Wrap exceptions of the Swift tokenizer in a TokenMgrError, so
        // they are correctly handled
        // when CPD is executed with the '--skipLexicalErrors' command line
        // option
        throw new TokenMgrError("Lexical error in file " + sourceCode.getFileName() + " at line "
                + err.getLine() + ", column " + err.getColumn() + ".  Encountered: " + err.getMessage(),
                TokenMgrError.LEXICAL_ERROR);
    } finally {
        tokenEntries.add(TokenEntry.getEOF());
    }
}

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);//w  w w. j  av  a2  s .  co  m
    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);
    }/*from   w  ww  . j a  v a 2 s .  c o  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  w  w . j a v a  2  s  . 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//from  www.ja v a 2s  .  co  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);
    }/*from ww  w . ja v  a 2s  .  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);
}