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
ConstructorsConstructorDescriptionLookAheadReader(Reader reader)Creates a newLookAheadReaderwhich wraps the providedReaderwith the default lookahead limit of 128 characters.LookAheadReader(Reader reader, int capacity)Creates a newLookAheadReaderwhich wraps the providedReaderwith specified lookahead limit. -
Method Summary
Modifier and TypeMethodDescriptionbooleanat(char c)Returnstrueif the nextcharthat will be read isc.booleanat(int n, char c)Returnstrueif thenthcharthat will be read isc, counting from 0 (at(0, 'x')tests the firstcharthat will be read).booleanbooleanReturnstrueif the nextcharsthat will be read is#.booleanatEOF()Returnstrueif all thecharss have been read.booleanatEOL()booleanatSkip(char c)If the nextcharthat will be read isc, returnstrueand removescfrom the input.booleanIf the nextchars that will be read match theStrings, returnstrueand removes thechars from the input.booleanIf the next characters to be read will be a "comment", returnstrueand removes the comment from the input.booleanIf the nextcharthat will be read will be a white space character, returnstrueand removes thecharfrom the input.booleanatSkipWord(String w)If the "word" that will be read matchesw(and no other word).booleanReturnstrueif the nextcharthat will be read will be a white space character.booleanReturns true if the "word" that will be read matchesw(and no other word).voidclose()Closes theLookAheadReaderand the wrappedjava.lang.Reader.static booleanisDecimalDigit(char c)static booleanisIdentifierChar(char c)static booleanisIdentifierStart(char c)static booleanisWhitespace(char c)voidmark(int readAheadLimit)Marks the present position in the stream.booleanReturnstrueif this reader supports themark(int)operation, which it does, but only up to the maximum capacity of the buffer.charpeek()Returns the nextcharthat will be read.charpeek(int n)Returns thenthcharthat will be read, counting from 0 (peek(0)returns the firstcharthat will be read).intread()Reads a single character.intread(char[] cbuf, int off, int len)Reads characters into a portion of an array.charreadChar()Reads a single character.booleanready()Returnstrueif this reader is ready to be read.voidreset()Repositions the reader to the mark.longskip(long n)Skips characters.voidSkips white space and comments.Methods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
Constructor Details
-
LookAheadReader
Creates a newLookAheadReaderwhich wraps the providedReaderwith the default lookahead limit of 128 characters.- Parameters:
reader- theReaderto wrap
-
LookAheadReader
Creates a newLookAheadReaderwhich wraps the providedReaderwith specified lookahead limit.- Parameters:
reader- theReaderto wrapcapacity- the look ahead limit (number of characters)
-
-
Method Details
-
isDecimalDigit
-
isIdentifierChar
-
isIdentifierStart
-
isWhitespace
-
at
Returnstrueif the nextcharthat will be read isc.- Parameters:
c- thecharto test for- Returns:
trueif the nextcharthat will be read isc- Throws:
IOException
-
at
Returnstrueif thenthcharthat will be read isc, counting from 0 (at(0, 'x')tests the firstcharthat will be read).- Parameters:
n-c- thecharto test for- Returns:
trueif thenthcharthat will be read isc- Throws:
IOException
-
at
- Parameters:
s- theStringto match- Returns:
trueif the nextcharss that will be read match theStrings- Throws:
IOException
-
atComment
Returnstrueif the nextcharsthat 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
Returnstrueif all thecharss have been read.- Returns:
trueif all thecharss have been read- Throws:
IOException
-
atEOL
- Returns:
trueif all the nextcharthat will be read is '\n' or '\r' or ifatEOF()- Throws:
IOException
-
atSkip
If the nextcharthat will be read isc, returnstrueand removescfrom the input.- Parameters:
c- thetrueto test for- Returns:
trueif the nextcharthat will be read isc- Throws:
IOException
-
atSkip
If the nextchars that will be read match theStrings, returnstrueand removes thechars from the input.- Parameters:
s- theStringto match- Returns:
trueif the nextcharthat will be read match theStrings- Throws:
IOException
-
atSkipComment
If the next characters to be read will be a "comment", returnstrueand 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 nextcharthat will be read will be a white space character, returnstrueand removes thecharfrom the input.Can easily be overridden to accommodate alternative definitions of "white space".
- Returns:
trueif the nextcharthat 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:
trueif the "word" that will be read matchesw(and no other word)- Throws:
IOException
-
atWhitespace
Returnstrueif the nextcharthat will be read will be a white space character.Can easily be overridden to accommodate alternative definitions of "white space".
- Returns:
trueif the nextcharthat 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:
trueif the "word" that will be read matchesw(and no other word)- Throws:
IOException
-
close
Closes theLookAheadReaderand the wrappedjava.lang.Reader.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException
-
mark
Marks the present position in the stream. Subsequent calls toreset()will reposition the stream to this point.- Overrides:
markin 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- ifreadAheadLimitexceeds buffer capacity
-
markSupported
Returnstrueif this reader supports themark(int)operation, which it does, but only up to the maximum capacity of the buffer.- Overrides:
markSupportedin classReader- Returns:
true
-
peek
Returns the nextcharthat will be read.- Returns:
- the next
charthat will be read - Throws:
IOException
-
peek
Returns thenthcharthat will be read, counting from 0 (peek(0)returns the firstcharthat will be read).- Returns:
- the
nthcharthat 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:
readin 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:
readin 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
Returnstrueif this reader is ready to be read.- Overrides:
readyin classReader- Returns:
trueif the next read() is guaranteed not to block for input,falseotherwise. Note that returningfalsedoes not guarantee that the next read will block.- Throws:
IOException
-
reset
Repositions the reader to the mark.- Overrides:
resetin 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:
skipin 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
-