feat: adopt VoidLibrary VL-001..VL-013, self-building build.cpp, live logs, and generated CLI help
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 10s
This commit is contained in:
1 parent
ab64147f9d
commit
363fb8200e
33 files changed
+1754
-3155
No files matched your search
+92
-642
@@ -1,13 +1,11 @@
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
#include <Void/Data/Toml.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
#include <set>
|
||||
|
||||
@@ -19,652 +17,101 @@ Void::Result::Error parse_err(const SourceOrigin &orig, std::string msg) {
|
||||
std::format("{}: {}", orig.format(), std::move(msg)));
|
||||
}
|
||||
|
||||
class TomlParser {
|
||||
public:
|
||||
TomlParser(std::string_view text, std::string source_name)
|
||||
: text_{text}, source_name_{std::move(source_name)} {}
|
||||
TomlValue convert_from_void_toml(const Void::Data::Toml::Value &src,
|
||||
const std::string &source_name);
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error> parse() {
|
||||
if (!Util::is_valid_utf8(text_)) {
|
||||
return std::unexpected{parse_err({source_name_, 1, 1}, "invalid UTF-8 in TOML")};
|
||||
}
|
||||
TomlValue root = TomlValue::make_table({source_name_, 1, 1});
|
||||
TomlTable *current_table = &root.as_table_mut();
|
||||
|
||||
while (!eof()) {
|
||||
skip_ws_and_comments(true);
|
||||
if (eof()) {
|
||||
break;
|
||||
}
|
||||
if (peek() == '[') {
|
||||
const auto hdr_origin = origin();
|
||||
advance();
|
||||
const bool is_array_table = (!eof() && peek() == '[');
|
||||
if (is_array_table) {
|
||||
advance();
|
||||
}
|
||||
skip_inline_ws();
|
||||
auto keys = parse_key_path();
|
||||
if (!keys) {
|
||||
return std::unexpected{keys.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (eof() || peek() != ']') {
|
||||
return std::unexpected{parse_err(origin(), "expected ']' in table header")};
|
||||
}
|
||||
advance();
|
||||
if (is_array_table) {
|
||||
if (eof() || peek() != ']') {
|
||||
return std::unexpected{
|
||||
parse_err(origin(), "expected ']]' in array-of-tables header")};
|
||||
}
|
||||
advance();
|
||||
}
|
||||
if (auto r = expect_line_end(); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
auto tbl =
|
||||
navigate_table_header(root.as_table_mut(), *keys, is_array_table, hdr_origin);
|
||||
if (!tbl) {
|
||||
return std::unexpected{tbl.error()};
|
||||
}
|
||||
current_table = *tbl;
|
||||
} else {
|
||||
const auto kv_origin = origin();
|
||||
auto keys = parse_key_path();
|
||||
if (!keys) {
|
||||
return std::unexpected{keys.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (eof() || peek() != '=') {
|
||||
return std::unexpected{parse_err(origin(), "expected '=' after key")};
|
||||
}
|
||||
advance();
|
||||
skip_inline_ws();
|
||||
auto val = parse_value();
|
||||
if (!val) {
|
||||
return std::unexpected{val.error()};
|
||||
}
|
||||
if (auto r = expect_line_end(); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
if (auto r = insert_key_value(*current_table, *keys, std::move(*val), kv_origin);
|
||||
!r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::move(root.as_table_mut());
|
||||
TomlTable convert_table_from_void_toml(const Void::Data::Toml::Table &src,
|
||||
const std::string &source_name) {
|
||||
TomlTable dst;
|
||||
for (const auto &[k, v] : src.entries) {
|
||||
dst.emplace(k, convert_from_void_toml(v, source_name));
|
||||
}
|
||||
|
||||
private:
|
||||
bool eof() const noexcept { return pos_ >= text_.size(); }
|
||||
char peek(std::size_t off = 0) const noexcept {
|
||||
return (pos_ + off < text_.size()) ? text_[pos_ + off] : '\0';
|
||||
}
|
||||
char advance() noexcept {
|
||||
const char c = text_[pos_++];
|
||||
if (c == '\n') {
|
||||
++line_;
|
||||
col_ = 1;
|
||||
} else {
|
||||
++col_;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
SourceOrigin origin() const { return {source_name_, line_, col_}; }
|
||||
|
||||
void skip_inline_ws() noexcept {
|
||||
while (!eof() && (peek() == ' ' || peek() == '\t')) {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
|
||||
void skip_ws_and_comments(bool allow_newlines) noexcept {
|
||||
while (!eof()) {
|
||||
const char c = peek();
|
||||
if (c == ' ' || c == '\t' || (allow_newlines && (c == '\n' || c == '\r'))) {
|
||||
advance();
|
||||
} else if (c == '#') {
|
||||
while (!eof() && peek() != '\n') {
|
||||
advance();
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error> expect_line_end() {
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == '#') {
|
||||
while (!eof() && peek() != '\n') {
|
||||
advance();
|
||||
}
|
||||
}
|
||||
if (eof()) {
|
||||
return {};
|
||||
}
|
||||
if (peek() == '\r') {
|
||||
advance();
|
||||
}
|
||||
if (eof() || peek() == '\n') {
|
||||
if (!eof()) {
|
||||
advance();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
return std::unexpected{parse_err(origin(), "unexpected trailing characters on line")};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> parse_basic_string(bool multiline) {
|
||||
const auto start = origin();
|
||||
if (multiline && !eof() && (peek() == '\n' || (peek() == '\r' && peek(1) == '\n'))) {
|
||||
if (peek() == '\r') {
|
||||
advance();
|
||||
}
|
||||
advance();
|
||||
}
|
||||
std::string out;
|
||||
while (!eof()) {
|
||||
if (multiline && peek() == '"' && peek(1) == '"' && peek(2) == '"') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
if (!multiline && peek() == '"') {
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
const char c = advance();
|
||||
if (!multiline && (c == '\n' || c == '\r')) {
|
||||
return std::unexpected{parse_err(start, "unterminated basic string")};
|
||||
}
|
||||
if (c == '\\') {
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(origin(), "unterminated escape sequence")};
|
||||
}
|
||||
const char esc = advance();
|
||||
switch (esc) {
|
||||
case 'b':
|
||||
out.push_back('\b');
|
||||
break;
|
||||
case 't':
|
||||
out.push_back('\t');
|
||||
break;
|
||||
case 'n':
|
||||
out.push_back('\n');
|
||||
break;
|
||||
case 'f':
|
||||
out.push_back('\f');
|
||||
break;
|
||||
case 'r':
|
||||
out.push_back('\r');
|
||||
break;
|
||||
case '"':
|
||||
out.push_back('"');
|
||||
break;
|
||||
case '\\':
|
||||
out.push_back('\\');
|
||||
break;
|
||||
case '\n':
|
||||
case '\r':
|
||||
case ' ':
|
||||
case '\t':
|
||||
if (!multiline) {
|
||||
return std::unexpected{parse_err(origin(), "invalid escape in string")};
|
||||
}
|
||||
while (!eof() &&
|
||||
(peek() == ' ' || peek() == '\t' || peek() == '\n' || peek() == '\r')) {
|
||||
advance();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return std::unexpected{
|
||||
parse_err(origin(), std::format("unsupported escape '\\{}'", esc))};
|
||||
}
|
||||
} else {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
return std::unexpected{parse_err(start, "unterminated string literal")};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> parse_literal_string(bool multiline) {
|
||||
const auto start = origin();
|
||||
if (multiline && !eof() && (peek() == '\n' || (peek() == '\r' && peek(1) == '\n'))) {
|
||||
if (peek() == '\r') {
|
||||
advance();
|
||||
}
|
||||
advance();
|
||||
}
|
||||
std::string out;
|
||||
while (!eof()) {
|
||||
if (multiline && peek() == '\'' && peek(1) == '\'' && peek(2) == '\'') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
if (!multiline && peek() == '\'') {
|
||||
advance();
|
||||
return out;
|
||||
}
|
||||
const char c = advance();
|
||||
if (!multiline && (c == '\n' || c == '\r')) {
|
||||
return std::unexpected{parse_err(start, "unterminated literal string")};
|
||||
}
|
||||
out.push_back(c);
|
||||
}
|
||||
return std::unexpected{parse_err(start, "unterminated literal string")};
|
||||
}
|
||||
|
||||
Void::Result::Result<std::string, Void::Result::Error> parse_single_key() {
|
||||
skip_inline_ws();
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(origin(), "expected key")};
|
||||
}
|
||||
if (peek() == '"') {
|
||||
advance();
|
||||
return parse_basic_string(false);
|
||||
}
|
||||
if (peek() == '\'') {
|
||||
advance();
|
||||
return parse_literal_string(false);
|
||||
}
|
||||
std::string key;
|
||||
while (!eof()) {
|
||||
const char c = peek();
|
||||
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '_' || c == '-') {
|
||||
key.push_back(advance());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (key.empty()) {
|
||||
return std::unexpected{parse_err(origin(), "empty or invalid bare key")};
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<std::string>, Void::Result::Error> parse_key_path() {
|
||||
std::vector<std::string> parts;
|
||||
auto first = parse_single_key();
|
||||
if (!first) {
|
||||
return std::unexpected{first.error()};
|
||||
}
|
||||
parts.push_back(std::move(*first));
|
||||
for (;;) {
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == '.') {
|
||||
advance();
|
||||
auto next = parse_single_key();
|
||||
if (!next) {
|
||||
return std::unexpected{next.error()};
|
||||
}
|
||||
parts.push_back(std::move(*next));
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlValue, Void::Result::Error> parse_value() {
|
||||
skip_inline_ws();
|
||||
const auto start = origin();
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(start, "expected value")};
|
||||
}
|
||||
const char c = peek();
|
||||
if (c == '"') {
|
||||
if (peek(1) == '"' && peek(2) == '"') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
auto s = parse_basic_string(true);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
advance();
|
||||
auto s = parse_basic_string(false);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
if (c == '\'') {
|
||||
if (peek(1) == '\'' && peek(2) == '\'') {
|
||||
advance();
|
||||
advance();
|
||||
advance();
|
||||
auto s = parse_literal_string(true);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
advance();
|
||||
auto s = parse_literal_string(false);
|
||||
if (!s) {
|
||||
return std::unexpected{s.error()};
|
||||
}
|
||||
return TomlValue{std::move(*s), start};
|
||||
}
|
||||
if (c == '[') {
|
||||
return parse_array();
|
||||
}
|
||||
if (c == '{') {
|
||||
return parse_inline_table();
|
||||
}
|
||||
|
||||
// Read scalar token (bool, int, float).
|
||||
std::string token;
|
||||
while (!eof()) {
|
||||
const char ch = peek();
|
||||
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#' || ch == ',' ||
|
||||
ch == ']' || ch == '}') {
|
||||
break;
|
||||
}
|
||||
token.push_back(advance());
|
||||
}
|
||||
if (token == "true") {
|
||||
return TomlValue{true, start};
|
||||
}
|
||||
if (token == "false") {
|
||||
return TomlValue{false, start};
|
||||
}
|
||||
|
||||
std::string clean;
|
||||
for (const char ch : token) {
|
||||
if (ch != '_') {
|
||||
clean.push_back(ch);
|
||||
}
|
||||
}
|
||||
if (clean.empty()) {
|
||||
return std::unexpected{parse_err(start, "empty scalar value")};
|
||||
}
|
||||
|
||||
// Integer (hex/oct/bin/dec).
|
||||
const bool looks_float = clean.find('.') != std::string::npos ||
|
||||
clean.find('e') != std::string::npos ||
|
||||
clean.find('E') != std::string::npos;
|
||||
if (!looks_float) {
|
||||
int base = 10;
|
||||
std::string_view digits{clean};
|
||||
if (digits.starts_with("0x")) {
|
||||
base = 16;
|
||||
digits.remove_prefix(2);
|
||||
} else if (digits.starts_with("0o")) {
|
||||
base = 8;
|
||||
digits.remove_prefix(2);
|
||||
} else if (digits.starts_with("0b")) {
|
||||
base = 2;
|
||||
digits.remove_prefix(2);
|
||||
} else if (digits.starts_with("+")) {
|
||||
digits.remove_prefix(1);
|
||||
}
|
||||
std::int64_t iv = 0;
|
||||
const auto [ptr, ec] =
|
||||
std::from_chars(digits.data(), digits.data() + digits.size(), iv, base);
|
||||
if (ec == std::errc{} && ptr == digits.data() + digits.size()) {
|
||||
return TomlValue{iv, start};
|
||||
}
|
||||
}
|
||||
double dv = 0.0;
|
||||
const auto [ptr, ec] = std::from_chars(clean.data(), clean.data() + clean.size(), dv);
|
||||
if (ec == std::errc{} && ptr == clean.data() + clean.size()) {
|
||||
return TomlValue{dv, start};
|
||||
}
|
||||
return std::unexpected{parse_err(start, std::format("invalid TOML value '{}'", token))};
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlValue, Void::Result::Error> parse_array() {
|
||||
const auto start = origin();
|
||||
advance(); // '['
|
||||
TomlValue arr = TomlValue::make_array(start);
|
||||
for (;;) {
|
||||
skip_ws_and_comments(true);
|
||||
if (eof()) {
|
||||
return std::unexpected{parse_err(start, "unterminated array")};
|
||||
}
|
||||
if (peek() == ']') {
|
||||
advance();
|
||||
return arr;
|
||||
}
|
||||
auto elem = parse_value();
|
||||
if (!elem) {
|
||||
return std::unexpected{elem.error()};
|
||||
}
|
||||
arr.as_array_mut().push_back(std::move(*elem));
|
||||
skip_ws_and_comments(true);
|
||||
if (!eof() && peek() == ',') {
|
||||
advance();
|
||||
continue;
|
||||
}
|
||||
if (!eof() && peek() == ']') {
|
||||
advance();
|
||||
return arr;
|
||||
}
|
||||
return std::unexpected{parse_err(origin(), "expected ',' or ']' in array")};
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlValue, Void::Result::Error> parse_inline_table() {
|
||||
const auto start = origin();
|
||||
advance(); // '{'
|
||||
TomlValue tbl = TomlValue::make_table(start);
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == '}') {
|
||||
advance();
|
||||
return tbl;
|
||||
}
|
||||
for (;;) {
|
||||
skip_inline_ws();
|
||||
const auto kv_origin = origin();
|
||||
auto keys = parse_key_path();
|
||||
if (!keys) {
|
||||
return std::unexpected{keys.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (eof() || peek() != '=') {
|
||||
return std::unexpected{parse_err(origin(), "expected '=' in inline table")};
|
||||
}
|
||||
advance();
|
||||
auto val = parse_value();
|
||||
if (!val) {
|
||||
return std::unexpected{val.error()};
|
||||
}
|
||||
if (auto r = insert_key_value(tbl.as_table_mut(), *keys, std::move(*val), kv_origin);
|
||||
!r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
skip_inline_ws();
|
||||
if (!eof() && peek() == ',') {
|
||||
advance();
|
||||
continue;
|
||||
}
|
||||
if (!eof() && peek() == '}') {
|
||||
advance();
|
||||
return tbl;
|
||||
}
|
||||
return std::unexpected{parse_err(origin(), "expected ',' or '}' in inline table")};
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable *, Void::Result::Error>
|
||||
navigate_table_header(TomlTable &root, const std::vector<std::string> &keys,
|
||||
bool is_array_table, const SourceOrigin &hdr_origin) {
|
||||
TomlTable *cur = &root;
|
||||
for (std::size_t i = 0; i + 1 < keys.size(); ++i) {
|
||||
const auto &k = keys[i];
|
||||
auto it = cur->find(k);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(k, TomlValue::make_table(hdr_origin)).first;
|
||||
}
|
||||
if (it->second.is_table()) {
|
||||
cur = &it->second.as_table_mut();
|
||||
} else if (it->second.is_array() && !it->second.as_array().empty() &&
|
||||
it->second.as_array().back().is_table()) {
|
||||
cur = &it->second.as_array_mut().back().as_table_mut();
|
||||
} else {
|
||||
return std::unexpected{
|
||||
parse_err(hdr_origin, std::format("key '{}' is not a table", k))};
|
||||
}
|
||||
}
|
||||
const auto &last = keys.back();
|
||||
if (is_array_table) {
|
||||
auto it = cur->find(last);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(last, TomlValue::make_array(hdr_origin)).first;
|
||||
}
|
||||
if (!it->second.is_array()) {
|
||||
return std::unexpected{parse_err(
|
||||
hdr_origin, std::format("key '{}' cannot be redefined as array table", last))};
|
||||
}
|
||||
it->second.as_array_mut().push_back(TomlValue::make_table(hdr_origin));
|
||||
return &it->second.as_array_mut().back().as_table_mut();
|
||||
}
|
||||
auto it = cur->find(last);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(last, TomlValue::make_table(hdr_origin)).first;
|
||||
}
|
||||
if (!it->second.is_table()) {
|
||||
return std::unexpected{
|
||||
parse_err(hdr_origin, std::format("key '{}' already defined as non-table", last))};
|
||||
}
|
||||
std::string full_header;
|
||||
for (std::size_t i = 0; i < keys.size(); ++i) {
|
||||
if (i > 0) {
|
||||
full_header += '.';
|
||||
}
|
||||
full_header += keys[i];
|
||||
}
|
||||
if (!explicit_tables_.insert(full_header).second) {
|
||||
return std::unexpected{
|
||||
parse_err(hdr_origin, std::format("duplicate table header [{}]", full_header))};
|
||||
}
|
||||
return &it->second.as_table_mut();
|
||||
}
|
||||
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
insert_key_value(TomlTable &table, const std::vector<std::string> &keys, TomlValue val,
|
||||
const SourceOrigin &kv_origin) {
|
||||
TomlTable *cur = &table;
|
||||
for (std::size_t i = 0; i + 1 < keys.size(); ++i) {
|
||||
const auto &k = keys[i];
|
||||
auto it = cur->find(k);
|
||||
if (it == cur->end()) {
|
||||
it = cur->emplace(k, TomlValue::make_table(kv_origin)).first;
|
||||
}
|
||||
if (!it->second.is_table()) {
|
||||
return std::unexpected{
|
||||
parse_err(kv_origin, std::format("cannot index into non-table key '{}'", k))};
|
||||
}
|
||||
cur = &it->second.as_table_mut();
|
||||
}
|
||||
const auto &leaf = keys.back();
|
||||
if (cur->contains(leaf)) {
|
||||
return std::unexpected{
|
||||
parse_err(kv_origin, std::format("duplicate TOML key '{}'", leaf))};
|
||||
}
|
||||
cur->emplace(leaf, std::move(val));
|
||||
return {};
|
||||
}
|
||||
|
||||
std::string_view text_;
|
||||
std::string source_name_;
|
||||
std::size_t pos_{0};
|
||||
std::size_t line_{1};
|
||||
std::size_t col_{1};
|
||||
std::set<std::string> explicit_tables_;
|
||||
};
|
||||
|
||||
std::string toml_quote_key(std::string_view key) {
|
||||
bool bare_ok = !key.empty();
|
||||
for (const char c : key) {
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
|
||||
c == '_' || c == '-')) {
|
||||
bare_ok = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bare_ok ? std::string{key} : Util::json_quote(key);
|
||||
return dst;
|
||||
}
|
||||
|
||||
std::string serialize_scalar_or_inline(const TomlValue &v) {
|
||||
if (v.is_string()) {
|
||||
return Util::json_quote(v.as_string());
|
||||
}
|
||||
if (v.is_bool()) {
|
||||
return v.as_bool() ? "true" : "false";
|
||||
}
|
||||
if (v.is_int()) {
|
||||
return std::to_string(v.as_int());
|
||||
}
|
||||
if (v.is_double()) {
|
||||
return std::format("{}", v.as_double());
|
||||
}
|
||||
if (v.is_array()) {
|
||||
std::string out{"["};
|
||||
const auto &arr = v.as_array();
|
||||
for (std::size_t i = 0; i < arr.size(); ++i) {
|
||||
if (i > 0) {
|
||||
out += ", ";
|
||||
}
|
||||
out += serialize_scalar_or_inline(arr[i]);
|
||||
TomlValue convert_from_void_toml(const Void::Data::Toml::Value &src,
|
||||
const std::string &source_name) {
|
||||
const SourceOrigin orig{source_name, src.origin.line, src.origin.column};
|
||||
switch (src.kind()) {
|
||||
case Void::Data::Toml::ValueKind::String:
|
||||
return TomlValue{*src.as_string(), orig};
|
||||
case Void::Data::Toml::ValueKind::Boolean:
|
||||
return TomlValue{*src.as_bool(), orig};
|
||||
case Void::Data::Toml::ValueKind::Integer:
|
||||
return TomlValue{*src.as_integer(), orig};
|
||||
case Void::Data::Toml::ValueKind::Float:
|
||||
return TomlValue{*src.as_float(), orig};
|
||||
case Void::Data::Toml::ValueKind::DateTime:
|
||||
return TomlValue{src.as_datetime()->to_string(), orig};
|
||||
case Void::Data::Toml::ValueKind::Array: {
|
||||
TomlValue out = TomlValue::make_array(orig);
|
||||
for (const auto &el : src.as_array()->elements) {
|
||||
out.as_array_mut().push_back(convert_from_void_toml(el, source_name));
|
||||
}
|
||||
out += "]";
|
||||
return out;
|
||||
}
|
||||
std::string out{"{"};
|
||||
bool first = true;
|
||||
for (const auto &[k, val] : v.as_table()) {
|
||||
if (!first) {
|
||||
out += ", ";
|
||||
}
|
||||
first = false;
|
||||
out += toml_quote_key(k) + " = " + serialize_scalar_or_inline(val);
|
||||
case Void::Data::Toml::ValueKind::Table: {
|
||||
TomlValue out = TomlValue::make_table(orig);
|
||||
out.as_table_mut() = convert_table_from_void_toml(*src.as_table(), source_name);
|
||||
return out;
|
||||
}
|
||||
out += "}";
|
||||
return out;
|
||||
case Void::Data::Toml::ValueKind::ArrayOfTables: {
|
||||
TomlValue out = TomlValue::make_array(orig);
|
||||
for (const auto &tbl : src.as_array_of_tables()->tables) {
|
||||
TomlValue item = TomlValue::make_table(orig);
|
||||
item.as_table_mut() = convert_table_from_void_toml(tbl, source_name);
|
||||
out.as_array_mut().push_back(std::move(item));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
return TomlValue{std::string{}, orig};
|
||||
}
|
||||
|
||||
void serialize_table_section(const TomlTable &tbl, const std::string &prefix, std::string &out) {
|
||||
// Scalars & arrays first.
|
||||
for (const auto &[k, v] : tbl) {
|
||||
if (!v.is_table()) {
|
||||
out += toml_quote_key(k) + " = " + serialize_scalar_or_inline(v) + "\n";
|
||||
}
|
||||
}
|
||||
// Nested subtables next.
|
||||
for (const auto &[k, v] : tbl) {
|
||||
if (v.is_table()) {
|
||||
const std::string header =
|
||||
prefix.empty() ? toml_quote_key(k) : (prefix + "." + toml_quote_key(k));
|
||||
out += "\n[" + header + "]\n";
|
||||
serialize_table_section(v.as_table(), header, out);
|
||||
}
|
||||
Void::Data::Toml::Value convert_to_void_toml(const TomlValue &src);
|
||||
|
||||
Void::Data::Toml::Table convert_table_to_void_toml(const TomlTable &src) {
|
||||
Void::Data::Toml::Table dst;
|
||||
for (const auto &[k, v] : src) {
|
||||
dst.entries.emplace(k, convert_to_void_toml(v));
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
bool option_value_equal(const OptionValue &a, const OptionValue &b) { return a.value == b.value; }
|
||||
Void::Data::Toml::Value convert_to_void_toml(const TomlValue &src) {
|
||||
if (src.is_string()) {
|
||||
return Void::Data::Toml::Value{src.as_string()};
|
||||
}
|
||||
if (src.is_bool()) {
|
||||
return Void::Data::Toml::Value{src.as_bool()};
|
||||
}
|
||||
if (src.is_int()) {
|
||||
return Void::Data::Toml::Value{src.as_int()};
|
||||
}
|
||||
if (src.is_double()) {
|
||||
return Void::Data::Toml::Value{src.as_double()};
|
||||
}
|
||||
if (src.is_array()) {
|
||||
Void::Data::Toml::Array arr;
|
||||
for (const auto &el : src.as_array()) {
|
||||
arr.elements.push_back(convert_to_void_toml(el));
|
||||
}
|
||||
return Void::Data::Toml::Value{std::move(arr)};
|
||||
}
|
||||
return Void::Data::Toml::Value{convert_table_to_void_toml(src.as_table())};
|
||||
}
|
||||
|
||||
bool dependency_records_equal(const DependencyRecord &a, const DependencyRecord &b) {
|
||||
if (a.source_kind != b.source_kind || a.location != b.location || a.revision != b.revision ||
|
||||
a.package != b.package || a.version != b.version || a.builder != b.builder ||
|
||||
a.registry_authority != b.registry_authority || a.sha256 != b.sha256 ||
|
||||
a.features != b.features || a.build_dependency != b.build_dependency ||
|
||||
a.options.size() != b.options.size()) {
|
||||
if (a.name != b.name || a.source_kind != b.source_kind || a.location != b.location ||
|
||||
a.revision != b.revision || a.sha256 != b.sha256 || a.package != b.package ||
|
||||
a.version != b.version || a.registry_authority != b.registry_authority ||
|
||||
a.builder != b.builder || a.features != b.features ||
|
||||
a.build_dependency != b.build_dependency || a.options.size() != b.options.size()) {
|
||||
return false;
|
||||
}
|
||||
for (const auto &[k, av] : a.options) {
|
||||
const auto bit = b.options.find(k);
|
||||
if (bit == b.options.end() || !option_value_equal(av, bit->second)) {
|
||||
const auto it = b.options.find(k);
|
||||
if (it == b.options.end() || av.value != it->second.value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -675,23 +122,29 @@ bool dependency_records_equal(const DependencyRecord &a, const DependencyRecord
|
||||
|
||||
std::string SourceOrigin::format() const {
|
||||
if (line == 0) {
|
||||
return file.empty() ? "<unknown>" : file;
|
||||
return file;
|
||||
}
|
||||
return std::format("{}:{}:{}", file, line, column);
|
||||
}
|
||||
|
||||
TomlValue TomlValue::make_table(SourceOrigin origin) {
|
||||
return TomlValue{std::make_shared<TomlTable>(), std::move(origin)};
|
||||
TomlValue TomlValue::make_table(SourceOrigin orig) {
|
||||
return TomlValue{std::make_shared<TomlTable>(), std::move(orig)};
|
||||
}
|
||||
|
||||
TomlValue TomlValue::make_array(SourceOrigin origin) {
|
||||
return TomlValue{std::make_shared<TomlArray>(), std::move(origin)};
|
||||
TomlValue TomlValue::make_array(SourceOrigin orig) {
|
||||
return TomlValue{std::make_shared<TomlArray>(), std::move(orig)};
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error> parse_toml(std::string_view text,
|
||||
std::string_view source_name) {
|
||||
TomlParser parser{text, std::string{source_name}};
|
||||
return parser.parse();
|
||||
// Delegate full TOML 1.0 parsing directly to Void::Data::Toml (VL-002).
|
||||
auto parsed = Void::Data::Toml::parse(text);
|
||||
if (!parsed) {
|
||||
const SourceOrigin err_orig{std::string{source_name}, parsed.error().origin.line,
|
||||
parsed.error().origin.column};
|
||||
return std::unexpected{parse_err(err_orig, parsed.error().message)};
|
||||
}
|
||||
return convert_table_from_void_toml(parsed->root, std::string{source_name});
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error>
|
||||
@@ -704,9 +157,7 @@ parse_toml_file(const std::filesystem::path &path) {
|
||||
}
|
||||
|
||||
std::string serialize_toml(const TomlTable &table) {
|
||||
std::string out;
|
||||
serialize_table_section(table, "", out);
|
||||
return out;
|
||||
return Void::Data::Toml::serialize_table(convert_table_to_void_toml(table));
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<NormalizedDependency>, Void::Result::Error>
|
||||
@@ -717,7 +168,6 @@ load_dependencies_toml(const std::filesystem::path &toml_path) {
|
||||
}
|
||||
const auto &root = *root_res;
|
||||
|
||||
// Validate top-level keys (reject unknown fields explicitly!).
|
||||
for (const auto &[k, v] : root) {
|
||||
if (k != "dependencies" && k != "build_dependencies" && k != "registries") {
|
||||
return std::unexpected{parse_err(
|
||||
|
||||
Reference in new issue
Block a user