mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-12-16 03:08:52 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1827e6fca | ||
|
|
18f84088b4 | ||
|
|
c199bd8b49 | ||
|
|
5b35c1a74e | ||
|
|
d3513c0f84 |
@@ -46,3 +46,19 @@ BOOST_AUTO_TEST_CASE(test_inline_table_value)
|
|||||||
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "{type.name = \"pug\"}", value(t));
|
TOML11_TEST_PARSE_EQUAL_VALUE(parse_value<toml::value>, "{type.name = \"pug\"}", value(t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(test_inline_table_immutability)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
std::istringstream stream(std::string(
|
||||||
|
"a = {b = 1}\n"
|
||||||
|
"a.c = 2\n"));
|
||||||
|
BOOST_CHECK_THROW(toml::parse(stream), toml::syntax_error);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
std::istringstream stream(std::string(
|
||||||
|
"a = {b = {c = 1}}\n"
|
||||||
|
"a.b.d = 2\n"));
|
||||||
|
BOOST_CHECK_THROW(toml::parse(stream), toml::syntax_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1331,7 +1331,7 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
|
|||||||
tab->insert(std::make_pair(k, v));
|
tab->insert(std::make_pair(k, v));
|
||||||
return ok(true);
|
return ok(true);
|
||||||
}
|
}
|
||||||
else
|
else // k is not the last one, we should insert recursively
|
||||||
{
|
{
|
||||||
// if there is no corresponding value, insert it first.
|
// if there is no corresponding value, insert it first.
|
||||||
// related: you don't need to write
|
// related: you don't need to write
|
||||||
@@ -1347,6 +1347,27 @@ insert_nested_key(typename Value::table_type& root, const Value& v,
|
|||||||
// type checking...
|
// type checking...
|
||||||
if(tab->at(k).is_table())
|
if(tab->at(k).is_table())
|
||||||
{
|
{
|
||||||
|
// According to toml-lang/toml:36d3091b3 "Clarify that inline
|
||||||
|
// tables are immutable", check if it adds key-value pair to an
|
||||||
|
// inline table.
|
||||||
|
// This is one of the unreleased (after-0.5.0) toml feature.
|
||||||
|
// But this is marked as "Clarify", so TOML-lang intended that
|
||||||
|
// inline tables are immutable in all version.
|
||||||
|
{
|
||||||
|
// here, if the value is a (multi-line) table, the region
|
||||||
|
// should be something like `[table-name]`.
|
||||||
|
if(get_region(tab->at(k)).front() == '{')
|
||||||
|
{
|
||||||
|
throw syntax_error(format_underline(concat_to_string(
|
||||||
|
"toml::insert_value: inserting to an inline table (",
|
||||||
|
format_dotted_keys(first, std::next(iter)),
|
||||||
|
") but inline tables are immutable"), {
|
||||||
|
{std::addressof(get_region(tab->at(k))),
|
||||||
|
"inline tables are immutable"},
|
||||||
|
{std::addressof(get_region(v)), "inserting this"}
|
||||||
|
}), v.location());
|
||||||
|
}
|
||||||
|
}
|
||||||
tab = std::addressof((*tab)[k].as_table());
|
tab = std::addressof((*tab)[k].as_table());
|
||||||
}
|
}
|
||||||
else if(tab->at(k).is_array()) // inserting to array-of-tables?
|
else if(tab->at(k).is_array()) // inserting to array-of-tables?
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ struct region_base
|
|||||||
region_base& operator=(region_base&& ) = default;
|
region_base& operator=(region_base&& ) = default;
|
||||||
|
|
||||||
virtual bool is_ok() const noexcept {return false;}
|
virtual bool is_ok() const noexcept {return false;}
|
||||||
|
virtual char front() const noexcept {return '\0';}
|
||||||
|
|
||||||
virtual std::string str() const {return std::string("unknown region");}
|
virtual std::string str() const {return std::string("unknown region");}
|
||||||
virtual std::string name() const {return std::string("unknown file");}
|
virtual std::string name() const {return std::string("unknown file");}
|
||||||
@@ -89,6 +90,7 @@ struct location final : public region_base
|
|||||||
~location() = default;
|
~location() = default;
|
||||||
|
|
||||||
bool is_ok() const noexcept override {return static_cast<bool>(source_);}
|
bool is_ok() const noexcept override {return static_cast<bool>(source_);}
|
||||||
|
char front() const noexcept override {return *iter_;}
|
||||||
|
|
||||||
// this const prohibits codes like `++(loc.iter())`.
|
// this const prohibits codes like `++(loc.iter())`.
|
||||||
const const_iterator iter() const noexcept {return iter_;}
|
const const_iterator iter() const noexcept {return iter_;}
|
||||||
@@ -240,6 +242,7 @@ struct region final : public region_base
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool is_ok() const noexcept override {return static_cast<bool>(source_);}
|
bool is_ok() const noexcept override {return static_cast<bool>(source_);}
|
||||||
|
char front() const noexcept override {return *first_;}
|
||||||
|
|
||||||
std::string str() const override {return make_string(first_, last_);}
|
std::string str() const override {return make_string(first_, last_);}
|
||||||
std::string line() const override
|
std::string line() const override
|
||||||
@@ -476,8 +479,9 @@ inline std::string format_underline(const std::string& message,
|
|||||||
const region_base* const reg = iter->first;
|
const region_base* const reg = iter->first;
|
||||||
const std::string& comment = iter->second;
|
const std::string& comment = iter->second;
|
||||||
|
|
||||||
retval << ' ' << std::setw(line_num_width) << color::bold << color::blue
|
retval << ' ' << color::bold << color::blue << std::setw(line_num_width)
|
||||||
<< reg->line_num() << " | " << color::reset << reg->line() << '\n';
|
<< std::right << reg->line_num() << " | " << color::reset
|
||||||
|
<< reg->line() << '\n';
|
||||||
|
|
||||||
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ')
|
retval << make_string(static_cast<std::size_t>(line_num_width + 1), ' ')
|
||||||
<< color::bold << color::blue << " | " << color::reset
|
<< color::bold << color::blue << " | " << color::reset
|
||||||
|
|||||||
Reference in New Issue
Block a user