1 2 package org.objectweb.speedo.query.parser; 3 4 import org.objectweb.speedo.query.parser.Token; 5 6 15 public class ParseException extends Exception { 16 17 29 public ParseException(Token currentTokenVal, 30 int[][] expectedTokenSequencesVal, 31 String [] tokenImageVal 32 ) { 33 super(""); 34 specialConstructor = true; 35 currentToken = currentTokenVal; 36 expectedTokenSequences = expectedTokenSequencesVal; 37 tokenImage = tokenImageVal; 38 } 39 40 49 50 public ParseException() { 51 super(); 52 specialConstructor = false; 53 } 54 55 public ParseException(String message) { 56 super(message); 57 specialConstructor = false; 58 } 59 60 65 protected boolean specialConstructor; 66 67 72 public Token currentToken; 73 74 79 public int[][] expectedTokenSequences; 80 81 86 public String [] tokenImage; 87 88 98 public String getMessage() { 99 if (!specialConstructor) { 100 return super.getMessage(); 101 } 102 String expected = ""; 103 int maxSize = 0; 104 for (int i = 0; i < expectedTokenSequences.length; i++) { 105 if (maxSize < expectedTokenSequences[i].length) { 106 maxSize = expectedTokenSequences[i].length; 107 } 108 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 109 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 110 } 111 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 112 expected += "..."; 113 } 114 expected += eol + " "; 115 } 116 String retval = "Encountered \""; 117 Token tok = currentToken.next; 118 for (int i = 0; i < maxSize; i++) { 119 if (i != 0) retval += " "; 120 if (tok.kind == 0) { 121 retval += tokenImage[0]; 122 break; 123 } 124 retval += add_escapes(tok.image); 125 tok = tok.next; 126 } 127 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 128 retval += "." + eol; 129 if (expectedTokenSequences.length == 1) { 130 retval += "Was expecting:" + eol + " "; 131 } else { 132 retval += "Was expecting one of:" + eol + " "; 133 } 134 retval += expected; 135 return retval; 136 } 137 138 141 protected String eol = System.getProperty("line.separator", "\n"); 142 143 148 protected String add_escapes(String str) { 149 StringBuffer retval = new StringBuffer (); 150 char ch; 151 for (int i = 0; i < str.length(); i++) { 152 switch (str.charAt(i)) { 153 case 0: 154 continue; 155 case '\b': 156 retval.append("\\b"); 157 continue; 158 case '\t': 159 retval.append("\\t"); 160 continue; 161 case '\n': 162 retval.append("\\n"); 163 continue; 164 case '\f': 165 retval.append("\\f"); 166 continue; 167 case '\r': 168 retval.append("\\r"); 169 continue; 170 case '\"': 171 retval.append("\\\""); 172 continue; 173 case '\'': 174 retval.append("\\\'"); 175 continue; 176 case '\\': 177 retval.append("\\\\"); 178 continue; 179 default: 180 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 181 String s = "0000" + Integer.toString(ch, 16); 182 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 183 } else { 184 retval.append(ch); 185 } 186 continue; 187 } 188 } 189 return retval.toString(); 190 } 191 192 } 193 | Popular Tags |