Skip to content

Conversation

@N-Dekker
Copy link
Contributor

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?

Pre-merge checklist

  • I've read CONTRIBUTING.md
  • I've rebased my changes against the current HEAD of origin/master (if necessary)
  • I've added new test cases to verify my change
  • I've regenerated toml.hpp (how-to)
  • I've updated any affected documentation
  • I've rebuilt and run the tests with at least one of:
    • Clang 8 or higher
    • GCC 8 or higher
    • MSVC 19.20 (Visual Studio 2019) or higher
  • I've added my name to the list of contributors in README.md

@N-Dekker N-Dekker force-pushed the print-line-in-error_printer branch 4 times, most recently from 3c80050 to 6c2b6e9 Compare February 28, 2025 10:38
@N-Dekker N-Dekker changed the title print parse error source line, in error_printer example let error_printer example print source line of parse error Feb 28, 2025
@N-Dekker N-Dekker marked this pull request as ready for review February 28, 2025 10:51
Comment on lines 178 to 187
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;
Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Owner

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 :)

Comment on lines 111 to 145
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,
};
Copy link
Contributor Author

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 🤷

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,
};

Copy link
Owner

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.

Copy link
Contributor Author

@N-Dekker N-Dekker Feb 28, 2025

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?

Copy link
Owner

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.

Copy link
Contributor Author

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 😇

@N-Dekker N-Dekker force-pushed the print-line-in-error_printer branch 2 times, most recently from ebdee44 to ffa0d3f Compare February 28, 2025 16:11
Showing a typical use case for `toml::get_line`.
@N-Dekker N-Dekker force-pushed the print-line-in-error_printer branch from ffa0d3f to ac52b9e Compare March 1, 2025 10:22
Copy link
Owner

@marzer marzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@marzer marzer merged commit cb34735 into marzer:master Mar 2, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants