From b6e2bbc4348f3fedc111546d8eaa30939c1014c9 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 21 Jan 2021 20:55:10 +0100 Subject: [PATCH] Added comments attribute --- .../libimhex/include/hex/lang/ast_node.hpp | 5 +-- plugins/libimhex/include/hex/lang/parser.hpp | 9 ++++++ .../include/hex/lang/pattern_data.hpp | 31 +++++++++++++++---- plugins/libimhex/source/lang/evaluator.cpp | 13 +++++--- plugins/libimhex/source/lang/parser.cpp | 2 +- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/plugins/libimhex/include/hex/lang/ast_node.hpp b/plugins/libimhex/include/hex/lang/ast_node.hpp index 3a3356187..f988bf24d 100644 --- a/plugins/libimhex/include/hex/lang/ast_node.hpp +++ b/plugins/libimhex/include/hex/lang/ast_node.hpp @@ -541,12 +541,13 @@ namespace hex::lang { return this->m_attribute; } - [[nodiscard]] std::string_view getValue() const { + [[nodiscard]] const std::optional& getValue() const { return this->m_value; } private: - std::string m_attribute, m_value; + std::string m_attribute; + std::optional m_value; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/lang/parser.hpp b/plugins/libimhex/include/hex/lang/parser.hpp index ecf9979b4..d1c2e4046 100644 --- a/plugins/libimhex/include/hex/lang/parser.hpp +++ b/plugins/libimhex/include/hex/lang/parser.hpp @@ -117,6 +117,15 @@ namespace hex::lang { return true; } + bool none(bool result) { + if (result) { + this->m_curr = this->m_originalPosition; + return false; + } + + return true; + } + bool sequence() { return true; } diff --git a/plugins/libimhex/include/hex/lang/pattern_data.hpp b/plugins/libimhex/include/hex/lang/pattern_data.hpp index f9407b4d3..de59262bd 100644 --- a/plugins/libimhex/include/hex/lang/pattern_data.hpp +++ b/plugins/libimhex/include/hex/lang/pattern_data.hpp @@ -60,6 +60,9 @@ namespace hex::lang { [[nodiscard]] const std::string& getVariableName() const { return this->m_variableName; } void setVariableName(std::string name) { this->m_variableName = std::move(name); } + [[nodiscard]] const std::optional& getComment() const { return this->m_comment; } + void setComment(std::string comment) { this->m_comment = std::move(comment); } + [[nodiscard]] const std::string& getTypeName() const { return this->m_typeName; } void setTypeName(std::string name) { this->m_typeName = std::move(name); } @@ -154,6 +157,7 @@ namespace hex::lang { Region selectRegion = { this->getOffset(), this->getSize() }; View::postEvent(Events::SelectionChangeRequest, selectRegion); } + this->drawCommentTooltip(); ImGui::SameLine(); ImGui::Text("%s", this->getVariableName().c_str()); ImGui::TableNextColumn(); @@ -168,6 +172,14 @@ namespace hex::lang { ImGui::Text("%s", value.data()); } + void drawCommentTooltip() const { + if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && this->getComment().has_value()) { + ImGui::BeginTooltip(); + ImGui::TextUnformatted(this->getComment()->c_str()); + ImGui::EndTooltip(); + } + } + protected: std::endian m_endian = std::endian::native; std::map m_highlightedAddresses; @@ -178,6 +190,7 @@ namespace hex::lang { u32 m_color; std::string m_variableName; + std::optional m_comment; std::string m_typeName; }; @@ -215,7 +228,8 @@ namespace hex::lang { ImGui::TableNextRow(); ImGui::TableNextColumn(); - bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); + this->drawCommentTooltip(); ImGui::TableNextColumn(); ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight())); ImGui::TableNextColumn(); @@ -449,7 +463,8 @@ namespace hex::lang { ImGui::TableNextRow(); ImGui::TableNextColumn(); - bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); + this->drawCommentTooltip(); ImGui::TableNextColumn(); ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight())); ImGui::TableNextColumn(); @@ -521,6 +536,7 @@ namespace hex::lang { ImGui::TableNextRow(); ImGui::TableNextColumn(); bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + this->drawCommentTooltip(); ImGui::TableNextColumn(); ImGui::TableNextColumn(); ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1)); @@ -600,7 +616,8 @@ namespace hex::lang { void createEntry(prv::Provider* &provider) override { ImGui::TableNextRow(); ImGui::TableNextColumn(); - bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); + this->drawCommentTooltip(); ImGui::TableNextColumn(); ImGui::TableNextColumn(); ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), std::max(this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1), u64(0))); @@ -699,7 +716,8 @@ namespace hex::lang { valueString += "???"; ImGui::TableNextRow(); - ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); + this->drawCommentTooltip(); ImGui::TableNextColumn(); if (ImGui::Selectable(("##PatternDataLine"s + std::to_string(this->getOffset())).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) { Region selectRegion = { this->getOffset(), this->getSize() }; @@ -749,7 +767,8 @@ namespace hex::lang { ImGui::TableNextRow(); ImGui::TableNextColumn(); - bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); + bool open = ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); + this->drawCommentTooltip(); ImGui::TableNextColumn(); ImGui::TableNextColumn(); ImGui::Text("0x%08llX : 0x%08llX", this->getOffset(), this->getOffset() + this->getSize() - 1); @@ -770,7 +789,7 @@ namespace hex::lang { u16 bitOffset = 0; for (auto &[entryName, entrySize] : this->m_fields) { ImGui::TableNextRow(); - ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth); + ImGui::TreeNodeEx(this->getVariableName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); ImGui::TableNextColumn(); ImGui::Text("%s", entryName.c_str()); ImGui::TableNextColumn(); diff --git a/plugins/libimhex/source/lang/evaluator.cpp b/plugins/libimhex/source/lang/evaluator.cpp index 0f651d06b..e8900c257 100644 --- a/plugins/libimhex/source/lang/evaluator.cpp +++ b/plugins/libimhex/source/lang/evaluator.cpp @@ -338,10 +338,12 @@ namespace hex::lang { auto handleVariableAttributes = [this, &currPattern](auto attribute, auto value) { - if (attribute == "color") - currPattern->setColor(hex::changeEndianess(u32(strtoul(value.data(), nullptr, 0)) << 8, std::endian::big)); - else if (attribute == "name") - currPattern->setVariableName(value.data()); + if (attribute == "color" && value.has_value()) + currPattern->setColor(hex::changeEndianess(u32(strtoul(value->data(), nullptr, 0)) << 8, std::endian::big)); + else if (attribute == "name" && value.has_value()) + currPattern->setVariableName(value->data()); + else if (attribute == "comment" && value.has_value()) + currPattern->setComment(value->data()); else this->getConsole().abortEvaluation("unknown or invalid attribute"); @@ -349,6 +351,9 @@ namespace hex::lang { auto &attributes = attributableNode->getAttributes(); + if (attributes.empty()) + return currPattern; + if (auto variableDeclNode = dynamic_cast(currNode); variableDeclNode != nullptr) { for (auto &attribute : attributes) handleVariableAttributes(attribute->getAttribute(), attribute->getValue()); diff --git a/plugins/libimhex/source/lang/parser.cpp b/plugins/libimhex/source/lang/parser.cpp index b0d8b528e..67d8075c3 100644 --- a/plugins/libimhex/source/lang/parser.cpp +++ b/plugins/libimhex/source/lang/parser.cpp @@ -420,7 +420,7 @@ namespace hex::lang { if (MATCHES(sequence(VALUETYPE_PADDING, SEPARATOR_SQUAREBRACKETOPEN))) member = parsePadding(); - else if (MATCHES((optional(KEYWORD_BE), optional(KEYWORD_LE)) && variant(IDENTIFIER, VALUETYPE_ANY) && sequence(IDENTIFIER, SEPARATOR_SQUAREBRACKETOPEN))) + else if (MATCHES((optional(KEYWORD_BE), optional(KEYWORD_LE)) && variant(IDENTIFIER, VALUETYPE_ANY) && sequence(IDENTIFIER, SEPARATOR_SQUAREBRACKETOPEN) && none(sequence(SEPARATOR_SQUAREBRACKETOPEN)))) member = parseMemberArrayVariable(); else if (MATCHES((optional(KEYWORD_BE), optional(KEYWORD_LE)) && variant(IDENTIFIER, VALUETYPE_ANY) && sequence(IDENTIFIER))) member = parseMemberVariable();