2013-11-15 20:13:03 +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 <sstream>
|
|
|
|
#include <string>
|
2013-11-28 21:49:18 +00:00
|
|
|
#include <cstdlib>
|
2013-11-27 22:57:33 +00:00
|
|
|
#include "util/debug.h"
|
2013-11-27 03:15:49 +00:00
|
|
|
#include "util/script_exception.h"
|
2013-11-15 20:13:03 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-11-27 22:57:33 +00:00
|
|
|
script_exception::script_exception(char const * lua_error) {
|
|
|
|
lean_assert(lua_error);
|
|
|
|
std::string fname;
|
|
|
|
std::string line;
|
|
|
|
std::string msg;
|
|
|
|
int state = 0;
|
|
|
|
char const * it = lua_error;
|
|
|
|
while (*it) {
|
|
|
|
if (state == 0) {
|
|
|
|
if (*it == ':') {
|
|
|
|
state = 1;
|
|
|
|
} else {
|
|
|
|
fname += *it;
|
|
|
|
}
|
|
|
|
} else if (state == 1) {
|
|
|
|
if (*it == ':') {
|
|
|
|
state = 2;
|
|
|
|
} else {
|
|
|
|
line += *it;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg += *it;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
if (state != 2) {
|
|
|
|
// failed to decode Lua error message
|
|
|
|
m_source = source::Unknown;
|
|
|
|
m_msg = lua_error;
|
|
|
|
} else {
|
|
|
|
if (fname == "[string \"...\"]") {
|
|
|
|
m_source = source::String;
|
|
|
|
} else {
|
|
|
|
m_source = source::File;
|
|
|
|
m_file = fname;
|
|
|
|
}
|
|
|
|
m_line = atoi(line.c_str());
|
|
|
|
m_msg = msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
script_exception::~script_exception() {
|
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:21 +00:00
|
|
|
char const * script_exception::get_file_name() const {
|
2013-11-27 22:57:33 +00:00
|
|
|
lean_assert(get_source() == source::File);
|
|
|
|
return m_file.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned script_exception::get_line() const {
|
|
|
|
lean_assert(get_source() != source::Unknown);
|
|
|
|
return m_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const * script_exception::get_msg() const noexcept {
|
|
|
|
return exception::what();
|
|
|
|
}
|
|
|
|
|
2013-11-15 20:13:03 +00:00
|
|
|
char const * script_exception::what() const noexcept {
|
|
|
|
static thread_local std::string buffer;
|
|
|
|
std::ostringstream strm;
|
2014-01-14 00:54:21 +00:00
|
|
|
char const * msg = get_msg();
|
|
|
|
char const * space = msg && *msg == ' ' ? "" : " ";
|
2013-11-15 20:13:03 +00:00
|
|
|
switch (get_source()) {
|
2014-01-14 00:54:21 +00:00
|
|
|
case source::String: strm << "[string]:" << get_line() << ":" << space << get_msg(); break;
|
|
|
|
case source::File: strm << get_file_name() << ":" << get_line() << ":" << space << get_msg(); break;
|
2013-11-15 20:13:03 +00:00
|
|
|
case source::Unknown: return get_msg();
|
|
|
|
}
|
|
|
|
buffer = strm.str();
|
|
|
|
return buffer.c_str();
|
|
|
|
}
|
2014-01-14 00:54:21 +00:00
|
|
|
|
|
|
|
script_nested_exception::script_nested_exception(source s, std::string f, unsigned l, std::shared_ptr<exception> const & ex):
|
|
|
|
script_exception(s, f, l, "Lean exception"),
|
|
|
|
m_exception(ex) {
|
|
|
|
lean_assert(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
script_nested_exception::~script_nested_exception() {}
|
|
|
|
|
|
|
|
char const * script_nested_exception::what() const noexcept {
|
|
|
|
static thread_local std::string buffer;
|
|
|
|
std::ostringstream strm;
|
|
|
|
strm << script_exception::what() << "\n" << get_exception().what();
|
|
|
|
buffer = strm.str();
|
|
|
|
return buffer.c_str();
|
|
|
|
}
|
2013-11-15 20:13:03 +00:00
|
|
|
}
|