1 package org.codehaus.groovy.antlr;
2
3 import antlr.collections.AST;
4 import antlr.*;
5
6 import java.util.List;
7 import java.util.ArrayList;
8
9 /***
10 * We have an AST subclass so we can track source information.
11 * Very odd that ANTLR doesn't do this by default.
12 *
13 * @author Mike Spille
14 * @author Jeremy Rayner <groovy@ross-rayner.com>
15 */
16 public class GroovySourceAST extends CommonAST implements Comparable {
17 private int line;
18 private int col;
19 private int lineLast;
20 private int colLast;
21 private String snippet;
22
23 public GroovySourceAST() {
24 }
25
26 public GroovySourceAST(Token t) {
27 super(t);
28 }
29
30 public void initialize(AST ast) {
31 super.initialize(ast);
32 line = ast.getLine();
33 col = ast.getColumn();
34 }
35
36 public void initialize(Token t) {
37 super.initialize(t);
38 line = t.getLine();
39 col = t.getColumn();
40 }
41
42 public void setLast(Token last) {
43 lineLast = last.getLine();
44 colLast = last.getColumn();
45 }
46
47 public int getLineLast() {
48 return lineLast;
49 }
50
51 public void setLineLast(int lineLast) {
52 this.lineLast = lineLast;
53 }
54
55 public int getColumnLast() {
56 return colLast;
57 }
58
59 public void setColumnLast(int colLast) {
60 this.colLast = colLast;
61 }
62
63 public void setLine(int line) {
64 this.line = line;
65 }
66
67 public int getLine() {
68 return (line);
69 }
70
71 public void setColumn(int column) {
72 this.col = column;
73 }
74
75 public int getColumn() {
76 return (col);
77 }
78
79 public void setSnippet(String snippet) {
80 this.snippet = snippet;
81 }
82
83 public String getSnippet() {
84 return snippet;
85 }
86
87 public int compareTo(Object object) {
88 if (object == null) {
89 return 0;
90 }
91 if (!(object instanceof AST)) {
92 return 0;
93 }
94 AST that = (AST) object;
95
96
97
98 if (this.getLine() < that.getLine()) {
99 return -1;
100 }
101 if (this.getLine() > that.getLine()) {
102 return 1;
103 }
104
105 if (this.getColumn() < that.getColumn()) {
106 return -1;
107 }
108 if (this.getColumn() > that.getColumn()) {
109 return 1;
110 }
111
112 return 0;
113 }
114
115 public GroovySourceAST childAt(int position) {
116 List list = new ArrayList();
117 AST child = this.getFirstChild();
118 while (child != null) {
119 list.add(child);
120 child = child.getNextSibling();
121 }
122 try {
123 return (GroovySourceAST)list.get(position);
124 } catch (IndexOutOfBoundsException e) {
125 return null;
126 }
127 }
128
129 public GroovySourceAST childOfType(int type) {
130 AST child = this.getFirstChild();
131 while (child != null) {
132 if (child.getType() == type) { return (GroovySourceAST)child; }
133 child = child.getNextSibling();
134 }
135 return null;
136 }
137
138 }