-
Notifications
You must be signed in to change notification settings - Fork 83
feat(ffi): Add new EncodedTextAst implementation using StringBlob.
#1561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LinZhihao-723
wants to merge
11
commits into
y-scope:main
Choose a base branch
from
LinZhihao-723:new-encoded-text-ast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
54c8e14
...
LinZhihao-723 8269abc
Implementation done.
LinZhihao-723 099441b
Revert...
LinZhihao-723 3380975
Done.
LinZhihao-723 332f586
Fix clang-format issues.
LinZhihao-723 cd6d6f7
Fix clang-tidy warnings.
LinZhihao-723 d0910a4
Apply suggestions from code review
LinZhihao-723 7c8c3f0
Apply code review comments
LinZhihao-723 dbfcd7d
Merge branch 'new-encoded-text-ast' of https://github.com/LinZhihao-7…
LinZhihao-723 e59609d
Add method to directly append a string to the string blob.
LinZhihao-723 bd72701
Fix docstring.
LinZhihao-723 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,264 @@ | ||
| #ifndef CLP_FFI_ENCODEDTEXTAST_HPP | ||
| #define CLP_FFI_ENCODEDTEXTAST_HPP | ||
|
|
||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <ystdlib/error_handling/Result.hpp> | ||
|
|
||
| #include "../ir/types.hpp" | ||
| #include "../type_utils.hpp" | ||
| #include "EncodedTextAstError.hpp" | ||
| #include "encoding_methods.hpp" | ||
| #include "StringBlob.hpp" | ||
| #include "type_utils.hpp" | ||
|
|
||
| namespace clp::ffi { | ||
| /** | ||
| * Method signature requirements for handling constant text segments in an encoded text AST. | ||
| * @tparam EncodedTextAstConstantHandlerType | ||
| */ | ||
| template <typename EncodedTextAstConstantHandlerType> | ||
| concept EncodedTextAstConstantHandlerReq | ||
| = requires(EncodedTextAstConstantHandlerType handler, std::string_view constant) { | ||
| { handler(constant) } -> std::same_as<void>; | ||
| }; | ||
|
|
||
| /** | ||
| * Method signature requirements for handling int variables in an encoded text AST. | ||
| * @tparam EncodedTextAstIntVarHandlerType | ||
| * @tparam encoded_variable_t | ||
| */ | ||
| template <typename EncodedTextAstIntVarHandlerType, typename encoded_variable_t> | ||
| concept EncodedTextAstIntVarHandlerReq | ||
| = requires(EncodedTextAstIntVarHandlerType handler, encoded_variable_t var) { | ||
| { handler(var) } -> std::same_as<void>; | ||
| }; | ||
|
|
||
| /** | ||
| * Method signature requirements for handling float variables in an encoded text AST. | ||
| * @tparam EncodedTextAstFloatVarHandlerType | ||
| * @tparam encoded_variable_t | ||
| */ | ||
| template <typename EncodedTextAstFloatVarHandlerType, typename encoded_variable_t> | ||
| concept EncodedTextAstFloatVarHandlerReq | ||
| = requires(EncodedTextAstFloatVarHandlerType handler, encoded_variable_t var) { | ||
| { handler(var) } -> std::same_as<void>; | ||
| }; | ||
|
|
||
| /** | ||
| * Method signature requirements for handling dictionary variables in an encoded text AST. | ||
| * @tparam EncodedTextAstDictVarHandlerType | ||
| */ | ||
| template <typename EncodedTextAstDictVarHandlerType> | ||
| concept EncodedTextAstDictVarHandlerReq | ||
| = requires(EncodedTextAstDictVarHandlerType handler, std::string_view var) { | ||
| { handler(var) } -> std::same_as<void>; | ||
| }; | ||
|
|
||
| /** | ||
| * A parsed and encoded unstructured text string. | ||
| * @tparam encoded_variable_t The type of encoded variables in the string. | ||
| */ | ||
| template <ir::EncodedVariableTypeReq encoded_variable_t> | ||
| class EncodedTextAst { | ||
| public: | ||
| // Factory function | ||
| /** | ||
| * @param encoded_vars | ||
| * @param string_blob A string blob containing a list of dictionary variables followed by a logtype. | ||
| * @return A result containing the newly created `EncodedTextAst` instance on success, or an | ||
| * error code indicating the failure: | ||
| * - EncodedTextAstErrEnum::MissingLogtype: if `string_blob` contains no strings. | ||
| */ | ||
| [[nodiscard]] static auto | ||
| create(std::vector<encoded_variable_t> encoded_vars, StringBlob string_blob) | ||
| -> ystdlib::error_handling::Result<EncodedTextAst> { | ||
| if (string_blob.get_num_strings() < 1) { | ||
| return EncodedTextAstError{EncodedTextAstErrorEnum::MissingLogtype}; | ||
| } | ||
| return EncodedTextAst{std::move(encoded_vars), std::move(string_blob)}; | ||
| } | ||
|
|
||
| // Default copy & move constructors and assignment operators | ||
| EncodedTextAst(EncodedTextAst const&) = default; | ||
| EncodedTextAst(EncodedTextAst&&) noexcept = default; | ||
| auto operator=(EncodedTextAst const&) -> EncodedTextAst& = default; | ||
| auto operator=(EncodedTextAst&&) noexcept -> EncodedTextAst& = default; | ||
|
|
||
| // Destructor | ||
| ~EncodedTextAst() = default; | ||
|
|
||
| // Methods | ||
| [[nodiscard]] auto get_logtype() const -> std::string_view { | ||
| return m_string_blob.get_string(m_num_dict_vars).value(); | ||
| } | ||
|
|
||
| /** | ||
| * Decodes the encoded text AST into its string form by calling the given handlers for each | ||
| * component of the message. | ||
| * @tparam unescape_logtype Whether to remove the escape characters from the logtype before | ||
| * calling `constant_handler`. | ||
| * @param constant_handler | ||
| * @param int_var_handler | ||
| * @param float_var_handler | ||
| * @param dict_var_handler | ||
| * @return A void result on success, or an error code indicating the failure: | ||
| * - EncodedTextAstErrEnum::MissingEncodedVar if an encoded variable is missing. | ||
| * - EncodedTextAstErrEnum::MissingDictVar if a dictionary variable is missing. | ||
| * - EncodedTextAstErrEnum::UnexpectedTrailingEscapeCharacter if the logtype ends with an | ||
| * unexpected escape character. | ||
| */ | ||
| template <bool unescape_logtype> | ||
| [[nodiscard]] auto decode( | ||
| EncodedTextAstConstantHandlerReq auto constant_handler, | ||
| EncodedTextAstIntVarHandlerReq<encoded_variable_t> auto int_var_handler, | ||
| EncodedTextAstFloatVarHandlerReq<encoded_variable_t> auto float_var_handler, | ||
| EncodedTextAstDictVarHandlerReq auto dict_var_handler | ||
| ) const -> ystdlib::error_handling::Result<void>; | ||
|
|
||
| /** | ||
| * Decodes and un-parses the encoded text AST into its string form. | ||
| * @return A result containing the decoded string on success, or an error code indicating the | ||
| * failure: | ||
| * - Forwards `decode`'s return values on failure. | ||
| */ | ||
| [[nodiscard]] auto to_string() const -> ystdlib::error_handling::Result<std::string> { | ||
| std::string decoded_string; | ||
| YSTDLIB_ERROR_HANDLING_TRYV( | ||
| decode<true>( | ||
| [&](std::string_view constant) { decoded_string.append(constant); }, | ||
| [&](encoded_variable_t int_var) { | ||
| decoded_string.append(decode_integer_var(int_var)); | ||
| }, | ||
| [&](encoded_variable_t float_var) { | ||
| decoded_string.append(decode_float_var(float_var)); | ||
| }, | ||
| [&](std::string_view dict_var) { decoded_string.append(dict_var); } | ||
| ) | ||
| ); | ||
| return decoded_string; | ||
| } | ||
|
|
||
| private: | ||
| // Constructor | ||
| EncodedTextAst(std::vector<encoded_variable_t> encoded_vars, StringBlob string_blob) | ||
| : m_encoded_vars{std::move(encoded_vars)}, | ||
| m_string_blob{std::move(string_blob)}, | ||
| m_num_dict_vars{m_string_blob.get_num_strings() - 1} {} | ||
|
|
||
| // Variables | ||
| std::vector<encoded_variable_t> m_encoded_vars; | ||
| StringBlob m_string_blob; | ||
| size_t m_num_dict_vars; | ||
| }; | ||
|
|
||
| template <ir::EncodedVariableTypeReq encoded_variable_t> | ||
| template <bool unescape_logtype> | ||
| [[nodiscard]] auto EncodedTextAst<encoded_variable_t>::decode( | ||
| EncodedTextAstConstantHandlerReq auto constant_handler, | ||
| EncodedTextAstIntVarHandlerReq<encoded_variable_t> auto int_var_handler, | ||
| EncodedTextAstFloatVarHandlerReq<encoded_variable_t> auto float_var_handler, | ||
| EncodedTextAstDictVarHandlerReq auto dict_var_handler | ||
| ) const -> ystdlib::error_handling::Result<void> { | ||
| auto const logtype{get_logtype()}; | ||
| auto const logtype_length = logtype.length(); | ||
| auto const num_encoded_vars{m_encoded_vars.size()}; | ||
|
|
||
| size_t next_static_text_begin_pos{0}; | ||
| size_t dictionary_vars_idx{0}; | ||
| size_t encoded_vars_idx{0}; | ||
|
|
||
| for (size_t curr_pos{0}; curr_pos < logtype_length; ++curr_pos) { | ||
| auto const c{logtype.at(curr_pos)}; | ||
| switch (c) { | ||
| case enum_to_underlying_type(ir::VariablePlaceholder::Float): { | ||
| constant_handler(logtype.substr( | ||
| next_static_text_begin_pos, | ||
| curr_pos - next_static_text_begin_pos | ||
| )); | ||
| next_static_text_begin_pos = curr_pos + 1; | ||
| if (encoded_vars_idx >= num_encoded_vars) { | ||
| return EncodedTextAstError{EncodedTextAstErrorEnum::MissingEncodedVar}; | ||
| } | ||
| float_var_handler(m_encoded_vars.at(encoded_vars_idx)); | ||
| ++encoded_vars_idx; | ||
| break; | ||
| } | ||
|
|
||
| case enum_to_underlying_type(ir::VariablePlaceholder::Integer): { | ||
| constant_handler(logtype.substr( | ||
| next_static_text_begin_pos, | ||
| curr_pos - next_static_text_begin_pos | ||
| )); | ||
| next_static_text_begin_pos = curr_pos + 1; | ||
| if (encoded_vars_idx >= num_encoded_vars) { | ||
| return EncodedTextAstError{EncodedTextAstErrorEnum::MissingEncodedVar}; | ||
| } | ||
| int_var_handler(m_encoded_vars.at(encoded_vars_idx)); | ||
| ++encoded_vars_idx; | ||
| break; | ||
| } | ||
|
|
||
| case enum_to_underlying_type(ir::VariablePlaceholder::Dictionary): { | ||
| constant_handler(logtype.substr( | ||
| next_static_text_begin_pos, | ||
| curr_pos - next_static_text_begin_pos | ||
| )); | ||
| next_static_text_begin_pos = curr_pos + 1; | ||
| if (dictionary_vars_idx >= m_num_dict_vars) { | ||
| return EncodedTextAstError{EncodedTextAstErrorEnum::MissingDictVar}; | ||
| } | ||
| dict_var_handler(m_string_blob.get_string(dictionary_vars_idx).value()); | ||
| ++dictionary_vars_idx; | ||
| break; | ||
| } | ||
|
|
||
| case enum_to_underlying_type(ir::VariablePlaceholder::Escape): { | ||
| // Ensure the escape character is followed by a character that's being escaped | ||
| if (curr_pos == logtype_length - 1) { | ||
| return EncodedTextAstError{ | ||
| EncodedTextAstErrorEnum::UnexpectedTrailingEscapeCharacter | ||
| }; | ||
| } | ||
|
|
||
| if constexpr (unescape_logtype) { | ||
| constant_handler(logtype.substr( | ||
| next_static_text_begin_pos, | ||
| curr_pos - next_static_text_begin_pos | ||
| )); | ||
| // Skip the escape character | ||
| next_static_text_begin_pos = curr_pos + 1; | ||
| } | ||
|
|
||
| // The character after the escape character is static text (regardless of whether it | ||
| // is a variable placeholder), so increment curr_pos by 1 to ensure we don't process | ||
| // the next character in any of the other cases (instead it will be added to the | ||
| // message). | ||
| ++curr_pos; | ||
| break; | ||
| } | ||
|
|
||
| default: | ||
| // Regular characters. Do nothing. | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Add remainder | ||
| if (next_static_text_begin_pos < logtype_length) { | ||
| constant_handler(logtype.substr( | ||
| next_static_text_begin_pos, | ||
| logtype_length - next_static_text_begin_pos | ||
| )); | ||
| } | ||
|
|
||
| return ystdlib::error_handling::success(); | ||
| } | ||
| } // namespace clp::ffi | ||
|
|
||
| #endif // CLP_FFI_ENCODEDTEXTAST_HPP | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include "EncodedTextAstError.hpp" | ||
|
|
||
| #include <string> | ||
|
|
||
| #include <ystdlib/error_handling/ErrorCode.hpp> | ||
|
|
||
| using clp::ffi::EncodedTextAstErrorEnum; | ||
| using EncodedTextAstErrorCategory = ystdlib::error_handling::ErrorCategory<EncodedTextAstErrorEnum>; | ||
|
|
||
| template <> | ||
| auto EncodedTextAstErrorCategory::name() const noexcept -> char const* { | ||
| return "clp::ffi::EncodedTextAstErrorCode"; | ||
| } | ||
|
|
||
| template <> | ||
| auto EncodedTextAstErrorCategory::message(EncodedTextAstErrorEnum error_enum) const -> std::string { | ||
| switch (error_enum) { | ||
| case EncodedTextAstErrorEnum::MissingEncodedVar: | ||
| return "An encoded variable is missing from the `EncodedTextAst`"; | ||
| case EncodedTextAstErrorEnum::MissingDictVar: | ||
| return "A dictionary variable is missing from the `EncodedTextAst`"; | ||
| case EncodedTextAstErrorEnum::MissingLogtype: | ||
| return "The logtype is missing from the `EncodedTextAst`"; | ||
| case EncodedTextAstErrorEnum::UnexpectedTrailingEscapeCharacter: | ||
| return "Unexpected escape character without escaped value at the end of the logtype"; | ||
| default: | ||
| return "Unknown error code enum"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #ifndef CLP_FFI_ENCODEDTEXTASTERROR_HPP | ||
| #define CLP_FFI_ENCODEDTEXTASTERROR_HPP | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| #include <ystdlib/error_handling/ErrorCode.hpp> | ||
|
|
||
| namespace clp::ffi { | ||
| /** | ||
| * Error enums for `EncodedTextAst`. | ||
| */ | ||
| enum class EncodedTextAstErrorEnum : uint8_t { | ||
| MissingDictVar = 1, | ||
| MissingEncodedVar, | ||
| MissingLogtype, | ||
| UnexpectedTrailingEscapeCharacter, | ||
| }; | ||
|
|
||
| using EncodedTextAstError = ystdlib::error_handling::ErrorCode<EncodedTextAstErrorEnum>; | ||
| } // namespace clp::ffi | ||
|
|
||
| YSTDLIB_ERROR_HANDLING_MARK_AS_ERROR_CODE_ENUM(clp::ffi::EncodedTextAstErrorEnum); | ||
|
|
||
| #endif // CLP_FFI_ENCODEDTEXTASTERROR_HPP |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.