|
8 | 8 | #include <format> |
9 | 9 | #include <concepts> |
10 | 10 | #include <expected> |
| 11 | +#include <algorithm> |
11 | 12 |
|
12 | 13 | namespace hk { |
13 | 14 |
|
14 | 15 | class error_list : public std::vector<error_item> { |
15 | 16 | public: |
16 | 17 | using element_type = error_item; |
17 | 18 |
|
18 | | - hkc_error add(line_table const& lines, char const* first, char const* last, hkc_error code, std::string message = std::string{}) |
| 19 | + /** Add an error. |
| 20 | + * |
| 21 | + * This add will make sure the error_list is sorted by file order. |
| 22 | + * Parsing the file multiple times will not introduce duplicate errors. |
| 23 | + * |
| 24 | + * @param lines The line table for this file. Used for print the error |
| 25 | + * message to stderr. |
| 26 | + * @param first Pointer to the first character causing the error. |
| 27 | + * @param last Pointer to beyond the last character causing the error. |
| 28 | + * @param error The error code. |
| 29 | + * @param message An extra message to print. |
| 30 | + * @return The iterator to the error, true if a new error was inserted. |
| 31 | + */ |
| 32 | + std::pair<iterator, bool> add(line_table const& lines, char const* first, char const* last, hkc_error error, std::string message = std::string{}) |
19 | 33 | { |
20 | | - auto const& e = this->emplace_back(code, first, last, std::move(message)); |
21 | | - e.print(lines); |
22 | | - return code; |
| 34 | + auto e = error_item{error, first, last, std::move(message)}; |
| 35 | + |
| 36 | + auto it = std::lower_bound(begin(), end(), e, [](auto const& item, auto const& x) { |
| 37 | + return item < x; |
| 38 | + }); |
| 39 | + if (it != end() and *it == e) { |
| 40 | + return {it, false}; |
| 41 | + } |
| 42 | + |
| 43 | + it = insert(it, std::move(e)); |
| 44 | + it->print(lines); |
| 45 | + return {it, true}; |
23 | 46 | } |
24 | 47 |
|
| 48 | + /** Add an error. |
| 49 | + * |
| 50 | + * This add will make sure the error_list is sorted by file order. |
| 51 | + * Parsing the file multiple times will not introduce duplicate errors. |
| 52 | + * |
| 53 | + * @param lines The line table for this file. Used for print the error |
| 54 | + * message to stderr. |
| 55 | + * @param first Pointer to the first character causing the error. |
| 56 | + * @param last Pointer to beyond the last character causing the error. |
| 57 | + * @param error The error code. |
| 58 | + * @param fmt Formatting string for an extra error message (optional) |
| 59 | + * @param args... Arguments for formatting. |
| 60 | + * @return The iterator to the error, true if a new error was inserted. |
| 61 | + */ |
25 | 62 | template<typename... Args> |
26 | | - hkc_error add(line_table const& lines, char const* first, char const* last, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args) |
| 63 | + std::pair<iterator, bool> add(line_table const& lines, char const* first, char const* last, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args) |
27 | 64 | { |
28 | 65 | return add(lines, first, last, code, std::format(std::move(fmt), std::forward<Args>(args)...)); |
29 | 66 | } |
30 | 67 |
|
| 68 | + /** Add an error. |
| 69 | + * |
| 70 | + * This add will make sure the error_list is sorted by file order. |
| 71 | + * Parsing the file multiple times will not introduce duplicate errors. |
| 72 | + * |
| 73 | + * @param lines The line table for this file. Used for print the error |
| 74 | + * message to stderr. |
| 75 | + * @param first Pointer to the first character causing the error. |
| 76 | + * @param error The error code. |
| 77 | + * @param fmt Formatting string for an extra error message (optional) |
| 78 | + * @param args... Arguments for formatting. |
| 79 | + * @return The iterator to the error, true if a new error was inserted. |
| 80 | + */ |
31 | 81 | template<typename... Args> |
32 | | - hkc_error add(line_table const& lines, char const* first, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args) |
| 82 | + std::pair<iterator, bool> add(line_table const& lines, char const* first, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args) |
33 | 83 | { |
34 | 84 | return add(lines, first, nullptr, code, std::format(std::move(fmt), std::forward<Args>(args)...)); |
35 | 85 | } |
36 | 86 |
|
| 87 | + /** Add an error. |
| 88 | + * |
| 89 | + * This add will make sure the error_list is sorted by file order. |
| 90 | + * Parsing the file multiple times will not introduce duplicate errors. |
| 91 | + * |
| 92 | + * @param lines The line table for this file. Used for print the error |
| 93 | + * message to stderr. |
| 94 | + * @param error The error code. |
| 95 | + * @param fmt Formatting string for an extra error message (optional) |
| 96 | + * @param args... Arguments for formatting. |
| 97 | + * @return The iterator to the error, true if a new error was inserted. |
| 98 | + */ |
37 | 99 | template<typename... Args> |
38 | | - hkc_error add(line_table const& lines, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args) |
| 100 | + std::pair<iterator, bool> add(line_table const& lines, hkc_error code, std::format_string<Args...> fmt = {}, Args&&... args) |
39 | 101 | { |
40 | 102 | return add(lines, nullptr, nullptr, code, std::format(std::move(fmt), std::forward<Args>(args)...)); |
41 | 103 | } |
|
0 commit comments