1 2 package org.apache.lucene.demo.html; 3 4 13 public class ParseException extends Exception { 14 15 private static final long serialVersionUID = 1L; 16 17 29 public ParseException(Token currentTokenVal, 30 int[][] expectedTokenSequencesVal, 31 String [] tokenImageVal 32 ) 33 { 34 super(""); specialConstructor = true; 36 currentToken = currentTokenVal; 37 expectedTokenSequences = expectedTokenSequencesVal; 38 tokenImage = tokenImageVal; 39 } 40 41 50 51 public ParseException() { 52 super(); 53 specialConstructor = false; 54 } 55 56 public ParseException(String message) { 57 super(message); 58 specialConstructor = false; 59 } 60 61 66 protected boolean specialConstructor; 67 68 73 public Token currentToken; 74 75 80 public int[][] expectedTokenSequences; 81 82 87 public String [] tokenImage; 88 89 99 public String getMessage() { 100 if (!specialConstructor) { 101 return super.getMessage(); 102 } 103 StringBuffer expected = new StringBuffer (); 104 int maxSize = 0; 105 for (int i = 0; i < expectedTokenSequences.length; i++) { 106 if (maxSize < expectedTokenSequences[i].length) { 107 maxSize = expectedTokenSequences[i].length; 108 } 109 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 110 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" "); } 112 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 113 expected.append("..."); } 115 expected.append(eol).append(" "); } 117 String retval = "Encountered \""; Token tok = currentToken.next; 119 for (int i = 0; i < maxSize; i++) { 120 if (i != 0) retval += " "; if (tok.kind == 0) { 122 retval += tokenImage[0]; 123 break; 124 } 125 retval += add_escapes(tok.image); 126 tok = tok.next; 127 } 128 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; retval += "." + eol; if (expectedTokenSequences.length == 1) { 131 retval += "Was expecting:" + eol + " "; } else { 133 retval += "Was expecting one of:" + eol + " "; } 135 retval += expected.toString(); 136 return retval; 137 } 138 139 142 protected String eol = System.getProperty("line.separator", "\n"); 144 149 protected String add_escapes(String str) { 150 StringBuffer retval = new StringBuffer (); 151 char ch; 152 for (int i = 0; i < str.length(); i++) { 153 switch (str.charAt(i)) 154 { 155 case 0 : 156 continue; 157 case '\b': 158 retval.append("\\b"); continue; 160 case '\t': 161 retval.append("\\t"); continue; 163 case '\n': 164 retval.append("\\n"); continue; 166 case '\f': 167 retval.append("\\f"); continue; 169 case '\r': 170 retval.append("\\r"); continue; 172 case '\"': 173 retval.append("\\\""); continue; 175 case '\'': 176 retval.append("\\\'"); continue; 178 case '\\': 179 retval.append("\\\\"); continue; 181 default: 182 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 183 String s = "0000" + Integer.toString(ch, 16); retval.append("\\u" + s.substring(s.length() - 4, s.length())); } else { 186 retval.append(ch); 187 } 188 continue; 189 } 190 } 191 return retval.toString(); 192 } 193 194 } 195 | Popular Tags |