Example #1
0
 private static String getErrorMessage(BaseRecognizer recognizer, RecognitionException e) {
   String[] tokenNames = recognizer.getTokenNames();
   String hdr = "Line " + e.line + ":" + e.charPositionInLine;
   String msg = recognizer.getErrorMessage(e, tokenNames);
   return hdr + " " + msg;
 }
 public void recover(BaseRecognizer br, IntStream input, RecognitionException re) {
   br.recover(input, re);
 }
 private static InvalidExpressionException generateException(
     String exp, BaseRecognizer antlr, RecognitionException re, Location l) {
   StringBuilder errorMsg = new StringBuilder();
   String[] names = antlr.getTokenNames();
   if (re.token != null && re.token.getType() == ExpressionParser.EOF) {
     errorMsg.append("unexpected end of expression");
   } else if (re instanceof MismatchedTokenException) {
     MismatchedTokenException mte = (MismatchedTokenException) re;
     String txt;
     String expecting;
     // bleh same exception class has different fields set depending on
     // where it occurred
     if (antlr instanceof Lexer) {
       Lexer lexer = (Lexer) antlr;
       txt = lexer.getCharErrorDisplay(re.c);
       expecting = lexer.getCharErrorDisplay(mte.expecting);
     } else {
       if (mte.token.getText() != null) {
         txt = "'" + mte.token.getText() + "'";
       } else {
         txt = names[mte.token.getType()];
       }
       if (mte.expecting < names.length && mte.expecting >= 0) {
         expecting = ExpressionParser.FRIENDLY_NAMES[mte.expecting];
       } else {
         expecting = null;
       }
     }
     if (expecting != null) {
       errorMsg.append("expecting ").append(expecting).append(", found ").append(txt);
     } else {
       errorMsg.append("unexpected token: ").append(txt);
     }
   } else if (re instanceof NoViableAltException) {
     Token token = re.token;
     if (token == null) {
       char ch = exp.charAt(re.charPositionInLine);
       if (re.charPositionInLine == 0 && ch == '{') {
         errorMsg.append("unclosed brace");
       } else {
         errorMsg.append("unexpected token: ").append("'").append(ch).append("'");
       }
     } else {
       errorMsg
           .append("unexpected token: ")
           .append(ExpressionParser.FRIENDLY_NAMES[token.getType()]);
     }
   } else {
     errorMsg.append(antlr.getErrorMessage(re, names));
   }
   if (re.line > 1) {
     errorMsg
         .append(" at line ")
         .append(re.line)
         .append(", column ")
         .append(re.charPositionInLine + 1);
   } else {
     errorMsg.append(" at column ").append(re.charPositionInLine + 1);
   }
   errorMsg.append(" of expression: ");
   errorMsg.append(exp);
   return new InvalidExpressionException(errorMsg.toString(), l);
 }
 public void reportError(BaseRecognizer br, RecognitionException re) {
   br.reportError(re);
 }