From ed9ae78dd6a3753f4745d2a8d0ea164384fc0503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 17 Jun 2021 01:48:49 +0200 Subject: [PATCH] trivial parser is working --- CMakeLists.txt | 7 ++++++- sources/Parser/ParserYaml.cpp | 22 ++++++++++++++++++---- sources/Parser/ParserYaml.hpp | 7 +++++-- sources/Utils/Utils.cpp | 22 ++++++++++++++++------ sources/Utils/Utils.hpp | 15 +++++++++------ sources/main.cpp | 4 ++++ 6 files changed, 58 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ac648f39..74f8a0b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,7 +147,12 @@ set(SOURCES sources/Runner/LobbyScene.cpp sources/Runner/ResumeLobbyScene.cpp sources/Runner/ScoreScene.cpp - sources/Parser/Node.cpp sources/Parser/Node.hpp sources/Utils/Utils.cpp sources/Utils/Utils.hpp) + sources/Parser/Node.cpp + sources/Parser/Node.hpp + sources/Utils/Utils.cpp + sources/Utils/Utils.hpp + ) + add_executable(bomberman sources/main.cpp ${SOURCES} diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index 3e848adf..a3ead2db 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -458,6 +458,7 @@ namespace BBM { if (headerName.back() != ':') { throw ParserError("header not ended with ':' , line: " + Utils::trimCopy(line)); } + headerName.pop_back(); return headerName; } @@ -476,13 +477,13 @@ namespace BBM { if (propertyName.back() != ':') { throw ParserError("property name not ended with ':' , line: " + Utils::trimCopy(line)); } - + propertyName.pop_back(); return std::make_pair(propertyName, propertyValue); } bool ParserYAML::isHeader(const std::string &line) { - return line.find(':') == line.back(); + return line.back() == ':'; } Node ParserYAML::parseFile(const std::string &path) @@ -492,7 +493,7 @@ namespace BBM { return parseNode(file, "root"); } - Node ParserYAML::parseNode(std::ifstream &file, const std::string &nodeName) + Node ParserYAML::parseNode(std::ifstream &file, const std::string &nodeName, int indentLevel) { std::string line; Node node(nodeName); @@ -500,12 +501,25 @@ namespace BBM { while(std::getline(file, line)) { if (line.empty()) continue; + if (!isCorrectIndentLevel(line, indentLevel)) + return node; if (isHeader(line)) { - node.addChildNode(parseNode(file, parseHeader(line))); + node.addChildNode(parseNode(file, parseHeader(line), indentLevel + 1)); } else { node.setProperty(parseProperty(line)); } } return node; } + + bool ParserYAML::isCorrectIndentLevel(const std::string &line, int indentLevel) + { + int nb = 0; + for (const auto &c : line) { + if (!std::isspace(c)) + break; + nb++; + } + return nb == indentLevel; + } } \ No newline at end of file diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index d2edb397..178e32cf 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -100,13 +100,16 @@ namespace BBM { static bool isHeader(const std::string &line); - static Node parseFile(const std::string &path); + static Node parseNode(std::ifstream &file, const std::string &nodeName, int indentLevel = 0); - static Node parseNode(std::ifstream &file, const std::string &nodeName); + static bool isCorrectIndentLevel(const std::string &line, int indentLevel); static constexpr const char* indent = " "; public: + + static Node parseFile(const std::string &path); + //! @brief All name that was into the file static std::vector playerName; //! @brief Player position diff --git a/sources/Utils/Utils.cpp b/sources/Utils/Utils.cpp index fd3449e4..fd4f39ce 100644 --- a/sources/Utils/Utils.cpp +++ b/sources/Utils/Utils.cpp @@ -6,46 +6,56 @@ #include #include #include +#include #include "Utils.hpp" namespace BBM { - inline void Utils::lTrim(std::string &s) + void Utils::lTrim(std::string &s) { s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); } - inline void Utils::rTrim(std::string &s) + void Utils::rTrim(std::string &s) { s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end()); } - inline void Utils::trim(std::string &s) + void Utils::trim(std::string &s) { lTrim(s); rTrim(s); } - inline std::string Utils::lTrimCopy(std::string s) + std::string Utils::lTrimCopy(std::string s) { lTrim(s); return s; } - inline std::string Utils::rTrimCopy(std::string s) + std::string Utils::rTrimCopy(std::string s) { rTrim(s); return s; } - inline std::string Utils::trimCopy(std::string s) + std::string Utils::trimCopy(std::string s) { trim(s); return s; } + int Utils::findFrequency(const std::string &s, const std::string &pattern) + { + std::regex c(pattern); + std::smatch m; + + ptrdiff_t numberOfMatches = std::distance(std::sregex_iterator(s.begin(), s.end(), c), std::sregex_iterator()); + return static_cast(numberOfMatches); + } + } \ No newline at end of file diff --git a/sources/Utils/Utils.hpp b/sources/Utils/Utils.hpp index a8209298..4bca5c4d 100644 --- a/sources/Utils/Utils.hpp +++ b/sources/Utils/Utils.hpp @@ -9,22 +9,25 @@ namespace BBM struct Utils { //! @brief trim left end - static inline void lTrim(std::string &s); + static void lTrim(std::string &s); //! @brief trim right end - static inline void rTrim(std::string &s); + static void rTrim(std::string &s); //! @brief trim from both ends - static inline void trim(std::string &s); + static void trim(std::string &s); //! @brief trim left end (copying) - static inline std::string lTrimCopy(std::string s); + static std::string lTrimCopy(std::string s); //! @brief trim right end (copying) - static inline std::string rTrimCopy(std::string s); + static std::string rTrimCopy(std::string s); //! @brief trim from both ends (copying) - static inline std::string trimCopy(std::string s); + static std::string trimCopy(std::string s); + + //! @brief find the frequency of a substring in a string + static int findFrequency(const std::string &s, const std::string &pattern); }; diff --git a/sources/main.cpp b/sources/main.cpp index c4e69c4a..6f11b524 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -8,6 +8,7 @@ #include #include "Runner/Runner.hpp" +#include "Parser/ParserYaml.hpp" void usage(const std::string &bin) { @@ -23,5 +24,8 @@ int main(int argc, char **argv) usage(argv[0]); return 1; } + BBM::Node node = BBM::ParserYAML::parseFile("test.yml"); + + return 0; return BBM::Runner::run(); }