Move Diagnostics to separate namespace

This commit is contained in:
Deukhoofd 2019-06-18 16:39:36 +02:00
parent 88df299e14
commit e07d5cb7cb
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
11 changed files with 127 additions and 118 deletions

View File

@ -77,7 +77,7 @@ namespace Porygon::Binder {
auto key = assignment.GetKey(); auto key = assignment.GetKey();
return new BoundAssignmentStatement(key, boundExpression); return new BoundAssignmentStatement(key, boundExpression);
} else { } else {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -96,7 +96,7 @@ namespace Porygon::Binder {
auto boundIndexType = indexable->GetType(); auto boundIndexType = indexable->GetType();
if (boundIndexType->GetClass() != TypeClass::Error && if (boundIndexType->GetClass() != TypeClass::Error &&
boundIndexType->operator!=(valueExpression->GetType().get())) { boundIndexType->operator!=(valueExpression->GetType().get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
statement->GetStartPosition(), statement->GetLength()); statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -133,7 +133,7 @@ namespace Porygon::Binder {
auto var = parameters->at(i); auto var = parameters->at(i);
auto parsedType = ParseTypeIdentifier(var->GetType()); auto parsedType = ParseTypeIdentifier(var->GetType());
if (parsedType == nullptr) { if (parsedType == nullptr) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTypeName, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTypeName, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -154,7 +154,7 @@ namespace Porygon::Binder {
auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type); auto assignment = this->_scope->AssignVariable(identifier.GetHash(), type);
if (assignment.GetResult() != VariableAssignmentResult::Ok) { if (assignment.GetResult() != VariableAssignmentResult::Ok) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantAssignVariable, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -173,7 +173,7 @@ namespace Porygon::Binder {
currentReturnType = this->_currentFunction->GetReturnType(); currentReturnType = this->_currentFunction->GetReturnType();
} }
if (expression == nullptr && currentReturnType != nullptr) { if (expression == nullptr && currentReturnType != nullptr) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -188,7 +188,7 @@ namespace Porygon::Binder {
return new BoundReturnStatement(boundExpression); return new BoundReturnStatement(boundExpression);
} }
if (currentReturnType.get()->operator!=(expresionType.get())) { if (currentReturnType.get()->operator!=(expresionType.get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidReturnType, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -199,7 +199,7 @@ namespace Porygon::Binder {
auto conditionalStatement = (ParsedConditionalStatement *) statement; auto conditionalStatement = (ParsedConditionalStatement *) statement;
auto boundCondition = this->BindExpression(conditionalStatement->GetCondition()); auto boundCondition = this->BindExpression(conditionalStatement->GetCondition());
if (boundCondition->GetType()->GetClass() != TypeClass::Bool) { if (boundCondition->GetType()->GetClass() != TypeClass::Bool) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ConditionNotABool, statement->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ConditionNotABool, statement->GetStartPosition(),
statement->GetLength()); statement->GetLength());
return new BoundBadStatement(); return new BoundBadStatement();
} }
@ -256,7 +256,7 @@ namespace Porygon::Binder {
auto key = expression->GetValue(); auto key = expression->GetValue();
auto scope = this->_scope->Exists(key.GetHash()); auto scope = this->_scope->Exists(key.GetHash());
if (scope == -1) { if (scope == -1) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::VariableNotFound, expression->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::VariableNotFound, expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
@ -400,7 +400,7 @@ namespace Porygon::Binder {
expression->GetStartPosition(), expression->GetLength()); expression->GetStartPosition(), expression->GetLength());
break; break;
} }
this->_scriptData->Diagnostics->LogError(DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NoBinaryOperationFound, expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
@ -435,7 +435,7 @@ namespace Porygon::Binder {
default: default:
break; break;
} }
this->_scriptData->Diagnostics->LogError(DiagnosticCode::NoUnaryOperationFound, expression->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::NoUnaryOperationFound, expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@ -445,7 +445,7 @@ namespace Porygon::Binder {
auto functionExpression = BindExpression(expression->GetFunction()); auto functionExpression = BindExpression(expression->GetFunction());
auto type = functionExpression->GetType(); auto type = functionExpression->GetType();
if (type->GetClass() != TypeClass::Function) { if (type->GetClass() != TypeClass::Function) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ExpressionIsNotAFunction, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ExpressionIsNotAFunction,
expression->GetStartPosition(), expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@ -454,7 +454,7 @@ namespace Porygon::Binder {
auto parameterTypes = functionType->GetParameterTypes(); auto parameterTypes = functionType->GetParameterTypes();
auto givenParameters = expression->GetParameters(); auto givenParameters = expression->GetParameters();
if (parameterTypes.size() != givenParameters->size()) { if (parameterTypes.size() != givenParameters->size()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterCountMismatch, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ParameterCountMismatch,
expression->GetStartPosition(), expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@ -464,7 +464,7 @@ namespace Porygon::Binder {
auto parameter = givenParameters->at(i); auto parameter = givenParameters->at(i);
auto boundParameter = this->BindExpression(parameter); auto boundParameter = this->BindExpression(parameter);
if (boundParameter->GetType().get()->operator!=(parameterTypes.at(i).get())) { if (boundParameter->GetType().get()->operator!=(parameterTypes.at(i).get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::ParameterTypeMismatch, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::ParameterTypeMismatch,
parameter->GetStartPosition(), parameter->GetStartPosition(),
parameter->GetLength()); parameter->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@ -482,7 +482,7 @@ namespace Porygon::Binder {
auto indexerType = indexer->GetType(); auto indexerType = indexer->GetType();
if (!indexerType->CanBeIndexedWith(index->GetType().get())) { if (!indexerType->CanBeIndexedWith(index->GetType().get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantIndex, index->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantIndex, index->GetStartPosition(),
index->GetLength()); index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
@ -491,14 +491,14 @@ namespace Porygon::Binder {
auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(stringKey->GetHashValue()); auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(stringKey->GetHashValue());
if (!setter) { if (!setter) {
if (!field->HasGetter()) { if (!field->HasGetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoGetter, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter,
index->GetStartPosition(), index->GetStartPosition(),
index->GetLength()); index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
} else { } else {
if (!field->HasSetter()) { if (!field->HasSetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoSetter, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoSetter,
index->GetStartPosition(), index->GetStartPosition(),
index->GetLength()); index->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@ -516,7 +516,7 @@ namespace Porygon::Binder {
const auto &identifier = expression->GetIndex(); const auto &identifier = expression->GetIndex();
const auto &indexerType = indexer->GetType(); const auto &indexerType = indexer->GetType();
if (!indexerType->CanBeIndexedWithIdentifier(identifier.GetHash())) { if (!indexerType->CanBeIndexedWithIdentifier(identifier.GetHash())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::CantIndex, expression->GetStartPosition(), this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantIndex, expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
@ -524,14 +524,14 @@ namespace Porygon::Binder {
auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(identifier.GetHash()); auto field = dynamic_pointer_cast<UserData::UserDataScriptType>(indexerType)->GetField(identifier.GetHash());
if (!setter) { if (!setter) {
if (!field->HasGetter()) { if (!field->HasGetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoGetter, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoGetter,
expression->GetStartPosition(), expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
} }
} else { } else {
if (!field->HasSetter()) { if (!field->HasSetter()) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::UserDataFieldNoSetter, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UserDataFieldNoSetter,
expression->GetStartPosition(), expression->GetStartPosition(),
expression->GetLength()); expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength()); return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
@ -555,7 +555,7 @@ namespace Porygon::Binder {
for (int i = 1; i < expressions->size(); i++) { for (int i = 1; i < expressions->size(); i++) {
boundExpressions[i] = this->BindExpression(expressions->at(i)); boundExpressions[i] = this->BindExpression(expressions->at(i));
if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) { if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) {
this->_scriptData->Diagnostics->LogError(DiagnosticCode::InvalidTableValueType, this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
boundExpressions[i]->GetStartPosition(), boundExpressions[i]->GetStartPosition(),
boundExpressions[i]->GetLength()); boundExpressions[i]->GetLength());
} }

View File

@ -5,33 +5,35 @@
#include "DiagnosticSeverity.hpp" #include "DiagnosticSeverity.hpp"
#include "DiagnosticCode.hpp" #include "DiagnosticCode.hpp"
class Diagnostic{ namespace Porygon::Diagnostics {
class Diagnostic {
DiagnosticSeverity _severity; DiagnosticSeverity _severity;
DiagnosticCode _code; DiagnosticCode _code;
unsigned int _start; unsigned int _start;
unsigned int _length; unsigned int _length;
public: public:
Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length){ Diagnostic(DiagnosticSeverity severity, DiagnosticCode code, unsigned int start, unsigned int length) {
_severity = severity; _severity = severity;
_code = code; _code = code;
_start = start; _start = start;
_length = length; _length = length;
} }
DiagnosticSeverity GetSeverity(){ DiagnosticSeverity GetSeverity() {
return _severity; return _severity;
} }
DiagnosticCode GetCode(){
DiagnosticCode GetCode() {
return _code; return _code;
} }
unsigned int GetStartPosition(){ unsigned int GetStartPosition() {
return _start; return _start;
} }
unsigned int GetLength(){ unsigned int GetLength() {
return _length; return _length;
} }
}; };
}
#endif //PORYGONLANG_DIAGNOSTIC_HPP #endif //PORYGONLANG_DIAGNOSTIC_HPP

View File

@ -2,7 +2,8 @@
#ifndef PORYGONLANG_DIAGNOSTICCODE_HPP #ifndef PORYGONLANG_DIAGNOSTICCODE_HPP
#define PORYGONLANG_DIAGNOSTICCODE_HPP #define PORYGONLANG_DIAGNOSTICCODE_HPP
enum class DiagnosticCode{ namespace Porygon::Diagnostics {
enum class DiagnosticCode {
// Lex diagnostics // Lex diagnostics
UnexpectedCharacter, UnexpectedCharacter,
InvalidStringControlCharacter, InvalidStringControlCharacter,
@ -25,6 +26,6 @@ enum class DiagnosticCode{
InvalidTypeName, InvalidTypeName,
UserDataFieldNoGetter, UserDataFieldNoGetter,
UserDataFieldNoSetter UserDataFieldNoSetter
}; };
}
#endif //PORYGONLANG_DIAGNOSTICCODE_HPP #endif //PORYGONLANG_DIAGNOSTICCODE_HPP

View File

@ -2,10 +2,12 @@
#ifndef PORYGONLANG_DIAGNOSTICSEVERITY_HPP #ifndef PORYGONLANG_DIAGNOSTICSEVERITY_HPP
#define PORYGONLANG_DIAGNOSTICSEVERITY_HPP #define PORYGONLANG_DIAGNOSTICSEVERITY_HPP
enum class DiagnosticSeverity{ namespace Porygon::Diagnostics {
enum class DiagnosticSeverity {
Info, Info,
Warning, Warning,
Error, Error,
}; };
}
#endif //PORYGONLANG_DIAGNOSTICSEVERITY_HPP #endif //PORYGONLANG_DIAGNOSTICSEVERITY_HPP

View File

@ -1,5 +1,5 @@
#include "DiagnosticsHolder.hpp" #include "DiagnosticsHolder.hpp"
using namespace Porygon::Diagnostics;
vector<Diagnostic> DiagnosticsHolder::GetDiagnostics() { vector<Diagnostic> DiagnosticsHolder::GetDiagnostics() {
return _diagnostics; return _diagnostics;
} }

View File

@ -9,21 +9,25 @@
using namespace std; using namespace std;
class DiagnosticsHolder { namespace Porygon::Diagnostics {
class DiagnosticsHolder {
bool _hasErrors; bool _hasErrors;
vector<Diagnostic> _diagnostics; vector<Diagnostic> _diagnostics;
public: public:
DiagnosticsHolder(){ DiagnosticsHolder() {
_hasErrors = false; _hasErrors = false;
} }
~DiagnosticsHolder(){ ~DiagnosticsHolder() {
_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);
void LogError(DiagnosticCode code, unsigned int start, unsigned int length); void LogError(DiagnosticCode code, unsigned int start, unsigned int length);
void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length); void LogWarning(DiagnosticCode code, unsigned int start, unsigned int length);
void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length); void LogInfo(DiagnosticCode code, unsigned int start, unsigned int length);
bool HasErrors(); bool HasErrors();
@ -32,8 +36,8 @@ public:
int DiagnosticsCount(); int DiagnosticsCount();
Diagnostic* GetDiagnosticAt(int position); Diagnostic *GetDiagnosticAt(int position);
}; };
}
#endif //PORYGONLANG_DIAGNOSTICSHOLDER_HPP #endif //PORYGONLANG_DIAGNOSTICSHOLDER_HPP

View File

@ -99,7 +99,7 @@ namespace Porygon::Parser {
Lexer::Next(); Lexer::Next();
return new SimpleToken(TokenKind::InequalityToken, this->_position - 2, 2); return new SimpleToken(TokenKind::InequalityToken, this->_position - 2, 2);
} }
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1); this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1);
return new SimpleToken(TokenKind::BadToken, this->_position - 1, 1); return new SimpleToken(TokenKind::BadToken, this->_position - 1, 1);
case '0': case '0':
case '1': case '1':
@ -122,7 +122,7 @@ namespace Porygon::Parser {
if (isalpha(c) || c > 255) { if (isalpha(c) || c > 255) {
return LexIdentifierOrKeyword(); return LexIdentifierOrKeyword();
} }
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1); this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1);
return new SimpleToken(TokenKind::BadToken, this->_position - 1, 1); return new SimpleToken(TokenKind::BadToken, this->_position - 1, 1);
} }
} }
@ -289,7 +289,7 @@ namespace Porygon::Parser {
} }
auto closeToken = this->Next(); auto closeToken = this->Next();
if (closeToken != c) { if (closeToken != c) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1); this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedCharacter, this->_position - 1, 1);
return new SimpleToken(TokenKind::BadToken, start, end - start + 1); return new SimpleToken(TokenKind::BadToken, start, end - start + 1);
} }
@ -303,7 +303,7 @@ 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 {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::InvalidStringControlCharacter, this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidStringControlCharacter,
start + 1 + i, 1); start + 1 + i, 1);
stream << c; stream << c;
} }

View File

@ -72,12 +72,12 @@ namespace Porygon::Parser {
auto expression = this->ParseExpression(this->Next()); auto expression = this->ParseExpression(this->Next());
if (identifier->GetKind() != TokenKind::Identifier) { if (identifier->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength()); identifier->GetLength());
return new ParsedBadStatement(identifier->GetStartPosition(), identifier->GetLength()); return new ParsedBadStatement(identifier->GetStartPosition(), identifier->GetLength());
} }
if (assignmentToken->GetKind() != TokenKind::AssignmentToken) { if (assignmentToken->GetKind() != TokenKind::AssignmentToken) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength()); identifier->GetLength());
return new ParsedBadStatement(identifier->GetStartPosition(), identifier->GetLength()); return new ParsedBadStatement(identifier->GetStartPosition(), identifier->GetLength());
} }
@ -107,7 +107,7 @@ namespace Porygon::Parser {
break; break;
} }
if (nextKind == TokenKind::EndOfFile) { if (nextKind == TokenKind::EndOfFile) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength()); next->GetLength());
break; break;
} }
@ -125,13 +125,13 @@ namespace Porygon::Parser {
vector<TypedVariableIdentifier *> parameters; vector<TypedVariableIdentifier *> parameters;
bool hasErrors = false; bool hasErrors = false;
if (functionIdentifierToken->GetKind() != TokenKind::Identifier) { if (functionIdentifierToken->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken,
functionIdentifierToken->GetStartPosition(), functionIdentifierToken->GetStartPosition(),
functionIdentifierToken->GetLength()); functionIdentifierToken->GetLength());
hasErrors = true; hasErrors = true;
} }
if (openParenthesis->GetKind() != TokenKind::OpenParenthesis && !hasErrors) { if (openParenthesis->GetKind() != TokenKind::OpenParenthesis && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken,
openParenthesis->GetStartPosition(), openParenthesis->GetLength()); openParenthesis->GetStartPosition(), openParenthesis->GetLength());
hasErrors = true; hasErrors = true;
} }
@ -144,20 +144,20 @@ namespace Porygon::Parser {
auto identifier = this->Next(); auto identifier = this->Next();
auto next = this->Next(); auto next = this->Next();
if (type->GetKind() != TokenKind::Identifier && !hasErrors) { if (type->GetKind() != TokenKind::Identifier && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, type->GetStartPosition(),
type->GetLength()); type->GetLength());
hasErrors = true; hasErrors = true;
continue; continue;
} }
if (identifier->GetKind() != TokenKind::Identifier && !hasErrors) { if (identifier->GetKind() != TokenKind::Identifier && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength()); identifier->GetLength());
hasErrors = true; hasErrors = true;
continue; continue;
} }
if (type->GetKind() != TokenKind::Identifier || identifier->GetKind() != TokenKind::Identifier) { if (type->GetKind() != TokenKind::Identifier || identifier->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, type->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, type->GetStartPosition(),
type->GetLength()); type->GetLength());
hasErrors = true; hasErrors = true;
continue; continue;
@ -170,7 +170,7 @@ namespace Porygon::Parser {
if (nextKind == TokenKind::CloseParenthesis || nextKind == TokenKind::EndOfFile) { if (nextKind == TokenKind::CloseParenthesis || nextKind == TokenKind::EndOfFile) {
break; break;
} else if (nextKind != TokenKind::CommaToken && !hasErrors) { } else if (nextKind != TokenKind::CommaToken && !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength()); next->GetLength());
hasErrors = true; hasErrors = true;
} }
@ -201,7 +201,7 @@ namespace Porygon::Parser {
auto condition = this->ParseExpression(this->Next()); auto condition = this->ParseExpression(this->Next());
auto next = this->Next(); auto next = this->Next();
if (next->GetKind() != TokenKind::ThenKeyword) { if (next->GetKind() != TokenKind::ThenKeyword) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, next->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, next->GetStartPosition(),
next->GetLength()); next->GetLength());
return new ParsedBadStatement(next->GetStartPosition(), next->GetLength()); return new ParsedBadStatement(next->GetStartPosition(), next->GetLength());
} }
@ -385,7 +385,7 @@ namespace Porygon::Parser {
case TokenKind::BadToken: case TokenKind::BadToken:
return new BadExpression(current->GetStartPosition(), current->GetLength()); return new BadExpression(current->GetStartPosition(), current->GetLength());
default: default:
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, current->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, current->GetStartPosition(),
current->GetLength()); current->GetLength());
return new BadExpression(current->GetStartPosition(), current->GetLength()); return new BadExpression(current->GetStartPosition(), current->GetLength());
} }
@ -396,7 +396,7 @@ namespace Porygon::Parser {
auto expression = this->ParseExpression(next); auto expression = this->ParseExpression(next);
auto closeToken = this->Next(); auto closeToken = this->Next();
if (closeToken->GetKind() != TokenKind::CloseParenthesis) { if (closeToken->GetKind() != TokenKind::CloseParenthesis) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeToken->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, closeToken->GetStartPosition(),
closeToken->GetLength()); closeToken->GetLength());
return new BadExpression(closeToken->GetStartPosition(), closeToken->GetLength()); return new BadExpression(closeToken->GetStartPosition(), closeToken->GetLength());
} }
@ -414,7 +414,7 @@ namespace Porygon::Parser {
} else { } else {
while (peekedKind != TokenKind::CloseParenthesis) { while (peekedKind != TokenKind::CloseParenthesis) {
if (peekedKind == TokenKind::EndOfFile) { if (peekedKind == TokenKind::EndOfFile) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(),
peeked->GetLength()); peeked->GetLength());
return new BadExpression(peeked->GetStartPosition(), peeked->GetLength()); return new BadExpression(peeked->GetStartPosition(), peeked->GetLength());
} }
@ -422,7 +422,7 @@ namespace Porygon::Parser {
peeked = this->Next(); peeked = this->Next();
peekedKind = peeked->GetKind(); peekedKind = peeked->GetKind();
if (peekedKind != TokenKind::CloseParenthesis && peekedKind != TokenKind::CommaToken) { if (peekedKind != TokenKind::CloseParenthesis && peekedKind != TokenKind::CommaToken) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, peeked->GetStartPosition(),
peeked->GetLength()); peeked->GetLength());
return new BadExpression(peeked->GetStartPosition(), peeked->GetLength()); return new BadExpression(peeked->GetStartPosition(), peeked->GetLength());
} }
@ -437,7 +437,7 @@ namespace Porygon::Parser {
auto indexExpression = this->ParseExpression(this->Next()); auto indexExpression = this->ParseExpression(this->Next());
auto closeBracket = this->Next(); auto closeBracket = this->Next();
if (closeBracket->GetKind() != TokenKind::CloseSquareBracket) { if (closeBracket->GetKind() != TokenKind::CloseSquareBracket) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, closeBracket->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, closeBracket->GetStartPosition(),
closeBracket->GetLength()); closeBracket->GetLength());
return new BadExpression(closeBracket->GetStartPosition(), closeBracket->GetLength()); return new BadExpression(closeBracket->GetStartPosition(), closeBracket->GetLength());
} }
@ -449,7 +449,7 @@ namespace Porygon::Parser {
this->Next(); // consume '.' token this->Next(); // consume '.' token
auto identifier = this->Next(); auto identifier = this->Next();
if (identifier->GetKind() != TokenKind::Identifier) { if (identifier->GetKind() != TokenKind::Identifier) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, identifier->GetStartPosition(),
identifier->GetLength()); identifier->GetLength());
return new BadExpression(indexingExpression->GetStartPosition(), return new BadExpression(indexingExpression->GetStartPosition(),
identifier->GetEndPosition() - indexingExpression->GetStartPosition()); identifier->GetEndPosition() - indexingExpression->GetStartPosition());
@ -481,7 +481,7 @@ namespace Porygon::Parser {
n = this->Next(); n = this->Next();
if (n->GetKind() != TokenKind::CommaToken && n->GetKind() != TokenKind::CloseCurlyBracket && if (n->GetKind() != TokenKind::CommaToken && n->GetKind() != TokenKind::CloseCurlyBracket &&
!hasErrors) { !hasErrors) {
this->ScriptData->Diagnostics->LogError(DiagnosticCode::UnexpectedToken, n->GetStartPosition(), this->ScriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::UnexpectedToken, n->GetStartPosition(),
n->GetLength()); n->GetLength());
hasErrors = true; hasErrors = true;
} }

