Skip to content

Commit

Permalink
pattern: Fixed many code inconsistencies and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jan 31, 2022
1 parent 8f8f3c5 commit 61fc479
Show file tree
Hide file tree
Showing 22 changed files with 303 additions and 246 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ add_subdirectory(main)
add_custom_target(imhex ALL DEPENDS main)

# Add unit tests
enable_testing()
add_subdirectory(tests EXCLUDE_FROM_ALL)

# Configure packaging
Expand Down
11 changes: 11 additions & 0 deletions lib/libimhex/include/hex/helpers/concepts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,14 @@ namespace hex {
concept has_size = sizeof(T) == Size;

}


namespace hex {

template<typename T>
class Cloneable {
public:
[[nodiscard]] virtual T* clone() const = 0;
};

}
103 changes: 51 additions & 52 deletions lib/libimhex/include/hex/pattern_language/ast_node.hpp

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions lib/libimhex/include/hex/pattern_language/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <hex.hpp>

#include <stdexcept>
#include <string>

namespace hex::pl {

class PatternLanguageError : public std::exception {
public:
PatternLanguageError(u32 lineNumber, std::string message) : m_lineNumber(lineNumber), m_message(std::move(message)) { }

[[nodiscard]] const char *what() const noexcept override {
return this->m_message.c_str();
}

[[nodiscard]] u32 getLineNumber() const {
return this->m_lineNumber;
}

private:
u32 m_lineNumber;
std::string m_message;
};

}
10 changes: 4 additions & 6 deletions lib/libimhex/include/hex/pattern_language/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ namespace hex::pl {

class Lexer {
public:
using LexerError = std::pair<u32, std::string>;

Lexer() = default;

std::optional<std::vector<Token>> lex(const std::string &code);
const std::optional<LexerError> &getError() { return this->m_error; }
const std::optional<PatternLanguageError> &getError() { return this->m_error; }

private:
std::optional<LexerError> m_error;
std::optional<PatternLanguageError> m_error;

[[noreturn]] void throwLexerError(const std::string &error, u32 lineNumber) const {
throw LexerError(lineNumber, "Lexer: " + error);
[[noreturn]] static void throwLexerError(const std::string &error, u32 lineNumber) {
throw PatternLanguageError(lineNumber, "Lexer: " + error);
}
};

Expand Down
10 changes: 5 additions & 5 deletions lib/libimhex/include/hex/pattern_language/log_console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <utility>
#include <vector>

#include <hex/pattern_language/error.hpp>

namespace hex::pl {

class ASTNode;
Expand All @@ -23,8 +25,6 @@ namespace hex::pl {

[[nodiscard]] const auto &getLog() const { return this->m_consoleLog; }

using EvaluateError = std::pair<u32, std::string>;

void log(Level level, const std::string &message);

[[noreturn]] static void abortEvaluation(const std::string &message);
Expand All @@ -33,13 +33,13 @@ namespace hex::pl {

void clear();

void setHardError(const EvaluateError &error) { this->m_lastHardError = error; }
void setHardError(const PatternLanguageError &error) { this->m_lastHardError = error; }

[[nodiscard]] const std::optional<EvaluateError> &getLastHardError() { return this->m_lastHardError; };
[[nodiscard]] const std::optional<PatternLanguageError> &getLastHardError() { return this->m_lastHardError; };

private:
std::vector<std::pair<Level, std::string>> m_consoleLog;
std::optional<EvaluateError> m_lastHardError;
std::optional<PatternLanguageError> m_lastHardError;
};

}
12 changes: 6 additions & 6 deletions lib/libimhex/include/hex/pattern_language/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <hex.hpp>
#include <hex/helpers/utils.hpp>

#include <hex/pattern_language/error.hpp>
#include <hex/pattern_language/token.hpp>
#include <hex/pattern_language/ast_node.hpp>

Expand All @@ -16,16 +17,15 @@ namespace hex::pl {
class Parser {
public:
using TokenIter = std::vector<Token>::const_iterator;
using ParseError = std::pair<u32, std::string>;

Parser() = default;
~Parser() = default;

std::optional<std::vector<ASTNode *>> parse(const std::vector<Token> &tokens);
const std::optional<ParseError> &getError() { return this->m_error; }
const std::optional<PatternLanguageError> &getError() { return this->m_error; }

private:
std::optional<ParseError> m_error;
std::optional<PatternLanguageError> m_error;
TokenIter m_curr;
TokenIter m_originalPosition, m_partOriginalPosition;

Expand All @@ -47,7 +47,7 @@ namespace hex::pl {
auto value = std::get_if<T>(&this->m_curr[index].value);

if (value == nullptr)
throwParseError("failed to decode token. Invalid type.", getLineNumber(index));
throwParserError("failed to decode token. Invalid type.", getLineNumber(index));

return *value;
}
Expand Down Expand Up @@ -143,8 +143,8 @@ namespace hex::pl {
return program;
}

[[noreturn]] void throwParseError(const std::string &error, i32 token = -1) const {
throw ParseError(this->m_curr[token].lineNumber, "Parser: " + error);
[[noreturn]] void throwParserError(const std::string &error, i32 token = -1) const {
throw PatternLanguageError(this->m_curr[token].lineNumber, "Parser: " + error);
}

/* Token consuming */
Expand Down
Loading

0 comments on commit 61fc479

Please sign in to comment.