mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
Diagnostic.hpp
Go to the documentation of this file.
1#pragma once
2
3
4#include <cstdarg>
5#include <cstdio>
7
8
9namespace m {
10
12{
13 static constexpr const char *RESET = "\033[0m";
14 static constexpr const char *BOLD = "\033[1;37m";
15 static constexpr const char *ITALIC = "\033[3;37m";
16 static constexpr const char *NOTE = "\033[1;2;37m";
17 static constexpr const char *WARNING = "\033[1;35m";
18 static constexpr const char *ERROR = "\033[1;31m";
19
20 Diagnostic(const bool color, std::ostream &out, std::ostream &err)
21 : color_(color)
22 , out_(out)
23 , err_(err)
24 { }
25
26 std::ostream & operator()(const Position pos) {
27 print_pos(out_, pos, K_None);
28 return out_;
29 }
30
31 std::ostream & n(const Position pos) {
32 print_pos(out_, pos, K_Note);
33 return out_;
34 }
35
36 std::ostream & w(const Position pos) {
38 return err_;
39 }
40
41 std::ostream & e(const Position pos) {
43 print_pos(err_, pos, K_Error);
44 return err_;
45 }
46
48 unsigned num_errors() const { return num_errors_; }
50 void clear() { num_errors_ = 0; }
51
52 std::ostream & out() const { return out_; }
53 std::ostream & err() {
55 err_ << ERROR << "error: " << RESET;
56 return err_;
57 }
58
59 private:
60 const bool color_;
61 std::ostream &out_;
62 std::ostream &err_;
63 unsigned num_errors_ = 0;
64
65 enum Kind {
70 };
71
72 void print_pos(std::ostream &out, const Position pos, const Kind kind) {
73 if (color_) out << BOLD;
74 out << pos.name << ':' << pos.line << ':' << pos.column << ": ";
75 if (color_) out << RESET;
76 switch (kind) {
77 case K_None: break;
78 case K_Note: if (color_) { out << NOTE; } out << "note: "; break;
79 case K_Warning: if (color_) { out << WARNING; } out << "warning: "; break;
80 case K_Error: if (color_) { out << ERROR; } out << "error: "; break;
81 }
82 if (color_) out << RESET;
83 }
84};
85
86}
‍mutable namespace
Definition: Backend.hpp:10
static constexpr const char * BOLD
Definition: Diagnostic.hpp:14
void print_pos(std::ostream &out, const Position pos, const Kind kind)
Definition: Diagnostic.hpp:72
std::ostream & out_
Definition: Diagnostic.hpp:61
const bool color_
Definition: Diagnostic.hpp:60
static constexpr const char * ERROR
Definition: Diagnostic.hpp:18
unsigned num_errors_
Definition: Diagnostic.hpp:63
std::ostream & e(const Position pos)
Definition: Diagnostic.hpp:41
static constexpr const char * NOTE
Definition: Diagnostic.hpp:16
void clear()
Resets the error counter.
Definition: Diagnostic.hpp:50
std::ostream & n(const Position pos)
Definition: Diagnostic.hpp:31
Diagnostic(const bool color, std::ostream &out, std::ostream &err)
Definition: Diagnostic.hpp:20
static constexpr const char * RESET
Definition: Diagnostic.hpp:13
std::ostream & err_
Definition: Diagnostic.hpp:62
std::ostream & out() const
Definition: Diagnostic.hpp:52
unsigned num_errors() const
Returns the number of errors emitted since the last call to clear().
Definition: Diagnostic.hpp:48
std::ostream & w(const Position pos)
Definition: Diagnostic.hpp:36
static constexpr const char * WARNING
Definition: Diagnostic.hpp:17
std::ostream & operator()(const Position pos)
Definition: Diagnostic.hpp:26
static constexpr const char * ITALIC
Definition: Diagnostic.hpp:15
std::ostream & err()
Definition: Diagnostic.hpp:53
unsigned line
Definition: Position.hpp:14
unsigned column
Definition: Position.hpp:15
const char * name
Definition: Position.hpp:13