public class GenericLexer extends AbstractLexer implements GenericTokens
GenericLexer
is a concrete implementation of the Lexer
interface that can be used to lex Language
implementations. This allows you to quickly prototype a language without having to write an optimized language-specific Lexer
implementation.AbstractLexer.DefaultLexerToken
Modifier and Type | Field and Description |
---|---|
protected int |
endOffset
The ending offset of the last token found.
|
protected KeywordTable |
keywordTable
The keyword table of the language.
|
protected Language |
language
The language that this lexer was created for.
|
protected int |
lastToken
The last token found from
lex() operation. |
protected boolean |
skipComments
Whether to skip comments or not.
|
protected int |
startOffset
The starting offset of the last token found.
|
protected boolean |
useLastToken
Whether to use the last token that was found.
|
currentPos, textBuffer
TK_CLOSE_BRACE, TK_FLOAT, TK_IDENTIFIER, TK_INTEGER, TK_KEYWORD, TK_MULTI_COMMENT, TK_OPEN_BRACE, TK_OPERATOR, TK_QUOTES, TK_SINGLE_COMMENT
TK_EOF, TK_NOT_FOUND
Constructor and Description |
---|
GenericLexer(Language language)
Constructs a
GenericLexer for the specified language with a starting position of 0. |
Modifier and Type | Method and Description |
---|---|
void |
backup()
Unlexes the last found token.
|
protected int |
fillLexerToken(LexerToken lexedToken)
Utility routine to fill in the
LexerToken structure with the current token information we have. |
int |
lex(LexerToken lexedToken)
Scans the text buffer at the current position and returns the token that was found.
|
void |
setPosition(int offset)
Sets the current lex (read) position to the given offset in the buffer.
|
void |
setSkipComments(boolean skipComments)
Sets whether the
GenericLexer should generate tokens for comments. |
static java.lang.String |
tokenToString(int token)
Utility routine to map the token to a string representation of the token (for debug printing.)
|
static java.lang.String |
tokenToText(int token)
Utility routine to map the token to the original text (if retrievable) of the token (for debug printing.)
|
createLexerToken, getTextBuffer, setTextBuffer
protected int lastToken
lex()
operation.protected int startOffset
protected int endOffset
protected boolean useLastToken
protected boolean skipComments
protected KeywordTable keywordTable
protected Language language
public GenericLexer(Language language)
GenericLexer
for the specified language with a starting position of 0. Clients must call setTextBuffer()
to initialize the text buffer used for the Lexer. To start lexing from an offset other than 0, call setPosition()
.public void setSkipComments(boolean skipComments)
GenericLexer
should generate tokens for comments.skipComments
- true to ignore comments in token generationpublic int lex(LexerToken lexedToken)
lexedToken
instance passed in to the call.lex
in interface Lexer
lex
in class AbstractLexer
lexedToken
- the instance passed in where token info is storedlexedToken.getToken()
(for convenience)public void backup()
lex()
will return the last token and offset information found.backup
in interface Lexer
backup
in class AbstractLexer
public void setPosition(int offset)
setPosition
in interface Lexer
setPosition
in class AbstractLexer
offset
- the offset for the next lex()
operation.protected int fillLexerToken(LexerToken lexedToken)
LexerToken
structure with the current token information we have. Returns the current token.lexedToken
- the client-specified LexerToken
public static java.lang.String tokenToString(int token)
token
- the token to mappublic static java.lang.String tokenToText(int token)
token
- the token to map