Added support for full error messages
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
dc35ba4698
commit
8541085b27
|
@ -1,7 +1,11 @@
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
#ifndef PORYGONLANG_DIAGNOSTIC_HPP
|
#ifndef PORYGONLANG_DIAGNOSTIC_HPP
|
||||||
#define PORYGONLANG_DIAGNOSTIC_HPP
|
#define PORYGONLANG_DIAGNOSTIC_HPP
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
#include "DiagnosticSeverity.hpp"
|
#include "DiagnosticSeverity.hpp"
|
||||||
#include "DiagnosticCode.hpp"
|
#include "DiagnosticCode.hpp"
|
||||||
|
|
||||||
|
@ -11,12 +15,14 @@ namespace Porygon::Diagnostics {
|
||||||
DiagnosticCode _code;
|
DiagnosticCode _code;
|
||||||
unsigned int _start;
|
unsigned int _start;
|
||||||
unsigned int _length;
|
unsigned int _length;
|
||||||
|
std::vector<std::string> _arguments;
|
||||||
public:
|
public:
|
||||||
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length) {
|
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<std::string> arguments) {
|
||||||
_severity = severity;
|
_severity = severity;
|
||||||
_code = code;
|
_code = code;
|
||||||
_start = start;
|
_start = start;
|
||||||
_length = length;
|
_length = length;
|
||||||
|
_arguments = std::move(arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
DiagnosticSeverity GetSeverity() {
|
DiagnosticSeverity GetSeverity() {
|
||||||
|
@ -34,6 +40,10 @@ namespace Porygon::Diagnostics {
|
||||||
unsigned int GetLength() {
|
unsigned int GetLength() {
|
||||||
return _length;
|
return _length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> GetArguments(){
|
||||||
|
return _arguments;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif //PORYGONLANG_DIAGNOSTIC_HPP
|
#endif //PORYGONLANG_DIAGNOSTIC_HPP
|
||||||
|
|
|
@ -5,14 +5,14 @@
|
||||||
namespace Porygon::Diagnostics {
|
namespace Porygon::Diagnostics {
|
||||||
enum class DiagnosticCode {
|
enum class DiagnosticCode {
|
||||||
// Lex diagnostics
|
// Lex diagnostics
|
||||||
UnexpectedCharacter,
|
UnexpectedCharacter,
|
||||||
InvalidStringControlCharacter,
|
InvalidStringControlCharacter,
|
||||||
|
|
||||||
// Parse diagnostics
|
// Parse diagnostics
|
||||||
UnexpectedToken,
|
UnexpectedToken,
|
||||||
|
|
||||||
// Bind diagnostics
|
// Bind diagnostics
|
||||||
NoBinaryOperationFound,
|
NoBinaryOperationFound,
|
||||||
NoUnaryOperationFound,
|
NoUnaryOperationFound,
|
||||||
CantAssignVariable,
|
CantAssignVariable,
|
||||||
VariableNotFound,
|
VariableNotFound,
|
||||||
|
|
|
@ -1,26 +1,32 @@
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <memory> // For std::unique_ptr
|
||||||
#include "DiagnosticsHolder.hpp"
|
#include "DiagnosticsHolder.hpp"
|
||||||
|
#include "ErrorMessages-EN-US.cpp"
|
||||||
|
|
||||||
using namespace Porygon::Diagnostics;
|
using namespace Porygon::Diagnostics;
|
||||||
vector<Diagnostic> DiagnosticsHolder::GetDiagnostics() {
|
vector<Diagnostic> DiagnosticsHolder::GetDiagnostics() {
|
||||||
return _diagnostics;
|
return _diagnostics;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length) {
|
void DiagnosticsHolder::Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length,
|
||||||
_diagnostics.emplace_back(severity, code, start, length);
|
std::vector<string> arguments) {
|
||||||
|
_diagnostics.emplace_back(severity, code, start, length, arguments);
|
||||||
if (severity >= DiagnosticSeverity::Error){
|
if (severity >= DiagnosticSeverity::Error){
|
||||||
_hasErrors = true;
|
_hasErrors = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiagnosticsHolder::LogError(DiagnosticCode code, unsigned int start, unsigned int length) {
|
void DiagnosticsHolder::LogError(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments) {
|
||||||
Log(DiagnosticSeverity::Error, code, start, length);
|
Log(DiagnosticSeverity::Error, code, start, length, std::move(arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiagnosticsHolder::LogWarning(DiagnosticCode code, unsigned int start, unsigned int length) {
|
void DiagnosticsHolder::LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments) {
|
||||||
Log(DiagnosticSeverity::Warning, code, start, length);
|
Log(DiagnosticSeverity::Warning, code, start, length, std::move(arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigned int length) {
|
void DiagnosticsHolder::LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments) {
|
||||||
Log(DiagnosticSeverity::Info, code, start, length);
|
Log(DiagnosticSeverity::Info, code, start, length, std::move(arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DiagnosticsHolder::HasErrors() {
|
bool DiagnosticsHolder::HasErrors() {
|
||||||
|
@ -35,6 +41,76 @@ Diagnostic *DiagnosticsHolder::GetDiagnosticAt(int position) {
|
||||||
return &_diagnostics[position];
|
return &_diagnostics[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t DiagnosticsHolder::GetLineFromPosition(size_t i) {
|
||||||
|
size_t topLimit = _lineStarts.size() - 1;
|
||||||
|
size_t bottomLimit = 0;
|
||||||
|
while (true){
|
||||||
|
if (bottomLimit == topLimit){
|
||||||
|
return bottomLimit;
|
||||||
|
}
|
||||||
|
size_t half = (topLimit - bottomLimit) / 2;
|
||||||
|
size_t pos = _lineStarts[half];
|
||||||
|
size_t length = _lineLength[half];
|
||||||
|
if (pos < i && pos + length > i){
|
||||||
|
return half;
|
||||||
|
}
|
||||||
|
if (pos > i){
|
||||||
|
bottomLimit = half;
|
||||||
|
} else if (pos < i){
|
||||||
|
topLimit = half;
|
||||||
|
} else{
|
||||||
|
return half;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string SeverityToString(DiagnosticSeverity s){
|
||||||
|
switch (s){
|
||||||
|
case DiagnosticSeverity::Info: return "Info";
|
||||||
|
case DiagnosticSeverity::Warning: return "Warning";
|
||||||
|
case DiagnosticSeverity::Error: return "Error";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void findAndReplaceAll(std::string & data, const std::string& toSearch, const std::string& replaceStr)
|
||||||
|
{
|
||||||
|
// Get the first occurrence
|
||||||
|
size_t pos = data.find(toSearch);
|
||||||
|
// Repeat till end is reached
|
||||||
|
while( pos != std::string::npos)
|
||||||
|
{
|
||||||
|
// Replace this occurrence of Sub String
|
||||||
|
data.replace(pos, toSearch.size(), replaceStr);
|
||||||
|
// Get the next occurrence from the current position
|
||||||
|
pos =data.find(toSearch, pos + replaceStr.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const string PrettyDiagnostic ( const char * format, vector<string> arguments)
|
||||||
|
{
|
||||||
|
string result = string(format);
|
||||||
|
for (int i = 0; i < arguments.size(); i++){
|
||||||
|
auto keyToReplace = "{" + to_string(i) + "}";
|
||||||
|
auto argument = arguments[i];
|
||||||
|
findAndReplaceAll(result, keyToReplace, argument);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string DiagnosticsHolder::GetFullErrorMessage(Diagnostic diagnostic) {
|
||||||
|
stringstream stream;
|
||||||
|
stream << "[" << SeverityToString(diagnostic.GetSeverity()) << "] ";
|
||||||
|
auto startPos = diagnostic.GetStartPosition();
|
||||||
|
auto line = this -> GetLineFromPosition(startPos);
|
||||||
|
auto linePos = startPos - this ->GetStartPositionForLine(line);
|
||||||
|
stream << " (" << line << ", " << linePos << ") ";
|
||||||
|
auto unformatted = ErrorMessages.find(diagnostic.GetCode());
|
||||||
|
if (unformatted != ErrorMessages.end()){
|
||||||
|
stream << PrettyDiagnostic(unformatted->second, diagnostic.GetArguments());
|
||||||
|
}
|
||||||
|
return stream.str();
|
||||||
|
}
|
||||||
|
|
||||||
extern "C" int GetDiagnosticsCount (DiagnosticsHolder* diagnostics){
|
extern "C" int GetDiagnosticsCount (DiagnosticsHolder* diagnostics){
|
||||||
return diagnostics->DiagnosticsCount();
|
return diagnostics->DiagnosticsCount();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include "DiagnosticSeverity.hpp"
|
#include "DiagnosticSeverity.hpp"
|
||||||
#include "DiagnosticCode.hpp"
|
#include "DiagnosticCode.hpp"
|
||||||
#include "Diagnostic.hpp"
|
#include "Diagnostic.hpp"
|
||||||
|
@ -16,6 +17,7 @@ namespace Porygon::Diagnostics {
|
||||||
vector<Diagnostic> _diagnostics;
|
vector<Diagnostic> _diagnostics;
|
||||||
vector<size_t> _lineStarts;
|
vector<size_t> _lineStarts;
|
||||||
vector<size_t> _lineLength;
|
vector<size_t> _lineLength;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DiagnosticsHolder(const u16string& str) {
|
explicit DiagnosticsHolder(const u16string& str) {
|
||||||
_hasErrors = false;
|
_hasErrors = false;
|
||||||
|
@ -35,13 +37,13 @@ namespace Porygon::Diagnostics {
|
||||||
_diagnostics.clear();
|
_diagnostics.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length);
|
void Log(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {});
|
||||||
|
|
||||||
void LogError(DiagnosticCode code, unsigned int start, unsigned int length);
|
void LogError(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {});
|
||||||
|
|
||||||
void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length);
|
void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {});
|
||||||
|
|
||||||
void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length);
|
void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length, std::vector<string> arguments = {});
|
||||||
|
|
||||||
bool HasErrors();
|
bool HasErrors();
|
||||||
|
|
||||||
|
@ -51,28 +53,12 @@ namespace Porygon::Diagnostics {
|
||||||
|
|
||||||
Diagnostic *GetDiagnosticAt(int position);
|
Diagnostic *GetDiagnosticAt(int position);
|
||||||
|
|
||||||
size_t GetLineFromPosition(size_t i){
|
size_t GetLineFromPosition(size_t i);
|
||||||
size_t topLimit = _lineStarts.size() - 1;
|
size_t GetStartPositionForLine(size_t i){
|
||||||
size_t bottomLimit = 0;
|
return _lineStarts[i];
|
||||||
while (true){
|
|
||||||
if (bottomLimit == topLimit){
|
|
||||||
return bottomLimit;
|
|
||||||
}
|
|
||||||
size_t half = (topLimit - bottomLimit) / 2;
|
|
||||||
size_t pos = _lineStarts[half];
|
|
||||||
size_t length = _lineLength[half];
|
|
||||||
if (pos < i && pos + length > i){
|
|
||||||
return half;
|
|
||||||
}
|
|
||||||
if (pos > i){
|
|
||||||
bottomLimit = half;
|
|
||||||
} else if (pos < i){
|
|
||||||
topLimit = half;
|
|
||||||
} else{
|
|
||||||
return half;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string GetFullErrorMessage(Diagnostic diagnostic);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
#include "DiagnosticCode.hpp"
|
||||||
|
|
||||||
|
namespace Porygon::Diagnostics {
|
||||||
|
const std::unordered_map<DiagnosticCode, const char*> ErrorMessages{ // NOLINT(cert-err58-cpp)
|
||||||
|
{DiagnosticCode ::UnexpectedCharacter, "Encountered an unexpected character: '{0}'."},
|
||||||
|
{DiagnosticCode ::InvalidStringControlCharacter, "'{0}' is not a valid control character."},
|
||||||
|
};
|
||||||
|
}
|
|
@ -289,7 +289,8 @@ namespace Porygon::Parser {
|
||||||
}
|
}
|
||||||
auto closeToken = this->Next();
|
auto closeToken = this->Next();
|
||||||
if (closeToken != c) {
|
if (closeToken != c) {
|
||||||
this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1);
|
const char* s = string(1, closeToken).c_str();
|
||||||
|
this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1, { s });
|
||||||
return new SimpleToken(TokenKind::BadToken, start, end - start + 1);
|
return new SimpleToken(TokenKind::BadToken, start, end - start + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,8 +304,9 @@ namespace Porygon::Parser {
|
||||||
if (ControlCharacters.find(c) != ControlCharacters.end()) {
|
if (ControlCharacters.find(c) != ControlCharacters.end()) {
|
||||||
stream << ControlCharacters.at(c);
|
stream << ControlCharacters.at(c);
|
||||||
} else {
|
} else {
|
||||||
|
auto v = string(1, c).c_str();
|
||||||
this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidStringControlCharacter,
|
this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidStringControlCharacter,
|
||||||
start + 1 + i, 1);
|
start + 1 + i, 1, {v});
|
||||||
stream << c;
|
stream << c;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -41,6 +41,16 @@ TEST_CASE( "Get diagnostic line", "[integration]" ) {
|
||||||
delete script;
|
delete script;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Get full diagnostic message", "[integration]" ) {
|
||||||
|
auto script = Script::Create(uR"(
|
||||||
|
"\x"
|
||||||
|
)");
|
||||||
|
REQUIRE(script->Diagnostics -> HasErrors());
|
||||||
|
auto diags = script->Diagnostics -> GetDiagnostics();
|
||||||
|
auto msg = script -> Diagnostics -> GetFullErrorMessage(diags[0]);
|
||||||
|
REQUIRE(msg == "[Error] (1, 2) 'x' is not a valid control character.");
|
||||||
|
delete script;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue