Repository navigation
Expand file tree
/
Copy pathscanner.cpp
More file actions
196 lines (164 loc) · 4.41 KB
/
Copy pathscanner.cpp
File metadata and controls
196 lines (164 loc) · 4.41 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
#include "scanner.h"
#include <utility>
#include "lox.h"
const std::unordered_map<std::string, token_type> scanner::keywords = {
{"and", AND},
{"class", CLASS},
{"else", ELSE},
{"false", FALSE},
{"for", FOR},
{"fun", FUN},
{"if", IF},
{"nil", NIL},
{"or", OR},
{"print", PRINT},
{"return", RETURN},
{"super", SUPER},
{"this", THIS},
{"true", TRUE},
{"var", VAR},
{"while", WHILE},
};
// helpers
bool scanner::is_at_end() const
{
return current >= source.length();
}
char scanner::advance()
{
return source[current++];
}
bool scanner::match(char expected)
{
if (is_at_end()) return false;
if (source[current] != expected) return false;
current++;
return true;
}
char scanner::peek() const
{
if (is_at_end()) return '\0';
return source[current];
}
char scanner::peek_next() const
{
if (current + 1 >= source.length()) return '\0';
return source[current + 1];
}
// token
std::vector<token> scanner::scan_tokens()
{
while (!is_at_end())
{
// We are at the beginning of the next lexeme.
start = current;
scan_token();
}
tokens.emplace_back(END_OF_FILE, "", "", line);
return tokens;
}
void scanner::scan_token()
{
char c = advance();
switch (c)
{
case '(': add_token(LEFT_PAREN); break;
case ')': add_token(RIGHT_PAREN); break;
case '{': add_token(LEFT_BRACE); break;
case '}': add_token(RIGHT_BRACE); break;
case ',': add_token(COMMA); break;
case '.': add_token(DOT); break;
case '-': add_token(MINUS); break;
case '+': add_token(PLUS); break;
case ';': add_token(SEMICOLON); break;
case '*': add_token(STAR); break;
case '!': add_token(match('=') ? BANG_EQUAL : BANG); break;
case '=': add_token(match('=') ? EQUAL_EQUAL : EQUAL); break;
case '<': add_token(match('=') ? LESS_EQUAL : LESS); break;
case '>': add_token(match('=') ? GREATER_EQUAL : GREATER); break;
case '/':
if (match('/')) { while (peek() != '\n' && !is_at_end()) advance(); }
else { add_token(SLASH); }
break;
case ' ':
case '\r':
case '\t':
break;
case '\n': line++; break;
// String Literals
case '"': string(); break;
// Digits (default case to avoid manual checking all 0-9 cases)
default:
// I used static_cast with std here, then defined is_digit later, this is just for future reference & cool
if (std::isdigit(static_cast<unsigned char>(c))) {
number();
} else if (is_alpha(c)) {
identifier();
} else {
error(line, "Unexpected character.");
}
}
}
void scanner::add_token(token_type type)
{
add_token(type, "");
}
void scanner::add_token(token_type type, const std::string& literal)
{
std::string text = source.substr(start, current - start);
tokens.emplace_back(type, text, literal, line);
}
// literal scanner helpers
void scanner::string()
{
while (peek() != '"' && !is_at_end()) {
if (peek() == '\n') line++;
advance();
}
if (is_at_end()) {
error(line, "Unterminated string.");
return;
}
// The closing ".
advance();
// Trim the surrounding quotes.
std::string value = source.substr(start + 1, current - start - 2);
add_token(STRING, value);
}
void scanner::number()
{
while (is_digit(peek())) advance();
// Look for a fractional part.
if (peek() == '.' && is_digit(peek_next())) {
// Consume the "."
advance();
while (is_digit(peek())) advance();
}
// TODO: add_token can actually take other types (in Java we used Object, in C++ ?)
// add_token(NUMBER, std::stod(source.substr(start, current - start)));
add_token(NUMBER, source.substr(start, current - start));
}
void scanner::identifier()
{
while (is_alphanumeric(peek())) advance();
std::string text = source.substr(start, current - start);
token_type type = IDENTIFIER;
auto it = keywords.find(text);
if (it != keywords.end()) type = it->second;
add_token(type);
}
// static character classification
bool scanner::is_digit(const char c)
{
return c >= '0' && c <= '9';
}
bool scanner::is_alpha(const char c)
{
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
bool scanner::is_alphanumeric(const char c)
{
return is_alpha(c) || is_digit(c);
}