Support for diagnostics system.

This commit is contained in:
2020-10-04 19:38:13 +02:00
parent 20976010d6
commit b6a5e047c2
13 changed files with 347 additions and 217 deletions

View File

@@ -0,0 +1,23 @@
#ifndef ELOHIMSCRIPT_DIAGNOSTIC_HPP
#define ELOHIMSCRIPT_DIAGNOSTIC_HPP
#include "../Parser/TextSpan.hpp"
#include "DiagnosticLevel.hpp"
#include "DiagnosticType.hpp"
namespace ElohimScript::Diagnostics {
class Diagnostic {
DiagnosticLevel _level;
DiagnosticType _type;
TextSpan _span;
public:
inline Diagnostic(DiagnosticLevel level, DiagnosticType type, TextSpan span)
: _level(level), _type(type), _span(span) {}
[[nodiscard]] inline DiagnosticLevel GetLevel() const noexcept { return _level; }
[[nodiscard]] inline DiagnosticType GetType() const noexcept { return _type; }
[[nodiscard]] inline const TextSpan& GetSpan() const noexcept { return _span; }
};
}
#endif // ELOHIMSCRIPT_DIAGNOSTIC_HPP

View File

@@ -0,0 +1,15 @@
#ifndef ELOHIMSCRIPT_DIAGNOSTICLEVEL_HPP
#define ELOHIMSCRIPT_DIAGNOSTICLEVEL_HPP
#include <cstdint>
namespace ElohimScript::Diagnostics {
enum class DiagnosticLevel : uint8_t {
Trace,
Information,
Warning,
Error,
Critical,
};
}
#endif // ELOHIMSCRIPT_DIAGNOSTICLEVEL_HPP

View File

@@ -0,0 +1,20 @@
#ifndef ELOHIMSCRIPT_DIAGNOSTICTYPE_HPP
#define ELOHIMSCRIPT_DIAGNOSTICTYPE_HPP
#include <string>
namespace ElohimScript::Diagnostics {
enum class DiagnosticType : uint8_t { UnknownToken, InvalidNumericalBase, ExpectedEndOfString };
class DiagnosticTypeHelper {
static std::string ToEnglishString(DiagnosticType type) {
switch (type) {
case DiagnosticType::UnknownToken: return "Unknown token";
case DiagnosticType::InvalidNumericalBase: return "Invalid numerical base";
case DiagnosticType::ExpectedEndOfString: return "Expected end of string";
}
return std::to_string((uint8_t)type);
}
};
}
#endif // ELOHIMSCRIPT_DIAGNOSTICTYPE_HPP

View File

@@ -0,0 +1 @@
#include "Diagnostics.hpp"

View File

@@ -0,0 +1,25 @@
#ifndef ELOHIMSCRIPT_DIAGNOSTICS_HPP
#define ELOHIMSCRIPT_DIAGNOSTICS_HPP
#include <vector>
#include "Diagnostic.hpp"
namespace ElohimScript::Diagnostics {
class Diagnostics {
std::vector<Diagnostic> _messages;
public:
inline void Log(DiagnosticLevel level, DiagnosticType type, TextSpan span) {
_messages.emplace_back(level, type, span);
}
inline void LogTrace(DiagnosticType type, TextSpan span) { Log(DiagnosticLevel::Trace, type, span); }
inline void LogInfo(DiagnosticType type, TextSpan span) { Log(DiagnosticLevel::Information, type, span); }
inline void LogWarning(DiagnosticType type, TextSpan span) { Log(DiagnosticLevel::Warning, type, span); }
inline void LogError(DiagnosticType type, TextSpan span) { Log(DiagnosticLevel::Error, type, span); }
inline void LogCritical(DiagnosticType type, TextSpan span) { Log(DiagnosticLevel::Critical, type, span); }
[[nodiscard]] const std::vector<Diagnostic>& GetMessages() const noexcept { return _messages; }
};
}
#endif // ELOHIMSCRIPT_DIAGNOSTICS_HPP