c-repl/src/include/errors.h

98 lines
2.0 KiB
C
Raw Normal View History

2023-12-15 14:57:07 +01:00
#ifndef DEF_ERRORS_H
#define DEF_ERRORS_H
2023-11-15 14:37:20 +01:00
#include <string>
using namespace std;
#include "tokenize.h"
2023-12-15 14:52:46 +01:00
/**
* Display the line of code associated with an error, and highlight the associated part
*/
void print_error_position(vector<string> history, CodePosition pos);
2023-12-15 14:57:07 +01:00
/**
* Errors
*/
enum class ErrorType {
// Generic
NotImplemented,
// Lexing
UnknownToken,
IntegerTooLarge,
// Parsing
EmptyInput,
InvalidSyntax,
ExceptedLParen,
ExpectedRParen,
ExpectedRCurlyBracket,
ExpectedSemicolon,
DependentDeclaration,
// Analysis
UnknownType,
TypeNotCastable,
TypesNotComparable,
ExpectedIntegralType,
ExpectedArithmeticType,
// Runtime
DivisionByZero,
ModuloByZero,
UnknownIdentifier,
AlreadyDefinedIdentifier,
UninitializedIdentifier,
BreakNotWithinLoop,
ContinueNotWithinLoop,
};
using ErrorData = variant<monostate, string>;
class UserError : public exception {
public:
explicit UserError(ErrorType type, CodePosition pos, ErrorData data = {})
: pos(pos), type(type), data(data) {}
const char* what() const noexcept override {
return "User error occurred.";
}
const CodePosition pos;
const ErrorType type;
const ErrorData data;
};
class SyntaxError : public UserError {
public:
explicit SyntaxError(ErrorType type, CodePosition pos, ErrorData data = {})
: UserError(type, pos, data) {}
};
class TypeError : public UserError {
public:
explicit TypeError(ErrorType type, CodePosition pos, ErrorData data = {})
: UserError(type, pos, data) {}
};
class RuntimeError : public UserError {
public:
explicit RuntimeError(ErrorType type, CodePosition pos, ErrorData data = {})
: UserError(type, pos, data) {}
};
class InternalError : public exception {
public:
explicit InternalError(CodePosition pos = {})
: pos(pos) {}
const char* what() const noexcept override {
return "Internal error occurred.";
}
const CodePosition pos;
};
#endif