Class DefaultLexicalParser

java.lang.Object
com.maroontress.clione.impl.DefaultLexicalParser
All Implemented Interfaces:
LexicalParser, AutoCloseable

public final class DefaultLexicalParser extends Object implements LexicalParser
The default implementation of LexicalParser.
  • Constructor Details

    • DefaultLexicalParser

      public DefaultLexicalParser(Reader reader)
      Creates a new instance.

      The instance considers Keywords.C11 as reserved words.

      Parameters:
      reader - The reader that provides the stream of the source file.
    • DefaultLexicalParser

      public DefaultLexicalParser(Reader reader, Collection<String> reservedWords)
      Creates a new instance.
      Parameters:
      reader - The reader that provides the stream of the source file.
      reservedWords - The collection that contains reserved keywords. Note that the constructor copies the collection, so changes to the collection do not affect this instance.
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface LexicalParser
      Throws:
      IOException
    • getEof

      public Optional<SourceChar> getEof() throws IOException
      Returns the character representing EOF.

      Note that there is no need for this method in most cases. If you want to detect when the line concatenation (a backslash followed by a newline character) is immediately followed by EOF, you can do so as follows:

              void parse(LexicalParser parser) {
                  for (;;) {
                      var maybeToken = parser.next();
                      if (maybeToken.isEmpty()) {
                          var maybeEof = parser.getEof();
                          assert(maybeEof.isPresent());
                          var eof = maybeEof.get();
                          assert(eof.isEof());
                          var list = eof.getChildren();
                          if (list.size() > 0) {
                              var m = "backslash-newline at end of file";
                              System.err.println(eof.getSpan() + ": warning: " + m);
                          }
                          break;
                      }
                      ...
                  }
              }
      Specified by:
      getEof in interface LexicalParser
      Returns:
      The character representing EOF. Or Optional.empty() if this parser has not yet reached EOF.
      Throws:
      IOException - If an I/O error occurs.
    • getLocation

      public SourceLocation getLocation()
      Returns the current location of the source file.
      Specified by:
      getLocation in interface LexicalParser
      Returns:
      The current location.
    • next

      public Optional<Token> next() throws IOException
      Returns the next token.
      Specified by:
      next in interface LexicalParser
      Returns:
      The next token. Or Optional.empty() if this parser reaches EOF.
      Throws:
      IOException - If an I/O error occurs.