00001 package edu.ksu.cis.bandera.abstraction.predicate.parser;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 public class ParseException extends Exception {
00014
00015
00016
00017
00018
00019
00020 protected boolean specialConstructor;
00021
00022
00023
00024
00025
00026
00027 public Token currentToken;
00028
00029
00030
00031
00032
00033
00034 public int[][] expectedTokenSequences;
00035
00036
00037
00038
00039
00040
00041 public String[] tokenImage;
00042
00043
00044
00045
00046 protected String eol = System.getProperty("line.separator", "\n");
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 public ParseException() {
00059 super();
00060 specialConstructor = false;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 public ParseException(Token currentTokenVal,
00075 int[][] expectedTokenSequencesVal,
00076 String[] tokenImageVal
00077 )
00078 {
00079 super("");
00080 specialConstructor = true;
00081 currentToken = currentTokenVal;
00082 expectedTokenSequences = expectedTokenSequencesVal;
00083 tokenImage = tokenImageVal;
00084 }
00085 public ParseException(String message) {
00086 super(message);
00087 specialConstructor = false;
00088 }
00089
00090
00091
00092
00093
00094 protected String add_escapes(String str) {
00095 StringBuffer retval = new StringBuffer();
00096 char ch;
00097 for (int i = 0; i < str.length(); i++) {
00098 switch (str.charAt(i))
00099 {
00100 case 0 :
00101 continue;
00102 case '\b':
00103 retval.append("\\b");
00104 continue;
00105 case '\t':
00106 retval.append("\\t");
00107 continue;
00108 case '\n':
00109 retval.append("\\n");
00110 continue;
00111 case '\f':
00112 retval.append("\\f");
00113 continue;
00114 case '\r':
00115 retval.append("\\r");
00116 continue;
00117 case '\"':
00118 retval.append("\\\"");
00119 continue;
00120 case '\'':
00121 retval.append("\\\'");
00122 continue;
00123 case '\\':
00124 retval.append("\\\\");
00125 continue;
00126 default:
00127 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
00128 String s = "0000" + Integer.toString(ch, 16);
00129 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
00130 } else {
00131 retval.append(ch);
00132 }
00133 continue;
00134 }
00135 }
00136 return retval.toString();
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 public String getMessage() {
00149 if (!specialConstructor) {
00150 return super.getMessage();
00151 }
00152 String expected = "";
00153 int maxSize = 0;
00154 for (int i = 0; i < expectedTokenSequences.length; i++) {
00155 if (maxSize < expectedTokenSequences[i].length) {
00156 maxSize = expectedTokenSequences[i].length;
00157 }
00158 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
00159 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
00160 }
00161 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
00162 expected += "...";
00163 }
00164 expected += eol + " ";
00165 }
00166 String retval = "Encountered \"";
00167 Token tok = currentToken.next;
00168 for (int i = 0; i < maxSize; i++) {
00169 if (i != 0) retval += " ";
00170 if (tok.kind == 0) {
00171 retval += tokenImage[0];
00172 break;
00173 }
00174 retval += add_escapes(tok.image);
00175 tok = tok.next;
00176 }
00177 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
00178 if (expectedTokenSequences.length == 1) {
00179 retval += "Was expecting:" + eol + " ";
00180 } else {
00181 retval += "Was expecting one of:" + eol + " ";
00182 }
00183 retval += expected;
00184 return retval;
00185 }
00186 }