From 74a5c974e678851a60027bbcb582f744bd990c4d Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 7 Apr 2022 13:15:07 +0200 Subject: [PATCH] patterns: Fixed comments behind pre-processor defines --- .../hex/pattern_language/preprocessor.hpp | 2 +- .../source/pattern_language/preprocessor.cpp | 46 ++++++++++++------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/libimhex/include/hex/pattern_language/preprocessor.hpp b/lib/libimhex/include/hex/pattern_language/preprocessor.hpp index 57928ed44..80e8576bc 100644 --- a/lib/libimhex/include/hex/pattern_language/preprocessor.hpp +++ b/lib/libimhex/include/hex/pattern_language/preprocessor.hpp @@ -19,7 +19,7 @@ namespace hex::pl { public: Preprocessor() = default; - std::optional preprocess(const std::string &code, bool initialRun = true); + std::optional preprocess(std::string code, bool initialRun = true); void addPragmaHandler(const std::string &pragmaType, const std::function &function); void removePragmaHandler(const std::string &pragmaType); diff --git a/lib/libimhex/source/pattern_language/preprocessor.cpp b/lib/libimhex/source/pattern_language/preprocessor.cpp index c5f671035..f4b7865e5 100644 --- a/lib/libimhex/source/pattern_language/preprocessor.cpp +++ b/lib/libimhex/source/pattern_language/preprocessor.cpp @@ -8,7 +8,7 @@ namespace hex::pl { - std::optional Preprocessor::preprocess(const std::string &code, bool initialRun) { + std::optional Preprocessor::preprocess(std::string code, bool initialRun) { u32 offset = 0; u32 lineNumber = 1; bool isInString = false; @@ -22,6 +22,34 @@ namespace hex::pl { output.reserve(code.length()); try { + while (offset < code.length()) { + if (code.substr(offset, 2) == "//") { + while (code[offset] != '\n' && offset < code.length()) + offset += 1; + } else if (code.substr(offset, 2) == "/*") { + while (code.substr(offset, 2) != "*/" && offset < code.length()) { + if (code[offset] == '\n') { + output += '\n'; + lineNumber++; + } + + offset += 1; + } + + offset += 2; + if (offset >= code.length()) + throwPreprocessorError("unterminated comment", lineNumber - 1); + } else { + output += code[offset]; + offset++; + } + } + + offset = 0; + code = output; + output.clear(); + output.reserve(code.size()); + bool startOfLine = true; while (offset < code.length()) { if (offset > 0 && code[offset - 1] != '\\' && code[offset] == '\"') @@ -179,22 +207,6 @@ namespace hex::pl { this->m_pragmas.emplace(pragmaKey, pragmaValue, lineNumber); } else throwPreprocessorError("unknown preprocessor directive", lineNumber); - } else if (code.substr(offset, 2) == "//") { - while (code[offset] != '\n' && offset < code.length()) - offset += 1; - } else if (code.substr(offset, 2) == "/*") { - while (code.substr(offset, 2) != "*/" && offset < code.length()) { - if (code[offset] == '\n') { - output += '\n'; - lineNumber++; - } - - offset += 1; - } - - offset += 2; - if (offset >= code.length()) - throwPreprocessorError("unterminated comment", lineNumber - 1); } if (code[offset] == '\n') {