Repository navigation
Expand file tree
/
Copy pathScanner.cpp
More file actions
164 lines (140 loc) · 4.63 KB
/
Copy pathScanner.cpp
File metadata and controls
164 lines (140 loc) · 4.63 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
#include "Scanner.h"
#include "Utils.h"
#include "Number.hpp"
#include <format>
Scanner::Scanner(const std::string &source): source(source) {
keywords.insert({
{"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() {}
void Scanner::addToken(const TokenType tokenType) {
addToken(tokenType, std::make_shared<Object>(std::monostate{}));
}
void Scanner::addToken(TokenType tokenType, ObjPtr literal) {
std::string text = source.substr(startPos, currentPos - startPos);
tokens.push_back({tokenType, text, literal, line, startPos});
}
bool Scanner::isAtEnd() const {
return currentPos >= source.size();
}
std::vector<Token>& Scanner::scanTokens() {
while (!isAtEnd()) {
startPos = currentPos;
scanToken();
}
tokens.push_back(Token{TokenType::END_OF_FILE, "", std::make_shared<Object>(std::monostate{}), line, startPos});
return tokens;
}
void Scanner::scanToken() {
switch (char c = advance()) {
case '(' : addToken(TokenType::LEFT_PAREN); break;
case ')' : addToken(TokenType::RIGHT_PAREN); break;
case '{' : addToken(TokenType::LEFT_BRACE); break;
case '}' : addToken(TokenType::RIGHT_BRACE); break;
case ',' : addToken(TokenType::COMMA); break;
case '.' : addToken(TokenType::DOT); break;
case '-' : addToken(TokenType::MINUS); break;
case '+' : addToken(TokenType::PLUS); break;
case ';' : addToken(TokenType::SEMICOLON); break;
case '*' : addToken(TokenType::STAR); break;
case '!' : addToken(match('=') ? TokenType::BANG_EQUAL : TokenType::BANG); break;
case '=' : addToken(match('=') ? TokenType::EQUAL_EQUAL : TokenType::EQUAL); break;
case '<' : addToken(match('=') ? TokenType::LESS_EQUAL : TokenType::LESS); break;
case '>' : addToken(match('=') ? TokenType::GREATER_EQUAL : TokenType::GREATER); break;
case '/' : if (match('/')) {while (peek() != '\n' && !isAtEnd()) advance();} else {if (match('*')) {comment();} else {addToken(TokenType::SLASH);}} break;
case ' ':
case '\r':
case '\t': break;
case '\n': line++; break;
case '"' : string(); break;
default:
if (Utils::is_digit(c)) {
number();
break;
}
if (Utils::is_alpha(c)) {
identifier();
break;
}
Utils::error(line, std::format("Unexpected character: {}.", c));
hadError = true;
break;
}
}
char Scanner::advance() {
return source[currentPos++];
}
bool Scanner::match(const char expected) {
if (isAtEnd()) return false;
if (source[currentPos] != expected) return false;
currentPos++;
return true;
}
char Scanner::peek(const int i) const {
if ((currentPos+i) >= source.size()) return '\0';
return source[currentPos+i];
}
void Scanner::string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++;
advance();
}
if (isAtEnd()) {
Utils::error(line-1, "Unterminated string.");
return;
}
// The closing ".
advance();
std::string value = source.substr(startPos + 1, currentPos - startPos - 2);
addToken(TokenType::STRING, std::make_shared<Object>(value));
}
void Scanner::number() {
bool has_dot = false;
while (Utils::is_digit(peek())) advance();
if (peek() == '.' && Utils::is_digit(peek(1))) {
has_dot = true;
// Consume the "."
advance();
while (Utils::is_digit(peek())) advance();
}
if (!has_dot) {
int64_t value;
auto s = source.substr(startPos, currentPos - startPos);
auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), value);
if (ec == std::errc() && ptr == s.data() + s.size()) {
addToken(TokenType::NUMBER, std::make_shared<Object>(Number(value)));
return;
}
}
addToken(TokenType::NUMBER, std::make_shared<Object>(Number(std::stod(source.substr(startPos, currentPos - startPos)))));
}
void Scanner::identifier() {
while (Utils::is_alphaNumeric(peek())) advance();
const std::string value = source.substr(startPos, currentPos - startPos);
const auto it = keywords.find(value);
const auto tokenType = (it != keywords.end()) ? it->second : TokenType::IDENTIFIER;
addToken(tokenType);
}
void Scanner::comment() {
while (peek() != '*' or peek(1) != '/')
advance();
// Consume the "*/"
advance();
advance();
}