From 39b80f6c5677c14ce750fb179617019f27cc52a8 Mon Sep 17 00:00:00 2001 From: Mark Gillard Date: Sat, 14 May 2022 15:22:21 +0300 Subject: [PATCH] fixed table source columns being off by one closes #152 also other minor refactors --- CHANGELOG.md | 3 ++ docs/pages/main_page.dox | 62 +++++++++++++++++----------------- examples/toml_generator.cpp | 19 ++++++----- include/toml++/impl/parser.inl | 2 +- tests/user_feedback.cpp | 30 ++++++++++++++++ toml.hpp | 2 +- tools/requirements.txt | 2 +- 7 files changed, 77 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e61171c..f0c2d62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ template: ## Unreleased +#### Fixes: +- Fixed `[dotted.table]` source columns sometimes being off by one (#152) (@vaartis) + #### Additions: - Added value type deduction to `emplace()` methods diff --git a/docs/pages/main_page.dox b/docs/pages/main_page.dox index 7d0c557..3b91c7d 100644 --- a/docs/pages/main_page.dox +++ b/docs/pages/main_page.dox @@ -55,7 +55,7 @@ int main(int argc, char** argv) return 0; } -\ecpp +\endcpp \see - toml::parse_file() @@ -108,7 +108,7 @@ int main() return 0; } -\ecpp +\endcpp \out [library] @@ -120,7 +120,7 @@ name = 'toml++' authors = [ 'Mark Gillard ' ] cpp = 17 name = 'toml++' -\eout +\endout \see - toml::parse_file() @@ -152,7 +152,7 @@ int main() do_stuff_with_your_config(std::move(result).table()); // 'steal' the table from the result return 0; } -\ecpp +\endcpp @@ -161,7 +161,7 @@ The examples above use an overloaded `operator<<` with ostreams to print basic e \out Error while parsing key: expected bare key starting character or string delimiter, saw '?' (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 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"; return 1; } -\ecpp +\endcpp \see - toml::parse_error @@ -267,7 +267,7 @@ int main() return 0; } -\ecpp +\endcpp \out hello world @@ -281,7 +281,7 @@ numbers: [ 2, 3, 4, 'five', 6.0, 7, [ 8, 9 ] ] cats: [ 'tiger', 'lion', 'puma' ] fish[1]: 'trout' dinosaurs: -\eout +\endout \see - toml::node @@ -332,7 +332,7 @@ int main() return 0; } -\ecpp +\endcpp \out ###### TOML ###### @@ -383,7 +383,7 @@ repo: 'https://github.com/marzer/tomlplusplus/' toml: - '1.0.0' - 'and beyond' -\eout +\endout \see - 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 #include -\ecpp +\endcpp Step 2: Define #TOML_IMPLEMENTATION before including toml++ in one specific translation unit @@ -416,7 +416,7 @@ do it manually before including toml++ in some global header that's used everywh #define TOML_IMPLEMENTATION #include "global_header_that_includes_toml++.h" -\ecpp +\endcpp Bonus Step: Disable any library features you don't need @@ -459,11 +459,11 @@ Add `tomlplusplus/3.1.0` to your conanfile. \subsection mainpage-adding-lib-dds DDS Add `tomlpp` to your `package.json5`, e.g.: -\bash +\json depends: [ 'tomlpp^3.1.0', ] -\ebash +\endjson \see [What is DDS?](https://dds.pizza/) @@ -472,37 +472,37 @@ depends: [ \subsection mainpage-adding-lib-meson Meson You can install the wrap with: -\bash +\shell meson wrap install tomlplusplus -\ebash +\endshell After that, you can use it like a regular dependency: -``` +\meson tomlplusplus_dep = dependency('tomlplusplus') -``` +\endmeson 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`: - \bash +\json { - "marzer/tomlplusplus": { } + "marzer/tomlplusplus": { } } - \ebash +\endjson \subsection mainpage-adding-lib-vcpkg Vcpkg -\bash +\shell vcpkg install tomlplusplus -\ebash +\endshell \subsection mainpage-adding-lib-cmake-fetch-content CMake FetchContent -\code{.cmake} +\cmake include(FetchContent) FetchContent_Declare( tomlplusplus @@ -510,17 +510,17 @@ FetchContent_Declare( GIT_TAG v3.1.0 ) FetchContent_MakeAvailable(tomlplusplus) -\endcode +\endcmake \see [What is FetchContent?](https://cmake.org/cmake/help/latest/module/FetchContent.html) \subsection mainpage-adding-lib-git-submodules Git submodules -\bash +\shell git submodule add --depth 1 https://github.com/marzer/tomlplusplus.git tomlplusplus 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**! 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) qtoml: 7.760 s (11.17x) tomlkit: 32.708 s (47.09x) -\eout +\endout Install it using `pip`: -\bash +\shell 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 implementation please do so at their repository, not on the main toml++ one. diff --git a/examples/toml_generator.cpp b/examples/toml_generator.cpp index 4244e7b..5a51d77 100644 --- a/examples/toml_generator.cpp +++ b/examples/toml_generator.cpp @@ -68,6 +68,12 @@ namespace return static_cast(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(integer(0, 1000)) <= val; + } + [[nodiscard]] static toml::date date() noexcept { return toml::date{ integer(1900, 2021), // @@ -80,7 +86,7 @@ namespace return toml::time{ integer(24), // integer(60), integer(60), - integer(100) > 80 ? integer(1000000000u) : 0u }; + boolean() ? integer(1000000000u) : 0u }; } [[nodiscard]] static toml::time_offset time_offset() noexcept @@ -90,8 +96,8 @@ namespace [[nodiscard]] static toml::date_time date_time() noexcept { - return integer(100) >= 75 ? toml::date_time{ date(), time() } - : toml::date_time{ date(), time(), time_offset() }; + return boolean() ? toml::date_time{ date(), time() } // + : toml::date_time{ date(), time(), time_offset() }; } [[nodiscard]] static std::string_view word() noexcept @@ -116,11 +122,6 @@ namespace 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(integer(0, 1000)) <= val; - } } template @@ -149,7 +150,7 @@ namespace { static_assert(toml::is_container); - switch (rand() % 7) + switch (random::integer(7)) { case 0: add_to(container, random::string(random::integer(8u))); break; case 1: add_to(container, random::integer(1000)); break; diff --git a/include/toml++/impl/parser.inl b/include/toml++/impl/parser.inl index d72f587..3de12b7 100644 --- a/include/toml++/impl/parser.inl +++ b/include/toml++/impl/parser.inl @@ -3225,7 +3225,7 @@ TOML_IMPL_NAMESPACE_START { pit = parent->emplace_hint(pit, make_key(i)); table& p = pit->second.ref_cast
(); - p.source_ = pit->first.source(); + p.source_ = { header_begin_pos, header_end_pos, reader.source_path() }; implicit_tables.push_back(&p); parent = &p; diff --git a/tests/user_feedback.cpp b/tests/user_feedback.cpp index 4d136be..71bec13 100644 --- a/tests/user_feedback.cpp +++ b/tests/user_feedback.cpp @@ -320,4 +320,34 @@ b = [] "1=1\n" "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(line)); + CHECK(v.node()->source().begin.column == static_cast(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); + }); + } } diff --git a/toml.hpp b/toml.hpp index a8c7416..157b166 100644 --- a/toml.hpp +++ b/toml.hpp @@ -13752,7 +13752,7 @@ TOML_IMPL_NAMESPACE_START { pit = parent->emplace_hint
(pit, make_key(i)); table& p = pit->second.ref_cast
(); - p.source_ = pit->first.source(); + p.source_ = { header_begin_pos, header_end_pos, reader.source_path() }; implicit_tables.push_back(&p); parent = &p; diff --git a/tools/requirements.txt b/tools/requirements.txt index 3f42e60..facdc59 100644 --- a/tools/requirements.txt +++ b/tools/requirements.txt @@ -1,4 +1,4 @@ misk>=0.6.0 -poxy>=0.5.5 +poxy>=0.5.6 pyyaml python-dateutil