Frobby 0.10.0
Loading...
Searching...
No Matches
Scanner.h
1/* Frobby: Software for monomial ideal computations.
2 Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see http://www.gnu.org/licenses/.
16*/
17#ifndef SCANNER_GUARD
18#define SCANNER_GUARD
19
20#include "VarNames.h"
21#include <string>
22
23class IOHandler;
24
50class Scanner {
51public:
58 Scanner(const string& formatName, FILE* in);
59 ~Scanner() {delete[] _tmpString;}
60
61 const string& getFormat() const {return _formatName;}
62 void setFormat(const string& format) {_formatName = format;}
63 unique_ptr<IOHandler> createIOHandler() const;
64
67 bool match(char c);
68
70 bool matchEOF();
71
74 void expect(char expected);
75
78 void expect(char a, char b);
79
82 void expect(const char* str);
83
86 void expect(const string& str) {expect(str.c_str());}
87
89 void expectEOF();
90
93
95 void readInteger(mpz_class& integer);
96
98 void readIntegerNoSign(string& str);
99
101 void readIntegerNoSign(mpz_class& str);
102
106 void readIntegerAndNegativeAsZero(mpz_class& integer);
107
111 void readIntegerAndNegativeAsZero(std::string& integer);
112
116 void readSizeT(size_t& size);
117
120 const char* readIdentifier();
121
123 void readIdentifier(string& str);
124
129 size_t readVariable(const VarNames& names);
130
133 bool peekIdentifier();
134
137 bool peekWhite() {return isspace(peek());}
138
141 bool peek(char character);
142
145 unsigned int getLineNumber() const {return _lineNumber;}
146
148 int peek() {return _char;}
149
152 inline void eatWhite();
153
154private:
156 size_t readIntegerString();
157
159 size_t readIntegerStringNoSign();
160
161 void parseInteger(mpz_class& integer, size_t size);
162
163 void errorExpectTwo(char a, char b, int got);
164 void errorExpectOne(char expected, int got);
165 void errorReadVariable(const char* name);
166 void errorReadIdentifier();
167
168 void reportErrorUnexpectedToken(const string& expected, int got);
169 void reportErrorUnexpectedToken(const string& expected, const string& got);
170
171 inline int getChar();
172 void growTmpString();
173 int readBuffer();
174
175 mpz_class _integer;
176 FILE* _in;
177 unsigned long _lineNumber;
178 int _char; // next character on stream
179
180 char* _tmpString;
181 size_t _tmpStringCapacity;
182
183 string _formatName;
184
185 vector<char> _buffer;
186 vector<char>::iterator _bufferPos;
187};
188
189
190
191inline void Scanner::readIdentifier(string& str) {
192 eatWhite();
193 if (!isalpha(peek()))
194 errorReadIdentifier();
195 str.clear();
196 do {
197 str += static_cast<char>(getChar());
198 } while (isalnum(peek()) || peek() == '_');
199}
200
201inline size_t Scanner::readVariable(const VarNames& names) {
202 const char* name = readIdentifier();
203 size_t var = names.getIndex(name);
204 if (var == VarNames::invalidIndex)
205 errorReadVariable(name);
206 return var;
207}
208
209
210inline bool Scanner::matchEOF() {
211 eatWhite();
212 return peek() == EOF;
213}
214
215inline bool Scanner::match(char c) {
216 eatWhite();
217 if (c == peek()) {
218 getChar();
219 return true;
220 } else
221 return false;
222}
223
224inline void Scanner::expect(char a, char b) {
225 eatWhite();
226 int got = getChar();
227 if (got != a && got != b)
228 errorExpectTwo(a, b, got);
229}
230
231inline void Scanner::expect(char expected) {
232 eatWhite();
233 int got = getChar();
234 if (got != expected)
235 errorExpectOne(expected, got);
236}
237
238inline void Scanner::readInteger(mpz_class& integer) {
239 size_t size = readIntegerString();
240 parseInteger(integer, size);
241}
242
244 readIntegerStringNoSign();
245}
246
247inline void Scanner::readIntegerNoSign(mpz_class& integer) {
248 readIntegerStringNoSign();
249 integer = _tmpString;
250}
251
252inline void Scanner::readIntegerNoSign(string& integer) {
253 readIntegerStringNoSign();
254 integer = _tmpString;
255}
256
258 eatWhite();
259 return isalpha(peek());
260}
261
262inline bool Scanner::peek(char character) {
263 eatWhite();
264 return peek() == character;
265}
266
267inline void Scanner::eatWhite() {
268 while (isspace(peek()))
269 getChar();
270}
271
272inline int Scanner::getChar() {
273 if (_char == '\n')
274 ++_lineNumber;
275 int oldChar = _char;
276 if (_bufferPos == _buffer.end())
277 _char = readBuffer();
278 else {
279 _char = *_bufferPos;
280 ++_bufferPos;
281 }
282 return oldChar;
283}
284
285#endif
Definition IOHandler.h:41
void expect(const string &str)
Definition Scanner.h:86
int peek()
Definition Scanner.h:148
void readIntegerNoSign(string &str)
Definition Scanner.h:252
Scanner(const string &formatName, FILE *in)
Definition Scanner.cpp:28
void eatWhite()
Definition Scanner.h:267
void expectEOF()
Definition Scanner.cpp:77
bool peekWhite()
Definition Scanner.h:137
void readIntegerAndNegativeAsZero(mpz_class &integer)
Definition Scanner.cpp:171
void expectIntegerNoSign()
Definition Scanner.h:243
void expect(char expected)
Definition Scanner.h:231
const char * readIdentifier()
Definition Scanner.cpp:255
bool peekIdentifier()
Definition Scanner.h:257
void readIntegerAndNegativeAsZero(std::string &integer)
unsigned int getLineNumber() const
Definition Scanner.h:145
void readSizeT(size_t &size)
Definition Scanner.cpp:205
size_t readVariable(const VarNames &names)
Definition Scanner.h:201
bool matchEOF()
Definition Scanner.h:210
void readInteger(mpz_class &integer)
Definition Scanner.h:238
bool match(char c)
Definition Scanner.h:215
Definition VarNames.h:38
static const size_t invalidIndex
Definition VarNames.h:98
size_t getIndex(const string &name) const
Definition VarNames.cpp:83