#include #include #include #include #include "klammerset_registry.h" #include "error.h" #include "file.h" #include "log.h" #include "util.h" #include "show.h" #include "katom.h" Klammerset_registry::Klammerset_registry() : m_parameters(Parameter_set("symbol | desc :name :author :date :requires :files")) { } void Klammerset_registry::add(Klammerset klammerset) { (void)K::log(3, klammerset.m_symbol); m_klammersets[klammerset.m_symbol] = klammerset; m_symbols.push_back(klammerset.m_symbol); } std::optional Klammerset_registry::add( std::vector::iterator begin, std::vector::iterator end, std::vector& katoms) { (void)K::log(3, *begin, *(end - 1)); auto [positional, optional, rest] = argument_split(begin + 1, end - 1, m_parameters.m_positional.size()); auto values = m_parameters.value_map(positional, optional, rest, begin->m_loc); modify_type(katom_t::replaced, begin, end); auto next_iter = end; ignore_whitespace(next_iter, katoms); std::string symbol = values["symbol"]; check_symbol(symbol, begin->m_loc); if (has(symbol)) { (void)K::log(2, "Klammerset \"" + symbol + "\" is already loaded; declaration skipped"); return std::nullopt; } // The filename lists follow the standard conventions (spaces allowed, // standalone "/" separator). The existence rescue tests names against // the declaring file's directory, where relative names are later // resolved by Machine::load_klammerset_files. fs::path declaring(begin->m_loc.m_filename); std::string base_dir = fs::exists(declaring) ? declaring.parent_path().string() : ""; Klammerset klammerset(symbol, values["desc"], begin->m_loc); klammerset.m_name = values["name"]; klammerset.m_author = values["author"]; klammerset.m_date = values["date"]; klammerset.m_requires = resolve_filename_list(values["requires"], base_dir); klammerset.m_files = resolve_filename_list(values["files"], base_dir); add(klammerset); return klammerset; } void Klammerset_registry::check_symbol(const std::string& symbol, const Locator& loc) const { bool valid = !symbol.empty() && std::isalpha(static_cast(symbol[0])); for (char c : symbol) { if (!std::isalnum(static_cast(c)) && c != '_') { valid = false; } } if (!valid) { throw Klammerset_error( "The klammerset symbol \"" + symbol + "\" is not valid. A symbol begins " "with a letter and contains only letters, digits, and underscores.", loc); } } bool Klammerset_registry::has(const std::string& symbol) const { return m_klammersets.count(symbol) > 0; } Klammerset Klammerset_registry::get(const std::string& symbol, const Locator& loc) const { if (has(symbol)) { return m_klammersets.at(symbol); } else { throw Klammerset_error("Klammerset " + symbol + " does not exist", loc); } } // --- The klammerset search path --- bool is_klammerset_symbol(const std::string& name) { if (name.empty() || !std::isalpha(static_cast(name[0]))) { return false; } for (char c : name) { if (!std::isalnum(static_cast(c)) && c != '_') { return false; } } return true; } std::vector klammerset_search_dirs(const std::string& local_dir) { std::vector result {}; // A repeated directory (e.g. the local stage already IS // $KLAMMERTEXT_HOME) adds nothing and clutters error messages. auto push_unique = [&result](const std::string& dir) { std::string canonical = fs::weakly_canonical(dir).string(); if (!is_in(canonical, result)) { result.push_back(canonical); } }; if (!local_dir.empty()) { push_unique(local_dir); } std::string paths {}; const char* env = std::getenv("KLAMMERTEXT_KLAMMERSETS"); if (env && *env) { paths = env; } else if (const char* home = std::getenv("HOME"); home && *home) { paths = std::string(home) + "/.klammertext/klammersets"; } std::stringstream ss(paths); std::string dir; while (std::getline(ss, dir, ':')) { if (!dir.empty()) { push_unique(dir); } } if (const char* kthome = std::getenv(klammertext_home_var.c_str()); kthome && *kthome) { push_unique(kthome); } return result; } fs::path resolve_klammerset_symbol( const std::string& symbol, const std::string& local_dir, const Locator& loc) { std::vector dirs = klammerset_search_dirs(local_dir); for (const std::string& dir : dirs) { fs::path candidate = fs::path(dir) / symbol / (symbol + ".k"); if (fs::exists(candidate) && fs::is_regular_file(candidate)) { return fs::weakly_canonical(candidate); } } throw Klammerset_error( "The klammerset \"" + symbol + "\" was not found. A symbol x names the " "declaration file x/x.k in one of the search directories: " + join(dirs, ", ") + ". Enter \"kdesc --klammerset\" to list the available klammersets.", loc); } std::string describe_klammerset_search(const std::string& local_dir, int margin) { std::string tab(margin, ' '); std::stringstream ss {}; std::map first_hit {}; for (const std::string& dir : klammerset_search_dirs(local_dir)) { if (!fs::is_directory(dir)) { continue; } for (const auto& entry : fs::directory_iterator(dir)) { if (!entry.is_directory()) { continue; } std::string symbol = entry.path().filename().string(); fs::path declaration = entry.path() / (symbol + ".k"); if (!is_klammerset_symbol(symbol) || !fs::is_regular_file(declaration)) { continue; } ss << tab << symbol << sp_arrow << declaration.string(); if (first_hit.count(symbol)) { ss << " (shadowed by " << first_hit[symbol] << ")"; } else { first_hit[symbol] = declaration.string(); } ss << "\n"; } } if (first_hit.empty()) { ss << tab << "(no klammersets found)\n"; } return ss.str(); } std::string Klammerset_registry::describe(int margin, bool long_format) const { std::string tab(margin, ' '); std::stringstream ss {}; std::vector descs {}; for (const std::string& symbol : m_symbols) { descs.push_back(m_klammersets.at(symbol).m_desc); } auto symbol_width = max_length(m_symbols); auto desc_width = max_length(descs); for (const std::string& symbol : m_symbols) { const Klammerset& ks = m_klammersets.at(symbol); if (long_format) { ss << tab << std::setfill(' ') << std::setw(symbol_width) << std::left << symbol << " " << std::setw(desc_width) << std::left << ks.m_desc << " " << ks.m_loc.str() << "\n"; if (!ks.m_name.empty()) ss << tab << std::string(symbol_width, ' ') << " name: " << ks.m_name << "\n"; if (!ks.m_author.empty()) ss << tab << std::string(symbol_width, ' ') << " author: " << ks.m_author << "\n"; if (!ks.m_date.empty()) ss << tab << std::string(symbol_width, ' ') << " date: " << ks.m_date << "\n"; if (!ks.m_requires.empty()) ss << tab << std::string(symbol_width, ' ') << " requires: " << join(ks.m_requires, " / ") << "\n"; if (!ks.m_files.empty()) ss << tab << std::string(symbol_width, ' ') << " files: " << join(ks.m_files, " / ") << "\n"; } else { ss << tab << std::setfill(' ') << std::setw(symbol_width) << std::left << symbol << sp_arrow << ks << "\n"; } } return ss.str(); }