001    package org.codehaus.groovy.antlr;
002    
003    import antlr.collections.AST;
004    import antlr.*;
005    
006    /**
007     * We have an AST subclass so we can track source information.
008     * Very odd that ANTLR doesn't do this by default.
009     *
010     * @author Mike Spille
011     * @author Jeremy Rayner <groovy@ross-rayner.com>
012     */
013    public class GroovySourceAST extends CommonAST implements Comparable {
014        private int line;
015        private int col;
016        private int lineLast;
017        private int colLast;
018        private String snippet;
019    
020        public GroovySourceAST() {
021        }
022    
023        public GroovySourceAST(Token t) {
024            super(t);
025        }
026    
027        public void initialize(AST ast) {
028            super.initialize(ast);
029            line = ast.getLine();
030            col = ast.getColumn();
031        }
032    
033        public void initialize(Token t) {
034            super.initialize(t);
035            line = t.getLine();
036            col = t.getColumn();
037        }
038    
039        public void setLast(Token last) {
040            lineLast = last.getLine();
041            colLast = last.getColumn();
042        }
043    
044        public int getLineLast() {
045            return lineLast;
046        }
047    
048        public void setLineLast(int lineLast) {
049            this.lineLast = lineLast;
050        }
051    
052        public int getColumnLast() {
053            return colLast;
054        }
055    
056        public void setColumnLast(int colLast) {
057            this.colLast = colLast;
058        }
059    
060        public void setLine(int line) {
061            this.line = line;
062        }
063    
064        public int getLine() {
065            return (line);
066        }
067    
068        public void setColumn(int column) {
069            this.col = column;
070        }
071    
072        public int getColumn() {
073            return (col);
074        }
075    
076        public void setSnippet(String snippet) {
077            this.snippet = snippet;
078        }
079    
080        public String getSnippet() {
081            return snippet;
082        }
083    
084        public int compareTo(Object object) {
085            if (object == null) {
086                return 0;
087            }
088            if (!(object instanceof AST)) {
089                return 0;
090            }
091            AST that = (AST) object;
092    
093            // todo - possibly check for line/col with values of 0 or less...
094    
095            if (this.getLine() < that.getLine()) {
096                return -1;
097            }
098            if (this.getLine() > that.getLine()) {
099                return 1;
100            }
101    
102            if (this.getColumn() < that.getColumn()) {
103                return -1;
104            }
105            if (this.getColumn() > that.getColumn()) {
106                return 1;
107            }
108    
109            return 0;
110        }
111    }