diff --git a/components/core/src/clp/regex_utils/CMakeLists.txt b/components/core/src/clp/regex_utils/CMakeLists.txt index c5d54dde0..5daee5336 100644 --- a/components/core/src/clp/regex_utils/CMakeLists.txt +++ b/components/core/src/clp/regex_utils/CMakeLists.txt @@ -3,11 +3,13 @@ set( "constants.hpp" "ErrorCode.hpp" "regex_translation_utils.hpp" + "RegexErrorCode.hpp" "RegexToWildcardTranslatorConfig.hpp" ) add_library( regex_utils ErrorCode.cpp + RegexErrorCode.cpp regex_translation_utils.cpp ${REGEX_UTILS_HEADER_LIST} ) diff --git a/components/core/src/clp/regex_utils/RegexErrorCode.cpp b/components/core/src/clp/regex_utils/RegexErrorCode.cpp new file mode 100644 index 000000000..c5d5919bb --- /dev/null +++ b/components/core/src/clp/regex_utils/RegexErrorCode.cpp @@ -0,0 +1,38 @@ +#include "RegexErrorCode.hpp" + +#include + +//#include "error_handling/ErrorCode.hpp" +#include "GenericErrorCode.hpp" + +template <> +auto clp::regex_utils::RegexErrorCategory::name() const noexcept -> char const* { + return "regex utility"; +} + +template <> +auto clp::regex_utils::RegexErrorCategory::message(int ev) const -> std::string { + switch (static_cast(ev)) { + case clp::regex_utils::RegexErrorEnum::Success: + return "Success."; + + case clp::regex_utils::RegexErrorEnum::IllegalState: + return "Unrecognized state."; + + case clp::regex_utils::RegexErrorEnum::Star: + return "Failed to translate due to metachar `*` (zero or more occurences)."; + + case clp::regex_utils::RegexErrorEnum::Plus: + return "Failed to translate due to metachar `+` (one or more occurences)."; + + case clp::regex_utils::RegexErrorEnum::Question: + return "Currently does not support returning a list of wildcard translations. The " + "metachar `?` (lazy match) may be supported in the future."; + + default: + return "(unrecognized error)"; + } +} + +template class clp::ErrorCategory; +template class clp::ErrorCode; diff --git a/components/core/src/clp/regex_utils/RegexErrorCode.hpp b/components/core/src/clp/regex_utils/RegexErrorCode.hpp new file mode 100644 index 000000000..a731b6612 --- /dev/null +++ b/components/core/src/clp/regex_utils/RegexErrorCode.hpp @@ -0,0 +1,40 @@ +#ifndef CLP_REGEXERRORCODE_HPP +#define CLP_REGEXERRORCODE_HPP + +#include +#include +#include + +//#include "error_handling/ErrorCode.hpp" +#include "GenericErrorCode.hpp" + +namespace clp::regex_utils { +enum class RegexErrorEnum : uint8_t { + Success = 0, + IllegalState, + Star, + Plus, + Question, + Pipe, + Caret, + Dollar, + DisallowedEscapeSequence, + UnmatchedParenthesis, + UnsupportedCharsets, + IncompleteCharsetStructure, + UnsupportedQuantifier, + TokenUnquantifiable, +}; + +//using RegexErrorCategory = clp::error_handling::ErrorCategory; +//using RegexErrorCode = clp::error_handling::ErrorCode; +using RegexErrorCategory = clp::ErrorCategory; +using RegexErrorCode = clp::ErrorCode; +} // namespace clp::regex_utils + +namespace std { +template <> +struct is_error_code_enum : std::true_type {}; +} // namespace std + +#endif // CLP_REGEXERRORCODE_HPP diff --git a/components/core/src/clp/regex_utils/regex_translation_utils.cpp b/components/core/src/clp/regex_utils/regex_translation_utils.cpp index 2ae2c4429..563e6d2ed 100644 --- a/components/core/src/clp/regex_utils/regex_translation_utils.cpp +++ b/components/core/src/clp/regex_utils/regex_translation_utils.cpp @@ -11,6 +11,7 @@ #include "regex_utils/constants.hpp" #include "regex_utils/ErrorCode.hpp" +#include "regex_utils/RegexErrorCode.hpp" #include "regex_utils/RegexToWildcardTranslatorConfig.hpp" namespace clp::regex_utils { @@ -204,7 +205,8 @@ auto normal_state_transition( state.set_next_state(TranslatorState::RegexPatternState::End); break; case cRegexZeroOrMore: - return ErrorCode::UntranslatableStar; + return RegexErrorCode{RegexErrorEnum::Star}; + //return ErrorCode::UntranslatableStar; case cRegexOneOrMore: return ErrorCode::UntranslatablePlus; case cRegexZeroOrOne: diff --git a/components/core/tests/test-regex_utils.cpp b/components/core/tests/test-regex_utils.cpp index 64af60318..0340b1ed3 100644 --- a/components/core/tests/test-regex_utils.cpp +++ b/components/core/tests/test-regex_utils.cpp @@ -1,9 +1,12 @@ #include #include +#include #include #include using clp::regex_utils::ErrorCode; +using clp::regex_utils::RegexErrorCode; +using clp::regex_utils::RegexErrorEnum; using clp::regex_utils::regex_to_wildcard; using clp::regex_utils::RegexToWildcardTranslatorConfig; @@ -17,7 +20,7 @@ TEST_CASE("regex_to_wildcard_simple_translations", "[regex_utils][re2wc][simple_ TEST_CASE("regex_to_wildcard_unescaped_metachar", "[regex_utils][re2wc][unescaped_metachar]") { REQUIRE((regex_to_wildcard(".? xyz .* zyx .").error() == ErrorCode::UnsupportedQuestionMark)); - REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == ErrorCode::UntranslatableStar)); + REQUIRE((regex_to_wildcard(". xyz .** zyx .").error() == RegexErrorCode{RegexErrorEnum::Star})); REQUIRE((regex_to_wildcard(". xyz .*+ zyx .").error() == ErrorCode::UntranslatablePlus)); REQUIRE((regex_to_wildcard(". xyz |.* zyx .").error() == ErrorCode::UnsupportedPipe)); REQUIRE((regex_to_wildcard(". xyz ^.* zyx .").error() == ErrorCode::IllegalCaret));