public class Tokenizer
extends java.lang.Object
Reader into a stream of Tokens.| Constructor and Description |
|---|
Tokenizer(LineReader reader) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
accept(int offset,
TokenType type)
Returns true if the token at the given offset from the current is of the given
TokenType. |
boolean |
accept(TokenType... types)
Returns true if the current token is and of the given
TokenTypes. |
void |
end() |
Token |
expect(TokenType... types)
Returns a
Token if it's one of the given types, or throws a CarrotException if it's not. |
CarrotException |
unexpected(java.lang.String msg)
Creates a
CarrotException with the given message, populated with our current state. |
public Tokenizer(LineReader reader) throws CarrotException
CarrotExceptionpublic boolean accept(TokenType... types) throws CarrotException
TokenTypes. You can then use expect(au.com.codeka.carrot.expr.TokenType...) to get
the token (and advance to the next one).types - The TokenType we want to accept.CarrotException - If there's an error parsing the tokens.public boolean accept(int offset,
TokenType type)
throws CarrotException
TokenType. The 0th token
is the current one, the 1st is the after that and so on. This can be used to "look ahead" into the token stream.offset - The offset from "current" that we want to peek. 0 is the current token, 1 is the next and so on.type - The TokenType we want to accept.CarrotException - If there's an error parsing the tokens.@Nullable public Token expect(TokenType... types) throws CarrotException
Token if it's one of the given types, or throws a CarrotException if it's not.types - The TokenTypes we want to accept one of.Token, if it's of the given type.CarrotException - If there's an error parsing the token, or if it's not of the given type.public void end()
throws CarrotException
CarrotException - unless we're at the end of the tokens.public CarrotException unexpected(java.lang.String msg)
CarrotException with the given message, populated with our current state.msg - The message to create the exception with.CarrotException with the given message (presumably because we got an unexpected token).