2014-01-14 00:54:21 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <utility>
|
|
|
|
#include <string>
|
|
|
|
#include "util/script_exception.h"
|
|
|
|
#include "util/name_set.h"
|
|
|
|
#include "kernel/kernel_exception.h"
|
|
|
|
#include "kernel/for_each_fn.h"
|
|
|
|
#include "library/io_state_stream.h"
|
2014-04-29 18:52:09 +00:00
|
|
|
// #include "library/elaborator/elaborator_justification.h"
|
|
|
|
// #include "library/elaborator/elaborator_exception.h"
|
2014-01-14 00:54:21 +00:00
|
|
|
#include "library/parser_nested_exception.h"
|
2014-04-29 18:52:09 +00:00
|
|
|
// #include "library/unsolved_metavar_exception.h"
|
2014-01-14 00:54:21 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-04-30 17:28:07 +00:00
|
|
|
void display_error_pos(io_state_stream const & ios, char const * strm_name, unsigned line, unsigned pos) {
|
|
|
|
ios << strm_name << ":" << line << ":";
|
2014-01-14 00:54:21 +00:00
|
|
|
if (pos != static_cast<unsigned>(-1))
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << pos << ":";
|
|
|
|
ios << " error:";
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
void display_error_pos(io_state_stream const & ios, pos_info_provider const * p, expr const & e) {
|
2014-01-14 00:54:21 +00:00
|
|
|
if (p) {
|
|
|
|
auto pos = p->get_pos_info(e);
|
|
|
|
display_error_pos(ios, p->get_file_name(), pos.first, pos.second);
|
|
|
|
} else {
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << "error:";
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
void display_error_pos(io_state_stream const & ios, pos_info_provider const * p, optional<expr> const & e) {
|
2014-01-14 00:54:21 +00:00
|
|
|
if (e) {
|
|
|
|
display_error_pos(ios, p, *e);
|
|
|
|
} else if (p) {
|
|
|
|
auto pos = p->get_some_pos();
|
|
|
|
display_error_pos(ios, p->get_file_name(), pos.first, pos.second);
|
|
|
|
} else {
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << "error:";
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
void display_error(io_state_stream const & ios, pos_info_provider const * p, exception const & ex);
|
2014-01-14 00:54:21 +00:00
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
static void display_error(io_state_stream const & ios, pos_info_provider const * p, kernel_exception const & ex) {
|
2014-01-14 00:54:21 +00:00
|
|
|
display_error_pos(ios, p, ex.get_main_expr());
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " " << ex << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
// static void display_error(io_state_stream const & ios, pos_info_provider const * p, elaborator_exception const & ex) {
|
2014-04-29 18:52:09 +00:00
|
|
|
// formatter fmt = ios.get_formatter();
|
|
|
|
// options opts = ios.get_options();
|
|
|
|
// auto j = ex.get_justification();
|
|
|
|
// j = remove_detail(j);
|
2014-04-30 17:28:07 +00:00
|
|
|
// ios << mk_pair(j.pp(fmt, opts, p, true), opts) << endl;
|
2014-04-29 18:52:09 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// struct delta_pos_info_provider : public pos_info_provider {
|
|
|
|
// unsigned m_delta;
|
|
|
|
// std::string m_file_name;
|
|
|
|
// pos_info_provider const & m_provider;
|
|
|
|
// delta_pos_info_provider(unsigned d, char const * fname, pos_info_provider const & p):m_delta(d), m_file_name(fname), m_provider(p) {}
|
|
|
|
// virtual std::pair<unsigned, unsigned> get_pos_info(expr const & e) const {
|
|
|
|
// auto r = m_provider.get_pos_info(e);
|
|
|
|
// return mk_pair(r.first + m_delta, r.second);
|
|
|
|
// }
|
|
|
|
// virtual char const * get_file_name() const { return m_file_name.c_str(); }
|
|
|
|
// virtual std::pair<unsigned, unsigned> get_some_pos() const {
|
|
|
|
// auto r = m_provider.get_some_pos();
|
|
|
|
// return mk_pair(r.first + m_delta, r.second);
|
|
|
|
// }
|
|
|
|
// };
|
2014-01-14 00:54:21 +00:00
|
|
|
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
static void display_error(io_state_stream const & ios, pos_info_provider const * p, script_exception const & ex) {
|
2014-01-14 00:54:21 +00:00
|
|
|
if (p) {
|
|
|
|
char const * msg = ex.get_msg();
|
|
|
|
char const * space = msg && *msg == ' ' ? "" : " ";
|
|
|
|
switch (ex.get_source()) {
|
|
|
|
case script_exception::source::String:
|
|
|
|
display_error_pos(ios, p->get_file_name(), ex.get_line() + p->get_some_pos().first - 1, static_cast<unsigned>(-1));
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " executing script," << space << msg << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
break;
|
|
|
|
case script_exception::source::File:
|
|
|
|
display_error_pos(ios, p->get_file_name(), p->get_some_pos().first, p->get_some_pos().second);
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " executing external script (" << ex.get_file_name() << ":" << ex.get_line() << ")," << space << msg << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
break;
|
|
|
|
case script_exception::source::Unknown:
|
|
|
|
display_error_pos(ios, p->get_file_name(), p->get_some_pos().first, p->get_some_pos().second);
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " executing script, exact error position is not available, " << ex.what() << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << ex.what() << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
static void display_error(io_state_stream const & ios, pos_info_provider const * p, script_nested_exception const & ex) {
|
2014-01-14 00:54:21 +00:00
|
|
|
switch (ex.get_source()) {
|
|
|
|
case script_exception::source::String:
|
|
|
|
if (p) {
|
|
|
|
display_error_pos(ios, p->get_file_name(), ex.get_line() + p->get_some_pos().first - 1, static_cast<unsigned>(-1));
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " executing script" << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
display_error(ios, nullptr, ex.get_exception());
|
|
|
|
break;
|
|
|
|
case script_exception::source::File:
|
|
|
|
if (p) {
|
|
|
|
display_error_pos(ios, p->get_file_name(), p->get_some_pos().first, p->get_some_pos().second);
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " executing external script (" << ex.get_file_name() << ":" << ex.get_line() << ")" << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
} else {
|
|
|
|
display_error_pos(ios, ex.get_file_name(), ex.get_line(), -1);
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " executing script" << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
display_error(ios, nullptr, ex.get_exception());
|
|
|
|
break;
|
|
|
|
case script_exception::source::Unknown:
|
|
|
|
display_error(ios, nullptr, ex.get_exception());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
// static void display_error(io_state_stream const & ios, pos_info_provider const *, parser_nested_exception const & ex) {
|
2014-04-29 18:52:09 +00:00
|
|
|
// display_error(ios, &(ex.get_provider()), ex.get_exception());
|
|
|
|
// }
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
// static void display_error(io_state_stream const & ios, pos_info_provider const *, parser_exception const & ex) {
|
|
|
|
// ios << ex.what() << endl;
|
2014-04-29 18:52:09 +00:00
|
|
|
// }
|
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
// static void display_error(io_state_stream const & ios, pos_info_provider const * p, unsolved_metavar_exception const & ex) {
|
2014-04-29 18:52:09 +00:00
|
|
|
// display_error_pos(ios, p, ex.get_expr());
|
|
|
|
// formatter fmt = ios.get_formatter();
|
|
|
|
// options opts = ios.get_options();
|
|
|
|
// unsigned indent = get_pp_indent(opts);
|
|
|
|
// format r = nest(indent, compose(line(), fmt(ex.get_expr(), opts)));
|
2014-04-30 17:28:07 +00:00
|
|
|
// ios << " " << ex.what() << mk_pair(r, opts) << endl;
|
2014-04-29 18:52:09 +00:00
|
|
|
// if (p) {
|
|
|
|
// name_set already_displayed;
|
|
|
|
// for_each(ex.get_expr(), [&](expr const & e, unsigned) -> bool {
|
|
|
|
// if (is_metavar(e)) {
|
|
|
|
// name const & m = metavar_name(e);
|
|
|
|
// if (already_displayed.find(m) == already_displayed.end()) {
|
|
|
|
// already_displayed.insert(m);
|
2014-04-30 17:28:07 +00:00
|
|
|
// for (unsigned i = 0; i < indent; i++) ios << " ";
|
2014-04-29 18:52:09 +00:00
|
|
|
// display_error_pos(ios, p, e);
|
2014-04-30 17:28:07 +00:00
|
|
|
// ios << " unsolved metavar " << m << endl;
|
2014-04-29 18:52:09 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// }
|
2014-01-14 00:54:21 +00:00
|
|
|
|
2014-04-30 17:28:07 +00:00
|
|
|
void display_error(io_state_stream const & ios, pos_info_provider const * p, exception const & ex) {
|
2014-01-14 00:54:21 +00:00
|
|
|
if (auto k_ex = dynamic_cast<kernel_exception const *>(&ex)) {
|
|
|
|
display_error(ios, p, *k_ex);
|
2014-04-29 18:52:09 +00:00
|
|
|
// } else if (auto e_ex = dynamic_cast<elaborator_exception const *>(&ex)) {
|
|
|
|
// display_error(ios, p, *e_ex);
|
|
|
|
// } else if (auto m_ex = dynamic_cast<unsolved_metavar_exception const *>(&ex)) {
|
|
|
|
// display_error(ios, p, *m_ex);
|
2014-01-14 00:54:21 +00:00
|
|
|
} else if (auto ls_ex = dynamic_cast<script_nested_exception const *>(&ex)) {
|
|
|
|
display_error(ios, p, *ls_ex);
|
|
|
|
} else if (auto s_ex = dynamic_cast<script_exception const *>(&ex)) {
|
|
|
|
display_error(ios, p, *s_ex);
|
2014-04-29 18:52:09 +00:00
|
|
|
// } else if (auto n_ex = dynamic_cast<parser_nested_exception const *>(&ex)) {
|
|
|
|
// display_error(ios, p, *n_ex);
|
|
|
|
// } else if (auto n_ex = dynamic_cast<parser_exception const *>(&ex)) {
|
|
|
|
// display_error(ios, p, *n_ex);
|
2014-01-14 00:54:21 +00:00
|
|
|
} else if (p) {
|
|
|
|
display_error_pos(ios, p->get_file_name(), p->get_some_pos().first, p->get_some_pos().second);
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << " " << ex.what() << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
} else {
|
2014-04-30 17:28:07 +00:00
|
|
|
ios << "error: " << ex.what() << endl;
|
2014-01-14 00:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|