| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <gyoji-context/errors.hpp> | ||
| 2 | #include <gyoji-context/token-stream.hpp> | ||
| 3 | |||
| 4 | using namespace Gyoji::context; | ||
| 5 | |||
| 6 | ////////////////////////////////////////////////// | ||
| 7 | 298 | Errors::Errors(TokenStream & _token_stream) | |
| 8 | 298 | : token_stream(_token_stream) | |
| 9 | 298 | {} | |
| 10 | 298 | Errors::~Errors() | |
| 11 | 298 | {} | |
| 12 | |||
| 13 | void | ||
| 14 | 4 | Errors::print() const | |
| 15 | { | ||
| 16 | // TODO: Sort errors in ascending order by | ||
| 17 | // source reference line number, not by | ||
| 18 | // order of occurrence. | ||
| 19 |
2/2✓ Branch 5 taken 10 times.
✓ Branch 6 taken 4 times.
|
14 | for (const auto & error : errors) { |
| 20 | 10 | error->print(); | |
| 21 | } | ||
| 22 | 4 | } | |
| 23 | |||
| 24 | void | ||
| 25 | 12 | Errors::add_error(Gyoji::owned<Error> error) | |
| 26 | { | ||
| 27 | // TODO: Iterate the messages and resolve the context. | ||
| 28 |
2/2✓ Branch 6 taken 22 times.
✓ Branch 7 taken 12 times.
|
34 | for (const auto & message : error->get_messages()) { |
| 29 | 44 | message->add_context( | |
| 30 | 44 | token_stream.context( | |
| 31 | 22 | message->get_source_ref().get_line()-2, | |
| 32 | 22 | message->get_source_ref().get_line()+1 | |
| 33 | ) | ||
| 34 | ); | ||
| 35 | |||
| 36 | } | ||
| 37 | 12 | errors.push_back(std::move(error)); | |
| 38 | 12 | } | |
| 39 | |||
| 40 | size_t | ||
| 41 | 308 | Errors::size() const | |
| 42 | { | ||
| 43 | 308 | return errors.size(); | |
| 44 | } | ||
| 45 | |||
| 46 | const Error & | ||
| 47 | 4 | Errors::get(size_t n) const | |
| 48 | { | ||
| 49 | 4 | return *errors.at(n); | |
| 50 | } | ||
| 51 | |||
| 52 | ////////////////////////////////////////////////// | ||
| 53 | 12 | Error::Error(std::string _error_title) | |
| 54 | 12 | : error_title(_error_title) | |
| 55 | 12 | {} | |
| 56 | 12 | Error::~Error() | |
| 57 | 12 | {} | |
| 58 | void | ||
| 59 | 10 | Error::print() | |
| 60 | { | ||
| 61 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
|
10 | const std::string filename = messages.size() > 0 ? messages.back()->get_source_ref().get_filename() : std::string("unknown filename"); |
| 62 | 10 | fprintf(stderr, "%s: Error: %s\n", filename.c_str(), error_title.c_str()); | |
| 63 |
2/2✓ Branch 5 taken 20 times.
✓ Branch 6 taken 10 times.
|
30 | for (const Gyoji::owned<ErrorMessage> & msg : messages) { |
| 64 | 20 | msg->print(); | |
| 65 | } | ||
| 66 | 10 | } | |
| 67 | |||
| 68 | void | ||
| 69 | 22 | Error::add_message( | |
| 70 | const SourceReference & _src_ref, | ||
| 71 | std::string _errormsg) | ||
| 72 | { | ||
| 73 | Gyoji::owned<ErrorMessage> message = std::make_unique<ErrorMessage>( | ||
| 74 | _src_ref, | ||
| 75 | _errormsg | ||
| 76 | 22 | ); | |
| 77 | 22 | messages.push_back(std::move(message)); | |
| 78 | 22 | } | |
| 79 | |||
| 80 | |||
| 81 | void | ||
| 82 | ✗ | Errors::add_simple_error( | |
| 83 | const SourceReference & _src_ref, | ||
| 84 | std::string _error_title, | ||
| 85 | std::string _error_message | ||
| 86 | ) | ||
| 87 | { | ||
| 88 | ✗ | auto error = std::make_unique<Gyoji::context::Error>(_error_title); | |
| 89 | ✗ | error->add_message(_src_ref, _error_message); | |
| 90 | ✗ | add_error(std::move(error)); | |
| 91 | ✗ | } | |
| 92 | |||
| 93 | const std::vector<Gyoji::owned<ErrorMessage>> & | ||
| 94 | 12 | Error::get_messages() const | |
| 95 | 12 | { return messages; } | |
| 96 | |||
| 97 | size_t | ||
| 98 | 2 | Error::size() const | |
| 99 | 2 | { return messages.size(); } | |
| 100 | |||
| 101 | const ErrorMessage & | ||
| 102 | 2 | Error::get(size_t n) const | |
| 103 | 2 | { return *messages.at(n); } | |
| 104 | |||
| 105 | ////////////////////////////////////////////////// | ||
| 106 | 22 | ErrorMessage::ErrorMessage( | |
| 107 | const SourceReference & _src_ref, | ||
| 108 | std::string _errormsg | ||
| 109 | 22 | ) | |
| 110 | 22 | : context() | |
| 111 | 22 | , src_ref(_src_ref) | |
| 112 | 22 | , errormsg(_errormsg) | |
| 113 | 22 | {} | |
| 114 | |||
| 115 | 22 | ErrorMessage::~ErrorMessage() | |
| 116 | 22 | {} | |
| 117 | |||
| 118 | void | ||
| 119 | 22 | ErrorMessage::add_context(const std::vector<std::pair<size_t, std::string>> & _context) | |
| 120 | 22 | { context = _context; } | |
| 121 | |||
| 122 | const SourceReference & | ||
| 123 | 54 | ErrorMessage::get_source_ref() const | |
| 124 | 54 | { return src_ref; } | |
| 125 | |||
| 126 | const std::string & | ||
| 127 | ✗ | ErrorMessage::get_message() const | |
| 128 | ✗ | { return errormsg; } | |
| 129 | |||
| 130 | size_t | ||
| 131 | 2 | ErrorMessage::get_line() const | |
| 132 | 2 | { return src_ref.get_line(); } | |
| 133 | |||
| 134 | // Case 1. | ||
| 135 | // ^ | ||
| 136 | // +---------message goes here, wrapped | ||
| 137 | // to indent level | ||
| 138 | |||
| 139 | // Case 2 | ||
| 140 | // ^ | ||
| 141 | // | | ||
| 142 | // message goes here, wrapped -------+ | ||
| 143 | // to indent level | ||
| 144 | |||
| 145 | 42 | static std::string pad_string(size_t length, std::string padder) | |
| 146 | { | ||
| 147 | 42 | std::string prefix; | |
| 148 |
2/2✓ Branch 0 taken 308 times.
✓ Branch 1 taken 42 times.
|
350 | for (size_t i = 0; i < length; i++) { |
| 149 | 308 | prefix = prefix + padder; | |
| 150 | } | ||
| 151 | 42 | return prefix; | |
| 152 | } | ||
| 153 | |||
| 154 | 20 | static void draw_arrow(size_t column, size_t length) | |
| 155 | { | ||
| 156 | 40 | std::string arrowhead_line("^"); | |
| 157 | 20 | std::string prefix = pad_string(column, std::string(" ")); | |
| 158 | 20 | arrowhead_line = prefix + arrowhead_line; | |
| 159 | 20 | length = std::min(length, (size_t)80); | |
| 160 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
|
20 | if (length >= 2) { |
| 161 | 4 | arrowhead_line = arrowhead_line + pad_string(length-2, std::string("~")); | |
| 162 | 4 | arrowhead_line = arrowhead_line + std::string("^"); | |
| 163 | } | ||
| 164 | 20 | fprintf(stderr, "%s\n", arrowhead_line.c_str()); | |
| 165 | 20 | } | |
| 166 | |||
| 167 | 20 | static std::string wrap_text(size_t max_width, std::string input) | |
| 168 | { | ||
| 169 | 20 | std::string wrapped; | |
| 170 | |||
| 171 | 20 | size_t linelen = 0; | |
| 172 |
2/2✓ Branch 1 taken 806 times.
✓ Branch 2 taken 20 times.
|
826 | for (size_t i = 0; i < input.size(); i++) { |
| 173 | 806 | char c = input[i]; | |
| 174 | 806 | linelen++; | |
| 175 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 712 times.
|
806 | if (isspace(c)) { |
| 176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
|
94 | if (linelen > max_width) { |
| 177 | ✗ | wrapped += '\n'; | |
| 178 | ✗ | linelen = 0; | |
| 179 | } | ||
| 180 | else { | ||
| 181 | 94 | wrapped += c; | |
| 182 | } | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | 712 | wrapped += c; | |
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | 20 | return wrapped; | |
| 190 | } | ||
| 191 | |||
| 192 | 20 | static std::string indent_text(size_t indent, std::string input) | |
| 193 | { | ||
| 194 | 20 | std::string wrapped; | |
| 195 | |||
| 196 | 20 | std::string pad = pad_string(indent, std::string(" ")); | |
| 197 | 20 | wrapped.append(pad); | |
| 198 | 20 | wrapped.append("|--"); | |
| 199 |
2/2✓ Branch 1 taken 806 times.
✓ Branch 2 taken 20 times.
|
826 | for (size_t i = 0; i < input.size(); i++) { |
| 200 | 806 | char c = input[i]; | |
| 201 | 806 | wrapped += c; | |
| 202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 806 times.
|
806 | if (c == '\n') { |
| 203 | ✗ | wrapped.append(pad); | |
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | 20 | return wrapped; | |
| 208 | 20 | } | |
| 209 | |||
| 210 | void | ||
| 211 | 20 | ErrorMessage::print() | |
| 212 | { | ||
| 213 | 20 | size_t line = src_ref.get_line(); | |
| 214 | 20 | size_t column = src_ref.get_column(); | |
| 215 | 20 | size_t length = src_ref.get_length(); | |
| 216 |
2/2✓ Branch 5 taken 64 times.
✓ Branch 6 taken 20 times.
|
84 | for (const std::pair<size_t, std::string> & linepair : context) { |
| 217 | 64 | fprintf(stderr, "%4ld: %s", linepair.first, linepair.second.c_str()); | |
| 218 |
2/2✓ Branch 1 taken 16 times.
✓ Branch 2 taken 48 times.
|
64 | if (linepair.second.size() > 0) { |
| 219 |
1/2✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
|
16 | if (linepair.second.at(linepair.second.size()-1) != '\n') { |
| 220 | 16 | fprintf(stderr, "\n"); | |
| 221 | } | ||
| 222 | } | ||
| 223 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 44 times.
|
64 | if (line == linepair.first) { |
| 224 | 20 | draw_arrow(column+6, length); | |
| 225 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (column < 40) { |
| 226 | 20 | std::string wrapped = wrap_text(80-column, errormsg); | |
| 227 | 20 | std::string indented = indent_text(column+6, wrapped); | |
| 228 | 20 | printf("%s\n", indented.c_str()); | |
| 229 | 20 | } | |
| 230 | else { | ||
| 231 | ✗ | std::string wrapped = wrap_text(column, errormsg); | |
| 232 | ✗ | std::string indented = indent_text(6, wrapped); | |
| 233 | ✗ | printf("%s\n", indented.c_str()); | |
| 234 | ✗ | } | |
| 235 | } | ||
| 236 | } | ||
| 237 | 20 | } | |
| 238 | |||
| 239 | |||
| 240 |