Refactored basic parsing.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <plasp/sas/Value.h>
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -26,8 +27,8 @@ using AssignedVariables = std::vector<AssignedVariable>;
|
||||
class AssignedVariable
|
||||
{
|
||||
public:
|
||||
static AssignedVariable fromSAS(std::istream &istream, const Variables &variables);
|
||||
static AssignedVariable fromSAS(std::istream &istream, 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);
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <plasp/sas/AssignedVariable.h>
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -28,7 +29,7 @@ class AxiomRule
|
||||
using Condition = AssignedVariable;
|
||||
using Conditions = AssignedVariables;
|
||||
|
||||
static AxiomRule fromSAS(std::istream &istream, const Variables &variables);
|
||||
static AxiomRule fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Conditions &conditions() const;
|
||||
|
@@ -13,6 +13,7 @@
|
||||
#include <plasp/sas/MutexGroup.h>
|
||||
#include <plasp/sas/Operator.h>
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -46,14 +47,14 @@ class Description
|
||||
private:
|
||||
Description();
|
||||
|
||||
void parseVersionSection(std::istream &istream) const;
|
||||
void parseMetricSection(std::istream &istream);
|
||||
void parseVariablesSection(std::istream &istream);
|
||||
void parseMutexSection(std::istream &istream);
|
||||
void parseInitialStateSection(std::istream &istream);
|
||||
void parseGoalSection(std::istream &istream);
|
||||
void parseOperatorSection(std::istream &istream);
|
||||
void parseAxiomSection(std::istream &istream);
|
||||
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;
|
||||
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <plasp/sas/AssignedVariable.h>
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -28,7 +29,7 @@ class Effect
|
||||
using Condition = AssignedVariable;
|
||||
using Conditions = AssignedVariables;
|
||||
|
||||
static Effect fromSAS(std::istream &istream, const Variables &variables, Conditions &preconditions);
|
||||
static Effect fromSAS(utils::Parser &parser, const Variables &variables, Conditions &preconditions);
|
||||
|
||||
public:
|
||||
const Conditions &conditions() const;
|
||||
|
@@ -2,6 +2,7 @@
|
||||
#define __PLASP__SAS__GOAL_H
|
||||
|
||||
#include <plasp/sas/AssignedVariable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -20,7 +21,7 @@ class Goal
|
||||
using Fact = AssignedVariable;
|
||||
using Facts = AssignedVariables;
|
||||
|
||||
static Goal fromSAS(std::istream &istream, const Variables &variables);
|
||||
static Goal fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Facts &facts() const;
|
||||
|
@@ -2,6 +2,7 @@
|
||||
#define __PLASP__SAS__INITIAL_STATE_H
|
||||
|
||||
#include <plasp/sas/AssignedVariable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -20,7 +21,7 @@ class InitialState
|
||||
using Fact = AssignedVariable;
|
||||
using Facts = AssignedVariables;
|
||||
|
||||
static InitialState fromSAS(std::istream &istream, const Variables &variables);
|
||||
static InitialState fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Facts &facts() const;
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include <plasp/sas/AssignedVariable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -27,7 +28,7 @@ class MutexGroup
|
||||
using Fact = AssignedVariable;
|
||||
using Facts = AssignedVariables;
|
||||
|
||||
static MutexGroup fromSAS(std::istream &istream, const Variables &variables);
|
||||
static MutexGroup fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Facts &facts() const;
|
||||
|
@@ -8,6 +8,7 @@
|
||||
#include <plasp/sas/Effect.h>
|
||||
#include <plasp/sas/Predicate.h>
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -31,7 +32,7 @@ class Operator
|
||||
using Condition = AssignedVariable;
|
||||
using Conditions = AssignedVariables;
|
||||
|
||||
static Operator fromSAS(std::istream &istream, const Variables &variables);
|
||||
static Operator fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
void printPredicateAsASP(std::ostream &ostream) const;
|
||||
|
@@ -5,6 +5,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace sas
|
||||
@@ -19,7 +21,7 @@ namespace sas
|
||||
class Predicate
|
||||
{
|
||||
public:
|
||||
static Predicate fromSAS(std::istream &istream);
|
||||
static Predicate fromSAS(utils::Parser &parser);
|
||||
|
||||
using Arguments = std::vector<std::string>;
|
||||
|
||||
|
@@ -5,6 +5,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace sas
|
||||
@@ -36,8 +38,8 @@ struct Value
|
||||
static const Value Any;
|
||||
static const Value None;
|
||||
|
||||
static Value fromSAS(std::istream &istream);
|
||||
static const Value &referenceFromSAS(std::istream &istream, const Variable &variable);
|
||||
static Value fromSAS(utils::Parser &parser);
|
||||
static const Value &referenceFromSAS(utils::Parser &parser, const Variable &variable);
|
||||
|
||||
public:
|
||||
Value negated() const;
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include <plasp/sas/Value.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -26,8 +27,8 @@ using Variables = std::vector<Variable>;
|
||||
class Variable
|
||||
{
|
||||
public:
|
||||
static Variable fromSAS(std::istream &istream);
|
||||
static const Variable &referenceFromSAS(std::istream &istream, const Variables &variables);
|
||||
static Variable fromSAS(utils::Parser &parser);
|
||||
static const Variable &referenceFromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
void printNameAsASPPredicate(std::ostream &ostream) const;
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <plasp/sas/Value.h>
|
||||
#include <plasp/sas/Variable.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -25,7 +26,7 @@ using VariableTransitions = std::vector<VariableTransition>;
|
||||
class VariableTransition
|
||||
{
|
||||
public:
|
||||
static VariableTransition fromSAS(std::istream &istream, const Variables &variables);
|
||||
static VariableTransition fromSAS(utils::Parser &parser, const Variables &variables);
|
||||
|
||||
public:
|
||||
const Variable &variable() const;
|
||||
|
46
include/plasp/utils/IO.h
Normal file
46
include/plasp/utils/IO.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef __PLASP__UTILS__IO_H
|
||||
#define __PLASP__UTILS__IO_H
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// IO
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline std::string escapeASP(const std::string &string)
|
||||
{
|
||||
auto escaped = string;
|
||||
|
||||
boost::replace_all(escaped, "_", "__");
|
||||
boost::replace_all(escaped, "-", "_h");
|
||||
boost::replace_all(escaped, "@", "_a");
|
||||
|
||||
return escaped;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline std::string unescapeASP(const std::string &string)
|
||||
{
|
||||
auto unescaped = string;
|
||||
|
||||
boost::replace_all(unescaped, "_a", "@");
|
||||
boost::replace_all(unescaped, "_h", "-");
|
||||
boost::replace_all(unescaped, "__", "_");
|
||||
|
||||
return unescaped;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
62
include/plasp/utils/Parser.h
Normal file
62
include/plasp/utils/Parser.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef __PLASP__UTILS__PARSER_H
|
||||
#define __PLASP__UTILS__PARSER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Parser
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Parser
|
||||
{
|
||||
public:
|
||||
Parser(std::istream &istream);
|
||||
|
||||
size_t row() const;
|
||||
size_t column() const;
|
||||
|
||||
template<typename T>
|
||||
T parse();
|
||||
|
||||
template<typename T>
|
||||
void expect(const T &expectedValue);
|
||||
|
||||
void skipWhiteSpace();
|
||||
void skipLine();
|
||||
|
||||
std::string getLine();
|
||||
|
||||
private:
|
||||
static const std::istream_iterator<unsigned char> EndOfFile;
|
||||
|
||||
private:
|
||||
void checkStream() const;
|
||||
void advance();
|
||||
|
||||
bool advanceIf(unsigned char expectedCharacter);
|
||||
|
||||
uint64_t parseIntegerBody();
|
||||
|
||||
std::istream &m_istream;
|
||||
std::istream_iterator<unsigned char> m_position;
|
||||
|
||||
size_t m_row;
|
||||
size_t m_column;
|
||||
|
||||
bool m_endOfFile;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@@ -18,17 +18,18 @@ namespace utils
|
||||
class ParserException: public std::exception
|
||||
{
|
||||
public:
|
||||
explicit ParserException()
|
||||
explicit ParserException(size_t row, size_t column)
|
||||
: ParserException(row, column, "Unspecified parser error")
|
||||
{
|
||||
}
|
||||
|
||||
explicit ParserException(const char *message)
|
||||
: m_message(message)
|
||||
explicit ParserException(size_t row, size_t column, const char *message)
|
||||
: ParserException(row, column, static_cast<std::string>(message))
|
||||
{
|
||||
}
|
||||
|
||||
explicit ParserException(const std::string &message)
|
||||
: m_message(message)
|
||||
explicit ParserException(size_t row, size_t column, const std::string &message)
|
||||
: m_message{std::to_string(row) + ":" + std::to_string(column) + "\t" + message}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -39,7 +40,7 @@ class ParserException: public std::exception
|
||||
const char *what() const throw()
|
||||
{
|
||||
if (m_message.empty())
|
||||
return "Unspecified error while parsing SAS description file";
|
||||
return "Unspecified parser error";
|
||||
|
||||
return m_message.c_str();
|
||||
}
|
||||
|
@@ -1,91 +0,0 @@
|
||||
#ifndef __PLASP__UTILS__PARSING_H
|
||||
#define __PLASP__UTILS__PARSING_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <exception>
|
||||
#include <iosfwd>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace utils
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Parsing
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class T>
|
||||
T parse(std::istream &istream)
|
||||
{
|
||||
T value;
|
||||
|
||||
try
|
||||
{
|
||||
istream >> value;
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
throw ParserException(std::string("Could not parse value of type ") + typeid(T).name() + " (" + e.what() + ")");
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class T>
|
||||
void parseExpected(std::istream &istream, const T &expectedValue)
|
||||
{
|
||||
const auto value = parse<T>(istream);
|
||||
|
||||
if (value == expectedValue)
|
||||
return;
|
||||
|
||||
std::stringstream errorStream;
|
||||
|
||||
errorStream << "Invalid format, expected " << expectedValue << ", got " + value;
|
||||
|
||||
throw utils::ParserException(errorStream.str());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline std::string escapeASP(const std::string &string)
|
||||
{
|
||||
auto escaped = string;
|
||||
|
||||
boost::replace_all(escaped, "_", "__");
|
||||
boost::replace_all(escaped, "-", "_h");
|
||||
boost::replace_all(escaped, "@", "_a");
|
||||
|
||||
return escaped;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline std::string unescapeASP(const std::string &string)
|
||||
{
|
||||
auto unescaped = string;
|
||||
|
||||
boost::replace_all(unescaped, "_a", "@");
|
||||
boost::replace_all(unescaped, "_h", "-");
|
||||
boost::replace_all(unescaped, "__", "_");
|
||||
|
||||
return unescaped;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user