public abstract class Source extends Object
assert file.getName().endsWith(".java") :
"Imagine 'c:\\sources\\Example.java' file";
String language = Source.findLanguage(file);
Source source = Source.newBuilder(language, file).build();
assert file.getName().equals(source.getName());
assert file.getPath().equals(source.getPath());
assert file.toUri().equals(source.getURI());
The starting point is Source.newBuilder(String, TruffleFile) method.
Source.newBuilder(String, java.net.URL)
factory: Each URL source is represented as a canonical object, indexed by the URL. Contents are read eagerly once theURLresource = relativeClass.getResource("sample.js");Sourcesource =Source.newBuilder("js", resource) .build(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toURI().equals(source.getURI());
Source.SourceBuilder.build() method is called.
Source.newBuilder(String, CharSequence, String) factory method: Sourcesource =Source.newBuilder("js", "function() {\n" + " return 'Hi';\n" + "}\n", "hi.js").build(); assert "hi.js".equals(source.getName());
Reader one can convert its content into a Source via
Source.newBuilder(String, java.io.Reader, String) method: the content is read eagerly once theReaderstream = newInputStreamReader( relativeClass.getResourceAsStream("sample.js") );Sourcesource =Source.newBuilder("js", stream, "sample.js") .build(); assert "sample.js".equals(source.getName());
Source.SourceBuilder.build() method is called.
UnsupportedOperationException if that is the case.
Source.getName() ,
(), Source.getMimeType() and these are immutable. The system makes the best
effort to derive values of these attributes from the location and/or content of the
Source object. However, to give the user that creates the source control over these
attributes, the API offers an easy way to alter values of these attributes.
byte or character based, or none of
those when no content is specified. For literal sources it depends on
whether the byte or character based factory method was used. When the source was loaded from a
file or url then the default
MIME type of the provided language will be used to determine whether bytes or characters should
be loaded. The behavior can be customized by specifying a MIME type or content explicitly. If the specified or
inferred MIME type starts with 'text/ or the MIME types is null then it
will be interpreted as character based, otherwise byte based.| Modifier and Type | Class and Description |
|---|---|
class |
Source.LiteralBuilder
|
class |
Source.SourceBuilder
|
| Modifier and Type | Field and Description |
|---|---|
static CharSequence |
CONTENT_NONE
Constant to be used as an argument to
Source.SourceBuilder.content(CharSequence) to set no
content to the Source built. |
| Modifier and Type | Method and Description |
|---|---|
SourceSection |
createSection(int lineNumber)
Creates a representation of a line of text in the source identified only by line number, from
which the character information will be computed.
|
SourceSection |
createSection(int charIndex,
int length)
Creates a representation of a contiguous region of text in the source.
|
SourceSection |
createSection(int startLine,
int startColumn,
int length)
Creates a representation of a contiguous region of text in the source.
|
SourceSection |
createSection(int startLine,
int startColumn,
int endLine,
int endColumn)
Create representation of a contiguous region in the source that does not have the character
content available.
|
SourceSection |
createUnavailableSection()
Returns an unavailable source section indicating that the source location is not available.
|
boolean |
equals(Object obj) |
static String |
findLanguage(String mimeType)
Returns the first installed language that supports evaluating sources for a given MIME type.
|
static String |
findLanguage(TruffleFile file)
Returns the first language that supports evaluating the probed mime type of a given
file. |
static String |
findLanguage(URL url)
Returns the first language that supports evaluating the probed mime type of a given
URL. |
static String |
findMimeType(TruffleFile file)
Returns the probed MIME type for a given file, or
null if no MIME type could be
resolved. |
static String |
findMimeType(URL url)
Returns the probed MIME type for a given url, or
null if no MIME type could be
resolved. |
abstract ByteSequence |
getBytes()
Returns the bytes of the source if it is a
byte based source. |
abstract CharSequence |
getCharacters()
Returns all characters of the source.
|
CharSequence |
getCharacters(int lineNumber)
Gets the text (not including a possible terminating newline) in a (1-based) numbered line.
|
int |
getColumnNumber(int offset)
Given a 0-based character offset, return the 1-based number of the column at the position.
|
abstract String |
getLanguage()
Returns the language this source was created with.
|
int |
getLength()
Gets the number of characters or bytes of the source.
|
int |
getLineCount()
The number of text lines in the source, including empty lines; characters at the end of the
source without a terminating newline count as a line.
|
int |
getLineLength(int lineNumber)
The number of characters (not counting a possible terminating newline) in a (1-based)
numbered line.
|
int |
getLineNumber(int offset)
Given a 0-based character offset, return the 1-based number of the line that includes the
position.
|
int |
getLineStartOffset(int lineNumber)
Given a 1-based line number, return the 0-based offset of the first character in the line.
|
abstract String |
getMimeType()
Returns the MIME type that is associated with this source.
|
abstract String |
getName()
Returns the name of this resource holding a guest language program.
|
abstract String |
getPath()
The fully qualified name of the source.
|
Reader |
getReader()
Access to the source contents.
|
URI |
getURI()
Get URI of the source.
|
abstract URL |
getURL()
The URL if the source is retrieved via URL.
|
abstract boolean |
hasBytes()
Returns
true if this source represents a byte based source, else
false. |
abstract boolean |
hasCharacters()
Returns
true if this source represents a character based source, else
false. |
int |
hashCode() |
abstract boolean |
isCached()
Returns
true if code caching is enabled for this source. |
abstract boolean |
isInteractive()
Check whether this source has been marked as interactive.
|
abstract boolean |
isInternal()
Check whether this source has been marked as internal, meaning that it has been
provided by the infrastructure, language implementation, or system library.
|
static Source.LiteralBuilder |
newBuilder(Source source)
Creates a new source builder that inherits from the given Source.
|
static Source.LiteralBuilder |
newBuilder(String language,
ByteSequence bytes,
String name)
Creates a new byte based literal source from a byte sequence.
|
static Source.LiteralBuilder |
newBuilder(String language,
CharSequence characters,
String name)
Creates a new character based literal source from a character sequence.
|
static Source.SourceBuilder |
newBuilder(String language,
Reader source,
String name)
Creates new character based literal source from a reader.
|
static Source.SourceBuilder |
newBuilder(String language,
TruffleFile file)
Creates a new file based source builder from a given file.
|
static Source.SourceBuilder |
newBuilder(String language,
URL url)
Creates a new URL based source builder from a given URL.
|
Source |
subSource(int baseCharIndex,
int length)
|
String |
toString() |
public static final CharSequence CONTENT_NONE
Source.SourceBuilder.content(CharSequence) to set no
content to the Source built. The created sections will contain location information only and
no characters. That's useful mainly when the source code is not available, but there are
relative file paths, like in Java bytecode or LLVM bitcode. It's up to tools to resolve those
relative paths and use the section location in resolved sources.public abstract String getLanguage()
id of the language.public abstract String getName()
Source.getPath().public abstract String getPath()
File or
TruffleFile, then the path is the normalized, canonical path. If canonicalizePath(false)
is used when building the source then TruffleFile.getPath() is used instead. If the
source originates from an URL, then it's the path component of the URL.public abstract boolean isInternal()
On the other hand, tools should be free to make internal sources visible in (possibly privileged) modes that are useful for language implementors.
One can specify whether a source is internal when building it.
public abstract boolean isCached()
true if code caching is enabled for this source. If true
then the source does not require parsing every time this source is evaluated. If
false then the source requires parsing every time the source is evaluated but
does not remember any state. Disabling caching may be useful if the source is known to only
be evaluated once.public abstract boolean isInteractive()
Depending on language interactive capability, when interactive sources are executed, the
appropriate result can be passed directly to the environment
output stream or
error stream and
input stream can be used to read user
input during the execution, to clarify the execution behavior by asking questions for
instance. Non-interactive languages are expected to ignore this property.
One can specify whether a source is interactive when
building it.
public Source subSource(int baseCharIndex, int length)
baseCharIndex - 0-based index of the first character of the sub-rangelength - the number of characters in the sub-rangeIllegalArgumentException - if the specified sub-range is not contained in the baseUnsupportedOperationException - if this source cannot contain characters.public abstract CharSequence getCharacters()
UnsupportedOperationException - if this source cannot contain characters.public abstract boolean hasBytes()
true if this source represents a byte based source, else
false. A source is either a byte based, a character based, or with
no content, but never both byte and character based at the same time.
The method Source.getBytes() is only supported if this method returns true.
Source.getBytes()public abstract boolean hasCharacters()
true if this source represents a character based source, else
false. A source is either a byte based, a character based, or with
no content, but never both byte and character based at the same time.
The following methods are only supported if Source.hasCharacters() is true:
public abstract ByteSequence getBytes()
byte based source.UnsupportedOperationException - if this source cannot contain bytes
.Source.hasBytes()public abstract URL getURL()
nullpublic final URI getURI()
URI, which can be used as a
persistent identification of the source. For example one can
register a breakpoint using a URI to a source that isn't loaded yet and it will be activated
when the source is evaluated. The URI returned by this method should be as unique as
possible, yet it can happen that different sources return the same
Source.getURI() - for example when content of a file on a disk changes and is re-loaded.nullpublic abstract String getMimeType()
null
MIME type by default. If the MIME type is null then the
default MIME type of the language will be used to
interpret the source. If set explicitly then the language needs to
support the MIME type in order to evaluate the source. If not null the MIME type is already verified containing
no spaces and a '/' between group and sub-group. An example for a valid MIME type is:
text/javascript.
The MIME type can be guessed by the system based on files
or urls
null, if not explicitly set.LanguageInfo.getDefaultMimeType(),
LanguageInfo.getMimeTypes(),
Source.findMimeType(TruffleFile),
Source.findMimeType(URL)public final Reader getReader()
public final int getLength()
UnsupportedOperationException - if this source does not contain characters, nor bytes.public final CharSequence getCharacters(int lineNumber)
UnsupportedOperationException - if this source cannot contain characters.public final int getLineCount()
UnsupportedOperationException - if this source cannot contain characters.public final int getLineNumber(int offset)
throws IllegalArgumentException
UnsupportedOperationException - if this source cannot contain characters.IllegalArgumentException - if the offset is outside the text contentspublic final int getColumnNumber(int offset)
throws IllegalArgumentException
UnsupportedOperationException - if this source cannot contain characters.IllegalArgumentException - if the offset is outside the text contentspublic final int getLineStartOffset(int lineNumber)
throws IllegalArgumentException
UnsupportedOperationException - if this source cannot contain characters.IllegalArgumentException - if there is no such line in the textpublic final int getLineLength(int lineNumber)
throws IllegalArgumentException
UnsupportedOperationException - if this source cannot contain characters.IllegalArgumentException - if there is no such line in the textpublic final SourceSection createUnavailableSection()
0, but returns false for
SourceSection.isAvailable(). Unavailable source sections may be created for character
and byte based sources.SourceSection.isAvailable()public final SourceSection createSection(int startLine, int startColumn, int endLine, int endColumn)
startLine - 1-based line number of the first character in the sectionstartColumn - 1-based column number of the first character in the section, or
-1 when the column is not definedendLine - 1-based line number of the last character in the sectionendColumn - 1-based column number of the last character in the section, or
-1 when the column is not definedUnsupportedOperationException - if this source has bytes.public final SourceSection createSection(int lineNumber)
code of this source to be loaded, if there is any.
If no code is available, a
SourceSection without character content is created with
the start line and the same
end line defined only.lineNumber - 1-based line number of the first character in the sectionUnsupportedOperationException - if this source contains bytes.IllegalArgumentException - if the given lineNumber does not exist the sourcepublic final SourceSection createSection(int charIndex, int length)
code of this source to
be loaded if assertions enabled. The bounds of the source section are only verified if
assertions (-ea) are enabled in the host system. An IllegalArgumentException is
thrown if the given indices are out of bounds of the source bounds.charIndex - 0-based position of the first character in the sectionlength - the number of characters in the sectionUnsupportedOperationException - if this source cannot contain characters.IllegalArgumentException - if charIndex < 0 or length < 0; in case assertions are
enabled also if the given bounds are out of the source bounds.public final SourceSection createSection(int startLine, int startColumn, int length)
charIndex value by building a text map of lines in the
source. Please note that calling this method does cause the code of this source to be loaded, if there is any. If no code
is available, UnsupportedOperationException is thrown.
Use Source.createSection(int, int, int, int) to create a SourceSection without character
content.startLine - 1-based line number of the first character in the sectionstartColumn - 1-based column number of the first character in the sectionlength - the number of characters in the sectionUnsupportedOperationException - if this source does not contain characters.IllegalArgumentException - if arguments are outside the text of the source boundsSource.createSection(int, int),
Source.createSection(int, int, int, int)public static Source.LiteralBuilder newBuilder(String language, CharSequence characters, String name)
Use this method for sources that do originate from a literal. For file or URL sources use the
appropriate builder constructor and Source.SourceBuilder.content(CharSequence).
Example usage:
Sourcesource =Source.newBuilder("js", "function() {\n" + " return 'Hi';\n" + "}\n", "hi.js").build(); assert "hi.js".equals(source.getName());
language - the language id, must not be nullcharacters - the character sequence or string, must not be nullname - the name of the source, if null then "Unnamed" will be
used as name.public static Source.LiteralBuilder newBuilder(String language, ByteSequence bytes, String name)
Use this method for sources that do originate from a literal. For file or URL sources use the
appropriate builder constructor and Source.SourceBuilder.content(ByteSequence).
Example usage:
byte[] bytes = new byte[] {/* Binary */};
Source source = Source.newBuilder("llvm",
ByteSequence.create(bytes),
"<literal>").build();
language - the language id, must not be nullbytes - the byte sequence or string, must not be nullname - the name of the source, if null then "Unnamed" will be
used as name.public static Source.SourceBuilder newBuilder(String language, TruffleFile file)
binary or character
source depending on the default MIME type of the
language, the contents or the specified
MIME type. A language may be detected from an existing
file using Source.findLanguage(TruffleFile).
Example usage:
assert file.getName().endsWith(".java") :
"Imagine 'c:\\sources\\Example.java' file";
String language = Source.findLanguage(file);
Source source = Source.newBuilder(language, file).build();
assert file.getName().equals(source.getName());
assert file.getPath().equals(source.getPath());
assert file.toUri().equals(source.getURI());
language - the language id, must not be nullfile - the file to use and load, must not be nullpublic static Source.SourceBuilder newBuilder(String language, URL url)
binary or character
source depending on the default MIME type of the
language, the contents or the specified
MIME type. A language may be detected from an existing
file using Source.findLanguage(URL).
Example usage:
URLresource = relativeClass.getResource("sample.js");Sourcesource =Source.newBuilder("js", resource) .build(); assert resource.toExternalForm().equals(source.getPath()); assert "sample.js".equals(source.getName()); assert resource.toURI().equals(source.getURI());
language - the language id, must not be nullurl - the URL to use and load, must not be nullpublic static Source.SourceBuilder newBuilder(String language, Reader source, String name)
Use this method for sources that do originate from a literal. For file or URL sources use the
appropriate builder constructor and Source.SourceBuilder.content(CharSequence).
Example usage:
Readerstream = newInputStreamReader( relativeClass.getResourceAsStream("sample.js") );Sourcesource =Source.newBuilder("js", stream, "sample.js") .build(); assert "sample.js".equals(source.getName());
public static Source.LiteralBuilder newBuilder(Source source)
source - the source to inherit the properties frompublic static String findLanguage(TruffleFile file) throws IOException
file. This method is a shortcut for:
String mimeType = Source.findMimeType(file);
return mimeType != null ? Source.findLanguage(mimeType) : null;
file - the file to find the first language forIOException - if an error opening the file occurred.Source.findMimeType(URL),
Source.findLanguage(String)public static String findLanguage(URL url) throws IOException
URL. This method is a shortcut for:
String mimeType = Source.findMimeType(url);
return mimeType != null ? Source.findLanguage(mimeType) : null;
url - the url to find the first language forIOException - if an error opening the url occurred.Source.findMimeType(URL),
Source.findLanguage(String)public static String findMimeType(TruffleFile file) throws IOException
null if no MIME type could be
resolved. Typically the MIME type is identified using the file extension and/or using its
contents. Probing the MIME type of an TruffleFile may require to opening the file.IOException - if an error opening the file occurred.SecurityException - if the used filesystem denied file reading.Source.findLanguage(TruffleFile)public static String findMimeType(URL url) throws IOException
null if no MIME type could be
resolved. Typically the MIME type is identified using the file extension, connection
meta-data and/or using it contents. Returns null if the language of the given
file could not be detected. Probing the language of an URL may require to open a new URL
connection.IOException - if an error opening the url occurred.SecurityException - if the used filesystem denied file reading.Source.findLanguage(URL)public static String findLanguage(String mimeType)
null if no language was found that supports a given MIME type. The
languages are queried in the same order as returned by Engine.getLanguages().