KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > lexer > yacc > SourcePosition


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
15  * Copyright (C) 2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
16  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
17  *
18  * Alternatively, the contents of this file may be used under the terms of
19  * either of the GNU General Public License Version 2 or later (the "GPL"),
20  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
21  * in which case the provisions of the GPL or the LGPL are applicable instead
22  * of those above. If you wish to allow use of your version of this file only
23  * under the terms of either the GPL or the LGPL, and not to allow others to
24  * use your version of this file under the terms of the CPL, indicate your
25  * decision by deleting the provisions above and replace them with the notice
26  * and other provisions required by the GPL or the LGPL. If you do not delete
27  * the provisions above, a recipient may use your version of this file under
28  * the terms of any one of the CPL, the GPL or the LGPL.
29  ***** END LICENSE BLOCK *****/

30  package org.jruby.lexer.yacc;
31
32 import java.io.Serializable JavaDoc;
33
34 /**
35  *
36  * Position within a source. This could have column as well, but it currently
37  * does not. A normal ruby intrepretter does not use this information in
38  * error/warning information. An IDE using this may need it though. This is
39  * trivially added if need be.
40  *
41  * @see org.jruby.lexer.yacc.ISourcePosition
42  */

43 public class SourcePosition implements ISourcePosition, Serializable JavaDoc {
44     private static final long serialVersionUID = 3762529027281400377L;
45     
46     // The file of the source
47
private final String JavaDoc file;
48     
49     // The state/end rows of the source
50
private final int startLine;
51     private final int endLine;
52     
53     // The start/end offsets of the source
54
private int startOffset;
55     private final int endOffset;
56
57     /**
58      * Creates a default source position - required for serialization.
59      */

60     public SourcePosition() {
61         this("", 0);
62     }
63     
64     /**
65      * Creates a new source position.
66      *
67      * @param file location of the source (must not be null)
68      * @param endLine what line within the source
69      */

70     public SourcePosition(String JavaDoc file, int endLine) {
71         if (file == null) { //otherwise equals() and getInstance() will fail
72
throw new NullPointerException JavaDoc();
73         }
74         this.file = file;
75         this.startLine = 0;
76         this.endLine = endLine;
77         this.startOffset = 0;
78         this.endOffset = 0;
79     }
80
81     /**
82      * Creates a new source position.
83      *
84      * @param file location of the source (must not be null)
85      * @param line what line within the source
86      */

87     public SourcePosition(String JavaDoc file, int startLine, int endLine, int startOffset, int endOffset) {
88         if (file == null) { //otherwise equals() and getInstance() will fail
89
throw new NullPointerException JavaDoc();
90         }
91         this.file = file;
92         this.startLine = startLine;
93         this.endLine = endLine;
94         this.startOffset = startOffset;
95         this.endOffset = endOffset;
96     }
97
98     /**
99      * @see org.jruby.lexer.yacc.ISourcePosition#getFile()
100      */

101     public String JavaDoc getFile() {
102         return file;
103     }
104     
105     /**
106      * Not used in interpreter
107      *
108      * @see org.jruby.lexer.yacc.ISourcePosition#getStartLine()
109      */

110     public int getStartLine() {
111         return startLine;
112     }
113
114     /**
115      * @see org.jruby.lexer.yacc.ISourcePosition#getEndLine()
116      */

117     public int getEndLine() {
118         return endLine;
119     }
120
121     /**
122      * @param object the object which should be compared
123      * @return simple Object.equals() implementation
124      */

125     public boolean equals(Object JavaDoc object) {
126         if (object == this) {
127             return true;
128         }
129         if (!(object instanceof SourcePosition)) {
130             return false;
131         }
132         
133         SourcePosition other = (SourcePosition) object;
134
135         return file.equals(other.file) && endLine == other.endLine;
136     }
137
138     /**
139      * Something we can use for identity in hashing, etc...
140      *
141      * @see java.lang.Object#hashCode()
142      */

143     public int hashCode() {
144         return file.hashCode() ^ endLine;
145     }
146
147     /**
148      * @see java.lang.Object#toString()
149      */

150     public String JavaDoc toString() {
151         return file + ":[" + startLine + "," + endLine + "]:[" +
152             getStartOffset() + "," + getEndOffset() + "]";
153     }
154     
155     /**
156      * @see org.jruby.lexer.yacc.ISourcePosition#adjustStartOffset(int)
157      */

158     public void adjustStartOffset(int relativeValue) {
159         startOffset += relativeValue;
160         if(startOffset < 0) startOffset = 0;
161     }
162     
163     /**
164      * Not used in interpreter
165      *
166      * @see org.jruby.lexer.yacc.ISourcePosition#getStartOffset()
167      */

168     public int getStartOffset() {
169         return startOffset;
170     }
171     
172     /**
173      * Not used in interpreter
174      *
175      * @see org.jruby.lexer.yacc.ISourcePosition#getEndOffset()
176      */

177     public int getEndOffset() {
178         return endOffset;
179     }
180     
181     /**
182      * @see org.jruby.lexer.yacc.ISourcePosition#union(ISourcePosition)
183      */

184     public ISourcePosition union(ISourcePosition other) {
185         // Enebo: All AST nodes but IterNode are in ascending order position-wise. We should not
186
// need to safe-guard that other is a smaller source position
187
return new SourcePosition(file, startLine, other.getEndLine(), startOffset, other.getEndOffset());
188     }
189 }
190
Popular Tags