Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 establish a complete exception handling framework #7

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "deps/Catch2"]
path = deps/Catch2
url = https://github.com/catchorg/Catch2.git
[submodule "deps/fmt"]
path = deps/fmt
url = https://github.com/fmtlib/fmt
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ set(CMAKE_CXX_STANDARD_REQUIRED on)

add_subdirectory(src)
add_subdirectory(tests)
add_subdirectory(deps/Catch2)
add_subdirectory(deps/Catch2)
add_subdirectory(deps/fmt)
1 change: 1 addition & 0 deletions deps/fmt
Submodule fmt added at 0379bf
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ file(GLOB sources "*.cpp")
add_library(myxml ${sources})

target_include_directories(myxml PUBLIC ".")

target_link_libraries(myxml fmt::fmt)
45 changes: 45 additions & 0 deletions src/error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "error.hpp"

namespace myxml
{
ParseError::ParseError(std::string message)
: message(message)
{
}

const char *ParseError::what() const noexcept
{
this->fullMessage = this->prefix() + this->message;
return this->fullMessage.c_str();
}

const char *SyntaxError::prefix() const
{
return "Syntax Error: ";
}

SyntaxError::SyntaxError(std::string message)
: ParseError(message)
{
}

const char *SemanticError::prefix() const
{
return "Sematic Error: ";
}

SemanticError::SemanticError(std::string message)
: ParseError(message)
{
}

const char *UnexpectedEndOfInput::prefix() const
{
return "Unexpected End of Input: ";
}

UnexpectedEndOfInput::UnexpectedEndOfInput()
: ParseError("End of input")
{
}
}
60 changes: 60 additions & 0 deletions src/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once
#include <stdexcept>

namespace myxml
{
class ParseError : public std::exception
{
private:
virtual const char *prefix() const = 0;

protected:
std::string message;
// store message after being concated with prefix
mutable std::string fullMessage;

public:
ParseError(std::string message);

virtual const char *what() const noexcept override;
};

/**
* The input data do not conform to the expected grammar rule. Including:
* 1. Missing or mismatch symbols. For example, missing a '>' in the end of a tag.
* 2. Unexpected token. Encounter a token that is not expected in the context. For example: extra semicolon.
* ...
*/
class SyntaxError : public ParseError
{
private:
virtual const char *prefix() const;

public:
SyntaxError(std::string);
};

/**
*
*/
class SemanticError : public ParseError
{
private:
virtual const char *prefix() const;

public:
SemanticError(std::string);
};

/**
* e.g. EOF
*/
class UnexpectedEndOfInput : public ParseError
{
private:
virtual const char *prefix() const;

public:
UnexpectedEndOfInput();
};
}
Loading