2016-05-27 03:58:59 +02:00
|
|
|
#ifndef __PLASP__UTILS__PARSER_H
|
|
|
|
#define __PLASP__UTILS__PARSER_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
2016-05-28 14:21:05 +02:00
|
|
|
#include <vector>
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
namespace plasp
|
|
|
|
{
|
|
|
|
namespace utils
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Parser
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
{
|
|
|
|
public:
|
2016-05-27 19:08:31 +02:00
|
|
|
explicit Parser(std::istream &istream);
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
size_t row() const;
|
|
|
|
size_t column() const;
|
|
|
|
|
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-05-30 20:43:36 +02:00
|
|
|
bool advanceIf(char expectedCharacter);
|
2016-05-29 16:27:11 +02:00
|
|
|
bool atEndOfFile() 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-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();
|
|
|
|
|
|
|
|
std::istream &m_istream;
|
2016-05-30 12:54:56 +02:00
|
|
|
std::istreambuf_iterator<char> m_position;
|
2016-05-27 03:58:59 +02:00
|
|
|
|
|
|
|
size_t m_row;
|
|
|
|
size_t m_column;
|
|
|
|
|
2016-06-04 18:28:43 +02:00
|
|
|
bool m_isCaseSensitive;
|
|
|
|
|
2016-05-27 03:58:59 +02:00
|
|
|
bool m_endOfFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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-05-30 12:54:56 +02:00
|
|
|
while (!atEndOfFile() && 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
|