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