1 18 package org.apache.beehive.netui.script.el.parser; 19 20 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 21 22 31 public class ParseException 32 extends Exception { 33 34 46 public ParseException(Token currentTokenVal, 47 int[][] expectedTokenSequencesVal, 48 String [] tokenImageVal) { 49 super(""); 50 specialConstructor = true; 51 currentToken = currentTokenVal; 52 expectedTokenSequences = expectedTokenSequencesVal; 53 tokenImage = tokenImageVal; 54 } 55 56 65 66 public ParseException() { 67 super(); 68 specialConstructor = false; 69 } 70 71 public ParseException(String message) { 72 super(message); 73 specialConstructor = false; 74 } 75 76 81 protected boolean specialConstructor; 82 83 88 public Token currentToken; 89 90 95 public int[][] expectedTokenSequences; 96 97 102 public String [] tokenImage; 103 104 114 public String getMessage() { 115 if(!specialConstructor) { 116 return super.getMessage(); 117 } 118 String expected = ""; 119 int maxSize = 0; 120 for(int i = 0; i < expectedTokenSequences.length; i++) { 121 if(maxSize < expectedTokenSequences[i].length) { 122 maxSize = expectedTokenSequences[i].length; 123 } 124 for(int j = 0; j < expectedTokenSequences[i].length; j++) { 125 expected += tokenImage[expectedTokenSequences[i][j]] + " "; 126 } 127 if(expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 128 expected += "..."; 129 } 130 expected += eol + " "; 131 } 132 String retval = "Encountered \""; 133 Token tok = currentToken.next; 134 for(int i = 0; i < maxSize; i++) { 135 if(i != 0) retval += " "; 136 if(tok.kind == 0) { 137 retval += tokenImage[0]; 138 break; 139 } 140 retval += add_escapes(tok.image); 141 tok = tok.next; 142 } 143 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 144 retval += "." + eol; 145 if(expectedTokenSequences.length == 1) { 146 retval += "Was expecting:" + eol + " "; 147 } else { 148 retval += "Was expecting one of:" + eol + " "; 149 } 150 retval += expected; 151 return retval; 152 } 153 154 157 protected String eol = System.getProperty("line.separator", "\n"); 158 159 164 protected String add_escapes(String str) { 165 InternalStringBuilder retval = new InternalStringBuilder(); 166 char ch; 167 for(int i = 0; i < str.length(); i++) { 168 switch(str.charAt(i)) { 169 case 0: 170 continue; 171 case '\b': 172 retval.append("\\b"); 173 continue; 174 case '\t': 175 retval.append("\\t"); 176 continue; 177 case '\n': 178 retval.append("\\n"); 179 continue; 180 case '\f': 181 retval.append("\\f"); 182 continue; 183 case '\r': 184 retval.append("\\r"); 185 continue; 186 case '\"': 187 retval.append("\\\""); 188 continue; 189 case '\'': 190 retval.append("\\\'"); 191 continue; 192 case '\\': 193 retval.append("\\\\"); 194 continue; 195 default: 196 if((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 197 String s = "0000" + Integer.toString(ch, 16); 198 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 199 } else { 200 retval.append(ch); 201 } 202 continue; 203 } 204 } 205 return retval.toString(); 206 } 207 208 } 209 | Popular Tags |