Package easyIO

Class Scanner


  • public class Scanner
    extends BacktrackScanner
    A BacktrackScanner extended with convenience methods for parsing common things like integers and identifiers. Compared to a java.util.Scanner, it has the ability to peek() at the next character on the input and the ability to read its input from multiple Readers, which may be added on the fly. Beyond this, it can also do arbitary lookahead and rollback of the input stream using the mark(), accept(), and abort() methods from BacktrackScanner. Generally, all methods parse input starting at the current position, and throw UnexpectedInput if the expected input is not found. If an exception is thrown, the current input position is left unchanged.
    • Constructor Detail

      • Scanner

        public Scanner()
      • Scanner

        public Scanner​(java.io.Reader r,
                       java.lang.String name)
        Create scanner that reads from source input r, calling the source name.
      • Scanner

        public Scanner​(java.lang.String filename)
                throws java.io.FileNotFoundException
        Create scanner that reads from file named filename. To read directly from a string, use a StringReader.
        Throws:
        java.io.FileNotFoundException
        See Also:
        StringReader
    • Method Detail

      • whitespace

        public void whitespace()
        Scan past any whitespace.
      • consume

        public void consume​(java.lang.String s)
                     throws UnexpectedInput
        Scan past the specified string.
        Throws:
        UnexpectedInput - if anything other than the expected string is encountered, leaving the scanner at the position where an unexpected character is reached.
      • optDigits

        public void optDigits()
        Scan past all digits at the current posn, if any.
      • integer

        public void integer()
                     throws UnexpectedInput
        Scan past an integer constant.
        Throws:
        UnexpectedInput - if the characters at the current position are not a integer literal.
      • floatingPoint

        public void floatingPoint()
                           throws UnexpectedInput
        Scan a floating-point number.
        Throws:
        UnexpectedInput - if the characters at the current position are not a floating-point literal.
      • identifier

        public void identifier()
                        throws UnexpectedInput
        Scan an identifier ala Java.
        Throws:
        UnexpectedInput - if the next characters are not an identifier.
      • nextInt

        public int nextInt()
                    throws UnexpectedInput
        Scan past an integer constant and return its value.
        Throws:
        UnexpectedInput - if the characters following the current position are not an integer literal.
      • nextDouble

        public double nextDouble()
                          throws UnexpectedInput
        Scan past a floating-point constant and return its value as a double
        Throws:
        UnexpectedInput - if the characters following the current position are not a legal representation of a double literal.
      • nextLine

        public java.lang.String nextLine()
                                  throws UnexpectedInput
        Scan past and return all text from the current position to the next newline character.
        Throws:
        UnexpectedInput - if there is no terminated line after the current position.
      • nextIdentifier

        public java.lang.String nextIdentifier()
                                        throws UnexpectedInput
        Scan past and return an identifier.
        Throws:
        UnexpectedInput - if the characters at the current position are not a legal Java identifier.
      • newline

        public void newline()
                     throws UnexpectedInput
        Scan past a optional carriage return character and a newline character.
        Throws:
        UnexpectedInput - if the next characters are neither "\r\n" nor "\n".
      • eol

        public void eol()
        Scan to the end of the current line or to the end of the input, whichever is first.
      • trailingWhitespace

        public void trailingWhitespace()
        Scan past any whitespace up to the end of the line, the end of the file, or the next non-whitespace character.