c-repl/src/include/errors.h

100 lines
2.1 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: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
UnknownIdentifier,
2023-12-15 15:36:09 +01:00
AlreadyDeclaredIdentifier,
2023-12-15 14:57:07 +01:00
UninitializedIdentifier,
2023-12-15 15:36:09 +01:00
DivisionByZero,
ModuloByZero,
2023-12-15 14:57:07 +01:00
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) {}
2023-12-15 15:36:09 +01:00
const char* what() const noexcept override;
2023-12-15 14:57:07 +01:00
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;
};
2023-12-15 15:36:09 +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);
/**
* Returns the error message associated with a user error
*/
string get_error_message(const UserError& e);
2023-12-15 14:57:07 +01:00
#endif