Scanner Class Reference

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.
| ~Scanner ()
const string & | getFormat () const
void | setFormat (const string &format)
unique_ptr< IOHandler > | createIOHandler () const
bool | match (char c)
| Return true if the next character is c, and in that case skip past it.
bool | matchEOF ()
| Return true if no more input.
void | expect (char expected)
| Require the next character to be equal to expected.
void | expect (char a, char b)
| Require the next character to be equal to a or b.
void | expect (const char *str)
| Require the following characters to be equal to str.
void | expect (const string &str)
| Require the following characters to be equal to str.
void | expectEOF ()
| Require that there is no more input.
void | expectIntegerNoSign ()
| Read an arbitrary-precision integer.
void | readInteger (mpz_class &integer)
| Read an arbitrary-precision integer.
void | readIntegerNoSign (string &str)
| Read an arbitrary-precision integer.
void | readIntegerNoSign (mpz_class &str)
| Read an arbitrary-precision integer.
void | readIntegerAndNegativeAsZero (mpz_class &integer)
| Read an integer and set it to zero if it is negative.
void | readIntegerAndNegativeAsZero (std::string &integer)
| Read an integer and set it to zero if it is negative.
void | readSizeT (size_t &size)
| Reads a size_t, where the representable range of that type determines when the number is too big.
const char * | readIdentifier ()
| The returned string is only valid until the next method on this object gets called.
void | readIdentifier (string &str)
| Reads an identifier into str.
size_t | readVariable (const VarNames &names)
| Reads an identifier and returns the index of that identifier as the index of a variable in names.
bool | peekIdentifier ()
| Skips whitespace and returns true if the next token is an identifier.
bool | peekWhite ()
| Returns true if the next character is whitespace.
bool | peek (char character)
| Skips whitespace and returns true if the next character is equal to the parameter(s).
unsigned int | getLineNumber () const
| Returns the number of newlines seen.
int | peek ()
| Returns the next character or EOF.
void | eatWhite ()
| Reads past any whitespace, where whitespace is defined by the standard function isspace().

## Private Member Functions

size_t | readIntegerString ()
| Returns the size of the string.
size_t | readIntegerStringNoSign ()
| Returns the size of the string.
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

## Detailed Description

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.

Definition at line 50 of file Scanner.h.

## Constructor & Destructor Documentation

## Scanner()

Scanner::Scanner | ( | const string & | formatName,
|  | FILE * | in )

Construct a Scanner object.

Parameters

  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.

## ~Scanner()

Scanner::~Scanner | ( |  | ) |

inline

Definition at line 59 of file Scanner.h.

## Member Function Documentation

## createIOHandler()

unique_ptr< IOHandler > Scanner::createIOHandler | ( |  | ) | const

Definition at line 42 of file Scanner.cpp.

## eatWhite()

void Scanner::eatWhite | ( |  | ) |

inline

Reads past any whitespace, where whitespace is defined by the standard function isspace().

Definition at line 267 of file Scanner.h.

## errorExpectOne()

void Scanner::errorExpectOne | ( | char | expected,
|  | int | got )

private

Definition at line 94 of file Scanner.cpp.

## errorExpectTwo()

void Scanner::errorExpectTwo | ( | char | a,
|  | char | b,
|  | int | got )

private

Definition at line 87 of file Scanner.cpp.

## errorReadIdentifier()

void Scanner::errorReadIdentifier | ( |  | ) |

private

Definition at line 274 of file Scanner.cpp.

## errorReadVariable()

void Scanner::errorReadVariable | ( | const char * | name | ) |

private

Definition at line 278 of file Scanner.cpp.

## expect() [1/4]

void Scanner::expect | ( | char | a,
|  | char | b )

inline

Require the next character to be equal to a or b.

This character is skipped past.

Definition at line 224 of file Scanner.h.

## expect() [2/4]

void Scanner::expect | ( | char | expected | ) |

inline

Require the next character to be equal to expected.

This character is skipped past.

Definition at line 231 of file Scanner.h.

## expect() [3/4]

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.

## expect() [4/4]

void Scanner::expect | ( | const string & | str | ) |

inline

Require the following characters to be equal to str.

These characters are skipped past.

Definition at line 86 of file Scanner.h.

## expectEOF()

void Scanner::expectEOF | ( |  | ) |

Require that there is no more input.

Definition at line 77 of file Scanner.cpp.

## expectIntegerNoSign()

void Scanner::expectIntegerNoSign | ( |  | ) |

inline

Read an arbitrary-precision integer.

Definition at line 243 of file Scanner.h.

## getChar()

int Scanner::getChar | ( |  | ) |

inline private

Definition at line 272 of file Scanner.h.

## getFormat()

const string & Scanner::getFormat | ( |  | ) | const

inline

Definition at line 61 of file Scanner.h.

