Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
Add BasicWarning and BasicError
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Jul 14, 2023
1 parent 4079506 commit 8a35057
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ add_library(compiler_lib
# === Utils ===
utils/OptionsParser.cpp
# === Message ===
message/Message.cpp message/MessageCollector.cpp)
message/Message.cpp message/MessageCollector.cpp message/Warning.cpp message/Error.cpp)
target_include_directories(compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} utils message ../../lib)
target_link_libraries(compiler_lib PUBLIC coverage_config)

Expand Down
37 changes: 37 additions & 0 deletions src/lib/message/Error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Error.h"

namespace filc::message {
auto Error::print(std::ostream &out) -> std::ostream & {
if (_printed) {
return out;
}

out << "\033[1;31mERROR:\033[0m " << _content;
_printed = true;

return out;
}
}
39 changes: 39 additions & 0 deletions src/lib/message/Error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FILC_ERROR_H
#define FILC_ERROR_H

#include "Message.h"
#include <utility>

namespace filc::message {
class Error : public Message {
public:
Error(uint level, std::string content) : Message(level, std::move(content)) {}

auto print(std::ostream &out) -> std::ostream & override;
};
}

#endif //FILC_ERROR_H
6 changes: 4 additions & 2 deletions src/lib/message/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ namespace filc::message {
public:
Message(uint level, std::string content);

auto print(std::ostream &out) -> std::ostream &;
virtual ~Message() = default;

virtual auto print(std::ostream &out) -> std::ostream &;

auto getLevel() const -> uint;

private:
protected:
uint _level;
std::string _content;
bool _printed;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/message/MessageCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ namespace filc::message {

auto MessageCollector::printMessages() -> MessageCollector & {
std::for_each(_messages.begin(), _messages.end(), [this](Message *message) {
if (message->getLevel() >= _level) {
std::cout << message << std::endl;
if (message->getLevel() <= _level) {
std::cout << *message << std::endl;
}
});

Expand All @@ -71,8 +71,8 @@ namespace filc::message {

auto MessageCollector::printErrors() -> MessageCollector & {
std::for_each(_errors.begin(), _errors.end(), [this](Message *error) {
if (error->getLevel() >= _level) {
std::cerr << error << std::endl;
if (error->getLevel() <= _level) {
std::cerr << *error << std::endl;
}
});

Expand Down
37 changes: 37 additions & 0 deletions src/lib/message/Warning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Warning.h"

namespace filc::message {
auto BasicWarning::print(std::ostream &out) -> std::ostream & {
if (_printed) {
return out;
}

out << "\033[1;33mWARNING:\033[0m " << _content;
_printed = true;

return out;
}
}
39 changes: 39 additions & 0 deletions src/lib/message/Warning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* MIT License
*
* Copyright (c) 2023-Present Kevin Traini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef FILC_WARNING_H
#define FILC_WARNING_H

#include "Message.h"
#include <utility>

namespace filc::message {
class BasicWarning : public Message {
public:
BasicWarning(uint level, std::string content) : Message(level, std::move(content)) {}

auto print(std::ostream &out) -> std::ostream & override;
};
}

#endif //FILC_WARNING_H

0 comments on commit 8a35057

Please sign in to comment.