marzer / tomlplusplus

Header-only TOML config file parser and serializer for C++17.
https://marzer.github.io/tomlplusplus/
MIT License
1.47k stars 141 forks source link

"escape sequence '\e' is not supported in TOML 1.0.0 and earlier" even with TOML_LANG_UNRELEASED defined #225

Closed Toni500github closed 1 month ago

Toni500github commented 1 month ago

Environment

toml++ version and/or commit hash: 3.4.0

Compiler: GCC 14.1.1 20240507

C++ standard mode: 20

Target arch: x64

Library configuration overrides: TOML_LANG_UNRELEASED=1, TOML_ENABLE_UNRELEASED_FEATURES=1, TOML_UNRELEASED_FEATURES=1, TOML_ENABLE_FORMATTERS=0

Relevant compilation flags:

Describe the bug

I want to parse a file, and when ever I try to parse a string, inside of an array, with the escape code '\e', so that I could print on screen the bash colors, it gives the error: Error while parsing string: escape sequence '\e' is not supported in TOML 1.0.0 and earlier

Looking through the header toml.hpp, I saw there was some macros to define for enabling un-released toml features, TOML_LANG_UNRELEASED, TOML_ENABLE_UNRELEASED_FEATURES, TOML_UNRELEASED_FEATURES.

I tried to #define all of those on a global header and even adding -DTOML_LANG_UNRELEASED=1 DTOML_ENABLE_UNRELEASED_FEATURES=1 -DTOML_UNRELEASED_FEATURES=1 to the compile flags, but nothing works

Steps to reproduce (or a small repo code sample)

So it seems I can't reproduce it with a simple string so i'll post a snippet of my code that produces the error

void Config::loadConfigFile(std::string_view filename) {
    try {
        this->tbl = toml::parse_file(filename); //Config::tbl is a toml::table
    } catch (const toml::parse_error& err) {
        error("Parsing config file {} failed:", filename);
        std::cerr << err << std::endl;
        exit(-1);
    }

    // https://stackoverflow.com/a/78266628
    // changed instead of vector<int> to vector<string>
    // just a workaround for having the layout config variable as a vector<string>
    auto layout_array = tbl["config"]["layout"];
    if (toml::array* arr = layout_array.as_array())
        arr->for_each([this,filename](auto&& el)
        {
            if (const auto* str_elem = el.as_string()) {
                auto v = *str_elem;
                this->layouts.push_back(v->data()); // here's the thing (Config::layouts is a std::vector<std::string>)
            }
            else 
                die("An element of the layout variable in {} is not a string", filename);
        });
}

here's the config

[config]
layout = [
    "=================",
    "Bash command: $(ls -l /boot)",
    "\e[34m my gpu vendor it's $<gpu.vendor>",
    "================="
    ]

Additional information

Toni500github commented 1 month ago

I forgot that I had a toml.cpp where TOML_IMPLEMENTATION and TOML_HEADER_ONLY are defined, and so I added these 3 defines (TOML_LANG_UNRELEASED, TOML_ENABLE_UNRELEASED_FEATURES, TOML_UNRELEASED_FEATURES) to the file, recompile and now it works.

Closing and you can delete this issue if it's useless

marzer commented 1 month ago

Great :)

FYI, the only one of those you should actually need to override is TOML_ENABLE_UNRELEASED_FEATURES. The other two are internal/deprecated.

Toni500github commented 1 month ago

ah ok, just wanted to make it sure lol