Class LookAheadReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
Reader
which provides look-ahead methods (at
, atSkip
, atWordSkip
, peek
)
and some useful utility methods (readChar()
, atEOF()
, skipWhitespaceAndComments()
).
Makes most parsing tasks easy and efficient, including parsing without a separate lexer and parsing nested grammars.
The capacity is the maximum lookahead (in number of characters).
This must be specified when the LookAheadReader
is created.
LookAheadReader
uses a fast but fixed-size ring buffer.
The intended use is parsing source code from start to finish in one thread. Not thread safe.
- Author:
- sherstDotNet@yahoo.com
-
Field Summary
-
Constructor Summary
ConstructorDescriptionLookAheadReader(Reader reader)
Creates a newLookAheadReader
which wraps the providedReader
with the default lookahead limit of 128 characters.LookAheadReader(Reader reader, int capacity)
Creates a newLookAheadReader
which wraps the providedReader
with specified lookahead limit. -
Method Summary
Modifier and TypeMethodDescriptionboolean
at(char c)
Returnstrue
if the nextchar
that will be read isc
.boolean
at(int n, char c)
Returnstrue
if then
thchar
that will be read isc
, counting from 0 (at(0, 'x')
tests the firstchar
that will be read).boolean
boolean
Returnstrue
if the nextchars
that will be read is#
.boolean
atEOF()
Returnstrue
if all thechars
s have been read.boolean
atEOL()
boolean
atSkip(char c)
If the nextchar
that will be read isc
, returnstrue
and removesc
from the input.boolean
If the nextchar
s that will be read match theString
s
, returnstrue
and removes thechar
s from the input.boolean
If the next characters to be read will be a "comment", returnstrue
and removes the comment from the input.boolean
If the nextchar
that will be read will be a white space character, returnstrue
and removes thechar
from the input.boolean
atSkipWord(String w)
If the "word" that will be read matchesw
(and no other word).boolean
Returnstrue
if the nextchar
that will be read will be a white space character.boolean
Returns true if the "word" that will be read matchesw
(and no other word).void
close()
Closes theLookAheadReader
and the wrappedjava.lang.Reader
.static boolean
isDecimalDigit(char c)
static boolean
isIdentifierChar(char c)
static boolean
isIdentifierStart(char c)
static boolean
isWhitespace(char c)
void
mark(int readAheadLimit)
Marks the present position in the stream.boolean
Returnstrue
if this reader supports themark(int)
operation, which it does, but only up to the maximum capacity of the buffer.char
peek()
Returns the nextchar
that will be read.char
peek(int n)
Returns then
thchar
that will be read, counting from 0 (peek(0)
returns the firstchar
that will be read).int
read()
Reads a single character.int
read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.char
readChar()
Reads a single character.boolean
ready()
Returnstrue
if this reader is ready to be read.void
reset()
Repositions the reader to the mark.long
skip(long n)
Skips characters.void
Skips white space and comments.Methods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
Constructor Details
-
LookAheadReader
Creates a newLookAheadReader
which wraps the providedReader
with the default lookahead limit of 128 characters.- Parameters:
reader
- theReader
to wrap
-
LookAheadReader
Creates a newLookAheadReader
which wraps the providedReader
with specified lookahead limit.- Parameters:
reader
- theReader
to wrapcapacity
- the look ahead limit (number of characters)
-
-
Method Details
-
isDecimalDigit
-
isIdentifierChar
-
isIdentifierStart
-
isWhitespace
-
at
Returnstrue
if the nextchar
that will be read isc
.- Parameters:
c
- thechar
to test for- Returns:
true
if the nextchar
that will be read isc
- Throws:
IOException
-
at
Returnstrue
if then
thchar
that will be read isc
, counting from 0 (at(0, 'x')
tests the firstchar
that will be read).- Parameters:
n
-c
- thechar
to test for- Returns:
true
if then
thchar
that will be read isc
- Throws:
IOException
-
at
- Parameters:
s
- theString
to match- Returns:
true
if the nextchars
s that will be read match theString
s
- Throws:
IOException
-
atComment
Returnstrue
if the nextchars
that will be read is#
.A "comment" starts with
#
and runs until the end of the line.Can easily be overridden to accommodate alternative definitions of "comment".
- Throws:
IOException
-
atEOF
Returnstrue
if all thechars
s have been read.- Returns:
true
if all thechars
s have been read- Throws:
IOException
-
atEOL
- Returns:
true
if all the nextchar
that will be read is '\n' or '\r' or ifatEOF()
- Throws:
IOException
-
atSkip
If the nextchar
that will be read isc
, returnstrue
and removesc
from the input.- Parameters:
c
- thetrue
to test for- Returns:
true
if the nextchar
that will be read isc
- Throws:
IOException
-
atSkip
If the nextchar
s that will be read match theString
s
, returnstrue
and removes thechar
s from the input.- Parameters:
s
- theString
to match- Returns:
true
if the nextchar
that will be read match theString
s
- Throws:
IOException
-
atSkipComment
If the next characters to be read will be a "comment", returnstrue
and removes the comment from the input.A "comment" starts with
#
and runs until the end of the line.Can easily be overridden to accommodate alternative definitions of "comment".
- Throws:
IOException
-
atSkipWhitespaceChar
If the nextchar
that will be read will be a white space character, returnstrue
and removes thechar
from the input.Can easily be overridden to accommodate alternative definitions of "white space".
- Returns:
true
if the nextchar
that will be read will be a white space character- Throws:
IOException
-
atSkipWord
If the "word" that will be read matchesw
(and no other word). returns true and removes the word from the input. Case sensitive.A "word" starts with a letter or underscore and contains only letters, underscores and digits.
Can easily be overridden to accommodate alternative definitions of "word".
- Parameters:
w
- the "word" to match- Returns:
true
if the "word" that will be read matchesw
(and no other word)- Throws:
IOException
-
atWhitespace
Returnstrue
if the nextchar
that will be read will be a white space character.Can easily be overridden to accommodate alternative definitions of "white space".
- Returns:
true
if the nextchar
that will be read will be a white space character- Throws:
IOException
-
atWord
Returns true if the "word" that will be read matchesw
(and no other word). Case sensitive.A "word" starts with a letter or underscore and contains only letters, underscores and digits.
Can easily be overridden to accommodate alternative definitions of "word".
- Parameters:
w
- the "word" to match- Returns:
true
if the "word" that will be read matchesw
(and no other word)- Throws:
IOException
-
close
Closes theLookAheadReader
and the wrappedjava.lang.Reader
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classReader
- Throws:
IOException
-
mark
Marks the present position in the stream. Subsequent calls toreset()
will reposition the stream to this point.- Overrides:
mark
in classReader
- Parameters:
readAheadLimit
- Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.- Throws:
IOException
- ifreadAheadLimit
exceeds buffer capacity
-
markSupported
Returnstrue
if this reader supports themark(int)
operation, which it does, but only up to the maximum capacity of the buffer.- Overrides:
markSupported
in classReader
- Returns:
true
-
peek
Returns the nextchar
that will be read.- Returns:
- the next
char
that will be read - Throws:
IOException
-
peek
Returns then
thchar
that will be read, counting from 0 (peek(0)
returns the firstchar
that will be read).- Returns:
- the
n
thchar
that will be read - Throws:
IOException
-
read
Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.- Overrides:
read
in classReader
- Returns:
- The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
- Throws:
IOException
-
read
Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.- Specified by:
read
in classReader
- Parameters:
cbuf
- Destination bufferoff
- Offset at which to start storing characterslen
- Maximum number of characters to read- Returns:
- The number of characters read, or -1 if the end of the stream has been reached
- Throws:
IOException
-
readChar
Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.- Returns:
- The character read, as
char
, or (char
)-1 if the end of the stream has been reached - Throws:
IOException
-
ready
Returnstrue
if this reader is ready to be read.- Overrides:
ready
in classReader
- Returns:
true
if the next read() is guaranteed not to block for input,false
otherwise. Note that returningfalse
does not guarantee that the next read will block.- Throws:
IOException
-
reset
Repositions the reader to the mark.- Overrides:
reset
in classReader
- Throws:
IOException
- if the reader was not marked or if the mark was invalidated by reading past the read ahead limit.
-
skip
Skips characters.- Overrides:
skip
in classReader
- Parameters:
n
- the number of characters to skip- Returns:
- the number of characters actually skipped
- Throws:
IOException
-
skipWhitespaceAndComments
Skips white space and comments.The class can easily be subclassed to accommodate alternative definitions of white space and comments.
- Throws:
IOException
-