Package easyIO

Class BacktrackScanner

  • Direct Known Subclasses:
    Scanner

    public class BacktrackScanner
    extends java.lang.Object
    A scanner class that, unlike java.util.Scanner, supports arbitrary lookahead and backtracking. The caller can use mark() to set some number of marks in the input stream, then accept() to erase the previous mark or abort() to roll back to (and erase) the previous mark. The marks function as a stack of previous points in the input. The class also allows reading a stream that is spread across multiple input sources, and keeps track of the current source, current line number, and current position within the line. Arbitrary lookahead is allowed, but the space consumed by a scanner is proportional to the number of chars between the first mark and last lookahead position.
    See Also:
    easyIO.Scanner
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  BacktrackScanner.Location
      An input character along with information about the source of the character, and its line number and position within the line.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void abort()
      Roll the input position back to the most recent mark, and erase the mark, effectively restarting scanning from that position.
      void accept()
      Effect: Erase the previous mark from the input, effectively accepting all input up to the current position.
      void advance()
      Advance past the next character, if any.
      void appendSource​(java.io.Reader r, java.lang.String name)
      Add r to the input stream after existing inputs.
      int charPos()  
      void close()  
      int depth()
      The current number of marks.
      void dump​(java.lang.StringBuilder w)
      Dump the state of the scanner to w in a human-readable form.
      BacktrackScanner.Location getMarkLocation()
      Location in input source of the last mark.
      java.lang.String getToken()
      Return a string containing the characters from the most recent mark to the current position.
      boolean hasNext()
      Whether there is a character ahead in input.
      void includeSource​(java.io.Reader r, java.lang.String name)
      Add r to the input stream ahead of any existing inputs.
      boolean invariant()  
      int lineNo()  
      BacktrackScanner.Location location()
      Location in input source of the current position.
      void mark()
      Add a mark at the current position.
      char next()
      Read the next character from the stream.
      int peek()
      The next character ahead in the input.
      java.lang.String source()  
      void string​(java.lang.String s)
      Scan the characters of string s from the input.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • BacktrackScanner

        public BacktrackScanner()
    • Method Detail

      • invariant

        public boolean invariant()
      • dump

        public void dump​(java.lang.StringBuilder w)
        Dump the state of the scanner to w in a human-readable form.
      • close

        public void close()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • source

        public java.lang.String source()
      • lineNo

        public int lineNo()
      • charPos

        public int charPos()
      • includeSource

        public void includeSource​(java.io.Reader r,
                                  java.lang.String name)
        Add r to the input stream ahead of any existing inputs.
      • appendSource

        public void appendSource​(java.io.Reader r,
                                 java.lang.String name)
        Add r to the input stream after existing inputs.
      • hasNext

        public boolean hasNext()
        Whether there is a character ahead in input.
      • peek

        public int peek()
        The next character ahead in the input. Equivalent to begin(); c = next(); abort(); return c;
      • mark

        public void mark()
        Add a mark at the current position.
      • accept

        public void accept()
        Effect: Erase the previous mark from the input, effectively accepting all input up to the current position.
      • depth

        public int depth()
        The current number of marks. Exposed for use in assertions, so client code can check that matching mark()...accept() calls occur at the same depth.
      • getToken

        public java.lang.String getToken()
        Return a string containing the characters from the most recent mark to the current position.
      • abort

        public void abort()
        Roll the input position back to the most recent mark, and erase the mark, effectively restarting scanning from that position.
      • advance

        public void advance()
        Advance past the next character, if any. Do nothing if at end of input.
      • next

        public char next()
                  throws EOF
        Read the next character from the stream.
        Throws:
        EOF
      • string

        public void string​(java.lang.String s)
                    throws UnexpectedInput
        Scan the characters of string s from the input.
        Throws:
        UnexpectedInput - if something other than the expected characters are encountered.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object