1 11 package org.eclipse.jdt.internal.formatter; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jdt.internal.compiler.ASTVisitor; 16 import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression; 17 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression; 18 import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression; 19 import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; 20 import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference; 21 import org.eclipse.jdt.internal.compiler.ast.ArrayReference; 22 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference; 23 import org.eclipse.jdt.internal.compiler.ast.Assignment; 24 import org.eclipse.jdt.internal.compiler.ast.ASTNode; 25 import org.eclipse.jdt.internal.compiler.ast.BinaryExpression; 26 import org.eclipse.jdt.internal.compiler.ast.CastExpression; 27 import org.eclipse.jdt.internal.compiler.ast.CharLiteral; 28 import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; 29 import org.eclipse.jdt.internal.compiler.ast.CombinedBinaryExpression; 30 import org.eclipse.jdt.internal.compiler.ast.CompoundAssignment; 31 import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression; 32 import org.eclipse.jdt.internal.compiler.ast.DoubleLiteral; 33 import org.eclipse.jdt.internal.compiler.ast.EqualExpression; 34 import org.eclipse.jdt.internal.compiler.ast.Expression; 35 import org.eclipse.jdt.internal.compiler.ast.ExtendedStringLiteral; 36 import org.eclipse.jdt.internal.compiler.ast.FalseLiteral; 37 import org.eclipse.jdt.internal.compiler.ast.FieldReference; 38 import org.eclipse.jdt.internal.compiler.ast.FloatLiteral; 39 import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression; 40 import org.eclipse.jdt.internal.compiler.ast.IntLiteral; 41 import org.eclipse.jdt.internal.compiler.ast.LongLiteral; 42 import org.eclipse.jdt.internal.compiler.ast.MessageSend; 43 import org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation; 44 import org.eclipse.jdt.internal.compiler.ast.NullLiteral; 45 import org.eclipse.jdt.internal.compiler.ast.OR_OR_Expression; 46 import org.eclipse.jdt.internal.compiler.ast.OperatorIds; 47 import org.eclipse.jdt.internal.compiler.ast.PostfixExpression; 48 import org.eclipse.jdt.internal.compiler.ast.PrefixExpression; 49 import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression; 50 import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference; 51 import org.eclipse.jdt.internal.compiler.ast.QualifiedSuperReference; 52 import org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference; 53 import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; 54 import org.eclipse.jdt.internal.compiler.ast.StringLiteral; 55 import org.eclipse.jdt.internal.compiler.ast.SuperReference; 56 import org.eclipse.jdt.internal.compiler.ast.ThisReference; 57 import org.eclipse.jdt.internal.compiler.ast.TrueLiteral; 58 import org.eclipse.jdt.internal.compiler.ast.UnaryExpression; 59 import org.eclipse.jdt.internal.compiler.lookup.BlockScope; 60 import org.eclipse.jdt.internal.compiler.lookup.ClassScope; 61 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; 62 63 class BinaryExpressionFragmentBuilder 64 extends ASTVisitor { 65 66 ArrayList fragmentsList; 67 ArrayList operatorsList; 68 private int realFragmentsSize; 69 70 BinaryExpressionFragmentBuilder() { 71 this.fragmentsList = new ArrayList (); 72 this.operatorsList = new ArrayList (); 73 this.realFragmentsSize = 0; 74 } 75 76 private final void addRealFragment(ASTNode node) { 77 this.fragmentsList.add(node); 78 this.realFragmentsSize++; 79 } 80 81 private final void addSmallFragment(ASTNode node) { 82 this.fragmentsList.add(node); 83 } 84 85 private boolean buildFragments(Expression expression) { 86 if (((expression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) != 0) { 87 addRealFragment(expression); 88 return false; 89 } else { 90 return true; 91 } 92 } 93 94 public ASTNode[] fragments() { 95 ASTNode[] fragments = new ASTNode[this.fragmentsList.size()]; 96 this.fragmentsList.toArray(fragments); 97 return fragments; 98 } 99 100 public int[] operators() { 101 int length = operatorsList.size(); 102 int[] tab = new int[length]; 103 for (int i = 0; i < length; i++) { 104 tab[i] = ((Integer )operatorsList.get(i)).intValue(); 105 } 106 return tab; 107 } 108 109 public int realFragmentsSize() { 110 return this.realFragmentsSize; 111 } 112 113 public boolean visit( 114 AllocationExpression allocationExpression, 115 BlockScope scope) { 116 this.addRealFragment(allocationExpression); 117 return false; 118 } 119 120 public boolean visit( 121 AND_AND_Expression and_and_Expression, 122 BlockScope scope) { 123 124 if (((and_and_Expression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) != 0) { 125 addRealFragment(and_and_Expression); 126 } else { 127 and_and_Expression.left.traverse(this, scope); 128 this.operatorsList.add(new Integer (TerminalTokens.TokenNameAND_AND)); 129 and_and_Expression.right.traverse(this, scope); 130 } 131 return false; 132 } 133 134 public boolean visit( 135 ArrayAllocationExpression arrayAllocationExpression, 136 BlockScope scope) { 137 this.addRealFragment(arrayAllocationExpression); 138 return false; 139 } 140 141 public boolean visit(ArrayInitializer arrayInitializer, BlockScope scope) { 142 this.addRealFragment(arrayInitializer); 143 return false; 144 } 145 146 public boolean visit( 147 ArrayQualifiedTypeReference arrayQualifiedTypeReference, 148 BlockScope scope) { 149 this.addRealFragment(arrayQualifiedTypeReference); 150 return false; 151 } 152 153 public boolean visit( 154 ArrayQualifiedTypeReference arrayQualifiedTypeReference, 155 ClassScope scope) { 156 this.addRealFragment(arrayQualifiedTypeReference); 157 return false; 158 } 159 160 public boolean visit(ArrayReference arrayReference, BlockScope scope) { 161 this.addRealFragment(arrayReference); 162 return false; 163 } 164 165 public boolean visit( 166 ArrayTypeReference arrayTypeReference, 167 BlockScope scope) { 168 this.addRealFragment(arrayTypeReference); 169 return false; 170 } 171 172 public boolean visit( 173 ArrayTypeReference arrayTypeReference, 174 ClassScope scope) { 175 this.addRealFragment(arrayTypeReference); 176 return false; 177 } 178 179 public boolean visit(Assignment assignment, BlockScope scope) { 180 this.addRealFragment(assignment); 181 return false; 182 } 183 184 public boolean visit(BinaryExpression binaryExpression, BlockScope scope) { 185 if (binaryExpression instanceof CombinedBinaryExpression) { 186 CombinedBinaryExpression expression = (CombinedBinaryExpression) binaryExpression; 187 if (expression.referencesTable != null) { 188 return this.visit(expression, scope); 189 } 190 } 191 final int numberOfParens = (binaryExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; 192 if (numberOfParens > 0) { 193 this.addRealFragment(binaryExpression); 194 } else { 195 switch((binaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) { 196 case OperatorIds.PLUS : 197 if (buildFragments(binaryExpression)) { 198 binaryExpression.left.traverse(this, scope); 199 this.operatorsList.add(new Integer (TerminalTokens.TokenNamePLUS)); 200 binaryExpression.right.traverse(this, scope); 201 } 202 return false; 203 case OperatorIds.MINUS : 204 if (buildFragments(binaryExpression)) { 205 binaryExpression.left.traverse(this, scope); 206 this.operatorsList.add(new Integer (TerminalTokens.TokenNameMINUS)); 207 binaryExpression.right.traverse(this, scope); 208 } 209 return false; 210 case OperatorIds.MULTIPLY : 211 if (buildFragments(binaryExpression)) { 212 binaryExpression.left.traverse(this, scope); 213 this.operatorsList.add(new Integer (TerminalTokens.TokenNameMULTIPLY)); 214 binaryExpression.right.traverse(this, scope); 215 } 216 return false; 217 case OperatorIds.REMAINDER : 218 if (buildFragments(binaryExpression)) { 219 binaryExpression.left.traverse(this, scope); 220 this.operatorsList.add(new Integer (TerminalTokens.TokenNameREMAINDER)); 221 binaryExpression.right.traverse(this, scope); 222 } 223 return false; 224 case OperatorIds.XOR : 225 if (buildFragments(binaryExpression)) { 226 binaryExpression.left.traverse(this, scope); 227 this.operatorsList.add(new Integer (TerminalTokens.TokenNameXOR)); 228 binaryExpression.right.traverse(this, scope); 229 } 230 return false; 231 case OperatorIds.DIVIDE : 232 if (buildFragments(binaryExpression)) { 233 binaryExpression.left.traverse(this, scope); 234 this.operatorsList.add(new Integer (TerminalTokens.TokenNameDIVIDE)); 235 binaryExpression.right.traverse(this, scope); 236 } 237 return false; 238 case OperatorIds.OR : 239 if (buildFragments(binaryExpression)) { 240 binaryExpression.left.traverse(this, scope); 241 this.operatorsList.add(new Integer (TerminalTokens.TokenNameOR)); 242 binaryExpression.right.traverse(this, scope); 243 } 244 return false; 245 case OperatorIds.AND : 246 if (buildFragments(binaryExpression)) { 247 binaryExpression.left.traverse(this, scope); 248 this.operatorsList.add(new Integer (TerminalTokens.TokenNameAND)); 249 binaryExpression.right.traverse(this, scope); 250 } 251 return false; 252 default: 253 this.addRealFragment(binaryExpression); 254 } 255 } 256 return false; 257 } 258 259 public boolean visit(CombinedBinaryExpression combinedBinaryExpression, BlockScope scope) { 260 if (combinedBinaryExpression.referencesTable == null) { 262 this.addRealFragment(combinedBinaryExpression.left); 263 this.operatorsList.add(new Integer (TerminalTokens.TokenNamePLUS)); 264 this.addRealFragment(combinedBinaryExpression.right); 265 return false; 266 } 267 BinaryExpression cursor = combinedBinaryExpression.referencesTable[0]; 268 if (cursor.left instanceof CombinedBinaryExpression) { 269 this.visit((CombinedBinaryExpression) cursor.left, scope); 270 } else { 271 this.addRealFragment(cursor.left); 272 } 273 for (int i = 0, end = combinedBinaryExpression.arity; i < end; i ++) { 274 this.operatorsList.add(new Integer (TerminalTokens.TokenNamePLUS)); 275 this.addRealFragment(combinedBinaryExpression.referencesTable[i].right); 276 } 277 this.operatorsList.add(new Integer (TerminalTokens.TokenNamePLUS)); 278 this.addRealFragment(combinedBinaryExpression.right); 279 return false; 280 } 281 282 public boolean visit(CastExpression castExpression, BlockScope scope) { 283 this.addRealFragment(castExpression); 284 return false; 285 } 286 287 public boolean visit(CharLiteral charLiteral, BlockScope scope) { 288 this.addSmallFragment(charLiteral); 289 return false; 290 } 291 292 public boolean visit( 293 ClassLiteralAccess classLiteralAccess, 294 BlockScope scope) { 295 this.addRealFragment(classLiteralAccess); 296 return false; 297 } 298 299 public boolean visit( 300 CompoundAssignment compoundAssignment, 301 BlockScope scope) { 302 this.addRealFragment(compoundAssignment); 303 return false; 304 } 305 306 public boolean visit( 307 ConditionalExpression conditionalExpression, 308 BlockScope scope) { 309 this.addRealFragment(conditionalExpression); 310 return false; 311 } 312 313 public boolean visit(DoubleLiteral doubleLiteral, BlockScope scope) { 314 this.addSmallFragment(doubleLiteral); 315 return false; 316 } 317 318 public boolean visit(EqualExpression equalExpression, BlockScope scope) { 319 this.addRealFragment(equalExpression); 320 return false; 321 } 322 323 public boolean visit( 324 ExtendedStringLiteral extendedStringLiteral, 325 BlockScope scope) { 326 this.addRealFragment(extendedStringLiteral); 327 return false; 328 } 329 330 public boolean visit(FalseLiteral falseLiteral, BlockScope scope) { 331 this.addSmallFragment(falseLiteral); 332 return false; 333 } 334 335 public boolean visit(FieldReference fieldReference, BlockScope scope) { 336 this.addRealFragment(fieldReference); 337 return false; 338 } 339 340 public boolean visit(FloatLiteral floatLiteral, BlockScope scope) { 341 this.addSmallFragment(floatLiteral); 342 return false; 343 } 344 345 public boolean visit( 346 InstanceOfExpression instanceOfExpression, 347 BlockScope scope) { 348 this.addRealFragment(instanceOfExpression); 349 return false; 350 } 351 352 public boolean visit(IntLiteral intLiteral, BlockScope scope) { 353 this.addSmallFragment(intLiteral); 354 return false; 355 } 356 357 public boolean visit(LongLiteral longLiteral, BlockScope scope) { 358 this.addSmallFragment(longLiteral); 359 return false; 360 } 361 362 public boolean visit(MessageSend messageSend, BlockScope scope) { 363 this.addRealFragment(messageSend); 364 return false; 365 } 366 367 public boolean visit(StringLiteralConcatenation stringLiteral, BlockScope scope) { 368 if (((stringLiteral.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) != 0) { 369 addRealFragment(stringLiteral); 370 return false; 371 } else { 372 for (int i = 0, max = stringLiteral.counter; i < max; i++) { 373 this.addRealFragment(stringLiteral.literals[i]); 374 if (i < max - 1) { 375 this.operatorsList.add(new Integer (TerminalTokens.TokenNamePLUS)); 376 } 377 } 378 return false; 379 } 380 } 381 382 public boolean visit(NullLiteral nullLiteral, BlockScope scope) { 383 this.addRealFragment(nullLiteral); 384 return false; 385 } 386 387 public boolean visit(OR_OR_Expression or_or_Expression, BlockScope scope) { 388 if (((or_or_Expression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT) != 0) { 389 addRealFragment(or_or_Expression); 390 } else { 391 or_or_Expression.left.traverse(this, scope); 392 this.operatorsList.add(new Integer (TerminalTokens.TokenNameOR_OR)); 393 or_or_Expression.right.traverse(this, scope); 394 } 395 return false; 396 } 397 398 public boolean visit( 399 PostfixExpression postfixExpression, 400 BlockScope scope) { 401 this.addRealFragment(postfixExpression); 402 return false; 403 } 404 405 public boolean visit(PrefixExpression prefixExpression, BlockScope scope) { 406 this.addRealFragment(prefixExpression); 407 return false; 408 } 409 410 public boolean visit( 411 QualifiedAllocationExpression qualifiedAllocationExpression, 412 BlockScope scope) { 413 this.addRealFragment(qualifiedAllocationExpression); 414 return false; 415 } 416 public boolean visit( 417 QualifiedNameReference qualifiedNameReference, 418 BlockScope scope) { 419 this.addRealFragment(qualifiedNameReference); 420 return false; 421 } 422 423 426 public boolean visit( 427 QualifiedSuperReference qualifiedSuperReference, 428 BlockScope scope) { 429 this.addRealFragment(qualifiedSuperReference); 430 return false; 431 } 432 433 436 public boolean visit( 437 QualifiedThisReference qualifiedThisReference, 438 BlockScope scope) { 439 this.addRealFragment(qualifiedThisReference); 440 return false; 441 } 442 443 public boolean visit( 444 SingleNameReference singleNameReference, 445 BlockScope scope) { 446 this.addRealFragment(singleNameReference); 447 return false; 448 } 449 450 public boolean visit(StringLiteral stringLiteral, BlockScope scope) { 451 this.addRealFragment(stringLiteral); 452 return false; 453 } 454 455 458 public boolean visit(SuperReference superReference, BlockScope scope) { 459 this.addRealFragment(superReference); 460 return false; 461 } 462 463 public boolean visit(ThisReference thisReference, BlockScope scope) { 464 this.addRealFragment(thisReference); 465 return false; 466 } 467 468 public boolean visit(TrueLiteral trueLiteral, BlockScope scope) { 469 this.addSmallFragment(trueLiteral); 470 return false; 471 } 472 473 public boolean visit(UnaryExpression unaryExpression, BlockScope scope) { 474 this.addRealFragment(unaryExpression); 475 return false; 476 } 477 478 public int size() { 479 return this.fragmentsList.size(); 480 } 481 } 482 | Popular Tags |