1 2 6 7 package net.sourceforge.pmd.jsp.ast; 8 9 18 public class ParseException extends net.sourceforge.pmd.ast.ParseException { 19 20 32 public ParseException(Token currentTokenVal, 33 int[][] expectedTokenSequencesVal, 34 String [] tokenImageVal) { 35 super(""); 36 specialConstructor = true; 37 currentToken = currentTokenVal; 38 expectedTokenSequences = expectedTokenSequencesVal; 39 tokenImage = tokenImageVal; 40 } 41 42 51 52 public ParseException() { 53 super(); 54 specialConstructor = false; 55 } 56 57 public ParseException(String message) { 58 super(message); 59 specialConstructor = false; 60 } 61 62 67 protected boolean specialConstructor; 68 69 74 public Token currentToken; 75 76 81 public int[][] expectedTokenSequences; 82 83 88 public String [] tokenImage; 89 90 100 public String getMessage() { 101 if (!specialConstructor) { 102 return super.getMessage(); 103 } 104 String expected = ""; 105 int maxSize = 0; 106 for (int i = 0; i < expectedTokenSequences.length; i++) { 107 if (maxSize < expectedTokenSequences[i].length) { 108 maxSize = expectedTokenSequences[i].length; 109 } 110 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 111 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 112 } 113 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 114 expected += "..."; 115 } 116 expected += eol + " "; 117 } 118 String retval = "Encountered \""; 119 Token tok = currentToken.next; 120 for (int i = 0; i < maxSize; i++) { 121 if (i != 0) retval += " "; 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 " + currentToken.next.beginColumn; 130 retval += "." + eol; 131 if (expectedTokenSequences.length == 1) { 132 retval += "Was expecting:" + eol + " "; 133 } else { 134 retval += "Was expecting one of:" + eol + " "; 135 } 136 retval += expected; 137 return retval; 138 } 139 140 143 protected String eol = System.getProperty("line.separator", "\n"); 144 145 150 protected String add_escapes(String str) { 151 StringBuffer retval = new StringBuffer (); 152 char ch; 153 for (int i = 0; i < str.length(); i++) { 154 switch (str.charAt(i)) { 155 case 0: 156 continue; 157 case '\b': 158 retval.append("\\b"); 159 continue; 160 case '\t': 161 retval.append("\\t"); 162 continue; 163 case '\n': 164 retval.append("\\n"); 165 continue; 166 case '\f': 167 retval.append("\\f"); 168 continue; 169 case '\r': 170 retval.append("\\r"); 171 continue; 172 case '\"': 173 retval.append("\\\""); 174 continue; 175 case '\'': 176 retval.append("\\\'"); 177 continue; 178 case '\\': 179 retval.append("\\\\"); 180 continue; 181 default: 182 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 183 String s = "0000" + Integer.toString(ch, 16); 184 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 185 } else { 186 retval.append(ch); 187 } 188 continue; 189 } 190 } 191 return retval.toString(); 192 } 193 194 } 195 | Popular Tags |