annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 20022:eebf140fa6e4

syncronize the access to Source.allSources. Should fix random crashes we experienced before.
author Christian Wirth <christian.wirth@oracle.com>
date Tue, 24 Mar 2015 17:19:23 +0100
parents 191c55f08ed2
children bbf53b35292e
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
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
32 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
33
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
34 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
35 * 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
36 * several ways:
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
37 * <ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
38 * <li><strong>Literal:</strong> A named text string. These are not indexed and should be considered
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
39 * 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
40 * 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
41 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
42 * <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
43 * 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
44 * optionally <em>cached</em>. <br>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
45 * See {@link Source#fromFileName(String)}<br>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
46 * See {@link Source#fromFileName(String, boolean)}</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
47 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
48 * <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
49 * URL. Contents are <em>read eagerly</em> and <em>cached</em>. <br>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
50 * See {@link Source#fromURL(URL, String)}</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
51 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
52 * <li><strong>Reader:</strong> Contents are <em>read eagerly</em> and treated as a <em>Literal</em>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
53 * . <br>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
54 * See {@link Source#fromReader(Reader, String)}</li>
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>Pseudo File:</strong> A literal text string that can be retrieved by name as if it
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
57 * were a file, unlike literal sources; useful for testing. <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
58 * See {@link Source#asPseudoFile(CharSequence, String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
59 * </ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
60 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
61 * <strong>File cache:</strong>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
62 * <ol>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
63 * <li>File content caching is optional, <em>off</em> by default.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
64 * <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
65 * enabled) cached.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
66 * <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
67 * {@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
68 * <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
69 * reload.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
70 * </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
71 * <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
72 *
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
73 * @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
74 * @see SourceListener
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
75 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
76 public abstract class Source {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
77
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
78 // 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
79 // 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
80
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
81 /**
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
82 * 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
83 */
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
84 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
85
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
86 // Files and pseudo files are indexed.
16989
19faa7ca37c1 Truffle/Source: use a synchronized hash table
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16140
diff changeset
87 private static final Map<String, WeakReference<Source>> filePathToSource = new Hashtable<>();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
88
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
89 private static boolean fileCacheEnabled = true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
90
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
91 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
92
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
93 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
94 * 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
95 * then cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
96 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
97 * @param fileName name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
98 * @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
99 * @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
100 * @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
101 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
102 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
103
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
104 final WeakReference<Source> nameRef = filePathToSource.get(fileName);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
105 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
106 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
107 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
108 if (!file.canRead()) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
109 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
110 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
111 final String path = file.getCanonicalPath();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
112 final WeakReference<Source> pathRef = filePathToSource.get(path);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
113 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
114 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
115 source = new FileSource(file, fileName, path);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
116 filePathToSource.put(path, new WeakReference<>(source));
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
117 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
118 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
119 if (reset) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
120 source.reset();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
121 }
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
122 notifyNewSource(source).tagAs(StandardSourceTag.FROM_FILE);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
123 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
124 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
125
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
126 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
127 * 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
128 * then cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
129 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
130 * @param fileName name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
131 * @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
132 * @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
133 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
134 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
135 return fromFileName(fileName, false);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
136 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
137
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
138 /**
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
139 * 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
140 * 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
141 * 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
142 * 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
143 *
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
144 * @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
145 * @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
146 * @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
147 * @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
148 */
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
149 public static Source fromFileName(CharSequence chars, String fileName) throws IOException {
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
150
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
151 final WeakReference<Source> nameRef = filePathToSource.get(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
152 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
153 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
154 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
155 // 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
156 final String path = file.getCanonicalPath();
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
157 final WeakReference<Source> pathRef = filePathToSource.get(path);
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
158 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
159 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
160 source = new FileSource(file, fileName, path, chars);
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
161 filePathToSource.put(path, new WeakReference<>(source));
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
162 }
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
163 }
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
164 notifyNewSource(source).tagAs(StandardSourceTag.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
165 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
166 }
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
167
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
168 /**
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
169 * Creates a non-canonical source from literal text. If an already created literal source must
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
170 * be retrievable by name, use {@link #asPseudoFile(CharSequence, String)}.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
171 *
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
172 * @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
173 * @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
174 * @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
175 */
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
176 public static Source fromText(CharSequence chars, String description) {
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
177 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
178 final LiteralSource source = new LiteralSource(description, chars.toString());
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
179 notifyNewSource(source).tagAs(StandardSourceTag.FROM_LITERAL);
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
180 return source;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
181 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
182
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
183 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
184 * 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
185 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
186 * @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
187 * @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
188 * @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
189 * @throws IOException if reading fails
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
190 */
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
191 public static Source fromURL(URL url, String description) throws IOException {
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
192 final URLSource source = URLSource.get(url, description);
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
193 notifyNewSource(source).tagAs(StandardSourceTag.FROM_URL);
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
194 return source;
16068
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 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
198 * 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
199 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
200 * @param reader
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
201 * @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
202 * @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
203 * @throws IOException if reading fails
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
204 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
205 public static Source fromReader(Reader reader, String description) throws IOException {
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
206 final LiteralSource source = new LiteralSource(description, read(reader));
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
207 notifyNewSource(source).tagAs(StandardSourceTag.FROM_READER);
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
208 return source;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
209 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
210
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
211 /**
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
212 * 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
213 * 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
214 * 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
215 *
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
216 * @param bytes the raw bytes of the source
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
217 * @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
218 * @param decoder how to decode the bytes into Java strings
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
219 * @return a newly created, non-indexed source representation
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
220 */
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
221 public static Source fromBytes(byte[] bytes, String description, BytesDecoder decoder) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
222 return fromBytes(bytes, 0, bytes.length, description, decoder);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
223 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
224
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
225 /**
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
226 * 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
227 * 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
228 * 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
229 * relative to byteIndex.
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
230 *
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
231 * @param bytes the raw bytes of the source
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
232 * @param byteIndex where the string starts in the byte array
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
233 * @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
234 * @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
235 * @param decoder how to decode the bytes into Java strings
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
236 * @return a newly created, non-indexed source representation
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
237 */
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
238 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, BytesDecoder decoder) {
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
239 final BytesSource source = new BytesSource(description, bytes, byteIndex, length, decoder);
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
240 notifyNewSource(source).tagAs(StandardSourceTag.FROM_BYTES);
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
241 return source;
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
242 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
243
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
244 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
245 * Creates a source from literal text, but which acts as a file and can be retrieved by name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
246 * (unlike other literal sources); intended for testing.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
247 *
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
248 * @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
249 * @param pseudoFileName string to use for indexing/lookup
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
250 * @return a newly created, source representation, canonical with respect to its name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
251 */
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
252 public static Source asPseudoFile(CharSequence chars, String pseudoFileName) {
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
253 final Source source = new LiteralSource(pseudoFileName, chars.toString());
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
254 filePathToSource.put(pseudoFileName, new 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
255 notifyNewSource(source).tagAs(StandardSourceTag.FROM_LITERAL);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
256 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
257 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
258
16989
19faa7ca37c1 Truffle/Source: use a synchronized hash table
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16140
diff changeset
259 // 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
260 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
261 * 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
262 * 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
263 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
264 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
265 fileCacheEnabled = enabled;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
266 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
267
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
268 /**
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
269 * 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
270 * 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
271 *
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
272 * @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
273 */
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
274 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
275 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
276 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
277 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
278 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
279 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
280 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
281 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
282 }
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
283 }
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
284 }
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
285 }
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
286 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
287 }
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
288
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
289 /**
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
290 * 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
291 */
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
292 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
293 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
294 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
295 }
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
296
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
297 /**
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
298 * 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
299 */
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
300 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
301 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
302 }
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
303
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
304 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
305 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
306 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
307 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
308 }
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
309 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
310 }
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
311
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
312 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
313 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
314 final StringBuilder builder = new StringBuilder();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
315 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
316
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
317 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
318 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
319 if (n == -1) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
320 break;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
321 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
322 builder.append(buffer, 0, n);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
323 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
324
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
325 return builder.toString();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
326 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
327
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
328 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
329
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
330 Source() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
331 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
332
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
333 private TextMap textMap = null;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
334
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
335 protected abstract void reset();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
336
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
337 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
338 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
339 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
340 }
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
341
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
342 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
343 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
344 }
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
345
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
346 /**
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
347 * 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
348 * {@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
349 *
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
350 * @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
351 */
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
352 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
353 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
354 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
355 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
356 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
357 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
358 }
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
359 }
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
360 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
361 }
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
362
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
363 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
364 * 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
365 * 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
366 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
367 * @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
368 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
369 public abstract String getName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
370
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
371 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
372 * 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
373 * 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
374 * full path.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
375 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
376 * @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
377 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
378 public abstract String getShortName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
379
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
380 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
381 * 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
382 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
383 public abstract String getPath();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
384
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
385 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
386 * 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
387 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
388 public abstract URL getURL();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
389
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
390 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
391 * Access to the source contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
392 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
393 public abstract Reader getReader();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
394
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
395 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
396 * Access to the source contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
397 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
398 public final InputStream getInputStream() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
399 return new ByteArrayInputStream(getCode().getBytes());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
400 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
401
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
402 /**
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
403 * 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
404 */
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
405 public final int getLength() {
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
406 return checkTextMap().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
407 }
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
408
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
409 /**
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
410 * 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
411 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
412 public abstract String getCode();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
413
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
414 /**
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
415 * 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
416 */
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
417 public String getCode(int charIndex, int charLength) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
418 return getCode().substring(charIndex, charIndex + charLength);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
419 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
420
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
421 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
422 * 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
423 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
424 public final String getCode(int lineNumber) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
425 checkTextMap();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
426 final int offset = textMap.lineStartOffset(lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
427 final int length = textMap.lineLength(lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
428 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
429 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
430
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
431 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
432 * 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
433 * 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
434 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
435 public final int getLineCount() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
436 return checkTextMap().lineCount();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
437 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
438
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
439 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
440 * 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
441 * position.
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
442 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
443 * @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
444 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
445 public final int getLineNumber(int offset) throws IllegalArgumentException {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
446 return checkTextMap().offsetToLine(offset);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
447 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
448
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
449 /**
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
450 * 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
451 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
452 * @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
453 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
454 public final int getColumnNumber(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
455 return checkTextMap().offsetToCol(offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
456 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
457
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
458 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
459 * 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
460 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
461 * @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
462 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
463 public final int getLineStartOffset(int lineNumber) throws IllegalArgumentException {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
464 return checkTextMap().lineStartOffset(lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
465 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
466
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 * 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
469 * numbered line.
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
470 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
471 * @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
472 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
473 public final int getLineLength(int lineNumber) throws IllegalArgumentException {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
474 return checkTextMap().lineLength(lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
475 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
476
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 * 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
479 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
480 * 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
481 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
482 * 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
483 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
484 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
485 * @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
486 * @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
487 * @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
488 * @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
489 * @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
490 * @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
491 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
492 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
493 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
494 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
495
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
496 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
497 * Creates a representation of a contiguous region of text in the source. Computes the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
498 * {@code charIndex} value by building a {@linkplain TextMap map} of lines in the source.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
499 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
500 * 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
501 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
502 * 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
503 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
504 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
505 * @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
506 * @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
507 * @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
508 * @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
509 * @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
510 * @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
511 * @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
512 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
513 public final SourceSection createSection(String identifier, int startLine, int startColumn, int length) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
514 checkTextMap();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
515 final int lineStartOffset = textMap.lineStartOffset(startLine);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
516 if (startColumn > textMap.lineLength(startLine)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
517 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
518 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
519 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
520 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
521 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
522
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 * Creates a representation of a contiguous region of text in the source. Computes the
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
525 * {@code (startLine, startColumn)} values by building a {@linkplain TextMap map} of lines in
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
526 * the source.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
527 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
528 * 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
529 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
530 * 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
531 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
532 *
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 * @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
535 * @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
536 * @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
537 * @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
538 * @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
539 * source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
540 * @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
541 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
542 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
543 checkRange(charIndex, length);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
544 checkTextMap();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
545 final int startLine = getLineNumber(charIndex);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
546 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
547
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
548 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
549 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
550
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
551 protected void checkRange(int charIndex, int length) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
552 if (!(charIndex >= 0 && length >= 0 && charIndex + length <= getCode().length())) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
553 throw new IllegalArgumentException("text positions out of range");
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
554 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
555 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
556
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 * 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
559 * 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
560 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
561 * @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
562 * @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
563 * @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
564 * @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
565 * @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
566 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
567 public final SourceSection createSection(String identifier, int lineNumber) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
568 checkTextMap();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
569 final int charIndex = textMap.lineStartOffset(lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
570 final int length = textMap.lineLength(lineNumber);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
571 return createSection(identifier, charIndex, length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
572 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
573
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 * 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
576 * 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
577 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
578 * @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
579 * @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
580 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
581 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
582 return new LineLocationImpl(this, lineNumber);
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 private TextMap checkTextMap() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
586 if (textMap == null) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
587 textMap = createTextMap();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
588 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
589 return textMap;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
590 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
591
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
592 protected TextMap createTextMap() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
593 final String code = getCode();
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
594 if (code == null) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
595 throw new RuntimeException("can't read file " + getName());
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
596 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
597 return TextMap.fromString(code);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
598 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
599
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
600 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
601
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
602 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
603 private final String code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
604
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
605 public LiteralSource(String name, String code) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
606 this.name = name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
607 this.code = code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
608 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
609
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
610 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
611 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
612 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
613 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
614
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
615 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
616 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
617 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
618 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
619
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
620 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
621 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
622 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
623 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
624
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
625 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
626 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
627 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
628 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
629
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
630 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
631 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
632 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
633 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
634
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
635 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
636 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
637 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
638 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
641 protected void reset() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
642 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
643
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
644 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
645 public int hashCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
646 final int prime = 31;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
647 int result = 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
648 result = prime * result + name.hashCode();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
649 result = prime * result + (code == null ? 0 : code.hashCode());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
650 return result;
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
653 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
654 public boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
655 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
656 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
657 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
658 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
659 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
660 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
661 if (!(obj instanceof LiteralSource)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
662 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
663 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
664 LiteralSource other = (LiteralSource) obj;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
665 return name.equals(other.name) && code.equals(other.code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
666 }
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 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
669
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
670 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
671
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
672 private final File file;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
673 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
674 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
675
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
676 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
677 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
678
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
679 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
680 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
681 }
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
682
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
683 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
684 this.file = file.getAbsoluteFile();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
685 this.name = name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
686 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
687 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
688 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
689 }
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
690 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
691
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
692 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
693 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
694 return name;
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
697 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
698 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
699 return file.getName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
700 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
703 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
704 if (fileCacheEnabled) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
705 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
706 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
707 code = read(getReader());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
708 timeStamp = file.lastModified();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
709 } catch (IOException e) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
710 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
711 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
712 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
713 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
714 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
715 return read(new FileReader(file));
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
716 } catch (IOException e) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
717 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
718 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
719 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
720
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
721 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
722 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
723 return path;
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
727 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
728 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
729 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
730
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
731 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
732 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
733 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
734 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
735 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
736 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
737 return new FileReader(file);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
738 } catch (FileNotFoundException e) {
18979
bdb24e259c11 include original exception in FileSource.getReader
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18676
diff changeset
739
bdb24e259c11 include original exception in FileSource.getReader
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18676
diff changeset
740 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
741 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
742 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
743
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
744 @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
745 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
746 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
747 }
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
748
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
749 @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
750 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
751 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
752 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
753 }
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
754 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
755 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
756 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
757 }
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
758 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
759 }
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
760
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
761 @Override
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
762 protected void reset() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
763 this.code = null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
764 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
765
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
766 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
767
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
768 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
769
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
770 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
771
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
772 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
773 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
774 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
775 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
776 source = new URLSource(url, name);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
777 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
778 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
779 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
780 }
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 private final URL url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
783 private final String name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
784 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
785
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
786 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
787 this.url = url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
788 this.name = name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
789 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
790 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
793 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
794 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
795 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
798 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
799 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
800 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
803 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
804 return url.getPath();
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 URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
809 return url;
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 Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
814 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
815 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
818 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
819 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
820 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
821
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
822 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
823 protected void reset() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
824 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
825
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
826 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
827
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
828 private static final class BytesSource extends Source {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
829
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
830 private final String name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
831 private final byte[] bytes;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
832 private final int byteIndex;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
833 private final int length;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
834 private final BytesDecoder decoder;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
835
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
836 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
837 this.name = name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
838 this.bytes = bytes;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
839 this.byteIndex = byteIndex;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
840 this.length = length;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
841 this.decoder = decoder;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
842 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
843
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
844 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
845 protected void reset() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
846 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
847
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
848 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
849 public String getName() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
850 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
851 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
852
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
853 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
854 public String getShortName() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
855 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
856 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
857
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
858 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
859 public String getPath() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
860 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
861 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
862
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
863 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
864 public URL getURL() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
865 return null;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
866 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
867
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
868 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
869 public Reader getReader() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
870 return null;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
871 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
872
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
873 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
874 public String getCode() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
875 return decoder.decode(bytes, byteIndex, length);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
876 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
877
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
878 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
879 public String getCode(int byteOffset, int codeLength) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
880 return decoder.decode(bytes, byteIndex + byteOffset, codeLength);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
881 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
882
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
883 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
884 protected void checkRange(int charIndex, int rangeLength) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
885 if (!(charIndex >= 0 && rangeLength >= 0 && charIndex + rangeLength <= length)) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
886 throw new IllegalArgumentException("text positions out of range");
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
887 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
888 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
889
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
890 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
891 protected TextMap createTextMap() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
892 return TextMap.fromBytes(bytes, byteIndex, length, decoder);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
893 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
894 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
895
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
896 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
897
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
898 private final Source source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
899 private final String identifier;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
900 private final int startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
901 private final int startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
902 private final int charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
903 private final int charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
904
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
905 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
906 * 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
907 * guest language program's text.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
908 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
909 * 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
910 * <ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
911 * <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
912 * 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
913 * <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
914 * 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
915 * </ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
916 * 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
917 * 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
918 * appear in a text section.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
919 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
920 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
921 * @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
922 * @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
923 * @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
924 * @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
925 * @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
926 * @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
927 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
928 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
929 this.source = source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
930 this.identifier = identifier;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
931 this.startLine = startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
932 this.startColumn = startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
933 this.charIndex = charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
934 this.charLength = charLength;
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
937 @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
938 public Source getSource() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
939 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
940 }
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 @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
943 public int getStartLine() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
944 return startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
945 }
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 @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
948 public LineLocation getLineLocation() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
949 return source.createLineLocation(startLine);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
950 }
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 @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
953 public int getStartColumn() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
954 return startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
955 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
956
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
957 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
958 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
959 }
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
960
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
961 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
962 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
963 }
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
964
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
965 @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
966 public int getCharIndex() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
967 return charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
968 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
969
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
970 @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
971 public int getCharLength() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
972 return charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
973 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
974
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
975 @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
976 public int getCharEndIndex() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
977 return charIndex + charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
978 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
979
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
980 @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
981 public String getIdentifier() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
982 return identifier;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
983 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
984
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
985 @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
986 public String getCode() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
987 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
988 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
989
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
990 @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
991 public String getShortDescription() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
992 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
993 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
994
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
995 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
996 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
997 return getCode();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
998 }
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1001 public int hashCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1002 final int prime = 31;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1003 int result = 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1004 result = prime * result + charIndex;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1005 result = prime * result + charLength;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1006 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
1007 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
1008 result = prime * result + startColumn;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1009 result = prime * result + startLine;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1010 return result;
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 boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1015 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1016 return true;
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 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1019 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1020 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1021 if (!(obj instanceof DefaultSourceSection)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1022 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1023 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1024 DefaultSourceSection other = (DefaultSourceSection) obj;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1025 if (charIndex != other.charIndex) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1026 return false;
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 if (charLength != other.charLength) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1029 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1030 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1031 if (identifier == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1032 if (other.identifier != null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1033 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1034 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1035 } 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
1036 return false;
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 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1039 if (other.source != null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1040 return false;
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 } 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
1043 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1044 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1045 if (startColumn != other.startColumn) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1046 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1047 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1048 if (startLine != other.startLine) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1049 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1050 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1051 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1052 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1053 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1054
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1055 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
1056 private final Source source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1057 private final int line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1058
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1059 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
1060 assert source != null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1061 this.source = source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1062 this.line = line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1063 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1064
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1065 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1066 public Source getSource() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1067 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1068 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1069
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1070 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1071 public int getLineNumber() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1072 return line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1073 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1074
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1075 @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
1076 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
1077 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
1078 }
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
1079
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
1080 @Override
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1081 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
1082 return "Line[" + getShortDescription() + "]";
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1083 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1084
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1085 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1086 public int hashCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1087 final int prime = 31;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1088 int result = 1;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1089 result = prime * result + line;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1090 result = prime * result + source.hashCode();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1091 return result;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1092 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1093
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1094 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1095 public boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1096 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1097 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1098 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1099 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1100 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1101 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1102 if (!(obj instanceof LineLocationImpl)) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1103 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1104 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1105 LineLocationImpl other = (LineLocationImpl) obj;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1106 if (line != other.line) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1107 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1108 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1109 return source.equals(other.source);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1110 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1111
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1112 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1113
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1114 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1115 * 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
1116 * newline characters. The coordinate systems are:
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1117 * <ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1118 * <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
1119 * 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
1120 * <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
1121 * text is at (1,1).</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1122 * </ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1123 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1124 * 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
1125 * 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
1126 * 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
1127 * 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
1128 * 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
1129 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1130 * 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
1131 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1132 * 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
1133 * 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
1134 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1135 * <strong>Limitations:</strong>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1136 * <ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1137 * <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
1138 * <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
1139 * <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
1140 * </ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1141 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1142 private static final class TextMap {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1143
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1144 // 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
1145 private final int[] nlOffsets;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1146
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1147 // 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
1148 private final int textLength;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1149
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1150 // Is the final text character a newline?
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1151 final boolean finalNL;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1152
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1153 public TextMap(int[] nlOffsets, int textLength, boolean finalNL) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1154 this.nlOffsets = nlOffsets;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1155 this.textLength = textLength;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1156 this.finalNL = finalNL;
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
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1159 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1160 * 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
1161 * lines/columns.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1162 */
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1163 public static TextMap fromString(String text) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1164 final int textLength = text.length();
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1165 final ArrayList<Integer> lines = new ArrayList<>();
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1166 lines.add(0);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1167 int offset = 0;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1168
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1169 while (offset < text.length()) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1170 final int nlIndex = text.indexOf('\n', offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1171 if (nlIndex >= 0) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1172 offset = nlIndex + 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1173 lines.add(offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1174 } else {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1175 break;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1176 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1177 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1178 lines.add(Integer.MAX_VALUE);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1179
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1180 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
1181 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
1182 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
1183 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1184
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1185 final boolean finalNL = textLength > 0 && (textLength == nlOffsets[nlOffsets.length - 2]);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1186
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1187 return new TextMap(nlOffsets, textLength, finalNL);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1188 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1189
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1190 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
1191 final ArrayList<Integer> lines = new ArrayList<>();
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1192 lines.add(0);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1193
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1194 bytesDecoder.decodeLines(bytes, byteIndex, length, new BytesDecoder.LineMarker() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1195
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1196 public void markLine(int index) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1197 lines.add(index);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1198 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1199 });
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1200
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1201 lines.add(Integer.MAX_VALUE);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1202
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1203 final int[] nlOffsets = new int[lines.size()];
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1204 for (int line = 0; line < lines.size(); line++) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1205 nlOffsets[line] = lines.get(line);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1206 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1207
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1208 final boolean finalNL = length > 0 && (length == nlOffsets[nlOffsets.length - 2]);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1209
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1210 return new TextMap(nlOffsets, length, finalNL);
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1211 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1212
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1213 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1214 * 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
1215 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1216 * @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
1217 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1218 public int offsetToLine(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1219 if (offset < 0 || offset >= textLength) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1220 throw new IllegalArgumentException("offset out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1221 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1222 int line = 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1223 while (offset >= nlOffsets[line]) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1224 line++;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1225 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1226 return line;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1227 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1228
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1229 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1230 * 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
1231 * character.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1232 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1233 * 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
1234 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1235 * @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
1236 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1237 public int offsetToCol(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1238 return 1 + offset - nlOffsets[offsetToLine(offset) - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1239 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1240
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1241 /**
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
1242 * 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
1243 */
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
1244 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
1245 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
1246 }
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
1247
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
1248 /**
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1249 * 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
1250 * 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
1251 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1252 public int lineCount() {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1253 if (textLength == 0) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1254 return 0;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1255 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1256 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
1257 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1258
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1259 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1260 * 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
1261 * 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
1262 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1263 * @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
1264 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1265 public int lineStartOffset(int line) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1266 if (textLength == 0 || lineOutOfRange(line)) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1267 throw new IllegalArgumentException("line out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1268 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1269 return nlOffsets[line - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1270 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1271
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1272 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1273 * 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
1274 * <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
1275 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1276 * @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
1277 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1278 public int lineLength(int line) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1279 if (textLength == 0 || lineOutOfRange(line)) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1280 throw new IllegalArgumentException("line out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1281 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1282 if (line == nlOffsets.length - 1 && !finalNL) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1283 return textLength - nlOffsets[line - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1284 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1285 return (nlOffsets[line] - nlOffsets[line - 1]) - 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1286
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1287 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1288
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1289 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1290 * Is the line number out of range.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1291 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1292 private boolean lineOutOfRange(int line) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1293 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
1294 }
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 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1297
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1298 }