-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenizer.hpp
256 lines (214 loc) · 5.42 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef TOKENIZER_HPP
#define TOKENIZER_HPP
#include <regex>
#include <stdexcept>
#include <string>
#include <vector>
#include "Token.hpp"
class Tokenizer
{
public:
Tokenizer()
{
}
~Tokenizer()
{
ClearTokens();
}
friend std::ostream& operator<<(std::ostream& os, const Tokenizer& t)
{
if (!t.tokens.empty())
{
for (auto itr = t.tokens.begin(); itr != t.tokens.end(); itr++)
{
os << "Object {" << std::endl;
os << "\t";
switch ((*itr)->type)
{
case Token::Type::NIL:
os << "type: NIL";
break;
case Token::Type::PAREN:
os << "type: PAREN";
break;
case Token::Type::NUM:
os << "type: NUM";
break;
case Token::Type::NAME:
os << "type: NAME";
break;
case Token::Type::STRING:
os << "type: STRING";
break;
default:
break;
}
os << std::endl << "\tvalue: " << (*itr)->value << std::endl;
os << "}" << std::endl;
}
}
return os;
}
void ClearTokens()
{
if (!tokens.empty())
{
for (auto itr = tokens.begin(); itr != tokens.end(); itr++)
{
delete (*itr);
(*itr) = nullptr;
}
tokens.clear();
}
}
void Tokenize(std::string const& input)
{
// Clear the tokens
ClearTokens();
// Keep track of where we are in the input
int current = 0;
// Go through the input one character at a time
while (current < (int)input.length())
{
// Counts the number of characters read from the input
int consumedCharacters = 1;
// Skip whitespace
if (!IsWhitespace(input, current))
{
// Create a pointer to store token in
Token* tok = nullptr;
// Find the correct token
if ((tok = TokenizeParenOpen(input, current)) != nullptr)
{
consumedCharacters = 1;
}
else if ((tok = TokenizeParenClose(input, current)) != nullptr)
{
consumedCharacters = 1;
}
else if ((tok = TokenizeString(input, current)) != nullptr)
{
// Add to to account for the opening and closing "
consumedCharacters = tok->length + 2;
}
else if ((tok = TokenizeNumber(input, current)) != nullptr)
{
consumedCharacters = tok->length;
}
else if ((tok = TokenizeName(input, current)) != nullptr)
{
consumedCharacters = tok->length;
}
else
{
throw std::invalid_argument("Unknown character.");
}
// Add the token to the tokens vector
tokens.push_back(tok);
}
// Increment the current
current += consumedCharacters;
}
}
public:
// Holds the tokens
std::vector<Token*> tokens;
private:
// Generic function that tokenizes a single character
Token * TokenizeCharacter(Token::Type type, std::string value, std::string const& input, int current)
{
// If the input matches the value
if (input.substr(current, 1) == value)
{
// Create a token to return
Token* tok = new Token(type, value);
return tok;
}
return nullptr;
}
// Tokenizer for open parenthesis (
Token* TokenizeParenOpen(std::string const& input, int current)
{
return TokenizeCharacter(Token::Type::PAREN, "(", input, current);
}
// Tokenizer for open parenthesis (
Token* TokenizeParenClose(std::string const& input, int current)
{
return TokenizeCharacter(Token::Type::PAREN, ")", input, current);
}
// Multiple character tokens
Token* TokenizePattern(Token::Type type, std::string pattern, std::string const& input, int current)
{
// Get the first character of the input
std::string character = input.substr(current, 1);
// Use this to keep track of the size of the value
int consumedCharacters = 0;
// Regex
std::regex r(pattern);
if (std::regex_match(character, r))
{
// Store the value of the token
std::string tokenValue;
while (std::regex_match(character, r))
{
// Append the character to the token value
tokenValue.append(character);
// Increment the count
consumedCharacters++;
// Get the next character in the input to test
character = input.substr(current + consumedCharacters, 1);
}
// Create a token to return
Token* tok = new Token(type, tokenValue);
return tok;
}
return nullptr;
}
// Tokenizer for numbers
Token* TokenizeNumber(std::string const& input, int current)
{
return TokenizePattern(Token::Type::NUM, "[0-9]", input, current);
}
// Tokenizer for names
Token* TokenizeName(std::string const& input, int current)
{
return TokenizePattern(Token::Type::NAME, "[A-Za-z]", input, current);
}
// Tokenizer for strings
Token* TokenizeString(std::string const& input, int current)
{
// Strings must start with "
if (input[current] == '"')
{
// Move the input to the next character to check
int consumedCharacters = 1;
char character = input[current + consumedCharacters];
// Use to build up string for token's value
std::string tokenValue = "";
// Loop until end of string is found
while (character != '"')
{
// If there is no end of the string
if (character == input.back())
{
throw std::invalid_argument("Unterminated string.");
}
// Add the character to the value
tokenValue += character;
// Move to the next character
consumedCharacters++;
character = input[current + consumedCharacters];
}
// Create a token to return
Token* tok = new Token(Token::Type::STRING, tokenValue);
return tok;
}
return nullptr;
}
// Skip whitespace
bool IsWhitespace(std::string const& input, int current)
{
return (input.substr(current, 1) == " ") ? true : false;
}
};
#endif