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