annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java @ 22396:78f014d57b4b

Source: remove redundant assertion
author Andreas Woess <andreas.woess@oracle.com>
date Thu, 19 Nov 2015 16:02:48 +0100
parents 75c4ce3a352a
children 7015a77b222f
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
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
27 import java.io.BufferedReader;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
28 import java.io.ByteArrayInputStream;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
29 import java.io.File;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
30 import java.io.FileInputStream;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
31 import java.io.FileNotFoundException;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
32 import java.io.IOException;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
33 import java.io.InputStream;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
34 import java.io.InputStreamReader;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
35 import java.io.Reader;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
36 import java.io.StringReader;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
37 import java.io.UnsupportedEncodingException;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
38 import java.lang.ref.WeakReference;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
39 import java.net.URL;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
40 import java.net.URLConnection;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
41 import java.nio.ByteBuffer;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
42 import java.nio.CharBuffer;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
43 import java.nio.charset.CharacterCodingException;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
44 import java.nio.charset.Charset;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
45 import java.nio.charset.CharsetDecoder;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
46 import java.nio.file.Files;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
47 import java.nio.file.spi.FileTypeDetector;
22157
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
48 import java.util.ArrayList;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
49 import java.util.HashMap;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
50 import java.util.List;
dc83cc1f94f2 Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22105
diff changeset
51 import java.util.Map;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
52 import java.util.logging.Level;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
53 import java.util.logging.Logger;
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
54
22219
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22218
diff changeset
55 import com.oracle.truffle.api.CompilerAsserts;
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22218
diff changeset
56 import com.oracle.truffle.api.TruffleLanguage.Registration;
1c0f490984d5 Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22157 22218
diff changeset
57
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
58 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
59 * 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
60 * several ways:
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
61 * <ul>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
62 * <li><strong>Literal:</strong> An anonymous text string: not named and not indexed. These should
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
63 * be considered value objects; equality is defined based on contents.<br>
18672
f17b2a0303db Truffle/Source: change signature of Source factory methods to take literal text as CharSequence instead of String.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18485
diff changeset
64 * 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
65 * <p>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
66 * <li><strong>Named Literal:</strong> A text string that can be retrieved by name as if it were a
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
67 * file, but without any assumption that the name is related to a file path. Creating a new literal
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
68 * with an already existing name will replace its predecessor in the index.<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
69 * See {@link Source#fromNamedText(CharSequence, String)}<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
70 * See {@link Source#find(String)}</li>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
71 * <p>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
72 * <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
73 * 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
74 * optionally <em>cached</em>. <br>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
75 * See {@link Source#fromFileName(String)}<br>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
76 * See {@link Source#fromFileName(String, boolean)}<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
77 * See {@link Source#find(String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
78 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
79 * <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
80 * URL. Contents are <em>read eagerly</em> and <em>cached</em>. <br>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
81 * See {@link Source#fromURL(URL, String)}<br>
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
82 * See {@link Source#find(String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
83 * <p>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
84 * <li><strong>Reader:</strong> Contents are <em>read eagerly</em> and treated as an anonymous
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
85 * (non-indexed) <em>Literal</em> . <br>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
86 * See {@link Source#fromReader(Reader, String)}</li>
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
87 * <p>
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
88 * <li><strong>Sub-Source:</strong> A representation of the contents of a sub-range of another
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
89 * {@link Source}.<br>
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
90 * See @link {@link Source#subSource(Source, int, int)}<br>
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
91 * See @link {@link Source#subSource(Source, int)}</li>
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
92 * <p>
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
93 * <li><strong>AppendableSource:</strong> Literal contents are provided by the client,
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
94 * incrementally, after the instance is created.<br>
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
95 * See {@link Source#fromAppendableText(String)}<br>
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
96 * See {@link Source#fromNamedAppendableText(String)}</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
97 * </ul>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
98 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
99 * <strong>File cache:</strong>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
100 * <ol>
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
101 * <li>File content caching is optional, <em>on</em> by default.</li>
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
102 * <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
103 * enabled) cached.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
104 * <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
105 * {@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
106 * <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
107 * reload.</li>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
108 * </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
109 * <p>
16068
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 public abstract class Source {
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
112 private static final Logger LOG = Logger.getLogger(Source.class.getName());
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
113
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
114 // 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
115 // 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
116
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
117 /**
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
118 * Index of all named sources.
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
119 */
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
120 private static final Map<String, WeakReference<Source>> nameToSource = new HashMap<>();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
121
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
122 private static boolean fileCacheEnabled = true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
123
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
124 /**
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
125 * Locates an existing instance by the name under which it was indexed.
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
126 */
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
127 public static Source find(String name) {
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
128 final WeakReference<Source> nameRef = nameToSource.get(name);
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
129 return nameRef == null ? null : nameRef.get();
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
130 }
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
131
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
132 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
133 * 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
134 * then cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
135 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
136 * @param fileName name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
137 * @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
138 * @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
139 * @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
140 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
141 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
142
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
143 final WeakReference<Source> nameRef = nameToSource.get(fileName);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
144 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
145 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
146 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
147 if (!file.canRead()) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
148 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
149 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
150 final String path = file.getCanonicalPath();
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
151 final WeakReference<Source> pathRef = nameToSource.get(path);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
152 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
153 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
154 source = new FileSource(file, fileName, path);
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
155 nameToSource.put(path, new WeakReference<>(source));
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
156 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
157 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
158 if (reset) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
159 source.reset();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
160 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
161 return source;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
162 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
163
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
164 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
165 * 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
166 * then cached.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
167 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
168 * @param fileName name
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
169 * @return canonical representation of the file's contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
170 * @throws IOException if the file can not be read
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
171 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
172 public static Source fromFileName(String fileName) throws IOException {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
173 return fromFileName(fileName, false);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
174 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
175
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
176 /**
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
177 * Gets the canonical representation of a source file whose contents are the responsibility of
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
178 * the client:
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
179 * <ul>
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
180 * <li>If no Source exists corresponding to the provided file name, then a new Source is created
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
181 * whose contents are those provided. It is confirmed that the file resolves to a file name, so
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
182 * it can be indexed by canonical path. However there is no confirmation that the text supplied
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
183 * agrees with the file's contents or even whether the file is readable.</li>
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
184 * <li>If a Source exists corresponding to the provided file name, and that Source was created
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
185 * originally by this method, then that Source will be returned after replacement of its
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
186 * contents with no further confirmation.</li>
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
187 * <li>If a Source exists corresponding to the provided file name, and that Source was not
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
188 * created originally by this method, then an exception will be raised.</li>
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
189 * </ul>
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
190 *
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
191 * @param chars textual source code already read from the file, must not be null
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
192 * @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
193 * @return canonical representation of the file's contents.
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
194 * @throws IOException if the file cannot be found, or if an existing Source not created by this
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
195 * method matches the file name
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
196 */
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
197 public static Source fromFileName(CharSequence chars, String fileName) throws IOException {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
198 CompilerAsserts.neverPartOfCompilation();
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
199 assert chars != null;
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
200
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
201 final WeakReference<Source> nameRef = nameToSource.get(fileName);
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
202 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
203 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
204 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
205 // 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
206 final String path = file.getCanonicalPath();
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
207 final WeakReference<Source> pathRef = nameToSource.get(path);
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
208 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
209 if (source == null) {
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
210 source = new ClientManagedFileSource(file, fileName, path, chars);
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
211 nameToSource.put(path, new WeakReference<>(source));
19687
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
212 }
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
213 } else if (source instanceof ClientManagedFileSource) {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
214 final ClientManagedFileSource modifiableSource = (ClientManagedFileSource) source;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
215 modifiableSource.setCode(chars);
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
216 return modifiableSource;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
217 } else {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
218 throw new IOException("Attempt to modify contents of a file 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
219 }
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
220 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
221 }
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
222
787b5aa53401 Truffle: new factory method for files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 18980
diff changeset
223 /**
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
224 * Creates an anonymous source from literal text: not named and not indexed.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
225 *
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
226 * @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
227 * @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
228 * @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
229 */
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
230 public static Source fromText(CharSequence chars, String description) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
231 CompilerAsserts.neverPartOfCompilation();
22093
0f0e34039769 Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22046
diff changeset
232 return new LiteralSource(description, chars.toString());
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
233 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
234
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
235 /**
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
236 * Creates an anonymous source from literal text that is provided incrementally after creation:
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
237 * not named and not indexed.
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
238 *
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
239 * @param description a note about the origin, for error messages and debugging
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
240 * @return a newly created, non-indexed, initially empty, appendable source representation
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
241 */
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
242 public static Source fromAppendableText(String description) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
243 CompilerAsserts.neverPartOfCompilation();
22093
0f0e34039769 Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22046
diff changeset
244 return new AppendableLiteralSource(description);
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
245 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
246
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
247 /**
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
248 * Creates a source from literal text that can be retrieved by name, with no assumptions about
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
249 * the structure or meaning of the name. If the name is already in the index, the new instance
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
250 * will replace the previously existing instance in the index.
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
251 *
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
252 * @param chars textual source code
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
253 * @param name string to use for indexing/lookup
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
254 * @return a newly created, source representation
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
255 */
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
256 public static Source fromNamedText(CharSequence chars, String name) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
257 CompilerAsserts.neverPartOfCompilation();
21270
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
258 final Source source = new LiteralSource(name, chars.toString());
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
259 nameToSource.put(name, new WeakReference<>(source));
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
260 return source;
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
261 }
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
262
a43c7adc9d99 Truffle/Source: rename asPseudoFile() to fromNamedText(), which more accurately describes its use cases. Rework Javadoc for completeness and clarity.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 20906
diff changeset
263 /**
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
264 * Creates a source from literal text that is provided incrementally after creation and which
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
265 * can be retrieved by name, with no assumptions about the structure or meaning of the name. If
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
266 * the name is already in the index, the new instance will replace the previously existing
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
267 * instance in the index.
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
268 *
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
269 * @param name string to use for indexing/lookup
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
270 * @return a newly created, indexed, initially empty, appendable source representation
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
271 */
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
272 public static Source fromNamedAppendableText(String name) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
273 CompilerAsserts.neverPartOfCompilation();
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
274 final Source source = new AppendableLiteralSource(name);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
275 nameToSource.put(name, new WeakReference<>(source));
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
276 return source;
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
277 }
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
278
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
279 /**
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
280 * Creates a {@linkplain Source Source instance} that represents the contents of a sub-range of
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
281 * an existing {@link Source}.
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
282 *
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
283 * @param base an existing Source instance
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
284 * @param baseCharIndex 0-based index of the first character of the sub-range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
285 * @param length the number of characters in the sub-range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
286 * @return a new instance representing a sub-range of another Source
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
287 * @throws IllegalArgumentException if the specified sub-range is not contained in the base
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
288 */
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
289 public static Source subSource(Source base, int baseCharIndex, int length) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
290 CompilerAsserts.neverPartOfCompilation();
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
291 final SubSource subSource = SubSource.create(base, baseCharIndex, length);
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
292 return subSource;
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
293 }
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
294
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
295 /**
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
296 * Creates a {@linkplain Source Source instance} that represents the contents of a sub-range at
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
297 * the end of an existing {@link Source}.
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
298 *
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
299 * @param base an existing Source instance
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
300 * @param baseCharIndex 0-based index of the first character of the sub-range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
301 * @return a new instance representing a sub-range at the end of another Source
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
302 * @throws IllegalArgumentException if the index is out of range
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
303 */
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
304 public static Source subSource(Source base, int baseCharIndex) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
305 CompilerAsserts.neverPartOfCompilation();
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
306
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
307 return subSource(base, baseCharIndex, base.getLength() - baseCharIndex);
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
308 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
309
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
310 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
311 * 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
312 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
313 * @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
314 * @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
315 * @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
316 * @throws IOException if reading fails
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
317 */
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 public static Source fromURL(URL url, String description) throws IOException {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
319 CompilerAsserts.neverPartOfCompilation();
22093
0f0e34039769 Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22046
diff changeset
320 return URLSource.get(url, description);
16068
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
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 * 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
325 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
326 * @param reader
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
327 * @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
328 * @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
329 * @throws IOException if reading fails
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
330 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
331 public static Source fromReader(Reader reader, String description) throws IOException {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
332 CompilerAsserts.neverPartOfCompilation();
22093
0f0e34039769 Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22046
diff changeset
333 return new LiteralSource(description, read(reader));
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
336 /**
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
337 * 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
338 * 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
339 * 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
340 *
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
341 * @param bytes the raw bytes of the source
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
342 * @param description a note about the origin, possibly useful for debugging
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
343 * @param charset how to decode the bytes into Java strings
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
344 * @return a newly created, non-indexed source representation
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
345 */
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
346 public static Source fromBytes(byte[] bytes, String description, Charset charset) {
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
347 return fromBytes(bytes, 0, bytes.length, description, charset);
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
348 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
349
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
350 /**
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
351 * 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
352 * 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
353 * 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
354 * relative to byteIndex.
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
355 *
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
356 * @param bytes the raw bytes of the source
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
357 * @param byteIndex where the string starts in the byte array
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
358 * @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
359 * @param description a note about the origin, possibly useful for debugging
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
360 * @param charset how to decode the bytes into Java strings
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
361 * @return a newly created, non-indexed source representation
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
362 */
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
363 public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, Charset charset) {
21966
5023b913e2ba Help the partial evaluator / language developer by marking API methods as neverPartOfCompilation() when they are too complicated to be compiled.
Christian Wimmer <christian.wimmer@oracle.com>
parents: 21964
diff changeset
364 CompilerAsserts.neverPartOfCompilation();
22093
0f0e34039769 Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22046
diff changeset
365 return new BytesSource(description, bytes, byteIndex, length, charset);
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
366 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
367
16989
19faa7ca37c1 Truffle/Source: use a synchronized hash table
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16140
diff changeset
368 // 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
369 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
370 * 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
371 * 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
372 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
373 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
374 fileCacheEnabled = enabled;
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
377 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
378 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
379 final StringBuilder builder = new StringBuilder();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
380 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
381
21989
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
382 try {
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
383 while (true) {
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
384 final int n = bufferedReader.read(buffer);
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
385 if (n == -1) {
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
386 break;
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
387 }
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
388 builder.append(buffer, 0, n);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
389 }
21989
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
390 } finally {
72010b401152 Truffle/Source: close the Reader when finished.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21987
diff changeset
391 bufferedReader.close();
16068
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 return builder.toString();
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
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
396 private Source() {
16068
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
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
399 private String mimeType;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
400 private TextMap textMap;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
401
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
402 abstract void reset();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
403
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
404 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
405 * 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
406 * 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
407 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
408 * @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
409 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
410 public abstract String getName();
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 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
413 * 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
414 * 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
415 * full path.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
416 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
417 * @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
418 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
419 public abstract String getShortName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
420
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 * 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
423 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
424 public abstract String getPath();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
425
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
426 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
427 * The URL if the source is retrieved via URL.
22218
f47b601edbc6 Truffle/Source: add the ".js" and ".JS" extensions to the hard-wired getMimeType() implementations
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22105
diff changeset
428 *
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
429 * @return URL or <code>null</code>
16068
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 public abstract URL getURL();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
432
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
433 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
434 * Access to the source contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
435 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
436 public abstract Reader getReader();
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 * Access to the source contents.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
440 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
441 public final InputStream getInputStream() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
442 return new ByteArrayInputStream(getCode().getBytes());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
443 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
444
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
445 /**
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
446 * 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
447 */
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
448 public final int getLength() {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
449 return getTextMap().length();
19690
c152a485d747 Truffle: new method Source.getLength() and semantic adjustments to the new factory method for creating files whose contents have already been read.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 19688
diff changeset
450 }
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
451
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
452 /**
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
453 * 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
454 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
455 public abstract String getCode();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
456
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
457 /**
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
458 * 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
459 */
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
460 public String getCode(int charIndex, int charLength) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
461 return getCode().substring(charIndex, charIndex + charLength);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
462 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
463
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
464 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
465 * 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
466 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
467 public final String getCode(int lineNumber) {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
468 final int offset = getTextMap().lineStartOffset(lineNumber);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
469 final int length = getTextMap().lineLength(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
470 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
471 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
472
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
473 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
474 * 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
475 * 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
476 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
477 public final int getLineCount() {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
478 return getTextMap().lineCount();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
479 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
480
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
481 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
482 * 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
483 * position.
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
484 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
485 * @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
486 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
487 public final int getLineNumber(int offset) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
488 return getTextMap().offsetToLine(offset);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
489 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
490
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
491 /**
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
492 * 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
493 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
494 * @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
495 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
496 public final int getColumnNumber(int offset) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
497 return getTextMap().offsetToCol(offset);
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
498 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
499
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
500 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
501 * 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
502 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
503 * @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
504 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
505 public final int getLineStartOffset(int lineNumber) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
506 return getTextMap().lineStartOffset(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
507 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
508
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
509 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
510 * 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
511 * numbered line.
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
512 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
513 * @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
514 */
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
515 public final int getLineLength(int lineNumber) throws IllegalArgumentException {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
516 return getTextMap().lineLength(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
517 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
518
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
519 /**
21291
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
520 * Append text to a Source explicitly created as <em>Appendable</em>.
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
521 *
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
522 * @param chars the text to append
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
523 * @throws UnsupportedOperationException by concrete subclasses that do not support appending
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
524 */
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
525 public void appendCode(CharSequence chars) {
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
526 throw new UnsupportedOperationException();
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
527 }
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
528
70bbc530a2d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21271
diff changeset
529 /**
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
530 * 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
531 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
532 * 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
533 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
534 * 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
535 * {@link Source} representations are canonical.
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
536 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
537 * @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
538 * @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
539 * @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
540 * @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
541 * @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
542 * @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
543 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
544 public final SourceSection createSection(String identifier, int startLine, int startColumn, int charIndex, int length) {
22371
75c4ce3a352a add range check to 5-parameter Source#createSection
Andreas Woess <andreas.woess@oracle.com>
parents: 22268
diff changeset
545 checkRange(charIndex, length);
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
546 return new SourceSection(null, this, identifier, startLine, startColumn, charIndex, length);
16068
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
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 * Creates a representation of a contiguous region of text in the source. Computes the
21972
ff6f34159b8a Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21966
diff changeset
551 * {@code charIndex} value by building a {@code TextMap map} of lines in the source.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
552 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
553 * 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
554 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
555 * 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
556 * {@link Source} representations are canonical.
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 * @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
559 * @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
560 * @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
561 * @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
562 * @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
563 * @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
564 * @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
565 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
566 public final SourceSection createSection(String identifier, int startLine, int startColumn, int length) {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
567 final int lineStartOffset = getTextMap().lineStartOffset(startLine);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
568 if (startColumn > getTextMap().lineLength(startLine)) {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
569 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
570 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
571 final int startOffset = lineStartOffset + startColumn - 1;
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
572 return new SourceSection(null, this, identifier, startLine, startColumn, startOffset, length);
16068
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 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
576 * Creates a representation of a contiguous region of text in the source. Computes the
21972
ff6f34159b8a Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21966
diff changeset
577 * {@code (startLine, startColumn)} values by building a {@code TextMap map} of lines in the
ff6f34159b8a Providing package-info for most of API packages. Feel free to provide your package-info.java for anything that has API in its name.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21966
diff changeset
578 * source.
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
579 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
580 * 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
581 * <p>
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
582 * 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
583 * {@link Source} representations are canonical.
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 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
586 * @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
587 * @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
588 * @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
589 * @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
590 * @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
591 * source
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
592 * @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
593 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
594 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
595 checkRange(charIndex, length);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
596 final int startLine = getLineNumber(charIndex);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
597 final int startColumn = charIndex - getLineStartOffset(startLine) + 1;
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
598 return new SourceSection(null, this, identifier, startLine, startColumn, charIndex, length);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
599 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
600
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
601 void checkRange(int charIndex, int length) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
602 if (!(charIndex >= 0 && length >= 0 && charIndex + length <= getCode().length())) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
603 throw new IllegalArgumentException("text positions out of range");
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
604 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
605 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
606
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
607 /**
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
608 * 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
609 * 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
610 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
611 * @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
612 * @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
613 * @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
614 * @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
615 * @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
616 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
617 public final SourceSection createSection(String identifier, int lineNumber) {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
618 final int charIndex = getTextMap().lineStartOffset(lineNumber);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
619 final int length = getTextMap().lineLength(lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
620 return createSection(identifier, charIndex, length);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
621 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
622
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 * 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
625 * 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
626 *
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
627 * @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
628 * @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
629 */
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
630 public final LineLocation createLineLocation(int lineNumber) {
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
631 return new LineLocation(this, lineNumber);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
632 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
633
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
634 /**
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
635 * An object suitable for using as a key into a hashtable that defines equivalence between
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
636 * different source types.
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
637 */
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
638 Object getHashKey() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
639 return getName();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
640 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
641
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
642 final TextMap getTextMap() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
643 if (textMap == null) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
644 textMap = createTextMap();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
645 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
646 return textMap;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
647 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
648
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
649 final void clearTextMap() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
650 textMap = null;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
651 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
652
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
653 TextMap createTextMap() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
654 final String code = getCode();
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
655 if (code == null) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
656 throw new RuntimeException("can't read file " + getName());
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
657 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
658 return TextMap.fromString(code);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
659 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
660
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
661 /**
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
662 * Associates the source with specified MIME type. The mime type may be used to select the right
22105
7ee578004be7 Formating and lints
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22104
diff changeset
663 * {@link Registration Truffle language} to use to execute the returned source. The value of
7ee578004be7 Formating and lints
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22104
diff changeset
664 * MIME type can be obtained via {@link #getMimeType()} method.
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
665 *
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
666 * @param mime mime type to use
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
667 * @return new (identical) source, just associated {@link #getMimeType()}
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
668 */
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
669 public final Source withMimeType(String mime) {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
670 try {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
671 Source another = (Source) clone();
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
672 another.mimeType = mime;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
673 return another;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
674 } catch (CloneNotSupportedException ex) {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
675 throw new IllegalStateException(ex);
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
676 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
677 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
678
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
679 /**
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
680 * MIME type that is associated with this source. By default file extensions known to the system
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
681 * are used to determine the MIME type (via registered {@link FileTypeDetector} classes), yet
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
682 * one can directly {@link #withMimeType(java.lang.String) provide a MIME type} to each source.
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
683 *
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
684 * @return MIME type of this source or <code>null</code>, if unknown
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
685 */
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
686 public String getMimeType() {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
687 if (mimeType == null) {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
688 mimeType = findMimeType();
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
689 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
690 return mimeType;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
691 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
692
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
693 String findMimeType() {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
694 return null;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
695 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
696
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
697 final boolean equalMime(Source other) {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
698 if (mimeType == null) {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
699 return other.mimeType == null;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
700 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
701 return mimeType.equals(other.mimeType);
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
702 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
703
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
704 private static final class LiteralSource extends Source implements Cloneable {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
705
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
706 private final String description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
707 private final String code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
708
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
709 public LiteralSource(String description, String code) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
710 this.description = description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
711 this.code = code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
712 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
713
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
714 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
715 public String getName() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
716 return description;
16068
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
719 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
720 public String getShortName() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
721 return description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
722 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
723
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
724 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
725 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
726 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
727 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
728
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
729 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
730 public String getPath() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
731 return description;
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
732 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
733
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
734 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
735 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
736 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
737 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
738
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
739 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
740 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
741 return new StringReader(code);
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
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
745 void reset() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
746 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
747
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
748 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
749 public int hashCode() {
22046
e7c2d36daf72 TruffleLanguage.parse method to convert a source to CallTarget. Basic caching to make sure the code is shared among tenants in one JVM.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22036
diff changeset
750 return description.hashCode() * code.hashCode();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
751 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
752
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
753 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
754 public boolean equals(Object obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
755 if (this == obj) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
756 return true;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
757 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
758 if (obj == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
759 return false;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
760 }
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
761 if (obj instanceof LiteralSource) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
762 LiteralSource other = (LiteralSource) obj;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
763 return description.equals(other.description) && code.equals(other.code) && equalMime(other);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
764 }
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
765 return false;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
766 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
767 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
768
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
769 private static final class AppendableLiteralSource extends Source implements Cloneable {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
770 private final String description;
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
771 final List<CharSequence> codeList = new ArrayList<>();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
772
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
773 public AppendableLiteralSource(String description) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
774 this.description = description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
775 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
776
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
777 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
778 public String getName() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
779 return description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
780 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
781
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
782 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
783 public String getShortName() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
784 return description;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
785 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
786
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
787 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
788 public String getCode() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
789 return getCodeFromIndex(0);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
790 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
791
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
792 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
793 public String getPath() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
794 return description;
16068
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
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
797 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
798 public URL getURL() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
799 return null;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
800 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
801
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
802 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
803 public Reader getReader() {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
804 return new StringReader(getCode());
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
805 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
806
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
807 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
808 void reset() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
809 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
810
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
811 private String getCodeFromIndex(int index) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
812 StringBuilder sb = new StringBuilder();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
813 for (int i = index; i < codeList.size(); i++) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
814 CharSequence s = codeList.get(i);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
815 sb.append(s);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
816 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
817 return sb.toString();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
818 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
819
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
820 @Override
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
821 public void appendCode(CharSequence chars) {
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
822 codeList.add(chars);
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
823 clearTextMap();
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
824 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
825
16068
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
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
828 private static final class FileSource extends Source implements Cloneable {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
829
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
830 private final File file;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
831 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
832 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
833
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
834 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
835 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
836
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
837 public FileSource(File file, String name, String path) {
18980
ae0eb836e6f4 store absolute file in FileSource
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18979
diff changeset
838 this.file = file.getAbsoluteFile();
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
839 this.name = name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
840 this.path = path;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
841 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
842
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
843 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
844 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
845 return name;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
846 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
847
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
848 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
849 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
850 return file.getName();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
851 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
852
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
853 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
854 Object getHashKey() {
21271
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
855 return path;
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
856 }
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
857
1516d26e8f2b Truffle/Source: add a new kind of Source (both indexed and non-indexed flavors) whose contents are unavailable at creation, but will be provided incrementally.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21270
diff changeset
858 @Override
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
859 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
860 if (fileCacheEnabled) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
861 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
862 try {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
863 code = read(getReader());
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
864 timeStamp = file.lastModified();
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
865 } catch (IOException e) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
866 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
867 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
868 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
869 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
870 try {
22098
0556c0056d71 read files in UTF-8 encoding
Christian Wirth <christian.wirth@oracle.com>
parents: 22093
diff changeset
871 return read(new InputStreamReader(new FileInputStream(file), "UTF-8"));
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
872 } catch (IOException e) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
873 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
874 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
875 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
876
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
877 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
878 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
879 return path;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
880 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
881
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
882 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
883 public URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
884 return null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
885 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
886
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
887 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
888 public Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
889 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
890 return new StringReader(code);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
891 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
892 try {
22098
0556c0056d71 read files in UTF-8 encoding
Christian Wirth <christian.wirth@oracle.com>
parents: 22093
diff changeset
893 return new InputStreamReader(new FileInputStream(file), "UTF-8");
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
894 } catch (FileNotFoundException e) {
18979
bdb24e259c11 include original exception in FileSource.getReader
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18676
diff changeset
895
bdb24e259c11 include original exception in FileSource.getReader
Lukas Stadler <lukas.stadler@oracle.com>
parents: 18676
diff changeset
896 throw new RuntimeException("Can't find file " + path, e);
22098
0556c0056d71 read files in UTF-8 encoding
Christian Wirth <christian.wirth@oracle.com>
parents: 22093
diff changeset
897 } catch (UnsupportedEncodingException e) {
0556c0056d71 read files in UTF-8 encoding
Christian Wirth <christian.wirth@oracle.com>
parents: 22093
diff changeset
898 throw new RuntimeException("Unsupported encoding in file " + path, e);
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
899 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
900 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
901
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
902 @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
903 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
904 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
905 }
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
906
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
907 @Override
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
908 String findMimeType() {
22209
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
909 try {
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
910 return Files.probeContentType(file.toPath());
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
911 } catch (IOException ex) {
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
912 LOG.log(Level.SEVERE, null, ex);
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
913 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
914 return null;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
915 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
916
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
917 @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
918 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
919 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
920 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
921 }
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
922 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
923 FileSource other = (FileSource) obj;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
924 return path.equals(other.path) && equalMime(other);
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
925 }
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
926 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
927 }
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
928
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
929 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
930 void reset() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
931 this.code = null;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
932 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
933 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
934
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
935 // TODO (mlvdv) if we keep this, hoist a superclass in common with FileSource.
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
936 private static final class ClientManagedFileSource extends Source implements Cloneable {
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
937
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
938 private final File file;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
939 private final String name; // Name used originally to describe the source
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
940 private final String path; // Normalized path description of an actual file
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
941 private String code; // The file's contents, as provided by the client
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
942
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
943 public ClientManagedFileSource(File file, String name, String path, CharSequence chars) {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
944 this.file = file.getAbsoluteFile();
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
945 this.name = name;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
946 this.path = path;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
947 setCode(chars);
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
948 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
949
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
950 void setCode(CharSequence chars) {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
951 this.code = chars.toString();
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
952 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
953
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
954 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
955 public String getName() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
956 return name;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
957 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
958
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
959 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
960 public String getShortName() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
961 return file.getName();
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
962 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
963
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
964 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
965 Object getHashKey() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
966 return path;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
967 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
968
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
969 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
970 public String getCode() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
971 return code;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
972 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
973
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
974 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
975 public String getPath() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
976 return path;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
977 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
978
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
979 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
980 public URL getURL() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
981 return null;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
982 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
983
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
984 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
985 public Reader getReader() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
986 return new StringReader(code);
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
987 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
988
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
989 @Override
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
990 String findMimeType() {
22209
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
991 try {
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
992 return Files.probeContentType(file.toPath());
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
993 } catch (IOException ex) {
dcee887614b1 Removing hardcoded MIME types in the source. Each language is supposed to register its own FileTypeDetector as SL did in cf19259edf87.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22157
diff changeset
994 LOG.log(Level.SEVERE, null, ex);
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
995 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
996 return null;
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
997 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
998
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
999 @Override
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1000 public int hashCode() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1001 return path.hashCode();
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1002 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1003
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1004 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1005 public boolean equals(Object obj) {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1006 if (this == obj) {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1007 return true;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1008 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1009 if (obj instanceof ClientManagedFileSource) {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1010 ClientManagedFileSource other = (ClientManagedFileSource) obj;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1011 return path.equals(other.path) && equalMime(other);
22102
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1012 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1013 return false;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1014 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1015
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1016 @Override
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1017 void reset() {
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1018 this.code = null;
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1019 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1020 }
04b13488da2a Truffle/Source: modify behavior of factory Source.fromFileName((CharSequence, String). If a Source already exists with this file name, and it was created with this method originally, then the original Source will be returned with contents replaced.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 22098
diff changeset
1021
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1022 private static final class URLSource extends Source implements Cloneable {
16068
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 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
1025
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1026 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
1027 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
1028 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
1029 if (source == null) {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1030 source = new URLSource(url, name);
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1031 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
1032 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1033 return source;
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1036 private final URL url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1037 private final String name;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1038 private String code; // A cache of the source contents
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1039
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1040 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
1041 this.url = url;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1042 this.name = name;
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1043 URLConnection c = url.openConnection();
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1044 if (super.mimeType == null) {
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1045 super.mimeType = c.getContentType();
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1046 }
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1047 code = read(new InputStreamReader(c.getInputStream()));
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1048 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1049
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1050 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1051 public String getName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1052 return name;
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 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1056 public String getShortName() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1057 return name;
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1060 @Override
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1061 public String getPath() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1062 return url.getPath();
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 URL getURL() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1067 return url;
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 Reader getReader() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1072 return new StringReader(code);
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
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1076 public String getCode() {
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1077 return code;
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1078 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1079
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1080 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1081 void reset() {
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1082 }
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
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1085 private static final class SubSource extends Source implements Cloneable {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1086 private final Source base;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1087 private final int baseIndex;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1088 private final int subLength;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1089
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1090 private static SubSource create(Source base, int baseIndex, int length) {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1091 if (baseIndex < 0 || length < 0 || baseIndex + length > base.getLength()) {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1092 throw new IllegalArgumentException("text positions out of range");
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1093 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1094 return new SubSource(base, baseIndex, length);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1095 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1096
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1097 private SubSource(Source base, int baseIndex, int length) {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1098 this.base = base;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1099 this.baseIndex = baseIndex;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1100 this.subLength = length;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1101 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1102
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1103 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1104 void reset() {
21292
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1105 assert false;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1106 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1107
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1108 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1109 public String getName() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1110 return base.getName();
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1111 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1112
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1113 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1114 public String getShortName() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1115 return base.getShortName();
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1116 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1117
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1118 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1119 public String getPath() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1120 return base.getPath();
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1121 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1122
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1123 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1124 public URL getURL() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1125 return null;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1126 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1127
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1128 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1129 public Reader getReader() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1130 assert false;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1131 return null;
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1132 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1133
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1134 @Override
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1135 public String getCode() {
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1136 return base.getCode(baseIndex, subLength);
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1137 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1138 }
8eadc82f4a93 Truffle/Source: fix an inconsistency in the ordering of the private subclass sources; clean up the getTextMap() abstraction for more safety.
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 21291
diff changeset
1139
22104
cf19259edf87 TruffleVM.eval and Source.withMimeType
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 22102
diff changeset
1140 private static final class BytesSource extends Source implements Cloneable {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1141
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1142 private final String name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1143 private final byte[] bytes;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1144 private final int byteIndex;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1145 private final int length;
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1146 private final CharsetDecoder decoder;
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1147
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1148 public BytesSource(String name, byte[] bytes, int byteIndex, int length, Charset decoder) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1149 this.name = name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1150 this.bytes = bytes;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1151 this.byteIndex = byteIndex;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1152 this.length = length;
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1153 this.decoder = decoder.newDecoder();
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1154 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1155
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1156 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1157 void reset() {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1158 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1159
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1160 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1161 public String getName() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1162 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1163 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1164
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1165 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1166 public String getShortName() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1167 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1168 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1169
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1170 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1171 public String getPath() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1172 return name;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1173 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1174
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1175 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1176 public URL getURL() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1177 return null;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1178 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1179
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1180 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1181 public Reader getReader() {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1182 return null;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1183 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1184
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1185 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1186 public String getCode() {
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1187 ByteBuffer bb = ByteBuffer.wrap(bytes, byteIndex, length);
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1188 CharBuffer chb;
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1189 try {
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1190 chb = decoder.decode(bb);
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1191 } catch (CharacterCodingException ex) {
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1192 return "";
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1193 }
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1194 return chb.toString();
17066
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
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1197 @Override
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1198 public String getCode(int byteOffset, int codeLength) {
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1199 ByteBuffer bb = ByteBuffer.wrap(bytes, byteIndex + byteOffset, codeLength);
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1200 CharBuffer chb;
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1201 try {
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1202 chb = decoder.decode(bb);
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1203 } catch (CharacterCodingException ex) {
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1204 return "";
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1205 }
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1206 return chb.toString();
17066
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
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1209 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1210 void checkRange(int charIndex, int rangeLength) {
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1211 if (!(charIndex >= 0 && rangeLength >= 0 && charIndex + rangeLength <= length)) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1212 throw new IllegalArgumentException("text positions out of range");
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1213 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1214 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1215
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1216 @Override
21964
9d15f06f3537 Using protected access modifier in non-subclassable class is suspicious. Making package private.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21951
diff changeset
1217 TextMap createTextMap() {
21987
b2d1c8ff592a Less classes in the source API package. Merging interfaces and their only implementation into final classes. Hiding NullSourceSection behind factory method. Using JDK's standard CharsetDecoder instead of proprietary BytesDecoder.
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents: 21972
diff changeset
1218 return TextMap.fromString(getCode());
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1219 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1220 }
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 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1223 * 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
1224 * newline characters. The coordinate systems are:
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1225 * <ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1226 * <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
1227 * 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
1228 * <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
1229 * text is at (1,1).</li>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1230 * </ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1231 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1232 * 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
1233 * 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
1234 * 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
1235 * 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
1236 * 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
1237 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1238 * 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
1239 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1240 * 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
1241 * 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
1242 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1243 * <strong>Limitations:</strong>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1244 * <ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1245 * <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
1246 * <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
1247 * <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
1248 * </ul>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1249 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1250 private static final class TextMap {
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 // 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
1253 private final int[] nlOffsets;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1254
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1255 // 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
1256 private final int textLength;
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 // Is the final text character a newline?
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1259 final boolean finalNL;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1260
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1261 public TextMap(int[] nlOffsets, int textLength, boolean finalNL) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1262 this.nlOffsets = nlOffsets;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1263 this.textLength = textLength;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1264 this.finalNL = finalNL;
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1265 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1266
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1267 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1268 * 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
1269 * lines/columns.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1270 */
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1271 public static TextMap fromString(String text) {
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1272 final int textLength = text.length();
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1273 final ArrayList<Integer> lines = new ArrayList<>();
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1274 lines.add(0);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1275 int offset = 0;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1276
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1277 while (offset < text.length()) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1278 final int nlIndex = text.indexOf('\n', offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1279 if (nlIndex >= 0) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1280 offset = nlIndex + 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1281 lines.add(offset);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1282 } else {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1283 break;
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 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1286 lines.add(Integer.MAX_VALUE);
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1287
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1288 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
1289 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
1290 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
1291 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1292
17066
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1293 final boolean finalNL = textLength > 0 && (textLength == nlOffsets[nlOffsets.length - 2]);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1294
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1295 return new TextMap(nlOffsets, textLength, finalNL);
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1296 }
0bcefb0f8488 Truffle: byte[] sources.
Chris Seaton <chris.seaton@oracle.com>
parents: 16989
diff changeset
1297
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1298 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1299 * 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
1300 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1301 * @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
1302 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1303 public int offsetToLine(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1304 if (offset < 0 || offset >= textLength) {
22036
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1305 if (offset == 0 && textLength == 0) {
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1306 return 1;
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1307 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1308 throw new IllegalArgumentException("offset out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1309 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1310 int line = 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1311 while (offset >= nlOffsets[line]) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1312 line++;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1313 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1314 return line;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1315 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1316
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1317 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1318 * 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
1319 * character.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1320 * <p>
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1321 * 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
1322 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1323 * @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
1324 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1325 public int offsetToCol(int offset) throws IllegalArgumentException {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1326 return 1 + offset - nlOffsets[offsetToLine(offset) - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1327 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1328
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1329 /**
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
1330 * 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
1331 */
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
1332 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
1333 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
1334 }
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
1335
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
1336 /**
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1337 * 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
1338 * 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
1339 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1340 public int lineCount() {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1341 if (textLength == 0) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1342 return 0;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1343 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1344 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
1345 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1346
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1347 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1348 * 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
1349 * 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
1350 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1351 * @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
1352 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1353 public int lineStartOffset(int line) throws IllegalArgumentException {
22036
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1354 if (textLength == 0) {
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1355 return 0;
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1356 }
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1357 if (lineOutOfRange(line)) {
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1358 throw new IllegalArgumentException("line out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1359 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1360 return nlOffsets[line - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1361 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1362
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1363 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1364 * 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
1365 * <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
1366 *
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1367 * @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
1368 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1369 public int lineLength(int line) throws IllegalArgumentException {
22036
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1370 if (textLength == 0) {
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1371 return 0;
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1372 }
f26b6524e5e0 fixes for empty sources
Mick Jordan <mick.jordan@oracle.com>
parents: 21989
diff changeset
1373 if (lineOutOfRange(line)) {
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1374 throw new IllegalArgumentException("line out of bounds");
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1375 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1376 if (line == nlOffsets.length - 1 && !finalNL) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1377 return textLength - nlOffsets[line - 1];
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1378 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1379 return (nlOffsets[line] - nlOffsets[line - 1]) - 1;
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1380
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1381 }
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1382
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1383 /**
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1384 * Is the line number out of range.
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1385 */
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1386 private boolean lineOutOfRange(int line) {
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1387 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
1388 }
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1389
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1390 }
16130
6f7d3f3703d3 Truffle/Source:
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents: 16068
diff changeset
1391
16068
74e142bd2b12 Truffle/Source: major API revision
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
diff changeset
1392 }