1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.jdt.core.compiler.InvalidInputException; 18 import org.eclipse.jdt.internal.compiler.parser.Scanner; 19 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; 20 21 26 public class CharacterLiteral extends Expression { 27 28 32 public static final SimplePropertyDescriptor ESCAPED_VALUE_PROPERTY = 33 new SimplePropertyDescriptor(CharacterLiteral.class, "escapedValue", String .class, MANDATORY); 35 40 private static final List PROPERTY_DESCRIPTORS; 41 42 static { 43 List properyList = new ArrayList (2); 44 createPropertyList(CharacterLiteral.class, properyList); 45 addProperty(ESCAPED_VALUE_PROPERTY, properyList); 46 PROPERTY_DESCRIPTORS = reapPropertyList(properyList); 47 } 48 49 60 public static List propertyDescriptors(int apiLevel) { 61 return PROPERTY_DESCRIPTORS; 62 } 63 64 68 private String escapedValue = "\'X\'"; 70 79 CharacterLiteral(AST ast) { 80 super(ast); 81 } 82 83 86 final List internalStructuralPropertiesForType(int apiLevel) { 87 return propertyDescriptors(apiLevel); 88 } 89 90 93 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) { 94 if (property == ESCAPED_VALUE_PROPERTY) { 95 if (get) { 96 return getEscapedValue(); 97 } else { 98 setEscapedValue((String ) value); 99 return null; 100 } 101 } 102 return super.internalGetSetObjectProperty(property, get, value); 104 } 105 106 109 final int getNodeType0() { 110 return CHARACTER_LITERAL; 111 } 112 113 116 ASTNode clone0(AST target) { 117 CharacterLiteral result = new CharacterLiteral(target); 118 result.setSourceRange(this.getStartPosition(), this.getLength()); 119 result.setEscapedValue(getEscapedValue()); 120 return result; 121 } 122 123 126 final boolean subtreeMatch0(ASTMatcher matcher, Object other) { 127 return matcher.match(this, other); 129 } 130 131 134 void accept0(ASTVisitor visitor) { 135 visitor.visit(this); 136 visitor.endVisit(this); 137 } 138 139 147 public String getEscapedValue() { 148 return this.escapedValue; 149 } 150 151 164 public void setEscapedValue(String value) { 165 if (value == null) { 167 throw new IllegalArgumentException (); 168 } 169 Scanner scanner = this.ast.scanner; 170 char[] source = value.toCharArray(); 171 scanner.setSource(source); 172 scanner.resetTo(0, source.length); 173 try { 174 int tokenType = scanner.getNextToken(); 175 switch(tokenType) { 176 case TerminalTokens.TokenNameCharacterLiteral: 177 break; 178 default: 179 throw new IllegalArgumentException (); 180 } 181 } catch(InvalidInputException e) { 182 throw new IllegalArgumentException (); 183 } 184 preValueChange(ESCAPED_VALUE_PROPERTY); 185 this.escapedValue = value; 186 postValueChange(ESCAPED_VALUE_PROPERTY); 187 } 188 189 190 193 void internalSetEscapedValue(String value) { 194 preValueChange(ESCAPED_VALUE_PROPERTY); 195 this.escapedValue = value; 196 postValueChange(ESCAPED_VALUE_PROPERTY); 197 } 198 199 214 public char charValue() { 215 Scanner scanner = this.ast.scanner; 216 char[] source = escapedValue.toCharArray(); 217 scanner.setSource(source); 218 scanner.resetTo(0, source.length); 219 int firstChar = scanner.getNextChar(); 220 int secondChar = scanner.getNextChar(); 221 222 if (firstChar == -1 || firstChar != '\'') { 223 throw new IllegalArgumentException ("illegal character literal"); } 225 char value = (char) secondChar; 226 char nextChar = (char) scanner.getNextChar(); 227 if (secondChar == '\\') { 228 if (nextChar == -1) { 229 throw new IllegalArgumentException ("illegal character literal"); } 231 switch(nextChar) { 232 case 'b' : 233 value = '\b'; 234 break; 235 case 't' : 236 value = '\t'; 237 break; 238 case 'n' : 239 value = '\n'; 240 break; 241 case 'f' : 242 value = '\f'; 243 break; 244 case 'r' : 245 value = '\r'; 246 break; 247 case '\"': 248 value = '\"'; 249 break; 250 case '\'': 251 value = '\''; 252 break; 253 case '\\': 254 value = '\\'; 255 break; 256 default : if (Character.isDigit(nextChar)) { 258 int number = Character.getNumericValue(nextChar); 259 nextChar = (char) scanner.getNextChar(); 260 if (nextChar == -1) { 261 throw new IllegalArgumentException ("illegal character literal"); } 263 if (nextChar != '\'') { 264 if (!Character.isDigit(nextChar)) { 265 throw new IllegalArgumentException ("illegal character literal"); } 267 number = (number * 8) + Character.getNumericValue(nextChar); 268 } 269 nextChar = (char) scanner.getNextChar(); 270 if (nextChar == -1) { 271 throw new IllegalArgumentException ("illegal character literal"); } 273 if (nextChar != '\'') { 274 if (!Character.isDigit(nextChar)) { 275 throw new IllegalArgumentException ("illegal character literal"); } 277 number = (number * 8) + Character.getNumericValue(nextChar); 278 } 279 value = (char) number; 280 } else { 281 throw new IllegalArgumentException ("illegal character literal"); } 283 break; 284 } 285 nextChar = (char) scanner.getNextChar(); 286 if (nextChar == -1) { 287 throw new IllegalArgumentException ("illegal character literal"); } 289 } 290 if (nextChar == -1 || nextChar != '\'') { 291 throw new IllegalArgumentException ("illegal character literal"); } 293 return value; 294 } 295 309 public void setCharValue(char value) { 310 StringBuffer b = new StringBuffer (3); 311 312 b.append('\''); switch(value) { 314 case '\b' : 315 b.append("\\b"); break; 317 case '\t' : 318 b.append("\\t"); break; 320 case '\n' : 321 b.append("\\n"); break; 323 case '\f' : 324 b.append("\\f"); break; 326 case '\r' : 327 b.append("\\r"); break; 329 case '\"': 330 b.append("\\\""); break; 332 case '\'': 333 b.append("\\\'"); break; 335 case '\\': 336 b.append("\\\\"); break; 338 case '\0' : 339 b.append("\\0"); break; 341 case '\1' : 342 b.append("\\1"); break; 344 case '\2' : 345 b.append("\\2"); break; 347 case '\3' : 348 b.append("\\3"); break; 350 case '\4' : 351 b.append("\\4"); break; 353 case '\5' : 354 b.append("\\5"); break; 356 case '\6' : 357 b.append("\\6"); break; 359 case '\7' : 360 b.append("\\7"); break; 362 default: 363 b.append(value); 364 } 365 b.append('\''); setEscapedValue(b.toString()); 367 } 368 369 372 int memSize() { 373 int size = BASE_NODE_SIZE + 1 * 4 + stringSize(escapedValue); 374 return size; 375 } 376 377 380 int treeSize() { 381 return memSize(); 382 } 383 } 384 385 | Popular Tags |