View File

@ -25,7 +25,7 @@ Porygon::Script *Porygon::Script::Create(const string &script) {
} }
Porygon::Script::Script() { Porygon::Script::Script() {
Diagnostics = new DiagnosticsHolder(); Diagnostics = new Diagnostics::DiagnosticsHolder();
_boundScript = nullptr; _boundScript = nullptr;
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0); _scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
_evaluator = new Evaluator(this -> _scriptVariables); _evaluator = new Evaluator(this -> _scriptVariables);

View File

@ -27,7 +27,7 @@ namespace Porygon{
public: public:
static Script* Create(const u16string& script); static Script* Create(const u16string& script);
static Script* Create(const string& script); static Script* Create(const string& script);
DiagnosticsHolder* Diagnostics; Diagnostics::DiagnosticsHolder* Diagnostics;
~Script(); ~Script();

View File

@ -9,7 +9,7 @@ TEST_CASE( "Diagnostic invalid character", "[integration]" ) {
REQUIRE(script->Diagnostics -> HasErrors()); REQUIRE(script->Diagnostics -> HasErrors());
auto diags = script->Diagnostics -> GetDiagnostics(); auto diags = script->Diagnostics -> GetDiagnostics();
REQUIRE(diags.size() == 1); REQUIRE(diags.size() == 1);
CHECK(diags[0].GetCode() == DiagnosticCode::UnexpectedCharacter); CHECK(diags[0].GetCode() == Diagnostics::DiagnosticCode::UnexpectedCharacter);
CHECK(diags[0].GetStartPosition() == 6); CHECK(diags[0].GetStartPosition() == 6);
CHECK(diags[0].GetLength() == 1); CHECK(diags[0].GetLength() == 1);
delete script; delete script;
@ -20,7 +20,7 @@ TEST_CASE( "Diagnostic invalid token", "[integration]" ) {
REQUIRE(script->Diagnostics -> HasErrors()); REQUIRE(script->Diagnostics -> HasErrors());
auto diags = script->Diagnostics -> GetDiagnostics(); auto diags = script->Diagnostics -> GetDiagnostics();
REQUIRE(diags.size() == 1); REQUIRE(diags.size() == 1);
CHECK(diags[0].GetCode() == DiagnosticCode::UnexpectedToken); CHECK(diags[0].GetCode() == Diagnostics::DiagnosticCode::UnexpectedToken);
CHECK(diags[0].GetStartPosition() == 3); CHECK(diags[0].GetStartPosition() == 3);
CHECK(diags[0].GetLength() == 1); CHECK(diags[0].GetLength() == 1);
delete script; delete script;