feat(frontends/lean/parser): gracefully handle scanner exceptions in imports

This commit is contained in:
Gabriel Ebner 2017-01-12 08:12:59 +01:00 committed by Leonardo de Moura
parent 7319000c98
commit db81e4b5b8
5 changed files with 27 additions and 13 deletions

View file

@ -2111,14 +2111,13 @@ void parser::reset_doc_string() {
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
std::vector<module_name> parser::parse_imports(unsigned & fingerprint) {
void parser::parse_imports(unsigned & fingerprint, std::vector<module_name> & imports) {
m_last_cmd_pos = pos();
bool prelude = false;
if (curr_is_token(get_prelude_tk())) {
next();
prelude = true;
}
std::vector<module_name> imports;
if (!prelude) {
imports.push_back({ "init", optional<unsigned>() });
}
@ -2171,12 +2170,18 @@ std::vector<module_name> parser::parse_imports(unsigned & fingerprint) {
}
}
}
return imports;
}
void parser::process_imports() {
unsigned fingerprint = 0;
auto imports = parse_imports(fingerprint);
std::vector<module_name> imports;
std::exception_ptr exception_during_scanning;
try {
parse_imports(fingerprint, imports);
} catch (parser_exception) {
exception_during_scanning = std::current_exception();
}
buffer<import_error> import_errors;
m_env = import_modules(m_env, m_file_name, imports, m_import_fn, import_errors);
@ -2209,12 +2214,14 @@ void parser::process_imports() {
#endif
}
m_imports_parsed = true;
if (exception_during_scanning) std::rethrow_exception(exception_during_scanning);
}
std::vector<module_name> parser::get_imports() {
void parser::get_imports(std::vector<module_name> & imports) {
scope_pos_info_provider scope1(*this);
unsigned fingerprint;
return parse_imports(fingerprint);
parse_imports(fingerprint, imports);
}
bool parser::parse_commands() {

View file

@ -431,7 +431,7 @@ public:
expr parse_tactic(unsigned rbp = 0);
std::vector<module_name> parse_imports(unsigned & fingerprint);
void parse_imports(unsigned & fingerprint, std::vector<module_name> &);
struct local_scope {
parser & m_p; environment m_env;
@ -522,7 +522,7 @@ public:
/** parse all commands in the input stream */
bool operator()() { return parse_commands(); }
std::vector<module_name> get_imports();
void get_imports(std::vector<module_name> &);
class in_notation_ctx {
scanner::in_notation_ctx m_ctx;

View file

@ -404,16 +404,16 @@ module_mgr::get_snapshots_or_unchanged_module(module_id const &id, std::string c
}
std::vector<module_name> module_mgr::get_direct_imports(module_id const & id, std::string const & contents) {
std::vector<module_name> imports;
try {
scope_message_context scope("dependencies");
std::istringstream in(contents);
bool use_exceptions = true;
parser p(get_initial_env(), m_ios, nullptr, in, id, use_exceptions);
std::vector<std::pair<module_name, module_id>> deps;
return p.get_imports();
} catch (...) {
return {};
}
p.get_imports(imports);
} catch (...) {}
return imports;
}
std::tuple<std::string, module_src, time_t> fs_module_vfs::load_module(module_id const & id, bool can_use_olean) {

View file

@ -0,0 +1,3 @@
import data.bitvec 0b311
print bitvec

View file

@ -0,0 +1,4 @@
import_invalid_tk.lean:1:21: error: invalid binary digit
attribute [reducible]
definition bitvec : → Type :=
λ (n : ), tuple bool n