From c63ac7e43574c621a0088d81c3938f9582fbb9dd Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 22 Dec 2018 17:07:06 +0900 Subject: [PATCH] detect syntax_error; appending array-of-tables toml file like the following is explicitly prohibited. a = [{b = 1}] [[a]] b = 2 this commit detects this kind of syntax-error while parsing toml file --- toml/parser.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/toml/parser.hpp b/toml/parser.hpp index b496831..92c4c65 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -1050,6 +1050,31 @@ insert_nested_key(table& root, const toml::value& v, " value already exists"), get_region(v), "while inserting this array-of-tables")); } + // avoid conflicting array of table like the following. + // ```toml + // a = [{b = 42}] # define a as an array of *inline* tables + // [[a]] # a is an array of *multi-line* tables + // b = 54 + // ``` + // Here, from the type information, these cannot be detected + // bacause inline table is also a table. + // But toml v0.5.0 explicitly says it is invalid. The above + // array-of-tables has a static size and appending to the + // array is invalid. + // In this library, multi-line table value has a region + // that points to the key of the table (e.g. [[a]]). By + // comparing the first two letters in key, we can detect + // the array-of-table is inline or multiline. + if(detail::get_region(a.front()).str().substr(0,2) != "[[") + { + throw syntax_error(format_underline(concat_to_string( + "[error] toml::insert_value: array of table (\"", + format_dotted_keys(first, last), "\") collides with" + " existing array-of-tables"), get_region(tab->at(k)), + concat_to_string("this ", tab->at(k).type(), + " value has static size"), get_region(v), + "appending this to the statically sized array")); + } a.push_back(v); return ok(true); }