Skip to content

misut/tomlcpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tomlcpp

A TOML parser for C++. No dependencies.

Installation

exon

[dependencies]
"github.com/misut/tomlcpp" = "0.3.1"

CMake

add_library(tomlcpp)
target_sources(tomlcpp PUBLIC FILE_SET CXX_MODULES FILES path/to/toml.cppm)
target_link_libraries(your_target PRIVATE tomlcpp)

Quick Start

import toml;

auto table = toml::parse(R"(
    [server]
    host = "localhost"
    port = 8080
)");

auto host = table.at("server").as_table().at("host").as_string();
auto port = table.at("server").as_table().at("port").as_integer();

API

Parsing

toml::Table parse(std::string_view input);
toml::Table parse_file(std::string_view path);

Types

Type Alias
toml::Table std::map<std::string, Value>
toml::Array std::vector<Value>
toml::Value variant of string, int64, double, bool, Array, Table

Value Methods

// Type checks
v.is_string()   v.is_integer()  v.is_float()
v.is_bool()     v.is_array()    v.is_table()

// Accessors
v.as_string()   v.as_integer()  v.as_float()
v.as_bool()     v.as_array()    v.as_table()

// Table helpers
v.contains("key")
v["key"]

Errors

try {
    auto t = toml::parse(input);
} catch (toml::ParseError const& e) {
    // e.what() → "line 3: expected '='"
    // e.line() → 3
}

When the translation unit is compiled with -fno-exceptions (for example the wasi-sdk WebAssembly target), parse failures print toml: line N: <msg> to stderr and call std::abort() instead of throwing. Callers targeting WASM should validate input before invoking parse or ensure inputs are trusted.

Supported TOML features

  • Strings (basic, literal, escape sequences)
  • Integers, floats (including exponent notation), booleans
  • Arrays (including nested, trailing comma, multi-line with comments)
  • Tables and nested tables ([a.b.c])
  • Arrays of tables ([[items]])
  • Inline tables (x = { a = 1, b = [2, 3] })
  • Quoted keys, comments

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors