public class BacktrackScanner
extends java.lang.Object
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.easyIO.Scanner
Modifier and Type | Class and 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.
|
Constructor and Description |
---|
BacktrackScanner() |
Modifier and Type | Method and 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() |
public boolean invariant()
public void dump(java.lang.StringBuilder w)
public void close() throws java.io.IOException
java.io.IOException
public java.lang.String source()
public int lineNo()
public int charPos()
public void includeSource(java.io.Reader r, java.lang.String name)
public void appendSource(java.io.Reader r, java.lang.String name)
public boolean hasNext()
public int peek()
public BacktrackScanner.Location location()
public BacktrackScanner.Location getMarkLocation()
public void mark()
public void accept()
public int depth()
public java.lang.String getToken()
public void abort()
public void advance()
public void string(java.lang.String s) throws UnexpectedInput
UnexpectedInput
- if something other than the expected characters are encountered.public java.lang.String toString()
toString
in class java.lang.Object