java.lang.Object
io.github.mmm.scanner.AbstractCharStreamScanner
io.github.mmm.scanner.CharReaderScanner
- All Implemented Interfaces:
TextFormatProcessor,TextPosition,CharStreamScanner,AutoCloseable
Implementation of
CharStreamScanner that adapts a Reader to read and parse textual data.-
Field Summary
FieldsFields inherited from class io.github.mmm.scanner.AbstractCharStreamScanner
buffer, column, limit, line, offsetFields inherited from interface io.github.mmm.scanner.CharStreamScanner
EOS -
Constructor Summary
ConstructorsConstructorDescriptionThe constructor.CharReaderScanner(int capacity) The constructor.CharReaderScanner(int capacity, TextFormatMessageHandler messageHandler) The constructor.CharReaderScanner(int capacity, TextFormatMessageHandler messageHandler, Reader reader) The constructor.CharReaderScanner(int capacity, Reader reader) The constructor.CharReaderScanner(TextFormatMessageHandler messageHandler) The constructor.CharReaderScanner(TextFormatMessageHandler messageHandler, Reader reader) The constructor.CharReaderScanner(Reader reader) The constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()booleanThis method determines if the givenexpectedStringis completely present at the current position.protected booleanexpectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip) protected booleanfill()Consumes all remaining data in the internalAbstractCharStreamScanner.bufferand fills theAbstractCharStreamScanner.bufferwith further data (if available from underlying source such as a stream/reader).intprotected booleanisEob()booleanisEos()booleanisEot()ATTENTION:charpeek(int lookaheadOffset) LikeCharStreamScanner.peek()but with further lookahead.
Attention:
This method requires lookahead.peekString(int count) peekWhile(CharFilter filter, int maxLen) protected voidreset()Resets the internal state.voidprotected voidverifyLookahead(int length) Methods inherited from class io.github.mmm.scanner.AbstractCharStreamScanner
addMessage, append, builder, eot, expectOne, expectOne, expectUnsafe, getAppended, getBufferParsed, getColumn, getLine, getMessages, handleChar, hasNext, next, peek, read, read, readDigit, readDouble, readFloat, readInteger, readJavaCharLiteral, readJavaNumberLiteral, readJavaStringLiteral, readLine, readLong, readNumber, readUnsignedLong, readUntil, readUntil, readUntil, readUntil, readUntil, readUntil, readWhile, require, requireMin, setOffset, skip, skipNewLine, skipOver, skipUntil, skipUntil, skipWhile, skipWhile, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.github.mmm.scanner.CharStreamScanner
expect, expect, expect, expect, expectOne, expectUnsafe, peekUntil, readBoolean, readBoolean, readBoolean, readDigit, readDouble, readFloat, readInteger, readJavaCharLiteral, readJavaStringLiteral, readLine, readLong, readUntil, readUntil, readUntil, readWhile, require, require, requireOne, requireOne, requireOneOrMore, skipOver, skipOver, skipWhile, skipWhileAndPeek, skipWhileAndPeekMethods inherited from interface io.github.mmm.base.text.TextFormatProcessor
addError, addInfo, addMessage, addWarning
-
Field Details
-
position
protected int position- See Also:
-
-
Constructor Details
-
CharReaderScanner
public CharReaderScanner()The constructor. -
CharReaderScanner
The constructor.- Parameters:
messageHandler- theTextFormatMessageHandler.
-
CharReaderScanner
The constructor.- Parameters:
reader- the (initial)Reader.
-
CharReaderScanner
The constructor.- Parameters:
messageHandler- theTextFormatMessageHandler.reader- the (initial)Reader.
-
CharReaderScanner
public CharReaderScanner(int capacity) The constructor.- Parameters:
capacity- the buffer capacity.
-
CharReaderScanner
The constructor.- Parameters:
capacity- the buffer capacity.messageHandler- theTextFormatMessageHandler.
-
CharReaderScanner
The constructor.- Parameters:
capacity- the buffer capacity.reader- the (initial)Reader.
-
CharReaderScanner
The constructor.- Parameters:
capacity- the buffer capacity.messageHandler- theTextFormatMessageHandler.reader- the (initial)Reader.
-
-
Method Details
-
getPosition
public int getPosition()- Returns:
- the position in the sequence to scan or in other words the number of characters that have been read. Will
initially be
0. Please note that this API is designed for scanning textual content (for parsers). Therefore we consider 2.1 terabyte as a suitablelimit.
-
peek
public char peek(int lookaheadOffset) Description copied from interface:CharStreamScannerLikeCharStreamScanner.peek()but with further lookahead.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the givenlookaheadOffsetshall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
lookaheadOffset- the lookahead offset. If0this method will behave likeCharStreamScanner.peek(). In case of1it will return the character after the next one and so forth.- Returns:
- the
peekedcharacter at the givenlookaheadOffsetorCharStreamScanner.EOSif no such character exists.
-
peekString
Description copied from interface:CharStreamScannerThis method peeks the number ofnext charactersgiven bycountand returns them asString. If there are less charactersavailablethe returnedStringwill be shorter thancountand only contain the available characters. UnlikeCharStreamScanner.read(int)this method does notconsumethe characters and will therefore not change the state of this scanner.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the givencountshall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
count- is the number of characters to peek. You may useInteger.MAX_VALUEto peek until the end of text (EOT) if the data-size is suitable.- Returns:
- a string with the given number of characters or all available characters if less than
count. Will be the empty string if no character isavailableat all.
-
peekWhile
- Parameters:
filter- theCharFilteracceptingonly the characters to peek.maxLen- the maximum number of characters to peek (to get as lookahead without modifying this stream).- Returns:
- a
Stringwith thepeekedcharacters of the givenmaxLenor less if a character was hit that is notacceptedby the givenfilteror the end-of-text has been reached before. The state of this stream remains unchanged. - See Also:
-
getBufferToParse
- Specified by:
getBufferToParsein interfaceCharStreamScanner- Overrides:
getBufferToParsein classAbstractCharStreamScanner- Returns:
- the
Stringwith the characters that have not yet been parsed but are available in the underlying buffer. May be used for debugging or error messages.
-
setReader
- Parameters:
reader- the newReaderto set. May benullto entirely clear this buffer.
-
fill
protected boolean fill()Description copied from class:AbstractCharStreamScannerConsumes all remaining data in the internalAbstractCharStreamScanner.bufferand fills theAbstractCharStreamScanner.bufferwith further data (if available from underlying source such as a stream/reader). If theend of the streamhas not been reached, all buffers should be filled now.- Overrides:
fillin classAbstractCharStreamScanner- Returns:
trueif data was filled,falseifEOS.
-
close
public void close() -
isEos
public boolean isEos()- Overrides:
isEosin classAbstractCharStreamScanner- Returns:
trueif the end of stream (EOS) has been reached,falseotherwise. Iftrue(EOS) the internal buffer contains the entire rest of the data to scan in memory. If then also all data is consumed from the buffer,EOThas been reached. For instances of that are not backed by an underlying stream of data (likeCharSequenceScanner) this method will always returntrue.
-
isEob
protected boolean isEob()- Overrides:
isEobin classAbstractCharStreamScanner- Returns:
trueif end of buffer (EOB) or in other words no data is available after the currentAbstractCharStreamScanner.buffer,falseotherwise (e.g. if notEOS).
-
isEot
public boolean isEot()Description copied from class:AbstractCharStreamScannerATTENTION:- Overrides:
isEotin classAbstractCharStreamScanner- Returns:
trueif end of text (EOT) is known to have been reached,falseotherwise. The returned result will be almost the same as!but this method will not modify the state of this scanner (read additional data, modify buffers, etc.). However, if the underlying stream is already consumed without returningAbstractCharStreamScanner.hasNext()-1to signalEOSthis method may returnfalseeven though the next call ofAbstractCharStreamScanner.hasNext()may also returnfalse.
-
expect
Description copied from interface:CharStreamScannerThis method determines if the givenexpectedStringis completely present at the current position. It will onlyconsumecharacters and change the state iflookaheadisfalseand theexpectedStringwas found (entirely).
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) thelengthof the expectedStringshall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
expected- the expectedStringto search for.ignoreCase- - iftruethe case of the characters is ignored when compared,falseotherwise.lookahead- - iftruethe state of the scanner remains unchanged even if the expectedStringhas been found,falseotherwise (expectedStringis consumed on match).off- the number of characters that have already beenpeekedand after which the givenStringis expected. Will typically be0. Iflookaheadisfalseand the expectedStringwas found these characters will beskippedtogether with the expectedString.- Returns:
trueif theexpectedstring was successfully found,falseotherwise.
-
verifyLookahead
protected void verifyLookahead(int length) - Overrides:
verifyLookaheadin classAbstractCharStreamScanner- Parameters:
length- the number of characters for lookahead (match without consuming). May fail if thelengthexceeds the buffer size.
-
expectRestWithLookahead
protected boolean expectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip) - Specified by:
expectRestWithLookaheadin classAbstractCharStreamScanner- Parameters:
stopChars- the stopStringaschar[]. IfignoreCaseistruein lower case.ignoreCase- -trueto (also) compare chars inlower case,falseotherwise.appender- an optional lambda torunbefore shifting buffers to append data.skip- -trueto update buffers and offset such that on success this scanner points after the expected stopString,falseotherwise (to not consume any character in any case).- Returns:
trueif the stopString(stopChars) was found and consumed,falseotherwise (and no data consumed).- See Also:
-
reset
protected void reset()Description copied from class:AbstractCharStreamScannerResets the internal state.- Overrides:
resetin classAbstractCharStreamScanner
-