1 2 package com.genimen.djeneric.repository.sqlparser.core; 3 4 13 public class ParseException extends com.genimen.djeneric.repository.exceptions.DjenericException 14 { 15 private static final long serialVersionUID = 3688788072401744689L; 16 17 29 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) 30 { 31 super(""); 32 specialConstructor = true; 33 currentToken = currentTokenVal; 34 expectedTokenSequences = expectedTokenSequencesVal; 35 tokenImage = tokenImageVal; 36 } 37 38 47 48 public ParseException() 49 { 50 super(); 51 specialConstructor = false; 52 } 53 54 public ParseException(Exception x) 55 { 56 super(x); 57 specialConstructor = false; 58 } 59 60 public ParseException(String message) 61 { 62 super(message); 63 specialConstructor = false; 64 } 65 66 71 protected boolean specialConstructor; 72 73 78 public Token currentToken; 79 80 85 public int[][] expectedTokenSequences; 86 87 92 public String [] tokenImage; 93 94 104 public String getMessage() 105 { 106 if (!specialConstructor) 107 { 108 return super.getMessage(); 109 } 110 String expected = ""; 111 int maxSize = 0; 112 for (int i = 0; i < expectedTokenSequences.length; i++) 113 { 114 if (maxSize < expectedTokenSequences[i].length) 115 { 116 maxSize = expectedTokenSequences[i].length; 117 } 118 for (int j = 0; j < expectedTokenSequences[i].length; j++) 119 { 120 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 121 } 122 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) 123 { 124 expected += "..."; 125 } 126 expected += eol + " "; 127 } 128 String retval = "Encountered \""; 129 Token tok = currentToken.next; 130 for (int i = 0; i < maxSize; i++) 131 { 132 if (i != 0) retval += " "; 133 if (tok.kind == 0) 134 { 135 retval += tokenImage[0]; 136 break; 137 } 138 retval += add_escapes(tok.image); 139 tok = tok.next; 140 } 141 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 142 retval += "." + eol; 143 if (expectedTokenSequences.length == 1) 144 { 145 retval += "Was expecting:" + eol + " "; 146 } 147 else 148 { 149 retval += "Was expecting one of:" + eol + " "; 150 } 151 retval += expected; 152 return retval; 153 } 154 155 158 protected String eol = System.getProperty("line.separator", "\n"); 159 160 165 protected String add_escapes(String str) 166 { 167 StringBuffer retval = new StringBuffer (); 168 char ch; 169 for (int i = 0; i < str.length(); i++) 170 { 171 switch (str.charAt(i)) 172 { 173 case 0 : 174 continue; 175 case '\b' : 176 retval.append("\\b"); 177 continue; 178 case '\t' : 179 retval.append("\\t"); 180 continue; 181 case '\n' : 182 retval.append("\\n"); 183 continue; 184 case '\f' : 185 retval.append("\\f"); 186 continue; 187 case '\r' : 188 retval.append("\\r"); 189 continue; 190 case '\"' : 191 retval.append("\\\""); 192 continue; 193 case '\'' : 194 retval.append("\\\'"); 195 continue; 196 case '\\' : 197 retval.append("\\\\"); 198 continue; 199 default : 200 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) 201 { 202 String s = "0000" + Integer.toString(ch, 16); 203 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 204 } 205 else 206 { 207 retval.append(ch); 208 } 209 continue; 210 } 211 } 212 return retval.toString(); 213 } 214 215 } | Popular Tags |