00001
00002 package edu.tum.cs.logic.parser;
00003
00013 public @SuppressWarnings("all") class ParseException extends Exception {
00014
00027 public ParseException(Token currentTokenVal,
00028 int[][] expectedTokenSequencesVal,
00029 String[] tokenImageVal
00030 )
00031 {
00032 super("");
00033 specialConstructor = true;
00034 currentToken = currentTokenVal;
00035 expectedTokenSequences = expectedTokenSequencesVal;
00036 tokenImage = tokenImageVal;
00037 }
00038
00049 public ParseException() {
00050 super();
00051 specialConstructor = false;
00052 }
00053
00054 public ParseException(String message) {
00055 super(message);
00056 specialConstructor = false;
00057 }
00058
00064 protected boolean specialConstructor;
00065
00071 public Token currentToken;
00072
00078 public int[][] expectedTokenSequences;
00079
00085 public String[] tokenImage;
00086
00097 public String getMessage() {
00098 if (!specialConstructor) {
00099 return super.getMessage();
00100 }
00101 StringBuffer expected = new StringBuffer();
00102 int maxSize = 0;
00103 for (int i = 0; i < expectedTokenSequences.length; i++) {
00104 if (maxSize < expectedTokenSequences[i].length) {
00105 maxSize = expectedTokenSequences[i].length;
00106 }
00107 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
00108 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
00109 }
00110 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
00111 expected.append("...");
00112 }
00113 expected.append(eol).append(" ");
00114 }
00115 String retval = "Encountered \"";
00116 Token tok = currentToken.next;
00117 for (int i = 0; i < maxSize; i++) {
00118 if (i != 0) retval += " ";
00119 if (tok.kind == 0) {
00120 retval += tokenImage[0];
00121 break;
00122 }
00123 retval += add_escapes(tok.image);
00124 tok = tok.next;
00125 }
00126 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
00127 retval += "." + eol;
00128 if (expectedTokenSequences.length == 1) {
00129 retval += "Was expecting:" + eol + " ";
00130 } else {
00131 retval += "Was expecting one of:" + eol + " ";
00132 }
00133 retval += expected.toString();
00134 return retval;
00135 }
00136
00140 protected String eol = System.getProperty("line.separator", "\n");
00141
00147 protected String add_escapes(String str) {
00148 StringBuffer retval = new StringBuffer();
00149 char ch;
00150 for (int i = 0; i < str.length(); i++) {
00151 switch (str.charAt(i))
00152 {
00153 case 0 :
00154 continue;
00155 case '\b':
00156 retval.append("\\b");
00157 continue;
00158 case '\t':
00159 retval.append("\\t");
00160 continue;
00161 case '\n':
00162 retval.append("\\n");
00163 continue;
00164 case '\f':
00165 retval.append("\\f");
00166 continue;
00167 case '\r':
00168 retval.append("\\r");
00169 continue;
00170 case '\"':
00171 retval.append("\\\"");
00172 continue;
00173 case '\'':
00174 retval.append("\\\'");
00175 continue;
00176 case '\\':
00177 retval.append("\\\\");
00178 continue;
00179 default:
00180 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
00181 String s = "0000" + Integer.toString(ch, 16);
00182 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
00183 } else {
00184 retval.append(ch);
00185 }
00186 continue;
00187 }
00188 }
00189 return retval.toString();
00190 }
00191
00192 }