Skip to content

Commit

Permalink
fixed table source columns being off by one
Browse files Browse the repository at this point in the history
closes #152

also other minor refactors
  • Loading branch information
marzer committed May 14, 2022
1 parent e55ac02 commit 39b80f6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
62 changes: 31 additions & 31 deletions docs/pages/main_page.dox
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char** argv)
return 0;
}

\ecpp
\endcpp

\see
- toml::parse_file()
Expand Down Expand Up @@ -108,7 +108,7 @@ int main()

return 0;
}
\ecpp
\endcpp

\out
[library]
Expand All @@ -120,7 +120,7 @@ name = 'toml++'
authors = [ 'Mark Gillard <[email protected]>' ]
cpp = 17
name = 'toml++'
\eout
\endout

\see
- toml::parse_file()
Expand Down Expand Up @@ -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



Expand All @@ -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()
Expand All @@ -181,7 +181,7 @@ catch (const toml::parse_error& err)
<< "\n (" << err.source().begin << ")\n";
return 1;
}
\ecpp
\endcpp

\see
- toml::parse_error
Expand Down Expand Up @@ -267,7 +267,7 @@ int main()

return 0;
}
\ecpp
\endcpp

\out
hello world
Expand All @@ -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
Expand Down Expand Up @@ -332,7 +332,7 @@ int main()

return 0;
}
\ecpp
\endcpp

\out
###### TOML ######
Expand Down Expand Up @@ -383,7 +383,7 @@ repo: 'https://github.com/marzer/tomlplusplus/'
toml:
- '1.0.0'
- 'and beyond'
\eout
\endout

\see
- toml::toml_formatter
Expand All @@ -407,7 +407,7 @@ do it manually before including toml++ in some global header that's used everywh

#define TOML_HEADER_ONLY 0
#include <toml.hpp>
\ecpp
\endcpp

<strong>Step 2: Define #TOML_IMPLEMENTATION before including toml++ in one specific translation unit</strong>

Expand All @@ -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

<strong>Bonus Step: Disable any library features you don't need</strong>

Expand Down Expand Up @@ -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/)

Expand All @@ -472,55 +472,55 @@ 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
GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git
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.

Expand All @@ -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.
Expand Down
19 changes: 10 additions & 9 deletions examples/toml_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ namespace
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
{
return toml::date{ integer(1900, 2021), //
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<float>(integer(0, 1000)) <= val;
}
}

template <typename T>
Expand Down Expand Up @@ -149,7 +150,7 @@ namespace
{
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 1: add_to(container, random::integer(1000)); break;
Expand Down
2 changes: 1 addition & 1 deletion include/toml++/impl/parser.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,7 @@ TOML_IMPL_NAMESPACE_START
{
pit = parent->emplace_hint<table>(pit, make_key(i));
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);
parent = &p;
Expand Down
30 changes: 30 additions & 0 deletions tests/user_feedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
});
}
}
2 changes: 1 addition & 1 deletion toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13752,7 +13752,7 @@ TOML_IMPL_NAMESPACE_START
{
pit = parent->emplace_hint<table>(pit, make_key(i));
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);
parent = &p;
Expand Down
2 changes: 1 addition & 1 deletion tools/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
misk>=0.6.0
poxy>=0.5.5
poxy>=0.5.6
pyyaml
python-dateutil

0 comments on commit 39b80f6

Please sign in to comment.