1 2 package org.objectweb.jonas_ejb.deployment.ejbql; 3 4 16 public class ParseException extends Exception { 17 18 30 public ParseException(Token currentTokenVal, 31 int[][] expectedTokenSequencesVal, 32 String [] tokenImageVal 33 ) 34 { 35 super(""); 36 specialConstructor = true; 37 currentToken = currentTokenVal; 38 expectedTokenSequences = expectedTokenSequencesVal; 39 tokenImage = tokenImageVal; 40 } 41 42 51 52 public ParseException() { 53 super(); 54 specialConstructor = false; 55 } 56 57 public ParseException(String message) { 58 super(message); 59 specialConstructor = false; 60 } 61 62 67 protected boolean specialConstructor; 68 69 74 public Token currentToken; 75 76 81 public int[][] expectedTokenSequences; 82 83 88 public String [] tokenImage; 89 90 100 public String getMessage(String query) { 101 if (!specialConstructor) { 102 return super.getMessage(); 103 } 104 String expected = ""; 105 int maxSize = 0; 106 for (int i = 0; i < expectedTokenSequences.length; i++) { 107 if (maxSize < expectedTokenSequences[i].length) { 108 maxSize = expectedTokenSequences[i].length; 109 } 110 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 111 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 112 } 113 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 114 expected += "..."; 115 } 116 expected += eol + " "; 117 } 118 String retval = "Encountered \""; 119 Token tok = currentToken.next; 120 for (int i = 0; i < maxSize; i++) { 121 if (i != 0) retval += " "; 122 if (tok.kind == 0) { 123 retval += tokenImage[0]; 124 break; 125 } 126 retval += add_escapes(tok.image); 127 tok = tok.next; 128 } 129 if ((query==null) || ("".equals(query))) { 132 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 133 } else { 134 retval += "\" after \""+query.substring(0,currentToken.next.beginColumn-1)+"\""+ eol; 136 retval += "in \""+query+"\""; 137 } 138 retval += "." + eol; 140 if (expectedTokenSequences.length == 1) { 141 retval += "Was expecting:" + eol + " "; 142 } else { 143 retval += "Was expecting one of:" + eol + " "; 144 } 145 retval += expected; 146 return retval; 147 } 148 149 152 protected String eol = System.getProperty("line.separator", "\n"); 153 154 159 protected String add_escapes(String str) { 160 StringBuffer retval = new StringBuffer (); 161 char ch; 162 for (int i = 0; i < str.length(); i++) { 163 switch (str.charAt(i)) 164 { 165 case 0 : 166 continue; 167 case '\b': 168 retval.append("\\b"); 169 continue; 170 case '\t': 171 retval.append("\\t"); 172 continue; 173 case '\n': 174 retval.append("\\n"); 175 continue; 176 case '\f': 177 retval.append("\\f"); 178 continue; 179 case '\r': 180 retval.append("\\r"); 181 continue; 182 case '\"': 183 retval.append("\\\""); 184 continue; 185 case '\'': 186 retval.append("\\\'"); 187 continue; 188 case '\\': 189 retval.append("\\\\"); 190 continue; 191 default: 192 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 193 String s = "0000" + Integer.toString(ch, 16); 194 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 195 } else { 196 retval.append(ch); 197 } 198 continue; 199 } 200 } 201 return retval.toString(); 202 } 203 204 } 205 | Popular Tags |