-
Notifications
You must be signed in to change notification settings - Fork 3
/
tokenizer.hpp
96 lines (77 loc) · 1.54 KB
/
tokenizer.hpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef tokenizer_hpp
#define tokenizer_hpp
/*! \file
\brief Tokenizer class for parsing .dsc files
*/
#include <iostream>
#include <string>
using std::istream;
using std::string;
//! Tokenizer class for parsing .dsc files
class Tokenizer
{
public:
//! input stream
istream & in;
//! current line number
int lineNo;
//! current token
string token;
//! Construct with an input stream
Tokenizer(istream & in_) : in(in_), lineNo(1) { }
//! retrieve current token as an integer
int getInt()
{
return atoi(token.c_str());
}
//! retrieve current token as a double
double getDouble()
{
return atof(token.c_str());
}
//! skip over whitespace and comments
bool eatWhite()
{
bool inComment = false;
for(;;)
{
int c = in.peek();
bool isEof = c == EOF;
bool isSpace = c <= 32;
bool isLine = c == '\n';
bool isComment = c == '#';
if (isLine) ++lineNo;
if (isEof)
return false;
else if (inComment)
{
if (isLine) inComment = false;
in.get();
}
else if (isComment)
{
inComment = true;
in.get();
}
else if (isSpace)
in.get();
else
return true;
}
}
//! get next token value, or return false if at the end of the file
bool next()
{
token = "";
if (!eatWhite()) return false;
for(;;)
{
int c = in.peek();
bool isSpace = c <= 32;
if (isSpace) return true;
token += (char)c;
in.get();
}
}
};
#endif