Package io.token
Class TokenStream
java.lang.Object
io.token.TokenStream
A handy structure to perform the lexical analysis on a given
String
.
Uses the Scanner
class and TokenReader
s reading character by character to tokenize an input.- Since:
- 08.12.2021
- Author:
- Juyas
- See Also:
-
Constructor Summary
ConstructorDescriptionTokenStream
(String input) Create a newTokenStream
for a given inputString
. -
Method Summary
Modifier and TypeMethodDescriptione
(TokenReader reader) Used for {@link this#eatConditionally(TokenReader, Predicate)} to create a predicate.eat
(TokenReader reader) Try to read a token using the givenTokenReader
based on the current position of the internalScanner
.eatConditionally
(TokenReader reader, Predicate<Token> previousToken) Try to read a token if the previous token in history matches the given condition.eatHistorically
(TokenReader reader, Predicate<LinkedList<Token>> previousToken) Try to read a token if all previous tokens in history match the given condition.static Predicate<LinkedList<Token>>
eh
(TokenReader... readers) Used for {@link this#eatHistorically(TokenReader, Predicate)} to create a predicate.The current token history of this stream.boolean
If there is any token left to read.ne
(TokenReader reader) Used for {@link this#eatConditionally(TokenReader, Predicate)} to create a predicate.
-
Constructor Details
-
TokenStream
Create a newTokenStream
for a given inputString
. Also creates an empty token history list {@link this#getHistory()} and theScanner
with the single character delimiter.- Parameters:
input
- the inputString
-
-
Method Details
-
eat
Try to read a token using the givenTokenReader
based on the current position of the internalScanner
. This method will read and add a new token to the history, if theTokenReader.canRead(Scanner)
method returns true.- Parameters:
reader
- the reader to read the next token- Returns:
- this stream
-
eatConditionally
Try to read a token if the previous token in history matches the given condition.- Parameters:
reader
- the reader to read the next tokenpreviousToken
- the condition for the previous token- Returns:
- this stream
-
eatHistorically
Try to read a token if all previous tokens in history match the given condition.- Parameters:
reader
- the reader to read the next tokenpreviousToken
- the conditions for the previous tokens- Returns:
- this stream
-
getHistory
The current token history of this stream. Might produce the result at the end of calculation.- Returns:
- the current token history list.
-
isOffering
public boolean isOffering()If there is any token left to read. Is identical toScanner.hasNext()
for the internal scanner.- Returns:
- whether there are characters/tokens left to be read
-
eh
Used for {@link this#eatHistorically(TokenReader, Predicate)} to create a predicate. Calling this method with A,B,C would result in a predicate that matches if the recent history ends with A,B,C. Where C is the most recent entry - therefore the given tokens get matched with the end of the history list.- Parameters:
readers
- theTokenReader
s containing the types to match in history- Returns:
- whether the expected history is matched - as predicate
-
e
Used for {@link this#eatConditionally(TokenReader, Predicate)} to create a predicate. The predicate will match the given readers type with the last token in the history list. This means the predicate passes, if the latest/most recent eaten token is equal to the given one.- Parameters:
reader
- the reader containing the type to match in history- Returns:
- whether the most recent token matches the given one - as predicate
-
ne
Used for {@link this#eatConditionally(TokenReader, Predicate)} to create a predicate. The predicate will NOT match the given readers type with the last token in the history list. This means the predicate passes, if the latest/most recent eaten token is NOT equal to the given one. This method is mostly the opposite to {@link this#e(TokenReader)}, although this predicate also passes, if there is no previous token at all.- Parameters:
reader
- the reader containing the type to match in history- Returns:
- whether the most recent token does not match the given one - as predicate
-