Skip to content

Instantly share code, notes, and snippets.

@Romain-P
Created March 1, 2018 22:34
Show Gist options
  • Select an option

  • Save Romain-P/30659e4df2d88f736dd33bd858b70952 to your computer and use it in GitHub Desktop.

Select an option

Save Romain-P/30659e4df2d88f736dd33bd858b70952 to your computer and use it in GitHub Desktop.
//
// EPITECH PROJECT, 2018
//
// File description:
//
//
#include "Parser.hpp"
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>
#include <vector>
#include <Exceptions.hpp>
namespace nts {
std::regex const Parser::REGEX_COMMENTS("^([^#]+)");
std::regex const Parser::REGEX_CHIPSETS(R"(^(\S+)\s+([^\\s(]+)(?:\(([^\s]+)\))?$)");
std::regex const Parser::REGEX_LINKS(R"(^(\S+):(\d+)[^\S]*(\S+):(\d+)$)");
bool (*Parser::PARSERS[])(std::smatch &matcher, std::string &line) = {
//State.CLEAR_FUNC
[] (std::smatch &matcher, std::string &line) {
if (std::regex_search(line, matcher, REGEX_COMMENTS)) {
std::string (matcher[1]);
if (std::all_of(line.begin(), line.end(),
[](char c) { return c == '\t' || c == ' '; })) {
return true;
}
} else return true;
return false;
},
//State.CHIPSETS
[] (std::smatch &matcher, std::string &line) {
return false;
},
//State.LINKS
[] (std::smatch &matcher, std::string &line) {
return false;
}
};
bool Parser::run() const {
std::ifstream stream(this->_file);
if (!stream.is_open())
throw ParsingError("Can't open the file " + this->_file);
std::smatch matcher;
std::string line;
while (std::getline(stream, line)) {
if (_state != CLEAR_FUNC && PARSERS[CLEAR_FUNC](matcher, line))
continue;
line = PARSERS[_state](matcher, line);
}
return true;
}
}
//
// Created by romain on 01/03/2018.
//
#ifndef CPP_NANOTEKSPICE_HEADER_H
#define CPP_NANOTEKSPICE_HEADER_H
#include "ComponentFactory.hpp"
#include <memory>
#include <regex>
namespace nts
{
class Parser
{
public:
Parser(ComponentFactory * const factory, std::string const &file)
: _factory(factory), _file(file), _state(CLEAR_FUNC) {};
~Parser() = default;
/**
* Perform the parsing of the file field that was previously opened
*
* @return false in case of error, true otherwise
*/
bool run() const;
private:
static char constexpr CONFIG_KW_CHIPSETS[] = ".chipsets:";
static char constexpr CONFIG_KW_LINKS[] = ".links:";
static std::regex const REGEX_COMMENTS;
static std::regex const REGEX_CHIPSETS;
static std::regex const REGEX_LINKS;
static bool (*PARSERS[])(std::smatch &matcher, std::string &line);
enum ParserFunc {
CLEAR_FUNC,
CHIPSETS,
LINKS
};
ComponentFactory * const _factory;
std::string const _file;
ParserFunc _state;
};
}
#endif //CPP_NANOTEKSPICE_HEADER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment