-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.cpp
241 lines (213 loc) · 4.59 KB
/
scanner.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
/*
* scanner.cpp
* This file implements the Scanner class defined in scanner.h.
* The Scanner class is used to scan the source code and generate a list of tokens.
* It includes methods for scanning individual tokens, checking the next character in the source code, and checking if the end of the source code has been reached.
*/
#include "scanner.h"
#include "token_type_functions.h"
#include "error.h"
Scanner::Scanner(std::string source) : source(source) {}
std::vector<Token> Scanner::ScanTokens()
{
while (!IsAtEnd())
{
// We are at the beginning of the next lexeme.
start = current;
ScanToken();
}
return tokens;
}
void Scanner::ScanToken()
{
char c = Advance();
switch (c)
{
case '(':
AddToken(LEFT_PAREN);
break;
case ')':
AddToken(RIGHT_PAREN);
break;
case '{':
AddToken(LEFT_BRACE);
break;
case '}':
AddToken(RIGHT_BRACE);
break;
case ',':
AddToken(COMMA);
break;
case '.':
AddToken(DOT);
break;
case '-':
AddToken(MINUS);
break;
case '+':
AddToken(PLUS);
break;
case ';':
AddToken(SEMICOLON);
break;
case '*':
AddToken(STAR);
break;
case '!':
AddToken(Match('=') ? BANG_EQUAL : BANG);
break;
case '=':
AddToken(Match('=') ? EQUAL_EQUAL : EQUAL);
break;
case '<':
AddToken(Match('=') ? LESS_EQUAL : LESS);
break;
case '>':
AddToken(Match('=') ? GREATER_EQUAL : GREATER);
break;
case '/':
if (Match('/'))
{
while (Peek() != '\n' && !IsAtEnd())
Advance();
}
else
{
AddToken(SLASH);
}
break;
case ' ':
case '\r':
case '\t':
// Ignore whitespace.
break;
case '\n':
line++;
break;
case '"':
String();
break;
default:
if (IsDigit(c))
{
Number();
}
else if (IsAlpha(c))
{
Identifier();
}
else
{
Error::ReportError(line, "Unexpected character.");
}
break;
}
}
/*
* Returns the current character without consuming it.
* If the current character index is beyond the end of the source string, it returns a null character.
* Otherwise, it returns the current character.
*/
char Scanner::Peek()
{
if (IsAtEnd())
return '\0';
return source[current];
}
char Scanner::PeekNext()
{
if (current + 1 >= source.length())
return '\0';
return source[current + 1];
}
char Scanner::Advance()
{
current++;
return source[current - 1];
}
void Scanner::AddToken(TokenType type)
{
AddToken(type, nullptr);
}
void Scanner::AddToken(TokenType type, Object literal)
{
std::string text = source.substr(start, current - start);
tokens.push_back(Token(type, text, literal, line));
}
void Scanner::Number()
{
while (IsDigit(Peek()))
Advance();
// Look for a fractional part.
if (Peek() == '.' && IsDigit(PeekNext()))
{
// Consume the "."
Advance();
while (IsDigit(Peek()))
Advance();
}
AddToken(NUMBER, std::stod(source.substr(start, current - start)));
}
void Scanner::Identifier()
{
while (IsAlphaNumeric(Peek()))
Advance();
std::string text = source.substr(start, current - start);
TokenType type;
auto it = keywords.find(text);
if (it != keywords.end())
{
type = it->second;
}
else
{
type = TokenType::IDENTIFIER;
}
AddToken(type);
}
void Scanner::String()
{
while (Peek() != '"' && !IsAtEnd())
{
if (Peek() == '\n')
line++;
Advance();
}
if (IsAtEnd())
{
Error::ReportError(line, "Unterminated string.");
return;
}
// The closing ".
Advance();
// Trim the surrounding quotes.
std::string value = source.substr(start + 1, current - start - 2);
AddToken(STRING, value);
}
bool Scanner::IsDigit(char c)
{
return c >= '0' && c <= '9';
}
bool Scanner::Match(const char expected)
{
if (IsAtEnd())
return false;
if (source[current] != expected)
return false;
current++;
return true;
}
bool Scanner::IsAlpha(char c)
{
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
bool Scanner::IsAlphaNumeric(char c)
{
return IsAlpha(c) || IsDigit(c);
}
bool Scanner::IsAtEnd()
{
return current >= source.length();
}