Example usage for org.antlr.v4.runtime CommonTokenStream getTokenSource

List of usage examples for org.antlr.v4.runtime CommonTokenStream getTokenSource

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonTokenStream getTokenSource.

Prototype

@Override
    public TokenSource getTokenSource() 

Source Link

Usage

From source file:org.smallpearl.compiler.DescriptiveErrorListener.java

License:BSD 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  v  a  2s . c o  m
    System.err.println("^");
}

From source file:org.tinygroup.template.parser.TinyTemplateErrorListener.java

License:GNU General Public License

public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String msg, RecognitionException e) {

    CommonTokenStream tokens = (CommonTokenStream) recognizer.getInputStream();
    String input = tokens.getTokenSource().getInputStream().toString();
    String[] sourceLines = input.split("\r?\n", -1);
    Token offendingToken = (Token) offendingSymbol;

    StringBuilder sb = new StringBuilder(128);
    sb.append("Template parse failed.\n");
    sb.append(recognizer.getInputStream().getSourceName());
    sb.append(':');
    sb.append(line);//from  w  ww . java  2 s.  c  o  m
    sb.append(':');
    sb.append(charPositionInLine);
    sb.append("\nmessage: ");
    sb.append(msg);
    sb.append('\n');
    sb.append(MemorySourceCompiler.getPrettyError(sourceLines, line, charPositionInLine + 1,
            offendingToken.getStartIndex(), offendingToken.getStopIndex(), 5));

    if (e != null) {
        throw new SyntaxErrorException(sb.toString(), line, charPositionInLine, e);
    } else {
        throw new SyntaxErrorException(sb.toString(), line, charPositionInLine);
    }
}