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

107 lines
2.3 KiB
C++

#ifndef __PLASP__UTILS__PARSER_H
#define __PLASP__UTILS__PARSER_H
#include <iostream>
#include <iterator>
#include <vector>
namespace plasp
{
namespace utils
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Parser
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Parser
{
public:
using CharacterType = unsigned char;
public:
explicit Parser(std::istream &istream);
size_t row() const;
size_t column() const;
CharacterType currentCharacter() const;
template<typename Type>
Type parse();
template<class WhiteSpacePredicate, class CharacterPredicate>
std::string parseIdentifier(WhiteSpacePredicate whiteSpacePredicate, CharacterPredicate characterPredicate);
template<typename Type>
void expect(const Type &expectedValue);
template<class WhiteSpacePredicate>
void skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate);
void skipWhiteSpace();
void skipLine();
std::string getLine();
private:
static const std::istream_iterator<CharacterType> EndOfFile;
private:
void checkStream() const;
void advance();
bool advanceIf(CharacterType expectedCharacter);
uint64_t parseIntegerBody();
std::istream &m_istream;
std::istream_iterator<CharacterType> m_position;
size_t m_row;
size_t m_column;
bool m_endOfFile;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class WhiteSpacePredicate, class CharacterPredicate>
std::string Parser::parseIdentifier(WhiteSpacePredicate whiteSpacePredicate, CharacterPredicate characterPredicate)
{
skipWhiteSpace(whiteSpacePredicate);
std::string value;
while (true)
{
const auto character = *m_position;
if (!characterPredicate(character))
return value;
value.push_back(character);
advance();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class WhiteSpacePredicate>
void Parser::skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate)
{
checkStream();
while (whiteSpacePredicate(*m_position))
advance();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif