-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
415 lines (368 loc) · 6.96 KB
/
scanner.cpp
File metadata and controls
415 lines (368 loc) · 6.96 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "scanner.h"
Scanner::Scanner (const std::string &code) : m_code (code) {}
std::ostream & Scanner::error ()
{
m_error = true;
std::cerr << "line " << m_line << ":";
return std::cerr;
}
/*
* EOF
*/
bool Scanner::is_at_end () const
{
return m_current >= static_cast<int> (m_code.size ());
}
/*
* Consume and advance to next character
*/
char Scanner::advance ()
{
return m_code[m_current++];
}
/*
* Peek at the current character
*/
char Scanner::peek () const
{
return is_at_end () ? '\0' : m_code[m_current];
}
/*
* Peek two chars ahead.
* This could be rolled into the peek function above, but is
* implemented as a separate function instead to clarify that this
* scanner only has lookahead 2 and no more.
*
*/
char Scanner::peek_next () const
{
return (m_code.size () - m_current) < 2 ? '\0' : m_code[m_current+1];
}
/*
* Consume character if it matches
*/
bool Scanner::match (char m)
{
if (is_at_end ())
{
return false;
}
if (m_code[m_current] != m)
{
return false;
}
/*
* Character matched, advance and return true
*/
++m_current;
return true;
}
/*
* Parse a string
*/
void Scanner::string ()
{
/*
* Remember line where the string started
*/
const auto from_line = m_line;
/*
* Search for the next quote, incrementing our line counter
* in the process.
*/
while (peek () != '"' && !is_at_end ())
{
if (peek () == '\n')
{
++m_line;
}
advance ();
}
/*
* Unterminated string check
*/
if (is_at_end ())
{
error () << "Unterminated string, started at line "
<< from_line << std::endl;
return;
}
/*
* Consume closing quote
*/
advance ();
/*
* Add token with surrounding quotes trimmed
*/
const auto s = m_code.substr (m_start+1, m_current - m_start-2);
add_token (TokenType::TOK_STRING, s);
}
bool Scanner::is_digit (char c) const
{
return c >= '0' && c <= '9';
}
bool Scanner::is_alpha (char c) const
{
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
bool Scanner::is_alphanumeric (char c) const
{
return is_digit (c) || is_alpha (c);
}
/*
* Parse number literal
* Valid numbers:
* 1
* 1.23
* Invalid numbers:
* .23
* 1223.
*/
void Scanner::number ()
{
/*
* Eat all the leading digits
*/
while (is_digit (peek ()))
{
advance ();
}
/*
* If the next is a period, followed by digits, keep eating
*/
if (peek () == '.' && is_digit (peek_next ()))
{
/*
* Consume the period
*/
advance ();
/*
* Consume all digits
*/
while (is_digit (peek ()))
{
advance ();
}
}
const auto f = std::atof (m_code.substr (m_start, m_current - m_start).c_str ());
add_token (TokenType::TOK_NUMBER, f);
}
/*
* Add identifier, which might be a keyword
*/
void Scanner::identifier ()
{
while (is_alphanumeric (peek ()))
{
advance ();
}
/*
* Extract text
*/
const auto text = m_code.substr (m_start, m_current - m_start);
/*
* Define keywords
*/
static const std::map<std::string, TokenType> keywords =
{{"and", TokenType::TOK_AND},
{"class", TokenType::TOK_CLASS},
{"else", TokenType::TOK_ELSE},
{"false", TokenType::TOK_FALSE},
{"for", TokenType::TOK_FOR},
{"fun", TokenType::TOK_FUN},
{"if", TokenType::TOK_IF},
{"nil", TokenType::TOK_NIL},
{"or", TokenType::TOK_OR},
{"print", TokenType::TOK_PRINT},
{"return", TokenType::TOK_RETURN},
{"super", TokenType::TOK_SUPER},
{"this", TokenType::TOK_THIS},
{"true", TokenType::TOK_TRUE},
{"var", TokenType::TOK_VAR},
{"while", TokenType::TOK_WHILE}};
/*
* If it is a keyword, add that, otherwise add identifier
*/
const auto it = keywords.find (text);
add_token (it == keywords.end () ?
TokenType::TOK_IDENTIFIER :
it->second);
}
/*
* Create token of type
*/
void Scanner::add_token (TokenType type)
{
m_tokens.push_back
(Token (type,
m_code.substr (m_start, m_current - m_start),
m_line));
}
/*
* Add token with value
*/
template <typename T>
void Scanner::add_token (TokenType type, T && v)
{
m_tokens.push_back
(Token (type,
m_code.substr (m_start, m_current - m_start),
m_line,
v));
}
/*
* Find next token
*/
void Scanner::scan_token () {
/*
* Read one character
*/
char c = advance ();
switch (c) {
/*
* Single char tokens
*/
case '(':
add_token (TokenType::TOK_LEFT_PAREN);
break;
case ')':
add_token (TokenType::TOK_RIGHT_PAREN);
break;
case '{':
add_token (TokenType::TOK_LEFT_BRACE);
break;
case '}':
add_token (TokenType::TOK_RIGHT_BRACE);
break;
case ',':
add_token (TokenType::TOK_COMMA);
break;
case '.':
add_token (TokenType::TOK_DOT);
break;
case '-':
add_token (TokenType::TOK_MINUS);
break;
case '+':
add_token (TokenType::TOK_PLUS);
break;
case ';':
add_token (TokenType::TOK_SEMICOLON);
break;
case '*':
add_token (TokenType::TOK_STAR);
break;
/*
* Single-or-two-char tokens
*/
case '!':
add_token (match ('=') ?
TokenType::TOK_BANG_EQUAL :
TokenType::TOK_BANG);
break;
case '=':
add_token (match ('=') ?
TokenType::TOK_EQUAL_EQUAL :
TokenType::TOK_EQUAL);
break;
case '<':
add_token (match ('=') ?
TokenType::TOK_LESS_EQUAL :
TokenType::TOK_LESS);
break;
case '>':
add_token (match ('=') ?
TokenType::TOK_GREATER_EQUAL :
TokenType::TOK_GREATER);
break;
/*
* Division '/' or comment '// ...'
*/
case '/':
/*
* If it is a comment, consume to the end of the line
*/
if (match ('/'))
{
while (peek () != '\n' && !is_at_end ())
{
advance ();
}
}
else
{
/*
* Was actually just /, not a comment
*/
add_token (TokenType::TOK_SLASH);
}
break;
/*
* Whitespace
*/
case ' ':
case '\r':
case '\t':
break;
/*
* Newline
*/
case '\n':
m_line++;
break;
/*
* Strings
*/
case '"':
string ();
break;
/*
* Number literals
*/
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
number ();
break;
default:
/*
* Identifiers or keywords
*/
if (is_alpha (c))
{
identifier ();
}
else
{
/*
* Error case
*/
error () << "Unexpected Character '" << c << "'" << std::endl;
}
}
}
/*
* Convert text to tokens
*/
std::vector<Token> Scanner::scan_tokens()
{
while (!is_at_end ())
{
m_start = m_current;
scan_token ();
}
/*
* Final EOF token to indicate end-of-text
*/
m_tokens.push_back (Token (TokenType::TOK_EOF, "", m_line));
return m_tokens;
}