-
-
Notifications
You must be signed in to change notification settings - Fork 184
let error_printer example print source line of parse error #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
3c80050 to
6c2b6e9
Compare
examples/error_printer.cpp
Outdated
| std::cout << err; | ||
|
|
||
| auto line_num = err.source().begin.line; | ||
|
|
||
| if (auto line = toml::get_line(str, line_num)) | ||
| { | ||
| std::cout << "\nLine "sv << line_num << ": "sv; | ||
| print(*line); | ||
| } | ||
| std::cout << "\n\n"sv; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nice. Though, note you've only added this to the exception-based path - the exceptionless one lacks this feature. Ideally you'd split this new logic so that the get_line part would come after the exception branching, since it's the same in both cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just in case you missed it, I have addressed this comment of yours in the mean time, by adding a local helper function, print_parse_error, called by both the exception-based and the exceptionless path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I saw, looks good :)
examples/error_printer.cpp
Outdated
| constexpr std::string_view control_char_escapes[] = | ||
| { | ||
| "\\u0000"sv, | ||
| "\\u0001"sv, | ||
| "\\u0002"sv, | ||
| "\\u0003"sv, | ||
| "\\u0004"sv, | ||
| "\\u0005"sv, | ||
| "\\u0006"sv, | ||
| "\\u0007"sv, | ||
| "\\b"sv, | ||
| "\\t"sv, | ||
| "\\n"sv, | ||
| "\\u000B"sv, | ||
| "\\f"sv, | ||
| "\\r"sv, | ||
| "\\u000E"sv, | ||
| "\\u000F"sv, | ||
| "\\u0010"sv, | ||
| "\\u0011"sv, | ||
| "\\u0012"sv, | ||
| "\\u0013"sv, | ||
| "\\u0014"sv, | ||
| "\\u0015"sv, | ||
| "\\u0016"sv, | ||
| "\\u0017"sv, | ||
| "\\u0018"sv, | ||
| "\\u0019"sv, | ||
| "\\u001A"sv, | ||
| "\\u001B"sv, | ||
| "\\u001C"sv, | ||
| "\\u001D"sv, | ||
| "\\u001E"sv, | ||
| "\\u001F"sv, | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This list was copied from forward_declarations.hpp, of course! @marzer Would it be OK if the example would directly use impl::control_char_escapes instead? I guess not, because it's an implementation detail. Just asking anyway 🤷
tomlplusplus/include/toml++/impl/forward_declarations.hpp
Lines 127 to 161 in 2ca7ac6
| inline constexpr std::string_view control_char_escapes[] = | |
| { | |
| "\\u0000"sv, | |
| "\\u0001"sv, | |
| "\\u0002"sv, | |
| "\\u0003"sv, | |
| "\\u0004"sv, | |
| "\\u0005"sv, | |
| "\\u0006"sv, | |
| "\\u0007"sv, | |
| "\\b"sv, | |
| "\\t"sv, | |
| "\\n"sv, | |
| "\\u000B"sv, | |
| "\\f"sv, | |
| "\\r"sv, | |
| "\\u000E"sv, | |
| "\\u000F"sv, | |
| "\\u0010"sv, | |
| "\\u0011"sv, | |
| "\\u0012"sv, | |
| "\\u0013"sv, | |
| "\\u0014"sv, | |
| "\\u0015"sv, | |
| "\\u0016"sv, | |
| "\\u0017"sv, | |
| "\\u0018"sv, | |
| "\\u0019"sv, | |
| "\\u001A"sv, | |
| "\\u001B"sv, | |
| "\\u001C"sv, | |
| "\\u001D"sv, | |
| "\\u001E"sv, | |
| "\\u001F"sv, | |
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not fussed to be honest. That particular example isn't really intended as a code example, but more of an example of how the error messages themselves are formatted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so would you prefer "error_printer.cpp" to use control_char_escapes from "impl/forward_declarations.hpp"? Or do you mean it doesn't really matter to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if you want to simplify it by re-using the implementation detail bit, rather than copying it, go ahead, I'm not going to be a purist about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hereby force-pushed, please check!
Honestly I am a bit of a purist on trying to avoid repetitive code, trying to follow the DRY principle 😇
ebdee44 to
ffa0d3f
Compare
Showing a typical use case for `toml::get_line`.
ffa0d3f to
ac52b9e
Compare
marzer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
What does this change do?
It aims to demonstrate a typical use case for
toml::get_line: printing the source line of a parse error.Is it related to an exisiting bug report or feature request?
toml::get_linereturn an _optional_ string_view #258.Pre-merge checklist
origin/master(if necessary)