public abstract class Token<T extends TokenId> extends Object
Token.id()
and a textual body (aka token's image) by Token.text()
.
TokenFactory
.
Note:
Do not create custom extensions of this class - lexers may only return
implementations produced by TokenFactory
.
Creation of any other token implementations will be refused.
Token guarantees stability of the Token.id()
and Token.length()
methods.
The Token.hashCode()
and Token.equals(Object)
methods
use the default implementations from java.lang.Object
.
The two tokens are only equal if they are the same object.
Modifier | Constructor and Description |
---|---|
protected |
Token()
Create token instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object o)
Make sure the default implementation of
equals() is used
and the token can safely be used in maps. |
abstract Object |
getProperty(Object key)
Get extra property of this token.
|
int |
hashCode()
Make sure the default implementation of
hashCode() is used
and the token can safely be used in maps. |
abstract boolean |
hasProperties()
Quickly determine whether this token has any extra properties.
|
abstract T |
id()
Get identification of this token.
|
abstract boolean |
isCustomText()
Check whether
Token.text() returns a custom value that may differ
from the original content of the text input. |
abstract boolean |
isFlyweight()
Checks whether this token instance is used for multiple occurrences
of this token in this or other inputs.
|
boolean |
isRemoved()
Check whether this token is no longer part of the token hierarchy.
|
List<? extends Token<T>> |
joinedParts()
Get all token parts comprising this token ordered from lowest to highest part's offset.
|
Token<T> |
joinToken()
Get a complete token that is joined from multiple parts (this token is one of those parts).
|
abstract int |
length()
Get number of characters in the original text input
that the token spans.
|
abstract int |
offset(TokenHierarchy<?> tokenHierarchy)
Get the offset at which this token is present in the input
or
-1 if this token is flyweight (and therefore does not store offset). |
abstract PartType |
partType()
Check whether this token represents a complete token
or whether it's a particular part of a complete token.
|
abstract CharSequence |
text()
Get text of this token (aka token's image) as a character sequence.
|
protected Token()
IllegalStateException
- if a non-lexer-module-implementation token
is attempted to be created.public abstract T id()
public abstract CharSequence text()
Token.isCustomText()
returns true.
Note for mutable input sources:
This method should only be called
within a readonly (or read-write) transaction
over the underlying input source
(such as javax.swing.text.Document.render()
for Swing documents).
The result returned by this method
is only valid within a readonly (or read-write) transaction
over the input source (method must be re-called
during the next readonly transaction).
null
in case the token was used
for a mutable input and it was removed
from the token list for the given input (but even in such case
the text can be retained in certain cases).
The behavior of equals()
and hashCode()
of the returned character sequence is generally undefined.
The returned character sequence can NOT be compared to another
character sequence by using its equals()
method.
TokenUtilities
contains
utility methods related to token text comparing.
The returned text is just a pointer to the primary source of the data e.g. a swing document. The character data are not duplicated in the tokens.
public abstract boolean isCustomText()
Token.text()
returns a custom value that may differ
from the original content of the text input.
public abstract int length()
Token.text()
.length()
unless Token.isCustomText()
returns true.
null
.public abstract int offset(TokenHierarchy<?> tokenHierarchy)
-1
if this token is flyweight (and therefore does not store offset).
TokenSequence.offset()
is usually preferred over
this method because it returns actual offset even for the flyweight tokens.
TokenSequence.offsetToken()
.
The complexity of the method should generally be constant regardless of the level of the language embedding.
tokenHierarchy
- null
should be passed
(the parameter is reserved for future use when token hierarchy snapshots will be implemented).-1
if this token is flyweight.public abstract boolean isFlyweight()
If the token is flyweight its Token.offset(TokenHierarchy)
returns -1.
public boolean isRemoved()
public abstract PartType partType()
PartType.START
.PartType.COMPLETE
for regular token or other part types
for particular token parts.public Token<T> joinToken()
public List<? extends Token<T>> joinedParts()
public abstract boolean hasProperties()
public abstract Object getProperty(Object key)
key
- non-null key of the property to get.Token.hasProperties()
public final int hashCode()
hashCode()
is used
and the token can safely be used in maps.