-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.cpp
181 lines (158 loc) · 4.7 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
#include "scanner.hpp"
const std::unordered_map<std::string_view, TokenType> Scanner::s_keywords{
{"and", TokenType::AND},
{"class", TokenType::CLASS},
{"else", TokenType::ELSE},
{"false", TokenType::FALSE},
{"for", TokenType::FOR},
{"fun", TokenType::FUN},
{"if", TokenType::IF},
{"nil", TokenType::NIL},
{"or", TokenType::OR},
{"print", TokenType::PRINT},
{"return", TokenType::RETURN},
{"super", TokenType::SUPER},
{"this", TokenType::THIS},
{"true", TokenType::TRUE},
{"var", TokenType::VAR},
{"while", TokenType::WHILE}
};
Scanner::Scanner(const char* source) {
m_start = source;
m_current = source;
m_line = 1;
}
Token Scanner::scan_token() {
skip_whitespace();
m_start = m_current;
if (is_at_end()) return make_token(TokenType::END_OF_FILE);
char c = advance();
if (is_alpha_or_underscore(c)) return identifier();
if (is_digit(c)) return number();
switch (c) {
case '(': return make_token(TokenType::LEFT_PAREN);
case ')': return make_token(TokenType::RIGHT_PAREN);
case '{': return make_token(TokenType::LEFT_BRACE);
case '}': return make_token(TokenType::RIGHT_BRACE);
case ';': return make_token(TokenType::SEMICOLON);
case ',': return make_token(TokenType::COMMA);
case '.': return make_token(TokenType::DOT);
case '-': return make_token(TokenType::MINUS);
case '+': return make_token(TokenType::PLUS);
case '/': return make_token(TokenType::SLASH);
case '*': return make_token(TokenType::STAR);
case '!':
return make_token(
match('=') ? TokenType::BANG_EQUAL : TokenType::BANG);
case '=':
return make_token(
match('=') ? TokenType::EQUAL_EQUAL : TokenType::EQUAL);
case '<':
return make_token(
match('=') ? TokenType::LESS_EQUAL : TokenType::LESS);
case '>':
return make_token(
match('=') ? TokenType::GREATER_EQUAL : TokenType::GREATER);
case '"':
return string();
default:
// Break and return error token below
break;
}
return error_token("Unexpected character.");
}
bool Scanner::is_alpha_or_underscore(char c) {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
char Scanner::advance() {
m_current++;
return m_current[-1];
}
char Scanner::peek_next() {
if (is_at_end()) return '\0';
return m_current[1];
}
bool Scanner::match(char expected) {
if(is_at_end()) return false;
if (*m_current != expected) return false;
m_current++;
return true;
}
Token Scanner::make_token(TokenType type) {
Token token;
token.type = type;
token.start = m_start;
token.length = token_len();
token.line = m_line;
return token;
}
Token Scanner::error_token(const char* message) {
Token token;
token.type = TokenType::ERROR;
token.start = message;
token.length = strlen(message);
token.line = m_line;
return token;
}
void Scanner::skip_whitespace() {
for (;;) {
char c = peek();
switch (c) {
case ' ':
case '\r':
case '\t':
advance();
break;
case '\n':
m_line++;
advance();
break;
case '/':
if (peek_next() == '/') {
// A comment goes until the end of the line
while (peek() != '\n' && !is_at_end())
advance();
} else {
return;
}
break;
default:
return;
}
}
}
TokenType Scanner::identifier_type() {
auto it = s_keywords.find(std::string_view(m_start, token_len()));
if (it != s_keywords.end()) {
return it->second;
}
return TokenType::IDENTIFIER;
}
Token Scanner::identifier() {
while (is_alpha_or_underscore(peek()) || is_digit(peek()))
advance();
return make_token(identifier_type());
}
Token Scanner::number() {
while (Scanner::is_digit(peek()))
advance();
// Look for a fractional part
if (peek() == '.' && Scanner::is_digit(peek_next())) {
// Consume the "."
advance();
while (is_digit(peek())) advance();
}
return make_token(TokenType::NUMBER);
}
Token Scanner::string() {
while (peek() != '"' && !is_at_end()) {
if (peek() == '\n') m_line++;
advance();
}
if (is_at_end()) return error_token("Unterminated string.");
// The closing quote
advance();
return make_token(TokenType::STRING);
}