comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/SourceSection.java @ 11610:5f532ea846fb

applied changes to basic-graal that were made in a downstream repo
author Doug Simon <doug.simon@oracle.com>
date Thu, 12 Sep 2013 14:43:21 +0200
parents 494b818b527c
children 69d2e4baa215
comparison
equal deleted inserted replaced
11609:103795ab699d 11610:5f532ea846fb
23 * questions. 23 * questions.
24 */ 24 */
25 package com.oracle.truffle.api; 25 package com.oracle.truffle.api;
26 26
27 /** 27 /**
28 * Represents a contiguous text section within the source code of a guest language program. 28 * Description of contiguous text section within the source code of a guest language program.
29 */ 29 */
30 public class SourceSection { 30 public interface SourceSection {
31
32 private final Source source;
33 private final String identifier;
34 private final int startLine;
35 private final int startColumn;
36 private final int charIndex;
37 private final int charLength;
38
39 /**
40 * Creates a new object representing a contiguous text section within the source code of a guest
41 * language program.
42 * <p>
43 * The starting location of the section is specified using two different coordinate:
44 * <ul>
45 * <li><b>(row, column)</b>: rows and columns are 1-based, so the first character in a source
46 * file is at position {@code (1,1)}.</li>
47 * <li><b>character index</b>: 0-based offset of the character from the beginning of the source,
48 * so the first character in a file is at index {@code 0}.</li>
49 * </ul>
50 * The <b>newline</b> that terminates each line counts as a single character for the purpose of
51 * a character index. The (row,column) coordinates of a newline character should never appear in
52 * a text section.
53 * <p>
54 *
55 * @param source object representing the complete source program that contains this section
56 * @param identifier an identifier used when printing the section
57 * @param startLine the 1-based number of the start line of the section
58 * @param startColumn the 1-based number of the start column of the section
59 * @param charIndex the 0-based index of the first character of the section
60 * @param charLength the length of the section in number of characters
61 */
62 public SourceSection(Source source, String identifier, int startLine, int startColumn, int charIndex, int charLength) {
63 this.source = source;
64 this.identifier = identifier;
65 this.startLine = startLine;
66 this.startColumn = startColumn;
67 this.charIndex = charIndex;
68 this.charLength = charLength;
69 }
70 31
71 /** 32 /**
72 * Returns the object representing the source program that contains this section. 33 * Returns the object representing the source program that contains this section.
73 * 34 *
74 * @return the source object 35 * @return the source object
75 */ 36 */
76 public final Source getSource() { 37 Source getSource();
77 return source;
78 }
79 38
80 /** 39 /**
81 * Returns 1-based line number of the first character in this source section (inclusive). 40 * Returns 1-based line number of the first character in this source section (inclusive).
82 * 41 *
83 * @return the starting line number 42 * @return the starting line number
84 */ 43 */
85 public final int getStartLine() { 44 int getStartLine();
86 return startLine;
87 }
88 45
89 /** 46 /**
90 * Returns the 1-based column number of the first character in this source section (inclusive). 47 * Returns the 1-based column number of the first character in this source section (inclusive).
91 * 48 *
92 * @return the starting column number 49 * @return the starting column number
93 */ 50 */
94 public final int getStartColumn() { 51 int getStartColumn();
95 return startColumn;
96 }
97 52
98 /** 53 /**
99 * Returns the 0-based index of the first character in this source section. 54 * Returns the 0-based index of the first character in this source section.
100 * <p> 55 * <p>
101 * The complete text of the source that contains this section can be retrieved via 56 * The complete text of the source that contains this section can be retrieved via
102 * {@link Source#getCode()}. 57 * {@link Source#getCode()}.
103 * 58 *
104 * @return the starting character index 59 * @return the starting character index
105 */ 60 */
106 public final int getCharIndex() { 61 int getCharIndex();
107 return charIndex;
108 }
109 62
110 /** 63 /**
111 * Returns the length of this source section in characters. 64 * Returns the length of this source section in characters.
112 * <p> 65 * <p>
113 * The complete text of the source that contains this section can be retrieved via 66 * The complete text of the source that contains this section can be retrieved via
114 * {@link Source#getCode()}. 67 * {@link Source#getCode()}.
115 * 68 *
116 * @return the number of characters in the section 69 * @return the number of characters in the section
117 */ 70 */
118 public final int getCharLength() { 71 int getCharLength();
119 return charLength; 72
120 } 73 /**
74 * Returns the index of the text position immediately following the last character in the
75 * section.
76 *
77 * @return the end position of the section
78 */
79 int getCharEndIndex();
121 80
122 /** 81 /**
123 * Returns the identifier of this source section that is used for printing the section. 82 * Returns the identifier of this source section that is used for printing the section.
124 * 83 *
125 * @return the identifier of the section 84 * @return the identifier of the section
126 */ 85 */
127 public final String getIdentifier() { 86 String getIdentifier();
128 return identifier;
129 }
130 87
131 /** 88 /**
132 * Returns text of the code represented by this source section. 89 * Returns text of the code represented by this source section.
133 * 90 *
134 * @return the code as a String object 91 * @return the code as a String object
135 */ 92 */
136 public final String getCode() { 93 String getCode();
137 return getSource().getCode().substring(charIndex, charIndex + charLength);
138 }
139
140 @Override
141 public String toString() {
142 return String.format("%s:%d", source.getName(), startLine);
143 }
144
145 @Override
146 public int hashCode() {
147 final int prime = 31;
148 int result = 1;
149 result = prime * result + charIndex;
150 result = prime * result + charLength;
151 result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
152 result = prime * result + ((source == null) ? 0 : source.hashCode());
153 result = prime * result + startColumn;
154 result = prime * result + startLine;
155 return result;
156 }
157
158 @Override
159 public boolean equals(Object obj) {
160 if (this == obj) {
161 return true;
162 }
163 if (obj == null) {
164 return false;
165 }
166 if (!(obj instanceof SourceSection)) {
167 return false;
168 }
169 SourceSection other = (SourceSection) obj;
170 if (charIndex != other.charIndex) {
171 return false;
172 }
173 if (charLength != other.charLength) {
174 return false;
175 }
176 if (identifier == null) {
177 if (other.identifier != null) {
178 return false;
179 }
180 } else if (!identifier.equals(other.identifier)) {
181 return false;
182 }
183 if (source == null) {
184 if (other.source != null) {
185 return false;
186 }
187 } else if (!source.equals(other.source)) {
188 return false;
189 }
190 if (startColumn != other.startColumn) {
191 return false;
192 }
193 if (startLine != other.startLine) {
194 return false;
195 }
196 return true;
197 }
198 94
199 } 95 }