1 package com.puppycrawl.tools.checkstyle.api; 20 21 import java.util.BitSet ; 22 23 import antlr.CommonAST; 24 import antlr.Token; 25 import antlr.collections.AST; 26 27 38 public final class DetailAST extends CommonAST 39 { 40 41 private static final int NOT_INITIALIZED = Integer.MIN_VALUE; 42 43 44 private int mLineNo = NOT_INITIALIZED; 45 46 private int mColumnNo = NOT_INITIALIZED; 47 48 49 private int mChildCount = NOT_INITIALIZED; 50 51 private DetailAST mParent; 52 53 private DetailAST mPreviousSibling; 54 55 60 private BitSet mBranchTokenTypes; 61 62 63 public void initialize(Token aTok) 64 { 65 super.initialize(aTok); 66 mLineNo = aTok.getLine(); 67 mColumnNo = aTok.getColumn() - 1; } 69 70 71 public void initialize(AST aAST) 72 { 73 final DetailAST da = (DetailAST) aAST; 74 setText(da.getText()); 75 setType(da.getType()); 76 mLineNo = da.getLineNo(); 77 mColumnNo = da.getColumnNo(); 78 } 79 80 84 public void setFirstChild(AST aAST) 85 { 86 mChildCount = NOT_INITIALIZED; 87 super.setFirstChild(aAST); 88 if (aAST != null) { 89 ((DetailAST) aAST).setParent(this); 90 } 91 } 92 93 97 public void setNextSibling(AST aAST) 98 { 99 super.setNextSibling(aAST); 100 if ((aAST != null) && (mParent != null)) { 101 ((DetailAST) aAST).setParent(mParent); 102 } 103 if (aAST != null) { 104 ((DetailAST) aAST).setPreviousSibling(this); 105 } 106 } 107 108 112 void setPreviousSibling(DetailAST aAST) 113 { 114 mPreviousSibling = aAST; 115 } 116 117 121 public void addChild(AST aAST) 122 { 123 super.addChild(aAST); 124 if (aAST != null) { 125 ((DetailAST) aAST).setParent(this); 126 ((DetailAST) getFirstChild()).setParent(this); 127 } 128 } 129 130 135 public int getChildCount() 136 { 137 if (mChildCount == NOT_INITIALIZED) { 139 mChildCount = 0; 140 AST child = getFirstChild(); 141 142 while (child != null) { 143 mChildCount += 1; 144 child = child.getNextSibling(); 145 } 146 } 147 return mChildCount; 148 } 149 150 154 void setParent(DetailAST aParent) 157 { 158 mParent = aParent; 161 final DetailAST nextSibling = (DetailAST) getNextSibling(); 162 if (nextSibling != null) { 163 nextSibling.setParent(aParent); 164 nextSibling.setPreviousSibling(this); 165 } 166 } 167 168 172 public DetailAST getParent() 173 { 174 return mParent; 175 } 176 177 178 public int getLineNo() 179 { 180 if (mLineNo == NOT_INITIALIZED) { 181 final DetailAST child = (DetailAST) getFirstChild(); 184 final DetailAST sibling = (DetailAST) getNextSibling(); 185 if (child != null) { 186 return child.getLineNo(); 187 } 188 else if (sibling != null) { 189 return sibling.getLineNo(); 190 } 191 } 192 return mLineNo; 193 } 194 195 196 public int getColumnNo() 197 { 198 if (mColumnNo == NOT_INITIALIZED) { 199 final DetailAST child = (DetailAST) getFirstChild(); 202 final DetailAST sibling = (DetailAST) getNextSibling(); 203 if (child != null) { 204 return child.getColumnNo(); 205 } 206 else if (sibling != null) { 207 return sibling.getColumnNo(); 208 } 209 } 210 return mColumnNo; 211 } 212 213 214 public DetailAST getLastChild() 215 { 216 AST ast = getFirstChild(); 217 while ((ast != null) && (ast.getNextSibling() != null)) { 218 ast = ast.getNextSibling(); 219 } 220 return (DetailAST) ast; 221 } 222 223 226 private BitSet getBranchTokenTypes() 227 { 228 if (mBranchTokenTypes == null) { 230 231 mBranchTokenTypes = new BitSet (); 232 mBranchTokenTypes.set(getType()); 233 234 DetailAST child = (DetailAST) getFirstChild(); 236 while (child != null) { 237 final BitSet childTypes = child.getBranchTokenTypes(); 238 mBranchTokenTypes.or(childTypes); 239 240 child = (DetailAST) child.getNextSibling(); 241 } 242 } 243 return mBranchTokenTypes; 244 } 245 246 253 public boolean branchContains(int aType) 254 { 255 return getBranchTokenTypes().get(aType); 256 } 257 258 263 public int getChildCount(int aType) 264 { 265 int count = 0; 266 for (AST i = getFirstChild(); i != null; i = i.getNextSibling()) { 267 if (i.getType() == aType) { 268 count++; 269 } 270 } 271 return count; 272 } 273 274 278 public DetailAST getPreviousSibling() 279 { 280 return mPreviousSibling; 281 } 282 283 288 public DetailAST findFirstToken(int aType) 289 { 290 DetailAST retVal = null; 291 for (AST i = getFirstChild(); i != null; i = i.getNextSibling()) { 292 if (i.getType() == aType) { 293 retVal = (DetailAST) i; 294 break; 295 } 296 } 297 return retVal; 298 } 299 300 301 public String toString() 302 { 303 return super.toString() + "[" + getLineNo() + "x" + getColumnNo() + "]"; 304 } 305 } 306 | Popular Tags |