feat: improve error message about invalid keys

This commit is contained in:
ToruNiina
2020-08-09 18:38:50 +09:00
parent 08f7ea9c56
commit 3c3ebd88b4

View File

@@ -879,7 +879,8 @@ inline result<std::pair<std::vector<key>, region>, std::string>
parse_key(location& loc) parse_key(location& loc)
{ {
const auto first = loc.iter(); const auto first = loc.iter();
// dotted key -> foo.bar.baz whitespaces are allowed // dotted key -> `foo.bar.baz` where several single keys are chained by
// dots. Whitespaces between keys and dots are allowed.
if(const auto token = lex_dotted_key::invoke(loc)) if(const auto token = lex_dotted_key::invoke(loc))
{ {
const auto reg = token.unwrap(); const auto reg = token.unwrap();
@@ -922,14 +923,18 @@ parse_key(location& loc)
} }
loc.reset(first); loc.reset(first);
// simple key -> foo // simple_key: a single (basic_string|literal_string|bare key)
if(const auto smpl = parse_simple_key(loc)) if(const auto smpl = parse_simple_key(loc))
{ {
return ok(std::make_pair(std::vector<key>(1, smpl.unwrap().first), return ok(std::make_pair(std::vector<key>(1, smpl.unwrap().first),
smpl.unwrap().second)); smpl.unwrap().second));
} }
return err(format_underline("toml::parse_key: ", return err(format_underline("toml::parse_key: an invalid key appeaed.",
{{source_location(loc), "is not a valid key"}})); {{source_location(loc), "is not a valid key"}}, {
"bare keys : non-empty strings composed only of [A-Za-z0-9_-].",
"quoted keys: same as \"basic strings\" or 'literal strings'.",
"dotted keys: sequence of bare or quoted keys joined with a dot."
}));
} }
// forward-decl to implement parse_array and parse_table // forward-decl to implement parse_array and parse_table