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
Fields inherited from class io.github.mmm.scanner.AbstractCharStreamScanner
buffer, column, limit, line, offset
Fields inherited from interface io.github.mmm.scanner.CharStreamScanner
EOS
-
Constructor Summary
ConstructorDescriptionThe 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 TypeMethodDescriptionvoid
close()
boolean
This method determines if the givenexpected
String
is completely present at the current position.protected boolean
expectRestWithLookahead
(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip) protected boolean
fill()
Consumes all remaining data in the internalAbstractCharStreamScanner.buffer
and fills theAbstractCharStreamScanner.buffer
with further data (if available from underlying source such as a stream/reader).int
protected boolean
isEob()
boolean
isEos()
boolean
isEot()
ATTENTION:char
peek
(int lookaheadOffset) LikeCharStreamScanner.peek()
but with further lookahead.
Attention:
This method requires lookahead.peekString
(int count) peekWhile
(CharFilter filter, int maxLen) protected void
reset()
Resets the internal state.void
protected void
verifyLookahead
(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, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods 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, skipWhileAndPeek
Methods 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:CharStreamScanner
LikeCharStreamScanner.peek()
but with further lookahead.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the givenlookaheadOffset
shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
lookaheadOffset
- the lookahead offset. If0
this method will behave likeCharStreamScanner.peek()
. In case of1
it will return the character after the next one and so forth.- Returns:
- the
peeked
character at the givenlookaheadOffset
orCharStreamScanner.EOS
if no such character exists.
-
peekString
Description copied from interface:CharStreamScanner
This method peeks the number ofnext characters
given bycount
and returns them asString
. If there are less charactersavailable
the returnedString
will be shorter thancount
and only contain the available characters. UnlikeCharStreamScanner.read(int)
this method does notconsume
the 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 givencount
shall 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_VALUE
to 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 isavailable
at all.
-
peekWhile
- Parameters:
filter
- theCharFilter
accepting
only the characters to peek.maxLen
- the maximum number of characters to peek (to get as lookahead without modifying this stream).- Returns:
- a
String
with thepeeked
characters of the givenmaxLen
or less if a character was hit that is notaccepted
by the givenfilter
or the end-of-text has been reached before. The state of this stream remains unchanged. - See Also:
-
getBufferToParse
- Specified by:
getBufferToParse
in interfaceCharStreamScanner
- Overrides:
getBufferToParse
in classAbstractCharStreamScanner
- Returns:
- the
String
with 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 newReader
to set. May benull
to entirely clear this buffer.
-
fill
protected boolean fill()Description copied from class:AbstractCharStreamScanner
Consumes all remaining data in the internalAbstractCharStreamScanner.buffer
and fills theAbstractCharStreamScanner.buffer
with further data (if available from underlying source such as a stream/reader). If theend of the stream
has not been reached, all buffers should be filled now.- Overrides:
fill
in classAbstractCharStreamScanner
- Returns:
true
if data was filled,false
ifEOS
.
-
close
public void close() -
isEos
public boolean isEos()- Overrides:
isEos
in classAbstractCharStreamScanner
- Returns:
true
if the end of stream (EOS) has been reached,false
otherwise. 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,EOT
has 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:
isEob
in classAbstractCharStreamScanner
- Returns:
true
if end of buffer (EOB) or in other words no data is available after the currentAbstractCharStreamScanner.buffer
,false
otherwise (e.g. if notEOS
).
-
isEot
public boolean isEot()Description copied from class:AbstractCharStreamScanner
ATTENTION:- Overrides:
isEot
in classAbstractCharStreamScanner
- Returns:
true
if end of text (EOT) is known to have been reached,false
otherwise. 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()
-1
to signalEOS
this method may returnfalse
even though the next call ofAbstractCharStreamScanner.hasNext()
may also returnfalse
.
-
expect
Description copied from interface:CharStreamScanner
This method determines if the givenexpected
String
is completely present at the current position. It will onlyconsume
characters and change the state iflookahead
isfalse
and theexpected
String
was found (entirely).
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) thelength
of the expectedString
shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
expected
- the expectedString
to search for.ignoreCase
- - iftrue
the case of the characters is ignored when compared,false
otherwise.lookahead
- - iftrue
the state of the scanner remains unchanged even if the expectedString
has been found,false
otherwise (expectedString
is consumed on match).off
- the number of characters that have already beenpeeked
and after which the givenString
is expected. Will typically be0
. Iflookahead
isfalse
and the expectedString
was found these characters will beskipped
together with the expectedString
.- Returns:
true
if theexpected
string was successfully found,false
otherwise.
-
verifyLookahead
protected void verifyLookahead(int length) - Overrides:
verifyLookahead
in classAbstractCharStreamScanner
- Parameters:
length
- the number of characters for lookahead (match without consuming). May fail if thelength
exceeds the buffer size.
-
expectRestWithLookahead
protected boolean expectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip) - Specified by:
expectRestWithLookahead
in classAbstractCharStreamScanner
- Parameters:
stopChars
- the stopString
aschar[]
. IfignoreCase
istrue
in lower case.ignoreCase
- -true
to (also) compare chars inlower case
,false
otherwise.appender
- an optional lambda torun
before shifting buffers to append data.skip
- -true
to update buffers and offset such that on success this scanner points after the expected stopString
,false
otherwise (to not consume any character in any case).- Returns:
true
if the stopString
(stopChars
) was found and consumed,false
otherwise (and no data consumed).- See Also:
-
reset
protected void reset()Description copied from class:AbstractCharStreamScanner
Resets the internal state.- Overrides:
reset
in classAbstractCharStreamScanner
-