Example usage for org.antlr.v4.runtime Recognizer getInputStream

List of usage examples for org.antlr.v4.runtime Recognizer getInputStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime Recognizer getInputStream.

Prototype

public abstract IntStream getInputStream();

Source Link

Usage

From source file:boa.compiler.listeners.ParserErrorListener.java

License:Apache License

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
        final int charPositionInLine, final String msg, final RecognitionException e) {
    final Token offendingToken = (Token) offendingSymbol;
    error("parser", ((CommonTokenStream) recognizer.getInputStream()).getTokenSource(), offendingSymbol, line,
            charPositionInLine, offendingToken.getStopIndex() - offendingToken.getStartIndex() + 1, msg, e);
}

From source file:cloudlens.parser.ASTError.java

License:Apache License

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

    final String sourceName = recognizer.getInputStream().getSourceName();
    throw new ASTException(
            "Syntax Error: " + sourceName + " line " + line + " character " + charPositionInLine + "\n" + msg);
}

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 w  w  w  .  j  a  v  a2s  . c om*/
    System.err.println();
}

From source file:com.github.jknack.handlebars.internal.HbsErrorReporter.java

License:Apache License

/**
 * Extract lines.//from w  w  w  . j  a  va  2 s  .  c  om
 *
 * @param recognizer A lexer/parser.
 * @return Source lines
 */
private String[] lines(final Recognizer<?, ?> recognizer) {
    IntStream stream = recognizer.getInputStream();
    if (stream instanceof CommonTokenStream) {
        stream = ((CommonTokenStream) stream).getTokenSource().getInputStream();
    }
    final String input;
    if (stream instanceof CharStream) {
        input = ((CharStream) stream).getText(new Interval(0, stream.size()));
    } else {
        input = stream.toString();
    }
    String[] lines = input.split("\n");
    return lines;
}

From source file:com.globalforge.infix.FixRulesLexerErrorListener.java

License:Open Source License

/**
 * @see BaseErrorListener#syntaxError/* w  ww.j  a  va 2  s.  c o  m*/
 */
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
        String msg, RecognitionException e) {
    String logMsg = "Lexer ERROR: line " + line + ":" + charPositionInLine + " at " + offendingSymbol + ": "
            + msg;
    String sourceName = recognizer.getInputStream().getSourceName();
    if ((sourceName != null) && !sourceName.isEmpty()) {
        logMsg = String.format(logMsg + ": sourceName: " + sourceName);
    }
    FixRulesLexerErrorListener.logger.error(logMsg);
    throw new RuntimeException(logMsg);
}

From source file:com.huawei.streaming.cql.semanticanalyzer.parser.CQLErrorListener.java

License:Apache License

/**
 * ????/*from  w  w w . j ava 2s  .  com*/
 */
private String getOffendingSymbol(Recognizer<?, ?> recognizer, Object offendingSymbol, int charPositionInLine) {
    if (null != offendingSymbol) {
        return getOffendingSymbolWithHint(recognizer, offendingSymbol);
    }

    String inputSentence = recognizer.getInputStream().toString();
    return Strings.isNullOrEmpty(inputSentence) ? ""
            : getOffendingSymbolWithoutHint(inputSentence, charPositionInLine);
}

From source file:com.huawei.streaming.cql.semanticanalyzer.parser.CQLErrorListener.java

License:Apache License

/**
 * ????????/*  w ww .j  ava  2  s.  c  om*/
 */
private String getOffendingSymbolWithHint(Recognizer<?, ?> recognizer, Object offendingSymbol) {
    Token token = (Token) offendingSymbol;
    String tokenText = token.getText();
    if (tokenText.equals(SYMBOL_EOF)) {
        List<Token> allTokens = ((org.antlr.v4.runtime.CommonTokenStream) recognizer.getInputStream())
                .getTokens();
        int tokensCount = allTokens.size();
        return (tokensCount < MIN_SIZE_FOR_TOKENS) ? ""
                : allTokens.get(tokensCount - MIN_SIZE_FOR_TOKENS).getText();
    }
    return tokenText;
}

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(" ");
    }// ww w .j ava  2  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:compile.compilersource.AntlrErrorListener.java

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object o, int line, int charPositionInLine, String msg,
        RecognitionException e) {//from  w ww  .  ja  va  2 s . c o  m

    System.out.println("In syntaxError");

    String sourceName = recognizer.getInputStream().getSourceName();
    sourceName = !sourceName.isEmpty() ? sourceName + ": " : "";

    //make some custom error messages for better user code tracaeability
    try {
        if (msg.contains("no viable alternative")) {
            msg = "expected statement or functionDeclaration, but it does not match any of them.";
        } else if (msg.contains("extraneous input")) {
            String errorSubStr = msg.split("extraneous input")[1];
            String errorSubSubStr = errorSubStr.split("expecting")[0];
            msg = "extraneous input " + errorSubSubStr + ". delete the extra token to avoid the error.";
        } else if (msg.contains("mismatched input")) {
            String errorSubStr = msg.split("mismatched input")[1];
            String errorSubSubStr = errorSubStr.split("expecting")[0];
            msg = "Did not expect this token in that order : " + errorSubSubStr;
            if (errorSubStr.contains("Number") && errorSubStr.contains("String")
                    && errorSubStr.contains("Bool")) {
                msg += " . Expected expression tokens such as Number, String, etc.";
            }
            // if msg contains writeln, string.. then expected expression.
        } else if (msg.contains("extraneous input 'end'")) {
            msg = "mismatched 'end' keyword";
        }
    } catch (Exception ex) {
        System.out.println("Error in syntaxError while making descriptive error msg: " + ex.getMessage());
    }

    mainErrorString += /*sourceName+*/"Error at line " + line + ":" + charPositionInLine + " " + msg + "\n";

    this.ui.SearchEditorForPositionThenScrollIfFound(line);
    highlightErrorLine(line);
}

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 + "  ";
    }/*from  ww w  .j a  va 2 s  .com*/
    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();
}