public class TokenStreamParser extends java.lang.Object
Constructor and Description |
---|
TokenStreamParser(java.util.List<IToken> tokens)
Constructor.
|
TokenStreamParser(java.util.List<IToken> tokens,
int startPosition)
Constructor.
|
TokenStreamParser(java.util.List<IToken> tokens,
java.util.Set<ETokenType> filteredTokens)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
java.util.List<IToken> |
consumeAlternating(java.util.Set<ETokenType> typeSet1,
java.util.Set<ETokenType> typeSet2)
Consumes tokens as long as they alternate between the two type sets.
|
java.util.List<java.lang.String> |
consumeAnyExcept(java.util.Set<ETokenType> types)
Consumes all sequential tokens that are not of the given types and returns
their texts.
|
java.util.List<IToken> |
consumeAnyOf(java.util.Set<ETokenType> matchTypes)
Consumes all sequential tokens of the given types and returns the consumed
tokens.
|
java.util.Optional<IToken> |
consumeOneOrZeroOf(ETokenType type)
Consumes one token of the given type and returns the token in an
Optional or Optional.empty() if the current token is not
of any of those types. |
java.util.Optional<IToken> |
consumeOneOrZeroOf(java.util.Set<ETokenType> types)
Consumes one token of the given type and returns the token in an
Optional or Optional.empty() if the current token is not
of any of those types. |
boolean |
continuesWithSequence(ETokenType... tokenTypes)
Returns whether the next tokens correspond to the given sequence
|
java.lang.String |
currentText()
Returns the text of the current token or
null if all tokens have
been consumed. |
IToken |
currentToken()
Returns the current token or null iff
isDone() . |
ETokenType |
currentType()
Returns the type of the current token or
null if all tokens have
been consumed. |
int |
getConsumedTokenCount()
Returns the number of tokens consumed so far.
|
ELanguage |
getTokenLanguage()
Returns the language of the given
IncludedTokens (assuming all have
the same language). |
java.util.List<IToken> |
getTokens() |
boolean |
isAnyOf(java.util.Set<ETokenType> types)
Returns
true if the current token has any of the given types. |
boolean |
isDone()
Returns
true if all tokens have been consumed and parsing is
done. |
boolean |
previousTokenTypeIs(ETokenType type)
Returns whether the token before the current parsing position has the given
type.
|
void |
resetToPosition(int newPosition)
Reset parser position to the given token index.
|
Pair<java.lang.Boolean,java.util.List<IToken>> |
skipBalanced(ETokenType openingType,
ETokenType closingType,
java.util.Set<ETokenType> allowedInnerTypes)
Skips balanced braces as long as they only contain the allowed inner types.
|
Pair<java.lang.Boolean,java.util.List<IToken>> |
skipBalancedParentheses()
Syntactic sugar for skipping balanced LPAREN and RPAREN.
|
void |
skipToEnd()
Skips any remaining tokens.
|
boolean |
skipToFirstTopLevel(java.util.EnumSet<ETokenType> searchTypes,
java.util.List<ETokenType> openingTypes,
java.util.List<ETokenType> closingTypes)
Skips to the first occurrence of one of the given
ETokenType s,
ignoring tokens nested in pairs of the given opening/closing
ETokenType s. |
void |
skipToSequence(ETokenType... tokenTypes)
Skips to the beginning of the specified sequence or to the end if the
sequence was not found
|
java.lang.String |
toString() |
<T> java.util.Optional<T> |
tryConsume(java.util.function.Function<TokenStreamParser,java.util.Optional<T>> consumer)
Tries to apply the given token consumer to this
TokenStreamParser . |
public TokenStreamParser(java.util.List<IToken> tokens, java.util.Set<ETokenType> filteredTokens)
tokens
- The tokens to parse.filteredTokens
- A set of token types. All tokens of these types are filtered out
before parsing, i.e. they are simply skipped.public TokenStreamParser(java.util.List<IToken> tokens)
public TokenStreamParser(java.util.List<IToken> tokens, int startPosition)
public java.util.List<IToken> consumeAnyOf(java.util.Set<ETokenType> matchTypes)
public java.util.Optional<IToken> consumeOneOrZeroOf(java.util.Set<ETokenType> types)
Optional
or Optional.empty()
if the current token is not
of any of those types. This method only advances the parser if the current
token has one of the given types.public java.util.Optional<IToken> consumeOneOrZeroOf(ETokenType type)
Optional
or Optional.empty()
if the current token is not
of any of those types. This method only advances the parser if the current
token has one of the given types.public java.util.List<java.lang.String> consumeAnyExcept(java.util.Set<ETokenType> types)
public boolean skipToFirstTopLevel(java.util.EnumSet<ETokenType> searchTypes, java.util.List<ETokenType> openingTypes, java.util.List<ETokenType> closingTypes)
ETokenType
s,
ignoring tokens nested in pairs of the given opening/closing
ETokenType
s.
If no top-level occurrence of one of the search types is found, the parser
position is left unchanged and false
is returned. If an
occurrence of one of the search types is found, the parser is left pointing
at it and true
is returned.true
if the skipping succeeded and false
if
no top-level occurrence of the searchTypes could be found.public Pair<java.lang.Boolean,java.util.List<IToken>> skipBalanced(ETokenType openingType, ETokenType closingType, java.util.Set<ETokenType> allowedInnerTypes)
true
if did not encounter a disallowed inner
type). The second element is the list of consumed IToken
s.public Pair<java.lang.Boolean,java.util.List<IToken>> skipBalancedParentheses()
skipBalanced(ETokenType, ETokenType, Set)
with
ETokenType.LPAREN
and ETokenType.RPAREN
.public java.util.List<IToken> consumeAlternating(java.util.Set<ETokenType> typeSet1, java.util.Set<ETokenType> typeSet2)
public boolean isAnyOf(java.util.Set<ETokenType> types)
true
if the current token has any of the given types.public boolean isDone()
true
if all tokens have been consumed and parsing is
done.public java.lang.String currentText()
null
if all tokens have
been consumed.public ETokenType currentType()
null
if all tokens have
been consumed.public boolean continuesWithSequence(ETokenType... tokenTypes)
public void skipToSequence(ETokenType... tokenTypes)
public int getConsumedTokenCount()
public void resetToPosition(int newPosition)
getConsumedTokenCount()
.public java.lang.String toString()
toString
in class java.lang.Object
public <T> java.util.Optional<T> tryConsume(java.util.function.Function<TokenStreamParser,java.util.Optional<T>> consumer)
TokenStreamParser
. If
the consumer returns Optional.empty()
, resets the parser to the
current position and returns Optional.empty()
. Otherwise, returns the
return value of the consumer.public ELanguage getTokenLanguage()
IncludedTokens
(assuming all have
the same language).public void skipToEnd()
isDone()
will return
true
.public IToken currentToken()
isDone()
.public boolean previousTokenTypeIs(ETokenType type)