Package easyIO
Class Scanner
- java.lang.Object
-
- easyIO.BacktrackScanner
-
- easyIO.Scanner
-
public class Scanner extends BacktrackScanner
ABacktrackScanner
extended with convenience methods for parsing common things like integers and identifiers. Compared to ajava.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 fromBacktrackScanner
. Generally, all methods parse input starting at the current position, and throwUnexpectedInput
if the expected input is not found. If an exception is thrown, the current input position is left unchanged.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class easyIO.BacktrackScanner
BacktrackScanner.Location
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
consume(java.lang.String s)
Scan past the specified string.void
digits()
Scan one or more digits.void
eol()
Scan to the end of the current line or to the end of the input, whichever is first.void
floatingPoint()
Scan a floating-point number.void
identifier()
Scan an identifier ala Java.void
integer()
Scan past an integer constant.void
newline()
Scan past a optional carriage return character and a newline character.char
nextDigit()
Scan past and return the next character if it is a digit.double
nextDouble()
Scan past a floating-point constant and return its value as adouble
java.lang.String
nextIdentifier()
Scan past and return an identifier.int
nextInt()
Scan past an integer constant and return its value.java.lang.String
nextLine()
Scan past and return all text from the current position to the next newline character.void
optDigits()
Scan past all digits at the current posn, if any.void
trailingWhitespace()
Scan past any whitespace up to the end of the line, the end of the file, or the next non-whitespace character.void
whitespace()
Scan past any whitespace.
-
-
-
Constructor Detail
-
Scanner
public Scanner()
-
Scanner
public Scanner(java.io.Reader r, java.lang.String name)
Create scanner that reads from source inputr
, calling the sourcename
.
-
Scanner
public Scanner(java.lang.String filename) throws java.io.FileNotFoundException
Create scanner that reads from file namedfilename
. To read directly from a string, use aStringReader
.- 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.
-
nextDigit
public char nextDigit() throws UnexpectedInput
Scan past and return the next character if it is a digit.- Throws:
UnexpectedInput
-
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.
-
digits
public void digits() throws UnexpectedInput
Scan one or more digits.- Throws:
UnexpectedInput
- if the next character is not a digit.
-
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 adouble
- 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.
-
-