Class Transcriber

java.lang.Object
com.maroontress.clione.impl.Transcriber

public final class Transcriber extends Object
Transcribes a token read from the Source object into the TokenBuilder object.
  • Constructor Details

    • Transcriber

      public Transcriber(Source source)
      Creates a new instance.
      Parameters:
      source - The source that provides the stream of the source file.
  • Method Details

    • getSource

      public Source getSource()
      Returns the source.
      Returns:
      The source.
    • getBuilder

      public TokenBuilder getBuilder()
      Returns the token builder.
      Returns:
      The token builder.
    • toToken

      public Token toToken(TokenType type)
      Returns a new token with the specified token type.

      Invocation of this method is equivalent to:

      getBuilder().toToken(type)
      Parameters:
      type - The token type.
      Returns:
      The new token.
    • readComment

      public void readComment() throws IOException
      Reads a comment from the source.

      The token builder must have stored the slash and asterisk.

      This method will return when it reads the end of the comment (asterisk and slash) or reaches EOF.

      Throws:
      IOException - If an I/O error occurs.
    • readSingleLine

      public void readSingleLine() throws IOException
      Reads characters from the source until just before a newline character.

      This method will return when it reads characters up to just before the newline character or reaches EOF.

      Throws:
      IOException - If an I/O error occurs.
    • readFilename

      public void readFilename(char terminator) throws IOException
      Reads characters from the source up tp the specified terminator character (including the terminator character).

      This method will return when it reads the specified terminator character or reaches EOF.

      Parameters:
      terminator - The character that terminates the filename.
      Throws:
      IOException - If an I/O error occurs.
    • readStringOrCharacter

      public void readStringOrCharacter(char terminator) throws IOException
      Reads characters from the source up tp the specified terminator character (including the terminator character).

      The token builder must have stored either single- or double-quote character.

      This method takes escape sequences into account.

      This method will return when it reads the specified terminator character (except within an escape sequence) or reaches EOF.

      When this method reaches a newline character, it will return without reading it.

      Parameters:
      terminator - The terminator character.
      Throws:
      IOException - If an I/O error occurs.
    • readMax

      public int readMax(int max, Predicate<Character> accepts) throws IOException
      Reads at most the specified number of characters while the specified predicate with the character returns true.

      This method will return when it reaches EOF.

      Parameters:
      max - The maximum number of characters.
      accepts - The predicate that returns true if the specified character is accepted.
      Returns:
      The number of characters actually read.
      Throws:
      IOException - If an I/O error occurs.
    • readIdentifier

      public void readIdentifier() throws IOException
      Reads an identifier.

      The token builder must have stored the first character of an identifier. It may also have stored the second and subsequent characters of an identifier.

      The second and subsequent character of an identifier must be either:

      • An underscore character, an uppercase or lowercase letter, or a digit ([_A-Za-z0-9])
      • Universal character names (\uXXXX or \UXXXXXXXX, X is a hexadecimal digit)
      • Other implementation-defined characters

      The other implementation-defined characters are as follows:

      This method will return when it reaches EOF.

      When this method reaches a character that is not an identifier, it will return without reading it.

      Throws:
      IOException - If an I/O error occurs.
    • readZeroOrMoreChars

      public void readZeroOrMoreChars(Predicate<Character> accepts) throws IOException
      Reads zero or more characters while the specified predicate with the character returns true.

      This method will return when it reaches EOF.

      Parameters:
      accepts - The predicate that returns true if the specified character is accepted.
      Throws:
      IOException - If an I/O error occurs.
    • readZeroOrOneChar

      public SourceChar readZeroOrOneChar(Predicate<Character> accepts) throws IOException
      Reads at most one character with which the specified predicate returns true.

      This method will return when it reaches EOF.

      Parameters:
      accepts - The predicate that returns true if the specified character is accepted.
      Returns:
      The character to have read, or null if no character has been read.
      Throws:
      IOException - If an I/O error occurs.
    • readNumber

      public void readNumber() throws IOException
      Reads a preprocessing number.

      The token builder must have stored the first character of a preprocessing number. It may also have stored the second and subsequent characters of a preprocessing number.

      This method will return when it reaches EOF.

      When this method reaches a character that is not a preprocessing number, it will return without reading it.

      Throws:
      IOException - If an I/O error occurs.
    • tryReadToken

      public TokenType tryReadToken(Case.Mapper mapper, TokenType otherwise) throws IOException
      Reads characters according to the specified mapper.

      This method reads the character that the mapper maps to the Tokenizer object and then invokes the tokenizer with this transcriber.

      When this method reaches a character and the mapper does not map the character or reaches EOF, it returns the specified token type otherwise without reading any character.

      Parameters:
      mapper - The mapper.
      otherwise - The token type that this method returns either if it reaches a character that mapper does not map, or if it reaches EOF.
      Returns:
      The token type that the tokenizer returns if the mapper maps the character to the tokenizer. Otherwise, otherwise.
      Throws:
      IOException - If an I/O error occurs.
    • tryReadToken

      public TokenType tryReadToken(Case.Mapper mapper, Tokenizer otherwise) throws IOException
      Reads characters according to the specified mapper.

      This method reads the character that the mapper maps to the Tokenizer object and then invokes the tokenizer with this transcriber.

      When this method reaches a character and the mapper does not map the character or reaches EOF, it invokes the tokenizer otherwise without reading any character.

      Parameters:
      mapper - The mapper.
      otherwise - The tokenizer that this method invokes either if it reaches a character that mapper does not map, or if it reaches EOF.
      Returns:
      The token type that the tokenizer returns if the mapper maps the character to the tokenizer. Otherwise, the token type that the tokenizer otherwise returns.
      Throws:
      IOException - If an I/O error occurs.
    • readTokenOtherwise

      public TokenType readTokenOtherwise(Case.Mapper mapper, DefaultTokenizer otherwise) throws IOException
      Reads characters according to the specified mapper.

      This method reads the character that the mapper maps to the Tokenizer object and then invokes the tokenizer with this transcriber.

      When this method reads a character and the mapper does not map the character, it invokes the default tokenizer otherwise with it.

      When this method reaches EOF, it returns null without reading any character.

      Parameters:
      mapper - The mapper.
      otherwise - The default tokenizer that this method invokes if it has read a character that mapper does not map.
      Returns:
      The token type that the tokenizer returns if the mapper maps the character to the tokenizer. The token type that the default tokenizer otherwise returns if the mapper does not map the character. null if this method reaches EOF.
      Throws:
      IOException - If an I/O error occurs.