1 2 package org.python.parser; 3 4 13 public class ParseException extends Exception { 14 15 27 public ParseException(Token currentTokenVal, 28 int[][] expectedTokenSequencesVal, 29 String [] tokenImageVal 30 ) 31 { 32 super(""); 33 specialConstructor = true; 34 currentToken = currentTokenVal; 35 expectedTokenSequences = expectedTokenSequencesVal; 36 tokenImage = tokenImageVal; 37 } 38 39 48 49 public ParseException() { 50 super(); 51 specialConstructor = false; 52 } 53 54 public ParseException(String message) { 55 super(message); 56 specialConstructor = false; 57 } 58 59 public ParseException(String message, SimpleNode node) { 60 super(message); 61 Token t = new Token(); 63 t.beginLine = node.beginLine; 64 t.beginColumn = node.beginColumn; 65 66 currentToken = new Token(); 67 currentToken.next = t; 68 t = currentToken; 69 t.beginLine = node.beginLine; 70 t.beginColumn = node.beginColumn; 71 72 specialConstructor = false; 73 } 74 75 80 protected boolean specialConstructor; 81 82 87 public Token currentToken; 88 89 94 public int[][] expectedTokenSequences; 95 96 101 public String [] tokenImage; 102 103 113 public static boolean verboseExceptions = false; 114 public String getMessage() { 115 if (!specialConstructor) { 116 return super.getMessage(); 117 } 118 if (verboseExceptions) { 119 String expected = ""; 120 int maxSize = 0; 121 for (int i = 0; i < expectedTokenSequences.length; i++) { 122 if (maxSize < expectedTokenSequences[i].length) { 123 maxSize = expectedTokenSequences[i].length; 124 } 125 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 126 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 127 } 128 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 129 expected += "..."; 130 } 131 expected += eol + " "; 132 } 133 String retval = "Encountered \""; 134 Token tok = currentToken.next; 135 for (int i = 0; i < maxSize; i++) { 136 if (i != 0) retval += " "; 137 if (tok.kind == 0) { 138 retval += tokenImage[0]; 139 break; 140 } 141 retval += add_escapes(tok.image); 142 tok = tok.next; 143 } 144 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol; 145 if (expectedTokenSequences.length == 1) { 146 retval += "Was expecting:" + eol + " "; 147 } else { 148 retval += "Was expecting one of:" + eol + " "; 149 } 150 retval += expected; 151 return retval; 152 } else { 153 return "invalid syntax"; 154 } 155 } 156 157 160 protected String eol = System.getProperty("line.separator", "\n"); 161 162 167 protected String add_escapes(String str) { 168 StringBuffer retval = new StringBuffer (); 169 char ch; 170 for (int i = 0; i < str.length(); i++) { 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 String s = "0000" + Integer.toString(ch, 16); 202 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 203 } else { 204 retval.append(ch); 205 } 206 continue; 207 } 208 } 209 return retval.toString(); 210 } 211 212 } 213 | Popular Tags |