annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 21972:ff6f34159b8a

Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Tue, 23 Jun 2015 12:44:41 +0200
parents 5023b913e2ba
children b2d1c8ff592a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1 /*
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
4 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
10 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
15 * accompanied this code).
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
16 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
20 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
23 * questions.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
24 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
25 package com.oracle.truffle.api.source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
26
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
27 import java.io.*;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
28 import java.lang.ref.*;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
29 import java.net.*;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
30 import java.util.*;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
31
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
32 import com.oracle.truffle.api.*;
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
33 import com.oracle.truffle.api.instrument.*;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
34
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
35 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
36 * Representation of a guest language source code unit and its contents. Sources originate in
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
37 * several ways:
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
38 * <ul>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
39 * <li><strong>Literal:</strong> An anonymous text string: not named and not indexed. These should
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
40 * be considered value objects; equality is defined based on contents.<br>
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
41 * See {@link Source#fromText(CharSequence, String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
42 * <p>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
43 * <li><strong>Named Literal:</strong> A text string that can be retrieved by name as if it were a
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
44 * file, but without any assumption that the name is related to a file path. Creating a new literal
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
45 * with an already existing name will replace its predecessor in the index.<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
46 * See {@link Source#fromNamedText(CharSequence, String)}<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
47 * See {@link Source#find(String)}</li>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
48 * <p>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
49 * <li><strong>File:</strong> Each file is represented as a canonical object, indexed by the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
50 * absolute, canonical path name of the file. File contents are <em>read lazily</em> and contents
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
51 * optionally <em>cached</em>. <br>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
52 * See {@link Source#fromFileName(String)}<br>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
53 * See {@link Source#fromFileName(String, boolean)}<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
54 * See {@link Source#find(String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
55 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
56 * <li><strong>URL:</strong> Each URL source is represented as a canonical object, indexed by the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
57 * URL. Contents are <em>read eagerly</em> and <em>cached</em>. <br>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
58 * See {@link Source#fromURL(URL, String)}<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
59 * See {@link Source#find(String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
60 * <p>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
61 * <li><strong>Reader:</strong> Contents are <em>read eagerly</em> and treated as an anonymous
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
62 * (non-indexed) <em>Literal</em> . <br>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
63 * See {@link Source#fromReader(Reader, String)}</li>
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
64 * <p>
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
65 * <li><strong>Sub-Source:</strong> A representation of the contents of a sub-range of another
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
66 * {@link Source}.<br>
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
67 * See @link {@link Source#subSource(Source, int, int)}<br>
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
68 * See @link {@link Source#subSource(Source, int)}</li>
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
69 * <p>
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
70 * <li><strong>AppendableSource:</strong> Literal contents are provided by the client,
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
71 * incrementally, after the instance is created.<br>
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
72 * See {@link Source#fromAppendableText(String)}<br>
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
73 * See {@link Source#fromNamedAppendableText(String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
74 * </ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
75 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
76 * <strong>File cache:</strong>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
77 * <ol>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
78 * <li>File content caching is optional, <em>on</em> by default.</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
79 * <li>The first access to source file contents will result in the contents being read, and (if
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
80 * enabled) cached.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
81 * <li>If file contents have been cached, access to contents via {@link Source#getInputStream()} or
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
82 * {@link Source#getReader()} will be provided from the cache.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
83 * <li>Any access to file contents via the cache will result in a timestamp check and possible cache
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
84 * reload.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
85 * </ol>
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
86 * <p>
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
87 *
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
88 * @see SourceTag
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
89 * @see SourceListener
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
90 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
91 public abstract class Source {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
92
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
93 // TODO (mlvdv) consider canonicalizing and reusing SourceSection instances
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
94 // TOOD (mlvdv) connect SourceSections into a spatial tree for fast geometric lookup
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
95
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
96 public enum Tags implements SourceTag {
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
97
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
98 /**
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
99 * From bytes.
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
100 */
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
101 FROM_BYTES("bytes", "read from bytes"),
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
102
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
103 /**
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
104 * Read from a file.
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
105 */
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
106 FROM_FILE("file", "read from a file"),
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
107
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
108 /**
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
109 * From literal text.
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
110 */
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
111 FROM_LITERAL("literal", "from literal text"),
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
112
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
113 /**
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
114 * From a {@linkplain java.io.Reader Reader}.
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
115 */
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
116 FROM_READER("reader", "read from a Java Reader"),
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
117
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
118 /**
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
119 * Read from a URL.
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
120 */
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
121 FROM_URL("URL", "read from a URL");
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
122
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
123 private final String name;
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
124 private final String description;
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
125
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
126 private Tags(String name, String description) {
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
127 this.name = name;
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
128 this.description = description;
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
129 }
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
130
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
131 public String getName() {
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
132 return name;
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
133 }
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
134
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
135 public String getDescription() {
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
136 return description;
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
137 }
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
138
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
139 }
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
140
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
141 /**
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
142 * All Sources that have been created.
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
143 */
20022
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
144 private static final List<WeakReference<Source>> allSources = Collections.synchronizedList(new ArrayList<WeakReference<Source>>());
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
145
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
146 /**
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
147 * Index of all named sources.
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
148 */
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
149 private static final Map<String, WeakReference<Source>> nameToSource = new HashMap<>();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
150
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
151 private static boolean fileCacheEnabled = true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
152
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
153 private static final List<SourceListener> sourceListeners = new ArrayList<>();
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
154
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
155 /**
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
156 * Locates an existing instance by the name under which it was indexed.
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
157 */
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
158 public static Source find(String name) {
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
159 final WeakReference<Source> nameRef = nameToSource.get(name);
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
160 return nameRef == null ? null : nameRef.get();
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
161 }
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
162
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
163 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
164 * Gets the canonical representation of a source file, whose contents will be read lazily and
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
165 * then cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
166 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
167 * @param fileName name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
168 * @param reset forces any existing {@link Source} cache to be cleared, forcing a re-read
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
169 * @return canonical representation of the file's contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
170 * @throws IOException if the file can not be read
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
171 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
172 public static Source fromFileName(String fileName, boolean reset) throws IOException {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
173
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
174 final WeakReference<Source> nameRef = nameToSource.get(fileName);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
175 Source source = nameRef == null ? null : nameRef.get();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
176 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
177 final File file = new File(fileName);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
178 if (!file.canRead()) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
179 throw new IOException("Can't read file " + fileName);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
180 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
181 final String path = file.getCanonicalPath();
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
182 final WeakReference<Source> pathRef = nameToSource.get(path);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
183 source = pathRef == null ? null : pathRef.get();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
184 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
185 source = new FileSource(file, fileName, path);
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
186 nameToSource.put(path, new WeakReference<>(source));
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
187 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
188 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
189 if (reset) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
190 source.reset();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
191 }
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
192 notifyNewSource(source).tagAs(Tags.FROM_FILE);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
193 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
194 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
195
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
196 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
197 * Gets the canonical representation of a source file, whose contents will be read lazily and
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
198 * then cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
199 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
200 * @param fileName name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
201 * @return canonical representation of the file's contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
202 * @throws IOException if the file can not be read
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
203 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
204 public static Source fromFileName(String fileName) throws IOException {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
205 return fromFileName(fileName, false);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
206 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
207
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
208 /**
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
209 * Gets the canonical representation of a source file, whose contents have already been read and
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
210 * need not be read again. It is confirmed that the file resolves to a file name, so it can be
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
211 * indexed by canonical path. It is not confirmed that the text supplied agrees with the file's
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
212 * contents or even whether the file is readable.
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
213 *
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
214 * @param chars textual source code already read from the file
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
215 * @param fileName
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
216 * @return canonical representation of the file's contents.
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
217 * @throws IOException if the file cannot be found
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
218 */
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
219 public static Source fromFileName(CharSequence chars, String fileName) throws IOException {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
220 CompilerAsserts.neverPartOfCompilation();
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
221
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
222 final WeakReference<Source> nameRef = nameToSource.get(fileName);
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
223 Source source = nameRef == null ? null : nameRef.get();
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
224 if (source == null) {
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
225 final File file = new File(fileName);
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
226 // We are going to trust that the fileName is readable.
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
227 final String path = file.getCanonicalPath();
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
228 final WeakReference<Source> pathRef = nameToSource.get(path);
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
229 source = pathRef == null ? null : pathRef.get();
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
230 if (source == null) {
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
231 source = new FileSource(file, fileName, path, chars);
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
232 nameToSource.put(path, new WeakReference<>(source));
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
233 }
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
234 }
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
235 notifyNewSource(source).tagAs(Tags.FROM_FILE);
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
236 return source;
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
237 }
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
238
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
239 /**
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
240 * Creates an anonymous source from literal text: not named and not indexed.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
241 *
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
242 * @param chars textual source code
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
243 * @param description a note about the origin, for error messages and debugging
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
244 * @return a newly created, non-indexed source representation
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
245 */
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
246 public static Source fromText(CharSequence chars, String description) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
247 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
248
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
249 assert chars != null;
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
250 final LiteralSource source = new LiteralSource(description, chars.toString());
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
251 notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
252 return source;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
253 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
254
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
255 /**
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
256 * Creates an anonymous source from literal text that is provided incrementally after creation:
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
257 * not named and not indexed.
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
258 *
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
259 * @param description a note about the origin, for error messages and debugging
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
260 * @return a newly created, non-indexed, initially empty, appendable source representation
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
261 */
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
262 public static Source fromAppendableText(String description) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
263 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
264
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
265 final Source source = new AppendableLiteralSource(description);
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
266 notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
267 return source;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
268 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
269
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
270 /**
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
271 * Creates a source from literal text that can be retrieved by name, with no assumptions about
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
272 * the structure or meaning of the name. If the name is already in the index, the new instance
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
273 * will replace the previously existing instance in the index.
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
274 *
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
275 * @param chars textual source code
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
276 * @param name string to use for indexing/lookup
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
277 * @return a newly created, source representation
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
278 */
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
279 public static Source fromNamedText(CharSequence chars, String name) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
280 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
281
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
282 final Source source = new LiteralSource(name, chars.toString());
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
283 nameToSource.put(name, new WeakReference<>(source));
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
284 notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
285 return source;
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
286 }
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
287
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
288 /**
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
289 * Creates a source from literal text that is provided incrementally after creation and which
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
290 * can be retrieved by name, with no assumptions about the structure or meaning of the name. If
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
291 * the name is already in the index, the new instance will replace the previously existing
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
292 * instance in the index.
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
293 *
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
294 * @param name string to use for indexing/lookup
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
295 * @return a newly created, indexed, initially empty, appendable source representation
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
296 */
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
297 public static Source fromNamedAppendableText(String name) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
298 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
299
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
300 final Source source = new AppendableLiteralSource(name);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
301 nameToSource.put(name, new WeakReference<>(source));
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
302 notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
303 return source;
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
304 }
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
305
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
306 /**
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
307 * Creates a {@linkplain Source Source instance} that represents the contents of a sub-range of
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
308 * an existing {@link Source}.
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
309 *
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
310 * @param base an existing Source instance
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
311 * @param baseCharIndex 0-based index of the first character of the sub-range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
312 * @param length the number of characters in the sub-range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
313 * @return a new instance representing a sub-range of another Source
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
314 * @throws IllegalArgumentException if the specified sub-range is not contained in the base
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
315 */
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
316 public static Source subSource(Source base, int baseCharIndex, int length) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
317 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
318
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
319 final SubSource subSource = SubSource.create(base, baseCharIndex, length);
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
320 return subSource;
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
321 }
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
322
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
323 /**
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
324 * Creates a {@linkplain Source Source instance} that represents the contents of a sub-range at
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
325 * the end of an existing {@link Source}.
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
326 *
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
327 * @param base an existing Source instance
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
328 * @param baseCharIndex 0-based index of the first character of the sub-range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
329 * @return a new instance representing a sub-range at the end of another Source
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
330 * @throws IllegalArgumentException if the index is out of range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
331 */
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
332 public static Source subSource(Source base, int baseCharIndex) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
333 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
334
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
335 return subSource(base, baseCharIndex, base.getLength() - baseCharIndex);
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
336 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
337
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
338 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
339 * Creates a source whose contents will be read immediately from a URL and cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
340 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
341 * @param url
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
342 * @param description identifies the origin, possibly useful for debugging
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
343 * @return a newly created, non-indexed source representation
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
344 * @throws IOException if reading fails
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
345 */
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
346 public static Source fromURL(URL url, String description) throws IOException {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
347 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
348
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
349 final URLSource source = URLSource.get(url, description);
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
350 notifyNewSource(source).tagAs(Tags.FROM_URL);
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
351 return source;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
352 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
353
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
354 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
355 * Creates a source whose contents will be read immediately and cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
356 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
357 * @param reader
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
358 * @param description a note about the origin, possibly useful for debugging
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
359 * @return a newly created, non-indexed source representation
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
360 * @throws IOException if reading fails
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
361 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
362 public static Source fromReader(Reader reader, String description) throws IOException {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
363 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
364
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
365 final LiteralSource source = new LiteralSource(description, read(reader));
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
366 notifyNewSource(source).tagAs(Tags.FROM_READER);
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
367 return source;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
368 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
369
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
370 /**
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
371 * Creates a source from raw bytes. This can be used if the encoding of strings in your language
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
372 * is not compatible with Java strings, or if your parser returns byte indices instead of
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
373 * character indices. The returned source is then indexed by byte, not by character.
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
374 *
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
375 * @param bytes the raw bytes of the source
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
376 * @param description a note about the origin, possibly useful for debugging
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
377 * @param decoder how to decode the bytes into Java strings
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
378 * @return a newly created, non-indexed source representation
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
379 */
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
380 public static Source fromBytes(byte[] bytes, String description, BytesDecoder decoder) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
381 return fromBytes(bytes, 0, bytes.length, description, decoder);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
382 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
383
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
384 /**
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
385 * Creates a source from raw bytes. This can be used if the encoding of strings in your language
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
386 * is not compatible with Java strings, or if your parser returns byte indices instead of
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
387 * character indices. The returned source is then indexed by byte, not by character. Offsets are
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
388 * relative to byteIndex.
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
389 *
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
390 * @param bytes the raw bytes of the source
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
391 * @param byteIndex where the string starts in the byte array
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
392 * @param length the length of the string in the byte array
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
393 * @param description a note about the origin, possibly useful for debugging
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
394 * @param decoder how to decode the bytes into Java strings
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
395 * @return a newly created, non-indexed source representation
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
396 */
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
397 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
398 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
399
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
400 final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder);
20906
c8b83aa6cc82 Truffle/Source: remove proposed standard SourceTags; migrate the tags related to Source provenance into thte Source class.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20038
diff changeset
401 notifyNewSource(source).tagAs(Tags.FROM_BYTES);
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
402 return source;
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
403 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
404
16989
19faa7ca37c1 Truffle/Source: use a synchronized hash table
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16140
diff changeset
405 // TODO (mlvdv) enable per-file choice whether to cache?
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
406 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
407 * Enables/disables caching of file contents, <em>disabled</em> by default. Caching of sources
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
408 * created from literal text or readers is always enabled.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
409 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
410 public static void setFileCaching(boolean enabled) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
411 fileCacheEnabled = enabled;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
412 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
413
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
414 /**
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
415 * Returns all {@link Source}s holding a particular {@link SyntaxTag}, or the whole collection
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
416 * of Sources if the specified tag is {@code null}.
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
417 *
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
418 * @return A collection of Sources containing the given tag.
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
419 */
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
420 public static Collection<Source> findSourcesTaggedAs(SourceTag tag) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
421 final List<Source> taggedSources = new ArrayList<>();
20022
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
422 synchronized (allSources) {
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
423 for (WeakReference<Source> ref : allSources) {
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
424 Source source = ref.get();
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
425 if (source != null) {
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
426 if (tag == null || source.isTaggedAs(tag)) {
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
427 taggedSources.add(ref.get());
eebf140fa6e4 syncronize the access to Source.allSources. Should fix random crashes we experienced before.
Christian Wirth <christian.wirth@oracle.com>
parents: 19697
diff changeset
428 }
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
429 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
430 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
431 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
432 return taggedSources;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
433 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
434
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
435 /**
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
436 * Adds a {@link SourceListener} to receive events.
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
437 */
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
438 public static void addSourceListener(SourceListener listener) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
439 assert listener != null;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
440 sourceListeners.add(listener);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
441 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
442
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
443 /**
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
444 * Removes a {@link SourceListener}. Ignored if listener not found.
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
445 */
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
446 public static void removeSourceListener(SourceListener listener) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
447 sourceListeners.remove(listener);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
448 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
449
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
450 private static Source notifyNewSource(Source source) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
451 allSources.add(new WeakReference<>(source));
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
452 for (SourceListener listener : sourceListeners) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
453 listener.sourceCreated(source);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
454 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
455 return source;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
456 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
457
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
458 private static String read(Reader reader) throws IOException {
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
459 final BufferedReader bufferedReader = new BufferedReader(reader);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
460 final StringBuilder builder = new StringBuilder();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
461 final char[] buffer = new char[1024];
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
462
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
463 while (true) {
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
464 final int n = bufferedReader.read(buffer);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
465 if (n == -1) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
466 break;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
467 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
468 builder.append(buffer, 0, n);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
469 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
470
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
471 return builder.toString();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
472 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
473
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
474 private final ArrayList<SourceTag> tags = new ArrayList<>();
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
475
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
476 private Source() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
477 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
478
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
479 private TextMap textMap = null;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
480
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
481 abstract void reset();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
482
19697
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
483 public final boolean isTaggedAs(SourceTag tag) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
484 assert tag != null;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
485 return tags.contains(tag);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
486 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
487
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
488 public final Collection<SourceTag> getSourceTags() {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
489 return Collections.unmodifiableCollection(tags);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
490 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
491
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
492 /**
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
493 * Adds a {@linkplain SourceTag tag} to the set of tags associated with this {@link Source};
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
494 * {@code no-op} if already in the set.
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
495 *
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
496 * @return this
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
497 */
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
498 public final Source tagAs(SourceTag tag) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
499 assert tag != null;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
500 if (!tags.contains(tag)) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
501 tags.add(tag);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
502 for (SourceListener listener : sourceListeners) {
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
503 listener.sourceTaggedAs(this, tag);
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
504 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
505 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
506 return this;
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
507 }
191c55f08ed2 Truffle: add the ability to "tag" Sources with any number of standard or private tags, for example so that Sources might be marked as "FROM_FILE", "LIBRARY", "BUILTIN", or any other distinction that matters to some tools. Those tags can be applied by the language runtime when sources are created, for example when loading builtins. Alternately, you can listen for newly created sources from outside the implementation, where you might tag sources based on pattern matching against file paths or any other meta-information in the Source.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19690
diff changeset
508
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
509 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
510 * Returns the name of this resource holding a guest language program. An example would be the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
511 * name of a guest language source code file.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
512 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
513 * @return the name of the guest language program
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
514 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
515 public abstract String getName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
516
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
517 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
518 * Returns a short version of the name of the resource holding a guest language program (as
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
519 * described in @getName). For example, this could be just the name of the file, rather than a
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
520 * full path.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
521 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
522 * @return the short name of the guest language program
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
523 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
524 public abstract String getShortName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
525
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
526 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
527 * The normalized, canonical name if the source is a file.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
528 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
529 public abstract String getPath();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
530
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
531 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
532 * The URL if the source is retrieved via URL.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
533 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
534 public abstract URL getURL();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
535
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
536 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
537 * Access to the source contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
538 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
539 public abstract Reader getReader();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
540
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
541 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
542 * Access to the source contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
543 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
544 public final InputStream getInputStream() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
545 return new ByteArrayInputStream(getCode().getBytes());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
546 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
547
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
548 /**
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
549 * Gets the number of characters in the source.
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
550 */
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
551 public final int getLength() {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
552 return getTextMap().length();
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
553 }
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
554
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
555 /**
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
556 * Returns the complete text of the code.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
557 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
558 public abstract String getCode();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
559
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
560 /**
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
561 * Returns a subsection of the code test.
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
562 */
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
563 public String getCode(int charIndex, int charLength) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
564 return getCode().substring(charIndex, charIndex + charLength);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
565 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
566
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
567 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
568 * Gets the text (not including a possible terminating newline) in a (1-based) numbered line.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
569 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
570 public final String getCode(int lineNumber) {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
571 final int offset = getTextMap().lineStartOffset(lineNumber);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
572 final int length = getTextMap().lineLength(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
573 return getCode().substring(offset, offset + length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
574 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
575
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
576 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
577 * The number of text lines in the source, including empty lines; characters at the end of the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
578 * source without a terminating newline count as a line.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
579 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
580 public final int getLineCount() {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
581 return getTextMap().lineCount();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
582 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
583
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
584 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
585 * Given a 0-based character offset, return the 1-based number of the line that includes the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
586 * position.
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
587 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
588 * @throws IllegalArgumentException if the offset is outside the text contents
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
589 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
590 public final int getLineNumber(int offset) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
591 return getTextMap().offsetToLine(offset);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
592 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
593
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
594 /**
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
595 * Given a 0-based character offset, return the 1-based number of the column at the position.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
596 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
597 * @throws IllegalArgumentException if the offset is outside the text contents
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
598 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
599 public final int getColumnNumber(int offset) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
600 return getTextMap().offsetToCol(offset);
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
601 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
602
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
603 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
604 * Given a 1-based line number, return the 0-based offset of the first character in the line.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
605 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
606 * @throws IllegalArgumentException if there is no such line in the text
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
607 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
608 public final int getLineStartOffset(int lineNumber) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
609 return getTextMap().lineStartOffset(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
610 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
611
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
612 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
613 * The number of characters (not counting a possible terminating newline) in a (1-based)
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
614 * numbered line.
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
615 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
616 * @throws IllegalArgumentException if there is no such line in the text
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
617 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
618 public final int getLineLength(int lineNumber) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
619 return getTextMap().lineLength(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
620 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
621
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
622 /**
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
623 * Append text to a Source explicitly created as <em>Appendable</em>.
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
624 *
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
625 * @param chars the text to append
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
626 * @throws UnsupportedOperationException by concrete subclasses that do not support appending
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
627 */
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
628 public void appendCode(CharSequence chars) {
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
629 throw new UnsupportedOperationException();
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
630 }
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
631
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
632 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
633 * Creates a representation of a contiguous region of text in the source.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
634 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
635 * This method performs no checks on the validity of the arguments.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
636 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
637 * The resulting representation defines hash/equality around equivalent location, presuming that
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
638 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
639 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
640 * @param identifier terse description of the region
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
641 * @param startLine 1-based line number of the first character in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
642 * @param startColumn 1-based column number of the first character in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
643 * @param charIndex the 0-based index of the first character of the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
644 * @param length the number of characters in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
645 * @return newly created object representing the specified region
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
646 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
647 public final SourceSection createSection(String identifier, int startLine, int startColumn, int charIndex, int length) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
648 return new DefaultSourceSection(this, identifier, startLine, startColumn, charIndex, length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
649 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
650
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
651 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
652 * Creates a representation of a contiguous region of text in the source. Computes the
21972
ff6f34159b8a Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21966
diff changeset
653 * {@code charIndex} value by building a {@code TextMap map} of lines in the source.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
654 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
655 * Checks the position arguments for consistency with the source.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
656 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
657 * The resulting representation defines hash/equality around equivalent location, presuming that
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
658 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
659 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
660 * @param identifier terse description of the region
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
661 * @param startLine 1-based line number of the first character in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
662 * @param startColumn 1-based column number of the first character in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
663 * @param length the number of characters in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
664 * @return newly created object representing the specified region
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
665 * @throws IllegalArgumentException if arguments are outside the text of the source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
666 * @throws IllegalStateException if the source is one of the "null" instances
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
667 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
668 public final SourceSection createSection(String identifier, int startLine, int startColumn, int length) {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
669 final int lineStartOffset = getTextMap().lineStartOffset(startLine);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
670 if (startColumn > getTextMap().lineLength(startLine)) {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
671 throw new IllegalArgumentException("column out of range");
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
672 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
673 final int startOffset = lineStartOffset + startColumn - 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
674 return new DefaultSourceSection(this, identifier, startLine, startColumn, startOffset, length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
675 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
676
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
677 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
678 * Creates a representation of a contiguous region of text in the source. Computes the
21972
ff6f34159b8a Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21966
diff changeset
679 * {@code (startLine, startColumn)} values by building a {@code TextMap map} of lines in the
ff6f34159b8a Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21966
diff changeset
680 * source.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
681 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
682 * Checks the position arguments for consistency with the source.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
683 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
684 * The resulting representation defines hash/equality around equivalent location, presuming that
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
685 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
686 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
687 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
688 * @param identifier terse description of the region
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
689 * @param charIndex 0-based position of the first character in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
690 * @param length the number of characters in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
691 * @return newly created object representing the specified region
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
692 * @throws IllegalArgumentException if either of the arguments are outside the text of the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
693 * source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
694 * @throws IllegalStateException if the source is one of the "null" instances
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
695 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
696 public final SourceSection createSection(String identifier, int charIndex, int length) throws IllegalArgumentException {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
697 checkRange(charIndex, length);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
698 final int startLine = getLineNumber(charIndex);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
699 final int startColumn = charIndex - getLineStartOffset(startLine) + 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
700 return new DefaultSourceSection(this, identifier, startLine, startColumn, charIndex, length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
701 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
702
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
703 void checkRange(int charIndex, int length) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
704 if (!(charIndex >= 0 && length >= 0 && charIndex + length <= getCode().length())) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
705 throw new IllegalArgumentException("text positions out of range");
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
706 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
707 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
708
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
709 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
710 * Creates a representation of a line of text in the source identified only by line number, from
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
711 * which the character information will be computed.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
712 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
713 * @param identifier terse description of the line
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
714 * @param lineNumber 1-based line number of the first character in the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
715 * @return newly created object representing the specified line
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
716 * @throws IllegalArgumentException if the line does not exist the source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
717 * @throws IllegalStateException if the source is one of the "null" instances
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
718 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
719 public final SourceSection createSection(String identifier, int lineNumber) {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
720 final int charIndex = getTextMap().lineStartOffset(lineNumber);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
721 final int length = getTextMap().lineLength(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
722 return createSection(identifier, charIndex, length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
723 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
724
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
725 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
726 * Creates a representation of a line number in this source, suitable for use as a hash table
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
727 * key with equality defined to mean equivalent location.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
728 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
729 * @param lineNumber a 1-based line number in this source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
730 * @return a representation of a line in this source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
731 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
732 public final LineLocation createLineLocation(int lineNumber) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
733 return new LineLocationImpl(this, lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
734 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
735
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
736 /**
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
737 * An object suitable for using as a key into a hashtable that defines equivalence between
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
738 * different source types.
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
739 */
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
740 Object getHashKey() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
741 return getName();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
742 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
743
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
744 final TextMap getTextMap() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
745 if (textMap == null) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
746 textMap = createTextMap();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
747 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
748 return textMap;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
749 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
750
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
751 final void clearTextMap() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
752 textMap = null;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
753 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
754
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
755 TextMap createTextMap() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
756 final String code = getCode();
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
757 if (code == null) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
758 throw new RuntimeException("can't read file " + getName());
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
759 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
760 return TextMap.fromString(code);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
761 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
762
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
763 private static final class LiteralSource extends Source {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
764
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
765 private final String description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
766 private final String code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
767
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
768 public LiteralSource(String description, String code) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
769 this.description = description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
770 this.code = code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
771 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
772
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
773 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
774 public String getName() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
775 return description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
776 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
777
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
778 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
779 public String getShortName() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
780 return description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
781 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
782
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
783 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
784 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
785 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
786 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
787
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
788 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
789 public String getPath() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
790 return description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
791 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
792
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
793 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
794 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
795 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
796 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
797
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
798 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
799 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
800 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
801 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
802
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
803 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
804 void reset() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
805 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
806
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
807 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
808 public int hashCode() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
809 return description.hashCode();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
810 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
811
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
812 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
813 public boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
814 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
815 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
816 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
817 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
818 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
819 }
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
820 if (obj instanceof LiteralSource) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
821 LiteralSource other = (LiteralSource) obj;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
822 return description.equals(other.description);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
823 }
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
824 return false;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
825 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
826 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
827
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
828 private static final class AppendableLiteralSource extends Source {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
829 private String description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
830 final List<CharSequence> codeList = new ArrayList<>();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
831
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
832 public AppendableLiteralSource(String description) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
833 this.description = description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
834 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
835
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
836 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
837 public String getName() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
838 return description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
839 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
840
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
841 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
842 public String getShortName() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
843 return description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
844 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
845
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
846 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
847 public String getCode() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
848 return getCodeFromIndex(0);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
849 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
850
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
851 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
852 public String getPath() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
853 return description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
854 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
855
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
856 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
857 public URL getURL() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
858 return null;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
859 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
860
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
861 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
862 public Reader getReader() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
863 return new StringReader(getCode());
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
864 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
865
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
866 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
867 void reset() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
868 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
869
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
870 private String getCodeFromIndex(int index) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
871 StringBuilder sb = new StringBuilder();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
872 for (int i = index; i < codeList.size(); i++) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
873 CharSequence s = codeList.get(i);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
874 sb.append(s);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
875 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
876 return sb.toString();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
877 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
878
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
879 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
880 public void appendCode(CharSequence chars) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
881 codeList.add(chars);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
882 clearTextMap();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
883 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
884
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
885 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
886
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
887 private static final class FileSource extends Source {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
888
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
889 private final File file;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
890 private final String name; // Name used originally to describe the source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
891 private final String path; // Normalized path description of an actual file
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
892
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
893 private String code = null; // A cache of the file's contents
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
894 private long timeStamp; // timestamp of the cache in the file system
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
895
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
896 public FileSource(File file, String name, String path) {
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
897 this(file, name, path, null);
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
898 }
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
899
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
900 public FileSource(File file, String name, String path, CharSequence chars) {
18980
ae0eb836e6f4 store absolute file in FileSource
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18979
diff changeset
901 this.file = file.getAbsoluteFile();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
902 this.name = name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
903 this.path = path;
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
904 if (chars != null) {
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
905 this.code = chars.toString();
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
906 }
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
907 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
908
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
909 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
910 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
911 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
912 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
913
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
914 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
915 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
916 return file.getName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
917 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
918
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
919 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
920 Object getHashKey() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
921 return path;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
922 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
923
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
924 @Override
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
925 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
926 if (fileCacheEnabled) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
927 if (code == null || timeStamp != file.lastModified()) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
928 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
929 code = read(getReader());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
930 timeStamp = file.lastModified();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
931 } catch (IOException e) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
932 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
933 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
934 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
935 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
936 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
937 return read(new FileReader(file));
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
938 } catch (IOException e) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
939 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
940 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
941 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
942
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
943 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
944 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
945 return path;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
946 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
947
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
948 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
949 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
950 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
951 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
952
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
953 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
954 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
955 if (code != null && timeStamp == file.lastModified()) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
956 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
957 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
958 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
959 return new FileReader(file);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
960 } catch (FileNotFoundException e) {
18979
bdb24e259c11 include original exception in FileSource.getReader
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18676
diff changeset
961
bdb24e259c11 include original exception in FileSource.getReader
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18676
diff changeset
962 throw new RuntimeException("Can't find file " + path, e);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
963 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
964 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
965
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
966 @Override
18676
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
967 public int hashCode() {
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
968 return path.hashCode();
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
969 }
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
970
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
971 @Override
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
972 public boolean equals(Object obj) {
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
973 if (this == obj) {
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
974 return true;
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
975 }
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
976 if (obj instanceof FileSource) {
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
977 FileSource other = (FileSource) obj;
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
978 return path.equals(other.path);
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
979 }
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
980 return false;
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
981 }
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
982
1c12cf39281d Truffle/Source: redefine equality of FileSource objects strictly in terms of the canonicalized file path, for use as hash key.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18672
diff changeset
983 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
984 void reset() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
985 this.code = null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
986 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
987 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
988
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
989 private static final class URLSource extends Source {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
990
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
991 private static final Map<URL, WeakReference<URLSource>> urlToSource = new HashMap<>();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
992
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
993 public static URLSource get(URL url, String name) throws IOException {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
994 WeakReference<URLSource> sourceRef = urlToSource.get(url);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
995 URLSource source = sourceRef == null ? null : sourceRef.get();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
996 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
997 source = new URLSource(url, name);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
998 urlToSource.put(url, new WeakReference<>(source));
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
999 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1000 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1001 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1002
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1003 private final URL url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1004 private final String name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1005 private String code = null; // A cache of the source contents
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1006
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1007 public URLSource(URL url, String name) throws IOException {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1008 this.url = url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1009 this.name = name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1010 code = read(new InputStreamReader(url.openStream()));
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1011 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1012
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1013 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1014 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1015 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1016 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1017
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1018 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1019 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1020 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1021 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1022
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1023 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1024 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1025 return url.getPath();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1026 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1027
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1028 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1029 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1030 return url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1031 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1032
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1033 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1034 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1035 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1036 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1037
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1038 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1039 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1040 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1041 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1042
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1043 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1044 void reset() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1045 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1046 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1047
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1048 private static final class SubSource extends Source {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1049 private final Source base;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1050 private final int baseIndex;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1051 private final int subLength;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1052
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1053 private static SubSource create(Source base, int baseIndex, int length) {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1054 if (baseIndex < 0 || length < 0 || baseIndex + length > base.getLength()) {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1055 throw new IllegalArgumentException("text positions out of range");
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1056 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1057 return new SubSource(base, baseIndex, length);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1058 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1059
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1060 private SubSource(Source base, int baseIndex, int length) {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1061 this.base = base;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1062 this.baseIndex = baseIndex;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1063 this.subLength = length;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1064 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1065
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1066 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1067 void reset() {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1068 assert false;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1069 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1070
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1071 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1072 public String getName() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1073 return base.getName();
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1074 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1075
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1076 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1077 public String getShortName() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1078 return base.getShortName();
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1079 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1080
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1081 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1082 public String getPath() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1083 return base.getPath();
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1084 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1085
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1086 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1087 public URL getURL() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1088 return null;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1089 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1090
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1091 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1092 public Reader getReader() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1093 assert false;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1094 return null;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1095 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1096
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1097 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1098 public String getCode() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1099 return base.getCode(baseIndex, subLength);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1100 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1101 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1102
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1103 private static final class BytesSource extends Source {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1104
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1105 private final String name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1106 private final byte[] bytes;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1107 private final int byteIndex;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1108 private final int length;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1109 private final BytesDecoder decoder;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1110
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1111 public BytesSource(String name, byte[] bytes, int byteIndex, int length, BytesDecoder decoder) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1112 this.name = name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1113 this.bytes = bytes;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1114 this.byteIndex = byteIndex;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1115 this.length = length;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1116 this.decoder = decoder;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1117 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1118
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1119 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1120 void reset() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1121 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1122
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1123 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1124 public String getName() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1125 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1126 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1127
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1128 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1129 public String getShortName() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1130 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1131 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1132
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1133 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1134 public String getPath() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1135 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1136 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1137
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1138 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1139 public URL getURL() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1140 return null;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1141 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1142
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1143 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1144 public Reader getReader() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1145 return null;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1146 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1147
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1148 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1149 public String getCode() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1150 return decoder.decode(bytes, byteIndex, length);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1151 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1152
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1153 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1154 public String getCode(int byteOffset, int codeLength) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1155 return decoder.decode(bytes, byteIndex + byteOffset, codeLength);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1156 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1157
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1158 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1159 void checkRange(int charIndex, int rangeLength) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1160 if (!(charIndex >= 0 && rangeLength >= 0 && charIndex + rangeLength <= length)) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1161 throw new IllegalArgumentException("text positions out of range");
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1162 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1163 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1164
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1165 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1166 TextMap createTextMap() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1167 return TextMap.fromBytes(bytes, byteIndex, length, decoder);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1168 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1169 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1170
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1171 private static final class DefaultSourceSection implements SourceSection {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1172
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1173 private final Source source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1174 private final String identifier;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1175 private final int startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1176 private final int startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1177 private final int charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1178 private final int charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1179
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1180 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1181 * Creates a new object representing a contiguous text section within the source code of a
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1182 * guest language program's text.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1183 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1184 * The starting location of the section is specified using two different coordinate:
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1185 * <ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1186 * <li><b>(row, column)</b>: rows and columns are 1-based, so the first character in a
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1187 * source file is at position {@code (1,1)}.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1188 * <li><b>character index</b>: 0-based offset of the character from the beginning of the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1189 * source, so the first character in a file is at index {@code 0}.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1190 * </ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1191 * The <b>newline</b> that terminates each line counts as a single character for the purpose
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1192 * of a character index. The (row,column) coordinates of a newline character should never
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1193 * appear in a text section.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1194 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1195 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1196 * @param source object representing the complete source program that contains this section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1197 * @param identifier an identifier used when printing the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1198 * @param startLine the 1-based number of the start line of the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1199 * @param startColumn the 1-based number of the start column of the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1200 * @param charIndex the 0-based index of the first character of the section
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1201 * @param charLength the length of the section in number of characters
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1202 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1203 public DefaultSourceSection(Source source, String identifier, int startLine, int startColumn, int charIndex, int charLength) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1204 this.source = source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1205 this.identifier = identifier;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1206 this.startLine = startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1207 this.startColumn = startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1208 this.charIndex = charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1209 this.charLength = charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1210 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1211
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1212 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1213 public Source getSource() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1214 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1215 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1216
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1217 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1218 public int getStartLine() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1219 return startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1220 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1221
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1222 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1223 public LineLocation getLineLocation() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1224 return source.createLineLocation(startLine);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1225 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1226
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1227 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1228 public int getStartColumn() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1229 return startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1230 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1231
19688
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1232 public int getEndLine() {
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1233 return source.getLineNumber(charIndex + charLength - 1);
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1234 }
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1235
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1236 public int getEndColumn() {
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1237 return source.getColumnNumber(charIndex + charLength - 1);
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1238 }
33bdafbf285d Truffle: SourceSection can now produce the line/column coordinates of its final character
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19687
diff changeset
1239
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1240 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1241 public int getCharIndex() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1242 return charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1243 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1244
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1245 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1246 public int getCharLength() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1247 return charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1248 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1249
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1250 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1251 public int getCharEndIndex() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1252 return charIndex + charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1253 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1254
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1255 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1256 public String getIdentifier() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1257 return identifier;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1258 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1259
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1260 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1261 public String getCode() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1262 return getSource().getCode(charIndex, charLength);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1263 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1264
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1265 @Override
18163
c88ab4f1f04a re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents: 17066
diff changeset
1266 public String getShortDescription() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1267 return String.format("%s:%d", source.getShortName(), startLine);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1268 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1269
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1270 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1271 public String toString() {
16140
7109baa7b9eb Truffle/Source: SourceSection.toString() is now equivalent to getCode()
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16130
diff changeset
1272 return getCode();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1273 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1274
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1275 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1276 public int hashCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1277 final int prime = 31;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1278 int result = 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1279 result = prime * result + charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1280 result = prime * result + charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1281 result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1282 result = prime * result + ((source == null) ? 0 : source.hashCode());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1283 result = prime * result + startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1284 result = prime * result + startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1285 return result;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1286 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1287
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1288 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1289 public boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1290 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1291 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1292 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1293 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1294 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1295 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1296 if (!(obj instanceof DefaultSourceSection)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1297 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1298 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1299 DefaultSourceSection other = (DefaultSourceSection) obj;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1300 if (charIndex != other.charIndex) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1301 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1302 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1303 if (charLength != other.charLength) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1304 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1305 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1306 if (identifier == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1307 if (other.identifier != null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1308 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1309 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1310 } else if (!identifier.equals(other.identifier)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1311 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1312 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1313 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1314 if (other.source != null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1315 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1316 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1317 } else if (!source.equals(other.source)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1318 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1319 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1320 if (startColumn != other.startColumn) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1321 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1322 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1323 if (startLine != other.startLine) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1324 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1325 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1326 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1327 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1328 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1329
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1330 private static final class LineLocationImpl implements LineLocation {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1331 private final Source source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1332 private final int line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1333
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1334 public LineLocationImpl(Source source, int line) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1335 assert source != null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1336 this.source = source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1337 this.line = line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1338 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1339
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1340 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1341 public Source getSource() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1342 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1343 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1344
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1345 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1346 public int getLineNumber() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1347 return line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1348 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1349
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1350 @Override
18485
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18163
diff changeset
1351 public String getShortDescription() {
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18163
diff changeset
1352 return source.getShortName() + ":" + line;
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18163
diff changeset
1353 }
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18163
diff changeset
1354
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18163
diff changeset
1355 @Override
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1356 public String toString() {
18485
e3c95cbbb50c Truffle Instrumentation: major API revision, based around the Probe and Instrument classes; add Instrumentable API for language implementors, with most details automated; reimplemented to handle AST splitting automatically; more JUnit tests.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18163
diff changeset
1357 return "Line[" + getShortDescription() + "]";
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1358 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1359
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1360 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1361 public int hashCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1362 final int prime = 31;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1363 int result = 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1364 result = prime * result + line;
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
1365 result = prime * result + source.getHashKey().hashCode();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1366 return result;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1367 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1368
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1369 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1370 public boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1371 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1372 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1373 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1374 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1375 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1376 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1377 if (!(obj instanceof LineLocationImpl)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1378 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1379 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1380 LineLocationImpl other = (LineLocationImpl) obj;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1381 if (line != other.line) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1382 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1383 }
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
1384 return source.getHashKey().equals(other.source.getHashKey());
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1385 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1386
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1387 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1388
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1389 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1390 * A utility for converting between coordinate systems in a string of text interspersed with
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1391 * newline characters. The coordinate systems are:
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1392 * <ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1393 * <li>0-based character offset from the beginning of the text, where newline characters count
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1394 * as a single character and the first character in the text occupies position 0.</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1395 * <li>1-based position in the 2D space of lines and columns, in which the first position in the
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1396 * text is at (1,1).</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1397 * </ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1398 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1399 * This utility is based on positions occupied by characters, not text stream positions as in a
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1400 * text editor. The distinction shows up in editors where you can put the cursor just past the
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1401 * last character in a buffer; this is necessary, among other reasons, so that you can put the
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1402 * edit cursor in a new (empty) buffer. For the purposes of this utility, however, there are no
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1403 * character positions in an empty text string and there are no lines in an empty text string.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1404 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1405 * A newline character designates the end of a line and occupies a column position.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1406 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1407 * If the text ends with a character other than a newline, then the characters following the
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1408 * final newline character count as a line, even though not newline-terminated.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1409 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1410 * <strong>Limitations:</strong>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1411 * <ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1412 * <li>Does not handle multiple character encodings correctly.</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1413 * <li>Treats tabs as occupying 1 column.</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1414 * <li>Does not handle multiple-character line termination sequences correctly.</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1415 * </ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1416 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1417 private static final class TextMap {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1418
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1419 // 0-based offsets of newline characters in the text, with sentinel
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1420 private final int[] nlOffsets;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1421
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1422 // The number of characters in the text, including newlines (which count as 1).
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1423 private final int textLength;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1424
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1425 // Is the final text character a newline?
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1426 final boolean finalNL;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1427
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1428 public TextMap(int[] nlOffsets, int textLength, boolean finalNL) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1429 this.nlOffsets = nlOffsets;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1430 this.textLength = textLength;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1431 this.finalNL = finalNL;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1432 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1433
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1434 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1435 * Constructs map permitting translation between 0-based character offsets and 1-based
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1436 * lines/columns.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1437 */
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1438 public static TextMap fromString(String text) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1439 final int textLength = text.length();
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1440 final ArrayList<Integer> lines = new ArrayList<>();
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1441 lines.add(0);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1442 int offset = 0;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1443
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1444 while (offset < text.length()) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1445 final int nlIndex = text.indexOf('\n', offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1446 if (nlIndex >= 0) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1447 offset = nlIndex + 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1448 lines.add(offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1449 } else {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1450 break;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1451 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1452 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1453 lines.add(Integer.MAX_VALUE);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1454
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1455 final int[] nlOffsets = new int[lines.size()];
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1456 for (int line = 0; line < lines.size(); line++) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1457 nlOffsets[line] = lines.get(line);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1458 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1459
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1460 final boolean finalNL = textLength > 0 && (textLength == nlOffsets[nlOffsets.length - 2]);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1461
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1462 return new TextMap(nlOffsets, textLength, finalNL);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1463 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1464
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1465 public static TextMap fromBytes(byte[] bytes, int byteIndex, int length, BytesDecoder bytesDecoder) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1466 final ArrayList<Integer> lines = new ArrayList<>();
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1467 lines.add(0);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1468
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1469 bytesDecoder.decodeLines(bytes, byteIndex, length, new BytesDecoder.LineMarker() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1470
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1471 public void markLine(int index) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1472 lines.add(index);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1473 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1474 });
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1475
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1476 lines.add(Integer.MAX_VALUE);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1477
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1478 final int[] nlOffsets = new int[lines.size()];
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1479 for (int line = 0; line < lines.size(); line++) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1480 nlOffsets[line] = lines.get(line);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1481 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1482
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1483 final boolean finalNL = length > 0 && (length == nlOffsets[nlOffsets.length - 2]);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1484
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1485 return new TextMap(nlOffsets, length, finalNL);
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1486 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1487
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1488 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1489 * Converts 0-based character offset to 1-based number of the line containing the character.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1490 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1491 * @throws IllegalArgumentException if the offset is outside the string.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1492 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1493 public int offsetToLine(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1494 if (offset < 0 || offset >= textLength) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1495 throw new IllegalArgumentException("offset out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1496 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1497 int line = 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1498 while (offset >= nlOffsets[line]) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1499 line++;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1500 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1501 return line;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1502 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1503
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1504 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1505 * Converts 0-based character offset to 1-based number of the column occupied by the
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1506 * character.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1507 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1508 * Tabs are not expanded; they occupy 1 column.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1509 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1510 * @throws IllegalArgumentException if the offset is outside the string.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1511 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1512 public int offsetToCol(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1513 return 1 + offset - nlOffsets[offsetToLine(offset) - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1514 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1515
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1516 /**
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1517 * The number of characters in the mapped text.
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1518 */
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1519 public int length() {
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1520 return textLength;
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1521 }
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1522
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
1523 /**
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1524 * The number of lines in the text; if characters appear after the final newline, then they
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1525 * also count as a line, even though not newline-terminated.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1526 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1527 public int lineCount() {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1528 if (textLength == 0) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1529 return 0;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1530 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1531 return finalNL ? nlOffsets.length - 2 : nlOffsets.length - 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1532 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1533
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1534 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1535 * Converts 1-based line number to the 0-based offset of the line's first character; this
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1536 * would be the offset of a newline if the line is empty.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1537 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1538 * @throws IllegalArgumentException if there is no such line in the text.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1539 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1540 public int lineStartOffset(int line) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1541 if (textLength == 0 || lineOutOfRange(line)) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1542 throw new IllegalArgumentException("line out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1543 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1544 return nlOffsets[line - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1545 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1546
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1547 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1548 * Gets the number of characters in a line, identified by 1-based line number;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1549 * <em>does not</em> include the final newline, if any.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1550 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1551 * @throws IllegalArgumentException if there is no such line in the text.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1552 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1553 public int lineLength(int line) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1554 if (textLength == 0 || lineOutOfRange(line)) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1555 throw new IllegalArgumentException("line out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1556 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1557 if (line == nlOffsets.length - 1 && !finalNL) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1558 return textLength - nlOffsets[line - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1559 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1560 return (nlOffsets[line] - nlOffsets[line - 1]) - 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1561
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1562 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1563
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1564 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1565 * Is the line number out of range.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1566 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1567 private boolean lineOutOfRange(int line) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1568 return line <= 0 || line >= nlOffsets.length || (line == nlOffsets.length - 1 && finalNL);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1569 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1570
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1571 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1572
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1573 }