Skip to content

Commit

Permalink
Merge pull request #7 from Adamska1008/6-establish-a-complete-excepti…
Browse files Browse the repository at this point in the history
…on-handling-framework

6 establish a complete exception handling framework
  • Loading branch information
Adamska1008 authored Aug 28, 2024
2 parents 66729af + c8b5d67 commit abefee8
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 130 deletions.
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

0 comments on commit abefee8

Please sign in to comment.