1 2 17 package com.lutris.util; 18 19 28 public class ParseException extends Exception { 29 30 42 public ParseException(Token currentTokenVal, 43 int[][] expectedTokenSequencesVal, 44 String [] tokenImageVal 45 ) 46 { 47 super(""); 48 specialConstructor = true; 49 currentToken = currentTokenVal; 50 expectedTokenSequences = expectedTokenSequencesVal; 51 tokenImage = tokenImageVal; 52 } 53 54 63 64 public ParseException() { 65 super(); 66 specialConstructor = false; 67 } 68 69 public ParseException(String message) { 70 super(message); 71 specialConstructor = false; 72 } 73 74 79 protected boolean specialConstructor; 80 81 86 public Token currentToken; 87 88 93 public int[][] expectedTokenSequences; 94 95 100 public String [] tokenImage; 101 102 112 public String getMessage() { 113 if (!specialConstructor) { 114 return super.getMessage(); 115 } 116 String expected = ""; 117 int maxSize = 0; 118 for (int i = 0; i < expectedTokenSequences.length; i++) { 119 if (maxSize < expectedTokenSequences[i].length) { 120 maxSize = expectedTokenSequences[i].length; 121 } 122 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 123 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 124 } 125 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 126 expected += "..."; 127 } 128 expected += eol + " "; 129 } 130 String retval = "Encountered \""; 131 Token tok = currentToken.next; 132 for (int i = 0; i < maxSize; i++) { 133 if (i != 0) retval += " "; 134 if (tok.kind == 0) { 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 retval += "Was expecting:" + eol + " "; 145 } else { 146 retval += "Was expecting one of:" + eol + " "; 147 } 148 retval += expected; 149 return retval; 150 } 151 152 155 protected String eol = System.getProperty("line.separator", "\n"); 156 157 162 protected String add_escapes(String str) { 163 StringBuffer retval = new StringBuffer (); 164 char ch; 165 for (int i = 0; i < str.length(); i++) { 166 switch (str.charAt(i)) 167 { 168 case 0 : 169 continue; 170 case '\b': 171 retval.append("\\b"); 172 continue; 173 case '\t': 174 retval.append("\\t"); 175 continue; 176 case '\n': 177 retval.append("\\n"); 178 continue; 179 case '\f': 180 retval.append("\\f"); 181 continue; 182 case '\r': 183 retval.append("\\r"); 184 continue; 185 case '\"': 186 retval.append("\\\""); 187 continue; 188 case '\'': 189 retval.append("\\\'"); 190 continue; 191 case '\\': 192 retval.append("\\\\"); 193 continue; 194 default: 195 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 196 String s = "0000" + Integer.toString(ch, 16); 197 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 198 } else { 199 retval.append(ch); 200 } 201 continue; 202 } 203 } 204 return retval.toString(); 205 } 206 207 } 208 | Popular Tags |