1 2 29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify; 30 31 import java.io.File ; 32 33 import antlr.collections.AST; 34 35 import com.puppycrawl.tools.checkstyle.api.DetailAST; 36 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 37 38 39 40 55 57 public class SymTabAST 58 extends antlr.CommonASTWithHiddenTokens 59 { 60 private Scope _scope; 61 private IDefinition _definition = null; 62 private boolean _isMeaningful = true; 63 64 private File _file; 65 private int _line; 66 private int _column; 67 68 71 private Span _span; 72 73 74 private DetailAST detailNode; 75 76 84 92 97 public void setParent(SymTabAST parent) { 98 } 101 102 106 public Scope getScope() { 107 return _scope; 108 } 109 110 115 public void setScope(Scope scope) { 116 _scope = scope; 117 } 118 119 126 public void setDefinition(IDefinition definition, Scope scope) { 127 setDefinition(definition, scope, true); 128 } 129 130 139 public void setDefinition( 140 IDefinition definition, 141 Scope scope, 142 boolean createReference) { 143 _definition = definition; 144 Reference reference = new Reference(this); 145 if (scope != null) { 146 scope.addReferenceInScope(reference); 147 } 148 149 if (definition.isSourced() && createReference) { 150 _definition.addReference(reference); 151 } 152 } 153 154 158 public IDefinition getDefinition() { 159 return _definition; 160 } 161 162 166 public boolean isMeaningful() { 167 return _isMeaningful; 168 } 169 170 175 public void setMeaningfulness(boolean isMeaningful) { 176 _isMeaningful = isMeaningful; 177 } 178 179 184 public void ignoreChildren() { 185 if (getType() == TokenTypes.IDENT) { 186 setMeaningfulness(false); 187 } 188 SymTabAST child = (SymTabAST) getFirstChild(); 189 while (child != null) { 190 child.ignoreChildren(); 191 child = (SymTabAST) child.getNextSibling(); 192 } 193 } 194 195 200 public void setFile(File file) { 201 _file = file; 202 } 203 204 216 public Span finishDefinition( 217 File file, 218 SymTabAST parent) { 219 setFile(file); 220 setParent(parent); 221 222 Span result = finishChildren(file); 223 224 if (getLineNo() != 0) { 225 result.compose( 226 new Span( 227 getLineNo(), 228 getColumnNo(), 229 getLineNo(), 230 getColumnNo() 231 + ((getText() == null) ? 0 : getText().length() - 1))); 232 } 233 234 setSpan(result); 235 return result; 236 } 237 238 244 public Span finishChildren(File file) { 245 Span result = null; 246 SymTabAST current = (SymTabAST) getFirstChild(); 247 248 if (current == null) { 249 result = getSpan(); 250 } 251 else { 252 while (current != null) { 253 Span childSpan = 254 current.finishDefinition(file, this); 255 256 if (childSpan != null) { 257 if (result == null) { 258 result = new Span(childSpan); 259 } 260 else { 261 result.compose(childSpan); 262 } 263 } 264 265 current = (SymTabAST) current.getNextSibling(); 266 } 267 } 268 269 return result; 270 } 271 272 276 public File getFile() { 277 return _file; 278 } 279 280 284 public void setLine(int line) { 285 _line = line; 286 } 287 288 292 public int getLineNo() { 293 return _line; 294 } 295 296 300 public void setColumn(int column) { 301 _column = column; 302 } 303 304 308 public int getColumnNo() { 309 return _column; 310 } 311 312 317 public String getName() { 318 String result = null; 319 if (_definition != null) { 320 result = _definition.getName(); 321 } 322 323 return result; 324 } 325 326 330 public String toString() { 331 StringBuffer resultBuffer = new StringBuffer (prefixString(true)); 333 resultBuffer.append("[" + getLineNo() + "," + getColumnNo() + "]"); 334 resultBuffer.append(" in " + getFile()); 338 return resultBuffer.toString(); 340 } 341 342 public String prefixString(boolean verboseStringConversion) { 343 StringBuffer b = new StringBuffer (); 344 345 try { 346 final String name = TokenTypes.getTokenName(getType()); 347 if (verboseStringConversion && !getText().equalsIgnoreCase(name)) { 349 b.append('['); 350 b.append(getText()); 351 b.append(",<"); 352 b.append(name); 353 b.append(">]"); 354 return b.toString(); 355 } 356 } 357 catch (Exception ex) { 358 ; 359 } 360 return getText(); 361 } 362 363 367 public Span getSpan() { 368 if ((_span == null)) { 369 int endColumn = getColumnNo() + 1; 370 final String text = getText(); 371 if (text != null) { 372 endColumn += text.length() - 1; 373 } 374 _span = new Span(getLineNo(), getColumnNo() + 1, getLineNo(), endColumn); 375 } 376 return _span; 377 } 378 379 384 public void setSpan(Span span) { 385 _span = span; 386 } 387 388 400 407 public SymTabAST getEnclosingNode(int line, int column) { 408 SymTabAST result = null; 409 410 if ((getSpan() != null) && (getSpan().contains(line, column))) { 411 SymTabAST child = (SymTabAST) getFirstChild(); 412 while (child != null && result == null) { 413 result = child.getEnclosingNode(line, column); 414 child = (SymTabAST) child.getNextSibling(); 415 } 416 417 if (result == null) { 419 result = this; 420 } 421 422 } 423 return result; 424 } 425 426 public AST getFirstChild() 427 { 428 if (super.getFirstChild() == null) { 429 DetailAST childDetailAST = null; 430 final DetailAST detailAST = getDetailNode(); 431 if (detailAST != null) { 432 childDetailAST = (DetailAST) detailAST.getFirstChild(); 433 if (childDetailAST != null) { 434 final SymTabAST child = 435 SymTabASTFactory.create(childDetailAST); 436 setFirstChild(child); 437 child.setParent(this); 438 child.setFile(getFile()); 439 } 440 } 441 } 442 return super.getFirstChild(); 443 } 444 445 public AST getNextSibling() 446 { 447 if (super.getNextSibling() == null) { 448 DetailAST siblingDetailAST = null; 449 final DetailAST detailAST = getDetailNode(); 450 if (detailAST != null) { 451 siblingDetailAST = (DetailAST) detailAST.getNextSibling(); 452 if (siblingDetailAST != null) { 453 final SymTabAST sibling = 454 SymTabASTFactory.create(siblingDetailAST); 455 setNextSibling(sibling); 456 sibling.setFile(getFile()); 458 } 459 } 460 } 461 return super.getNextSibling(); 462 } 463 464 465 470 public void initialize(AST aAST) 471 { 472 if (aAST != null) { 473 super.initialize(aAST); 474 final DetailAST detailAST = (DetailAST) aAST; 475 setDetailNode(detailAST); 476 _column = detailAST.getColumnNo() + 1; 477 _line = detailAST.getLineNo(); 478 } 479 } 480 481 487 public SymTabAST findFirstToken(int type) { 488 SymTabAST result = null; 489 490 AST sibling = getFirstChild(); 491 while (sibling != null) { 492 if (sibling.getType() == type) { 493 result = (SymTabAST) sibling; 494 break; 495 } 496 sibling = sibling.getNextSibling(); 497 } 498 499 return result; 500 } 501 502 526 530 public SymTabASTIterator getChildren() 531 { 532 return new SymTabASTIterator(this); 533 } 534 535 539 public DetailAST getDetailNode() 540 { 541 return detailNode; 542 } 543 544 548 public void setDetailNode(DetailAST aDetailAST) 549 { 550 detailNode = aDetailAST; 551 ASTManager.getInstance().put(aDetailAST, this); 552 } 553 554 } | Popular Tags |