KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > Position


1 package polyglot.util;
2
3 import java.io.Serializable JavaDoc;
4
5 /**
6  * This class represents a posiiton within a file.
7  **/

8 public class Position implements Serializable JavaDoc
9 {
10     static final long serialVersionUID = -4588386982624074261L;
11
12     private String JavaDoc file;
13     private int line;
14     private int column;
15     
16     private int endLine;
17     private int endColumn;
18
19     public static final int UNKNOWN = -1;
20     public static final int END_UNUSED = -2;
21     public static final Position COMPILER_GENERATED = new Position("Compiler Generated");
22     
23     /**
24      * Get a compiler generated position using the caller at the given stack
25      * depth. Depth 1 is the caller. Depth 2 is the caller's caller, etc.
26      */

27     public static Position compilerGenerated(int depth) {
28         Position p = new Position();
29         StackTraceElement JavaDoc[] stack = new Exception JavaDoc().getStackTrace();
30         if (depth < stack.length) {
31             return new Position(stack[depth].getFileName() + " (compiler generated)", stack[depth].getLineNumber());
32         }
33         else {
34             return COMPILER_GENERATED;
35         }
36     }
37
38     /** Get a compiler generated position. */
39     public static Position compilerGenerated() {
40         return compilerGenerated(2);
41     }
42
43     /** For deserialization. */
44     protected Position() { }
45
46     public Position(String JavaDoc file) {
47     this(file, UNKNOWN, UNKNOWN);
48     }
49
50     public Position(String JavaDoc file, int line) {
51     this(file, line, UNKNOWN);
52     }
53
54     public Position(String JavaDoc file, int line, int column) {
55         this(file, line, column, END_UNUSED, END_UNUSED);
56     }
57     
58     public Position(String JavaDoc file, int line, int column, int endLine, int endColumn) {
59         this.file = file;
60         this.line = line;
61         this.column = column;
62         this.endLine = endLine;
63         this.endColumn = endColumn;
64     }
65
66     public Position(Position start, Position end) {
67         this(start.file(), start.line, start.column, end.endLine, end.endColumn);
68     }
69     
70     public int line() {
71     return line;
72     }
73
74     public int column() {
75     return column;
76     }
77
78     public int endLine() {
79         if (endLine == UNKNOWN || (line != UNKNOWN && endLine < line)) {
80             return line;
81         }
82         return endLine;
83     }
84
85     public int endColumn() {
86         if (endColumn == UNKNOWN || (column != UNKNOWN && endLine()==line() && endColumn < column)) {
87             return column;
88         }
89         return endColumn;
90     }
91
92     public String JavaDoc file() {
93     return file;
94     }
95
96     public String JavaDoc nameAndLineString() {
97     String JavaDoc s = file;
98
99     if (line != UNKNOWN) {
100         s += ":" + line;
101             if (endLine != line &&
102                       endLine != UNKNOWN && endLine != END_UNUSED) {
103                 s += "-" + endLine;
104             }
105     }
106
107     return s;
108     }
109
110     public String JavaDoc toString() {
111         String JavaDoc s = file;
112
113         if (line != UNKNOWN) {
114             s += ":" + line;
115         
116             if (column != UNKNOWN) {
117                 s += "," + column;
118                 if (line == endLine &&
119                   endColumn != UNKNOWN && endColumn != END_UNUSED) {
120                     s += "-" + endColumn;
121                 }
122                 if (line != endLine &&
123                       endColumn != UNKNOWN && endColumn != END_UNUSED) {
124                     s += "-" + endLine + ":" + endColumn;
125                 }
126             }
127         }
128         
129         return s;
130     }
131 }
132
Popular Tags