1 #ifndef _C4_YML_ERROR_DEF_HPP_
2 #define _C4_YML_ERROR_DEF_HPP_
6 #ifndef _C4_YML_ERROR_HPP_
17 template<
class DumpFn>
27 std::forward<DumpFn>(dumpfn)(loc.
name);
28 std::forward<DumpFn>(dumpfn)(
":");
29 count += loc.
name.len + 1;
33 csubstr val = detail::_to_chars_limited(buf, loc.
line);
36 std::forward<DumpFn>(dumpfn)(
"line=");
37 std::forward<DumpFn>(dumpfn)(val);
40 std::forward<DumpFn>(dumpfn)(
":");
47 std::forward<DumpFn>(dumpfn)(val);
48 std::forward<DumpFn>(dumpfn)(
":");
54 csubstr val = detail::_to_chars_limited(buf, loc.
col);
57 std::forward<DumpFn>(dumpfn)(
" ");
60 std::forward<DumpFn>(dumpfn)(
"col=");
61 std::forward<DumpFn>(dumpfn)(val);
65 std::forward<DumpFn>(dumpfn)(
":");
71 csubstr val = detail::_to_chars_limited(buf, loc.
offset);
74 std::forward<DumpFn>(dumpfn)(
" ");
77 std::forward<DumpFn>(dumpfn)(
"(");
78 std::forward<DumpFn>(dumpfn)(val);
79 std::forward<DumpFn>(dumpfn)(
"B):");
85 template<
class DumpFn>
88 csubstr source_buffer,
90 size_t num_lines_before,
91 size_t num_lines_after,
92 size_t first_col_highlight,
93 size_t last_col_highlight,
100 auto pr = [&](csubstr s){ std::forward<DumpFn>(dumpfn)(s); };
101 auto prn = [&](csubstr s,
size_t num_times){
102 for(
size_t i = 0; i < num_times; ++i)
103 std::forward<DumpFn>(dumpfn)(s);
106 size_t target_col = location.
col !=
npos ? location.
col : (last_col_highlight > first_col_highlight ? first_col_highlight :
npos);
107 size_t first_col_to_show = 0;
108 if(target_col !=
npos && target_col > maxlen)
109 first_col_to_show = target_col - maxlen + 1;
110 auto print_line_maybe_truncated = [&](csubstr contents){
111 if(contents.len <= maxlen)
113 if(first_col_to_show == 0)
117 else if(first_col_to_show < contents.len)
119 csubstr show = contents.sub(first_col_to_show);
122 if(maxlen > show.len)
123 prn(
" ", maxlen - show.len + 5);
124 pr(
" (showing columns ");
125 pr(detail::_to_chars_limited(buf, first_col_to_show));
127 pr(detail::_to_chars_limited(buf, contents.len));
129 pr(detail::_to_chars_limited(buf, contents.len));
135 prn(
" ", maxlen + 5);
136 pr(
" (not showing, columns=");
137 pr(detail::_to_chars_limited(buf, contents.len));
143 if(first_col_to_show == 0)
145 csubstr show = contents.first(maxlen);
147 pr(
"[...] (showing columns 0-");
148 pr(detail::_to_chars_limited(buf, show.len));
150 pr(detail::_to_chars_limited(buf, contents.len));
153 else if(first_col_to_show < contents.len && first_col_to_show + maxlen <= contents.len)
155 csubstr show = contents.sub(first_col_to_show, maxlen);
158 pr(
"[...] (showing columns ");
159 pr(detail::_to_chars_limited(buf, first_col_to_show));
161 pr(detail::_to_chars_limited(buf, first_col_to_show + maxlen));
163 pr(detail::_to_chars_limited(buf, contents.len));
166 else if(first_col_to_show < contents.len)
168 csubstr show = contents.sub(first_col_to_show);
171 if(maxlen > show.len)
172 prn(
" ", maxlen - show.len + 5);
173 pr(
" (showing columns ");
174 pr(detail::_to_chars_limited(buf, first_col_to_show));
176 pr(detail::_to_chars_limited(buf, contents.len));
178 pr(detail::_to_chars_limited(buf, contents.len));
184 prn(
" ", maxlen + 5);
185 pr(
" (not showing, columns=");
186 pr(detail::_to_chars_limited(buf, contents.len));
199 auto print_call = [&](csubstr after){
208 print_line_maybe_truncated(line);
210 jump = locsize + location.
col - first_col_to_show;
218 print_line_maybe_truncated(line);
224 const size_t first_col_jump = first_col_to_show == 0 ? 0 : 5;
226 auto print_cursor = [&](
size_t nocall_jump){
231 if(nocall_jump !=
npos)
233 prn(
" ", nocall_jump + first_col_jump);
235 prn(
" ", nocall_jump + first_col_jump);
243 prn(
" ", location.
col - first_col_to_show + first_col_jump);
247 prn(
" ", location.
col - first_col_to_show + first_col_jump);
252 size_t firstcol = first_col_highlight < line.len ? first_col_highlight : line.len;
253 size_t lastcol = last_col_highlight < line.len ? last_col_highlight : line.len;
254 firstcol = firstcol < maxlen ? firstcol : maxlen;
255 lastcol = lastcol < maxlen ? lastcol : maxlen;
256 if(firstcol < lastcol)
265 for(
size_t i = 0; i < locsize + firstcol; ++i)
268 for(
size_t i = locsize + firstcol; i < locsize + lastcol; ++i)
271 pr(detail::_to_chars_limited(buf, firstcol));
273 pr(detail::_to_chars_limited(buf, lastcol));
275 pr(detail::_to_chars_limited(buf, line.len));
283 if(num_lines_before || num_lines_after)
302 for(csubstr contents : region.split(
'\n'))
308 print_line_maybe_truncated(contents);
311 assert(location.
col ==
npos || location.
col >= first_col_to_show);
312 print_cursor(location.
col - first_col_to_show);
317 template<
class DumpFn>
323 std::forward<DumpFn>(dumpfn)(
" ");
325 std::forward<DumpFn>(dumpfn)(
"ERROR: [basic] ");
326 std::forward<DumpFn>(dumpfn)(msg);
330 template<
class DumpFn>
336 std::forward<DumpFn>(dumpfn)(
" ");
338 std::forward<DumpFn>(dumpfn)(
"ERROR: [parse] ");
339 std::forward<DumpFn>(dumpfn)(msg);
342 std::forward<DumpFn>(dumpfn)(
"\n");
344 std::forward<DumpFn>(dumpfn)(
" (detected here)");
349 template<
class DumpFn>
357 std::forward<DumpFn>(dumpfn)(
" ");
359 std::forward<DumpFn>(dumpfn)(
"ERROR: [visit] ");
360 std::forward<DumpFn>(dumpfn)(msg);
365 std::forward<DumpFn>(dumpfn)(
"\n");
367 std::forward<DumpFn>(dumpfn)(
" ");
369 std::forward<DumpFn>(dumpfn)(
"ERROR: (");
372 std::forward<DumpFn>(dumpfn)(
"node=");
373 std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf, errdata.
node));
374 if(errdata.
tree !=
nullptr)
375 std::forward<DumpFn>(dumpfn)(
" ");
377 if(errdata.
tree !=
nullptr)
379 std::forward<DumpFn>(dumpfn)(
"tree=");
380 std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf,
static_cast<void const*
>(errdata.
tree)));
382 std::forward<DumpFn>(dumpfn)(
")");
Error utilities used by ryml.
size_t location_format(DumpFn &&dumpfn, Location const &loc)
generic formatting of a location
void err_visit_format(DumpFn &&dumpfn, csubstr msg, ErrorDataVisit const &errdata)
Given an error message and associated visit error data, format it fully as a visit error message.
void location_format_with_context(DumpFn &&dumpfn, Location const &location, csubstr source_buffer, csubstr call, size_t num_lines_before, size_t num_lines_after, size_t first_col_highlight, size_t last_col_highlight, size_t maxlen)
Generic formatting of a location, printing the source code buffer region around the location.
void err_basic_format(DumpFn &&dumpfn, csubstr msg, ErrorDataBasic const &errdata)
Given an error message and associated basic error data, format it fully as a basic error message.
void err_parse_format(DumpFn &&dumpfn, csubstr msg, ErrorDataParse const &errdata)
Given an error message and associated parse error data, format it fully as a parse error message.
csubstr _get_text_region(csubstr text, size_t pos, size_t num_lines_before, size_t num_lines_after)
@ npos
a null string position
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Location location
location where the error was detected (may be from YAML or C++ source code)
Location cpploc
location in the C++ source file where the error was detected.
Location ymlloc
location in the YAML source buffer where the error was detected.
Location cpploc
location in the C++ source file where the error was detected.
Tree const * tree
tree where the error was detected
id_type node
node where the error was detected
holds a source or yaml file position, for example when an error is detected; See also location_format...
size_t offset
number of bytes from the beginning of the source buffer
csubstr name
name of the file