metaforce/hecl/lib/Frontend/Diagnostics.cpp

136 lines
4.1 KiB
C++
Raw Normal View History

2015-10-09 02:08:10 +00:00
#include "HECL/HECL.hpp"
#include "HECL/Frontend.hpp"
#include <stdarg.h>
/* ANSI sequences */
#define RED "\x1b[1;31m"
#define YELLOW "\x1b[1;33m"
#define GREEN "\x1b[1;32m"
#define MAGENTA "\x1b[1;35m"
#define CYAN "\x1b[1;36m"
#define BOLD "\x1b[1m"
#define NORMAL "\x1b[0m"
namespace HECL
{
namespace Frontend
{
2015-10-14 23:06:47 +00:00
std::string Diagnostics::sourceDiagString(const SourceLocation& l, bool ansi) const
{
std::string::const_iterator it = m_source.begin();
for (int i=1 ; i<l.line ; ++i)
{
while (*it != '\n' && it != m_source.end())
++it;
if (*it == '\n')
++it;
}
std::string::const_iterator begin = it;
while (*it != '\n' && it != m_source.end())
++it;
std::string::const_iterator end = it;
std::string retval(begin, end);
retval += '\n';
for (int i=1 ; i<l.col ; ++i)
retval += ' ';
if (ansi)
retval += GREEN "^" NORMAL;
else
retval += '^';
return retval;
}
2015-10-09 02:08:10 +00:00
void Diagnostics::reportParserErr(const SourceLocation& l, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char* result = nullptr;
#ifdef _WIN32
int length = _vscprintf(fmt, ap);
result = (char*)malloc(length);
vsnprintf(result, length, fmt, ap);
#else
vasprintf(&result, fmt, ap);
#endif
va_end(ap);
if (LogVisor::XtermColor)
2015-10-14 23:06:47 +00:00
LogModule.report(LogVisor::FatalError, CYAN "[Parser]" NORMAL " %s " YELLOW "@%d:%d " NORMAL "\n%s\n%s",
m_name.c_str(), l.line, l.col, result, sourceDiagString(l, true).c_str());
2015-10-09 02:08:10 +00:00
else
2015-10-14 23:06:47 +00:00
LogModule.report(LogVisor::FatalError, "[Parser] %s @%d:%d\n%s\n%s",
m_name.c_str(), l.line, l.col, result, sourceDiagString(l, false).c_str());
2015-10-09 02:08:10 +00:00
free(result);
}
void Diagnostics::reportLexerErr(const SourceLocation& l, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char* result = nullptr;
#ifdef _WIN32
int length = _vscprintf(fmt, ap);
result = (char*)malloc(length);
vsnprintf(result, length, fmt, ap);
#else
vasprintf(&result, fmt, ap);
#endif
va_end(ap);
if (LogVisor::XtermColor)
2015-10-14 23:06:47 +00:00
LogModule.report(LogVisor::FatalError, CYAN "[Lexer]" NORMAL " %s " YELLOW "@%d:%d " NORMAL "\n%s\n%s",
m_name.c_str(), l.line, l.col, result, sourceDiagString(l, true).c_str());
2015-10-09 02:08:10 +00:00
else
2015-10-14 23:06:47 +00:00
LogModule.report(LogVisor::FatalError, "[Lexer] %s @%d:%d\n%s\n%s",
m_name.c_str(), l.line, l.col, result, sourceDiagString(l, false).c_str());
2015-10-09 02:08:10 +00:00
free(result);
}
2015-10-14 02:16:21 +00:00
void Diagnostics::reportCompileErr(const SourceLocation& l, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char* result = nullptr;
#ifdef _WIN32
int length = _vscprintf(fmt, ap);
result = (char*)malloc(length);
vsnprintf(result, length, fmt, ap);
#else
vasprintf(&result, fmt, ap);
#endif
va_end(ap);
if (LogVisor::XtermColor)
2015-10-14 23:06:47 +00:00
LogModule.report(LogVisor::FatalError, CYAN "[Compiler]" NORMAL " %s " YELLOW "@%d:%d " NORMAL "\n%s\n%s",
m_name.c_str(), l.line, l.col, result, sourceDiagString(l, true).c_str());
2015-10-14 02:16:21 +00:00
else
2015-10-14 23:06:47 +00:00
LogModule.report(LogVisor::FatalError, "[Compiler] %s @%d:%d\n%s\n%s",
m_name.c_str(), l.line, l.col, result, sourceDiagString(l, false).c_str());
2015-10-14 02:16:21 +00:00
free(result);
}
void Diagnostics::reportBackendErr(const SourceLocation& l, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
char* result = nullptr;
#ifdef _WIN32
int length = _vscprintf(fmt, ap);
result = (char*)malloc(length);
vsnprintf(result, length, fmt, ap);
#else
vasprintf(&result, fmt, ap);
#endif
va_end(ap);
if (LogVisor::XtermColor)
2015-10-16 00:34:47 +00:00
LogModule.report(LogVisor::FatalError, CYAN "[%s]" NORMAL " %s " YELLOW "@%d:%d " NORMAL "\n%s\n%s",
m_backend.c_str(), m_name.c_str(), l.line, l.col, result, sourceDiagString(l, true).c_str());
2015-10-14 02:16:21 +00:00
else
2015-10-16 00:34:47 +00:00
LogModule.report(LogVisor::FatalError, "[%s] %s @%d:%d\n%s\n%s",
m_backend.c_str(), m_name.c_str(), l.line, l.col, result, sourceDiagString(l, false).c_str());
2015-10-14 02:16:21 +00:00
free(result);
}
2015-10-09 02:08:10 +00:00
}
}