ToruNiina / toml11

TOML for Modern C++
https://toruniina.github.io/toml11/
MIT License
1.05k stars 163 forks source link

User defined error messages with key regions #124

Open thisismiller opened 4 years ago

thisismiller commented 4 years ago

Suppose I have the following example.toml file:

foo = 1
bar = 2

After parsing this, I'd like to be able to print an error message like:

[error] Key `bar` is not allowed
 --> example.toml
 2 | bar = 2
   | ~~~ here

And I... can't seem to figure out how to make this happen. toml::key is an alias to std::string and not toml::string, so it loses (and the parser doesn't seem to track) the region information. I tried to get something close by using toml::get_region( toml::find( data, "bar" ) ) to get the value's region and trying to work backwards in the line from there, but base_region doesn't give me something like location::retrace nor can I construct a new location covering [line_begin(), begin()).

How can I get a source_location describing the location of a key in a table?

ToruNiina commented 4 years ago

Sorry, currently, it does not have the feature you said. To do that, as you pointed out, we need to define toml::key as a struct and add region information in parser. So far, I personally did not need to do that and no one craimed it. That is why it does not have the feature.

I'm now refactoring region related stuff to reduce complexity around it and once I've done it, maybe we would have full control on it. But since the region is an internal component, essentially interface might be changed without anouncement even in a minor release.

Since region of a key seems to be useful feature, so I will consider implementing it in the future release.