1 #ifndef _C4_YML_ERROR_DEF_HPP_
2 #define _C4_YML_ERROR_DEF_HPP_
6 #ifndef _C4_YML_ERROR_HPP_
14 template<
class DumpFn>
24 std::forward<DumpFn>(dumpfn)(loc.
name);
25 std::forward<DumpFn>(dumpfn)(
":");
26 count += loc.
name.len + 1;
30 csubstr val = detail::_to_chars_limited(buf, loc.
line);
33 std::forward<DumpFn>(dumpfn)(
"line=");
34 std::forward<DumpFn>(dumpfn)(val);
37 std::forward<DumpFn>(dumpfn)(
":");
44 std::forward<DumpFn>(dumpfn)(val);
45 std::forward<DumpFn>(dumpfn)(
":");
51 csubstr val = detail::_to_chars_limited(buf, loc.
col);
54 std::forward<DumpFn>(dumpfn)(
" ");
57 std::forward<DumpFn>(dumpfn)(
"col=");
58 std::forward<DumpFn>(dumpfn)(val);
62 std::forward<DumpFn>(dumpfn)(
":");
68 csubstr val = detail::_to_chars_limited(buf, loc.
offset);
71 std::forward<DumpFn>(dumpfn)(
" ");
74 std::forward<DumpFn>(dumpfn)(
"(");
75 std::forward<DumpFn>(dumpfn)(val);
76 std::forward<DumpFn>(dumpfn)(
"B):");
82 template<
class DumpFn>
85 csubstr source_buffer,
87 size_t num_lines_before,
88 size_t num_lines_after,
89 size_t first_col_highlight,
90 size_t last_col_highlight,
97 auto pr = [&](csubstr s){ std::forward<DumpFn>(dumpfn)(s); };
98 auto prn = [&](csubstr s,
size_t num_times){
99 for(
size_t i = 0; i < num_times; ++i)
100 std::forward<DumpFn>(dumpfn)(s);
103 size_t target_col = location.
col !=
npos ? location.
col : (last_col_highlight > first_col_highlight ? first_col_highlight :
npos);
104 size_t first_col_to_show = 0;
105 if(target_col !=
npos && target_col > maxlen)
106 first_col_to_show = target_col - maxlen + 1;
107 auto print_line_maybe_truncated = [&](csubstr contents){
108 if(contents.len <= maxlen)
110 if(first_col_to_show == 0)
114 else if(first_col_to_show < contents.len)
116 csubstr show = contents.sub(first_col_to_show);
119 if(maxlen > show.len)
120 prn(
" ", maxlen - show.len + 5);
121 pr(
" (showing columns ");
122 pr(detail::_to_chars_limited(buf, first_col_to_show));
124 pr(detail::_to_chars_limited(buf, contents.len));
126 pr(detail::_to_chars_limited(buf, contents.len));
132 prn(
" ", maxlen + 5);
133 pr(
" (not showing, columns=");
134 pr(detail::_to_chars_limited(buf, contents.len));
140 if(first_col_to_show == 0)
142 csubstr show = contents.first(maxlen);
144 pr(
"[...] (showing columns 0-");
145 pr(detail::_to_chars_limited(buf, show.len));
147 pr(detail::_to_chars_limited(buf, contents.len));
150 else if(first_col_to_show < contents.len && first_col_to_show + maxlen <= contents.len)
152 csubstr show = contents.sub(first_col_to_show, maxlen);
155 pr(
"[...] (showing columns ");
156 pr(detail::_to_chars_limited(buf, first_col_to_show));
158 pr(detail::_to_chars_limited(buf, first_col_to_show + maxlen));
160 pr(detail::_to_chars_limited(buf, contents.len));
163 else if(first_col_to_show < contents.len)
165 csubstr show = contents.sub(first_col_to_show);
168 if(maxlen > show.len)
169 prn(
" ", maxlen - show.len + 5);
170 pr(
" (showing columns ");
171 pr(detail::_to_chars_limited(buf, first_col_to_show));
173 pr(detail::_to_chars_limited(buf, contents.len));
175 pr(detail::_to_chars_limited(buf, contents.len));
181 prn(
" ", maxlen + 5);
182 pr(
" (not showing, columns=");
183 pr(detail::_to_chars_limited(buf, contents.len));
196 auto print_call = [&](csubstr after){
205 print_line_maybe_truncated(line);
207 jump = locsize + location.
col - first_col_to_show;
215 print_line_maybe_truncated(line);
221 const size_t first_col_jump = first_col_to_show == 0 ? 0 : 5;
223 auto print_cursor = [&](
size_t nocall_jump){
228 if(nocall_jump !=
npos)
230 prn(
" ", nocall_jump + first_col_jump);
232 prn(
" ", nocall_jump + first_col_jump);
240 prn(
" ", location.
col - first_col_to_show + first_col_jump);
244 prn(
" ", location.
col - first_col_to_show + first_col_jump);
249 size_t firstcol = first_col_highlight < line.len ? first_col_highlight : line.len;
250 size_t lastcol = last_col_highlight < line.len ? last_col_highlight : line.len;
251 firstcol = firstcol < maxlen ? firstcol : maxlen;
252 lastcol = lastcol < maxlen ? lastcol : maxlen;
253 if(firstcol < lastcol)
262 for(
size_t i = 0; i < locsize + firstcol; ++i)
265 for(
size_t i = locsize + firstcol; i < locsize + lastcol; ++i)
268 pr(detail::_to_chars_limited(buf, firstcol));
270 pr(detail::_to_chars_limited(buf, lastcol));
272 pr(detail::_to_chars_limited(buf, line.len));
280 if(num_lines_before || num_lines_after)
299 for(csubstr contents : region.split(
'\n'))
305 print_line_maybe_truncated(contents);
308 assert(location.
col ==
npos || location.
col >= first_col_to_show);
309 print_cursor(location.
col - first_col_to_show);
314 template<
class DumpFn>
320 std::forward<DumpFn>(dumpfn)(
" ");
322 std::forward<DumpFn>(dumpfn)(
"ERROR: [basic] ");
323 std::forward<DumpFn>(dumpfn)(msg);
327 template<
class DumpFn>
333 std::forward<DumpFn>(dumpfn)(
" ");
335 std::forward<DumpFn>(dumpfn)(
"ERROR: [parse] ");
336 std::forward<DumpFn>(dumpfn)(msg);
339 std::forward<DumpFn>(dumpfn)(
"\n");
341 std::forward<DumpFn>(dumpfn)(
" (detected here)");
346 template<
class DumpFn>
354 std::forward<DumpFn>(dumpfn)(
" ");
356 std::forward<DumpFn>(dumpfn)(
"ERROR: [visit] ");
357 std::forward<DumpFn>(dumpfn)(msg);
362 std::forward<DumpFn>(dumpfn)(
"\n");
364 std::forward<DumpFn>(dumpfn)(
" ");
366 std::forward<DumpFn>(dumpfn)(
"ERROR: (");
369 std::forward<DumpFn>(dumpfn)(
"node=");
370 std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf, errdata.
node));
371 if(errdata.
tree !=
nullptr)
372 std::forward<DumpFn>(dumpfn)(
" ");
374 if(errdata.
tree !=
nullptr)
376 std::forward<DumpFn>(dumpfn)(
"tree=");
377 std::forward<DumpFn>(dumpfn)(detail::_to_chars_limited(buf,
static_cast<void const*
>(errdata.
tree)));
379 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