-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.cpp
executable file
·80 lines (66 loc) · 2.08 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "error.hpp"
#include "token.hpp"
#include <cstdio>
#include <cstdarg>
using namespace compiler;
static void vmessage(const file_pos &loc, const char *format, std::va_list args) {
if(loc.m_name)
std::fprintf(stderr, "In file %s:%u:%u:\n", loc.m_name, loc.m_line, loc.m_column);
else
std::fprintf(stderr, "In temporary string %u:%u:\n", loc.m_line, loc.m_column);
print_fpos(loc);
std::vfprintf(stderr, format, args);
std::putc('\n', stderr);
}
void compiler::print_fpos(const file_pos &p) noexcept {
// Error/Warning occurs when the unexpected character extracted
static constexpr unsigned int start = 2;
auto str = p.m_begin;
for(;*str!='\n' && *str!='\0'; ++str)
std::putc(*str, stderr);
std::putc('\n', stderr);
for(auto i = start; i < p.m_column; ++i)
std::putc(' ', stderr);
std::fprintf(stderr, "^\n");
}
void compiler::error(const char *format, ...) throw(int) {
std::va_list args;
va_start(args, format);
std::vfprintf(stderr, format, args);
va_end(args);
// TODO: A better way to abort
throw 0;
}
void compiler::error(const token *tok, const char *format, ...) throw(int) {
std::va_list args;
va_start(args, format);
vmessage(tok->m_pos, format, args);
va_end(args);
throw 0;
}
void compiler::error(const file_pos &loc, const char *format, ...) throw(int) {
std::va_list args;
va_start(args, format);
vmessage(loc, format, args);
va_end(args);
// TODO: A better way to abort
throw 0;
}
void compiler::warning(const char *format, ...) noexcept {
std::va_list args;
va_start(args, format);
std::vfprintf(stderr, format, args);
va_end(args);
}
void compiler::warning(const file_pos &loc, const char *format, ...) noexcept {
std::va_list args;
va_start(args, format);
vmessage(loc, format, args);
va_end(args);
}
void compiler::warning(const token *tok, const char *format, ...) noexcept {
std::va_list args;
va_start(args, format);
vmessage(tok->m_pos, format, args);
va_end(args);
}