trivial parser is working

This commit is contained in:
Clément Le Bihan
2021-06-17 01:48:49 +02:00
parent fbe36b2323
commit ed9ae78dd6
6 changed files with 58 additions and 19 deletions
+6 -1
View File
@@ -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}
+18 -4
View File
@@ -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;
}
}
+5 -2
View File
@@ -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<std::string> playerName;
//! @brief Player position
+16 -6
View File
@@ -6,46 +6,56 @@
#include <cctype>
#include <locale>
#include <string>
#include <regex>
#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<int>(numberOfMatches);
}
}
+9 -6
View File
@@ -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);
};
+4
View File
@@ -8,6 +8,7 @@
#include <iostream>
#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();
}