irc: add flake reference support
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Icc96d297f02d3aad03b0373727d57f316a6a6964
This commit is contained in:
parent
66c0d5bb99
commit
760094a2b7
4 changed files with 235 additions and 9 deletions
165
src/irc/main.cpp
165
src/irc/main.cpp
|
|
@ -1,22 +1,165 @@
|
|||
#include "ir_gen.h"
|
||||
#include "parser.h"
|
||||
#include "resolver.h"
|
||||
#include "serializer.h"
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace nix_irc {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
void print_usage(const char* prog) {
|
||||
std::cout << "Usage: " << prog << " [options] <input.nix> [output.nixir]\n"
|
||||
std::cout << "Usage: " << prog << " [options] <input.nix|flake#attr> [output.nixir]\n"
|
||||
<< "\nOptions:\n"
|
||||
<< " -I <path> Add search path for imports\n"
|
||||
<< " --no-imports Disable import resolution\n"
|
||||
<< " --help Show this help\n";
|
||||
}
|
||||
|
||||
static bool is_flake_reference(const std::string& input) {
|
||||
return input.find('#') != std::string::npos;
|
||||
}
|
||||
|
||||
static std::string sanitize_output_stem(const std::string& input) {
|
||||
std::string stem;
|
||||
stem.reserve(input.size());
|
||||
|
||||
for (char ch : input) {
|
||||
if (std::isalnum(static_cast<unsigned char>(ch))) {
|
||||
stem.push_back(ch);
|
||||
} else if (stem.empty() || stem.back() != '-') {
|
||||
stem.push_back('-');
|
||||
}
|
||||
}
|
||||
|
||||
while (!stem.empty() && stem.back() == '-') {
|
||||
stem.pop_back();
|
||||
}
|
||||
|
||||
return stem.empty() ? "bundle" : stem;
|
||||
}
|
||||
|
||||
static std::string default_output_path_for(const std::string& input) {
|
||||
if (!is_flake_reference(input)) {
|
||||
return input + "ir";
|
||||
}
|
||||
|
||||
return sanitize_output_stem(input) + ".nixir";
|
||||
}
|
||||
|
||||
static std::string normalize_local_flake_path(const std::string& raw_path) {
|
||||
fs::path path = raw_path.empty() ? fs::current_path() : fs::path(raw_path);
|
||||
fs::path absolute = path.is_absolute() ? path : fs::absolute(path);
|
||||
fs::path normalized = absolute.lexically_normal();
|
||||
|
||||
if (!fs::exists(normalized)) {
|
||||
throw std::runtime_error("Flake path does not exist: " + normalized.string());
|
||||
}
|
||||
|
||||
if (fs::is_directory(normalized) && !fs::exists(normalized / "flake.nix")) {
|
||||
throw std::runtime_error("Flake directory does not contain flake.nix: " + normalized.string());
|
||||
}
|
||||
|
||||
return normalized.string();
|
||||
}
|
||||
|
||||
static std::string normalize_flake_ref_source(const std::string& ref) {
|
||||
if (ref.empty()) {
|
||||
return normalize_local_flake_path(".");
|
||||
}
|
||||
|
||||
if (ref.rfind("path:", 0) == 0) {
|
||||
return "path:" + normalize_local_flake_path(ref.substr(5));
|
||||
}
|
||||
|
||||
if (ref[0] == '.' || ref[0] == '/') {
|
||||
return normalize_local_flake_path(ref);
|
||||
}
|
||||
|
||||
if (fs::exists(ref)) {
|
||||
return normalize_local_flake_path(ref);
|
||||
}
|
||||
|
||||
return ref;
|
||||
}
|
||||
|
||||
static std::vector<std::string> parse_flake_attr_path(const std::string& raw_attr_path) {
|
||||
if (raw_attr_path.empty()) {
|
||||
throw std::runtime_error("Flake reference is missing an attribute path after '#'");
|
||||
}
|
||||
|
||||
std::vector<std::string> segments;
|
||||
std::string current;
|
||||
bool in_quotes = false;
|
||||
bool escaping = false;
|
||||
|
||||
for (char ch : raw_attr_path) {
|
||||
if (escaping) {
|
||||
current.push_back(ch);
|
||||
escaping = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (in_quotes) {
|
||||
if (ch == '\\') {
|
||||
escaping = true;
|
||||
} else if (ch == '"') {
|
||||
in_quotes = false;
|
||||
} else {
|
||||
current.push_back(ch);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"') {
|
||||
in_quotes = true;
|
||||
} else if (ch == '.') {
|
||||
if (current.empty()) {
|
||||
throw std::runtime_error("Flake attribute path contains an empty segment");
|
||||
}
|
||||
segments.push_back(current);
|
||||
current.clear();
|
||||
} else {
|
||||
current.push_back(ch);
|
||||
}
|
||||
}
|
||||
|
||||
if (escaping || in_quotes) {
|
||||
throw std::runtime_error("Unterminated quoted segment in flake attribute path");
|
||||
}
|
||||
|
||||
if (current.empty()) {
|
||||
throw std::runtime_error("Flake attribute path contains an empty segment");
|
||||
}
|
||||
|
||||
segments.push_back(current);
|
||||
return segments;
|
||||
}
|
||||
|
||||
static std::shared_ptr<Node> build_flake_ref_ast(const std::string& input) {
|
||||
size_t hash_pos = input.find('#');
|
||||
if (hash_pos == std::string::npos) {
|
||||
throw std::runtime_error("Not a flake reference: " + input);
|
||||
}
|
||||
|
||||
std::string flake_source = normalize_flake_ref_source(input.substr(0, hash_pos));
|
||||
auto attr_path = parse_flake_attr_path(input.substr(hash_pos + 1));
|
||||
|
||||
auto expr = std::make_shared<Node>(BuiltinCallNode(
|
||||
"getFlake",
|
||||
std::vector<std::shared_ptr<Node>>{std::make_shared<Node>(ConstStringNode(flake_source))}));
|
||||
|
||||
for (const auto& attr : attr_path) {
|
||||
expr = std::make_shared<Node>(SelectNode(expr, std::make_shared<Node>(ConstStringNode(attr))));
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
int run_compile(int argc, char** argv) {
|
||||
std::string input_file;
|
||||
std::string output_file;
|
||||
|
|
@ -57,20 +200,24 @@ int run_compile(int argc, char** argv) {
|
|||
}
|
||||
|
||||
if (output_file.empty()) {
|
||||
output_file = input_file + "ir";
|
||||
output_file = default_output_path_for(input_file);
|
||||
}
|
||||
|
||||
try {
|
||||
Parser parser;
|
||||
Resolver resolver;
|
||||
(void) search_paths;
|
||||
(void) resolve_imports;
|
||||
|
||||
for (const auto& path : search_paths) {
|
||||
resolver.add_search_path(path);
|
||||
std::shared_ptr<Node> ast;
|
||||
|
||||
if (is_flake_reference(input_file)) {
|
||||
std::cout << "Compiling flake reference: " << input_file << "\n";
|
||||
ast = build_flake_ref_ast(input_file);
|
||||
} else {
|
||||
std::cout << "Parsing: " << input_file << "\n";
|
||||
ast = parser.parse_file(input_file);
|
||||
}
|
||||
|
||||
std::cout << "Parsing: " << input_file << "\n";
|
||||
auto ast = parser.parse_file(input_file);
|
||||
|
||||
if (!ast) {
|
||||
std::cerr << "Error: Failed to parse input\n";
|
||||
return 1;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue