AST.java

biz/hammurapi/antlr/AST.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 161:51
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 376:25
Java Inspector 089 Undocumented method 2 60:5
Java Inspector 089 Undocumented method 2 69:5
Java Inspector 089 Undocumented method 2 81:5
Java Inspector 089 Undocumented method 2 86:5
Java Inspector 089 Undocumented method 2 91:5
Java Inspector 089 Method return value is not documented 2 98:5
Java Inspector 089 Undocumented method 2 107:5
Java Inspector 089 Undocumented method 2 111:5
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 118:5
Java Inspector 089 Undocumented parameter node 2 118:5
Java Inspector 089 Undocumented method 2 125:5
Java Inspector 089 Undocumented method 2 132:5
Java Inspector 089 Undocumented method 2 149:5
Java Inspector 089 Undocumented method 2 153:5
Java Inspector 089 Undocumented method 2 208:9
Java Inspector 089 Undocumented method 2 212:9
Java Inspector 089 Undocumented method 2 231:9
Java Inspector 089 Undocumented method 2 263:9
Java Inspector 089 Undocumented method 2 276:9
Java Inspector 089 Undocumented method 2 285:9
Java Inspector 089 Undocumented method 2 290:9
Java Inspector 089 Undocumented method 2 303:9
Java Inspector 089 Undocumented method 2 315:9
Java Inspector 089 Undocumented method 2 323:9
Java Inspector 089 Undocumented method 2 343:9
Java Inspector 089 Undocumented method 2 347:9
Java Inspector 089 Undocumented method 2 351:9
Java Inspector 089 Undocumented method 2 359:9
Java Inspector 089 Undocumented method 2 365:9
Java Inspector 089 Undocumented method 2 373:9
Java Inspector 089 Undocumented method 2 380:9
Java Inspector 089 Undocumented method 2 384:25
Java Inspector 089 Undocumented method 2 391:9
Java Inspector 089 Undocumented method 2 398:25
Java Inspector 089 Undocumented method 2 407:9
Java Inspector 089 Undocumented method 2 411:9
Java Inspector 089 Undocumented method 2 415:9
Java Inspector 089 Parameter type is not documented 2 424:9
Java Inspector 089 Method return value is not properly documented 2 424:9
Java Inspector 089 Parameter type is not documented 2 437:9
Java Inspector 089 Method return value is not documented 2 437:9
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 102:35
Java Inspector 024 Avoid hardwired character literals 3 310:69
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 361:35
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 367:54
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 368:50
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 369:29
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 369:90
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 369:113
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 369:134
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 369:159
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 381:53
Java Inspector 054 Discourage usage of instance variables like a, j by enforcing minimal variable name length (3). 3 52:5

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.antlr;
24
25import java.awt.event.WindowAdapter;
26import java.awt.event.WindowEvent;
27import java.io.PrintStream;
28import java.util.LinkedHashMap;
29
30import antlr.CommonAST;
31import biz.hammurapi.legacy.review.SourceMarker;
32import biz.hammurapi.util.Attributable;
33import biz.hammurapi.util.PoliteVisitor;
34import biz.hammurapi.util.Visitable;
35import biz.hammurapi.util.Visitor;
36
37
38/**
39 * Holds line and column numbers
40 * @author Pavel Vlasov
41 * @version $Revision: 1.8 $
42 */
43public class AST extends CommonAST implements SourceMarker, Visitable, Attributable {
44 /**
45 * Comment for <code>serialVersionUID</code>
46 */
47 private static final long serialVersionUID = -3283377943637002016L;
48 private int line;
49 private int column;
50 private boolean childrenQueried=false;
51 private AST parent;
52 private String id;
53 private Token token;
54 private boolean loaded=false;
55// private LanguageElement owner;
56
57 private Token firstToken;
58 private Token lastToken;
59
60 public void initialize(antlr.Token tok) {
61 super.initialize(tok);
62 line=tok.getLine();
63 column=tok.getColumn();
64 this.token=(Token) tok;
65 this.firstToken=this.token;
66 this.lastToken=this.token;
67 }
68
69 public void initialize(antlr.collections.AST node) {
70 super.initialize(node);
71 if (node instanceof AST) {
72 AST ast = (AST) node;
73 line=ast.getLine();
74 column=ast.getColumn();
75 this.token=ast.token;
76 this.firstToken=ast.firstToken;
77 this.lastToken=ast.lastToken;
78 }
79 }
80
81 public int getLine() {
82// queryChildren();
83 return line;
84 }
85
86 public int getColumn() {
87// queryChildren();
88 return column;
89 }
90
91 public Token getToken() {
92 return token;
93 }
94
95 /**
96 * Returns leftmost non-zero column among itself and all children
97 */
98 public int getLeftColumn() {
99 int ret=getColumn();
100 for (AST child=(AST) getFirstChild(); child!=null; child=(AST) child.getNextSibling()) {
101 int c=child.getLeftColumn();
102 if (c!=0 && c<ret) ret=c;
103 }
104 return ret;
105 }
106
107 public AST getParent() {
108 return parent;
109 }
110
111 protected void setParent(AST parent) {
112 this.parent=parent;
113 }
114
115 /**************************************************************************
116 * Parent calculation
117 *************************************************************************/
118 public void addChild(antlr.collections.AST node) {
119 super.addChild(node);
120 if (node instanceof AST) {
121 ((AST) node).parent=this;
122 }
123 }
124
125 public void setFirstChild(antlr.collections.AST c) {
126 super.setFirstChild(c);
127 if (c instanceof AST) {
128 ((AST) c).parent=this;
129 }
130 }
131
132 public void setNextSibling(antlr.collections.AST n) {
133 antlr.collections.AST currentNextSibling=getNextSibling();
134 if (currentNextSibling instanceof AST) {
135 ((AST) currentNextSibling).setPrevSibling(null);
136 }
137
138 super.setNextSibling(n);
139
140
141 if (n instanceof AST) {
142 ((AST) n).setParent(parent);
143 ((AST) n).setPrevSibling(this);
144 }
145 }
146
147 private AST prevSibling;
148
149 public AST getPrevSibling() {
150 return prevSibling;
151 }
152
153 protected void setPrevSibling(AST prevSibling) {
154 this.prevSibling=prevSibling;
155 }
156
157 /**
158 * Removes AST from tree
159 */
160 public void remove() {
161 if (parent!=null && parent.getFirstChild()==this) {
162 parent.setFirstChild(getNextSibling());
163 }
164
165 parent=null;
166
167 if (prevSibling==null) {
168 if (getNextSibling() instanceof AST) {
169 ((AST) getNextSibling()).setPrevSibling(null);
170 }
171 } else {
172 prevSibling.setNextSibling(getNextSibling());
173 }
174
175 setPrevSibling(null);
176 }
177
178 /**
179 * Reads column and line from children if its own are 0. Also sets first and last
180 * tokens taking children's into account.
181 */
182 public void queryChildren() {
183 if (!childrenQueried) {
184 childrenQueried=true;
185 for (AST child = (AST) getFirstChild(); child != null ; child = (AST) child.getNextSibling()) {
186 child.queryChildren();
187
188 if (line==0 && column==0 && child.getLine()!=0) {
189 line=child.getLine();
190 column=child.getColumn();
191 }
192
193 Token cft=child.getFirstToken();
194 if (cft!=null && (firstToken==null || cft.compareTo(firstToken)<0)) {
195 firstToken=cft;
196 }
197
198 Token clt=child.getLastToken();
199 if (clt!=null && (lastToken==null || clt.compareTo(lastToken)>0)) {
200 lastToken=clt;
201 }
202 }
203 }
204 }
205
206 private String sourceURL;
207
208 public String getSourceURL() {
209 return sourceURL;
210 }
211
212 public void setSourceURL(String sourceURL) {
213 this.sourceURL=sourceURL;
214 }
215
216 /**
217 * Indicates that the node has been processed by superclass and can be ignored
218 * @return Returns the loaded.
219 */
220 boolean isLoaded() {
221 return loaded;
222 }
223
224 /**
225 * @param loaded The loaded to set.
226 */
227 void setLoaded(boolean loaded) {
228 this.loaded = loaded;
229 }
230
231 public boolean equals(Object obj) {
232 if (obj==null) {
233 return false;
234 }
235
236 if (obj==this) {
237 return true;
238 }
239
240 if (getClass().equals(obj.getClass())) {
241 return false;
242 }
243
244 AST ast=(AST) obj;
245
246 if (super.equals(ast)) {
247
248 if (ast.getNumberOfChildren()!=getNumberOfChildren()) {
249 return false;
250 }
251
252 for (AST child=(AST) getFirstChild(), otherChild=(AST) ast.getFirstChild(); child!=null && otherChild!=null; child=(AST) child.getNextSibling(), otherChild=(AST) otherChild.getNextSibling()) {
253 if (!child.equals(ast.getFirstChild())) {
254 return false;
255 }
256 }
257 return true;
258 }
259
260 return false;
261 }
262
263 public int hashCode() {
264 int ret=getType();
265 ret^=getClass().getName().hashCode();
266
267 if (getText()!=null) {
268 ret^=getText().hashCode();
269 }
270 for (AST child=(AST) getFirstChild(); child!=null; child=(AST) child.getNextSibling()) {
271 ret^=child.hashCode();
272 }
273 return ret;
274 }
275
276 public int getSize() {
277 int ret=1;
278 for (AST child=(AST) getFirstChild(); child!=null; child=(AST) child.getNextSibling()) {
279 ret+=child.getSize();
280 }
281
282 return ret;
283 }
284
285 public Token getFirstToken() {
286 queryChildren();
287 return firstToken;
288 }
289
290 public Token getLastToken() {
291 queryChildren();
292 return lastToken;
293 }
294
295// protected LanguageElement getOwner() {
296// return owner;
297// }
298
299// protected void setOwner(LanguageElement owner) {
300// this.owner = owner;
301// }
302
303 public String getId() {
304 return id;
305 }
306
307 private void enumerateChildren() {
308 int i=0;
309 for (AST node=(AST) getFirstChild(); node!=null; node=(AST) node.getNextSibling(), i++) {
310 node.id = id==null ? String.valueOf(i) : id+'.'+i;
311 node.enumerateChildren();
312 }
313 }
314
315 public void enumerate() {
316 int i=0;
317 for (AST node=this; node!=null; node=(AST) node.getNextSibling(), i++) {
318 node.id=String.valueOf(i);
319 node.enumerateChildren();
320 }
321 }
322
323 public boolean accept(Visitor visitor) {
324 if (visitor.visit(this)) {
325 if (getFirstChild()!=null) {
326 ((Visitable) getFirstChild()).accept(visitor);
327 }
328
329 if (visitor instanceof PoliteVisitor) {
330 ((PoliteVisitor) visitor).leave(this);
331 }
332
333 if (getNextSibling()!=null) {
334 return ((Visitable) getNextSibling()).accept(visitor);
335 }
336
337 return true;
338 }
339
340 return false;
341 }
342
343 public Integer getSourceId() {
344 return null;
345 }
346
347 public void print(String[] tokenNames, boolean withSiblings) {
348 print(tokenNames, System.out, 0, withSiblings);
349 }
350
351 public void print(String[] tokenNames, PrintStream out, int level, boolean withSiblings) {
352 if (withSiblings) {
353 visitAll(tokenNames, this, level, out);
354 } else {
355 visit(tokenNames, this, level, out);
356 }
357 }
358
359 protected void tabs(PrintStream out, int level) {
360 for (int i = 0; i < level; i++) {
361 out.print(" ");
362 }
363 }
364
365 protected void visit(String[] tokenNames, AST node, int level, PrintStream out) {
366 tabs(out, level);
367 out.print(tokenNames[node.getType()]+" ");
368 out.print(node.getText()==null ? "nil" : node.getText());
369 out.println(" "+node.getLine()+(node.getColumn()==node.getLeftColumn() ? ":"+node.getColumn() : ":"+node.getColumn()+"("+node.getLeftColumn()+") "));
370 visitAll(tokenNames, (AST) node.getFirstChild(), level+1, out);
371 }
372
373 protected void visitAll(String[] tokenNames, AST node, int level, PrintStream out) {
374 while (node!=null) {
375 visit(tokenNames, node, level, out);
376 node = (AST) node.getNextSibling();
377 }
378 }
379
380 public void show(String[] tokenNames) {
381 final ASTFrame frame = new ASTFrame("AST: "+getText(), this, tokenNames);
382 frame.setVisible(true);
383 frame.addWindowListener(new WindowAdapter() {
384 public void windowClosing(WindowEvent e) {
385 frame.setVisible(false); // hide the Frame
386 frame.dispose();
387 }
388 });
389 }
390
391 public void showWithSiblings(String title, String[] tokenNames) {
392 AST root=new AST();
393 root.setText(title);
394 root.setFirstChild(this);
395 final ASTFrame frame = new ASTFrame(title, root, tokenNames);
396 frame.setVisible(true);
397 frame.addWindowListener(new WindowAdapter() {
398 public void windowClosing(WindowEvent e) {
399 frame.setVisible(false); // hide the Frame
400 frame.dispose();
401 }
402 });
403 }
404
405 private LinkedHashMap attributes = new LinkedHashMap();
406
407 public void setAttribute(Object key, Object value) {
408 attributes.put(key, value);
409 }
410
411 public Object getAttribute(Object key) {
412 return attributes.get(key);
413 }
414
415 public Object removeAttribute(Object key) {
416 return attributes.remove(key);
417 }
418
419 /**
420 * Finds AST of given type in this AST and siblings.
421 * @param type
422 * @return
423 */
424 public AST find(int type) {
425 if (type == getType()) {
426 return this;
427 }
428
429 AST nextSibling = (AST) getNextSibling();
430 return nextSibling == null ? null : nextSibling.find(type);
431 }
432
433 /**
434 * Finds child of given type.
435 * @param type
436 */
437 public AST findChild(int type) {
438 AST firstChild = (AST) getFirstChild();
439 return firstChild == null ? null : firstChild.find(type);
440 }
441}
442