private static class DrawingFileIO.SimpleScanner
extends java.lang.Object
nextLine()
is called.
The primary reason that this class was created (and that Scanner was unacceptable) was that
if Scanner's delimiter were set to just spaces and tabs, but not newlines, it would fail to recognize
valid content at the end of a line. for example getNextInt() would fail for "3\n".
This class allows testing (and extracting the results of) Patterns directly against the
content of the current line (at the current read position) without regard to delimiters, whereas Scanner
seems to only allow the testing of Patterns against pre-delimited text. For example:
Text: "Hello there!"
Scanner(text).hasNext("Hello There") => false
(because the test is only applied to { "Hello", "There!" }[0]
SimpleScanner(text).hasNext("Hello There") => true
(although whitespace BEFORE a match has already been skipped,
it can still be matched in the subsequent content.)
In general, Scanner's hasNextXXX functions are replaced by atXXX. Scanner.hasNext()
is replaced by
a (negated) call to atEndOfLine()
.
This function also keeps track of the current line and column numbers so users can be provided with more detailed
information about where a potential error was found.Modifier and Type | Field and Description |
---|---|
private static java.util.regex.Pattern |
ANY |
private static java.util.regex.Pattern |
BOOL |
private static java.lang.String |
END_TOKEN |
protected boolean |
eof |
private java.lang.String |
line |
protected int |
lineNum |
protected java.util.regex.Matcher |
matcher |
private static java.util.regex.Pattern |
NUM |
protected int |
pos |
protected java.io.BufferedReader |
reader |
private static java.util.regex.Pattern |
SPACE |
Constructor and Description |
---|
SimpleScanner(java.io.Reader input) |
SimpleScanner(java.lang.String content) |
Modifier and Type | Method and Description |
---|---|
protected void |
accept() |
boolean |
at(java.util.regex.Pattern pattern) |
boolean |
atBool() |
boolean |
atEndOfLine() |
boolean |
atEOF() |
boolean |
atNumber() |
int |
colNumber() |
int |
lineNumber() |
protected boolean |
matchNext(java.util.regex.Pattern p) |
java.lang.String |
next()
Returns the text of the next token, regardless of type, and advance the read position.
|
java.lang.String |
next(java.util.regex.Pattern pattern) |
boolean |
nextBool() |
double |
nextDouble() |
float |
nextFloat() |
int |
nextInt() |
java.lang.String |
nextLine()
Returns the remaining content on the current line (i.e.
|
java.util.regex.MatchResult |
nextMatch(java.util.regex.Pattern pattern) |
java.lang.String |
peek()
Returns the text of the next token, regardless of type, but does not advance the read position.
|
java.util.regex.MatchResult |
peekMatch(java.util.regex.Pattern pattern)
Returns a Matchresult if the pattern was matched, or null otherwise.
|
protected boolean |
readLine() |
protected java.lang.String |
require(java.util.regex.Pattern pattern) |
private static final java.util.regex.Pattern ANY
private static final java.util.regex.Pattern BOOL
private static final java.lang.String END_TOKEN
protected boolean eof
private java.lang.String line
protected int lineNum
protected java.util.regex.Matcher matcher
private static final java.util.regex.Pattern NUM
protected int pos
protected final java.io.BufferedReader reader
private static final java.util.regex.Pattern SPACE
public SimpleScanner(java.io.Reader input)
public SimpleScanner(java.lang.String content)
protected void accept()
public boolean at(java.util.regex.Pattern pattern)
public boolean atBool()
public boolean atEndOfLine()
public boolean atEOF()
public boolean atNumber()
public int colNumber()
public int lineNumber()
protected boolean matchNext(java.util.regex.Pattern p)
public java.lang.String next()
public java.lang.String next(java.util.regex.Pattern pattern)
public boolean nextBool()
public double nextDouble()
public float nextFloat()
public int nextInt()
public java.lang.String nextLine()
public java.util.regex.MatchResult nextMatch(java.util.regex.Pattern pattern)
public java.lang.String peek()
public java.util.regex.MatchResult peekMatch(java.util.regex.Pattern pattern)
protected boolean readLine()
protected java.lang.String require(java.util.regex.Pattern pattern)