Package org.apache.flink.cdc.common.text
Class TokenStream
- java.lang.Object
-
- org.apache.flink.cdc.common.text.TokenStream
-
@NotThreadSafe public class TokenStream extends Object
A stream of tokens that can be consumed by a parser. This class is not thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classTokenStream.CaseInsensitiveTokenAn immutableTokenStream.Tokenthat implements matching using case-insensitive logic.classTokenStream.CaseInsensitiveTokenFactoryAn implementation ofTokenStream.Tokensthat createsTokenStream.CaseInsensitiveTokenobjects.protected classTokenStream.CaseSensitiveTokenAn immutableTokenStream.Tokenthat implements matching using case-sensitive logic.classTokenStream.CaseSensitiveTokenFactoryAn implementation ofTokenStream.Tokensthat createsTokenStream.CaseSensitiveTokenobjects.static classTokenStream.CharacterArrayStreamAn implementation ofTokenStream.CharacterStreamthat works with a single character array.static interfaceTokenStream.CharacterStreamInterface used by aTokenStream.Tokenizerto iterate through the characters in the content input to theTokenStream.static classTokenStream.MarkerAn opaque marker for a position within the token stream.static interfaceTokenStream.TokenThe interface defining a token, which references the characters in the actual input character stream.protected classTokenStream.TokenFactoryAn implementation ofTokenStream.Tokensthat createsTokenStream.CaseSensitiveTokenobjects.static interfaceTokenStream.TokenizerInterface for a Tokenizer component responsible for processing the characters in aTokenStream.CharacterStreamand constructing the appropriateTokenStream.Tokenobjects.static interfaceTokenStream.TokensA factory for Token objects, used by aTokenStream.Tokenizerto create tokens in the correct order.
-
Field Summary
Fields Modifier and Type Field Description static intANY_TYPEstatic StringANY_VALUEprotected StringinputString
-
Constructor Summary
Constructors Constructor Description TokenStream(String content, TokenStream.Tokenizer tokenizer, boolean caseSensitive)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringconsume()Return the value of this token and move to the next token.booleanhasNext()Determine if this stream has another token to be consumed.protected List<TokenStream.Token>initializeTokens(List<TokenStream.Token> tokens)Method to allow subclasses to pre-process the set of tokens and return the correct tokens to use.TokenStream.Markermark()Obtain a marker that records the current position so that the stream can berewind(Marker)back to the mark even after having been advanced beyond the mark.booleanmatches(int type, String expected)Determine if the current token matches the expected type and a value.booleanmatches(String expected)Determine if the current token matches the expected value.booleanmatchesAnyOf(String[] options)Determine if the next token matches one of the supplied values.Stringpeek()booleanrewind(TokenStream.Marker marker)Reset the stream back to the position described by the supplied marker.TokenStreamstart()Begin the token stream, including (if required) the tokenization of the input content.protected voidthrowNoMoreContent()StringtoString()
-
-
-
Field Detail
-
ANY_VALUE
public static final String ANY_VALUE
- See Also:
- Constant Field Values
-
ANY_TYPE
public static final int ANY_TYPE
- See Also:
- Constant Field Values
-
inputString
protected final String inputString
-
-
Constructor Detail
-
TokenStream
public TokenStream(String content, TokenStream.Tokenizer tokenizer, boolean caseSensitive)
-
-
Method Detail
-
start
public TokenStream start() throws ParsingException
Begin the token stream, including (if required) the tokenization of the input content.- Returns:
- this object for easy method chaining; never null
- Throws:
ParsingException- if an error occurs during tokenization of the content
-
initializeTokens
protected List<TokenStream.Token> initializeTokens(List<TokenStream.Token> tokens)
Method to allow subclasses to pre-process the set of tokens and return the correct tokens to use. The default behavior is to simply return the supplied tokens.- Parameters:
tokens- the tokens- Returns:
- list of tokens.
-
mark
public TokenStream.Marker mark()
Obtain a marker that records the current position so that the stream can berewind(Marker)back to the mark even after having been advanced beyond the mark.- Returns:
- the marker; never null
- Throws:
IllegalStateException- if this method was called before the stream wasstartedNoSuchElementException- if there are no more tokens
-
rewind
public boolean rewind(TokenStream.Marker marker)
Reset the stream back to the position described by the supplied marker. This method does nothing if the mark is invalid. For example, it is not possible to advance the token stream beyond the current position.- Parameters:
marker- the marker- Returns:
- true if the token stream was reset, or false if the marker was invalid
-
consume
public String consume() throws ParsingException, IllegalStateException
Return the value of this token and move to the next token.- Returns:
- the value of the current token
- Throws:
ParsingException- if there is no such token to consumeIllegalStateException- if this method was called before the stream wasstarted
-
throwNoMoreContent
protected void throwNoMoreContent() throws ParsingException- Throws:
ParsingException
-
peek
public String peek() throws IllegalStateException
- Throws:
IllegalStateException
-
matches
public boolean matches(String expected) throws IllegalStateException
Determine if the current token matches the expected value.The
ANY_VALUEconstant can be used as a wildcard.- Parameters:
expected- the expected value of the current token- Returns:
- true if the current token did match, or false if the current token did not match
- Throws:
IllegalStateException- if this method was called before the stream wasstarted
-
matches
public boolean matches(int type, String expected) throws IllegalStateExceptionDetermine if the current token matches the expected type and a value.The
ANY_VALUEconstant can be used as a wildcard.- Parameters:
type- the expected type of the curent tokenexpected- the expected value of the current token- Returns:
- true if the current token did match, or false if the current token did not match
- Throws:
IllegalStateException- if this method was called before the stream wasstarted
-
matchesAnyOf
public boolean matchesAnyOf(String[] options) throws IllegalStateException
Determine if the next token matches one of the supplied values.- Parameters:
options- the options for the value of the current token- Returns:
- true if the current token's value did match one of the supplied options, or false otherwise
- Throws:
IllegalStateException- if this method was called before the stream wasstarted
-
hasNext
public boolean hasNext()
Determine if this stream has another token to be consumed.- Returns:
- true if there is another token ready for consumption, or false otherwise
- Throws:
IllegalStateException- if this method was called before the stream wasstarted
-
-