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

Commit

Permalink
Add DevWarning
Browse files Browse the repository at this point in the history
Use it for CharacterLiteral
  • Loading branch information
Gashmob committed Jul 26, 2023
1 parent 1c38241 commit ec8bb2f
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 8 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,17 @@ cd - && ./bin/coverage.sh
If during compilation you got a warning with a dev code, please open an issue on GitHub with the warning message and
your code.

| Dev code | Meaning | File |
|:------------:|------------------------------------------------------------------|:-------------------------:|
| <kbd>2</kbd> | A module was not imported during part A, and no error was raised | `src/fil/ast/Program.cpp` |
Dev warning are show like this :

```
DEV WARNING <code> <message>
<filename>
<line> |<code>
| ^
```

| Dev code | Meaning | File |
|:------------:|:--------------------------------------------|:-----------------------------------|
| <kbd>2</kbd> | Lexer found a character that is not regular | `src/lib/ast/CharacterLiteral.cpp` |

These codes are for events that should not happen, but if they do, it's better to fix them.
2 changes: 1 addition & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ add_library(compiler_lib
# === ANTLR ===
${ANTLR_Lexer_CXX_OUTPUTS} ${ANTLR_Parser_CXX_OUTPUTS} grammar/Parser.cpp
# === Message ===
message/Message.cpp message/MessageCollector.cpp message/Warning.cpp message/Error.cpp
message/Message.cpp message/MessageCollector.cpp message/Warning.cpp message/Error.cpp message/DevWarning.cpp
# === AST ===
ast/Program.cpp ast/AbstractExpression.cpp ast/BooleanLiteral.cpp ast/IntegerLiteral.cpp ast/FloatLiteral.cpp ast/CharacterLiteral.cpp)
target_include_directories(compiler_lib PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace filc::ast {
public:
explicit CharacterLiteral(char value);

static auto stringToChar(const std::string &snippet) -> char;
static auto stringToChar(const std::string &snippet, antlr4::Token *token = nullptr) -> char;
};
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib/ast/CharacterLiteral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
* SOFTWARE.
*/
#include "AST.h"
#include "MessageCollector.h"
#include "DevWarning.h"

namespace filc::ast {
CharacterLiteral::CharacterLiteral(char value)
: AbstractLiteral<char>(value) {}

auto CharacterLiteral::stringToChar(const std::string &snippet) -> char {
auto CharacterLiteral::stringToChar(const std::string &snippet, antlr4::Token *token) -> char {
auto value = snippet.substr(1, snippet.size() - 2);

if (value.size() == 1) {
Expand Down Expand Up @@ -64,7 +66,13 @@ namespace filc::ast {
}

// There is a problem with the lexer
// TODO : add dev warning
if (token != nullptr) {
filc::message::MessageCollector::getCollector()->addError(new filc::message::DevWarning(
2,
new filc::utils::Position(token),
"Lexer found a character that is not regular: " + snippet
));
}

return '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/grammar/FilParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ literal returns[filc::ast::AbstractExpression *tree]
}
| c=CHARACTER {
$tree = new filc::ast::CharacterLiteral(
filc::ast::CharacterLiteral::stringToChar($c.text)
filc::ast::CharacterLiteral::stringToChar($c.text, $c)
);
$tree->setPosition(new filc::utils::Position($c));
}
Expand Down
43 changes: 43 additions & 0 deletions src/lib/message/DevWarning.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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 "DevWarning.h"
#include <utility>

namespace filc::message {
DevWarning::DevWarning(unsigned int code, filc::utils::Position *position, std::string content)
: Message(ERROR, std::move(content)), _code(code), _position(position) {}

auto DevWarning::print(std::ostream &out) -> std::ostream & {
if (_printed) {
return out;
}

out << "\033[1;36mDEV WARNING:\033[0m " << "\033[1;46m " << _code << " \033[0m " << _content << std::endl;
out << _position->dump("\033[1;36m");

_printed = true;

return out;
}
}
43 changes: 43 additions & 0 deletions src/lib/message/DevWarning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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_DEVWARNING_H
#define FILC_DEVWARNING_H

#include "Message.h"
#include "Position.h"

namespace filc::message {
class DevWarning final : public Message {
public:
DevWarning(unsigned int code, filc::utils::Position *position, std::string content);

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

private:
unsigned int _code;
filc::utils::Position *_position;
};
}

#endif //FILC_DEVWARNING_H
12 changes: 12 additions & 0 deletions src/lib/utils/Position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ namespace filc::utils {

return line;
}

auto Position::dump(const std::string &color) const -> std::string {
std::string nth = " " + std::to_string(_line) + " ";
std::string res = std::string(nth.length() + 1, ' ') + _filename + "\n";

res += nth + "|" + getContent() + "\n";
res += std::string(nth.length(), ' ') + "|";
std::string spaces = _column > 0 ? std::string(_column - 1, ' ') : "";
res += spaces + color + "^" + "\033[0m" + "\n";

return res;
}
}
2 changes: 2 additions & 0 deletions src/lib/utils/Position.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace filc::utils {

auto getContent() const -> std::string;

auto dump(const std::string &color) const -> std::string;

private:
std::string _filename;
unsigned int _line;
Expand Down

0 comments on commit ec8bb2f

Please sign in to comment.