Package net.sherst.io

Class LookAheadReader

java.lang.Object
java.io.Reader
net.sherst.io.LookAheadReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class LookAheadReader extends Reader
Wrapper for any 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

    Fields inherited from class java.io.Reader

    lock
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new LookAheadReader which wraps the provided Reader with the default lookahead limit of 128 characters.
    LookAheadReader​(Reader reader, int capacity)
    Creates a new LookAheadReader which wraps the provided Reader with specified lookahead limit.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    at​(char c)
    Returns true if the next char that will be read is c.
    boolean
    at​(int n, char c)
    Returns true if the nth char that will be read is c, counting from 0 (at(0, 'x') tests the first char that will be read).
    boolean
    at​(String s)
    Returns true if the next charss that will be read match the String s.
    boolean
    Returns true if the next chars that will be read is #.
    boolean
    Returns true if all the charss have been read.
    boolean
    Returns true if all the next char that will be read is '\n' or '\r' or if atEOF().
    boolean
    atSkip​(char c)
    If the next char that will be read is c, returns true and removes c from the input.
    boolean
    atSkip​(String s)
    If the next chars that will be read match the String s, returns true and removes the chars from the input.
    boolean
    If the next characters to be read will be a "comment", returns true and removes the comment from the input.
    boolean
    If the next char that will be read will be a white space character, returns true and removes the char from the input.
    boolean
    If the "word" that will be read matches w (and no other word).
    boolean
    Returns true if the next char that will be read will be a white space character.
    boolean
    atWord​(String w)
    Returns true if the "word" that will be read matches w (and no other word).
    void
    Closes the LookAheadReader and the wrapped java.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
    Returns true if this reader supports the mark(int) operation, which it does, but only up to the maximum capacity of the buffer.
    char
    Returns the next char that will be read.
    char
    peek​(int n)
    Returns the nth char that will be read, counting from 0 (peek(0) returns the first char that will be read).
    int
    Reads a single character.
    int
    read​(char[] cbuf, int off, int len)
    Reads characters into a portion of an array.
    char
    Reads a single character.
    boolean
    Returns true if this reader is ready to be read.
    void
    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • isDecimalDigit

      public static boolean isDecimalDigit(char c)
    • isIdentifierChar

      public static boolean isIdentifierChar(char c)
    • isIdentifierStart

      public static boolean isIdentifierStart(char c)
    • isWhitespace

      public static boolean isWhitespace(char c)
    • at

      public boolean at(char c) throws IOException
      Returns true if the next char that will be read is c.
      Parameters:
      c - the char to test for
      Returns:
      true if the next char that will be read is c
      Throws:
      IOException
    • at

      public boolean at(int n, char c) throws IOException
      Returns true if the nth char that will be read is c, counting from 0 (at(0, 'x') tests the first char that will be read).
      Parameters:
      n -
      c - the char to test for
      Returns:
      true if the nth char that will be read is c
      Throws:
      IOException
    • at

      public boolean at(String s) throws IOException
      Returns true if the next charss that will be read match the String s.
      Parameters:
      s - the String to match
      Returns:
      true if the next charss that will be read match the String s
      Throws:
      IOException
    • atComment

      public boolean atComment() throws IOException
      Returns true if the next chars 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

      public boolean atEOF() throws IOException
      Returns true if all the charss have been read.
      Returns:
      true if all the charss have been read
      Throws:
      IOException
    • atEOL

      public boolean atEOL() throws IOException
      Returns true if all the next char that will be read is '\n' or '\r' or if atEOF().
      Returns:
      true if all the next char that will be read is '\n' or '\r' or if atEOF()
      Throws:
      IOException
    • atSkip

      public boolean atSkip(char c) throws IOException
      If the next char that will be read is c, returns true and removes c from the input.
      Parameters:
      c - the true to test for
      Returns:
      true if the next char that will be read is c
      Throws:
      IOException
    • atSkip

      public boolean atSkip(String s) throws IOException
      If the next chars that will be read match the String s, returns true and removes the chars from the input.
      Parameters:
      s - the String to match
      Returns:
      true if the next char that will be read match the String s
      Throws:
      IOException
    • atSkipComment

      public boolean atSkipComment() throws IOException
      If the next characters to be read will be a "comment", returns true 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

      public boolean atSkipWhitespaceChar() throws IOException
      If the next char that will be read will be a white space character, returns true and removes the char from the input.

      Can easily be overridden to accommodate alternative definitions of "white space".

      Returns:
      true if the next char that will be read will be a white space character
      Throws:
      IOException
    • atSkipWord

      public boolean atSkipWord(String w) throws IOException
      If the "word" that will be read matches w (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 matches w (and no other word)
      Throws:
      IOException
    • atWhitespace

      public boolean atWhitespace() throws IOException
      Returns true if the next char 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 next char that will be read will be a white space character
      Throws:
      IOException
    • atWord

      public boolean atWord(String w) throws IOException
      Returns true if the "word" that will be read matches w (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 matches w (and no other word)
      Throws:
      IOException
    • close

      public void close() throws IOException
      Closes the LookAheadReader and the wrapped java.lang.Reader.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader
      Throws:
      IOException
    • mark

      public void mark(int readAheadLimit) throws IOException
      Marks the present position in the stream. Subsequent calls to reset() will reposition the stream to this point.
      Overrides:
      mark in class Reader
      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 - if readAheadLimit exceeds buffer capacity
    • markSupported

      public boolean markSupported()
      Returns true if this reader supports the mark(int) operation, which it does, but only up to the maximum capacity of the buffer.
      Overrides:
      markSupported in class Reader
      Returns:
      true
    • peek

      public char peek() throws IOException
      Returns the next char that will be read.
      Returns:
      the next char that will be read
      Throws:
      IOException
    • peek

      public char peek(int n) throws IOException
      Returns the nth char that will be read, counting from 0 (peek(0) returns the first char that will be read).
      Returns:
      the nth char that will be read
      Throws:
      IOException
    • read

      public int read() throws IOException
      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 class Reader
      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

      public int read(char[] cbuf, int off, int len) throws IOException
      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 class Reader
      Parameters:
      cbuf - Destination buffer
      off - Offset at which to start storing characters
      len - 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

      public char readChar() throws IOException
      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

      public boolean ready() throws IOException
      Returns true if this reader is ready to be read.
      Overrides:
      ready in class Reader
      Returns:
      true if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
      Throws:
      IOException
    • reset

      public void reset() throws IOException
      Repositions the reader to the mark.
      Overrides:
      reset in class Reader
      Throws:
      IOException - if the reader was not marked or if the mark was invalidated by reading past the read ahead limit.
    • skip

      public long skip(long n) throws IOException
      Skips characters.
      Overrides:
      skip in class Reader
      Parameters:
      n - the number of characters to skip
      Returns:
      the number of characters actually skipped
      Throws:
      IOException
    • skipWhitespaceAndComments

      public void skipWhitespaceAndComments() throws IOException
      Skips white space and comments.

      The class can easily be subclassed to accommodate alternative definitions of white space and comments.

      Throws:
      IOException