1 2 12 13 package com.versant.core.jdo.query; 14 15 24 public class ParseException extends Exception { 25 26 38 public ParseException(Token currentTokenVal, 39 int[][] expectedTokenSequencesVal, 40 String [] tokenImageVal 41 ) 42 { 43 super(""); 44 specialConstructor = true; 45 currentToken = currentTokenVal; 46 expectedTokenSequences = expectedTokenSequencesVal; 47 tokenImage = tokenImageVal; 48 } 49 50 59 60 public ParseException() { 61 super(); 62 specialConstructor = false; 63 } 64 65 public ParseException(String message) { 66 super(message); 67 specialConstructor = false; 68 } 69 70 75 protected boolean specialConstructor; 76 77 82 public Token currentToken; 83 84 89 public int[][] expectedTokenSequences; 90 91 96 public String [] tokenImage; 97 98 108 public String getMessage() { 109 if (!specialConstructor) { 110 return super.getMessage(); 111 } 112 String expected = ""; 113 int maxSize = 0; 114 for (int i = 0; i < expectedTokenSequences.length; i++) { 115 if (maxSize < expectedTokenSequences[i].length) { 116 maxSize = expectedTokenSequences[i].length; 117 } 118 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 119 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 120 } 121 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 122 expected += "..."; 123 } 124 expected += eol + " "; 125 } 126 String retval = "Encountered \""; 127 Token tok = currentToken.next; 128 for (int i = 0; i < maxSize; i++) { 129 if (i != 0) retval += " "; 130 if (tok.kind == 0) { 131 retval += tokenImage[0]; 132 break; 133 } 134 retval += add_escapes(tok.image); 135 tok = tok.next; 136 } 137 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 138 retval += "." + eol; 139 if (expectedTokenSequences.length == 1) { 140 retval += "Was expecting:" + eol + " "; 141 } else { 142 retval += "Was expecting one of:" + eol + " "; 143 } 144 retval += expected; 145 return retval; 146 } 147 148 151 protected String eol = System.getProperty("line.separator", "\n"); 152 153 158 protected String add_escapes(String str) { 159 StringBuffer retval = new StringBuffer (); 160 char ch; 161 for (int i = 0; i < str.length(); i++) { 162 switch (str.charAt(i)) 163 { 164 case 0 : 165 continue; 166 case '\b': 167 retval.append("\\b"); 168 continue; 169 case '\t': 170 retval.append("\\t"); 171 continue; 172 case '\n': 173 retval.append("\\n"); 174 continue; 175 case '\f': 176 retval.append("\\f"); 177 continue; 178 case '\r': 179 retval.append("\\r"); 180 continue; 181 case '\"': 182 retval.append("\\\""); 183 continue; 184 case '\'': 185 retval.append("\\\'"); 186 continue; 187 case '\\': 188 retval.append("\\\\"); 189 continue; 190 default: 191 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 192 String s = "0000" + Integer.toString(ch, 16); 193 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 194 } else { 195 retval.append(ch); 196 } 197 continue; 198 } 199 } 200 return retval.toString(); 201 } 202 203 } 204 | Popular Tags |