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:com.basho.contact.ContactErrorListener.java

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

From source file:com.koltem.filetype.verilog.editor.VerilogErrorListener.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];
    System.err.println(errorLine);
    for (int i = 0; i < charPositionInLine; i++) {
        System.err.print(" ");
    }// w w  w  . j  a v  a2 s. c o  m
    int start = offendingToken.getStartIndex();
    int stop = offendingToken.getStopIndex();
    if (start >= 0 && stop >= 0) {
        for (int i = start; i <= stop; i++) {
            System.err.print("^");
        }
    }
    System.err.println();
}

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 w w  .j  ava2 s  .  c  o  m
    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.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("^");
    }//w w  w.j a v a  2  s .c o m
    System.out.println();
}

From source file:edu.clemson.cs.r2jt.absynnew.UnderliningErrorListener.java

License:Open Source License

protected void underlineError(Recognizer recognizer, Token offendingToken, int line, int charPositionInLine) {
    String input;/* w  w w.  java 2 s .  com*/
    if (recognizer == null) {
        input = offendingToken.getTokenSource().getInputStream().toString();
    } else {
        CommonTokenStream src = (CommonTokenStream) recognizer.getInputStream();
        input = src.getTokenSource().getInputStream().toString();
    }
    String[] lines = input.split("\n");
    String errorLine = lines[line - 1].replaceAll("\t", " ");

    System.err.println(errorLine);

    for (int i = 0; i < charPositionInLine; i++) {
        System.err.print(" ");
    }
    System.err.print("^");
    System.exit(1);
}

From source file:edu.clemson.cs.rsrg.statushandling.AntlrErrorListener.java

License:Open Source License

/**
 * <p>This is thrown when we encounter an syntax error when
 * parsing the input string.</p>//from  w w w  . j  a  v a2 s. com
 *
 * @param recognizer A recognizer provided by the ANTLR4.
 * @param offendingSymbol The offending token.
 * @param line The line number where the error occurred.
 * @param charPositionInLine The position in the line where the error occurred.
 * @param msg The message to be displayed.
 * @param e The exception thrown.
 */
@Override
public final void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
        int charPositionInLine, String msg, RecognitionException e) {
    ResolveToken offendingToken = (ResolveToken) offendingSymbol;
    String input;
    if (recognizer == null) {
        input = offendingToken.getTokenSource().getInputStream().toString();
    } else {
        CommonTokenStream src = (CommonTokenStream) recognizer.getInputStream();
        input = src.getTokenSource().getInputStream().toString();
    }
    String[] lines = input.split("\n");
    String errorLine = lines[line - 1].replaceAll("\t", " ");

    String errorMsg = buildErrorMsg(offendingToken, charPositionInLine, errorLine, msg);
    myStatusHandler.error(null, errorMsg);
}

From source file:edu.clemson.cs.rsrg.statushandling.AntlrParserErrorListener.java

License:Open Source License

/**
 * <p>This is thrown when we encounter an syntax error when
 * parsing the input string.</p>/*from w w w.j av  a 2 s .c  o  m*/
 *
 * @param recognizer A recognizer provided by the ANTLR4.
 * @param offendingSymbol The offending token.
 * @param line The line number where the error occurred.
 * @param charPositionInLine The position in the line where the error occurred.
 * @param msg The message to be displayed.
 * @param e The exception thrown.
 */
@Override
public final void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
        int charPositionInLine, String msg, RecognitionException e) {
    ResolveToken offendingToken = (ResolveToken) offendingSymbol;
    String input;
    if (recognizer == null) {
        input = offendingToken.getTokenSource().getInputStream().toString();
    } else {
        CommonTokenStream src = (CommonTokenStream) recognizer.getInputStream();
        input = src.getTokenSource().getInputStream().toString();
    }
    String[] lines = input.split("\n");
    String errorLine = lines[line - 1].replaceAll("\t", " ");

    // Obtain the location from the token if it is not null
    Location location = null;
    if (offendingToken != null) {
        location = offendingToken.getLocation();
    }

    String errorMsg = buildErrorMsg(charPositionInLine, errorLine, msg);
    myStatusHandler.error(location, errorMsg);
}

From source file:jetbrick.template.parser.JetTemplateErrorListener.java

License:Open Source License

@Override
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);//ww  w . j  a v a2s  .c  o  m
    sb.append("\nmessage: ");
    sb.append(msg);
    sb.append('\n');
    sb.append(StringUtils.getPrettyError(sourceLines, line, charPositionInLine + 1,
            offendingToken.getStartIndex(), offendingToken.getStopIndex(), 5));

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

From source file:kr.ac.korea.dbserver.parser.SQLErrorListener.java

License:Apache 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();
    Token token = (Token) offendingSymbol;
    String[] lines = StringUtils.splitPreserveAllTokens(input, '\n');
    String errorLine = lines[line - 1];

    throw new SQLParseError(token, line, charPositionInLine, msg, errorLine);
}

From source file:org.apache.tajo.parser.sql.SQLErrorListener.java

License:Apache 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();
    Token token = (Token) offendingSymbol;
    String[] lines = StringUtils.splitPreserveAllTokens(input, '\n');
    String errorLine = lines[line - 1];

    String simpleMessage = "syntax error at or near \"" + token.getText() + "\"";
    throw new SQLParseError(token, line, charPositionInLine, simpleMessage, errorLine);
}