Class SimpleCharStream

java.lang.Object
org.tweetyproject.preferences.io.SimpleCharStream

public class SimpleCharStream extends Object
An implementation of interface CharStream, where the stream is assumed to contain only ASCII characters (without unicode processing).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    Position in the buffer.
    static final boolean
    Whether the parser is static.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new `SimpleCharStream` using the specified input stream, starting at the first line and column, with a default buffer size.
    SimpleCharStream(InputStream dstream, int startline, int startcolumn)
    Constructs a new `SimpleCharStream` using the specified input stream, starting line, and starting column.
    SimpleCharStream(InputStream dstream, int startline, int startcolumn, int buffersize)
    Constructs a new `SimpleCharStream` using the specified input stream, starting line, starting column, and buffer size.
    SimpleCharStream(InputStream dstream, String encoding)
    Constructs a new `SimpleCharStream` using the specified input stream and character encoding, starting at the first line and column.
    SimpleCharStream(InputStream dstream, String encoding, int startline, int startcolumn)
    Constructs a new `SimpleCharStream` using the specified input stream, character encoding, starting line, and starting column.
    SimpleCharStream(InputStream dstream, String encoding, int startline, int startcolumn, int buffersize)
    Constructs a new `SimpleCharStream` using the specified input stream, character encoding, starting line, starting column, and buffer size.
    Constructor for `SimpleCharStream` with a specified `Reader`, using default starting line, column, and buffer size.
    SimpleCharStream(Reader dstream, int startline, int startcolumn)
    Constructor for `SimpleCharStream` with a specified `Reader`, starting line, and starting column.
    SimpleCharStream(Reader dstream, int startline, int startcolumn, int buffersize)
    Constructor for `SimpleCharStream` with a specified `Reader`, starting line, starting column, and buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    adjustBeginLineColumn(int newLine, int newCol)
    Adjusts the line and column numbers for the start of a token.
    void
    backup(int amount)
    Backups the character buffer by a specified amount.
    char
    Begins the token processing by reading the next character from the input stream.
    void
    Reset buffer when finished.
    int
    Gets the beginning column number of the current token.
    int
    Gets the beginning line number of the current token.
    int
    Deprecated. 
    int
    Gets the end column number of the current token.
    int
    Gets the end line number of the current token.
    Retrieves the literal value of the current token from the internal buffer.
    int
    Deprecated. 
    char[]
    GetSuffix(int len)
    Retrieves a suffix of the internal buffer.
    char
    Reads a single character from the input stream.
    void
    Reinitializes the lexer with a new input stream, starting at line 1 and column 1, with a default buffer size.
    void
    ReInit(InputStream dstream, int startline, int startcolumn)
    Reinitializes the lexer with a new input stream and specified starting line and column.
    void
    ReInit(InputStream dstream, int startline, int startcolumn, int buffersize)
    Reinitializes the lexer with a new input stream, starting at the given line and column, with a specified buffer size.
    void
    ReInit(InputStream dstream, String encoding)
    Reinitializes the lexer with a new input stream, specified encoding, starting at the first line and column, and using a default buffer size.
    void
    ReInit(InputStream dstream, String encoding, int startline, int startcolumn)
    Reinitializes the lexer with a new input stream, specified encoding, and starting line and column.
    void
    ReInit(InputStream dstream, String encoding, int startline, int startcolumn, int buffersize)
    Reinitializes the lexer with a new input stream, specified encoding, starting at the given line and column, with a specified buffer size.
    void
    ReInit(Reader dstream)
    Reinitialises the `SimpleCharStream` with a new `Reader`, starting at the first line and column.
    void
    ReInit(Reader dstream, int startline, int startcolumn)
    Reinitialises the `SimpleCharStream` with a new `Reader`, starting at the specified line and column.
    void
    ReInit(Reader dstream, int startline, int startcolumn, int buffersize)
    Reinitialises the `SimpleCharStream` with a new `Reader`, starting line, starting column, and buffer size.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • staticFlag

      public static final boolean staticFlag
      Whether the parser is static.

      This flag indicates whether the parser is intended to use a static character stream class or not. This value is `false` in this implementation.

      See Also:
    • bufpos

      public int bufpos
      Position in the buffer.

      This field indicates the current position within the buffer where characters are read or written.

  • Constructor Details

    • SimpleCharStream

      public SimpleCharStream(Reader dstream, int startline, int startcolumn, int buffersize)
      Constructor for `SimpleCharStream` with a specified `Reader`, starting line, starting column, and buffer size.

      This constructor initializes the character stream with the given reader and sets the initial line and column numbers for reading. It also sets the buffer size for reading characters from the stream.

      Parameters:
      dstream - The `Reader` to be used by the character stream. This reader provides the source of characters.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
      buffersize - The size of the buffer to be used for reading characters. This determines the number of characters that can be buffered before reading from the underlying stream again.
    • SimpleCharStream

      public SimpleCharStream(Reader dstream, int startline, int startcolumn)
      Constructor for `SimpleCharStream` with a specified `Reader`, starting line, and starting column.

      This constructor initializes the character stream with the given reader, and sets the initial line and column numbers for reading. It uses a default buffer size of 4096 characters.

      Parameters:
      dstream - The `Reader` to be used by the character stream. This reader provides the source of characters.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
    • SimpleCharStream

      public SimpleCharStream(Reader dstream)
      Constructor for `SimpleCharStream` with a specified `Reader`, using default starting line, column, and buffer size.

      This constructor initializes the character stream with the given reader, setting the initial line and column numbers to 1, and uses a default buffer size of 4096 characters.

      Parameters:
      dstream - The `Reader` to be used by the character stream. This reader provides the source of characters.
    • SimpleCharStream

      public SimpleCharStream(InputStream dstream, String encoding, int startline, int startcolumn, int buffersize) throws UnsupportedEncodingException
      Constructs a new `SimpleCharStream` using the specified input stream, character encoding, starting line, starting column, and buffer size.

      This constructor sets up the character stream with the provided input stream and character encoding, starting at the given line and column numbers, and uses the specified buffer size for reading characters.

      Parameters:
      dstream - The input stream to be used by the character stream. This stream provides the source of characters.
      encoding - The character encoding to be used for interpreting bytes from the input stream.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
      buffersize - The size of the buffer used for reading the input stream.
      Throws:
      UnsupportedEncodingException - if the specified encoding is not supported.
    • SimpleCharStream

      public SimpleCharStream(InputStream dstream, int startline, int startcolumn, int buffersize)
      Constructs a new `SimpleCharStream` using the specified input stream, starting line, starting column, and buffer size.

      This constructor sets up the character stream with the provided input stream, starting at the given line and column numbers, and uses the specified buffer size for reading characters.

      Parameters:
      dstream - The input stream to be used by the character stream. This stream provides the source of characters.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
      buffersize - The size of the buffer used for reading the input stream.
    • SimpleCharStream

      public SimpleCharStream(InputStream dstream, String encoding, int startline, int startcolumn) throws UnsupportedEncodingException
      Constructs a new `SimpleCharStream` using the specified input stream, character encoding, starting line, and starting column.

      This constructor sets up the character stream with the provided input stream and character encoding, starting at the given line and column numbers. The default buffer size of 4096 characters is used.

      Parameters:
      dstream - The input stream to be used by the character stream. This stream provides the source of characters.
      encoding - The character encoding to be used for interpreting bytes from the input stream.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
      Throws:
      UnsupportedEncodingException - if the specified encoding is not supported.
    • SimpleCharStream

      public SimpleCharStream(InputStream dstream, int startline, int startcolumn)
      Constructs a new `SimpleCharStream` using the specified input stream, starting line, and starting column.

      This constructor sets up the character stream with the provided input stream, starting at the given line and column numbers, and uses the default buffer size of 4096 characters.

      Parameters:
      dstream - The input stream to be used by the character stream. This stream provides the source of characters.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
    • SimpleCharStream

      public SimpleCharStream(InputStream dstream, String encoding) throws UnsupportedEncodingException
      Constructs a new `SimpleCharStream` using the specified input stream and character encoding, starting at the first line and column.

      This constructor sets up the character stream with the provided input stream and character encoding, starting from line 1 and column 1, and uses the default buffer size of 4096 characters.

      Parameters:
      dstream - The input stream to be used by the character stream. This stream provides the source of characters.
      encoding - The character encoding to be used for interpreting bytes from the input stream.
      Throws:
      UnsupportedEncodingException - if the specified encoding is not supported.
    • SimpleCharStream

      public SimpleCharStream(InputStream dstream)
      Constructs a new `SimpleCharStream` using the specified input stream, starting at the first line and column, with a default buffer size.

      This constructor sets up the character stream with the provided input stream, starting from line 1 and column 1, and uses the default buffer size of 4096 characters.

      Parameters:
      dstream - The input stream to be used by the character stream. This stream provides the source of characters.
  • Method Details

    • BeginToken

      public char BeginToken() throws IOException
      Begins the token processing by reading the next character from the input stream.

      This method initializes the starting position for the token by setting the `tokenBegin` to the current buffer position. It then reads the first character of the new token.

      The `tokenBegin` field is updated to mark the start of the new token, and the character read is returned. This method is typically called at the beginning of tokenization to ensure that the lexer is ready to process a new token from the correct position in the input stream.

      Returns:
      The first character of the new token.
      Throws:
      IOException - If an I/O error occurs while reading from the input stream.
    • readChar

      public char readChar() throws IOException
      Reads a single character from the input stream.

      This method attempts to read a character from the buffer if available. If the buffer is empty or if the end of the buffer has been reached, it will fill the buffer with more characters from the input stream before returning the character. It also updates the line and column numbers based on the character read.

      Returns:
      The character read from the input stream.
      Throws:
      IOException - If an I/O error occurs while reading from the stream.
    • getColumn

      @Deprecated public int getColumn()
      Deprecated.
      Returns:
      column
      See Also:
    • getLine

      @Deprecated public int getLine()
      Deprecated.
      getter
      Returns:
      line
      See Also:
    • getEndColumn

      public int getEndColumn()
      Gets the end column number of the current token.

      This method retrieves the column number where the current token ends. The column number is 1-based.

      Returns:
      The end column number of the current token.
    • getEndLine

      public int getEndLine()
      Gets the end line number of the current token.

      This method retrieves the line number where the current token ends. The line number is 1-based.

      Returns:
      The end line number of the current token.
    • getBeginColumn

      public int getBeginColumn()
      Gets the beginning column number of the current token.

      This method retrieves the column number where the current token begins. The column number is 1-based.

      Returns:
      The beginning column number of the current token.
    • getBeginLine

      public int getBeginLine()
      Gets the beginning line number of the current token.

      This method retrieves the line number where the current token begins. The line number is 1-based.

      Returns:
      The beginning line number of the current token.
    • backup

      public void backup(int amount)
      Backups the character buffer by a specified amount.

      This method moves the current position in the buffer backwards by the given amount. The buffer position is adjusted accordingly to handle wrapping around the buffer.

      Parameters:
      amount - The number of characters to backup in the buffer.
    • ReInit

      public void ReInit(Reader dstream, int startline, int startcolumn, int buffersize)
      Reinitialises the `SimpleCharStream` with a new `Reader`, starting line, starting column, and buffer size.

      This method resets the character stream with a new input reader and sets the initial line and column numbers. It also adjusts the internal buffer size and resets the stream's state.

      Parameters:
      dstream - The `Reader` to be used by the character stream. This reader provides the source of characters.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
      buffersize - The size of the buffer used for reading the input stream. This determines how many characters can be held in memory for efficient reading.
    • ReInit

      public void ReInit(Reader dstream, int startline, int startcolumn)
      Reinitialises the `SimpleCharStream` with a new `Reader`, starting at the specified line and column.

      This method allows you to reset the character stream with a new input reader, and start reading from the specified line and column. The default buffer size of 4096 characters is used.

      Parameters:
      dstream - The `Reader` to be used by the character stream. This reader provides the source of characters.
      startline - The line number from which to start reading characters. Line numbers are 1-based.
      startcolumn - The column number from which to start reading characters. Column numbers are 1-based.
    • ReInit

      public void ReInit(Reader dstream)
      Reinitialises the `SimpleCharStream` with a new `Reader`, starting at the first line and column.

      This method allows you to reset the character stream with a new input reader, starting from line 1 and column 1. The default buffer size of 4096 characters is used.

      Parameters:
      dstream - The `Reader` to be used by the character stream. This reader provides the source of characters.
    • ReInit

      public void ReInit(InputStream dstream, String encoding, int startline, int startcolumn, int buffersize) throws UnsupportedEncodingException
      Reinitializes the lexer with a new input stream, specified encoding, starting at the given line and column, with a specified buffer size.

      This method sets up the lexer to read from a new input stream, using the specified character encoding. The lexer will start tokenizing from the specified line and column, and will use the provided buffer size for reading the input.

      Parameters:
      dstream - The new input stream to be used by the lexer. This stream provides the source of characters to be tokenized.
      encoding - The character encoding to be used for interpreting the bytes from the input stream. If this is null, the default encoding will be used.
      startline - The line number at which to start tokenizing. Line numbers are 1-based.
      startcolumn - The column number at which to start tokenizing. Column numbers are 1-based.
      buffersize - The size of the buffer to be used for reading the input stream.
      Throws:
      UnsupportedEncodingException - if the specified encoding is not supported.
    • ReInit

      public void ReInit(InputStream dstream, int startline, int startcolumn, int buffersize)
      Reinitializes the lexer with a new input stream, starting at the given line and column, with a specified buffer size.

      This method sets up the lexer to read from a new input stream. The lexer will start tokenizing from the specified line and column, and will use the provided buffer size for reading the input.

      Parameters:
      dstream - The new input stream to be used by the lexer. This stream provides the source of characters to be tokenized.
      startline - The line number at which to start tokenizing. Line numbers are 1-based.
      startcolumn - The column number at which to start tokenizing. Column numbers are 1-based.
      buffersize - The size of the buffer to be used for reading the input stream.
    • ReInit

      public void ReInit(InputStream dstream, String encoding) throws UnsupportedEncodingException
      Reinitializes the lexer with a new input stream, specified encoding, starting at the first line and column, and using a default buffer size.

      This method sets up the lexer to read from a new input stream, using the specified character encoding, starting from line 1 and column 1, and using a default buffer size of 4096 characters.

      Parameters:
      dstream - The new input stream to be used by the lexer. This stream provides the source of characters to be tokenized.
      encoding - The character encoding to be used for interpreting the bytes from the input stream. If this is null, the default encoding will be used.
      Throws:
      UnsupportedEncodingException - if the specified encoding is not supported.
    • ReInit

      public void ReInit(InputStream dstream)
      Reinitializes the lexer with a new input stream, starting at line 1 and column 1, with a default buffer size.

      This method resets the lexer to start reading from the provided input stream. The tokenization process will begin at line 1 and column 1 of the new input. The buffer size used for reading the input is set to the default value of 4096 characters.

      Parameters:
      dstream - The new input stream to be used by the lexer. This stream provides the source of characters to be tokenized.
    • ReInit

      public void ReInit(InputStream dstream, String encoding, int startline, int startcolumn) throws UnsupportedEncodingException
      Reinitializes the lexer with a new input stream, specified encoding, and starting line and column.

      This method allows the lexer to be reset with a new input stream, including the option to specify the character encoding. Additionally, it sets the starting line and column numbers for tokenization. This is useful for handling different input sources or encoding formats while ensuring accurate line and column tracking.

      Parameters:
      dstream - The new input stream to be used by the lexer. This stream provides the source of characters to be tokenized.
      encoding - The name of the character encoding to be used for reading the input stream. If the encoding is not supported, an `UnsupportedEncodingException` will be thrown.
      startline - The line number at which tokenization should begin. This helps in aligning the lexer with the correct line number in the input.
      startcolumn - The column number at which tokenization should begin. This assists in aligning the lexer with the correct column position within the line.
      Throws:
      UnsupportedEncodingException - If the specified encoding is not supported by the Java runtime.
    • ReInit

      public void ReInit(InputStream dstream, int startline, int startcolumn)
      Reinitializes the lexer with a new input stream and specified starting line and column.

      This method allows for resetting the lexer with a new input stream, and optionally specifying the starting line and column numbers for tokenization. This is useful for cases where the lexer needs to be reused for different input streams or to start tokenization from a specific position.

      Parameters:
      dstream - The new input stream to be used by the lexer.
      startline - The line number at which tokenization should begin. This is typically used to set the starting line for the lexer to match the input stream's context.
      startcolumn - The column number at which tokenization should begin. This helps in aligning the lexer with the input stream's context starting from a specific column.
    • GetImage

      public String GetImage()
      Retrieves the literal value of the current token from the internal buffer.

      This method returns a String representing the sequence of characters in the buffer that corresponds to the current token. The literal value is the substring of the buffer from the start of the token to the current buffer position.

      Returns:
      A String containing the literal value of the current token.
    • GetSuffix

      public char[] GetSuffix(int len)
      Retrieves a suffix of the internal buffer.

      This method returns a character array that represents the suffix of the buffer, starting from the current buffer position and extending back by the specified length.

      Parameters:
      len - The length of the suffix to retrieve.
      Returns:
      A character array containing the suffix of the buffer.
      Throws:
      IllegalArgumentException - if the length specified is greater than the size of the buffer.
    • Done

      public void Done()
      Reset buffer when finished.
    • adjustBeginLineColumn

      public void adjustBeginLineColumn(int newLine, int newCol)
      Adjusts the line and column numbers for the start of a token.

      This method updates the line and column numbers in the internal buffer to reflect changes in the position of the start of a token. It recalculates the line and column numbers based on the new starting position of the token.

      Parameters:
      newLine - The new line number where the token begins.
      newCol - The new column number where the token begins.