public class ParserReader extends Reader
PushbackReader
with a pushback buffer of 1 character.
Code is optimized to work with a 1 character buffer.
Additionally keeps track of current line and column number, and provides the ability to set mark points and capture characters from the previous mark point.
Warning: Not thread safe.
Constructor and Description |
---|
ParserReader(ParserPipe pipe)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
No-op.
|
ParserReader |
delete()
Trims off the last character in the marking buffer.
|
ParserReader |
delete(int count)
Trims off the specified number of last characters in the marking buffer.
|
int |
getColumn()
Returns the current column number position in this reader.
|
int |
getLine()
Returns the current line number position in this reader.
|
ObjectMap |
getLocation(ParserSession session)
Returns the combined location information on both this reader and the session.
|
String |
getMarked()
Returns the contents of the reusable character buffer as a string, and resets the buffer for next usage.
|
String |
getMarked(int offsetStart,
int offsetEnd)
Same as
getMarked() except allows you to specify offsets into the buffer. |
ParserPipe |
getPipe()
Returns the pipe that was passed into the constructor.
|
void |
mark()
Start buffering the calls to read() so that the text can be gathered from the mark point on calling
getFromMarked() . |
int |
peek()
Peeks the next character in the stream.
|
int |
peekSkipWs()
Same as
peek() but skips over any whitespace characters. |
int |
read()
Reads a single character.
|
int |
read(char[] cbuf,
int off,
int len)
Subclasses can override this method to provide additional filtering.
|
String |
read(int num)
Read the specified number of characters off the stream.
|
int |
readCodePoint()
Same as
read() but detects and combines extended unicode characters (i.e. |
int |
readSkipWs()
Same as
read() but skips over any whitespace characters. |
ParserReader |
replace(char c)
Replace the last read character in the buffer with the specified character.
|
ParserReader |
replace(int c,
int offset)
Replaces the last character in the marking buffer with the specified character.
|
ParserReader |
unread()
Pushes the last read character back into the stream.
|
public ParserReader(ParserPipe pipe) throws IOException
pipe
- The parser input.IOException
public final int getLine()
public final int getColumn()
public final int read() throws IOException
Note that this method does NOT process extended unicode characters (i.e. characters above 0x10000), but rather
returns them as two readCodePoint()
to ensure proper handling of extended unicode.
read
in class Reader
IOException
- If a problem occurred trying to read from the reader.public final int readSkipWs() throws IOException
read()
but skips over any whitespace characters.IOException
public final int readCodePoint() throws IOException
read()
but detects and combines extended unicode characters (i.e. characters above 0x10000).IOException
- If a problem occurred trying to read from the reader.public final void mark()
getFromMarked()
.public final int peek() throws IOException
This is equivalent to doing a read()
followed by an unread()
.
IOException
- If a problem occurred trying to read from the reader.public final int peekSkipWs() throws IOException
peek()
but skips over any whitespace characters.
This is equivalent to doing a read()
followed by an unread()
.
IOException
- If a problem occurred trying to read from the reader.public final String read(int num) throws IOException
num
- The number of characters to read.IOException
- If a problem occurred trying to read from the reader.public ParserReader unread() throws IOException
IOException
- If a problem occurred trying to read from the reader.public void close() throws IOException
Input readers are closed in the ParserPipe
class.
close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
IOException
- If a problem occurred trying to read from the reader.public final String getMarked()
public final String getMarked(int offsetStart, int offsetEnd)
getMarked()
except allows you to specify offsets into the buffer.
For example, to return the marked string, but trim the first and last characters, call the following:
getFromMarked(1, -1);
offsetStart
- The offset of the start position.offsetEnd
- The offset of the end position.public final ParserReader delete()
Useful for removing escape characters from sequences.
public final ParserReader delete(int count)
count
- The number of characters to delete.public final ParserReader replace(int c, int offset) throws IOException
offset
must be at least 1
for normal characters, and 2
for extended
unicode characters in order for the replacement to fit into the buffer.
c
- The new character.offset
- The offset.IOException
public final ParserReader replace(char c) throws IOException
c
- The new character.IOException
public int read(char[] cbuf, int off, int len) throws IOException
Default implementation simply calls the same method on the underlying reader.
read
in class Reader
IOException
public ObjectMap getLocation(ParserSession session)
session
- The session object to read the last location on.public final ParserPipe getPipe()
Copyright © 2018 Apache. All rights reserved.