1 4 package com.tc.aspectwerkz.expression.ast; 5 6 11 public class ParseException extends Exception { 12 13 20 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String [] tokenImageVal) { 21 super(""); 22 specialConstructor = true; 23 currentToken = currentTokenVal; 24 expectedTokenSequences = expectedTokenSequencesVal; 25 tokenImage = tokenImageVal; 26 } 27 28 34 35 public ParseException() { 36 super(); 37 specialConstructor = false; 38 } 39 40 public ParseException(String message) { 41 super(message); 42 specialConstructor = false; 43 } 44 45 49 protected boolean specialConstructor; 50 51 55 public Token currentToken; 56 57 61 public int[][] expectedTokenSequences; 62 63 67 public String [] tokenImage; 68 69 76 public String getMessage() { 77 if (!specialConstructor) { 78 return super.getMessage(); 79 } 80 String expected = ""; 81 int maxSize = 0; 82 for (int i = 0; i < expectedTokenSequences.length; i++) { 83 if (maxSize < expectedTokenSequences[i].length) { 84 maxSize = expectedTokenSequences[i].length; 85 } 86 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 87 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 88 } 89 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 90 expected += "..."; 91 } 92 expected += eol + " "; 93 } 94 String retval = "Encountered \""; 95 Token tok = currentToken.next; 96 for (int i = 0; i < maxSize; i++) { 97 if (i != 0) { 98 retval += " "; 99 } 100 if (tok.kind == 0) { 101 retval += tokenImage[0]; 102 break; 103 } 104 retval += add_escapes(tok.image); 105 tok = tok.next; 106 } 107 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 108 retval += "." + eol; 109 if (expectedTokenSequences.length == 1) { 110 retval += "Was expecting:" + eol + " "; 111 } else { 112 retval += "Was expecting one of:" + eol + " "; 113 } 114 retval += expected; 115 return retval; 116 } 117 118 121 protected String eol = System.getProperty("line.separator", "\n"); 122 123 127 protected String add_escapes(String str) { 128 StringBuffer retval = new StringBuffer (); 129 char ch; 130 for (int i = 0; i < str.length(); i++) { 131 switch (str.charAt(i)) { 132 case 0: 133 continue; 134 case '\b': 135 retval.append("\\b"); 136 continue; 137 case '\t': 138 retval.append("\\t"); 139 continue; 140 case '\n': 141 retval.append("\\n"); 142 continue; 143 case '\f': 144 retval.append("\\f"); 145 continue; 146 case '\r': 147 retval.append("\\r"); 148 continue; 149 case '\"': 150 retval.append("\\\""); 151 continue; 152 case '\'': 153 retval.append("\\\'"); 154 continue; 155 case '\\': 156 retval.append("\\\\"); 157 continue; 158 default: 159 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 160 String s = "0000" + Integer.toString(ch, 16); 161 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 162 } else { 163 retval.append(ch); 164 } 165 continue; 166 } 167 } 168 return retval.toString(); 169 } 170 171 } | Popular Tags |