-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokeniser.cxx
43 lines (41 loc) · 914 Bytes
/
tokeniser.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "tokeniser.hxx"
#include <iostream>
#define debug_msg(x) std::cout << (x) << std::endl;
tokeniser::tokeniser(std::shared_ptr<std::ifstream> fh): filehandle(fh){
}
bool tokeniser::TryGetNextToken(tokens& token) throw (fileReadFailureException){
if (!filehandle->is_open()) {
debug_msg("could not open file")
fileReadFailureException ex;
throw ex;
}
bool foundtoken = false;
while (filehandle->good() && !foundtoken){
char ch;
ch = filehandle->get();
switch(ch){
case ' ':
token = tokens::space;
foundtoken = true;
break;
case '\t':
token = tokens::tab;
foundtoken = true;
break;
case '\n':
token = tokens::linefeed;
foundtoken = true;
break;
default:
//eat char
break;
}
}
if (filehandle->bad()) {
debug_msg("filehandle failed")
fileReadFailureException ex;
throw ex;
}
//false if eof as failure has thrown by this point
return filehandle->good();
}