From 3d86f3a4e1438ea8ab3fab8d8925a9f4582d3e46 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 13 Oct 2020 21:59:46 +0900 Subject: [PATCH] fix: avoid comment duplication in array of tables --- toml/parser.hpp | 87 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index a52d8c9..f6ba78a 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -1315,7 +1315,41 @@ insert_nested_key(typename Value::table_type& root, const Value& v, } else // if not, we need to create the array of table { - value_type aot(array_type(1, v), key_reg); + // XXX: Consider the following array of tables. + // ```toml + // # This is a comment. + // [[aot]] + // foo = "bar" + // ``` + // Here, the comment is for `aot`. But here, actually two + // values are defined. An array that contains tables, named + // `aot`, and the 0th element of the `aot`, `{foo = "bar"}`. + // Those two are different from each other. But both of them + // points to the same portion of the TOML file, `[[aot]]`, + // so `key_reg.comments()` returns `# This is a comment`. + // If it is assigned as a comment of `aot` defined here, the + // comment will be duplicated. Both the `aot` itself and + // the 0-th element will have the same comment. This causes + // "duplication of the same comments" bug when the data is + // serialized. + // Next, consider the following. + // ```toml + // # comment 1 + // aot = [ + // # coment 2 + // {foo = "bar"}, + // ] + // ``` + // In this case, we can distinguish those two comments. So + // here we need to add "comment 1" to the `aot` and + // "comment 2" to the 0th element of that. + // To distinguish those two, we check the key region. + std::vector comments{/* empty by default */}; + if(key_reg.str().substr(0, 2) != "[[") + { + comments = key_reg.comments(); + } + value_type aot(array_type(1, v), key_reg, std::move(comments)); tab->insert(std::make_pair(k, aot)); return ok(true); } @@ -1384,7 +1418,8 @@ insert_nested_key(typename Value::table_type& root, const Value& v, // [x.y.z] if(tab->count(k) == 0) { - (*tab)[k] = value_type(table_type{}, key_reg); + // a table that is defined implicitly doesn't have any comments. + (*tab)[k] = value_type(table_type{}, key_reg, {/*no comment*/}); } // type checking... @@ -1674,11 +1709,24 @@ inline result guess_value_type(const location& loc) } } +template +result +parse_value_helper(result, std::string> rslt) +{ + if(rslt.is_ok()) + { + auto comments = rslt.as_ok().second.comments(); + return ok(Value(std::move(rslt.as_ok()), std::move(comments))); + } + else + { + return err(std::move(rslt.as_err())); + } +} + template result parse_value(location& loc) { - using value_type = Value; - const auto first = loc.iter(); if(first == loc.end()) { @@ -1691,18 +1739,19 @@ result parse_value(location& loc) { return err(type.unwrap_err()); } + switch(type.unwrap()) { - case value_t::boolean : {return parse_boolean(loc); } - case value_t::integer : {return parse_integer(loc); } - case value_t::floating : {return parse_floating(loc); } - case value_t::string : {return parse_string(loc); } - case value_t::offset_datetime: {return parse_offset_datetime(loc);} - case value_t::local_datetime : {return parse_local_datetime(loc); } - case value_t::local_date : {return parse_local_date(loc); } - case value_t::local_time : {return parse_local_time(loc); } - case value_t::array : {return parse_array(loc); } - case value_t::table : {return parse_inline_table(loc);} + case value_t::boolean : {return parse_value_helper(parse_boolean(loc) );} + case value_t::integer : {return parse_value_helper(parse_integer(loc) );} + case value_t::floating : {return parse_value_helper(parse_floating(loc) );} + case value_t::string : {return parse_value_helper(parse_string(loc) );} + case value_t::offset_datetime: {return parse_value_helper(parse_offset_datetime(loc) );} + case value_t::local_datetime : {return parse_value_helper(parse_local_datetime(loc) );} + case value_t::local_date : {return parse_value_helper(parse_local_date(loc) );} + case value_t::local_time : {return parse_value_helper(parse_local_time(loc) );} + case value_t::array : {return parse_value_helper(parse_array(loc) );} + case value_t::table : {return parse_value_helper(parse_inline_table(loc));} default: { const auto msg = format_underline("toml::parse_value: " @@ -1993,7 +2042,7 @@ result parse_toml_file(location& loc) const auto& reg = tk.second; const auto inserted = insert_nested_key(data, - value_type(tab.unwrap(), reg), + value_type(tab.unwrap(), reg, reg.comments()), keys.begin(), keys.end(), reg, /*is_array_of_table=*/ true); if(!inserted) {return err(inserted.unwrap_err());} @@ -2010,7 +2059,8 @@ result parse_toml_file(location& loc) const auto& reg = tk.second; const auto inserted = insert_nested_key(data, - value_type(tab.unwrap(), reg), keys.begin(), keys.end(), reg); + value_type(tab.unwrap(), reg, reg.comments()), + keys.begin(), keys.end(), reg); if(!inserted) {return err(inserted.unwrap_err());} continue; @@ -2019,10 +2069,7 @@ result parse_toml_file(location& loc) "unknown line appeared", {{source_location(loc), "unknown format"}})); } - Value v(std::move(data), file); - v.comments() = comments; - - return ok(std::move(v)); + return ok(Value(std::move(data), file, comments)); } } // detail