DeclarationErrorListener.java 文件源码

java
阅读 20 收藏 0 点赞 0 评论 0

项目:LaTeXEE 作者:
/**
 * Method, which overrides method from antlr package. This method, as an input, gets the offending
 * mismatched symbol, location of the mismatch and the message returned by the antlr error listener.
 * In our case the line is always 1.
 * With this method we change the incoming error message to something more user friendly
 *
 * @param recognizer antlr parser
 * @param offendingSymbol mismatched symbol
 * @param line line of the mismatch
 * @param charPositionInLine position of the character for the mismatch
 * @param msg error message returned by antlr
 * @param e antlr recognition exception
 */
@Override
   public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, 
        int charPositionInLine, String msg, RecognitionException e) {

    errors = true;

    charPositionInLine++;

    if (msg.contains("extraneous input")) { //example: \declare{syntax={infix,7,"+",l}, meaning=artih1.sum, misc=&abc"}
        String input = getInput(msg);
        String expectedElements = getExpectedElements(msg);
        msg = "Extraneous input: " + input + ", were expecting one of the following: " + expectedElements;
    }
    else if (msg.contains("no viable alternative at input")) { //example: \declare{syntax={infix,7,"+",l}, meaning=artih1.sum, misc={&abc"} (???)
        msg = "Something missing from the declaration, possibly a brace or a key-value pair.";
    }
    else if (msg.contains("missing")) {
        msg = msg.replaceAll("missing", "Missing character:");
    }
    msg = msg.replaceAll("WS", "whitespace");
    msg = msg.replaceAll("<EOF>", "the end of the file");

    Logger.log("Syntax error at character " + charPositionInLine + ": " + msg);

}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号