public abstract class LanguageHierarchy<T extends TokenId> extends Object
Language
on SPI level containing
additional information necessary for the lexer infrastructure operation.
public enum MyTokenId implements TokenId { ERROR(null, "error"), IDENTIFIER(null, "identifier"), ABSTRACT("abstract", "keyword"), ... SEMICOLON(";", "separator"), ... private final String fixedText; // Used by lexer for production of flyweight tokens private final String primaryCategory; MyTokenId(String fixedText, String primaryCategory) { this.fixedText = fixedText; this.primaryCategory = primaryCategory; } public String fixedText() { return fixedText; } public String primaryCategory() { return primaryCategory; } private static final Language<MyTokenId> language = new LanguageHierarchy<MyTokenId>() {@Override
protected String mimeType() { return "text/x-my"; }@Override
protected Collection<MyTokenId> createTokenIds() { return EnumSet.allOf(MyTokenId.class); }@Override
protected Lexer<MyTokenId> createLexer(LexerInput input, TokenFactory<MyTokenId> tokenFactory, Object state) { return new MyLexer(input, tokenFactory, state); } }.language(); public static Language<MyTokenId> language() { return language; } }
Constructor and Description |
---|
LanguageHierarchy() |
Modifier and Type | Method and Description |
---|---|
protected abstract Lexer<T> |
createLexer(LexerRestartInfo<T> info)
Create lexer prepared for returning tokens
from subsequent calls to
Lexer.nextToken() . |
protected Map<String,Collection<T>> |
createTokenCategories()
Provide map of token category names to collection of its members.
|
protected abstract Collection<T> |
createTokenIds()
Provide a collection of token ids that comprise the language.
|
protected TokenValidator<T> |
createTokenValidator(T tokenId)
Create token validator for the given token id.
|
protected LanguageEmbedding<?> |
embedding(Token<T> token,
LanguagePath languagePath,
InputAttributes inputAttributes)
Get language embedding (if exists) for a particular token
of the language at this level of language hierarchy.
|
protected EmbeddingPresence |
embeddingPresence(T id)
Determine whether embedding may be present for a token with the given token id.
|
boolean |
equals(Object o)
Enforce default implementation of
equals() . |
int |
hashCode()
Enforce default implementation of
hashCode() . |
protected boolean |
isRetainTokenText(T tokenId)
This feature is currently not supported - Token.text()
will return null for non-flyweight tokens.
|
Language<T> |
language()
Get language constructed for this language hierarchy
based on token ids and token categories provided.
|
protected abstract String |
mimeType()
Gets the mime type of the language constructed from this language hierarchy.
|
static TokenId |
newId(String name,
int ordinal)
Create a default token id instance in case the token ids
are generated (not created by enum class).
|
static TokenId |
newId(String name,
int ordinal,
String primaryCategory)
Create a default token id instance in case the token ids
are generated (not created by enum class).
|
String |
toString() |
public static TokenId newId(String name, int ordinal)
public static TokenId newId(String name, int ordinal, String primaryCategory)
protected abstract Collection<T> createTokenIds()
EnumSet.allOf(MyTokenId.class)
.
This method is only called once by the infrastructure
(when constructing language) so it does
not need to cache its result.
This method is called in synchronized section.
If its implementation would use any synchronization
a care must be taken to prevent deadlocks.
TokenId
instances.protected Map<String,Collection<T>> createTokenCategories()
There is a convention that the category names should only consist of lowercase letters, numbers and hyphens.
protected abstract Lexer<T> createLexer(LexerRestartInfo<T> info)
Lexer.nextToken()
.info
- non-null lexer restart info containing the information
necessary for lexer restarting.protected abstract String mimeType()
LanguagePath.mimePath()
protected LanguageEmbedding<?> embedding(Token<T> token, LanguagePath languagePath, InputAttributes inputAttributes)
token.isFlyweight() == false && token.isCustomText() == false
token
- non-null token for which the language embedding will be resolved.
(Token.length()
== 0)
in case the language infrastructure performs a poll for all embedded
languages for thelanguagePath
- non-null language path at which the language embedding
is being created. It may be used for obtaining appropriate information
from inputAttributes.inputAttributes
- input attributes that could affect the embedding creation.
It may be null if there are no extra attributes.protected EmbeddingPresence embeddingPresence(T id)
EmbeddingPresence.CACHED_FIRST_QUERY
so the LanguageHierarchy.embedding(Token,LanguagePath,InputAttributes)
will be called once (for a first token instance with the given token id)
and if there is no embedding then the embedding creation will not be attempted
for any other token with the same token id. This should be appropriate
for most cases.
id
- non-null token id.protected TokenValidator<T> createTokenValidator(T tokenId)
tokenId
- token id for which the token validator should be returned.protected boolean isRetainTokenText(T tokenId)
Token.text()
will continue
to return the value that it had right before the token's removal.
Retaining text in the tokens has performance and memory implications
and should only be done selectively for tokens where it's desired
(such as identifiers).
The extra performance and memory penalty only happens during
token's removal from the token list for the given input.
Token creation performance and memory consumption during
token's lifetime stay unaffected.
Retaining will only work if the input source is capable of providing the removed text right after the modification has been performed.
public final Language<T> language()
public final int hashCode()
hashCode()
.public final boolean equals(Object o)
equals()
.