-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.h
94 lines (73 loc) · 2.36 KB
/
tokenizer.h
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
/*
* Copyright CERN 2016
* @author Maciej Suminski (maciej.suminski@cern.ch)
*
* 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 3 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 TOKENIZER_H
#define TOKENIZER_H
#include <fstream>
#include <string>
#include <cassert>
class Tokenizer {
public:
Tokenizer(const std::string&filename);
~Tokenizer();
/*
* @brief Gets a token (an array of characters without any whitespaces).
* @param dest is a pointer that will be set to the obtained token or NULL
* in case of failure.
* @return int number of read characters.
*/
int get(char*&dest);
/*
* @brief Gets a token, compares with the expected one and returns
* the comparison result (true if there is a match).
* @param token is the expected token.
*/
bool expect(const char*token);
/*
* @brief Returns the current token, without reading another one.
*/
inline const char*current() const {
return buf_cur_;
}
inline int line_number() const {
return line_number_;
}
inline bool valid() const {
return file_.good();
}
private:
// Loads another line if the buffer is empty
bool fill_if_empty();
// Strip whitespaces from the buffer beginning
void skip_whitespace();
/*
* @brief Doubles the buffer size, preserves the buffer contents.
* @return Pointer to the empty memory chunk
*/
char*inc_buffer();
// Handle to the processed file
std::ifstream file_;
// Current line number in the processed file
int line_number_;
// Buffer to store the currently processed line
char*buf_;
// Pointer to the current token
char*buf_cur_;
// Pointer to the next token
char*buf_ptr_;
// Current buffer size
int buf_size_;
};
#endif /* TOKENIZER_H */