1 #ifndef _C4_YML_ERROR_DEF_HPP_
2 #define _C4_YML_ERROR_DEF_HPP_
6 #ifndef _C4_YML_ERROR_HPP_
15 template<
class DumpFn>
25 std::forward<DumpFn>(dumpfn)(loc.
name);
26 std::forward<DumpFn>(dumpfn)(
":");
27 count += loc.
name.len + 1;
31 csubstr val = detail::_to_chars_limited(buf, loc.
line);
34 std::forward<DumpFn>(dumpfn)(
"line=");
35 std::forward<DumpFn>(dumpfn)(val);
38 std::forward<DumpFn>(dumpfn)(
":");
45 std::forward<DumpFn>(dumpfn)(val);
46 std::forward<DumpFn>(dumpfn)(
":");
52 csubstr val = detail::_to_chars_limited(buf, loc.
col);
55 std::forward<DumpFn>(dumpfn)(
" ");
58 std::forward<DumpFn>(dumpfn)(
"col=");
59 std::forward<DumpFn>(dumpfn)(val);
63 std::forward<DumpFn>(dumpfn)(
":");
69 csubstr val = detail::_to_chars_limited(buf, loc.
offset);
72 std::forward<DumpFn>(dumpfn)(
" ");
75 std::forward<DumpFn>(dumpfn)(
"(");
76 std::forward<DumpFn>(dumpfn)(val);
77 std::forward<DumpFn>(dumpfn)(
"B):");
83 template<
class DumpFn>
86 csubstr source_buffer,
88 size_t num_lines_before,
89 size_t num_lines_after,
90 size_t first_col_highlight,
91 size_t last_col_highlight,
98 auto pr = [&](csubstr s){ std::forward<DumpFn>(dumpfn)(s); };
99 auto prn = [&](csubstr s,
size_t num_times){
100 for(
size_t i = 0; i < num_times; ++i)
101 std::forward<DumpFn>(dumpfn)(s);
104 size_t target_col = location.
col !=
npos ? location.
col : (last_col_highlight > first_col_highlight ? first_col_highlight :
npos);
105 size_t first_col_to_show = 0;
106 if(target_col !=
npos && target_col > maxlen)
107 first_col_to_show = target_col - maxlen + 1;
108 auto print_line_maybe_truncated = [&](csubstr contents){
109 if(contents.len <= maxlen)
111 if(first_col_to_show == 0)
115 else if(first_col_to_show < contents.len)
117 csubstr show = contents.sub(first_col_to_show);
120 if(maxlen > show.len)
121 prn(
" ", maxlen - show.len + 5);
122 pr(
" (showing columns ");
123 pr(detail::_to_chars_limited(buf, first_col_to_show));
125 pr(detail::_to_chars_limited(buf, contents.len));
127 pr(detail::_to_chars_limited(buf, contents.len));
133 prn(
" ", maxlen + 5);
134 pr(
" (not showing, columns=");
135 pr(detail::_to_chars_limited(buf, contents.len));
141 if(first_col_to_show == 0)
143 csubstr show = contents.first(maxlen);
145 pr(
"[...] (showing columns 0-");
146 pr(detail::_to_chars_limited(buf, show.len));
148 pr(detail::_to_chars_limited(buf, contents.len));
151 else if(first_col_to_show < contents.len && first_col_to_show + maxlen <= contents.len)
153 csubstr show = contents.sub(first_col_to_show, maxlen);
156 pr(
"[...] (showing columns ");
157 pr(detail::_to_chars_limited(buf, first_col_to_show));
159 pr(detail::_to_chars_limited(buf, first_col_to_show + maxlen));
161 pr(detail::_to_chars_limited(buf, contents.len));
164 else if(first_col_to_show < contents.len)
166 csubstr show = contents.sub(first_col_to_show);
169 if(maxlen > show.len)
170 prn(
" ", maxlen - show.len + 5);
171 pr(
" (showing columns ");
172 pr(detail::_to_chars_limited(buf, first_col_to_show));
174 pr(detail::_to_chars_limited(buf, contents.len));
176 pr(detail::_to_chars_limited(buf, contents.len));
182 prn(
" ", maxlen + 5);
183 pr(
" (not showing, columns=");
184 pr(detail::_to_chars_limited(buf, contents.len));
197 auto print_call = [&](csubstr after){
206 print_line_maybe_truncated(line);
208 jump = locsize + location.
col - first_col_to_show;
216 print_line_maybe_truncated(line);
222 const size_t first_col_jump = first_col_to_show == 0 ? 0 : 5;
224 auto print_cursor = [&](
size_t nocall_jump){
229 if(nocall_jump !=
npos)
231 prn(
" ", nocall_jump + first_col_jump);
233 prn(
" ", nocall_jump + first_col_jump);
241 prn(
" ", location.
col - first_col_to_show + first_col_jump);
245 prn(
" ", location.
col - first_col_to_show + first_col_jump);
250 size_t firstcol = first_col_highlight < line.len ? first_col_highlight : line.len;
251 size_t lastcol = last_col_highlight < line.len ? last_col_highlight : line.len;
252 firstcol = firstcol < maxlen ? firstcol : maxlen;
253 lastcol = lastcol < maxlen ? lastcol : maxlen;
254 if(firstcol < lastcol)
263 for(
size_t i = 0; i < locsize + firstcol; ++i)
266 for(
size_t i = locsize + firstcol; i < locsize + lastcol; ++i)
269 pr(detail::_to_chars_limited(buf, firstcol));
271 pr(detail::_to_chars_limited(buf, lastcol));
273 pr(detail::_to_chars_limited(buf, line.len));
281 if(num_lines_before || num_lines_after)
300 for(csubstr contents : region.split(
'\n'))
306 print_line_maybe_truncated(contents);
309 assert(location.
col ==
npos || location.
col >= first_col_to_show);
310 print_cursor(location.
col - first_col_to_show);
315 template<
class DumpFn>
321 std::forward<DumpFn>(dumpfn)(
" ");
323 std::forward<DumpFn>(dumpfn)(
"ERROR: [basic] ");
324 std::forward<DumpFn>(dumpfn)(msg);
328 template<
class DumpFn>
334 std::forward<DumpFn>(dumpfn)(
" ");
336 std::forward<DumpFn>(dumpfn)(
"ERROR: [parse] ");
337 std::forward<DumpFn>(dumpfn)(msg);
340 std::forward<DumpFn>(dumpfn)(
"\n");
342 std::forward<DumpFn>(dumpfn)(
" (detected here)");
347 template<
class DumpFn>
355 std::forward<DumpFn>(dumpfn)(
" ");
357 std::forward<DumpFn>(dumpfn)(
"ERROR: [visit] ");
358 std::forward<DumpFn>(dumpfn)(msg);
363 std::forward<DumpFn>(dumpfn)(
"\n");
365 std::forward<DumpFn>(dumpfn)(
" ");
367 std::forward<DumpFn>(dumpfn)(
"ERROR: (");
370 std::forward<DumpFn>(dumpfn)(
"node=");
371 std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf, errdata.
node));
372 if(errdata.
tree !=
nullptr)
373 std::forward<DumpFn>(dumpfn)(
" ");
375 if(errdata.
tree !=
nullptr)
377 std::forward<DumpFn>(dumpfn)(
"tree=");
378 std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf,
static_cast<void const*
>(errdata.
tree)));
380 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