1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.puppycrawl.tools.checkstyle.api;
20
21 import java.util.BitSet;
22
23 import antlr.CommonASTWithHiddenTokens;
24 import antlr.Token;
25 import antlr.collections.AST;
26
27
28
29
30
31
32
33
34
35
36
37 public final class DetailAST extends CommonASTWithHiddenTokens
38 {
39
40 private static final long serialVersionUID = -2580884815577559874L;
41
42
43 private static final int NOT_INITIALIZED = Integer.MIN_VALUE;
44
45
46 private int lineNo = NOT_INITIALIZED;
47
48 private int columnNo = NOT_INITIALIZED;
49
50
51 private int childCount = NOT_INITIALIZED;
52
53 private DetailAST parent;
54
55 private DetailAST previousSibling;
56
57
58
59
60
61
62 private BitSet branchTokenTypes;
63
64 @Override
65 public void initialize(Token tok)
66 {
67 super.initialize(tok);
68 lineNo = tok.getLine();
69 columnNo = tok.getColumn() - 1;
70 }
71
72 @Override
73 public void initialize(AST ast)
74 {
75 final DetailAST da = (DetailAST) ast;
76 setText(da.getText());
77 setType(da.getType());
78 lineNo = da.getLineNo();
79 columnNo = da.getColumnNo();
80 hiddenAfter = da.getHiddenAfter();
81 hiddenBefore = da.getHiddenBefore();
82 }
83
84 @Override
85 public void setFirstChild(AST ast)
86 {
87 childCount = NOT_INITIALIZED;
88 super.setFirstChild(ast);
89 if (ast != null) {
90 ((DetailAST) ast).setParent(this);
91 }
92 }
93
94 @Override
95 public void setNextSibling(AST ast)
96 {
97 super.setNextSibling(ast);
98 if (ast != null && parent != null) {
99 ((DetailAST) ast).setParent(parent);
100 }
101 if (ast != null) {
102 ((DetailAST) ast).setPreviousSibling(this);
103 }
104 }
105
106
107
108
109
110
111 public void addPreviousSibling(DetailAST ast)
112 {
113 if (ast != null) {
114 ast.setParent(parent);
115 final DetailAST previousSibling = this.getPreviousSibling();
116
117 if (previousSibling != null) {
118 ast.setPreviousSibling(previousSibling);
119 previousSibling.setNextSibling(ast);
120 }
121 else if (parent != null) {
122 parent.setFirstChild(ast);
123 }
124
125 ast.setNextSibling(this);
126 this.setPreviousSibling(ast);
127 }
128 }
129
130
131
132
133
134
135 public void addNextSibling(DetailAST ast)
136 {
137 if (ast != null) {
138 ast.setParent(parent);
139 final DetailAST nextSibling = this.getNextSibling();
140
141 if (nextSibling != null) {
142 ast.setNextSibling(nextSibling);
143 nextSibling.setPreviousSibling(ast);
144 }
145
146 ast.setPreviousSibling(this);
147 this.setNextSibling(ast);
148 }
149 }
150
151
152
153
154
155 void setPreviousSibling(DetailAST ast)
156 {
157 previousSibling = ast;
158 }
159
160 @Override
161 public void addChild(AST ast)
162 {
163 super.addChild(ast);
164 if (ast != null) {
165 ((DetailAST) ast).setParent(this);
166 getFirstChild().setParent(this);
167 }
168 }
169
170
171
172
173
174
175 public int getChildCount()
176 {
177
178 if (childCount == NOT_INITIALIZED) {
179 childCount = 0;
180 AST child = getFirstChild();
181
182 while (child != null) {
183 childCount += 1;
184 child = child.getNextSibling();
185 }
186 }
187 return childCount;
188 }
189
190
191
192
193
194
195
196 void setParent(DetailAST parent)
197 {
198
199
200 this.parent = parent;
201 final DetailAST nextSibling = getNextSibling();
202 if (nextSibling != null) {
203 nextSibling.setParent(parent);
204 nextSibling.setPreviousSibling(this);
205 }
206 }
207
208
209
210
211
212 public DetailAST getParent()
213 {
214 return parent;
215 }
216
217
218 public int getLineNo()
219 {
220 if (lineNo == NOT_INITIALIZED) {
221
222
223 DetailAST child = getFirstChild();
224 while (child != null) {
225
226 if (TokenTypes.isCommentType(child.getType())) {
227 child = child.getNextSibling();
228 }
229 else {
230 return child.getLineNo();
231 }
232 }
233
234 DetailAST sibling = getNextSibling();
235 while (sibling != null) {
236
237 if (TokenTypes.isCommentType(sibling.getType())) {
238 sibling = sibling.getNextSibling();
239 }
240 else {
241 return sibling.getLineNo();
242 }
243 }
244 }
245 return lineNo;
246 }
247
248
249
250
251
252
253 public void setLineNo(int lineNo)
254 {
255 this.lineNo = lineNo;
256 }
257
258
259 public int getColumnNo()
260 {
261 if (columnNo == NOT_INITIALIZED) {
262
263
264 DetailAST child = getFirstChild();
265 while (child != null) {
266
267 if (TokenTypes.isCommentType(child.getType())) {
268 child = child.getNextSibling();
269 }
270 else {
271 return child.getColumnNo();
272 }
273 }
274
275 DetailAST sibling = getNextSibling();
276 while (sibling != null) {
277
278 if (TokenTypes.isCommentType(sibling.getType())) {
279 sibling = sibling.getNextSibling();
280 }
281 else {
282 return sibling.getColumnNo();
283 }
284 }
285 }
286 return columnNo;
287 }
288
289
290
291
292
293
294 public void setColumnNo(int columnNo)
295 {
296 this.columnNo = columnNo;
297 }
298
299
300 public DetailAST getLastChild()
301 {
302 DetailAST ast = getFirstChild();
303 while (ast != null && ast.getNextSibling() != null) {
304 ast = ast.getNextSibling();
305 }
306 return ast;
307 }
308
309
310
311
312 private BitSet getBranchTokenTypes()
313 {
314
315 if (branchTokenTypes == null) {
316
317 branchTokenTypes = new BitSet();
318 branchTokenTypes.set(getType());
319
320
321 DetailAST child = getFirstChild();
322 while (child != null) {
323 final BitSet childTypes = child.getBranchTokenTypes();
324 branchTokenTypes.or(childTypes);
325
326 child = child.getNextSibling();
327 }
328 }
329 return branchTokenTypes;
330 }
331
332
333
334
335
336
337
338
339 public boolean branchContains(int type)
340 {
341 return getBranchTokenTypes().get(type);
342 }
343
344
345
346
347
348
349 public int getChildCount(int type)
350 {
351 int count = 0;
352 for (AST i = getFirstChild(); i != null; i = i.getNextSibling()) {
353 if (i.getType() == type) {
354 count++;
355 }
356 }
357 return count;
358 }
359
360
361
362
363
364 public DetailAST getPreviousSibling()
365 {
366 return previousSibling;
367 }
368
369
370
371
372
373
374 public DetailAST findFirstToken(int type)
375 {
376 DetailAST retVal = null;
377 for (DetailAST i = getFirstChild(); i != null; i = i.getNextSibling()) {
378 if (i.getType() == type) {
379 retVal = i;
380 break;
381 }
382 }
383 return retVal;
384 }
385
386 @Override
387 public String toString()
388 {
389 return super.toString() + "[" + getLineNo() + "x" + getColumnNo() + "]";
390 }
391
392 @Override
393 public DetailAST getNextSibling()
394 {
395 return (DetailAST) super.getNextSibling();
396 }
397
398 @Override
399 public DetailAST getFirstChild()
400 {
401 return (DetailAST) super.getFirstChild();
402 }
403
404 }