1 7 8 9 package com.sun.jmx.snmp.IPAcl; 10 11 20 class ParseException extends Exception { 21 22 34 public ParseException(Token currentTokenVal, 35 int[][] expectedTokenSequencesVal, 36 String [] tokenImageVal 37 ) 38 { 39 super(""); 40 specialConstructor = true; 41 currentToken = currentTokenVal; 42 expectedTokenSequences = expectedTokenSequencesVal; 43 tokenImage = tokenImageVal; 44 } 45 46 55 56 public ParseException() { 57 super(); 58 specialConstructor = false; 59 } 60 61 public ParseException(String message) { 62 super(message); 63 specialConstructor = false; 64 } 65 66 71 protected boolean specialConstructor; 72 73 78 public Token currentToken; 79 80 85 public int[][] expectedTokenSequences; 86 87 92 public String [] tokenImage; 93 94 104 public String getMessage() { 105 if (!specialConstructor) { 106 return super.getMessage(); 107 } 108 String expected = ""; 109 int maxSize = 0; 110 for (int i = 0; i < expectedTokenSequences.length; i++) { 111 if (maxSize < expectedTokenSequences[i].length) { 112 maxSize = expectedTokenSequences[i].length; 113 } 114 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 115 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 116 } 117 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 118 expected += "..."; 119 } 120 expected += eol + " "; 121 } 122 String retval = "Encountered \""; 123 Token tok = currentToken.next; 124 for (int i = 0; i < maxSize; i++) { 125 if (i != 0) retval += " "; 126 if (tok.kind == 0) { 127 retval += tokenImage[0]; 128 break; 129 } 130 retval += add_escapes(tok.image); 131 tok = tok.next; 132 } 133 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol; 134 if (expectedTokenSequences.length == 1) { 135 retval += "Was expecting:" + eol + " "; 136 } else { 137 retval += "Was expecting one of:" + eol + " "; 138 } 139 retval += expected; 140 return retval; 141 } 142 143 146 protected String eol = System.getProperty("line.separator", "\n"); 147 148 153 protected String add_escapes(String str) { 154 StringBuffer retval = new StringBuffer (); 155 char ch; 156 for (int i = 0; i < str.length(); i++) { 157 switch (str.charAt(i)) 158 { 159 case 0 : 160 continue; 161 case '\b': 162 retval.append("\\b"); 163 continue; 164 case '\t': 165 retval.append("\\t"); 166 continue; 167 case '\n': 168 retval.append("\\n"); 169 continue; 170 case '\f': 171 retval.append("\\f"); 172 continue; 173 case '\r': 174 retval.append("\\r"); 175 continue; 176 case '\"': 177 retval.append("\\\""); 178 continue; 179 case '\'': 180 retval.append("\\\'"); 181 continue; 182 case '\\': 183 retval.append("\\\\"); 184 continue; 185 default: 186 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 187 String s = "0000" + Integer.toString(ch, 16); 188 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 189 } else { 190 retval.append(ch); 191 } 192 continue; 193 } 194 } 195 return retval.toString(); 196 } 197 198 } 199 | Popular Tags |