#include "rapidproto/resolver.hpp"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <functional>
#include <ios>
#include <optional>
#include <sstream>
#include <string>
#include <system_error>
#include <unordered_map>
#include <utility>
#include <vector>
#include "rapidproto/ast.hpp"
#include "rapidproto/lexer.hpp"
#include "rapidproto/parser.hpp"
#include "rapidproto/range.hpp"
#include "rapidproto/result.hpp"
#include "rapidproto/source.hpp"
#include "rapidproto/source_id.hpp"
#include "rapidproto/wellknown.hpp"
namespace rapidproto {
namespace {
constexpr std::size_t kMaxImportDepth = 100;
std::optional<std::string> read_file_contents(const std::filesystem::path& path) {
const std::ifstream file(path, std::ios::binary);
if (!file) {
return std::nullopt;
}
std::ostringstream contents;
contents << file.rdbuf();
return contents.str();
}
Result<FileNode> parse_source(const std::string& filename, std::string source, SourceId id) {
auto lexed_result = lex(std::move(source));
if (!lexed_result) {
Error error = std::move(lexed_result).error();
error.source = id; return error;
}
const LexResult lexed = std::move(lexed_result).value();
auto parsed = parse_file(Range<Token>(lexed.tokens));
if (!parsed) {
Error error = std::move(parsed).error();
const std::size_t token_index = error.byte_offset;
std::size_t byte_offset = lexed.source ? lexed.source->size() : 0;
if (token_index < lexed.tokens.size()) {
byte_offset = lexed.tokens[token_index].byte_offset;
}
error.byte_offset = byte_offset;
error.source = id;
return error;
}
if (!parsed.value().remaining.empty()) {
const std::size_t consumed = lexed.tokens.size() - parsed.value().remaining.size();
const std::size_t byte_offset =
consumed < lexed.tokens.size() ? lexed.tokens[consumed].byte_offset : 0;
return Error{id, byte_offset, "unexpected trailing input"};
}
FileNode file = std::move(parsed.value().value);
file.filename = filename;
return file;
}
enum class Color : std::uint8_t { Gray, Black };
struct SourceAccess {
std::function<std::optional<std::pair<std::string, std::string>>(const std::string&)>
fetch_entry;
std::function<std::optional<std::string>(const std::string&)> fetch_import;
std::string import_not_found_hint;
};
std::optional<std::string> wellknown_fallback(const std::string& import_path, bool use_wellknown) {
if (use_wellknown) {
if (auto embedded = wellknown_source(import_path)) {
return std::string(*embedded);
}
}
return std::nullopt;
}
SourceAccess make_provider_access(const ResolverConfig::SourceProvider& provider,
bool use_wellknown) {
return SourceAccess{
[provider](const std::string& entry) -> std::optional<std::pair<std::string, std::string>> {
if (auto text = provider(entry)) {
return std::make_pair(entry, std::move(*text));
}
return std::nullopt;
},
[provider, use_wellknown](const std::string& import_path) {
if (auto provided = provider(import_path)) {
return provided;
}
return wellknown_fallback(import_path, use_wellknown);
},
"; it was not in the source set provided",
};
}
SourceAccess make_filesystem_access(const std::vector<std::string>& include_paths,
bool use_wellknown) {
std::string hint = include_paths.empty()
? "; no include paths were given -- add -I <dir>"
: "; searched " + std::to_string(include_paths.size()) +
" include path(s) -- add the right -I <dir>";
return SourceAccess{
[include_paths](
const std::string& entry) -> std::optional<std::pair<std::string, std::string>> {
if (auto text = read_file_contents(entry)) {
return std::make_pair(canonical_entry_name(entry, include_paths), std::move(*text));
}
return std::nullopt;
},
[include_paths,
use_wellknown](const std::string& import_path) -> std::optional<std::string> {
for (const auto& include : include_paths) {
if (include.empty()) {
continue; }
const std::filesystem::path full = std::filesystem::path(include) / import_path;
if (std::filesystem::exists(full)) {
return read_file_contents(full);
}
}
return wellknown_fallback(import_path,
use_wellknown); },
std::move(hint),
};
}
SourceAccess make_source_access(const ResolverConfig& config) {
if (config.source_provider) {
return make_provider_access(config.source_provider, config.use_wellknown);
}
return make_filesystem_access(config.include_paths, config.use_wellknown);
}
class Resolver {
public:
Resolver(const ResolverConfig& config, SourceRegistry& sources)
: m_access(make_source_access(config)), m_sources(sources) {}
Result<ResolvedFileSet> run(const std::vector<std::string>& entry_files) {
for (const std::string& entry_file : entry_files) {
auto resolved = m_access.fetch_entry(entry_file);
if (!resolved) {
return Error{0, "entry file not found: " + entry_file};
}
auto& [entry_name, text] = *resolved;
if (m_parsed.count(entry_name) != 0) {
continue;
}
if (auto error = visit(entry_name, std::move(text))) {
return *error;
}
}
ResolvedFileSet set;
for (auto& name : m_order) {
set.file_index.emplace(name, set.files.size());
set.files.push_back(std::move(m_parsed[name]));
}
return set;
}
private:
std::string render_cycle(const std::string& import_path) const {
std::string message = "import cycle: ";
const auto start = std::find(m_stack.begin(), m_stack.end(), import_path);
for (auto it = start; it != m_stack.end(); ++it) {
message += *it;
message += " -> ";
}
message += import_path;
return message;
}
std::optional<Error> visit(const std::string& name, std::string source) {
if (m_stack.size() >= kMaxImportDepth) { std::string message = "import chain exceeds maximum depth (";
message += std::to_string(kMaxImportDepth);
message += ")";
return Error{0, std::move(message)};
}
m_color[name] = Color::Gray;
m_stack.push_back(name);
const SourceId id = m_sources.add(name, source);
auto file_result = parse_source(name, std::move(source), id);
if (!file_result) {
return std::move(file_result).error();
}
FileNode file = std::move(file_result).value();
file.source = id;
std::vector<std::string> imports;
imports.reserve(file.imports.size());
for (const auto& import : file.imports) {
imports.push_back(import.path);
}
m_parsed.emplace(name, std::move(file));
for (const auto& raw_import : imports) {
const std::string import_path = canonical_import_path(raw_import);
if (const auto found = m_color.find(import_path); found != m_color.end()) {
if (found->second == Color::Gray) {
return Error{0, render_cycle(import_path)};
}
continue; }
auto import_source = m_access.fetch_import(import_path);
if (!import_source) {
std::string message = "import not found: ";
message += import_path;
message += " (imported by ";
message += name;
message += ")";
message += m_access.import_not_found_hint;
return Error{0, std::move(message)};
}
if (auto error = visit(import_path, std::move(*import_source))) {
return error;
}
}
m_color[name] = Color::Black;
m_stack.pop_back();
m_order.push_back(name); return std::nullopt;
}
SourceAccess m_access; SourceRegistry& m_sources; std::unordered_map<std::string, Color> m_color;
std::unordered_map<std::string, FileNode> m_parsed;
std::vector<std::string> m_stack; std::vector<std::string> m_order;
};
}
std::string canonical_import_path(const std::string& import_path) {
return std::filesystem::path(import_path).lexically_normal().generic_string();
}
std::string canonical_entry_name(const std::string& entry,
const std::vector<std::string>& include_paths) {
std::error_code ec;
const std::filesystem::path abs_entry = std::filesystem::weakly_canonical(entry, ec);
if (ec) {
return entry;
}
for (const auto& include : include_paths) {
const std::filesystem::path abs_include = std::filesystem::weakly_canonical(include, ec);
if (ec) {
continue;
}
const std::filesystem::path relative =
std::filesystem::relative(abs_entry, abs_include, ec);
if (ec || relative.empty()) {
continue;
}
const std::string name = relative.generic_string();
if (name.rfind("..", 0) != 0) { return name;
}
}
return entry;
}
Result<ResolvedFileSet> resolve(const std::string& entry_file, const ResolverConfig& config,
SourceRegistry& sources) {
return Resolver(config, sources).run({entry_file});
}
Result<ResolvedFileSet> resolve(const std::string& entry_file, const ResolverConfig& config) {
SourceRegistry discard; return Resolver(config, discard).run({entry_file});
}
Result<ResolvedFileSet> resolve(const std::vector<std::string>& entry_files,
const ResolverConfig& config, SourceRegistry& sources) {
return Resolver(config, sources).run(entry_files);
}
}