Made Parser seekable for easier maintenance.
This commit is contained in:
@@ -28,7 +28,7 @@ class Description
|
||||
const Domain &domain() const;
|
||||
|
||||
private:
|
||||
Description(std::istream &istream);
|
||||
Description();
|
||||
|
||||
void parseContent();
|
||||
void parseSection();
|
||||
|
@@ -47,6 +47,8 @@ class Description
|
||||
private:
|
||||
Description();
|
||||
|
||||
void parseContent(utils::Parser &parser);
|
||||
|
||||
void parseVersionSection(utils::Parser &parser) const;
|
||||
void parseMetricSection(utils::Parser &parser);
|
||||
void parseVariablesSection(utils::Parser &parser);
|
||||
|
@@ -23,7 +23,7 @@ class Logger
|
||||
|
||||
void setPedantic(bool isPedantic = true);
|
||||
|
||||
void parserWarning(const Parser &parser, const std::string &text);
|
||||
void parserWarning(const Parser &parser, const std::string &message);
|
||||
|
||||
private:
|
||||
bool m_isPedantic;
|
||||
|
@@ -3,8 +3,11 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
@@ -19,22 +22,39 @@ namespace utils
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
explicit Parser(std::istream &istream);
|
||||
using Position = std::stringstream::pos_type;
|
||||
|
||||
void setFileName(std::string fileName);
|
||||
const std::string &fileName() const;
|
||||
struct Coordinate
|
||||
{
|
||||
std::string sectionName;
|
||||
size_t row;
|
||||
size_t column;
|
||||
};
|
||||
|
||||
void resetPosition();
|
||||
struct StreamDelimiter
|
||||
{
|
||||
Position position;
|
||||
std::string sectionName;
|
||||
};
|
||||
|
||||
size_t row() const;
|
||||
size_t column() const;
|
||||
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;
|
||||
|
||||
void setCaseSensitive(bool isCaseInsensitive = true);
|
||||
|
||||
char currentCharacter() const;
|
||||
void advance();
|
||||
bool advanceIf(char expectedCharacter);
|
||||
bool atEndOfFile() const;
|
||||
bool atEndOfStream() const;
|
||||
|
||||
template<typename Type>
|
||||
Type parse();
|
||||
@@ -64,16 +84,11 @@ class Parser
|
||||
|
||||
uint64_t parseIntegerBody();
|
||||
|
||||
std::istream &m_istream;
|
||||
std::string m_fileName;
|
||||
std::istreambuf_iterator<char> m_position;
|
||||
mutable std::stringstream m_stream;
|
||||
|
||||
size_t m_row;
|
||||
size_t m_column;
|
||||
std::vector<StreamDelimiter> m_streamDelimiters;
|
||||
|
||||
bool m_isCaseSensitive;
|
||||
|
||||
bool m_atEndOfFile;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -116,7 +131,7 @@ void Parser::skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate)
|
||||
{
|
||||
checkStream();
|
||||
|
||||
while (!atEndOfFile() && whiteSpacePredicate(currentCharacter()))
|
||||
while (!atEndOfStream() && whiteSpacePredicate(currentCharacter()))
|
||||
advance();
|
||||
}
|
||||
|
||||
|
@@ -31,8 +31,10 @@ class ParserException: public std::exception
|
||||
}
|
||||
|
||||
explicit ParserException(const utils::Parser &parser, const std::string &message)
|
||||
: m_message{parser.fileName() + ":" + std::to_string(parser.row()) + ":" + std::to_string(parser.column()) + " " + message}
|
||||
{
|
||||
const auto coordinate = parser.coordinate();
|
||||
|
||||
m_message = coordinate.sectionName + ":" + std::to_string(coordinate.row) + ":" + std::to_string(coordinate.column) + " " + message;
|
||||
}
|
||||
|
||||
~ParserException() throw()
|
||||
|
@@ -31,8 +31,10 @@ class ParserWarning: public std::exception
|
||||
}
|
||||
|
||||
explicit ParserWarning(const utils::Parser &parser, const std::string &message)
|
||||
: m_message{parser.fileName() + ":" + std::to_string(parser.row()) + ":" + std::to_string(parser.column()) + " " + message}
|
||||
{
|
||||
const auto coordinate = parser.coordinate();
|
||||
|
||||
m_message = coordinate.sectionName + ":" + std::to_string(coordinate.row) + ":" + std::to_string(coordinate.column) + " " + message;
|
||||
}
|
||||
|
||||
~ParserWarning() throw()
|
||||
|
Reference in New Issue
Block a user