From af2be5457d6b2717ff2fbb9cc3d4e05a3d8dea63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Thu, 17 Jun 2021 11:19:35 +0200 Subject: [PATCH] parser is working quite nicely --- sources/Parser/ParserYaml.cpp | 23 +++++++++++++++++------ sources/Parser/ParserYaml.hpp | 2 +- sources/main.cpp | 4 ---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/sources/Parser/ParserYaml.cpp b/sources/Parser/ParserYaml.cpp index a3ead2db..28817305 100644 --- a/sources/Parser/ParserYaml.cpp +++ b/sources/Parser/ParserYaml.cpp @@ -81,7 +81,7 @@ namespace BBM { std::replace(name.begin(), name.end(), ' ', '_'); _bonus << std::endl << " " << name << ":" << std::endl << " "; _bonus << std::string("bonus_type: ") << _getBonusType(entity.getName()) << std::endl << " "; - _bonus << "position: [" << std::to_string(position->getX()) << " " << std::to_string(position->getY()) << " " << std::to_string(position->getZ()) << "]"; + _bonus << "position: [" << std::to_string(position->getX()) << "," << std::to_string(position->getY()) << "," << std::to_string(position->getZ()) << "]"; } void ParserYAML::_savePlayer(const WAL::Entity &entity) @@ -100,7 +100,7 @@ namespace BBM { _player << "max_bomb: " << std::to_string(bombHolder->maxBombCount) << std::endl << " "; _player << "explosion_radius: " << std::to_string(bombHolder->explosionRadius) << std::endl << " "; _player << "speed: " << std::to_string(controllable->speed) << std::endl << " "; - _player << "position: [" << std::to_string(position->getX()) << " " << std::to_string(position->getY()) << " " << std::to_string(position->getZ()) << "]"; + _player << "position: [" << std::to_string(position->getX()) << "," << std::to_string(position->getY()) << "," << std::to_string(position->getZ()) << "]"; } void ParserYAML::_saveBlock(const WAL::Entity &entity) @@ -113,7 +113,7 @@ namespace BBM { std::replace(name.begin(), name.end(), ' ', '_'); _block << std::endl << " " << name << ":" << std::endl << " "; _block << std::string("block_type: ") << _getBlockType(entity.getName()) << std::endl << " "; - _block << "position: [" << std::to_string(position->getX()) << " " << std::to_string(position->getY()) << " " << std::to_string(position->getZ()) << "]"; + _block << "position: [" << std::to_string(position->getX()) << "," << std::to_string(position->getY()) << "," << std::to_string(position->getZ()) << "]"; } void ParserYAML::save(std::shared_ptr scene) @@ -501,8 +501,17 @@ namespace BBM { while(std::getline(file, line)) { if (line.empty()) continue; - if (!isCorrectIndentLevel(line, indentLevel)) + float lineIndentLevel = getIndent(line); + if (lineIndentLevel != static_cast(lineIndentLevel)) { + throw ParserError("Yaml only support 2 spaces as indent"); + } + if (lineIndentLevel > indentLevel) { + throw ParserError("indent issue"); + } + if (lineIndentLevel < indentLevel) { + file.seekg(static_cast(file.tellg()) - (line.length() + 1)); return node; + } if (isHeader(line)) { node.addChildNode(parseNode(file, parseHeader(line), indentLevel + 1)); } else { @@ -512,14 +521,16 @@ namespace BBM { return node; } - bool ParserYAML::isCorrectIndentLevel(const std::string &line, int indentLevel) + float ParserYAML::getIndent(const std::string &line) { int nb = 0; for (const auto &c : line) { if (!std::isspace(c)) break; + if (c != ' ') + throw ParserError("Yaml only support 2 spaces as indent"); nb++; } - return nb == indentLevel; + return static_cast(nb / 2.); } } \ No newline at end of file diff --git a/sources/Parser/ParserYaml.hpp b/sources/Parser/ParserYaml.hpp index 178e32cf..36f0a4c4 100644 --- a/sources/Parser/ParserYaml.hpp +++ b/sources/Parser/ParserYaml.hpp @@ -102,7 +102,7 @@ namespace BBM { static Node parseNode(std::ifstream &file, const std::string &nodeName, int indentLevel = 0); - static bool isCorrectIndentLevel(const std::string &line, int indentLevel); + static float getIndent(const std::string &line); static constexpr const char* indent = " "; diff --git a/sources/main.cpp b/sources/main.cpp index 6f11b524..c4e69c4a 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -8,7 +8,6 @@ #include #include "Runner/Runner.hpp" -#include "Parser/ParserYaml.hpp" void usage(const std::string &bin) { @@ -24,8 +23,5 @@ 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(); }