1 2 5 6 9 10 14 15 package org.openlaszlo.sc.parser; 16 import org.openlaszlo.sc.CompilerException; 17 18 27 public class ParseException extends CompilerException { 28 29 41 public ParseException(Token currentTokenVal, 42 int[][] expectedTokenSequencesVal, 43 String [] tokenImageVal 44 ) 45 { 46 super(""); 47 specialConstructor = true; 48 currentToken = currentTokenVal; 49 expectedTokenSequences = expectedTokenSequencesVal; 50 tokenImage = tokenImageVal; 51 } 52 53 62 63 public ParseException() { 64 super(); 65 specialConstructor = false; 66 } 67 68 public ParseException(String message) { 69 super(message); 70 specialConstructor = false; 71 } 72 73 78 protected boolean specialConstructor; 79 80 85 public Token currentToken; 86 87 92 public int[][] expectedTokenSequences; 93 94 99 public String [] tokenImage; 100 101 public int getBeginLine() { 102 if (currentToken.next != null) { 103 return currentToken.next.beginLine; 104 } else { 105 return -1; 106 } 107 } 108 109 public int getBeginColumn() { 110 if (currentToken.next != null) { 111 return currentToken.next.beginColumn; 112 } else { 113 return -1; 114 } 115 } 116 117 127 public String getMessage() { 128 return getMessage(true); 129 } 130 131 public String getMessage(boolean includeSourceLocation) { 132 if (!specialConstructor) { 133 return super.getMessage(); 134 } 135 String expected = ""; 136 int expectedCount = 0; 137 int maxSize = 0; 138 boolean firstTime = true; 139 for (int i = 0; i < expectedTokenSequences.length; i++) { 140 if (maxSize < expectedTokenSequences[i].length) { 141 maxSize = expectedTokenSequences[i].length; 142 } 143 String token = tokenImage[expectedTokenSequences[i][0]]; 144 if (token.length() > 0 && 145 token.charAt(0) == '"' && 146 token.charAt(token.length()-1) == '"') { 147 token = token.substring(1, token.length() - 1); 148 } 149 if (token.charAt(0) != '#') { 150 if (!firstTime) 151 expected += ", "; 152 firstTime = false; 153 expected += token; 154 expectedCount += 1; 155 } 156 } 157 String tokenstr = ""; 158 Token tok = currentToken.next; 159 for (int i = 0; i < maxSize; i++) { 160 if (i != 0) tokenstr += " "; 161 if (tok.kind == 0) { 162 tokenstr += tokenImage[0]; 163 break; 164 } 165 tokenstr += tok.image; tok = tok.next; 167 } 168 String msg = "the token \"" + tokenstr + 169 "\" was not expected at this position"; 170 boolean showMismatchSolution = false; 171 if (tokenstr.equals("#endAttribute")) { 172 msg = "the attribute value ended in mid-expression"; 173 showMismatchSolution = true; 174 } 175 if (tokenstr.equals("#endAttributeStatements")) { 176 msg = "the attribute value ended in mid-statement"; 177 showMismatchSolution = true; 178 } 179 if (tokenstr.equals("#endContent")) { 180 msg = "the element content ended in mid-program"; 181 showMismatchSolution = true; 182 } 183 String retval = "Syntax error: " + msg; 184 if (currentToken.next != null && includeSourceLocation) { 185 retval += " at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; 186 } 187 retval += "."; 188 if (expectedCount == 1) { 189 retval += eol + "Was expecting " + expected; 190 } else if (0 < expectedCount && expectedCount <= 5) { 191 retval += eol + "Was expecting one of: " + expected; 192 } else if (showMismatchSolution) { 193 retval += eol + "Look for an unclosed '(', '{', or '['."; 194 } 195 return retval; 196 } 197 198 201 protected String eol = System.getProperty("line.separator", "\n"); 202 203 208 protected String add_escapes(String str) { 209 StringBuffer retval = new StringBuffer (); 210 char ch; 211 for (int i = 0; i < str.length(); i++) { 212 switch (str.charAt(i)) 213 { 214 case 0 : 215 continue; 216 case '\b': 217 retval.append("\\b"); 218 continue; 219 case '\t': 220 retval.append("\\t"); 221 continue; 222 case '\n': 223 retval.append("\\n"); 224 continue; 225 case '\f': 226 retval.append("\\f"); 227 continue; 228 case '\r': 229 retval.append("\\r"); 230 continue; 231 case '\"': 232 retval.append("\\\""); 233 continue; 234 case '\'': 235 retval.append("\\\'"); 236 continue; 237 case '\\': 238 retval.append("\\\\"); 239 continue; 240 default: 241 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 242 String s = "0000" + Integer.toString(ch, 16); 243 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 244 } else { 245 retval.append(ch); 246 } 247 continue; 248 } 249 } 250 return retval.toString(); 251 } 252 253 } 254 | Popular Tags |