Preserve empty location for empty files

Without a region, error messages in exceptions are unable to print
a filename.

By retaining the location in a zero-length region and detecting this
when formatting the exception text it is possible to print the filename
and explicitly state that the file is completely empty.

Fixes #160
This commit is contained in:
Oliver Kahrmann
2021-05-25 20:51:08 +02:00
parent 0c4594f59a
commit 58542d36be
3 changed files with 18 additions and 3 deletions

View File

@@ -1977,7 +1977,9 @@ result<Value, std::string> parse_toml_file(location& loc)
const auto first = loc.iter();
if(first == loc.end())
{
return ok(value_type(table_type{} /*, empty file has no region ...*/));
// For empty files, return an empty table with an empty region (zero-length).
// Without the region, error messages would miss the filename.
return ok(value_type(table_type{}, region(loc, first, first), {}));
}
// put the first line as a region of a file