public abstract class Source
extends java.lang.Object
fromText(CharSequence, String)
fromFileName(String)
fromFileName(String, boolean)
fromURL(URL, String)
fromReader(Reader, String)
asPseudoFile(CharSequence, String)
File cache:
getInputStream()
or
getReader()
will be provided from the cache.Modifier and Type | Method and Description |
---|---|
static Source |
asPseudoFile(java.lang.CharSequence chars,
java.lang.String pseudoFileName)
Creates a source from literal text, but which acts as a file and can be retrieved by name
(unlike other literal sources); intended for testing.
|
protected void |
checkRange(int charIndex,
int length) |
LineLocation |
createLineLocation(int lineNumber)
Creates a representation of a line number in this source, suitable for use as a hash table
key with equality defined to mean equivalent location.
|
SourceSection |
createSection(java.lang.String identifier,
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(java.lang.String identifier,
int charIndex,
int length)
Creates a representation of a contiguous region of text in the source.
|
SourceSection |
createSection(java.lang.String identifier,
int startLine,
int startColumn,
int length)
Creates a representation of a contiguous region of text in the source.
|
SourceSection |
createSection(java.lang.String identifier,
int startLine,
int startColumn,
int charIndex,
int length)
Creates a representation of a contiguous region of text in the source.
|
protected com.oracle.truffle.api.source.Source.TextMap |
createTextMap() |
static Source |
fromBytes(byte[] bytes,
int byteIndex,
int length,
java.lang.String description,
BytesDecoder decoder)
Creates a source from raw bytes.
|
static Source |
fromBytes(byte[] bytes,
java.lang.String description,
BytesDecoder decoder)
Creates a source from raw bytes.
|
static Source |
fromFileName(java.lang.String fileName)
Gets the canonical representation of a source file, whose contents will be read lazily and
then cached.
|
static Source |
fromFileName(java.lang.String fileName,
boolean reset)
Gets the canonical representation of a source file, whose contents will be read lazily and
then cached.
|
static Source |
fromReader(java.io.Reader reader,
java.lang.String description)
Creates a source whose contents will be read immediately and cached.
|
static Source |
fromText(java.lang.CharSequence chars,
java.lang.String description)
Creates a non-canonical source from literal text.
|
static Source |
fromURL(java.net.URL url,
java.lang.String description)
Creates a source whose contents will be read immediately from a URL and cached.
|
abstract java.lang.String |
getCode()
Return the complete text of the code.
|
java.lang.String |
getCode(int lineNumber)
Gets the text (not including a possible terminating newline) in a (1-based) numbered line.
|
java.lang.String |
getCode(int charIndex,
int charLength) |
int |
getColumnNumber(int offset)
Given a 0-based character offset, return the 1-based number of the column at the position.
|
java.io.InputStream |
getInputStream()
Access to the source contents.
|
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 java.lang.String |
getName()
Returns the name of this resource holding a guest language program.
|
abstract java.lang.String |
getPath()
The normalized, canonical name if the source is a file.
|
abstract java.io.Reader |
getReader()
Access to the source contents.
|
abstract java.lang.String |
getShortName()
Returns a short version of the name of the resource holding a guest language program (as
described in @getName).
|
abstract java.net.URL |
getURL()
The URL if the source is retrieved via URL.
|
protected abstract void |
reset() |
static void |
setFileCaching(boolean enabled)
Enables/disables caching of file contents, disabled by default.
|
public static Source fromFileName(java.lang.String fileName, boolean reset) throws java.io.IOException
fileName
- namereset
- forces any existing Source
cache to be cleared, forcing a re-readjava.io.IOException
- if the file can not be readpublic static Source fromFileName(java.lang.String fileName) throws java.io.IOException
fileName
- namejava.io.IOException
- if the file can not be readpublic static Source fromText(java.lang.CharSequence chars, java.lang.String description)
asPseudoFile(CharSequence, String)
.chars
- textual source codedescription
- a note about the origin, for error messages and debuggingpublic static Source fromURL(java.net.URL url, java.lang.String description) throws java.io.IOException
url
- description
- identifies the origin, possibly useful for debuggingjava.io.IOException
- if reading failspublic static Source fromReader(java.io.Reader reader, java.lang.String description) throws java.io.IOException
reader
- description
- a note about the origin, possibly useful for debuggingjava.io.IOException
- if reading failspublic static Source fromBytes(byte[] bytes, java.lang.String description, BytesDecoder decoder)
bytes
- the raw bytes of the sourcedescription
- a note about the origin, possibly useful for debuggingdecoder
- how to decode the bytes into Java stringspublic static Source fromBytes(byte[] bytes, int byteIndex, int length, java.lang.String description, BytesDecoder decoder)
bytes
- the raw bytes of the sourcebyteIndex
- where the string starts in the byte arraylength
- the length of the string in the byte arraydescription
- a note about the origin, possibly useful for debuggingdecoder
- how to decode the bytes into Java stringspublic static Source asPseudoFile(java.lang.CharSequence chars, java.lang.String pseudoFileName)
chars
- textual source codepseudoFileName
- string to use for indexing/lookuppublic static void setFileCaching(boolean enabled)
protected abstract void reset()
public abstract java.lang.String getName()
public abstract java.lang.String getShortName()
public abstract java.lang.String getPath()
public abstract java.net.URL getURL()
public abstract java.io.Reader getReader()
public final java.io.InputStream getInputStream()
public abstract java.lang.String getCode()
public java.lang.String getCode(int charIndex, int charLength)
public final java.lang.String getCode(int lineNumber)
public final int getLineCount()
public final int getLineNumber(int offset) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the offset is outside the text contentspublic final int getColumnNumber(int offset) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if the offset is outside the text contentspublic final int getLineStartOffset(int lineNumber) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if there is no such line in the textpublic final int getLineLength(int lineNumber) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
- if there is no such line in the textpublic final SourceSection createSection(java.lang.String identifier, int startLine, int startColumn, int charIndex, int length)
This method performs no checks on the validity of the arguments.
The resulting representation defines hash/equality around equivalent location, presuming that
Source
representations are canonical.
identifier
- terse description of the regionstartLine
- 1-based line number of the first character in the sectionstartColumn
- 1-based column number of the first character in the sectioncharIndex
- the 0-based index of the first character of the sectionlength
- the number of characters in the sectionpublic final SourceSection createSection(java.lang.String identifier, int startLine, int startColumn, int length)
charIndex
value by building a map of lines in the source.
Checks the position arguments for consistency with the source.
The resulting representation defines hash/equality around equivalent location, presuming that
Source
representations are canonical.
identifier
- terse description of the regionstartLine
- 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 sectionjava.lang.IllegalArgumentException
- if arguments are outside the text of the sourcejava.lang.IllegalStateException
- if the source is one of the "null" instancespublic final SourceSection createSection(java.lang.String identifier, int charIndex, int length) throws java.lang.IllegalArgumentException
(startLine, startColumn)
values by building a map of lines in
the source.
Checks the position arguments for consistency with the source.
The resulting representation defines hash/equality around equivalent location, presuming that
Source
representations are canonical.
identifier
- terse description of the regioncharIndex
- 0-based position of the first character in the sectionlength
- the number of characters in the sectionjava.lang.IllegalArgumentException
- if either of the arguments are outside the text of the
sourcejava.lang.IllegalStateException
- if the source is one of the "null" instancesprotected void checkRange(int charIndex, int length)
public final SourceSection createSection(java.lang.String identifier, int lineNumber)
identifier
- terse description of the linelineNumber
- 1-based line number of the first character in the sectionjava.lang.IllegalArgumentException
- if the line does not exist the sourcejava.lang.IllegalStateException
- if the source is one of the "null" instancespublic final LineLocation createLineLocation(int lineNumber)
lineNumber
- a 1-based line number in this sourceprotected com.oracle.truffle.api.source.Source.TextMap createTextMap()