-
Notifications
You must be signed in to change notification settings - Fork 0
/
jack_tokenizer.cpp
361 lines (311 loc) · 8.08 KB
/
jack_tokenizer.cpp
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <sstream>
#include <cctype>
#include <boost/lexical_cast.hpp>
#include "jack_tokenizer.h"
using namespace std;
JackTokenizer::JackTokenizer(string jackcode)
:m_jackStreamCode(jackcode)
{
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_CLASS, "class") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_CONSTRUCTOR, "constructor") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_FUNCTION, "function") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_METHOD, "method") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_FIELD, "field") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_STATIC, "static") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_VAR, "var") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_INT, "int") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_CHAR, "char") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_BOOLEAN, "boolean") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_VOID, "void") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_TRUE, "true") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_FALSE, "false") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_NULL, "null") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_THIS, "this") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_LET, "let") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_DO, "do") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_IF, "if") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_ELSE, "else") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_WHILE, "while") );
m_keywords.insert( pair<TYPE_KEYWORD, string>(KW_RETURN, "return") );
m_symbols.push_back('{'); m_symbols.push_back('}');
m_symbols.push_back('('); m_symbols.push_back(')');
m_symbols.push_back('['); m_symbols.push_back(']');
m_symbols.push_back('.');
m_symbols.push_back(',');
m_symbols.push_back(';');
m_symbols.push_back('+');
m_symbols.push_back('-');
m_symbols.push_back('*');
m_symbols.push_back('/');
m_symbols.push_back('&');
m_symbols.push_back('|');
m_symbols.push_back('<');
m_symbols.push_back('>');
m_symbols.push_back('=');
m_symbols.push_back('~');
m_curLine = 1;
m_curCol = 1;
}
int JackTokenizer::getCurrentLine()
{
return m_curLine;
}
int JackTokenizer::getCurrentColumn()
{
return m_curCol - m_currentToken.size();
}
bool JackTokenizer::hasMoreTokens()
{
return m_jackStreamCode.good();
}
void JackTokenizer::advance()
{
// first, reset the current token to an empty string
m_currentToken = "";
/* we read the code one character at a time
storing his value in a string buffer (stringbuf).
if the char is a separator (space or line feed),
we skip the function storing the current token as :
- an empty string if buffer_size = 0;
- the buffer value.
if the char is a symbol, it returns that value or
the concatened buffer.
if the char is equal to '/' then if it's followed by :
- another slash, we skipped chars
until we found a separator char ('\n'); it's a line comment
- a star, we try to reach the end of that block comment
and erase it from the stream
- something else, it's stored as a symbol
*/
char c;
stringbuf buffer;
while ( hasMoreTokens() )
{
// read one char
c = m_jackStreamCode.get();
m_curCol ++; // update pointer position
string s = buffer.str();
// remove separators
// i could use isspace() => stdlib
if (c == ' ' || c == '\t' || c == '\n')
{
if ( s.size() > 0 )
{
m_currentToken = s;
m_jackStreamCode.unget();
m_curCol--;
return;
}
else if (c == '\n')
{
m_curLine++; m_curCol = 1; // update pointer position
}
continue;
}
// remove comments
if ( c == '/' )
{
// read the next char
char next_c = m_jackStreamCode.get();
m_curCol++; // update pointer position
if (next_c == '/')
{
// remove 1 line
char trash;
while ( hasMoreTokens() )
{
trash = m_jackStreamCode.get();
if (trash == '\n')
{
break;
}
}
m_curLine++; m_curCol = 1; // update pointer position
continue;
}
else if (next_c == '*')
{
// remove all chars until we found '*/'
char c1=0;
char c2=0;
bool found = false;
while ( hasMoreTokens() )
{
c1 = c2;
c2 = m_jackStreamCode.get();
// update pointer position
if (c2 == '\n')
{
m_curLine++; m_curCol = 1;
}
else
{
m_curCol++;
}
if (c1 == '*' && c2 == '/')
{
found = true;
break;
}
}
if (found) continue;
}
else
{
// put back next_c as the current char
m_jackStreamCode.unget();
m_curCol--; // update pointer position
}
}
// there's a special issue with string constant,
// we need to parse the entire string inside the quotes
if ( c == '"' )
{
char next_c;
while ( hasMoreTokens() )
{
next_c = m_jackStreamCode.get();
// update pointer position
if (next_c == '\n')
{
cerr << "At line " << getCurrentLine() << ", column " << getCurrentColumn() << " : ";
cerr << "Escape character is not allowed in a string constant" << endl;
exit(-1);
}
else
{
m_curCol++;
}
if (next_c == '"')
{
break;
}
buffer.sputc(next_c);
}
m_currentToken = '"' + buffer.str() + '"';
return;
}
// check if the character is a symbol
// i could use strpbrk http://www.cplusplus.com/reference/clibrary/cstring/strpbrk/
typedef vector<char>::iterator vec_it;
for (vec_it it = m_symbols.begin(), it_end = m_symbols.end();
it != it_end;
++it)
{
char symbol = *it;
if (c == symbol)
{
if ( s.size() > 0 )
{
m_currentToken = s;
// get the actual buffer value and
// return to the previous position in the stream (next time, we'll capture this symbol)
m_jackStreamCode.unget();
m_curCol--; // update pointer position
return;
}
else
{
m_currentToken = c;
return;
}
}
}
// append the current char to the buffer
buffer.sputc(c);
}
}
pair<TYPE_TOKEN, string> JackTokenizer::peek()
{
// save the current token
string curToken = m_currentToken;
advance();
int rewind = m_currentToken.size();
for (int i=0; i < rewind; i++)
{
m_jackStreamCode.unget();
m_curCol--;
}
pair<TYPE_TOKEN, string> token_value(tokenType(), m_currentToken);
// restore the current token as if nothing happened
m_currentToken = curToken;
return token_value;
}
TYPE_TOKEN JackTokenizer::tokenType()
{
if (m_currentToken.size() == 0)
return TOK_EMPTY;
typedef map<TYPE_KEYWORD, string>::iterator keys_it;
typedef vector<char>::iterator sym_it;
// check if the current token is:
// a keyword
for (keys_it it = m_keywords.begin(), it_end = m_keywords.end();
it != it_end;
++it)
{
if (m_currentToken == it->second)
return TOK_KEYWORD;
}
// a symbol
for (sym_it it = m_symbols.begin(), it_end = m_symbols.end();
it != it_end;
++it)
{
string str;
char c = *it;
str = c;
if (m_currentToken == str)
return TOK_SYMBOL;
}
// an integer constant
if (isdigit( m_currentToken[0] ))
return TOK_INT_CONST;
// a string constant
if (m_currentToken[0] == '"')
return TOK_STRING_CONST;
// it must be an identifier
return TOK_IDENTIFIER;
}
TYPE_KEYWORD JackTokenizer::keyword()
{
typedef map<TYPE_KEYWORD, string>::iterator keys_it;
for (keys_it it = m_keywords.begin(), it_end = m_keywords.end();
it != it_end;
++it)
{
if (m_currentToken == it->second)
return it->first;
}
throw TokenMismatch( "keyword" );
}
char JackTokenizer::symbol()
{
if (m_currentToken.size() == 1)
return m_currentToken[0];
else throw TokenMismatch( "symbol" );
}
string JackTokenizer::identifier()
{
if (m_currentToken.size() > 0)
return m_currentToken;
else throw TokenMismatch( "identifier" );
}
int JackTokenizer::intVal()
{
int num = 0;
try
{
num = boost::lexical_cast<int>( m_currentToken );
}
catch (const boost::bad_lexical_cast &e)
{
cout << e.what() << endl;
}
return num;
}
string JackTokenizer::stringVal()
{
if (m_currentToken.size() > 2 && m_currentToken[0] == '"')
return m_currentToken.substr(1, m_currentToken.size()-2);
else throw TokenMismatch( "string" );
}