Implement VoidBuildSystem v1 (Plans 01-19) with VoidLibrary integration
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
This commit is contained in:
1 parent
3cac890ce9
commit
ab64147f9d
114 files changed
+23777
-1392
No files matched your search
@@ -0,0 +1,949 @@
|
||||
#include <Void/Build/Package/Toml.hpp>
|
||||
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
#include <set>
|
||||
|
||||
namespace Void::Build::Package {
|
||||
namespace {
|
||||
|
||||
Void::Result::Error parse_err(const SourceOrigin &orig, std::string msg) {
|
||||
return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
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)} {}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
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);
|
||||
}
|
||||
out += "}";
|
||||
return out;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool option_value_equal(const OptionValue &a, const OptionValue &b) { return a.value == b.value; }
|
||||
|
||||
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()) {
|
||||
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)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string SourceOrigin::format() const {
|
||||
if (line == 0) {
|
||||
return file.empty() ? "<unknown>" : 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_array(SourceOrigin origin) {
|
||||
return TomlValue{std::make_shared<TomlArray>(), std::move(origin)};
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Void::Result::Result<TomlTable, Void::Result::Error>
|
||||
parse_toml_file(const std::filesystem::path &path) {
|
||||
auto content = Void::FileSystem::read_file(path);
|
||||
if (!content) {
|
||||
return std::unexpected{content.error()};
|
||||
}
|
||||
return parse_toml(*content, path.string());
|
||||
}
|
||||
|
||||
std::string serialize_toml(const TomlTable &table) {
|
||||
std::string out;
|
||||
serialize_table_section(table, "", out);
|
||||
return out;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<NormalizedDependency>, Void::Result::Error>
|
||||
load_dependencies_toml(const std::filesystem::path &toml_path) {
|
||||
auto root_res = parse_toml_file(toml_path);
|
||||
if (!root_res) {
|
||||
return std::unexpected{root_res.error()};
|
||||
}
|
||||
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(
|
||||
v.origin, std::format("unknown top-level key '{}' in dependency manifest", k))};
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<NormalizedDependency> out;
|
||||
auto parse_dep_section =
|
||||
[&](const std::string §ion_key,
|
||||
bool is_build_dep) -> Void::Result::Result<void, Void::Result::Error> {
|
||||
const auto sec_it = root.find(section_key);
|
||||
if (sec_it == root.end()) {
|
||||
return {};
|
||||
}
|
||||
if (!sec_it->second.is_table()) {
|
||||
return std::unexpected{
|
||||
parse_err(sec_it->second.origin, std::format("'{}' must be a table", section_key))};
|
||||
}
|
||||
for (const auto &[alias, dep_val] : sec_it->second.as_table()) {
|
||||
if (!dep_val.is_table()) {
|
||||
return std::unexpected{parse_err(
|
||||
dep_val.origin, std::format("dependency '{}' must be a table", alias))};
|
||||
}
|
||||
const auto &dtbl = dep_val.as_table();
|
||||
DependencyRecord rec;
|
||||
rec.name = alias;
|
||||
rec.build_dependency = is_build_dep;
|
||||
|
||||
static const std::set<std::string_view> kAllowedFields{
|
||||
"git", "rev", "path", "archive", "sha256", "package", "version",
|
||||
"conan", "builder", "options", "registry", "features", "build_dependency"};
|
||||
for (const auto &[fk, fv] : dtbl) {
|
||||
if (!kAllowedFields.contains(fk)) {
|
||||
return std::unexpected{
|
||||
parse_err(fv.origin,
|
||||
std::format("unknown field '{}' in dependency '{}'", fk, alias))};
|
||||
}
|
||||
}
|
||||
|
||||
int source_count = 0;
|
||||
if (const auto it = dtbl.find("git"); it != dtbl.end()) {
|
||||
++source_count;
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{parse_err(it->second.origin, "'git' must be a string")};
|
||||
}
|
||||
const auto rev_it = dtbl.find("rev");
|
||||
if (rev_it == dtbl.end() || !rev_it->second.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(dep_val.origin,
|
||||
std::format("git dependency '{}' requires 40-hex 'rev'", alias))};
|
||||
}
|
||||
const auto &rev = rev_it->second.as_string();
|
||||
if (rev.size() != 40 || !std::ranges::all_of(rev, [](char c) {
|
||||
return std::isxdigit(static_cast<unsigned char>(c)) != 0;
|
||||
})) {
|
||||
return std::unexpected{parse_err(
|
||||
rev_it->second.origin, "git 'rev' must be an exact 40-hex commit SHA")};
|
||||
}
|
||||
rec.source_kind = DependencySourceKind::Git;
|
||||
rec.location = it->second.as_string();
|
||||
rec.revision = rev;
|
||||
}
|
||||
if (const auto it = dtbl.find("package"); it != dtbl.end()) {
|
||||
++source_count;
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'package' must be a string")};
|
||||
}
|
||||
const auto ver_it = dtbl.find("version");
|
||||
if (ver_it == dtbl.end() || !ver_it->second.is_string()) {
|
||||
return std::unexpected{parse_err(
|
||||
dep_val.origin,
|
||||
std::format("package dependency '{}' requires 'version'", alias))};
|
||||
}
|
||||
rec.source_kind = DependencySourceKind::Package;
|
||||
rec.package = it->second.as_string();
|
||||
rec.version = ver_it->second.as_string();
|
||||
}
|
||||
if (const auto it = dtbl.find("path"); it != dtbl.end()) {
|
||||
++source_count;
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{parse_err(it->second.origin, "'path' must be a string")};
|
||||
}
|
||||
rec.source_kind = DependencySourceKind::Path;
|
||||
rec.location = it->second.as_string();
|
||||
}
|
||||
if (const auto it = dtbl.find("archive"); it != dtbl.end()) {
|
||||
++source_count;
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'archive' must be a string")};
|
||||
}
|
||||
const auto sha_it = dtbl.find("sha256");
|
||||
if (sha_it == dtbl.end() || !sha_it->second.is_string() ||
|
||||
sha_it->second.as_string().size() != 64) {
|
||||
return std::unexpected{parse_err(
|
||||
dep_val.origin,
|
||||
std::format("archive dependency '{}' requires 64-hex 'sha256'", alias))};
|
||||
}
|
||||
rec.source_kind = DependencySourceKind::Archive;
|
||||
rec.location = it->second.as_string();
|
||||
rec.sha256 = sha_it->second.as_string();
|
||||
}
|
||||
if (const auto it = dtbl.find("conan"); it != dtbl.end()) {
|
||||
++source_count;
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'conan' must be a string")};
|
||||
}
|
||||
rec.source_kind = DependencySourceKind::Conan;
|
||||
rec.package = it->second.as_string();
|
||||
rec.builder = "conan";
|
||||
}
|
||||
|
||||
if (source_count != 1) {
|
||||
return std::unexpected{parse_err(
|
||||
dep_val.origin, std::format("dependency '{}' must specify exactly one source "
|
||||
"(git/package/path/archive/conan)",
|
||||
alias))};
|
||||
}
|
||||
|
||||
if (const auto it = dtbl.find("builder"); it != dtbl.end()) {
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'builder' must be a string")};
|
||||
}
|
||||
const auto &b = it->second.as_string();
|
||||
if (b != "cmake" && b != "meson" && b != "conan" && b != "native") {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, std::format("unsupported builder '{}'", b))};
|
||||
}
|
||||
if (rec.source_kind == DependencySourceKind::Package && b != "native") {
|
||||
return std::unexpected{parse_err(
|
||||
it->second.origin, "contradictory builder on native package dependency")};
|
||||
}
|
||||
rec.builder = b;
|
||||
}
|
||||
if (const auto it = dtbl.find("registry"); it != dtbl.end()) {
|
||||
if (!it->second.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'registry' must be a string")};
|
||||
}
|
||||
rec.registry_authority = it->second.as_string();
|
||||
}
|
||||
if (const auto it = dtbl.find("build_dependency"); it != dtbl.end()) {
|
||||
if (!it->second.is_bool()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'build_dependency' must be a bool")};
|
||||
}
|
||||
rec.build_dependency = it->second.as_bool();
|
||||
}
|
||||
if (const auto it = dtbl.find("features"); it != dtbl.end()) {
|
||||
if (!it->second.is_array()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'features' must be an array of strings")};
|
||||
}
|
||||
for (const auto &fv : it->second.as_array()) {
|
||||
if (!fv.is_string()) {
|
||||
return std::unexpected{
|
||||
parse_err(fv.origin, "feature entry must be a string")};
|
||||
}
|
||||
rec.features.push_back(fv.as_string());
|
||||
}
|
||||
}
|
||||
if (const auto it = dtbl.find("options"); it != dtbl.end()) {
|
||||
if (!it->second.is_table()) {
|
||||
return std::unexpected{
|
||||
parse_err(it->second.origin, "'options' must be a table")};
|
||||
}
|
||||
for (const auto &[ok, ov] : it->second.as_table()) {
|
||||
if (ov.is_string()) {
|
||||
rec.options.emplace(ok, OptionValue{ov.as_string()});
|
||||
} else if (ov.is_bool()) {
|
||||
rec.options.emplace(ok, OptionValue{ov.as_bool()});
|
||||
} else if (ov.is_int()) {
|
||||
rec.options.emplace(ok, OptionValue{ov.as_int()});
|
||||
} else {
|
||||
return std::unexpected{parse_err(
|
||||
ov.origin,
|
||||
std::format("option '{}' must be string, bool, or integer", ok))};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out.push_back(NormalizedDependency{std::move(rec), dep_val.origin});
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
if (auto r = parse_dep_section("dependencies", false); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
if (auto r = parse_dep_section("build_dependencies", true); !r) {
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Void::Result::Result<std::vector<NormalizedDependency>, Void::Result::Error>
|
||||
merge_dependencies(const std::vector<DependencyRecord> &cpp_deps,
|
||||
const std::vector<NormalizedDependency> &toml_deps) {
|
||||
std::map<std::string, NormalizedDependency, std::less<>> merged;
|
||||
for (const auto &d : cpp_deps) {
|
||||
merged.emplace(d.name, NormalizedDependency{d, SourceOrigin{"build.cpp", 0, 0}});
|
||||
}
|
||||
for (const auto &td : toml_deps) {
|
||||
const auto it = merged.find(td.record.name);
|
||||
if (it == merged.end()) {
|
||||
merged.emplace(td.record.name, td);
|
||||
continue;
|
||||
}
|
||||
if (!dependency_records_equal(it->second.record, td.record)) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::invalid_argument,
|
||||
std::format("conflicting declaration for dependency '{}': first declared at {}, "
|
||||
"conflicting declaration at {}",
|
||||
td.record.name, it->second.origin.format(), td.origin.format()))};
|
||||
}
|
||||
}
|
||||
std::vector<NormalizedDependency> out;
|
||||
out.reserve(merged.size());
|
||||
for (auto &[k, v] : merged) {
|
||||
out.push_back(std::move(v));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Package
|
||||
Reference in new issue
Block a user