2010-06-27 17:20:21 +00:00
|
|
|
#include "streamerrorhandler.hpp"
|
|
|
|
|
2020-10-22 22:13:51 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2019-04-21 10:12:32 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
|
|
|
|
2010-06-27 17:20:21 +00:00
|
|
|
#include "tokenloc.hpp"
|
|
|
|
|
|
|
|
namespace Compiler
|
|
|
|
{
|
|
|
|
// Report error to the user.
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
void StreamErrorHandler::report(const std::string& message, const TokenLoc& loc, Type type)
|
2010-06-27 17:20:21 +00:00
|
|
|
{
|
2019-04-21 10:12:32 +00:00
|
|
|
Debug::Level logLevel = Debug::Info; // Usually script warnings are not too important
|
|
|
|
if (type == ErrorMessage)
|
|
|
|
logLevel = Debug::Error;
|
|
|
|
|
|
|
|
std::stringstream text;
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
if (type == ErrorMessage)
|
2019-04-21 10:12:32 +00:00
|
|
|
text << "Error: ";
|
2010-06-27 17:20:21 +00:00
|
|
|
else
|
2019-04-21 10:12:32 +00:00
|
|
|
text << "Warning: ";
|
2010-06-27 17:20:21 +00:00
|
|
|
|
2017-02-20 20:09:15 +00:00
|
|
|
if (!mContext.empty())
|
2019-04-21 10:12:32 +00:00
|
|
|
text << mContext << " ";
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
text << "line " << loc.mLine + 1 << ", column " << loc.mColumn + 1 << " (" << loc.mLiteral << "): " << message;
|
2017-02-20 20:09:15 +00:00
|
|
|
|
2019-04-21 10:12:32 +00:00
|
|
|
Log(logLevel) << text.str();
|
2010-06-27 17:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report a file related error
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
void StreamErrorHandler::report(const std::string& message, Type type)
|
2010-06-27 17:20:21 +00:00
|
|
|
{
|
2019-04-21 10:12:32 +00:00
|
|
|
Debug::Level logLevel = Debug::Info;
|
2022-09-22 18:26:05 +00:00
|
|
|
if (type == ErrorMessage)
|
2019-04-21 10:12:32 +00:00
|
|
|
logLevel = Debug::Error;
|
|
|
|
|
|
|
|
std::stringstream text;
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
if (type == ErrorMessage)
|
2019-04-21 10:12:32 +00:00
|
|
|
text << "Error: ";
|
2010-06-27 17:20:21 +00:00
|
|
|
else
|
2019-04-21 10:12:32 +00:00
|
|
|
text << "Warning: ";
|
|
|
|
|
|
|
|
if (!mContext.empty())
|
|
|
|
text << mContext << " ";
|
|
|
|
|
|
|
|
text << "file: " << message << std::endl;
|
2010-06-27 17:20:21 +00:00
|
|
|
|
2019-04-21 10:12:32 +00:00
|
|
|
Log(logLevel) << text.str();
|
2010-06-27 17:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
void StreamErrorHandler::setContext(const std::string& context)
|
2017-02-20 20:09:15 +00:00
|
|
|
{
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:57:53 +00:00
|
|
|
StreamErrorHandler::StreamErrorHandler() = default;
|
2020-04-02 18:14:52 +00:00
|
|
|
|
2022-09-22 18:26:05 +00:00
|
|
|
ContextOverride::ContextOverride(StreamErrorHandler& handler, const std::string& context)
|
|
|
|
: mHandler(handler)
|
|
|
|
, mContext(handler.mContext)
|
2020-04-02 18:27:52 +00:00
|
|
|
{
|
2020-04-04 12:09:00 +00:00
|
|
|
mHandler.setContext(context);
|
2020-04-02 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:09:00 +00:00
|
|
|
ContextOverride::~ContextOverride()
|
2020-04-02 18:14:52 +00:00
|
|
|
{
|
2020-04-04 12:09:00 +00:00
|
|
|
mHandler.setContext(mContext);
|
2020-04-02 18:14:52 +00:00
|
|
|
}
|
2012-07-17 07:27:12 +00:00
|
|
|
}
|