2014-06-29 00:43:30 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <utility>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include "util/sstream.h"
|
|
|
|
#include "util/lean_path.h"
|
|
|
|
#include "frontends/lean/scanner.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
void display_deps(environment const & env, std::ostream & out, char const * fname) {
|
2014-07-29 21:13:31 +00:00
|
|
|
name import("import");
|
2014-08-03 03:03:51 +00:00
|
|
|
name period(".");
|
2014-06-29 00:43:30 +00:00
|
|
|
std::ifstream in(fname);
|
|
|
|
if (in.bad() || in.fail())
|
|
|
|
throw exception(sstream() << "failed to open file '" << fname << "'");
|
|
|
|
scanner s(in, fname);
|
2014-08-03 03:03:51 +00:00
|
|
|
optional<unsigned> k;
|
2014-08-29 19:34:37 +00:00
|
|
|
std::unique_ptr<exception> ex;
|
2014-08-03 03:03:51 +00:00
|
|
|
std::string base = dirname(fname);
|
|
|
|
bool import_prefix = false;
|
|
|
|
bool import_args = false;
|
2014-07-29 21:13:31 +00:00
|
|
|
while (true) {
|
2014-08-01 02:59:05 +00:00
|
|
|
scanner::token_kind t = scanner::token_kind::Identifier;
|
2014-07-29 21:13:31 +00:00
|
|
|
try {
|
|
|
|
t = s.scan(env);
|
2014-08-01 02:59:05 +00:00
|
|
|
} catch (exception &) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-29 21:13:31 +00:00
|
|
|
if (t == scanner::token_kind::Eof) {
|
2014-08-29 19:34:37 +00:00
|
|
|
if (ex)
|
|
|
|
ex->rethrow();
|
2014-07-29 21:13:31 +00:00
|
|
|
return;
|
|
|
|
} else if (t == scanner::token_kind::CommandKeyword && s.get_token_info().value() == import) {
|
2014-08-03 03:03:51 +00:00
|
|
|
k = optional<unsigned>();
|
|
|
|
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) {
|
2014-07-29 21:13:31 +00:00
|
|
|
import_args = true;
|
2014-08-29 19:34:37 +00:00
|
|
|
try {
|
|
|
|
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<unsigned>();
|
|
|
|
import_prefix = true;
|
|
|
|
out << "\n";
|
|
|
|
} catch (exception & new_ex) {
|
|
|
|
if (!ex)
|
|
|
|
ex.reset(new_ex.clone());
|
|
|
|
}
|
2014-07-29 21:13:31 +00:00
|
|
|
} else {
|
2014-08-03 03:03:51 +00:00
|
|
|
import_args = false;
|
|
|
|
import_prefix = false;
|
2014-06-29 00:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|