-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathError.cpp
60 lines (51 loc) · 1.63 KB
/
Error.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
//
// Created by Aadi and Michael on 7/24/22.
//
#include <Error.h>
Error::Error(Position* posStart, Position* posEnd, std::string name, std::string message) {
this->posStart = posStart->copy();
this->posEnd = posEnd->copy();
this->posEnd->advance();
this->name = std::move(name);
this->message = std::move(message);
}
static std::vector<std::string> splitString(const std::string& str) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, '\n')) {
tokens.push_back(token);
}
return tokens;
}
std::string Error::toString() {
std::string s = name + ": " + message + "\n";
s += "File: " + *posStart->fName + ":" + std::to_string(posStart->line + 1) + ":" + std::to_string(posStart->col + 1) + "\n";
s += "\n\n" + addArrows(*posStart->fText);
return s;
}
std::string Error::addArrows(const std::string& text) const {
// Add '^' under the string s from the start index to the end index
std::vector<std::string> splits = splitString(text);
std::stringstream ss;
for (int i = 0; i < posStart->col; i++) {
ss << ' ';
}
for (int i = posStart->col; i < posEnd->col - 1; i++) {
ss << '^';
}
int line = posStart->line;
splits.insert(splits.begin() + line + 1, ss.str());
std::stringstream final;
for (int i = line; i <= posEnd->line + 1; i++) {
final << splits[i] << '\n';
}
return final.str();
}
Error::Error(Token* tok, std::string name, std::string message) {
this->posStart = tok->posStart->copy();
this->posEnd = tok->posEnd->copy();
this->posEnd->advance();
this->name = std::move(name);
this->message = std::move(message);
}