Support adding filename to diagnostic.

This commit is contained in:
2020-10-05 12:18:05 +02:00
parent dffc0d7f17
commit 125bb8459c
9 changed files with 51 additions and 32 deletions

View File

@@ -4,19 +4,23 @@
#include "../TextSpan.hpp"
#include "DiagnosticLevel.hpp"
#include "DiagnosticType.hpp"
namespace ElohimScript::Diagnostics {
class Diagnostic {
DiagnosticLevel _level;
DiagnosticType _type;
std::u8string_view _scriptName;
TextSpan _span;
public:
inline Diagnostic(DiagnosticLevel level, DiagnosticType type, TextSpan span)
: _level(level), _type(type), _span(span) {}
inline Diagnostic(DiagnosticLevel level, DiagnosticType type, const std::u8string_view& scriptName,
TextSpan span)
: _level(level), _type(type), _scriptName(scriptName), _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; }
[[nodiscard]] inline const std::u8string_view& GetScriptName() const noexcept { return _scriptName; }
};
}

View File

@@ -9,14 +9,24 @@ namespace ElohimScript::Diagnostics {
std::vector<Diagnostic> _messages;
public:
inline void Log(DiagnosticLevel level, DiagnosticType type, TextSpan span) {
_messages.emplace_back(level, type, span);
inline void Log(DiagnosticLevel level, DiagnosticType type, std::u8string_view scriptName, TextSpan span) {
_messages.emplace_back(level, type, scriptName, span);
}
inline void LogTrace(DiagnosticType type, std::u8string_view scriptName, TextSpan span) {
Log(DiagnosticLevel::Trace, type, scriptName, span);
}
inline void LogInfo(DiagnosticType type, std::u8string_view scriptName, TextSpan span) {
Log(DiagnosticLevel::Information, type, scriptName, span);
}
inline void LogWarning(DiagnosticType type, std::u8string_view scriptName, TextSpan span) {
Log(DiagnosticLevel::Warning, type, scriptName, span);
}
inline void LogError(DiagnosticType type, std::u8string_view scriptName, TextSpan span) {
Log(DiagnosticLevel::Error, type, scriptName, span);
}
inline void LogCritical(DiagnosticType type, std::u8string_view scriptName, TextSpan span) {
Log(DiagnosticLevel::Critical, type, scriptName, 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; }
};

View File

@@ -272,7 +272,7 @@ namespace ElohimScript::Parser {
default:
if (IsAlphaNumericalOrUnderscore(c))
return LexKeywordOrIdentifier();
_diagnostics->LogError(Diagnostics::DiagnosticType::UnknownToken, TextSpan(start, start + 1));
LogError(Diagnostics::DiagnosticType::UnknownToken, TextSpan(start, start + 1));
return Create<LexTokenImpl<LexTokenKind::Unknown>>(TextSpan(start, start + 1));
}
}
@@ -295,8 +295,8 @@ namespace ElohimScript::Parser {
case 'b':
case 'B': numericalSystem = 2; break;
default:
_diagnostics->LogError(Diagnostics::DiagnosticType::InvalidNumericalBase,
TextSpan(_position - 1, _position + 1));
LogError(Diagnostics::DiagnosticType::InvalidNumericalBase,
TextSpan(_position - 1, _position + 1));
// Set to the largest numerical system, so we can prevent errors down the line.
numericalSystem = 16;
break;
@@ -442,13 +442,11 @@ namespace ElohimScript::Parser {
break;
}
if (current == u8'\0') {
_diagnostics->LogError(Diagnostics::DiagnosticType::ExpectedEndOfString,
TextSpan(start, start + offset));
LogError(Diagnostics::DiagnosticType::ExpectedEndOfString, TextSpan(start, start + offset));
break;
}
if (!heredoc && (current == u8'\n' || current == u8'\r')) {
_diagnostics->LogError(Diagnostics::DiagnosticType::ExpectedEndOfString,
TextSpan(start, start + offset));
LogError(Diagnostics::DiagnosticType::ExpectedEndOfString, TextSpan(start, start + offset));
break;
}
offset++;

View File

@@ -9,14 +9,16 @@
namespace ElohimScript::Parser {
class Lexer {
public:
Lexer(const char* script, Diagnostics::Diagnostics* diag)
: Lexer(reinterpret_cast<const char8_t*>(script), diag) {}
Lexer(const char8_t* script, Diagnostics::Diagnostics* diag) : Lexer(std::u8string_view(script), diag) {}
Lexer(std::u8string_view script, Diagnostics::Diagnostics* diag)
: _script(script), _scriptLength(script.size()), _diagnostics(diag) {}
Lexer(const char* scriptName, const char* script, Diagnostics::Diagnostics* diag)
: Lexer(reinterpret_cast<const char8_t*>(scriptName), reinterpret_cast<const char8_t*>(script), diag) {}
Lexer(const char8_t* scriptName, const char8_t* script, Diagnostics::Diagnostics* diag)
: Lexer(std::u8string_view(scriptName), std::u8string_view(script), diag) {}
Lexer(std::u8string_view scriptName, std::u8string_view script, Diagnostics::Diagnostics* diag)
: _scriptName(scriptName), _script(script), _scriptLength(script.size()), _diagnostics(diag) {}
const LexToken* Lex();
private:
std::u8string_view _scriptName;
std::u8string_view _script;
size_t _position = -1;
size_t _scriptLength;
@@ -56,6 +58,10 @@ namespace ElohimScript::Parser {
template <class T, class... parameters> inline T* Create(parameters... args) {
return _allocator.Create<T>(args...);
}
inline void LogError(Diagnostics::DiagnosticType type, TextSpan span) {
_diagnostics->LogError(type, _scriptName, span);
}
};
}