2016-05-27 03:58:59 +02:00
|
|
|
#ifndef __PLASP__UTILS__PARSER_H
|
|
|
|
#define __PLASP__UTILS__PARSER_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
2016-06-07 18:41:01 +02:00
|
|
|
#include <sstream>
|
2016-05-28 14:21:05 +02:00
|
|
|
#include <vector>
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2016-05-27 03:58:59 +02:00
|
|
|
namespace plasp
|
|
|
|
{
|
|
|
|
namespace utils
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Parser
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
public:
|
2016-06-07 18:41:01 +02:00
|
|
|
using Position = std::stringstream::pos_type;
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
struct Coordinate
|
|
|
|
{
|
|
|
|
std::string sectionName;
|
|
|
|
size_t row;
|
|
|
|
size_t column;
|
|
|
|
};
|
2016-06-07 15:54:01 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
struct StreamDelimiter
|
|
|
|
{
|
|
|
|
Position position;
|
|
|
|
std::string sectionName;
|
|
|
|
};
|
2016-06-07 15:54:01 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
public:
|
|
|
|
explicit Parser();
|
|
|
|
explicit Parser(std::string streamName, std::istream &istream);
|
|
|
|
|
|
|
|
void readStream(std::string streamName, std::istream &istream);
|
|
|
|
void readFile(const boost::filesystem::path &path);
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
void seek(Position position);
|
|
|
|
Position position() const;
|
|
|
|
Coordinate coordinate() const;
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-06-04 18:28:43 +02:00
|
|
|
void setCaseSensitive(bool isCaseInsensitive = true);
|
|
|
|
|
2016-05-30 12:54:56 +02:00
|
|
|
char currentCharacter() const;
|
2016-05-29 16:27:11 +02:00
|
|
|
void advance();
|
2016-06-07 18:41:01 +02:00
|
|
|
bool atEndOfStream() const;
|
2016-05-29 15:08:10 +02:00
|
|
|
|
2016-05-27 18:39:43 +02:00
|
|
|
template<typename Type>
|
|
|
|
Type parse();
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-05-29 16:27:11 +02:00
|
|
|
template<class CharacterPredicate, class WhiteSpacePredicate>
|
|
|
|
std::string parseIdentifier(CharacterPredicate characterPredicate, WhiteSpacePredicate whiteSpacePredicate);
|
|
|
|
|
|
|
|
template<class CharacterPredicate>
|
|
|
|
std::string parseIdentifier(CharacterPredicate characterPredicate);
|
2016-05-28 14:21:05 +02:00
|
|
|
|
2016-06-08 00:02:40 +02:00
|
|
|
template<typename Type>
|
|
|
|
bool probe(const Type &expectedValue);
|
|
|
|
|
2016-06-09 15:33:09 +02:00
|
|
|
bool probeIdentifier(const std::string &identifier);
|
|
|
|
|
2016-05-27 18:39:43 +02:00
|
|
|
template<typename Type>
|
|
|
|
void expect(const Type &expectedValue);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-05-29 15:08:10 +02:00
|
|
|
template<class WhiteSpacePredicate>
|
|
|
|
void skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate);
|
|
|
|
|
2016-05-27 03:58:59 +02:00
|
|
|
void skipWhiteSpace();
|
|
|
|
void skipLine();
|
|
|
|
|
|
|
|
std::string getLine();
|
|
|
|
|
|
|
|
private:
|
2016-05-30 12:54:56 +02:00
|
|
|
static const std::istreambuf_iterator<char> EndOfFile;
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
void checkStream() const;
|
|
|
|
|
|
|
|
uint64_t parseIntegerBody();
|
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
mutable std::stringstream m_stream;
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
std::vector<StreamDelimiter> m_streamDelimiters;
|
2016-05-27 03:58:59 +02:00
|
|
|
|
2016-06-04 18:28:43 +02:00
|
|
|
bool m_isCaseSensitive;
|
2016-05-27 03:58:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-29 16:27:11 +02:00
|
|
|
template<class CharacterPredicate, class WhiteSpacePredicate>
|
|
|
|
std::string Parser::parseIdentifier(CharacterPredicate characterPredicate, WhiteSpacePredicate whiteSpacePredicate)
|
2016-05-28 14:21:05 +02:00
|
|
|
{
|
2016-05-29 15:08:10 +02:00
|
|
|
skipWhiteSpace(whiteSpacePredicate);
|
|
|
|
|
|
|
|
std::string value;
|
|
|
|
|
2016-05-28 14:21:05 +02:00
|
|
|
while (true)
|
|
|
|
{
|
2016-05-30 12:54:56 +02:00
|
|
|
const auto character = currentCharacter();
|
2016-05-28 14:21:05 +02:00
|
|
|
|
2016-05-29 15:08:10 +02:00
|
|
|
if (!characterPredicate(character))
|
|
|
|
return value;
|
2016-05-28 14:21:05 +02:00
|
|
|
|
2016-05-29 15:08:10 +02:00
|
|
|
value.push_back(character);
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 14:21:05 +02:00
|
|
|
|
2016-05-29 15:08:10 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2016-05-28 14:21:05 +02:00
|
|
|
|
2016-05-29 16:27:11 +02:00
|
|
|
template<class CharacterPredicate>
|
|
|
|
std::string Parser::parseIdentifier(CharacterPredicate characterPredicate)
|
|
|
|
{
|
|
|
|
return parseIdentifier(characterPredicate,
|
|
|
|
[&](const auto character)
|
|
|
|
{
|
|
|
|
return std::isspace(character);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-29 15:08:10 +02:00
|
|
|
template<class WhiteSpacePredicate>
|
|
|
|
void Parser::skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate)
|
|
|
|
{
|
|
|
|
checkStream();
|
2016-05-28 14:21:05 +02:00
|
|
|
|
2016-06-07 18:41:01 +02:00
|
|
|
while (!atEndOfStream() && whiteSpacePredicate(currentCharacter()))
|
2016-05-29 15:08:10 +02:00
|
|
|
advance();
|
2016-05-28 14:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-05-27 03:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|