1 2 package org.ejen.util.arl; 3 4 13 public class ParseException extends Exception { 14 15 27 public ParseException(Token currentTokenVal, 28 int[][] expectedTokenSequencesVal, 29 String [] tokenImageVal 30 ) { 31 super(""); 32 specialConstructor = true; 33 currentToken = currentTokenVal; 34 expectedTokenSequences = expectedTokenSequencesVal; 35 tokenImage = tokenImageVal; 36 } 37 38 47 public ParseException() { 48 super(); 49 specialConstructor = false; 50 } 51 52 public ParseException(String message) { 53 super(message); 54 specialConstructor = false; 55 } 56 57 62 protected boolean specialConstructor; 63 64 69 public Token currentToken; 70 71 76 public int[][] expectedTokenSequences; 77 78 83 public String [] tokenImage; 84 85 95 public String getMessage() { 96 if (!specialConstructor) { 97 return super.getMessage(); 98 } 99 String expected = ""; 100 int maxSize = 0; 101 102 for (int i = 0; i < expectedTokenSequences.length; i++) { 103 if (maxSize < expectedTokenSequences[i].length) { 104 maxSize = expectedTokenSequences[i].length; 105 } 106 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 107 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 108 } 109 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] 110 != 0) { 111 expected += "..."; 112 } 113 expected += eol + " "; 114 } 115 String retval = "Encountered \""; 116 Token tok = currentToken.next; 117 118 for (int i = 0; i < maxSize; i++) { 119 if (i != 0) { 120 retval += " "; 121 } 122 if (tok.kind == 0) { 123 retval += tokenImage[0]; 124 break; 125 } 126 retval += add_escapes(tok.image); 127 tok = tok.next; 128 } 129 retval += "\" at line " + currentToken.next.beginLine + ", column " 130 + currentToken.next.beginColumn; 131 retval += "." + eol; 132 if (expectedTokenSequences.length == 1) { 133 retval += "Was expecting:" + eol + " "; 134 } else { 135 retval += "Was expecting one of:" + eol + " "; 136 } 137 retval += expected; 138 return retval; 139 } 140 141 144 protected String eol = System.getProperty("line.separator", "\n"); 145 146 151 protected String add_escapes(String str) { 152 StringBuffer retval = new StringBuffer (); 153 char ch; 154 155 for (int i = 0; i < str.length(); i++) { 156 switch (str.charAt(i)) { 157 case 0: 158 continue; 159 160 case '\b': 161 retval.append("\\b"); 162 continue; 163 164 case '\t': 165 retval.append("\\t"); 166 continue; 167 168 case '\n': 169 retval.append("\\n"); 170 continue; 171 172 case '\f': 173 retval.append("\\f"); 174 continue; 175 176 case '\r': 177 retval.append("\\r"); 178 continue; 179 180 case '\"': 181 retval.append("\\\""); 182 continue; 183 184 case '\'': 185 retval.append("\\\'"); 186 continue; 187 188 case '\\': 189 retval.append("\\\\"); 190 continue; 191 192 default: 193 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 194 String s = "0000" + Integer.toString(ch, 16); 195 196 retval.append("\\u" 197 + 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 | Popular Tags |