|
Frobby 0.10.0
|
#include <Scanner.h>
Public Member Functions | |
| Scanner (const string &formatName, FILE *in) | |
| const string & | getFormat () const |
| void | setFormat (const string &format) |
| unique_ptr< IOHandler > | createIOHandler () const |
| bool | match (char c) |
| bool | matchEOF () |
| void | expect (char expected) |
| void | expect (char a, char b) |
| void | expect (const char *str) |
| void | expect (const string &str) |
| void | expectEOF () |
| void | expectIntegerNoSign () |
| void | readInteger (mpz_class &integer) |
| void | readIntegerNoSign (string &str) |
| void | readIntegerNoSign (mpz_class &str) |
| void | readIntegerAndNegativeAsZero (mpz_class &integer) |
| void | readIntegerAndNegativeAsZero (std::string &integer) |
| void | readSizeT (size_t &size) |
| const char * | readIdentifier () |
| void | readIdentifier (string &str) |
| size_t | readVariable (const VarNames &names) |
| bool | peekIdentifier () |
| bool | peekWhite () |
| bool | peek (char character) |
| unsigned int | getLineNumber () const |
| int | peek () |
| void | eatWhite () |
This class offers an input interface which is more convenient and for some purposes more efficient than dealing with a FILE* directly. It keeps track of the current line number to report better error messages. Only one Scanner should be reading from a given FILE*, since otherwise the line numbers will be inaccurate.
All input methods whose documentation does not specifically say otherwise skip whitespace as defined by the standard isspace() method.
There are four concepts for consuming input through a Scanner:
Read X: Require an X to be in the input, and return what is read.
Expect X: Require the exact value X to be in the input and skip past it.
Match X: Return true if the exact value X is in the input, and in that case skip past it. Otherwise return false and do nothing else.
Peek X: Return true if X is the next thing int he input. Do not skip past anything. May or may not skip whitespace depending on what X is.
If a requirement is not met, Scanner reports a syntax error using the functions in the error.h header.
| Scanner::Scanner | ( | const string & | formatName, |
| FILE * | in ) |
Construct a Scanner object.
| formatName | The format being read. Is used for e.g. error messages. |
| in | The file to read input from. |
|
inline |
Reads past any whitespace, where whitespace is defined by the standard function isspace().
|
inline |
Require the next character to be equal to a or b. This character is skipped past.
|
inline |
Require the next character to be equal to expected. This character is skipped past.
| void Scanner::expect | ( | const char * | str | ) |
Require the following characters to be equal to str. These characters are skipped past.
|
inline |
Require the following characters to be equal to str. These characters are skipped past.
| void Scanner::expectEOF | ( | ) |
Require that there is no more input.
|
inline |
Read an arbitrary-precision integer.
|
inline |
Returns the number of newlines seen. Does not skip whitespace.
|
inline |
Return true if the next character is c, and in that case skip past it.
|
inline |
Return true if no more input.
|
inline |
Returns the next character or EOF. Does not skip whitespace.
|
inline |
Skips whitespace and returns true if the next character is equal to the parameter(s).
|
inline |
Skips whitespace and returns true if the next token is an identifier.
|
inline |
Returns true if the next character is whitespace. Does not, obviously, skip whitespace.
| const char * Scanner::readIdentifier | ( | ) |
The returned string is only valid until the next method on this object gets called.
|
inline |
Reads an identifier into str. str must be empty.
|
inline |
Read an arbitrary-precision integer.
| void Scanner::readIntegerAndNegativeAsZero | ( | mpz_class & | integer | ) |
Read an integer and set it to zero if it is negative. This is more efficient because the sign can be detected before the integer is read.
| void Scanner::readIntegerAndNegativeAsZero | ( | std::string & | integer | ) |
Read an integer and set it to zero if it is negative. This is more efficient because the sign can be detected before the integer is read.
|
inline |
Read an arbitrary-precision integer.
|
inline |
Read an arbitrary-precision integer.
| void Scanner::readSizeT | ( | size_t & | size | ) |
Reads a size_t, where the representable range of that type determines when the number is too big. The number is required to be non-negative.
|
inline |
Reads an identifier and returns the index of that identifier as the index of a variable in names. Throws an exception if there is no identifier or the identifier is not a variable in names.