Started implementing colored output.

This commit is contained in:
2016-06-14 12:47:39 +02:00
parent e0dd9833a3
commit 342a346fce
22 changed files with 238 additions and 62 deletions

View File

@@ -71,7 +71,7 @@ std::unique_ptr<Derived> NAry<Derived>::parse(Context &context,
}
if (expression->m_arguments.empty())
context.logger.parserWarning(context.parser, "\"" + Derived::Identifier + "\" expressions should not be empty");
context.logger.parserWarning(context.parser, "" + Derived::Identifier + " expressions should not be empty");
parser.expect<std::string>(")");

View File

@@ -0,0 +1,81 @@
#ifndef __PLASP__UTILS__FORMATTING_H
#define __PLASP__UTILS__FORMATTING_H
#include <iostream>
#include <unistd.h>
namespace plasp
{
namespace utils
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Formatting
//
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class Color
{
Black = 0,
Red = 1,
Green = 2,
Yellow = 3,
Blue = 4,
Magenta = 5,
Cyan = 6,
White = 7
};
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class FontWeight
{
Normal = 0,
Bold = 1
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Format
{
Format(Color color, FontWeight fontWeight = FontWeight::Normal)
: color{color},
fontWeight{fontWeight}
{
}
Color color;
FontWeight fontWeight;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator<<(std::ostream &ostream, const Format &format)
{
const auto fontWeightCode = static_cast<size_t>(format.fontWeight);
const auto colorCode = 30 + static_cast<size_t>(format.color);
return (ostream << "\033[" << fontWeightCode << ";" << colorCode << "m");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
class ResetFormat
{
};
////////////////////////////////////////////////////////////////////////////////////////////////////
std::ostream &operator<<(std::ostream &ostream, const ResetFormat &)
{
return (ostream << "\033[0m");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -4,6 +4,7 @@
#include <string>
#include <plasp/utils/Parser.h>
#include <plasp/utils/ParserException.h>
namespace plasp
{
@@ -37,6 +38,8 @@ class Logger
void setWarningLevel(WarningLevel warningLevel);
void exception(const std::string &errorType, const std::string &message);
void parserException(const Parser::Coordinate &coordinate, const std::string &message);
void parserWarning(const Parser &parser, const std::string &message);
private:

View File

@@ -31,10 +31,11 @@ class ParserException: public std::exception
}
explicit ParserException(const utils::Parser &parser, const std::string &message)
: m_coordinate{parser.coordinate()},
m_message{message},
m_plainMessage{m_coordinate.sectionName + ":" + std::to_string(m_coordinate.row)
+ ":" + std::to_string(m_coordinate.column) + " " + m_message}
{
const auto coordinate = parser.coordinate();
m_message = coordinate.sectionName + ":" + std::to_string(coordinate.row) + ":" + std::to_string(coordinate.column) + " " + message;
}
~ParserException() throw()
@@ -43,14 +44,23 @@ class ParserException: public std::exception
const char *what() const throw()
{
if (m_message.empty())
return "Unspecified parser error";
return m_plainMessage.c_str();
}
return m_message.c_str();
const Parser::Coordinate &coordinate() const
{
return m_coordinate;
}
const std::string &message() const
{
return m_message;
}
private:
Parser::Coordinate m_coordinate;
std::string m_message;
std::string m_plainMessage;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -31,10 +31,11 @@ class ParserWarning: public std::exception
}
explicit ParserWarning(const utils::Parser &parser, const std::string &message)
: m_coordinate{parser.coordinate()},
m_message{message},
m_plainMessage{m_coordinate.sectionName + ":" + std::to_string(m_coordinate.row)
+ ":" + std::to_string(m_coordinate.column) + " " + m_message}
{
const auto coordinate = parser.coordinate();
m_message = coordinate.sectionName + ":" + std::to_string(coordinate.row) + ":" + std::to_string(coordinate.column) + " " + message;
}
~ParserWarning() throw()
@@ -43,14 +44,23 @@ class ParserWarning: public std::exception
const char *what() const throw()
{
if (m_message.empty())
return "Unspecified parser warning";
return m_plainMessage.c_str();
}
return m_message.c_str();
const Parser::Coordinate &coordinate() const
{
return m_coordinate;
}
const std::string &message() const
{
return m_message;
}
private:
Parser::Coordinate m_coordinate;
std::string m_message;
std::string m_plainMessage;
};
////////////////////////////////////////////////////////////////////////////////////////////////////