Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 19697:c152a485d747
Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Tue, 03 Mar 2015 17:55:10 -0800 |
parents | 33bdafbf285d |
children | 191c55f08ed2 |
comparison
equal
deleted
inserted
replaced
19696:9d2ff2e8360d | 19697:c152a485d747 |
---|---|
122 } | 122 } |
123 | 123 |
124 /** | 124 /** |
125 * Gets the canonical representation of a source file, whose contents have already been read and | 125 * Gets the canonical representation of a source file, whose contents have already been read and |
126 * need not be read again. It is confirmed that the file resolves to a file name, so it can be | 126 * need not be read again. It is confirmed that the file resolves to a file name, so it can be |
127 * indexed by canonical path, but it is not confirmed to be readable. | 127 * indexed by canonical path. It is not confirmed that the text supplied agrees with the file's |
128 * contents or even whether the file is readable. | |
128 * | 129 * |
129 * @param chars textual source code already read from the file | 130 * @param chars textual source code already read from the file |
130 * @param fileName | 131 * @param fileName |
131 * @return canonical representation of the file's contents. | 132 * @return canonical representation of the file's contents. |
132 * @throws IOException if the file cannot be found | 133 * @throws IOException if the file cannot be found |
133 * @throws IllegalArgumentException if there is already a Source indexed under this file name | 134 */ |
134 */ | 135 public static Source fromFileName(CharSequence chars, String fileName) throws IOException { |
135 public static Source fromFileName(CharSequence chars, String fileName) throws IOException, IllegalArgumentException { | 136 |
136 final WeakReference<Source> nameRef = filePathToSource.get(fileName); | 137 final WeakReference<Source> nameRef = filePathToSource.get(fileName); |
137 Source source = nameRef == null ? null : nameRef.get(); | 138 Source source = nameRef == null ? null : nameRef.get(); |
138 if (source == null) { | 139 if (source == null) { |
139 final File file = new File(fileName); | 140 final File file = new File(fileName); |
140 // We are going to trust that the fileName is readable. | 141 // We are going to trust that the fileName is readable. |
142 final WeakReference<Source> pathRef = filePathToSource.get(path); | 143 final WeakReference<Source> pathRef = filePathToSource.get(path); |
143 source = pathRef == null ? null : pathRef.get(); | 144 source = pathRef == null ? null : pathRef.get(); |
144 if (source == null) { | 145 if (source == null) { |
145 source = new FileSource(file, fileName, path, chars); | 146 source = new FileSource(file, fileName, path, chars); |
146 filePathToSource.put(path, new WeakReference<>(source)); | 147 filePathToSource.put(path, new WeakReference<>(source)); |
147 return source; | 148 } |
148 } | 149 } |
149 } | 150 return source; |
150 throw new IOException("Source already exists for file:" + fileName); | |
151 } | 151 } |
152 | 152 |
153 /** | 153 /** |
154 * Creates a non-canonical source from literal text. If an already created literal source must | 154 * Creates a non-canonical source from literal text. If an already created literal source must |
155 * be retrievable by name, use {@link #asPseudoFile(CharSequence, String)}. | 155 * be retrievable by name, use {@link #asPseudoFile(CharSequence, String)}. |
299 /** | 299 /** |
300 * Access to the source contents. | 300 * Access to the source contents. |
301 */ | 301 */ |
302 public final InputStream getInputStream() { | 302 public final InputStream getInputStream() { |
303 return new ByteArrayInputStream(getCode().getBytes()); | 303 return new ByteArrayInputStream(getCode().getBytes()); |
304 } | |
305 | |
306 /** | |
307 * Gets the number of characters in the source. | |
308 */ | |
309 public final int getLength() { | |
310 return checkTextMap().length(); | |
304 } | 311 } |
305 | 312 |
306 /** | 313 /** |
307 * Returns the complete text of the code. | 314 * Returns the complete text of the code. |
308 */ | 315 */ |
1134 public int offsetToCol(int offset) throws IllegalArgumentException { | 1141 public int offsetToCol(int offset) throws IllegalArgumentException { |
1135 return 1 + offset - nlOffsets[offsetToLine(offset) - 1]; | 1142 return 1 + offset - nlOffsets[offsetToLine(offset) - 1]; |
1136 } | 1143 } |
1137 | 1144 |
1138 /** | 1145 /** |
1146 * The number of characters in the mapped text. | |
1147 */ | |
1148 public int length() { | |
1149 return textLength; | |
1150 } | |
1151 | |
1152 /** | |
1139 * The number of lines in the text; if characters appear after the final newline, then they | 1153 * The number of lines in the text; if characters appear after the final newline, then they |
1140 * also count as a line, even though not newline-terminated. | 1154 * also count as a line, even though not newline-terminated. |
1141 */ | 1155 */ |
1142 public int lineCount() { | 1156 public int lineCount() { |
1143 if (textLength == 0) { | 1157 if (textLength == 0) { |