Major refactoring of underlying Parser class.
This commit is contained in:
@@ -13,13 +13,12 @@ namespace plasp
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Language::Type detectLanguage(utils::Parser &parser)
|
||||
Language::Type detectLanguage(utils::Parser<utils::CaseInsensitiveParserPolicy> &parser)
|
||||
{
|
||||
parser.setCaseSensitive(false);
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
// SAS begins with "begin_version"
|
||||
if (parser.probe<std::string>("begin"))
|
||||
if (parser.testAndSkip<std::string>("begin"))
|
||||
{
|
||||
parser.seek(0);
|
||||
return Language::Type::SAS;
|
||||
@@ -33,7 +32,7 @@ Language::Type detectLanguage(utils::Parser &parser)
|
||||
}
|
||||
|
||||
// PDDL contains sections starting with "(define"
|
||||
if (parser.probe<std::string>("(") && parser.probe<std::string>("define"))
|
||||
if (parser.testAndSkip<std::string>("(") && parser.testAndSkip<std::string>("define"))
|
||||
{
|
||||
parser.seek(std::ios::beg);
|
||||
return Language::Type::PDDL;
|
||||
|
@@ -5,6 +5,7 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <plasp/pddl/Parser.h>
|
||||
#include <plasp/utils/Logger.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -22,20 +23,21 @@ class Context
|
||||
{
|
||||
public:
|
||||
Context() = default;
|
||||
~Context() = default;
|
||||
|
||||
explicit Context(utils::Parser &&parser)
|
||||
: parser{std::move(parser)}
|
||||
explicit Context(Parser &&otherParser)
|
||||
: parser{std::move(otherParser)}
|
||||
{
|
||||
}
|
||||
|
||||
explicit Context(utils::Logger &&logger)
|
||||
: logger{std::move(logger)}
|
||||
explicit Context(utils::Logger &&otherLogger)
|
||||
: logger{std::move(otherLogger)}
|
||||
{
|
||||
}
|
||||
|
||||
explicit Context(utils::Parser &&parser, utils::Logger &&logger)
|
||||
: parser{std::move(parser)},
|
||||
logger{std::move(logger)}
|
||||
explicit Context(Parser &&otherParser, utils::Logger &&otherLogger)
|
||||
: parser{std::move(otherParser)},
|
||||
logger{std::move(otherLogger)}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -56,7 +58,7 @@ class Context
|
||||
return *this;
|
||||
}
|
||||
|
||||
utils::Parser parser;
|
||||
Parser parser;
|
||||
utils::Logger logger;
|
||||
};
|
||||
|
||||
|
@@ -45,9 +45,9 @@ class Description
|
||||
|
||||
Context m_context;
|
||||
|
||||
utils::Parser::Position m_domainPosition;
|
||||
utils::Stream::Position m_domainPosition;
|
||||
std::unique_ptr<Domain> m_domain;
|
||||
utils::Parser::Position m_problemPosition;
|
||||
utils::Stream::Position m_problemPosition;
|
||||
std::unique_ptr<Problem> m_problem;
|
||||
};
|
||||
|
||||
|
@@ -4,10 +4,11 @@
|
||||
#include <plasp/pddl/Action.h>
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Parser.h>
|
||||
#include <plasp/pddl/Requirement.h>
|
||||
#include <plasp/pddl/expressions/Constant.h>
|
||||
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
||||
#include <plasp/pddl/expressions/PrimitiveType.h>
|
||||
#include <plasp/pddl/Requirement.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -68,19 +69,19 @@ class Domain
|
||||
|
||||
std::string m_name;
|
||||
|
||||
utils::Parser::Position m_requirementsPosition;
|
||||
utils::Stream::Position m_requirementsPosition;
|
||||
Requirements m_requirements;
|
||||
|
||||
utils::Parser::Position m_typesPosition;
|
||||
utils::Stream::Position m_typesPosition;
|
||||
expressions::PrimitiveTypes m_types;
|
||||
|
||||
utils::Parser::Position m_constantsPosition;
|
||||
utils::Stream::Position m_constantsPosition;
|
||||
expressions::Constants m_constants;
|
||||
|
||||
utils::Parser::Position m_predicatesPosition;
|
||||
utils::Stream::Position m_predicatesPosition;
|
||||
expressions::PredicateDeclarations m_predicates;
|
||||
|
||||
std::vector<utils::Parser::Position> m_actionPositions;
|
||||
std::vector<utils::Stream::Position> m_actionPositions;
|
||||
std::vector<std::unique_ptr<Action>> m_actions;
|
||||
};
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <plasp/utils/Parser.h>
|
||||
#include <plasp/pddl/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -16,7 +16,7 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline void skipSection(utils::Parser &parser)
|
||||
inline void skipSection(Parser &parser)
|
||||
{
|
||||
size_t openParentheses = 1;
|
||||
|
||||
|
49
include/plasp/pddl/Parser.h
Normal file
49
include/plasp/pddl/Parser.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#ifndef __PLASP__PDDL__PARSER_H
|
||||
#define __PLASP__PDDL__PARSER_H
|
||||
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Parser
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class PDDLParserPolicy
|
||||
{
|
||||
public:
|
||||
static char transformCharacter(char c) noexcept
|
||||
{
|
||||
return std::tolower(c);
|
||||
}
|
||||
|
||||
static bool isWhiteSpace(char c)
|
||||
{
|
||||
return std::iswspace(c);
|
||||
}
|
||||
|
||||
static bool isIdentifierCharacter(char c)
|
||||
{
|
||||
return c != '?'
|
||||
&& c != '('
|
||||
&& c != ')'
|
||||
&& c != ';'
|
||||
&& std::isgraph(c);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using Parser = utils::Parser<PDDLParserPolicy>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@@ -4,6 +4,7 @@
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/InitialState.h>
|
||||
#include <plasp/pddl/Parser.h>
|
||||
#include <plasp/pddl/Requirement.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -61,18 +62,18 @@ class Problem
|
||||
|
||||
std::string m_name;
|
||||
|
||||
utils::Parser::Position m_domainPosition;
|
||||
utils::Stream::Position m_domainPosition;
|
||||
|
||||
utils::Parser::Position m_requirementsPosition;
|
||||
utils::Stream::Position m_requirementsPosition;
|
||||
Requirements m_requirements;
|
||||
|
||||
utils::Parser::Position m_objectsPosition;
|
||||
utils::Stream::Position m_objectsPosition;
|
||||
expressions::Constants m_objects;
|
||||
|
||||
utils::Parser::Position m_initialStatePosition;
|
||||
utils::Stream::Position m_initialStatePosition;
|
||||
std::unique_ptr<InitialState> m_initialState;
|
||||
|
||||
utils::Parser::Position m_goalPosition;
|
||||
utils::Stream::Position m_goalPosition;
|
||||
ExpressionPointer m_goal;
|
||||
};
|
||||
|
||||
|
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -57,8 +56,8 @@ AtPointer At::parse(Context &context, ExpressionContext &expressionContext,
|
||||
|
||||
const auto position = parser.position();
|
||||
|
||||
if (!parser.probe<std::string>("(")
|
||||
|| !parser.probeIdentifier("at", isIdentifier))
|
||||
if (!parser.testAndSkip<std::string>("(")
|
||||
|| !parser.testIdentifierAndSkip("at"))
|
||||
{
|
||||
parser.seek(position);
|
||||
return nullptr;
|
||||
@@ -68,9 +67,9 @@ AtPointer At::parse(Context &context, ExpressionContext &expressionContext,
|
||||
|
||||
const auto timePointPosition = parser.position();
|
||||
|
||||
if (parser.probeIdentifier("start", isIdentifier))
|
||||
if (parser.testIdentifierAndSkip("start"))
|
||||
timePoint = TimePointStart;
|
||||
else if (parser.probeIdentifier("end", isIdentifier))
|
||||
else if (parser.testIdentifierAndSkip("end"))
|
||||
timePoint = TimePointEnd;
|
||||
else if (parser.probeNumber())
|
||||
{
|
||||
|
@@ -4,7 +4,6 @@
|
||||
#include <plasp/pddl/ConsistencyException.h>
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Variable.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -52,8 +51,8 @@ std::unique_ptr<Derived> Binary<Derived>::parse(Context &context,
|
||||
|
||||
const auto position = parser.position();
|
||||
|
||||
if (!parser.probe<std::string>("(")
|
||||
|| !parser.probeIdentifier(Derived::Identifier, isIdentifier))
|
||||
if (!parser.testAndSkip<std::string>("(")
|
||||
|| !parser.testIdentifierAndSkip(Derived::Identifier))
|
||||
{
|
||||
parser.seek(position);
|
||||
return nullptr;
|
||||
|
@@ -2,7 +2,6 @@
|
||||
#define __PLASP__PDDL__EXPRESSIONS__CONSTANT_H
|
||||
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
|
@@ -4,7 +4,6 @@
|
||||
#include <plasp/pddl/ConsistencyException.h>
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Variable.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -50,8 +49,8 @@ std::unique_ptr<Derived> NAry<Derived>::parse(Context &context,
|
||||
|
||||
const auto position = parser.position();
|
||||
|
||||
if (!parser.probe<std::string>("(")
|
||||
|| !parser.probeIdentifier(Derived::Identifier, isIdentifier))
|
||||
if (!parser.testAndSkip<std::string>("(")
|
||||
|| !parser.testIdentifierAndSkip(Derived::Identifier))
|
||||
{
|
||||
parser.seek(position);
|
||||
return nullptr;
|
||||
@@ -71,7 +70,7 @@ std::unique_ptr<Derived> NAry<Derived>::parse(Context &context,
|
||||
}
|
||||
|
||||
if (expression->m_arguments.empty())
|
||||
context.logger.logWarning(context.parser, "“" + Derived::Identifier + "” expressions should not be empty");
|
||||
context.logger.logWarning(parser.coordinate(), "“" + Derived::Identifier + "” expressions should not be empty");
|
||||
|
||||
parser.expect<std::string>(")");
|
||||
|
||||
|
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -50,8 +49,8 @@ NotPointer Not::parse(Context &context, ExpressionContext &expressionContext,
|
||||
|
||||
const auto position = parser.position();
|
||||
|
||||
if (!parser.probe<std::string>("(")
|
||||
|| !parser.probeIdentifier("not", isIdentifier))
|
||||
if (!parser.testAndSkip<std::string>("(")
|
||||
|| !parser.testIdentifierAndSkip("not"))
|
||||
{
|
||||
parser.seek(position);
|
||||
return nullptr;
|
||||
|
@@ -3,7 +3,6 @@
|
||||
|
||||
#include <plasp/pddl/ConsistencyException.h>
|
||||
#include <plasp/pddl/Expression.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
|
@@ -27,8 +27,8 @@ using AssignedVariables = std::vector<AssignedVariable>;
|
||||
class AssignedVariable
|
||||
{
|
||||
public:
|
||||
static AssignedVariable fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static AssignedVariable fromSAS(utils::Parser &parser, const Variable &variable);
|
||||
static AssignedVariable fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
static AssignedVariable fromSAS(utils::Parser<> &parser, const Variable &variable);
|
||||
|
||||
public:
|
||||
explicit AssignedVariable(const Variable &variable, const Value &value);
|
||||
|
@@ -29,7 +29,7 @@ class AxiomRule
|
||||
using Condition = AssignedVariable;
|
||||
using Conditions = AssignedVariables;
|
||||
|
||||
static AxiomRule fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static AxiomRule fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Conditions &conditions() const;
|
||||
|
@@ -29,7 +29,7 @@ namespace sas
|
||||
class Description
|
||||
{
|
||||
public:
|
||||
static Description fromParser(utils::Parser &&parser);
|
||||
static Description fromParser(utils::Parser<> &&parser);
|
||||
static Description fromStream(std::istream &istream);
|
||||
static Description fromFile(const boost::filesystem::path &path);
|
||||
|
||||
@@ -48,16 +48,16 @@ class Description
|
||||
private:
|
||||
Description();
|
||||
|
||||
void parseContent(utils::Parser &parser);
|
||||
void parseContent(utils::Parser<> &parser);
|
||||
|
||||
void parseVersionSection(utils::Parser &parser) const;
|
||||
void parseMetricSection(utils::Parser &parser);
|
||||
void parseVariablesSection(utils::Parser &parser);
|
||||
void parseMutexSection(utils::Parser &parser);
|
||||
void parseInitialStateSection(utils::Parser &parser);
|
||||
void parseGoalSection(utils::Parser &parser);
|
||||
void parseOperatorSection(utils::Parser &parser);
|
||||
void parseAxiomSection(utils::Parser &parser);
|
||||
void parseVersionSection(utils::Parser<> &parser) const;
|
||||
void parseMetricSection(utils::Parser<> &parser);
|
||||
void parseVariablesSection(utils::Parser<> &parser);
|
||||
void parseMutexSection(utils::Parser<> &parser);
|
||||
void parseInitialStateSection(utils::Parser<> &parser);
|
||||
void parseGoalSection(utils::Parser<> &parser);
|
||||
void parseOperatorSection(utils::Parser<> &parser);
|
||||
void parseAxiomSection(utils::Parser<> &parser);
|
||||
|
||||
bool m_usesActionCosts;
|
||||
|
||||
|
@@ -29,7 +29,7 @@ class Effect
|
||||
using Condition = AssignedVariable;
|
||||
using Conditions = AssignedVariables;
|
||||
|
||||
static Effect fromSAS(utils::Parser &parser, const Variables &variables, Conditions &preconditions);
|
||||
static Effect fromSAS(utils::Parser<> &parser, const Variables &variables, Conditions &preconditions);
|
||||
|
||||
public:
|
||||
const Conditions &conditions() const;
|
||||
|
@@ -21,7 +21,7 @@ class Goal
|
||||
using Fact = AssignedVariable;
|
||||
using Facts = AssignedVariables;
|
||||
|
||||
static Goal fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static Goal fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Facts &facts() const;
|
||||
|
@@ -21,7 +21,7 @@ class InitialState
|
||||
using Fact = AssignedVariable;
|
||||
using Facts = AssignedVariables;
|
||||
|
||||
static InitialState fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static InitialState fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Facts &facts() const;
|
||||
|
@@ -28,7 +28,7 @@ class MutexGroup
|
||||
using Fact = AssignedVariable;
|
||||
using Facts = AssignedVariables;
|
||||
|
||||
static MutexGroup fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static MutexGroup fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Facts &facts() const;
|
||||
|
@@ -33,7 +33,7 @@ class Operator
|
||||
using Condition = AssignedVariable;
|
||||
using Conditions = AssignedVariables;
|
||||
|
||||
static Operator fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static Operator fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
void printPredicateAsASP(utils::LogStream &ostream) const;
|
||||
|
@@ -22,7 +22,7 @@ namespace sas
|
||||
class Predicate
|
||||
{
|
||||
public:
|
||||
static Predicate fromSAS(utils::Parser &parser);
|
||||
static Predicate fromSAS(utils::Parser<> &parser);
|
||||
|
||||
using Arguments = std::vector<std::string>;
|
||||
|
||||
|
@@ -39,8 +39,8 @@ struct Value
|
||||
static const Value Any;
|
||||
static const Value None;
|
||||
|
||||
static Value fromSAS(utils::Parser &parser);
|
||||
static const Value &referenceFromSAS(utils::Parser &parser, const Variable &variable);
|
||||
static Value fromSAS(utils::Parser<> &parser);
|
||||
static const Value &referenceFromSAS(utils::Parser<> &parser, const Variable &variable);
|
||||
|
||||
public:
|
||||
Value negated() const;
|
||||
|
@@ -28,8 +28,8 @@ using Variables = std::vector<Variable>;
|
||||
class Variable
|
||||
{
|
||||
public:
|
||||
static Variable fromSAS(utils::Parser &parser);
|
||||
static const Variable &referenceFromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static Variable fromSAS(utils::Parser<> &parser);
|
||||
static const Variable &referenceFromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
void printNameAsASPPredicate(utils::LogStream &outputStream) const;
|
||||
|
@@ -26,7 +26,7 @@ using VariableTransitions = std::vector<VariableTransition>;
|
||||
class VariableTransition
|
||||
{
|
||||
public:
|
||||
static VariableTransition fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
static VariableTransition fromSAS(utils::Parser<> &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Variable &variable() const;
|
||||
|
@@ -4,8 +4,8 @@
|
||||
#include <string>
|
||||
|
||||
#include <plasp/utils/LogStream.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
#include <plasp/utils/StreamCoordinate.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -44,8 +44,8 @@ class Logger
|
||||
void setColorPolicy(LogStream::ColorPolicy colorPolicy);
|
||||
|
||||
void logError(const std::string &message);
|
||||
void logError(const Parser::Coordinate &coordinate, const std::string &message);
|
||||
void logWarning(const Parser &parser, const std::string &message);
|
||||
void logError(const StreamCoordinate &coordinate, const std::string &message);
|
||||
void logWarning(const StreamCoordinate &parserCoordinate, const std::string &message);
|
||||
|
||||
private:
|
||||
LogStream m_outputStream;
|
||||
|
@@ -8,112 +8,192 @@
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <plasp/utils/ParserException.h>
|
||||
#include <plasp/utils/ParserPolicy.h>
|
||||
#include <plasp/utils/ParserTraits.h>
|
||||
#include <plasp/utils/Stream.h>
|
||||
#include <plasp/utils/StreamCoordinate.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
|
||||
template<typename Type>
|
||||
struct Tag
|
||||
{
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Parser
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Parser
|
||||
template<class ParserPolicy = CaseSensitiveParserPolicy>
|
||||
class Parser: public Stream, public ParserPolicy
|
||||
{
|
||||
public:
|
||||
using Position = std::stringstream::pos_type;
|
||||
|
||||
struct Coordinate
|
||||
{
|
||||
std::string sectionName;
|
||||
size_t row;
|
||||
size_t column;
|
||||
};
|
||||
|
||||
struct StreamDelimiter
|
||||
{
|
||||
Position position;
|
||||
std::string sectionName;
|
||||
};
|
||||
|
||||
public:
|
||||
explicit Parser();
|
||||
explicit Parser(std::string streamName, std::istream &istream);
|
||||
|
||||
// Forbid copy construction/assignment
|
||||
Parser(const Parser &other) = delete;
|
||||
Parser &operator=(const Parser &other) = delete;
|
||||
|
||||
Parser(Parser &&other);
|
||||
Parser &operator=(Parser &&other);
|
||||
|
||||
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);
|
||||
bool isCaseSensitive() const noexcept;
|
||||
|
||||
char currentCharacter() const;
|
||||
void advance();
|
||||
bool atEndOfStream() const;
|
||||
template<class OtherParser>
|
||||
Parser(OtherParser &&otherParser)
|
||||
{
|
||||
m_stream = std::move(otherParser.m_stream);
|
||||
m_delimiters = std::move(otherParser.m_delimiters);
|
||||
}
|
||||
|
||||
void removeComments(const std::string &startSequence, const std::string &endSequence, bool removeEnd);
|
||||
|
||||
char currentCharacter() const;
|
||||
|
||||
template<typename Type>
|
||||
Type parse();
|
||||
|
||||
template<class CharacterPredicate, class WhiteSpacePredicate>
|
||||
std::string parseIdentifier(CharacterPredicate characterPredicate, WhiteSpacePredicate whiteSpacePredicate);
|
||||
|
||||
template<class CharacterPredicate>
|
||||
std::string parseIdentifier(CharacterPredicate characterPredicate);
|
||||
template<typename Type>
|
||||
bool testAndReturn(const Type &expectedValue);
|
||||
|
||||
template<typename Type>
|
||||
bool probe(const Type &expectedValue);
|
||||
|
||||
template<class CharacterPredicate>
|
||||
bool probeIdentifier(const std::string &identifier, CharacterPredicate characterPredicate);
|
||||
|
||||
bool probeNumber();
|
||||
bool testAndSkip(const Type &expectedValue);
|
||||
|
||||
template<typename Type>
|
||||
void expect(const Type &expectedValue);
|
||||
|
||||
template<class WhiteSpacePredicate>
|
||||
void skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate);
|
||||
std::string parseIdentifier();
|
||||
bool testIdentifierAndReturn(const std::string &identifier);
|
||||
bool testIdentifierAndSkip(const std::string &identifier);
|
||||
|
||||
// TODO: remove
|
||||
bool probeNumber();
|
||||
|
||||
std::string parseLine();
|
||||
|
||||
void skipWhiteSpace();
|
||||
void skipLine();
|
||||
|
||||
std::string getLine();
|
||||
|
||||
private:
|
||||
static const std::istreambuf_iterator<char> EndOfFile;
|
||||
std::string parseImpl(Tag<std::string>);
|
||||
char parseImpl(Tag<char>);
|
||||
uint64_t parseImpl(Tag<uint64_t>);
|
||||
int64_t parseImpl(Tag<int64_t>);
|
||||
uint32_t parseImpl(Tag<uint32_t>);
|
||||
int32_t parseImpl(Tag<int32_t>);
|
||||
bool parseImpl(Tag<bool>);
|
||||
|
||||
private:
|
||||
void checkStream() const;
|
||||
bool testImpl(const std::string &expectedValue);
|
||||
bool testImpl(char expectedValue);
|
||||
bool testImpl(uint64_t expectedValue);
|
||||
bool testImpl(int64_t expectedValue);
|
||||
bool testImpl(uint32_t expectedValue);
|
||||
bool testImpl(int32_t expectedValue);
|
||||
bool testImpl(bool expectedValue);
|
||||
|
||||
uint64_t parseIntegerBody();
|
||||
|
||||
mutable std::stringstream m_stream;
|
||||
|
||||
std::vector<StreamDelimiter> m_streamDelimiters;
|
||||
|
||||
bool m_isCaseSensitive;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class CharacterPredicate, class WhiteSpacePredicate>
|
||||
std::string Parser::parseIdentifier(CharacterPredicate characterPredicate, WhiteSpacePredicate whiteSpacePredicate)
|
||||
template<class ParserPolicy>
|
||||
Parser<ParserPolicy>::Parser()
|
||||
: Stream()
|
||||
{
|
||||
skipWhiteSpace(whiteSpacePredicate);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
Parser<ParserPolicy>::Parser(std::string streamName, std::istream &istream)
|
||||
: Stream(streamName, istream)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::skipWhiteSpace()
|
||||
{
|
||||
check();
|
||||
|
||||
while (!atEnd() && ParserPolicy::isWhiteSpace(currentCharacter()))
|
||||
advance();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::skipLine()
|
||||
{
|
||||
check();
|
||||
|
||||
while (currentCharacter() != '\n')
|
||||
advance();
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
Type Parser<ParserPolicy>::parse()
|
||||
{
|
||||
return parseImpl(Tag<Type>());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
bool Parser<ParserPolicy>::testAndReturn(const Type &expectedValue)
|
||||
{
|
||||
const auto previousPosition = position();
|
||||
|
||||
const auto result = testImpl(expectedValue);
|
||||
|
||||
seek(previousPosition);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
bool Parser<ParserPolicy>::testAndSkip(const Type &expectedValue)
|
||||
{
|
||||
const auto previousPosition = position();
|
||||
|
||||
const auto result = testImpl(expectedValue);
|
||||
|
||||
if (result == false)
|
||||
seek(previousPosition);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
template<typename Type>
|
||||
void Parser<ParserPolicy>::expect(const Type &expectedValue)
|
||||
{
|
||||
if (testAndSkip(expectedValue))
|
||||
return;
|
||||
|
||||
std::stringstream message;
|
||||
message << "unexpected value, expected “" << expectedValue << "”";
|
||||
|
||||
throw ParserException(coordinate(), message.str());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
std::string Parser<ParserPolicy>::parseIdentifier()
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
std::string value;
|
||||
|
||||
@@ -121,7 +201,7 @@ std::string Parser::parseIdentifier(CharacterPredicate characterPredicate, White
|
||||
{
|
||||
const auto character = currentCharacter();
|
||||
|
||||
if (!characterPredicate(character))
|
||||
if (!ParserPolicy::isIdentifierCharacter(character))
|
||||
return value;
|
||||
|
||||
value.push_back(character);
|
||||
@@ -131,33 +211,342 @@ std::string Parser::parseIdentifier(CharacterPredicate characterPredicate, White
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class CharacterPredicate>
|
||||
std::string Parser::parseIdentifier(CharacterPredicate characterPredicate)
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testIdentifierAndSkip(const std::string &expectedValue)
|
||||
{
|
||||
return parseIdentifier(characterPredicate,
|
||||
[&](const auto character)
|
||||
return testAndSkip(expectedValue) && !ParserPolicy::isIdentifierCharacter(currentCharacter());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::probeNumber()
|
||||
{
|
||||
const auto previousPosition = position();
|
||||
|
||||
skipWhiteSpace();
|
||||
|
||||
while (!ParserPolicy::isWhiteSpace(currentCharacter()))
|
||||
if (!std::isdigit(currentCharacter()))
|
||||
{
|
||||
return std::isspace(character);
|
||||
});
|
||||
seek(previousPosition);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class CharacterPredicate>
|
||||
bool Parser::probeIdentifier(const std::string &expectedValue, CharacterPredicate characterPredicate)
|
||||
template<class ParserPolicy>
|
||||
std::string Parser<ParserPolicy>::parseLine()
|
||||
{
|
||||
return probe<std::string>(expectedValue) && !characterPredicate(currentCharacter());
|
||||
}
|
||||
std::string value;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class WhiteSpacePredicate>
|
||||
void Parser::skipWhiteSpace(WhiteSpacePredicate whiteSpacePredicate)
|
||||
{
|
||||
checkStream();
|
||||
|
||||
while (!atEndOfStream() && whiteSpacePredicate(currentCharacter()))
|
||||
while (true)
|
||||
{
|
||||
const auto character = currentCharacter();
|
||||
advance();
|
||||
|
||||
if (character == '\n')
|
||||
break;
|
||||
else if (character == '\r')
|
||||
continue;
|
||||
|
||||
value.push_back(character);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::removeComments(const std::string &startSequence, const std::string &endSequence, bool removeEnd)
|
||||
{
|
||||
const auto inPosition = m_stream.tellg();
|
||||
const auto outPosition = m_stream.tellp();
|
||||
|
||||
m_stream.seekg(0);
|
||||
|
||||
const auto removeRange =
|
||||
[&](const auto &start, const auto &end)
|
||||
{
|
||||
BOOST_ASSERT(start != -1);
|
||||
|
||||
m_stream.clear();
|
||||
m_stream.seekp(start);
|
||||
m_stream.seekg(start);
|
||||
|
||||
auto position = start;
|
||||
|
||||
while (end == -1 || position < end)
|
||||
{
|
||||
m_stream.ignore(1);
|
||||
|
||||
if (atEnd())
|
||||
return;
|
||||
|
||||
m_stream.put(' ');
|
||||
position += static_cast<std::streamoff>(1);
|
||||
}
|
||||
};
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
Position startPosition = m_stream.tellg();
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
startPosition = m_stream.tellg();
|
||||
|
||||
if (testAndSkip(startSequence))
|
||||
break;
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
Position endPosition = m_stream.tellg();
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
endPosition = m_stream.tellg();
|
||||
|
||||
if (testAndSkip(endSequence))
|
||||
break;
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
if (removeEnd)
|
||||
endPosition = m_stream.tellg();
|
||||
|
||||
removeRange(startPosition, endPosition);
|
||||
}
|
||||
|
||||
m_stream.clear();
|
||||
|
||||
m_stream.seekg(inPosition);
|
||||
m_stream.seekp(outPosition);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
char Parser<ParserPolicy>::currentCharacter() const
|
||||
{
|
||||
return ParserPolicy::transformCharacter(Stream::currentCharacter());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
std::string Parser<ParserPolicy>::parseImpl(Tag<std::string>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
const auto startPosition = position();
|
||||
|
||||
while (!ParserPolicy::isWhiteSpace(currentCharacter()))
|
||||
advance();
|
||||
|
||||
const auto endPosition = position();
|
||||
const auto length = static_cast<size_t>(endPosition - startPosition);
|
||||
|
||||
std::string value;
|
||||
value.reserve(length);
|
||||
|
||||
seek(startPosition);
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
value.push_back(currentCharacter());
|
||||
advance();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
char Parser<ParserPolicy>::parseImpl(Tag<char>)
|
||||
{
|
||||
const auto value = currentCharacter();
|
||||
|
||||
advance();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
uint64_t Parser<ParserPolicy>::parseIntegerBody()
|
||||
{
|
||||
check();
|
||||
|
||||
if (!std::isdigit(currentCharacter()))
|
||||
throw ParserException(coordinate(), "could not parse integer value");
|
||||
|
||||
uint64_t value = 0;
|
||||
|
||||
while (!atEnd())
|
||||
{
|
||||
const auto character = currentCharacter();
|
||||
|
||||
if (!std::isdigit(character))
|
||||
break;
|
||||
|
||||
value *= 10;
|
||||
value += character - '0';
|
||||
|
||||
advance();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
int64_t Parser<ParserPolicy>::parseImpl(Tag<int64_t>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
bool positive = testAndSkip<char>('+') || !testAndSkip<char>('-');
|
||||
|
||||
const auto value = parseIntegerBody();
|
||||
|
||||
return (positive ? value : -value);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
uint64_t Parser<ParserPolicy>::parseImpl(Tag<uint64_t>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
if (currentCharacter() == '-')
|
||||
throw ParserException(coordinate(), "expected unsigned integer, got signed one");
|
||||
|
||||
return parseIntegerBody();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
int32_t Parser<ParserPolicy>::parseImpl(Tag<int32_t>)
|
||||
{
|
||||
return static_cast<int32_t>(parseImpl(Tag<int64_t>()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
uint32_t Parser<ParserPolicy>::parseImpl(Tag<uint32_t>)
|
||||
{
|
||||
return static_cast<uint32_t>(parseImpl(Tag<uint64_t>()));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::parseImpl(Tag<bool>)
|
||||
{
|
||||
skipWhiteSpace();
|
||||
|
||||
if (testAndSkip<char>('0'))
|
||||
return false;
|
||||
|
||||
if (testAndSkip<char>('1'))
|
||||
return true;
|
||||
|
||||
throw ParserException(coordinate(), "could not parse Boolean value");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(const std::string &expectedValue)
|
||||
{
|
||||
if (!ParserPolicy::isWhiteSpace(expectedValue.front()))
|
||||
skipWhiteSpace();
|
||||
|
||||
const auto match = std::find_if(expectedValue.cbegin(), expectedValue.cend(),
|
||||
[&](const auto &expectedCharacter)
|
||||
{
|
||||
const auto character = static_cast<char>(this->currentCharacter());
|
||||
|
||||
if (character != expectedCharacter)
|
||||
return true;
|
||||
|
||||
this->advance();
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return (match == expectedValue.cend());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(char expectedValue)
|
||||
{
|
||||
const auto result = (currentCharacter() == expectedValue);
|
||||
|
||||
advance();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(int64_t expectedValue)
|
||||
{
|
||||
const auto value = parseImpl(Tag<int64_t>());
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(uint64_t expectedValue)
|
||||
{
|
||||
const auto value = parseImpl(Tag<uint64_t>());
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(int32_t expectedValue)
|
||||
{
|
||||
return testImpl(static_cast<int64_t>(expectedValue));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(uint32_t expectedValue)
|
||||
{
|
||||
return testImpl(static_cast<uint64_t>(expectedValue));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
bool Parser<ParserPolicy>::testImpl(bool expectedValue)
|
||||
{
|
||||
const auto value = parseImpl(Tag<bool>());
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
#include <plasp/utils/Parser.h>
|
||||
#include <plasp/utils/StreamCoordinate.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -20,18 +20,18 @@ namespace utils
|
||||
class ParserException: public std::exception
|
||||
{
|
||||
public:
|
||||
explicit ParserException(const utils::Parser &parser)
|
||||
: ParserException(parser, "unspecified parser error")
|
||||
explicit ParserException(const StreamCoordinate &coordinate)
|
||||
: ParserException(coordinate, "unspecified parser error")
|
||||
{
|
||||
}
|
||||
|
||||
explicit ParserException(const utils::Parser &parser, const char *message)
|
||||
: ParserException(parser, static_cast<std::string>(message))
|
||||
explicit ParserException(const StreamCoordinate &coordinate, const char *message)
|
||||
: ParserException(coordinate, static_cast<std::string>(message))
|
||||
{
|
||||
}
|
||||
|
||||
explicit ParserException(const utils::Parser &parser, const std::string &message)
|
||||
: m_coordinate{parser.coordinate()},
|
||||
explicit ParserException(const StreamCoordinate &coordinate, const std::string &message)
|
||||
: m_coordinate{coordinate},
|
||||
m_message{message},
|
||||
m_plainMessage{m_coordinate.sectionName + ":" + std::to_string(m_coordinate.row)
|
||||
+ ":" + std::to_string(m_coordinate.column) + " " + m_message}
|
||||
@@ -47,7 +47,7 @@ class ParserException: public std::exception
|
||||
return m_plainMessage.c_str();
|
||||
}
|
||||
|
||||
const Parser::Coordinate &coordinate() const
|
||||
const StreamCoordinate &coordinate() const
|
||||
{
|
||||
return m_coordinate;
|
||||
}
|
||||
@@ -58,7 +58,7 @@ class ParserException: public std::exception
|
||||
}
|
||||
|
||||
private:
|
||||
Parser::Coordinate m_coordinate;
|
||||
StreamCoordinate m_coordinate;
|
||||
std::string m_message;
|
||||
std::string m_plainMessage;
|
||||
};
|
||||
|
62
include/plasp/utils/ParserPolicy.h
Normal file
62
include/plasp/utils/ParserPolicy.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef __PLASP__UTILS__PARSER_POLICY_H
|
||||
#define __PLASP__UTILS__PARSER_POLICY_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ParserPolicy
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class CaseSensitiveParserPolicy
|
||||
{
|
||||
public:
|
||||
static constexpr char transformCharacter(char c) noexcept
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
static bool isWhiteSpace(char c)
|
||||
{
|
||||
return std::iswspace(c);
|
||||
}
|
||||
|
||||
static bool isIdentifierCharacter(char c)
|
||||
{
|
||||
return std::isgraph(c);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class CaseInsensitiveParserPolicy
|
||||
{
|
||||
public:
|
||||
static char transformCharacter(char c) noexcept
|
||||
{
|
||||
return std::tolower(c);
|
||||
}
|
||||
|
||||
static bool isWhiteSpace(char c)
|
||||
{
|
||||
return std::iswspace(c);
|
||||
}
|
||||
|
||||
static bool isIdentifierCharacter(char c)
|
||||
{
|
||||
return std::isgraph(c);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
363
include/plasp/utils/ParserTraits.h
Normal file
363
include/plasp/utils/ParserTraits.h
Normal file
@@ -0,0 +1,363 @@
|
||||
#ifndef __PLASP__UTILS__PARSER_TRAITS_H
|
||||
#define __PLASP__UTILS__PARSER_TRAITS_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
/*
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ParserTraits
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy, typename Type>
|
||||
class ParserTraits
|
||||
{
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, std::string>
|
||||
{
|
||||
public:
|
||||
static std::string parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const std::string &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const std::string &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const std::string &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const std::string &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, char>
|
||||
{
|
||||
public:
|
||||
static char parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const char &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const char &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const char &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const char &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, uint64_t>
|
||||
{
|
||||
public:
|
||||
static uint64_t parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const uint64_t &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const uint64_t &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const uint64_t &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const uint64_t &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, int64_t>
|
||||
{
|
||||
public:
|
||||
static int64_t parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const int64_t &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const int64_t &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const int64_t &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const int64_t &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, uint32_t>
|
||||
{
|
||||
public:
|
||||
static uint32_t parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const uint32_t &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const uint32_t &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const uint32_t &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const uint32_t &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, int32_t>
|
||||
{
|
||||
public:
|
||||
static int32_t parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const int32_t &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const int32_t &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const int32_t &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const int32_t &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
class ParserTraits<ParserPolicy, bool>
|
||||
{
|
||||
public:
|
||||
static bool parse(Parser<ParserPolicy> &parser);
|
||||
static bool testAndReturn(Parser<ParserPolicy> &parser, const bool &value);
|
||||
static bool testAndSkip(Parser<ParserPolicy> &parser, const bool &value);
|
||||
static void expect(Parser<ParserPolicy> &parser, const bool &expectedValue);
|
||||
static bool test(Parser<ParserPolicy> &parser, const bool &value);
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser, typename Type>
|
||||
bool ParserTraits<Parser, Type>::testAndReturn(Parser &parser, const Type &value)
|
||||
{
|
||||
const auto position = parser.position();
|
||||
|
||||
const auto result = test(parser, value);
|
||||
|
||||
parser.seek(position);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser, typename Type>
|
||||
bool ParserTraits<Parser, Type>::testAndSkip(Parser &parser, const Type &value)
|
||||
{
|
||||
const auto position = parser.position();
|
||||
|
||||
const auto result = test(parser, value);
|
||||
|
||||
if (result == false)
|
||||
parser.seek(position);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class ParserPolicy>
|
||||
void Parser<ParserPolicy>::expect(const Type &expectedValue)
|
||||
{
|
||||
if (testAndSkip(parser, expectedValue))
|
||||
return;
|
||||
|
||||
std::stringstream message;
|
||||
message << "unexpected value, expected “" << expectedValue << "”";
|
||||
|
||||
throw ParserException(parser.coordinate(), message.str());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
std::string ParserTraits<Parser, std::string>::parse(Parser &parser)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
const auto startPosition = parser.position();
|
||||
|
||||
while (!parser.isWhiteSpace(parser.currentCharacter()))
|
||||
parser.advance();
|
||||
|
||||
const auto endPosition = parser.position();
|
||||
const auto length = static_cast<size_t>(endPosition - startPosition);
|
||||
|
||||
std::string value;
|
||||
value.reserve(length);
|
||||
|
||||
parser.seek(startPosition);
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
value.push_back(parser.currentCharacter());
|
||||
parser.advance();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
char ParserTraits<Parser, char>::parse(Parser &parser)
|
||||
{
|
||||
const auto value = parser.currentCharacter();
|
||||
|
||||
parser.advance();
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
uint64_t parseIntegerBody(Parser &parser)
|
||||
{
|
||||
parser.checkStream();
|
||||
|
||||
if (!std::isdigit(parser.currentCharacter()))
|
||||
throw ParserException(parser.coordinate(), "could not parse integer value");
|
||||
|
||||
uint64_t value = 0;
|
||||
|
||||
while (!parser.atEndOfStream())
|
||||
{
|
||||
const auto character = parser.currentCharacter();
|
||||
|
||||
if (!std::isdigit(character))
|
||||
break;
|
||||
|
||||
value *= 10;
|
||||
value += character - '0';
|
||||
|
||||
parser.advance();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
int64_t ParserTraits<Parser, int64_t>::parse(Parser &parser)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
bool positive = parser.template testAndSkip<char>('+') || !parser.template testAndSkip<char>('-');
|
||||
|
||||
const auto value = parseIntegerBody(parser);
|
||||
|
||||
return (positive ? value : -value);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
uint64_t ParserTraits<Parser, uint64_t>::parse(Parser &parser)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
if (parser.currentCharacter() == '-')
|
||||
throw ParserException(parser.coordinate(), "expected unsigned integer, got signed one");
|
||||
|
||||
return parseIntegerBody(parser);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
int32_t ParserTraits<Parser, int32_t>::parse(Parser &parser)
|
||||
{
|
||||
return static_cast<int32_t>(ParserTraits<Parser, int64_t>::parse(parser));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
uint32_t ParserTraits<Parser, uint32_t>::parse(Parser &parser)
|
||||
{
|
||||
return static_cast<uint32_t>(ParserTraits<Parser, uint64_t>::parse(parser));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, bool>::parse(Parser &parser)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
if (parser.template testAndSkip<char>('0'))
|
||||
return false;
|
||||
|
||||
if (parser.template testAndSkip<char>('1'))
|
||||
return true;
|
||||
|
||||
throw ParserException(parser.coordinate(), "could not parse Boolean value");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, std::string>::test(Parser &parser, const std::string &expectedValue)
|
||||
{
|
||||
if (!parser.isWhiteSpace(expectedValue.front()))
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
const auto match = std::find_if(expectedValue.cbegin(), expectedValue.cend(),
|
||||
[&parser](const auto &expectedCharacter)
|
||||
{
|
||||
const auto character = static_cast<char>(parser.currentCharacter());
|
||||
|
||||
if (character != expectedCharacter)
|
||||
return true;
|
||||
|
||||
parser.advance();
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return (match == expectedValue.cend());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, char>::test(Parser &parser, const char &expectedValue)
|
||||
{
|
||||
const auto result = (parser.currentCharacter() == expectedValue);
|
||||
|
||||
parser.advance();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, int64_t>::test(Parser &parser, const int64_t &expectedValue)
|
||||
{
|
||||
const auto value = parse(parser);
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, uint64_t>::test(Parser &parser, const uint64_t &expectedValue)
|
||||
{
|
||||
const auto value = parse(parser);
|
||||
|
||||
return (value == expectedValue);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, int32_t>::test(Parser &parser, const int32_t &expectedValue)
|
||||
{
|
||||
return ParserTraits<Parser, int64_t>::test(parser, static_cast<int64_t>(expectedValue));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class Parser>
|
||||
bool ParserTraits<Parser, uint32_t>::test(Parser &parser, const uint32_t &expectedValue)
|
||||
{
|
||||
return ParserTraits<Parser, uint64_t>::test(parser, static_cast<uint64_t>(expectedValue));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
85
include/plasp/utils/Stream.h
Normal file
85
include/plasp/utils/Stream.h
Normal file
@@ -0,0 +1,85 @@
|
||||
#ifndef __PLASP__UTILS__STREAM_H
|
||||
#define __PLASP__UTILS__STREAM_H
|
||||
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <plasp/utils/ParserException.h>
|
||||
#include <plasp/utils/StreamCoordinate.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Stream
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
using Position = std::stringstream::pos_type;
|
||||
|
||||
struct Delimiter
|
||||
{
|
||||
Position position;
|
||||
std::string sectionName;
|
||||
};
|
||||
|
||||
public:
|
||||
explicit Stream();
|
||||
explicit Stream(std::string streamName, std::istream &istream);
|
||||
|
||||
Stream(const Stream &other) = delete;
|
||||
Stream &operator=(const Stream &other) = delete;
|
||||
|
||||
Stream(Stream &&other)
|
||||
: m_stream{std::move(other.m_stream)},
|
||||
m_delimiters{std::move(other.m_delimiters)}
|
||||
{
|
||||
}
|
||||
|
||||
Stream &operator=(Stream &&other)
|
||||
{
|
||||
m_stream = std::move(other.m_stream);
|
||||
m_delimiters = std::move(other.m_delimiters);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Stream() = default;
|
||||
|
||||
void read(std::string streamName, std::istream &istream);
|
||||
void read(const boost::filesystem::path &path);
|
||||
|
||||
void reset();
|
||||
void seek(Position position);
|
||||
Position position() const;
|
||||
StreamCoordinate coordinate() const;
|
||||
|
||||
char currentCharacter() const;
|
||||
void advance();
|
||||
bool atEnd() const;
|
||||
|
||||
void check() const;
|
||||
|
||||
// TODO: make protected again
|
||||
//protected:
|
||||
mutable std::stringstream m_stream;
|
||||
|
||||
std::vector<Delimiter> m_delimiters;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@@ -1,28 +1,25 @@
|
||||
#ifndef __PLASP__PDDL__IDENTIFIER_H
|
||||
#define __PLASP__PDDL__IDENTIFIER_H
|
||||
#ifndef __PLASP__UTILS__STREAM_COORDINATE_H
|
||||
#define __PLASP__UTILS__STREAM_COORDINATE_H
|
||||
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
namespace utils
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Identifier
|
||||
// StreamCoordinate
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const auto isIdentifier =
|
||||
[](const auto character)
|
||||
{
|
||||
return character != '?'
|
||||
&& character != '('
|
||||
&& character != ')'
|
||||
&& character != ';'
|
||||
&& std::isgraph(character);
|
||||
};
|
||||
struct StreamCoordinate
|
||||
{
|
||||
std::string sectionName;
|
||||
size_t row;
|
||||
size_t column;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
Reference in New Issue
Block a user