Scanner.h
Go to the documentation of this file.
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#ifndef SCANNER_GUARD
#define SCANNER_GUARD
#include "VarNames.h"
#include <string>
class IOHandler;
class Scanner {
public:
Scanner(const string& formatName, FILE* in);
~Scanner() {delete[] _tmpString;}
const string& getFormat() const {return _formatName;}
void setFormat(const string& format) {_formatName = 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) {expect(str.c_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() {return isspace(peek());}
bool peek(char character);
unsigned int getLineNumber() const {return _lineNumber;}
int peek() {return _char;}
inline void eatWhite();
private:
size_t readIntegerString();
size_t readIntegerStringNoSign();
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);
inline int getChar();
void growTmpString();
int readBuffer();
mpz_class _integer;
FILE* _in;
unsigned long _lineNumber;
int _char; // next character on stream
char* _tmpString;
size_t _tmpStringCapacity;
string _formatName;
vector<char> _buffer;
vector<char>::iterator _bufferPos;
};
inline void Scanner::readIdentifier(string& str) {
eatWhite();
if (!isalpha(peek()))
errorReadIdentifier();
str.clear();
do {
str += static_cast<char>(getChar());
} while (isalnum(peek()) || peek() == '_');
}
inline size_t Scanner::readVariable(const VarNames& names) {
const char* name = readIdentifier();
size_t var = names.getIndex(name);
if (var == VarNames::invalidIndex)
errorReadVariable(name);
return var;
}
inline bool Scanner::matchEOF() {
eatWhite();
return peek() == EOF;
}
inline bool Scanner::match(char c) {
eatWhite();
if (c == peek()) {
getChar();
return true;
} else
return false;
}
inline void Scanner::expect(char a, char b) {
eatWhite();
int got = getChar();
if (got != a && got != b)
errorExpectTwo(a, b, got);
}
inline void Scanner::expect(char expected) {
eatWhite();
int got = getChar();
if (got != expected)
errorExpectOne(expected, got);
}
inline void Scanner::readInteger(mpz_class& integer) {
size_t size = readIntegerString();
parseInteger(integer, size);
}
inline void Scanner::expectIntegerNoSign() {
readIntegerStringNoSign();
}
inline void Scanner::readIntegerNoSign(mpz_class& integer) {
readIntegerStringNoSign();
integer = _tmpString;
}
inline void Scanner::readIntegerNoSign(string& integer) {
readIntegerStringNoSign();
integer = _tmpString;
}
inline bool Scanner::peekIdentifier() {
eatWhite();
return isalpha(peek());
}
inline bool Scanner::peek(char character) {
eatWhite();
return peek() == character;
}
inline void Scanner::eatWhite() {
while (isspace(peek()))
getChar();
}
inline int Scanner::getChar() {
if (_char == '\n')
++_lineNumber;
int oldChar = _char;
if (_bufferPos == _buffer.end())
_char = readBuffer();
else {
_char = *_bufferPos;
++_bufferPos;
}
return oldChar;
}
#endif
VarNames.h
IOHandler
An IOHandler implements input and output for some format in such a way that client code does not need...
Definition IOHandler.h:41
Scanner::expect
void expect(const string &str)
Require the following characters to be equal to str.
Definition Scanner.h:86
Scanner::_integer
mpz_class _integer
Definition Scanner.h:175
Scanner::peek
int peek()
Returns the next character or EOF.
Definition Scanner.h:148
Scanner::readIntegerString
size_t readIntegerString()
Returns the size of the string.
Definition Scanner.cpp:101
Scanner::growTmpString
void growTmpString()
Definition Scanner.cpp:243
Scanner::readIntegerNoSign
void readIntegerNoSign(string &str)
Read an arbitrary-precision integer.
Definition Scanner.h:252
Scanner::_tmpString
char * _tmpString
Definition Scanner.h:180
Scanner::_tmpStringCapacity
size_t _tmpStringCapacity
Definition Scanner.h:181
Scanner::parseInteger
void parseInteger(mpz_class &integer, size_t size)
Definition Scanner.cpp:150
Scanner::_formatName
string _formatName
Definition Scanner.h:183
Scanner::Scanner
Scanner(const string &formatName, FILE *in)
Construct a Scanner object.
Definition Scanner.cpp:28
Scanner::reportErrorUnexpectedToken
void reportErrorUnexpectedToken(const string &expected, int got)
Definition Scanner.cpp:285
Scanner::_in
FILE * _in
Definition Scanner.h:176
Scanner::eatWhite
void eatWhite()
Reads past any whitespace, where whitespace is defined by the standard function isspace().
Definition Scanner.h:267
Scanner::~Scanner
~Scanner()
Definition Scanner.h:59
Scanner::createIOHandler
unique_ptr< IOHandler > createIOHandler() const
Definition Scanner.cpp:42
Scanner::_char
int _char
Definition Scanner.h:178
Scanner::_bufferPos
vector< char >::iterator _bufferPos
Definition Scanner.h:186
Scanner::_buffer
vector< char > _buffer
Definition Scanner.h:185
Scanner::errorReadIdentifier
void errorReadIdentifier()
Definition Scanner.cpp:274
Scanner::expectEOF
void expectEOF()
Require that there is no more input.
Definition Scanner.cpp:77
Scanner::peekWhite
bool peekWhite()
Returns true if the next character is whitespace.
Definition Scanner.h:137
Scanner::readIntegerAndNegativeAsZero
void readIntegerAndNegativeAsZero(mpz_class &integer)
Read an integer and set it to zero if it is negative.
Definition Scanner.cpp:171
Scanner::expectIntegerNoSign
void expectIntegerNoSign()
Read an arbitrary-precision integer.
Definition Scanner.h:243
Scanner::_lineNumber
unsigned long _lineNumber
Definition Scanner.h:177
Scanner::expect
void expect(char expected)
Require the next character to be equal to expected.
Definition Scanner.h:231
Scanner::readIdentifier
const char * readIdentifier()
The returned string is only valid until the next method on this object gets called.
Definition Scanner.cpp:255
Scanner::peekIdentifier
bool peekIdentifier()
Skips whitespace and returns true if the next token is an identifier.
Definition Scanner.h:257
Scanner::readIntegerAndNegativeAsZero
void readIntegerAndNegativeAsZero(std::string &integer)
Read an integer and set it to zero if it is negative.
Scanner::errorExpectTwo
void errorExpectTwo(char a, char b, int got)
Definition Scanner.cpp:87
Scanner::getLineNumber
unsigned int getLineNumber() const
Returns the number of newlines seen.
Definition Scanner.h:145
Scanner::readSizeT
void readSizeT(size_t &size)
Reads a size_t, where the representable range of that type determines when the number is too big.
Definition Scanner.cpp:205
Scanner::readBuffer
int readBuffer()
Definition Scanner.cpp:304
Scanner::getFormat
const string & getFormat() const
Definition Scanner.h:61
Scanner::readVariable
size_t readVariable(const VarNames &names)
Reads an identifier and returns the index of that identifier as the index of a variable in names.
Definition Scanner.h:201
Scanner::matchEOF
bool matchEOF()
Return true if no more input.
Definition Scanner.h:210
Scanner::readInteger
void readInteger(mpz_class &integer)
Read an arbitrary-precision integer.
Definition Scanner.h:238
Scanner::errorExpectOne
void errorExpectOne(char expected, int got)
Definition Scanner.cpp:94
Scanner::setFormat
void setFormat(const string &format)
Definition Scanner.h:62
Scanner::errorReadVariable
void errorReadVariable(const char *name)
Definition Scanner.cpp:278
Scanner::match
bool match(char c)
Return true if the next character is c, and in that case skip past it.
Definition Scanner.h:215
Scanner::getChar
int getChar()
Definition Scanner.h:272
Scanner::readIntegerStringNoSign
size_t readIntegerStringNoSign()
Returns the size of the string.
Definition Scanner.cpp:127
VarNames
Defines the variables of a polynomial ring and facilities IO involving them.
Definition VarNames.h:40
VarNames::invalidIndex
static const size_t invalidIndex
Returns a fixed variable offset that is always invalid.
Definition VarNames.h:100
VarNames::getIndex
size_t getIndex(const string &name) const
Returns VarNames::invalidIndex() if name is not known.
Definition VarNames.cpp:83