comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 20906:c8b83aa6cc82

Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Mon, 13 Apr 2015 10:55:15 -0700
parents bbf53b35292e
children a43c7adc9d99
comparison
equal deleted inserted replaced
20900:ca13a009e38b 20906:c8b83aa6cc82
76 public abstract class Source { 76 public abstract class Source {
77 77
78 // TODO (mlvdv) consider canonicalizing and reusing SourceSection instances 78 // TODO (mlvdv) consider canonicalizing and reusing SourceSection instances
79 // TOOD (mlvdv) connect SourceSections into a spatial tree for fast geometric lookup 79 // TOOD (mlvdv) connect SourceSections into a spatial tree for fast geometric lookup
80 80
81 public enum Tags implements SourceTag {
82
83 /**
84 * From bytes.
85 */
86 FROM_BYTES("bytes", "read from bytes"),
87
88 /**
89 * Read from a file.
90 */
91 FROM_FILE("file", "read from a file"),
92
93 /**
94 * From literal text.
95 */
96 FROM_LITERAL("literal", "from literal text"),
97
98 /**
99 * From a {@linkplain java.io.Reader Reader}.
100 */
101 FROM_READER("reader", "read from a Java Reader"),
102
103 /**
104 * Read from a URL.
105 */
106 FROM_URL("URL", "read from a URL");
107
108 private final String name;
109 private final String description;
110
111 private Tags(String name, String description) {
112 this.name = name;
113 this.description = description;
114 }
115
116 public String getName() {
117 return name;
118 }
119
120 public String getDescription() {
121 return description;
122 }
123
124 }
125
81 /** 126 /**
82 * All Sources that have been created. 127 * All Sources that have been created.
83 */ 128 */
84 private static final List<WeakReference<Source>> allSources = Collections.synchronizedList(new ArrayList<WeakReference<Source>>()); 129 private static final List<WeakReference<Source>> allSources = Collections.synchronizedList(new ArrayList<WeakReference<Source>>());
85 130
117 } 162 }
118 } 163 }
119 if (reset) { 164 if (reset) {
120 source.reset(); 165 source.reset();
121 } 166 }
122 notifyNewSource(source).tagAs(StandardSourceTag.FROM_FILE); 167 notifyNewSource(source).tagAs(Tags.FROM_FILE);
123 return source; 168 return source;
124 } 169 }
125 170
126 /** 171 /**
127 * Gets the canonical representation of a source file, whose contents will be read lazily and 172 * Gets the canonical representation of a source file, whose contents will be read lazily and
159 if (source == null) { 204 if (source == null) {
160 source = new FileSource(file, fileName, path, chars); 205 source = new FileSource(file, fileName, path, chars);
161 filePathToSource.put(path, new WeakReference<>(source)); 206 filePathToSource.put(path, new WeakReference<>(source));
162 } 207 }
163 } 208 }
164 notifyNewSource(source).tagAs(StandardSourceTag.FROM_FILE); 209 notifyNewSource(source).tagAs(Tags.FROM_FILE);
165 return source; 210 return source;
166 } 211 }
167 212
168 /** 213 /**
169 * Creates a non-canonical source from literal text. If an already created literal source must 214 * Creates a non-canonical source from literal text. If an already created literal source must
174 * @return a newly created, non-indexed source representation 219 * @return a newly created, non-indexed source representation
175 */ 220 */
176 public static Source fromText(CharSequence chars, String description) { 221 public static Source fromText(CharSequence chars, String description) {
177 assert chars != null; 222 assert chars != null;
178 final LiteralSource source = new LiteralSource(description, chars.toString()); 223 final LiteralSource source = new LiteralSource(description, chars.toString());
179 notifyNewSource(source).tagAs(StandardSourceTag.FROM_LITERAL); 224 notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
180 return source; 225 return source;
181 } 226 }
182 227
183 /** 228 /**
184 * Creates a source whose contents will be read immediately from a URL and cached. 229 * Creates a source whose contents will be read immediately from a URL and cached.
188 * @return a newly created, non-indexed source representation 233 * @return a newly created, non-indexed source representation
189 * @throws IOException if reading fails 234 * @throws IOException if reading fails
190 */ 235 */
191 public static Source fromURL(URL url, String description) throws IOException { 236 public static Source fromURL(URL url, String description) throws IOException {
192 final URLSource source = URLSource.get(url, description); 237 final URLSource source = URLSource.get(url, description);
193 notifyNewSource(source).tagAs(StandardSourceTag.FROM_URL); 238 notifyNewSource(source).tagAs(Tags.FROM_URL);
194 return source; 239 return source;
195 } 240 }
196 241
197 /** 242 /**
198 * Creates a source whose contents will be read immediately and cached. 243 * Creates a source whose contents will be read immediately and cached.
202 * @return a newly created, non-indexed source representation 247 * @return a newly created, non-indexed source representation
203 * @throws IOException if reading fails 248 * @throws IOException if reading fails
204 */ 249 */
205 public static Source fromReader(Reader reader, String description) throws IOException { 250 public static Source fromReader(Reader reader, String description) throws IOException {
206 final LiteralSource source = new LiteralSource(description, read(reader)); 251 final LiteralSource source = new LiteralSource(description, read(reader));
207 notifyNewSource(source).tagAs(StandardSourceTag.FROM_READER); 252 notifyNewSource(source).tagAs(Tags.FROM_READER);
208 return source; 253 return source;
209 } 254 }
210 255
211 /** 256 /**
212 * Creates a source from raw bytes. This can be used if the encoding of strings in your language 257 * Creates a source from raw bytes. This can be used if the encoding of strings in your language
235 * @param decoder how to decode the bytes into Java strings 280 * @param decoder how to decode the bytes into Java strings
236 * @return a newly created, non-indexed source representation 281 * @return a newly created, non-indexed source representation
237 */ 282 */
238 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) { 283 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) {
239 final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder); 284 final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder);
240 notifyNewSource(source).tagAs(StandardSourceTag.FROM_BYTES); 285 notifyNewSource(source).tagAs(Tags.FROM_BYTES);
241 return source; 286 return source;
242 } 287 }
243 288
244 /** 289 /**
245 * Creates a source from literal text, but which acts as a file and can be retrieved by name 290 * Creates a source from literal text, but which acts as a file and can be retrieved by name
250 * @return a newly created, source representation, canonical with respect to its name 295 * @return a newly created, source representation, canonical with respect to its name
251 */ 296 */
252 public static Source asPseudoFile(CharSequence chars, String pseudoFileName) { 297 public static Source asPseudoFile(CharSequence chars, String pseudoFileName) {
253 final Source source = new LiteralSource(pseudoFileName, chars.toString()); 298 final Source source = new LiteralSource(pseudoFileName, chars.toString());
254 filePathToSource.put(pseudoFileName, new WeakReference<>(source)); 299 filePathToSource.put(pseudoFileName, new WeakReference<>(source));
255 notifyNewSource(source).tagAs(StandardSourceTag.FROM_LITERAL); 300 notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
256 return source; 301 return source;
257 } 302 }
258 303
259 // TODO (mlvdv) enable per-file choice whether to cache? 304 // TODO (mlvdv) enable per-file choice whether to cache?
260 /** 305 /**