## getLineNumber()

unsigned int Scanner::getLineNumber | ( |  | ) | const

inline

Returns the number of newlines seen.

Does not skip whitespace.

Definition at line 145 of file Scanner.h.

## growTmpString()

void Scanner::growTmpString | ( |  | ) |

private

Definition at line 243 of file Scanner.cpp.

## match()

bool Scanner::match | ( | char | c | ) |

inline

Return true if the next character is c, and in that case skip past it.

Definition at line 215 of file Scanner.h.

## matchEOF()

bool Scanner::matchEOF | ( |  | ) |

inline

Return true if no more input.

Definition at line 210 of file Scanner.h.

## parseInteger()

void Scanner::parseInteger | ( | mpz_class & | integer,
|  | size_t | size )

private

Definition at line 150 of file Scanner.cpp.

## peek() [1/2]

int Scanner::peek | ( |  | ) |

inline

Returns the next character or EOF.

Does not skip whitespace.

Definition at line 148 of file Scanner.h.

## peek() [2/2]

bool Scanner::peek | ( | char | character | ) |

inline

Skips whitespace and returns true if the next character is equal to the parameter(s).

Definition at line 262 of file Scanner.h.

## peekIdentifier()

bool Scanner::peekIdentifier | ( |  | ) |

inline

Skips whitespace and returns true if the next token is an identifier.

Definition at line 257 of file Scanner.h.

## peekWhite()

bool Scanner::peekWhite | ( |  | ) |

inline

Returns true if the next character is whitespace.

Does not, obviously, skip whitespace.

Definition at line 137 of file Scanner.h.

## readBuffer()

int Scanner::readBuffer | ( |  | ) |

private

Definition at line 304 of file Scanner.cpp.

## readIdentifier() [1/2]

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.

## readIdentifier() [2/2]

void Scanner::readIdentifier | ( | string & | str | ) |

inline

Reads an identifier into str.

str must be empty.

Definition at line 191 of file Scanner.h.

## readInteger()

void Scanner::readInteger | ( | mpz_class & | integer | ) |

inline

Read an arbitrary-precision integer.

Definition at line 238 of file Scanner.h.

## readIntegerAndNegativeAsZero() [1/2]

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.

## readIntegerAndNegativeAsZero() [2/2]

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.

## readIntegerNoSign() [1/2]

void Scanner::readIntegerNoSign | ( | mpz_class & | str | ) |

inline

Read an arbitrary-precision integer.

Definition at line 247 of file Scanner.h.

## readIntegerNoSign() [2/2]

void Scanner::readIntegerNoSign | ( | string & | str | ) |

inline

Read an arbitrary-precision integer.

Definition at line 252 of file Scanner.h.

## readIntegerString()

size_t Scanner::readIntegerString | ( |  | ) |

private

Returns the size of the string.

Definition at line 101 of file Scanner.cpp.

## readIntegerStringNoSign()

size_t Scanner::readIntegerStringNoSign | ( |  | ) |

private

Returns the size of the string.

Definition at line 127 of file Scanner.cpp.

## readSizeT()

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.

## readVariable()

size_t Scanner::readVariable | ( | const VarNames & | names | ) |

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.

Definition at line 201 of file Scanner.h.

## reportErrorUnexpectedToken() [1/2]

void Scanner::reportErrorUnexpectedToken | ( | const string & | expected,
|  | const string & | got )

private

Definition at line 294 of file Scanner.cpp.

## reportErrorUnexpectedToken() [2/2]

void Scanner::reportErrorUnexpectedToken | ( | const string & | expected,
|  | int | got )

private

Definition at line 284 of file Scanner.cpp.

## setFormat()

void Scanner::setFormat | ( | const string & | format | ) |

inline

Definition at line 62 of file Scanner.h.

## Member Data Documentation

## _buffer

vector<char> Scanner::_buffer

private

Definition at line 185 of file Scanner.h.

## _bufferPos

vector<char>::iterator Scanner::_bufferPos

private

Definition at line 186 of file Scanner.h.

## _char

int Scanner::_char

private

Definition at line 178 of file Scanner.h.

## _formatName

string Scanner::_formatName

private

Definition at line 183 of file Scanner.h.

## _in

FILE* Scanner::_in

private

Definition at line 176 of file Scanner.h.

## _integer

mpz_class Scanner::_integer

private

Definition at line 175 of file Scanner.h.

## _lineNumber

unsigned long Scanner::_lineNumber

private

Definition at line 177 of file Scanner.h.

## _tmpString

char* Scanner::_tmpString

private

Definition at line 180 of file Scanner.h.

## _tmpStringCapacity

size_t Scanner::_tmpStringCapacity

private

Definition at line 181 of file Scanner.h.

The documentation for this class was generated from the following files:

- src/Scanner.h
- src/Scanner.cpp