Frobby
0.9.5
|
This class offers an input interface which is more convenient and for some purposes more efficient than dealing with a FILE* directly. More...
#include <Scanner.h>
Public Member Functions | |
Scanner (const string &formatName, FILE *in) | |
Construct a Scanner object. More... | |
~Scanner () | |
const string & | getFormat () const |
void | setFormat (const string &format) |
auto_ptr< IOHandler > | createIOHandler () const |
bool | match (char c) |
Return true if the next character is c, and in that case skip past it. More... | |
bool | matchEOF () |
Return true if no more input. More... | |
void | expect (char expected) |
Require the next character to be equal to expected. More... | |
void | expect (char a, char b) |
Require the next character to be equal to a or b. More... | |
void | expect (const char *str) |
Require the following characters to be equal to str. More... | |
void | expect (const string &str) |
Require the following characters to be equal to str. More... | |
void | expectEOF () |
Require that there is no more input. More... | |
void | expectIntegerNoSign () |
Read an arbitrary-precision integer. More... | |
void | readInteger (mpz_class &integer) |
Read an arbitrary-precision integer. More... | |
void | readIntegerNoSign (string &str) |
Read an arbitrary-precision integer. More... | |
void | readIntegerNoSign (mpz_class &str) |
Read an arbitrary-precision integer. More... | |
void | readIntegerAndNegativeAsZero (mpz_class &integer) |
Read an integer and set it to zero if it is negative. More... | |
void | readIntegerAndNegativeAsZero (std::string &integer) |
Read an integer and set it to zero if it is negative. More... | |
void | readSizeT (size_t &size) |
Reads a size_t, where the representable range of that type determines when the number is too big. More... | |
const char * | readIdentifier () |
The returned string is only valid until the next method on this object gets called. More... | |
void | readIdentifier (string &str) |
Reads an identifier into str. More... | |
size_t | readVariable (const VarNames &names) |
Reads an identifier and returns the index of that identifier as the index of a variable in names. More... | |
bool | peekIdentifier () |
Skips whitespace and returns true if the next token is an identifier. More... | |
bool | peekWhite () |
Returns true if the next character is whitespace. More... | |
bool | peek (char character) |
Skips whitespace and returns true if the next character is equal to the parameter(s). More... | |
unsigned int | getLineNumber () const |
Returns the number of newlines seen. More... | |
int | peek () |
Returns the next character or EOF. More... | |
void | eatWhite () |
Reads past any whitespace, where whitespace is defined by the standard function isspace(). More... | |
Private Member Functions | |
size_t | readIntegerString () |
Returns the size of the string. More... | |
size_t | readIntegerStringNoSign () |
Returns the size of the string. More... | |
void | parseInteger (mpz_class &integer, size_t size) |
void | errorExpectTwo (char a, char b, int got) |
void | errorExpectOne (char expected, int got) |
void | errorReadVariable (const char *name) |
void | errorReadIdentifier () |
void | reportErrorUnexpectedToken (const string &expected, int got) |
void | reportErrorUnexpectedToken (const string &expected, const string &got) |
int | getChar () |
void | growTmpString () |
int | readBuffer () |
Private Attributes | |
mpz_class | _integer |
FILE * | _in |
unsigned long | _lineNumber |
int | _char |
char * | _tmpString |
size_t | _tmpStringCapacity |
string | _formatName |
vector< char > | _buffer |
vector< char >::iterator | _bufferPos |
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. |
Definition at line 28 of file Scanner.cpp.
auto_ptr< IOHandler > Scanner::createIOHandler | ( | ) | const |
Definition at line 42 of file Scanner.cpp.
|
inline |
|
private |
Definition at line 94 of file Scanner.cpp.
|
private |
Definition at line 87 of file Scanner.cpp.
|
private |
Definition at line 274 of file Scanner.cpp.
|
private |
Definition at line 278 of file Scanner.cpp.
|
inline |
|
inline |
void Scanner::expect | ( | const char * | str | ) |
Require the following characters to be equal to str.
These characters are skipped past.
Definition at line 46 of file Scanner.cpp.
|
inline |
void Scanner::expectEOF | ( | ) |
Require that there is no more input.
Definition at line 77 of file Scanner.cpp.
|
inline |
|
inline |
|
private |
Definition at line 243 of file Scanner.cpp.
|
inline |
|
inline |
|
private |
Definition at line 150 of file Scanner.cpp.
|
inline |
|
inline |
|
inline |
|
inline |
|
private |
Definition at line 304 of file Scanner.cpp.
const char * Scanner::readIdentifier | ( | ) |
The returned string is only valid until the next method on this object gets called.
Definition at line 255 of file Scanner.cpp.
|
inline |
|
inline |
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.
Definition at line 171 of file Scanner.cpp.
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 |
|
inline |
|
private |
Returns the size of the string.
Definition at line 101 of file Scanner.cpp.
|
private |
Returns the size of the string.
Definition at line 127 of file Scanner.cpp.
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.
Definition at line 205 of file Scanner.cpp.
|
inline |
|
private |
Definition at line 294 of file Scanner.cpp.
|
private |
Definition at line 284 of file Scanner.cpp.