diff --git a/src/frontends/lean/dependencies.cpp b/src/frontends/lean/dependencies.cpp index 665a7e1b3..6fe1c931b 100644 --- a/src/frontends/lean/dependencies.cpp +++ b/src/frontends/lean/dependencies.cpp @@ -14,11 +14,15 @@ Author: Leonardo de Moura namespace lean { void display_deps(environment const & env, std::ostream & out, char const * fname) { name import("import"); + name period("."); std::ifstream in(fname); if (in.bad() || in.fail()) throw exception(sstream() << "failed to open file '" << fname << "'"); scanner s(in, fname); - bool import_args = false; + optional k; + std::string base = dirname(fname); + bool import_prefix = false; + bool import_args = false; while (true) { scanner::token_kind t = scanner::token_kind::Identifier; try { @@ -29,18 +33,28 @@ void display_deps(environment const & env, std::ostream & out, char const * fnam if (t == scanner::token_kind::Eof) { return; } else if (t == scanner::token_kind::CommandKeyword && s.get_token_info().value() == import) { + k = optional(); + import_prefix = true; + } else if (import_prefix && t == scanner::token_kind::Keyword && s.get_token_info().value() == period) { + if (!k) + k = 0; + else + k = *k + 1; + } else if ((import_prefix || import_args) && t == scanner::token_kind::Identifier) { import_args = true; - } else if (import_args == true && t == scanner::token_kind::Identifier) { - std::string m_name = find_file(name_to_file(s.get_name_val()), {".lean", ".olean", ".lua"}); + std::string m_name = find_file(base, k, name_to_file(s.get_name_val()), {".lean", ".olean", ".lua"}); int last_idx = m_name.find_last_of("."); std::string rawname = m_name.substr(0, last_idx); std::string ext = m_name.substr(last_idx); if (ext == ".lean") m_name = rawname + ".olean"; display_path(out, m_name); + k = optional(); + import_prefix = true; out << "\n"; } else { - import_args = false; + import_args = false; + import_prefix = false; } } } diff --git a/src/util/lean_path.cpp b/src/util/lean_path.cpp index fd392b7fe..f202555cd 100644 --- a/src/util/lean_path.cpp +++ b/src/util/lean_path.cpp @@ -245,21 +245,28 @@ std::string find_file(std::string fname, std::initializer_list con throw exception(sstream() << "file '" << fname << "' not found in the LEAN_PATH"); } -std::string find_file(std::string const & base, optional const & rel, name const & fname, char const * ext) { +std::string find_file(std::string const & base, optional const & rel, name const & fname, + std::initializer_list const & extensions) { if (!rel) { - return find_file(fname.to_string(g_sep_str.c_str()), {ext}); + return find_file(fname.to_string(g_sep_str.c_str()), extensions); } else { auto path = base; for (unsigned i = 0; i < *rel; i++) { path += g_sep; path += ".."; } - if (auto r = check_file(path, fname.to_string(g_sep_str.c_str()), ext)) - return *r; + for (auto ext : extensions) { + if (auto r = check_file(path, fname.to_string(g_sep_str.c_str()), ext)) + return *r; + } throw exception(sstream() << "file '" << fname << "' not found at '" << path << "'"); } } +std::string find_file(std::string const & base, optional const & k, name const & fname, char const * ext) { + return find_file(base, k, fname, {ext}); +} + std::string find_file(std::string fname) { return find_file(fname, {".olean", ".lean", ".lua"}); } diff --git a/src/util/lean_path.h b/src/util/lean_path.h index b15320034..358f28450 100644 --- a/src/util/lean_path.h +++ b/src/util/lean_path.h @@ -24,6 +24,8 @@ std::string find_file(name const & fname); std::string find_file(name const & fname, std::initializer_list const & exts); /** \brief \brief Similar to previous find_file, but if k is not none then search at the k-th parent of base. */ +std::string find_file(std::string const & base, optional const & rel, name const & fname, + std::initializer_list const & extensions); std::string find_file(std::string const & base, optional const & k, name const & fname, char const * ext); /** \brief Return true iff fname ends with ".lean" */