fixed table source columns being off by one

closes #152

also other minor refactors
This commit is contained in:
Mark Gillard 2022-05-14 15:22:21 +03:00
parent e55ac0288f
commit 39b80f6c56
7 changed files with 77 additions and 43 deletions

View File

@ -15,6 +15,9 @@ template:
## Unreleased ## Unreleased
#### Fixes:
- Fixed `[dotted.table]` source columns sometimes being off by one (#152) (@vaartis)
#### Additions: #### Additions:
- Added value type deduction to `emplace()` methods - Added value type deduction to `emplace()` methods

View File

@ -55,7 +55,7 @@ int main(int argc, char** argv)
return 0; return 0;
} }
\ecpp \endcpp
\see \see
- toml::parse_file() - toml::parse_file()
@ -108,7 +108,7 @@ int main()
return 0; return 0;
} }
\ecpp \endcpp
\out \out
[library] [library]
@ -120,7 +120,7 @@ name = 'toml++'
authors = [ 'Mark Gillard <mark.gillard@outlook.com.au>' ] authors = [ 'Mark Gillard <mark.gillard@outlook.com.au>' ]
cpp = 17 cpp = 17
name = 'toml++' name = 'toml++'
\eout \endout
\see \see
- toml::parse_file() - toml::parse_file()
@ -152,7 +152,7 @@ int main()
do_stuff_with_your_config(std::move(result).table()); // 'steal' the table from the result do_stuff_with_your_config(std::move(result).table()); // 'steal' the table from the result
return 0; return 0;
} }
\ecpp \endcpp
@ -161,7 +161,7 @@ The examples above use an overloaded `operator<<` with ostreams to print basic e
\out \out
Error while parsing key: expected bare key starting character or string delimiter, saw '?' Error while parsing key: expected bare key starting character or string delimiter, saw '?'
(error occurred at line 2, column 5) (error occurred at line 2, column 5)
\eout \endout
The library doesn't natively support error colouring in TTY environments, but instead provides the requisite information The library doesn't natively support error colouring in TTY environments, but instead provides the requisite information
for you to build that and any other custom error handling yourself if necessary via toml::parse_error's source() for you to build that and any other custom error handling yourself if necessary via toml::parse_error's source()
@ -181,7 +181,7 @@ catch (const toml::parse_error& err)
<< "\n (" << err.source().begin << ")\n"; << "\n (" << err.source().begin << ")\n";
return 1; return 1;
} }
\ecpp \endcpp
\see \see
- toml::parse_error - toml::parse_error
@ -267,7 +267,7 @@ int main()
return 0; return 0;
} }
\ecpp \endcpp
\out \out
hello world hello world
@ -281,7 +281,7 @@ numbers: [ 2, 3, 4, 'five', 6.0, 7, [ 8, 9 ] ]
cats: [ 'tiger', 'lion', 'puma' ] cats: [ 'tiger', 'lion', 'puma' ]
fish[1]: 'trout' fish[1]: 'trout'
dinosaurs: dinosaurs:
\eout \endout
\see \see
- toml::node - toml::node
@ -332,7 +332,7 @@ int main()
return 0; return 0;
} }
\ecpp \endcpp
\out \out
###### TOML ###### ###### TOML ######
@ -383,7 +383,7 @@ repo: 'https://github.com/marzer/tomlplusplus/'
toml: toml:
- '1.0.0' - '1.0.0'
- 'and beyond' - 'and beyond'
\eout \endout
\see \see
- toml::toml_formatter - toml::toml_formatter
@ -407,7 +407,7 @@ do it manually before including toml++ in some global header that's used everywh
#define TOML_HEADER_ONLY 0 #define TOML_HEADER_ONLY 0
#include <toml.hpp> #include <toml.hpp>
\ecpp \endcpp
<strong>Step 2: Define #TOML_IMPLEMENTATION before including toml++ in one specific translation unit</strong> <strong>Step 2: Define #TOML_IMPLEMENTATION before including toml++ in one specific translation unit</strong>
@ -416,7 +416,7 @@ do it manually before including toml++ in some global header that's used everywh
#define TOML_IMPLEMENTATION #define TOML_IMPLEMENTATION
#include "global_header_that_includes_toml++.h" #include "global_header_that_includes_toml++.h"
\ecpp \endcpp
<strong>Bonus Step: Disable any library features you don't need</strong> <strong>Bonus Step: Disable any library features you don't need</strong>
@ -459,11 +459,11 @@ Add `tomlplusplus/3.1.0` to your conanfile.
\subsection mainpage-adding-lib-dds DDS \subsection mainpage-adding-lib-dds DDS
Add `tomlpp` to your `package.json5`, e.g.: Add `tomlpp` to your `package.json5`, e.g.:
\bash \json
depends: [ depends: [
'tomlpp^3.1.0', 'tomlpp^3.1.0',
] ]
\ebash \endjson
\see [What is DDS?](https://dds.pizza/) \see [What is DDS?](https://dds.pizza/)
@ -472,37 +472,37 @@ depends: [
\subsection mainpage-adding-lib-meson Meson \subsection mainpage-adding-lib-meson Meson
You can install the wrap with: You can install the wrap with:
\bash \shell
meson wrap install tomlplusplus meson wrap install tomlplusplus
\ebash \endshell
After that, you can use it like a regular dependency: After that, you can use it like a regular dependency:
``` \meson
tomlplusplus_dep = dependency('tomlplusplus') tomlplusplus_dep = dependency('tomlplusplus')
``` \endmeson
You can also add it as a subproject directly. You can also add it as a subproject directly.
\subsection mainpage-adding-lib-tipi Tipi.build \subsection mainpage-adding-lib-tipi Tipi.build
`tomlplusplus` can be easily used in [tipi.build](https://tipi.build) projects by adding the following entry to your `.tipi/deps`: `tomlplusplus` can be easily used in [tipi.build](https://tipi.build) projects by adding the following entry to your `.tipi/deps`:
\bash \json
{ {
"marzer/tomlplusplus": { } "marzer/tomlplusplus": { }
} }
\ebash \endjson
\subsection mainpage-adding-lib-vcpkg Vcpkg \subsection mainpage-adding-lib-vcpkg Vcpkg
\bash \shell
vcpkg install tomlplusplus vcpkg install tomlplusplus
\ebash \endshell
\subsection mainpage-adding-lib-cmake-fetch-content CMake FetchContent \subsection mainpage-adding-lib-cmake-fetch-content CMake FetchContent
\code{.cmake} \cmake
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
tomlplusplus tomlplusplus
@ -510,17 +510,17 @@ FetchContent_Declare(
GIT_TAG v3.1.0 GIT_TAG v3.1.0
) )
FetchContent_MakeAvailable(tomlplusplus) FetchContent_MakeAvailable(tomlplusplus)
\endcode \endcmake
\see [What is FetchContent?](https://cmake.org/cmake/help/latest/module/FetchContent.html) \see [What is FetchContent?](https://cmake.org/cmake/help/latest/module/FetchContent.html)
\subsection mainpage-adding-lib-git-submodules Git submodules \subsection mainpage-adding-lib-git-submodules Git submodules
\bash \shell
git submodule add --depth 1 https://github.com/marzer/tomlplusplus.git tomlplusplus git submodule add --depth 1 https://github.com/marzer/tomlplusplus.git tomlplusplus
git config -f .gitmodules submodule.tomlplusplus.shallow true git config -f .gitmodules submodule.tomlplusplus.shallow true
\ebash \endshell
\attention The toml++ repository has some submodules of its own, but **they are only used for testing**! \attention The toml++ repository has some submodules of its own, but **they are only used for testing**!
You should **not** use the `--recursive` option for regular library consumption. You should **not** use the `--recursive` option for regular library consumption.
@ -546,13 +546,13 @@ Parsing data.toml 5000 times:
toml: 5.642 s ( 8.12x) toml: 5.642 s ( 8.12x)
qtoml: 7.760 s (11.17x) qtoml: 7.760 s (11.17x)
tomlkit: 32.708 s (47.09x) tomlkit: 32.708 s (47.09x)
\eout \endout
Install it using `pip`: Install it using `pip`:
\bash \shell
pip install pytomlpp pip install pytomlpp
\ebash \endshell
Note that I'm not the owner of that project, so if you wish to report a bug relating to the python Note that I'm not the owner of that project, so if you wish to report a bug relating to the python
implementation please do so at their repository, not on the main toml++ one. implementation please do so at their repository, not on the main toml++ one.

View File

@ -68,6 +68,12 @@ namespace
return static_cast<T>(incl_min + integer(excl_max - incl_min)); return static_cast<T>(incl_min + integer(excl_max - incl_min));
} }
[[nodiscard]] static bool chance(float val) noexcept
{
val = (val < 0.0f ? 0.0f : (val > 1.0f ? 1.0f : val)) * 1000.0f;
return static_cast<float>(integer(0, 1000)) <= val;
}
[[nodiscard]] static toml::date date() noexcept [[nodiscard]] static toml::date date() noexcept
{ {
return toml::date{ integer(1900, 2021), // return toml::date{ integer(1900, 2021), //
@ -80,7 +86,7 @@ namespace
return toml::time{ integer(24), // return toml::time{ integer(24), //
integer(60), integer(60),
integer(60), integer(60),
integer(100) > 80 ? integer(1000000000u) : 0u }; boolean() ? integer(1000000000u) : 0u };
} }
[[nodiscard]] static toml::time_offset time_offset() noexcept [[nodiscard]] static toml::time_offset time_offset() noexcept
@ -90,8 +96,8 @@ namespace
[[nodiscard]] static toml::date_time date_time() noexcept [[nodiscard]] static toml::date_time date_time() noexcept
{ {
return integer(100) >= 75 ? toml::date_time{ date(), time() } return boolean() ? toml::date_time{ date(), time() } //
: toml::date_time{ date(), time(), time_offset() }; : toml::date_time{ date(), time(), time_offset() };
} }
[[nodiscard]] static std::string_view word() noexcept [[nodiscard]] static std::string_view word() noexcept
@ -116,11 +122,6 @@ namespace
return random::string(random::integer(1u, 4u), '-'); return random::string(random::integer(1u, 4u), '-');
} }
[[nodiscard]] static bool chance(float val) noexcept
{
val = (val < 0.0f ? 0.0f : (val > 1.0f ? 1.0f : val)) * 1000.0f;
return static_cast<float>(integer(0, 1000)) <= val;
}
} }
template <typename T> template <typename T>
@ -149,7 +150,7 @@ namespace
{ {
static_assert(toml::is_container<Container>); static_assert(toml::is_container<Container>);
switch (rand() % 7) switch (random::integer(7))
{ {
case 0: add_to(container, random::string(random::integer(8u))); break; case 0: add_to(container, random::string(random::integer(8u))); break;
case 1: add_to(container, random::integer(1000)); break; case 1: add_to(container, random::integer(1000)); break;

View File

@ -3225,7 +3225,7 @@ TOML_IMPL_NAMESPACE_START
{ {
pit = parent->emplace_hint<table>(pit, make_key(i)); pit = parent->emplace_hint<table>(pit, make_key(i));
table& p = pit->second.ref_cast<table>(); table& p = pit->second.ref_cast<table>();
p.source_ = pit->first.source(); p.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
implicit_tables.push_back(&p); implicit_tables.push_back(&p);
parent = &p; parent = &p;

View File

@ -320,4 +320,34 @@ b = []
"1=1\n" "1=1\n"
"2=2\n"sv); "2=2\n"sv);
} }
SECTION("github/issues/152") // https://github.com/marzer/tomlplusplus/issues/152
{
// clang-format off
static constexpr auto data = R"([shaders.room_darker])" "\n"
R"(file = "room_darker.frag")" "\n"
R"(args = { n = "integer", ambientLightLevel = "float" })";
// clang-format on
parsing_should_succeed(FILE_LINE_ARGS,
data,
[](auto&& tbl)
{
const auto check_location = [&](std::string_view path, auto line, auto col)
{
INFO("Checking source location of \""sv << path << "\""sv)
auto v = tbl.at_path(path);
REQUIRE(v.node());
CHECK(v.node()->source().begin.line == static_cast<toml::source_index>(line));
CHECK(v.node()->source().begin.column == static_cast<toml::source_index>(col));
};
check_location("shaders"sv, 1, 1);
check_location("shaders.room_darker"sv, 1, 1);
check_location("shaders.room_darker.file"sv, 2, 8);
check_location("shaders.room_darker.args"sv, 3, 8);
check_location("shaders.room_darker.args.n"sv, 3, 14);
check_location("shaders.room_darker.args.ambientLightLevel"sv, 3, 45);
});
}
} }

View File

@ -13752,7 +13752,7 @@ TOML_IMPL_NAMESPACE_START
{ {
pit = parent->emplace_hint<table>(pit, make_key(i)); pit = parent->emplace_hint<table>(pit, make_key(i));
table& p = pit->second.ref_cast<table>(); table& p = pit->second.ref_cast<table>();
p.source_ = pit->first.source(); p.source_ = { header_begin_pos, header_end_pos, reader.source_path() };
implicit_tables.push_back(&p); implicit_tables.push_back(&p);
parent = &p; parent = &p;

View File

@ -1,4 +1,4 @@
misk>=0.6.0 misk>=0.6.0
poxy>=0.5.5 poxy>=0.5.6
pyyaml pyyaml
python-dateutil python-dateutil