This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPrettifyException.hpp
76 lines (62 loc) · 1.69 KB
/
PrettifyException.hpp
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
#ifndef CALCULATOR_PRETTIFYEXCEPTION_HPP
#define CALCULATOR_PRETTIFYEXCEPTION_HPP
#include "CalcASTException.hpp"
#include <string>
std::wstring prettify_exception(const std::wstring & input,
const CalcASTException & exception) {
/**
* Amount of characters on either side of the error location to show
*
* This is simply an `unsigned long` because the [[std::wstring]] methods
* use it.
*/
unsigned long context = 5;
/**
* PBKAC: Problem Between Keyboard And Chair
*/
Token pbkac = exception.get_token();
unsigned long pos = pbkac.pos;
/**
* The text snippet to display (this will be clipped accordingly)
*/
std::wstring to_display = input;
/**
* Avoid showing too much text
*/
if (pos > context) {
to_display = L"..." + input.substr(pos - context);
/**
* account for width of ellipsis
*/
pos = context + 3;
}
/**
* Avoid showing too much text
*/
if (to_display.length() > pbkac.data.length() + pos + context) {
to_display =
to_display.substr(0, pbkac.data.length() + pos + context) +
L"...";
}
/**
* The error message in full
*/
std::wstring built;
built.append(to_display + L"\n");
built.append(pos, ' ');
built.append(pbkac.data.length(), '^');
/**
* Show the position since the snippet of text may not be a full context
*/
if (pbkac.data.length() > 1) {
built
.append(L" (chars " + LD::wtostring(pbkac.pos + 1) + L"-" +
LD::wtostring(pbkac.pos + pbkac.data.length()) +
L")");
} else {
built.append(L" (char " + LD::wtostring(pbkac.pos + 1) + L")");
}
built.append(L"\nError: " + exception.get_msg() + L"\n");
return built;
}
#endif //CALCULATOR_PRETTIFYEXCEPTION_HPP