1 2 9 package org.codehaus.aspectwerkz.expression.ast; 10 11 16 public class ParseException extends Exception { 17 18 25 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) { 26 super(""); 27 specialConstructor = true; 28 currentToken = currentTokenVal; 29 expectedTokenSequences = expectedTokenSequencesVal; 30 tokenImage = tokenImageVal; 31 } 32 33 39 40 public ParseException() { 41 super(); 42 specialConstructor = false; 43 } 44 45 public ParseException(String message) { 46 super(message); 47 specialConstructor = false; 48 } 49 50 54 protected boolean specialConstructor; 55 56 60 public Token currentToken; 61 62 66 public int[][] expectedTokenSequences; 67 68 72 public String [] tokenImage; 73 74 81 public String getMessage() { 82 if (!specialConstructor) { 83 return super.getMessage(); 84 } 85 String expected = ""; 86 int maxSize = 0; 87 for (int i = 0; i < expectedTokenSequences.length; i++) { 88 if (maxSize < expectedTokenSequences[i].length) { 89 maxSize = expectedTokenSequences[i].length; 90 } 91 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 92 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 93 } 94 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 95 expected += "..."; 96 } 97 expected += eol + " "; 98 } 99 String retval = "Encountered \""; 100 Token tok = currentToken.next; 101 for (int i = 0; i < maxSize; i++) { 102 if (i != 0) { 103 retval += " "; 104 } 105 if (tok.kind == 0) { 106 retval += tokenImage[0]; 107 break; 108 } 109 retval += add_escapes(tok.image); 110 tok = tok.next; 111 } 112 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 113 retval += "." + eol; 114 if (expectedTokenSequences.length == 1) { 115 retval += "Was expecting:" + eol + " "; 116 } else { 117 retval += "Was expecting one of:" + eol + " "; 118 } 119 retval += expected; 120 return retval; 121 } 122 123 126 protected String eol = System.getProperty("line.separator", "\n"); 127 128 132 protected String add_escapes(String str) { 133 StringBuffer retval = new StringBuffer (); 134 char ch; 135 for (int i = 0; i < str.length(); i++) { 136 switch (str.charAt(i)) { 137 case 0: 138 continue; 139 case '\b': 140 retval.append("\\b"); 141 continue; 142 case '\t': 143 retval.append("\\t"); 144 continue; 145 case '\n': 146 retval.append("\\n"); 147 continue; 148 case '\f': 149 retval.append("\\f"); 150 continue; 151 case '\r': 152 retval.append("\\r"); 153 continue; 154 case '\"': 155 retval.append("\\\""); 156 continue; 157 case '\'': 158 retval.append("\\\'"); 159 continue; 160 case '\\': 161 retval.append("\\\\"); 162 continue; 163 default: 164 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 165 String s = "0000" + Integer.toString(ch, 16); 166 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 167 } else { 168 retval.append(ch); 169 } 170 continue; 171 } 172 } 173 return retval.toString(); 174 } 175 176 } | Popular Tags |