patrick
/
plasp
Archived
1
0
Fork 0
This repository has been archived on 2023-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
plasp/include/plasp/utils/Parser.h

123 lines
2.7 KiB
C
Raw Normal View History

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;
void setCaseSensitive(bool isCaseInsensitive = true);
char currentCharacter() const;
void advance();
bool advanceIf(char expectedCharacter);
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
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:
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;
std::istreambuf_iterator<char> m_position;
2016-05-27 03:58:59 +02:00
size_t m_row;
size_t m_column;
bool m_isCaseSensitive;
2016-05-27 03:58:59 +02:00
bool m_endOfFile;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
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)
{
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
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
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