Renamed “pddlparse” library to simply “pddl”.

This commit is contained in:
2017-08-09 17:52:50 +02:00
parent a24ce91acb
commit 9199b68080
120 changed files with 479 additions and 479 deletions

504
lib/pddl/include/pddl/AST.h Normal file
View File

@@ -0,0 +1,504 @@
#ifndef __PDDL__AST_H
#define __PDDL__AST_H
#include <limits>
#include <experimental/optional>
#include <set>
#include <type_traits>
#include <vector>
#include <pddl/ASTForward.h>
namespace pddl
{
namespace ast
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AST
//
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Constant
{
explicit Constant(ConstantDeclaration *declaration)
: declaration{declaration}
{
}
Constant(const Constant &other) = delete;
Constant &operator=(const Constant &&other) = delete;
Constant(Constant &&other) = default;
Constant &operator=(Constant &&other) = default;
ConstantDeclaration *declaration;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct ConstantDeclaration
{
explicit ConstantDeclaration(std::string &&name, std::experimental::optional<Type> &&type = std::experimental::nullopt)
: name{std::move(name)},
type{std::move(type)}
{
}
ConstantDeclaration(const ConstantDeclaration &other) = delete;
ConstantDeclaration &operator=(const ConstantDeclaration &&other) = delete;
ConstantDeclaration(ConstantDeclaration &&other) = delete;
ConstantDeclaration &operator=(ConstantDeclaration &&other) = delete;
std::string name;
// TODO: check whether “either” types should actually be allowed at all
std::experimental::optional<Type> type;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PrimitiveType
{
explicit PrimitiveType(PrimitiveTypeDeclaration *declaration)
: declaration{declaration}
{
}
PrimitiveType(const PrimitiveType &other) = delete;
PrimitiveType &operator=(const PrimitiveType &&other) = delete;
PrimitiveType(PrimitiveType &&other) = default;
PrimitiveType &operator=(PrimitiveType &&other) = default;
PrimitiveTypeDeclaration *declaration;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PrimitiveTypeDeclaration
{
explicit PrimitiveTypeDeclaration(std::string &&name)
: name{std::move(name)}
{
}
PrimitiveTypeDeclaration(const PrimitiveTypeDeclaration &other) = delete;
PrimitiveTypeDeclaration &operator=(const PrimitiveTypeDeclaration &&other) = delete;
PrimitiveTypeDeclaration(PrimitiveTypeDeclaration &&other) = delete;
PrimitiveTypeDeclaration &operator=(PrimitiveTypeDeclaration &&other) = delete;
std::string name;
std::vector<PrimitiveTypePointer> parentTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Variable
{
explicit Variable(VariableDeclaration *declaration)
: declaration{declaration}
{
}
Variable(const Variable &other) = delete;
Variable &operator=(const Variable &&other) = delete;
Variable(Variable &&other) = default;
Variable &operator=(Variable &&other) = default;
VariableDeclaration *declaration;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct VariableDeclaration
{
explicit VariableDeclaration(std::string &&name, std::experimental::optional<Type> type = std::experimental::nullopt)
: name{std::move(name)},
type{std::move(type)}
{
}
VariableDeclaration(const VariableDeclaration &other) = delete;
VariableDeclaration &operator=(const VariableDeclaration &&other) = delete;
VariableDeclaration(VariableDeclaration &&other) = delete;
VariableDeclaration &operator=(VariableDeclaration &&other) = delete;
std::string name;
std::experimental::optional<Type> type;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compounds
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Predicate
{
using Arguments = Terms;
explicit Predicate(Arguments &&arguments, PredicateDeclaration *declaration)
: arguments{std::move(arguments)},
declaration{declaration}
{
}
Predicate(const Predicate &other) = delete;
Predicate &operator=(const Predicate &&other) = delete;
Predicate(Predicate &&other) = default;
Predicate &operator=(Predicate &&other) = default;
Arguments arguments;
PredicateDeclaration *declaration;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PredicateDeclaration
{
explicit PredicateDeclaration(std::string &&name, VariableDeclarations &&parameters)
: name{std::move(name)},
parameters{std::move(parameters)}
{
}
PredicateDeclaration(const PredicateDeclaration &other) = delete;
PredicateDeclaration &operator=(const PredicateDeclaration &&other) = delete;
PredicateDeclaration(PredicateDeclaration &&other) = default;
PredicateDeclaration &operator=(PredicateDeclaration &&other) = default;
std::string name;
VariableDeclarations parameters;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions: Base Classes
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class ArgumentLeft, class ArgumentRight = ArgumentLeft>
struct Binary
{
explicit Binary(ArgumentLeft &&argumentLeft, ArgumentRight &&argumentRight)
: argumentLeft{std::move(argumentLeft)},
argumentRight{std::move(argumentRight)}
{
}
Binary(const Binary &other) = delete;
Binary &operator=(const Binary &&other) = delete;
Binary(Binary &&other) = default;
Binary &operator=(Binary &&other) = default;
ArgumentLeft argumentLeft;
ArgumentRight argumentRight;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
struct NAry
{
using Arguments = std::vector<Argument>;
explicit NAry(Arguments &&arguments) noexcept
: arguments{std::move(arguments)}
{
}
NAry(const NAry &other) = delete;
NAry &operator=(const NAry &&other) = delete;
NAry(NAry &&other) = default;
NAry &operator=(NAry &&other) = default;
Arguments arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
struct Quantified
{
using Parameters = VariableDeclarations;
explicit Quantified(Parameters &&parameters, Argument &&argument)
: parameters{std::move(parameters)},
argument{std::move(argument)}
{
}
Quantified(const Quantified &other) = delete;
Quantified &operator=(const Quantified &&other) = delete;
Quantified(Quantified &&other) = default;
Quantified &operator=(Quantified &&other) = default;
Parameters parameters;
Argument argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct And: public NAry<And<Argument>, Argument>
{
using typename NAry<And<Argument>, Argument>::Arguments;
static constexpr const auto Identifier = "and";
explicit And(Arguments &&arguments) noexcept
: NAry<And<Argument>, Argument>(std::move(arguments))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: make binary expression
template<class Argument>
struct At
{
static constexpr const auto TimePointStart = std::numeric_limits<std::size_t>::max();
static constexpr const auto TimePointEnd = std::numeric_limits<std::size_t>::max() - 1;
At(std::size_t timePoint, Argument &&argument)
: timePoint{timePoint},
argument{std::move(argument)}
{
}
At(const At &other) = delete;
At &operator=(const At &&other) = delete;
At(At &&other) = default;
At &operator=(At &&other) = default;
std::size_t timePoint;
Argument argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct Either: public NAry<Either<Argument>, Argument>
{
using typename NAry<Either<Argument>, Argument>::Arguments;
static constexpr const auto Identifier = "either";
explicit Either(Arguments &&arguments) noexcept
: NAry<Either<Argument>, Argument>(std::move(arguments))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct Exists: public Quantified<Exists<Argument>, Argument>
{
static constexpr const auto Identifier = "exists";
explicit Exists(VariableDeclarations &&parameters, Argument &&argument)
: Quantified<Exists<Argument>, Argument>(std::move(parameters), std::move(argument))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct ForAll: public Quantified<ForAll<Argument>, Argument>
{
static constexpr const auto Identifier = "forall";
explicit ForAll(VariableDeclarations &&parameters, Argument &&argument)
: Quantified<ForAll<Argument>, Argument>(std::move(parameters), std::move(argument))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct Imply: public Binary<Imply<Argument>, Argument>
{
static constexpr const auto Identifier = "imply";
// TODO: make noexcept consistent
explicit Imply(Argument &&argumentLeft, Argument &&argumentRight)
: Binary<Imply<Argument>, Argument>(std::move(argumentLeft), std::move(argumentRight))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct Not
{
explicit Not(Argument &&argument)
: argument{std::move(argument)}
{
}
Not(const Not &other) = delete;
Not &operator=(const Not &&other) = delete;
Not(Not &&other) = default;
Not &operator=(Not &&other) = default;
Argument argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct Or: public NAry<Or<Argument>, Argument>
{
using typename NAry<Or<Argument>, Argument>::Arguments;
static constexpr const auto Identifier = "or";
explicit Or(Arguments &&arguments) noexcept
: NAry<Or<Argument>, Argument>(std::move(arguments))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class ArgumentLeft, class ArgumentRight>
struct When: public Binary<When<ArgumentLeft, ArgumentRight>, ArgumentLeft, ArgumentRight>
{
static constexpr const auto Identifier = "when";
explicit When(ArgumentLeft &&argumentLeft, ArgumentRight &&argumentRight)
: Binary<When<ArgumentLeft, ArgumentRight>, ArgumentLeft, ArgumentRight>(std::move(argumentLeft), std::move(argumentRight))
{
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// PDDL Structure
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Action
{
Action() = default;
Action(const Action &other) = delete;
Action &operator=(const Action &&other) = delete;
Action(Action &&other) = default;
Action &operator=(Action &&other) = default;
std::string name;
VariableDeclarations parameters;
std::experimental::optional<Precondition> precondition;
std::experimental::optional<Effect> effect;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Domain
{
Domain() = default;
Domain(const Domain &other) = delete;
Domain &operator=(const Domain &&other) = delete;
Domain(Domain &&other) = delete;
Domain &operator=(Domain &&other) = delete;
std::string name;
Requirements requirements;
PrimitiveTypeDeclarations types;
ConstantDeclarations constants;
PredicateDeclarations predicates;
Actions actions;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct InitialState
{
InitialState() = default;
InitialState(const InitialState &other) = delete;
InitialState &operator=(const InitialState &&other) = delete;
InitialState(InitialState &&other) = default;
InitialState &operator=(InitialState &&other) = default;
Facts facts;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Problem
{
Problem() = default;
Problem(Domain *domain)
: domain{domain}
{
}
Problem(const Problem &other) = delete;
Problem &operator=(const Problem &&other) = delete;
Problem(Problem &&other) = default;
Problem &operator=(Problem &&other) = default;
Domain *domain;
std::string name;
Requirements requirements;
ConstantDeclarations objects;
InitialState initialState;
std::experimental::optional<Goal> goal;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: think about ignoring requirement statements entirely and computing them instead
enum class Requirement
{
STRIPS,
Typing,
NegativePreconditions,
DisjunctivePreconditions,
Equality,
ExistentialPreconditions,
UniversalPreconditions,
QuantifiedPreconditions,
ConditionalEffects,
Fluents,
NumericFluents,
ObjectFluents,
ADL,
DurativeActions,
DurationInequalities,
ContinuousEffects,
DerivedPredicates,
TimedInitialLiterals,
Preferences,
Constraints,
ActionCosts,
GoalUtilities
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Description
{
Description() = default;
Description(const Description &other) = delete;
Description &operator=(const Description &&other) = delete;
Description(Description &&other) = default;
Description &operator=(Description &&other) = default;
DomainPointer domain;
std::experimental::optional<ProblemPointer> problem;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,280 @@
#ifndef __PDDL__AST_FORWARD_H
#define __PDDL__AST_FORWARD_H
#include <iosfwd>
#include <memory>
#include <experimental/optional>
#include <set>
#include <vector>
#include <pddl/Variant.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AST Forward Declarations
//
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace ast
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Constant;
using ConstantPointer = std::unique_ptr<Constant>;
struct ConstantDeclaration;
using ConstantDeclarationPointer = std::unique_ptr<ConstantDeclaration>;
using ConstantDeclarations = std::vector<ConstantDeclarationPointer>;
struct PrimitiveType;
using PrimitiveTypePointer = std::unique_ptr<PrimitiveType>;
using PrimitiveTypes = std::vector<PrimitiveTypePointer>;
struct PrimitiveTypeDeclaration;
using PrimitiveTypeDeclarationPointer = std::unique_ptr<PrimitiveTypeDeclaration>;
using PrimitiveTypeDeclarations = std::vector<PrimitiveTypeDeclarationPointer>;
struct Variable;
using VariablePointer = std::unique_ptr<Variable>;
using Variables = std::vector<VariablePointer>;
struct VariableDeclaration;
using VariableDeclarationPointer = std::unique_ptr<VariableDeclaration>;
using VariableDeclarations = std::vector<VariableDeclarationPointer>;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compounds
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Predicate;
using PredicatePointer = std::unique_ptr<Predicate>;
using Predicates = std::vector<PredicatePointer>;
struct PredicateDeclaration;
using PredicateDeclarationPointer = std::unique_ptr<PredicateDeclaration>;
using PredicateDeclarations = std::vector<PredicateDeclarationPointer>;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
struct And;
template<class Argument>
using AndPointer = std::unique_ptr<And<Argument>>;
template<class Argument>
struct At;
template<class Argument>
using AtPointer = std::unique_ptr<At<Argument>>;
template<class Argument>
struct Either;
template<class Argument>
using EitherPointer = std::unique_ptr<Either<Argument>>;
template<class Argument>
struct Exists;
template<class Argument>
using ExistsPointer = std::unique_ptr<Exists<Argument>>;
template<class Argument>
struct ForAll;
template<class Argument>
using ForAllPointer = std::unique_ptr<ForAll<Argument>>;
template<class Argument>
struct Imply;
template<class Argument>
using ImplyPointer = std::unique_ptr<Imply<Argument>>;
template<class Argument>
struct Not;
template<class Argument>
using NotPointer = std::unique_ptr<Not<Argument>>;
template<class Argument>
struct Or;
template<class Argument>
using OrPointer = std::unique_ptr<Or<Argument>>;
template<class ArgumentLeft, class ArgumentRight>
struct When;
template<class ArgumentLeft, class ArgumentRight>
using WhenPointer = std::unique_ptr<When<ArgumentLeft, ArgumentRight>>;
////////////////////////////////////////////////////////////////////////////////////////////////////
// PDDL Structure
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Action;
using ActionPointer = std::unique_ptr<Action>;
using Actions = std::vector<ActionPointer>;
struct Description;
using DescriptionPointer = std::unique_ptr<Description>;
struct Domain;
using DomainPointer = std::unique_ptr<Domain>;
struct InitialState;
struct Problem;
using ProblemPointer = std::unique_ptr<Problem>;
enum class Requirement;
using Requirements = std::vector<Requirement>;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Variants
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
using TermT = Variant<
ConstantPointer,
VariablePointer>;
}
class Term : public detail::TermT
{
Term() = delete;
using detail::TermT::TermT;
};
using Terms = std::vector<Term>;
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
// TODO: add missing types
using AtomicFormulaT = Variant<
PredicatePointer>;
}
class AtomicFormula : public detail::AtomicFormulaT
{
AtomicFormula() = delete;
using detail::AtomicFormulaT::AtomicFormulaT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
using LiteralT = Variant<
AtomicFormula,
NotPointer<AtomicFormula>>;
}
class Literal : public detail::LiteralT
{
Literal() = delete;
using detail::LiteralT::LiteralT;
};
using Literals = std::vector<Literal>;
////////////////////////////////////////////////////////////////////////////////////////////////////
class Precondition;
namespace detail
{
// TODO: add missing types
using PreconditionT = Variant<
AtomicFormula,
AndPointer<Precondition>,
ExistsPointer<Precondition>,
ForAllPointer<Precondition>,
ImplyPointer<Precondition>,
NotPointer<Precondition>,
OrPointer<Precondition>>;
}
class Precondition : public detail::PreconditionT
{
Precondition() = delete;
using detail::PreconditionT::PreconditionT;
};
using Preconditions = std::vector<Precondition>;
////////////////////////////////////////////////////////////////////////////////////////////////////
class ConditionalEffect;
namespace detail
{
// TODO: add missing types
using ConditionalEffectT = Variant<
Literal,
AndPointer<Literal>>;
}
class ConditionalEffect : public detail::ConditionalEffectT
{
ConditionalEffect() = delete;
using detail::ConditionalEffectT::ConditionalEffectT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
class Effect;
namespace detail
{
// TODO: add missing types
using EffectT = Variant<
Literal,
AndPointer<Effect>,
ForAllPointer<Effect>,
WhenPointer<Precondition, ConditionalEffect>>;
}
class Effect : public detail::EffectT
{
Effect() = delete;
using detail::EffectT::EffectT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
using TypeT = Variant<
EitherPointer<PrimitiveTypePointer>,
PrimitiveTypePointer>;
}
class Type : public detail::TypeT
{
Type() = delete;
using detail::TypeT::TypeT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
class Fact;
namespace detail
{
using FactT = Variant<
AtPointer<Literal>,
Literal>;
}
class Fact : public detail::FactT
{
Fact() = delete;
using detail::FactT::FactT;
};
using Facts = std::vector<Fact>;
////////////////////////////////////////////////////////////////////////////////////////////////////
using Goal = Precondition;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,608 @@
#ifndef __PDDL__AST_OUTPUT_H
#define __PDDL__AST_OUTPUT_H
#include <colorlog/ColorStream.h>
#include <colorlog/Formatting.h>
#include <pddl/AST.h>
#include <pddl/detail/OutputUtils.h>
#include <pddl/detail/parsing/Requirement.h>
namespace pddl
{
namespace ast
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Output
//
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Constant &constant, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const ConstantDeclaration &constantDeclaration, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveType &primitiveType, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveTypeDeclaration &primitiveTypeDeclaration, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variable &variable, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const VariableDeclaration &variableDeclaration, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Predicate &predicate, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PredicateDeclaration &predicateDeclaration, pddl::detail::PrintContext &printContext);
template<class Derived, class ArgumentLeft, class ArgumentRight = ArgumentLeft>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Binary<Derived, ArgumentLeft, ArgumentRight> &binary, pddl::detail::PrintContext &printContext);
template<class Derived, class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const NAry<Derived, Argument> &nAry, pddl::detail::PrintContext &printContext);
template<class Derived, class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Quantified<Derived, Argument> &quantified, pddl::detail::PrintContext &printContext);
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const At<Argument> &at, pddl::detail::PrintContext &printContext);
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Not<Argument> &not_, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Action &action, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain &domain, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const InitialState &initialState, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Problem &problem, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Requirement &requirement, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Description &description, pddl::detail::PrintContext &printContext);
template<class ValueType>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::unique_ptr<ValueType> &variant, pddl::detail::PrintContext &printContext);
template<class ValueType>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::vector<ValueType> &variant, pddl::detail::PrintContext &printContext);
template<class Variant>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variant &variant, pddl::detail::PrintContext &printContext);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Constant &constant, pddl::detail::PrintContext &)
{
return stream << pddl::detail::Constant(constant.declaration->name);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const ConstantDeclaration &constantDeclaration, pddl::detail::PrintContext &printContext)
{
stream << pddl::detail::Constant(constantDeclaration.name);
if (constantDeclaration.type)
{
stream << " - ";
print(stream, constantDeclaration.type.value(), printContext);
}
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveType &primitiveType, pddl::detail::PrintContext &)
{
return stream << pddl::detail::Type(primitiveType.declaration->name);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveTypeDeclaration &primitiveTypeDeclaration, pddl::detail::PrintContext &printContext)
{
if (primitiveTypeDeclaration.parentTypes.empty())
return (stream << pddl::detail::Type(primitiveTypeDeclaration.name));
if (!primitiveTypeDeclaration.parentTypes.empty())
for (const auto &parentType : primitiveTypeDeclaration.parentTypes)
{
if (&parentType != &primitiveTypeDeclaration.parentTypes.front())
pddl::detail::printIndentedNewline(stream, printContext);
stream << pddl::detail::Type(primitiveTypeDeclaration.name) << " - " << pddl::detail::Type(parentType->declaration->name);
}
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variable &variable, pddl::detail::PrintContext &)
{
const auto variableName = "?" + variable.declaration->name;
return (stream << colorlog::Variable(variableName.c_str()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const VariableDeclaration &variableDeclaration, pddl::detail::PrintContext &printContext)
{
const auto variableName = "?" + variableDeclaration.name;
stream << colorlog::Variable(variableName.c_str());
if (variableDeclaration.type)
{
stream << " - ";
print(stream, *variableDeclaration.type, printContext);
}
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compounds
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Predicate &predicate, pddl::detail::PrintContext &printContext)
{
stream << "(" << pddl::detail::Identifier(predicate.declaration->name);
for (const auto &argument : predicate.arguments)
{
stream << " ";
print(stream, argument, printContext);
}
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PredicateDeclaration &predicateDeclaration, pddl::detail::PrintContext &printContext)
{
stream << "(" << pddl::detail::Identifier(predicateDeclaration.name);
for (const auto &parameter : predicateDeclaration.parameters)
{
stream << " ";
print(stream, *parameter, printContext);
}
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions: Base Classes
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class ArgumentLeft, class ArgumentRight>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Binary<Derived, ArgumentLeft, ArgumentRight> &binary, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword(Derived::Identifier);
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, binary.argumentLeft, printContext);
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, binary.argumentRight, printContext);
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const NAry<Derived, Argument> &nAry, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword(Derived::Identifier);
printContext.indentationLevel++;
for (const auto &argument : nAry.arguments)
{
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, argument, printContext);
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Quantified<Derived, Argument> &quantified, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword(Derived::Identifier);
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(";
print(stream, quantified.parameters, printContext);
stream << ")";
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, quantified.argument, printContext);
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const And<Argument> &and_, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const NAry<And<Argument>, Argument> &>(and_), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const At<Argument> &at, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword("at");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Number<decltype(at.timePoint)>(at.timePoint);
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, at.argument, printContext);
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Either<Argument> &either, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const NAry<Either<Argument>, Argument> &>(either), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Exists<Argument> &exists, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const Quantified<Exists<Argument>, Argument> &>(exists), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const ForAll<Argument> &forAll, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const Quantified<ForAll<Argument>, Argument> &>(forAll), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Imply<Argument> &imply, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const Binary<Imply<Argument>, Argument> &>(imply), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Not<Argument> &not_, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword("not") << " ";
print(stream, not_.argument, printContext);
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Or<Argument> &or_, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const NAry<Or<Argument>, Argument> &>(or_), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class ArgumentLeft, class ArgumentRight>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const When<ArgumentLeft, ArgumentRight> &when, pddl::detail::PrintContext &printContext)
{
return print(stream, static_cast<const Binary<When<ArgumentLeft, ArgumentRight>, ArgumentLeft, ArgumentRight> &>(when), printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// PDDL Structure
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Action &action, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword(":action") << " " << pddl::detail::Identifier(action.name);
printContext.indentationLevel++;
if (!action.parameters.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":parameters");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(";
print(stream, action.parameters, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (action.precondition)
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":precondition");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, action.precondition.value(), printContext);
printContext.indentationLevel--;
}
if (action.effect)
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":effect");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, action.effect.value(), printContext);
printContext.indentationLevel--;
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain &domain, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword("define") << " (" << colorlog::Keyword("domain") << " " << pddl::detail::Identifier(domain.name) << ")";
printContext.indentationLevel++;
if (!domain.requirements.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":requirements");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, domain.requirements, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.types.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":types");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, domain.types, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.constants.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":constants");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, domain.constants, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.predicates.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":predicates");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, domain.predicates, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.actions.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, domain.actions, printContext);
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const InitialState &initialState, pddl::detail::PrintContext &printContext)
{
assert(!initialState.facts.empty());
stream << "(" << colorlog::Keyword(":init");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, initialState.facts, printContext);
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Problem &problem, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword("define") << " (" << colorlog::Keyword("problem") << " " << pddl::detail::Identifier(problem.name) << ")";
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":domain") << " " << pddl::detail::Identifier(problem.domain->name) << ")";
if (!problem.requirements.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":requirements");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, problem.requirements, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!problem.objects.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":objects");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, problem.objects, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!problem.initialState.facts.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, problem.initialState, printContext);
}
if (problem.goal)
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":goal");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, problem.goal.value(), printContext);
stream << ")";
printContext.indentationLevel--;
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Requirement &requirement, pddl::detail::PrintContext &)
{
auto requirementName = std::string(":") + pddl::detail::toString(requirement);
return (stream << pddl::detail::Identifier(requirementName.c_str()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Description &description, pddl::detail::PrintContext &printContext)
{
print(stream, *description.domain, printContext);
stream << std::endl;
if (description.problem)
{
stream << std::endl;
print(stream, *description.problem.value(), printContext);
stream << std::endl;
}
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const Description &description)
{
pddl::detail::PrintContext printContext;
return print(stream, description, printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Variants
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class ValueType>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::unique_ptr<ValueType> &uniquePointer, pddl::detail::PrintContext &printContext)
{
assert(uniquePointer);
return print(stream, *uniquePointer, printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class ValueType>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::vector<ValueType> &vector, pddl::detail::PrintContext &printContext)
{
for (const auto &element : vector)
{
if (&element != &vector.front())
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, element, printContext);
}
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Variant>
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variant &variant, pddl::detail::PrintContext &printContext)
{
variant.match([&](const auto &x){return print(stream, x, printContext);});
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,53 @@
#ifndef __PDDL__CONTEXT_H
#define __PDDL__CONTEXT_H
#include <functional>
#include <pddl/Mode.h>
#include <pddl/Tokenizer.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Context
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Context
{
constexpr static const char *auxiliaryPrefix()
{
return "__plasp_";
}
// TODO: replace std::string with char *
using WarningCallback = std::function<void (tokenize::Location &&, const std::string &)>;
Context() = default;
~Context() = default;
explicit Context(Tokenizer &&tokenizer, WarningCallback warningCallback, Mode mode = Mode::Strict)
: tokenizer{std::move(tokenizer)},
warningCallback{warningCallback},
mode{mode}
{
}
Context(const Context &other) = delete;
Context &operator=(const Context &other) = delete;
Context(Context &&other) = default;
Context &operator=(Context &&other) = default;
Tokenizer tokenizer;
WarningCallback warningCallback;
Mode mode;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@@ -0,0 +1,95 @@
#ifndef __PDDL__EXCEPTION_H
#define __PDDL__EXCEPTION_H
#include <exception>
#include <experimental/optional>
#include <string>
#include <tokenize/Location.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Exception
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Exception: public std::exception
{
public:
Exception()
: Exception("unspecified parser error")
{
}
Exception(const char *message)
: Exception(static_cast<std::string>(message))
{
}
Exception(const std::string &message)
: m_message{message}
{
}
Exception(tokenize::Location &&location)
: Exception(std::forward<tokenize::Location>(location), "unspecified parser error")
{
}
Exception(tokenize::Location &&location, const char *message)
: Exception(std::forward<tokenize::Location>(location), static_cast<std::string>(message))
{
}
Exception(tokenize::Location &&location, const std::string &message)
: m_location{std::move(location)},
m_message{message}
{
}
~Exception() noexcept = default;
const char *what() const noexcept
{
return m_message.c_str();
}
const std::experimental::optional<tokenize::Location> &location() const
{
return m_location;
}
const std::string &message() const
{
return m_message;
}
private:
std::experimental::optional<tokenize::Location> m_location;
std::string m_message;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
class ParserException : public Exception
{
public:
using Exception::Exception;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
class NormalizationException : public Exception
{
public:
using Exception::Exception;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@@ -0,0 +1,27 @@
#ifndef __PDDL__MODE_H
#define __PDDL__MODE_H
#include <functional>
#include <pddl/Tokenizer.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Mode
//
////////////////////////////////////////////////////////////////////////////////////////////////////
enum class Mode
{
Strict,
Compatibility
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@@ -0,0 +1,24 @@
#ifndef __PDDL__NORMALIZE_H
#define __PDDL__NORMALIZE_H
#include <pddl/AST.h>
#include <pddl/Context.h>
#include <pddl/NormalizedAST.h>
#include <pddl/detail/normalization/Description.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Normalize
//
////////////////////////////////////////////////////////////////////////////////////////////////////
using detail::normalize;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@@ -0,0 +1,159 @@
#ifndef __PDDL__NORMALIZED_AST_H
#define __PDDL__NORMALIZED_AST_H
#include <limits>
#include <experimental/optional>
#include <set>
#include <type_traits>
#include <vector>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace normalizedAST
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Normalized AST
//
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// PDDL Structure
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Action
{
Action() = default;
Action(const Action &other) = delete;
Action &operator=(const Action &&other) = delete;
Action(Action &&other) = default;
Action &operator=(Action &&other) = default;
std::string name;
VariableDeclarations parameters;
std::experimental::optional<Precondition> precondition;
std::experimental::optional<Effect> effect;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Domain
{
Domain() = default;
Domain(const Domain &other) = delete;
Domain &operator=(const Domain &&other) = delete;
Domain(Domain &&other) = delete;
Domain &operator=(Domain &&other) = delete;
std::string name;
PrimitiveTypeDeclarations types;
ConstantDeclarations constants;
PredicateDeclarations predicates;
DerivedPredicateDeclarations derivedPredicates;
Actions actions;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct DerivedPredicate
{
using Arguments = Terms;
explicit DerivedPredicate(Arguments &&arguments, DerivedPredicateDeclaration *declaration)
: arguments{std::move(arguments)},
declaration{declaration}
{
}
DerivedPredicate(const DerivedPredicate &other) = delete;
DerivedPredicate &operator=(const DerivedPredicate &&other) = delete;
DerivedPredicate(DerivedPredicate &&other) = default;
DerivedPredicate &operator=(DerivedPredicate &&other) = default;
Arguments arguments;
DerivedPredicateDeclaration *declaration;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct DerivedPredicateDeclaration
{
explicit DerivedPredicateDeclaration() = default;
DerivedPredicateDeclaration(const DerivedPredicateDeclaration &other) = delete;
DerivedPredicateDeclaration &operator=(const DerivedPredicateDeclaration &&other) = delete;
DerivedPredicateDeclaration(DerivedPredicateDeclaration &&other) = default;
DerivedPredicateDeclaration &operator=(DerivedPredicateDeclaration &&other) = default;
std::string name;
std::vector<VariableDeclaration *> parameters;
VariableDeclarations existentialParameters;
std::experimental::optional<DerivedPredicatePrecondition> precondition;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct InitialState
{
InitialState() = default;
InitialState(const InitialState &other) = delete;
InitialState &operator=(const InitialState &&other) = delete;
InitialState(InitialState &&other) = default;
InitialState &operator=(InitialState &&other) = default;
Facts facts;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Problem
{
Problem() = default;
Problem(Domain *domain)
: domain{domain}
{
}
Problem(const Problem &other) = delete;
Problem &operator=(const Problem &&other) = delete;
Problem(Problem &&other) = default;
Problem &operator=(Problem &&other) = default;
Domain *domain;
std::string name;
DerivedPredicateDeclarations derivedPredicates;
ConstantDeclarations objects;
InitialState initialState;
std::experimental::optional<Goal> goal;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Description
{
Description() = default;
Description(const Description &other) = delete;
Description &operator=(const Description &&other) = delete;
Description(Description &&other) = default;
Description &operator=(Description &&other) = default;
DomainPointer domain;
std::experimental::optional<ProblemPointer> problem;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,230 @@
#ifndef __PDDL__NORMALIZED_AST_FORWARD_H
#define __PDDL__NORMALIZED_AST_FORWARD_H
#include <pddl/AST.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Normalized AST Forward Declarations
//
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace normalizedAST
{
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
using ast::Constant;
using ast::ConstantPointer;
using ast::ConstantDeclaration;
using ast::ConstantDeclarationPointer;
using ast::ConstantDeclarations;
using ast::PrimitiveTypePointer;
using ast::PrimitiveType;
using ast::PrimitiveTypePointer;
using ast::PrimitiveTypes;
using ast::PrimitiveTypeDeclaration;
using ast::PrimitiveTypeDeclarationPointer;
using ast::PrimitiveTypeDeclarations;
using ast::Variable;
using ast::VariablePointer;
using ast::Variables;
using ast::VariableDeclaration;
using ast::VariableDeclarationPointer;
using ast::VariableDeclarations;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compounds
////////////////////////////////////////////////////////////////////////////////////////////////////
struct DerivedPredicate;
using DerivedPredicatePointer = std::unique_ptr<DerivedPredicate>;
using DerivedPredicates = std::vector<DerivedPredicatePointer>;
struct DerivedPredicateDeclaration;
using DerivedPredicateDeclarationPointer = std::unique_ptr<DerivedPredicateDeclaration>;
using DerivedPredicateDeclarations = std::vector<DerivedPredicateDeclarationPointer>;
using ast::Predicate;
using ast::PredicatePointer;
using ast::Predicates;
using ast::PredicateDeclaration;
using ast::PredicateDeclarationPointer;
using ast::PredicateDeclarations;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
using ast::And;
using ast::AndPointer;
using ast::At;
using ast::AtPointer;
using ast::Either;
using ast::EitherPointer;
using ast::Exists;
using ast::ExistsPointer;
using ast::ForAll;
using ast::ForAllPointer;
using ast::Not;
using ast::NotPointer;
using ast::Or;
using ast::OrPointer;
using ast::When;
using ast::WhenPointer;
////////////////////////////////////////////////////////////////////////////////////////////////////
// PDDL Structure
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Action;
using ActionPointer = std::unique_ptr<Action>;
using Actions = std::vector<ActionPointer>;
struct Description;
using DescriptionPointer = std::unique_ptr<Description>;
struct Domain;
using DomainPointer = std::unique_ptr<Domain>;
struct InitialState;
struct Problem;
using ProblemPointer = std::unique_ptr<Problem>;
enum class Requirement;
using Requirements = std::vector<Requirement>;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Variants
////////////////////////////////////////////////////////////////////////////////////////////////////
using ast::Term;
using ast::Terms;
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
using AtomicFormulaT = Variant<
DerivedPredicatePointer,
PredicatePointer>;
}
class AtomicFormula : public detail::AtomicFormulaT
{
AtomicFormula() = delete;
using detail::AtomicFormulaT::AtomicFormulaT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
using LiteralT = Variant<
AtomicFormula,
NotPointer<AtomicFormula>>;
}
class Literal : public detail::LiteralT
{
Literal() = delete;
using detail::LiteralT::LiteralT;
};
using Literals = std::vector<Literal>;
////////////////////////////////////////////////////////////////////////////////////////////////////
class Precondition;
namespace detail
{
using PreconditionT = Variant<
Literal,
AndPointer<Literal>>;
}
class Precondition : public detail::PreconditionT
{
Precondition() = delete;
using detail::PreconditionT::PreconditionT;
};
using Preconditions = std::vector<Precondition>;
////////////////////////////////////////////////////////////////////////////////////////////////////
class DerivedPredicatePrecondition;
namespace detail
{
using DerivedPredicatePreconditionT = Variant<
Literal,
AndPointer<Literal>,
OrPointer<Literal>>;
}
class DerivedPredicatePrecondition : public detail::DerivedPredicatePreconditionT
{
DerivedPredicatePrecondition() = delete;
using detail::DerivedPredicatePreconditionT::DerivedPredicatePreconditionT;
};
using DerivedPredicatePreconditions = std::vector<DerivedPredicatePrecondition>;
////////////////////////////////////////////////////////////////////////////////////////////////////
class ConditionalEffect;
namespace detail
{
using ConditionalEffectT = Variant<
Literal,
AndPointer<Literal>>;
}
class ConditionalEffect : public detail::ConditionalEffectT
{
ConditionalEffect() = delete;
using detail::ConditionalEffectT::ConditionalEffectT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
class Effect;
namespace detail
{
using EffectT = Variant<
Literal,
AndPointer<Effect>,
ForAllPointer<Effect>,
WhenPointer<Precondition, ConditionalEffect>>;
}
class Effect : public detail::EffectT
{
Effect() = delete;
using detail::EffectT::EffectT;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
using Fact = Literal;
using Facts = std::vector<Fact>;
////////////////////////////////////////////////////////////////////////////////////////////////////
using Goal = Precondition;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,358 @@
#ifndef __PDDL__NORMALIZED_AST_OUTPUT_H
#define __PDDL__NORMALIZED_AST_OUTPUT_H
#include <colorlog/ColorStream.h>
#include <colorlog/Formatting.h>
#include <pddl/ASTOutput.h>
#include <pddl/NormalizedAST.h>
#include <pddl/detail/OutputUtils.h>
#include <pddl/detail/parsing/Requirement.h>
namespace pddl
{
namespace normalizedAST
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Output
//
////////////////////////////////////////////////////////////////////////////////////////////////////
using ast::print;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Forward declarations
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const DerivedPredicate &derivedPredicate, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const DerivedPredicateDeclaration &derivedPredicateDeclaration, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Action &action, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain &domain, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const InitialState &initialState, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Problem &problem, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Requirement &requirement, pddl::detail::PrintContext &printContext);
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Description &description, pddl::detail::PrintContext &printContext);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Compounds
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const DerivedPredicate &derivedPredicate, pddl::detail::PrintContext &printContext)
{
// TODO: implement correctly
stream << "(" << pddl::detail::Identifier(derivedPredicate.declaration->name);
for (const auto &argument : derivedPredicate.arguments)
{
stream << " ";
print(stream, argument, printContext);
}
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const DerivedPredicateDeclaration &derivedPredicateDeclaration, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword(":derived-predicate") << " " << pddl::detail::Identifier(derivedPredicateDeclaration.name);
printContext.indentationLevel++;
if (!derivedPredicateDeclaration.parameters.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":parameters");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(";
for (const auto &parameter : derivedPredicateDeclaration.parameters)
{
if (&parameter != &derivedPredicateDeclaration.parameters.front())
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, *parameter, printContext);
}
stream << ")";
printContext.indentationLevel--;
}
if (!derivedPredicateDeclaration.existentialParameters.empty())
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":exists");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
stream << "(";
for (const auto &parameter : derivedPredicateDeclaration.existentialParameters)
{
if (parameter.get() != derivedPredicateDeclaration.existentialParameters.front().get())
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, *parameter, printContext);
}
stream << ")";
printContext.indentationLevel--;
}
if (derivedPredicateDeclaration.precondition)
{
pddl::detail::printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":precondition");
printContext.indentationLevel++;
pddl::detail::printIndentedNewline(stream, printContext);
print(stream, derivedPredicateDeclaration.precondition.value(), printContext);
printContext.indentationLevel--;
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// PDDL Structure
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Action &action, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword(":action") << " " << pddl::detail::Identifier(action.name);
printContext.indentationLevel++;
if (!action.parameters.empty())
{
printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":parameters");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
stream << "(";
print(stream, action.parameters, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (action.precondition)
{
printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":precondition");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, action.precondition.value(), printContext);
printContext.indentationLevel--;
}
if (action.effect)
{
printIndentedNewline(stream, printContext);
stream << colorlog::Keyword(":effect");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, action.effect.value(), printContext);
printContext.indentationLevel--;
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain &domain, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword("define") << " (" << colorlog::Keyword("domain") << " " << pddl::detail::Identifier(domain.name) << ")";
printContext.indentationLevel++;
if (!domain.types.empty())
{
printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":types");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, domain.types, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.constants.empty())
{
printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":constants");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, domain.constants, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.predicates.empty())
{
printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":predicates");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, domain.predicates, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!domain.derivedPredicates.empty())
{
printIndentedNewline(stream, printContext);
print(stream, domain.derivedPredicates, printContext);
}
if (!domain.actions.empty())
{
printIndentedNewline(stream, printContext);
print(stream, domain.actions, printContext);
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const InitialState &initialState, pddl::detail::PrintContext &printContext)
{
assert(!initialState.facts.empty());
stream << "(" << colorlog::Keyword(":init");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, initialState.facts, printContext);
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Problem &problem, pddl::detail::PrintContext &printContext)
{
stream << "(" << colorlog::Keyword("define") << " (" << colorlog::Keyword("problem") << " " << pddl::detail::Identifier(problem.name) << ")";
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":domain") << " " << pddl::detail::Identifier(problem.domain->name) << ")";
if (!problem.derivedPredicates.empty())
{
printIndentedNewline(stream, printContext);
print(stream, problem.derivedPredicates, printContext);
}
if (!problem.objects.empty())
{
printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":objects");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, problem.objects, printContext);
stream << ")";
printContext.indentationLevel--;
}
if (!problem.initialState.facts.empty())
{
printIndentedNewline(stream, printContext);
print(stream, problem.initialState, printContext);
}
if (problem.goal)
{
printIndentedNewline(stream, printContext);
stream << "(" << colorlog::Keyword(":goal");
printContext.indentationLevel++;
printIndentedNewline(stream, printContext);
print(stream, problem.goal.value(), printContext);
stream << ")";
printContext.indentationLevel--;
}
printContext.indentationLevel--;
return (stream << ")");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Description &description, pddl::detail::PrintContext &printContext)
{
print(stream, *description.domain, printContext);
stream << std::endl;
if (description.problem)
{
stream << std::endl;
print(stream, *description.problem.value(), printContext);
stream << std::endl;
}
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const Description &description)
{
pddl::detail::PrintContext printContext;
return print(stream, description, printContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,24 @@
#ifndef __PDDL__PARSE_H
#define __PDDL__PARSE_H
#include <experimental/optional>
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Parse
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::Description parseDescription(Context &context);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@@ -0,0 +1,52 @@
#ifndef __PDDL__TOKENIZER_H
#define __PDDL__TOKENIZER_H
#include <iostream>
#include <tokenize/Tokenizer.h>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tokenizer
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PDDLTokenizerPolicy
{
static char transformCharacter(char c) noexcept
{
return std::tolower(c);
}
static bool isWhiteSpaceCharacter(char c)
{
return std::iswspace(c);
}
static bool isBlankCharacter(char c)
{
return std::isblank(c);
}
static bool isIdentifierCharacter(char c)
{
return c != '?'
&& c != '('
&& c != ')'
&& c != ';'
&& std::isgraph(c);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
using Tokenizer = tokenize::Tokenizer<PDDLTokenizerPolicy>;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

View File

@@ -0,0 +1,20 @@
#ifndef __PDDL__VARIANT_H
#define __PDDL__VARIANT_H
#include <mapbox/variant.hpp>
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class... Types>
using Variant = mapbox::util::variant<Types...>;
////////////////////////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#endif

View File

@@ -0,0 +1,48 @@
#ifndef __PDDL__DETAIL__AST_CONTEXT_H
#define __PDDL__DETAIL__AST_CONTEXT_H
#include <pddl/AST.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ASTContext
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct ASTContext
{
ASTContext(ast::Description &description)
: domain{description.domain.get()},
problem{description.problem.value() ? std::experimental::optional<ast::Problem *>(description.problem.value().get()) : std::experimental::nullopt}
{
}
ASTContext(ast::Domain &domain)
: domain{&domain}
{
}
ASTContext(ast::Problem &problem)
: domain{problem.domain},
problem{&problem}
{
}
ast::Domain *domain;
std::experimental::optional<ast::Problem *> problem;
VariableStack variables;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,165 @@
#ifndef __PDDL__DETAIL__AST_COPY_H
#define __PDDL__DETAIL__AST_COPY_H
#include <pddl/AST.h>
#include <pddl/Variant.h>
namespace pddl
{
namespace ast
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ASTCopy
//
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
inline Constant deepCopy(Constant &other);
inline PrimitiveType deepCopy(PrimitiveType &other);
inline Variable deepCopy(Variable &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions: Base Classes
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class ArgumentLeft, class ArgumentRight = ArgumentLeft>
inline Derived deepCopy(Binary<Derived, ArgumentLeft, ArgumentRight> &other);
template<class Derived, class Argument>
inline Derived deepCopy(NAry<Derived, Argument> &other);
template<class Derived, class Argument>
inline Derived deepCopy(Quantified<Derived, Argument> &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
inline At<Argument> deepCopy(At<Argument> &other);
template<class Argument>
inline Not<Argument> deepCopy(Not<Argument> &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Variants
////////////////////////////////////////////////////////////////////////////////////////////////////
inline ast::Type deepCopy(ast::Type &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Unique Pointers
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
std::unique_ptr<T> deepCopy(std::unique_ptr<T> &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
////////////////////////////////////////////////////////////////////////////////////////////////////
Constant deepCopy(Constant &other)
{
return Constant(other.declaration);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
PrimitiveType deepCopy(PrimitiveType &other)
{
return PrimitiveType(other.declaration);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Variable deepCopy(Variable &other)
{
return Variable(other.declaration);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions: Base Classes
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class ArgumentLeft, class ArgumentRight>
Derived deepCopy(Binary<Derived, ArgumentLeft, ArgumentRight> &other)
{
auto argumentLeft{deepCopy(other.argumentLeft)};
auto argumentRight{deepCopy(other.argumentRight)};
return Binary<Derived, ArgumentLeft, ArgumentRight>(std::move(argumentLeft), std::move(argumentRight));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
Derived deepCopy(NAry<Derived, Argument> &other)
{
typename Derived::Arguments arguments;
arguments.reserve(other.arguments.size());
for (auto &argument : other.arguments)
arguments.emplace_back(deepCopy(argument));
return Derived(std::move(arguments));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
Derived deepCopy(Quantified<Derived, Argument> &other)
{
auto argument{deepCopy(other.argument)};
return Quantified<Derived, Argument>(std::move(argument));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
At<Argument> deepCopy(At<Argument> &other)
{
auto argument{deepCopy(other.argument)};
return At<Argument>(other.timePoint, std::move(argument));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
Not<Argument> deepCopy(Not<Argument> &other)
{
auto argument{deepCopy(other.argument)};
return Not<Argument>(std::move(argument));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Variants
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::Type deepCopy(ast::Type &other)
{
return other.match([](auto &x) -> ast::Type {return deepCopy(x);});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Unique Pointers
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
std::unique_ptr<T> deepCopy(std::unique_ptr<T> &other)
{
return std::make_unique<T>(deepCopy(*other));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,117 @@
#ifndef __PDDL__DETAIL__OUTPUT_UTILS_H
#define __PDDL__DETAIL__OUTPUT_UTILS_H
#include <colorlog/Formatting.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// OutputUtils
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct PrintContext
{
size_t indentationLevel{0};
};
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Identifier
{
Identifier(const char *content)
: content{content}
{
}
Identifier(const std::string &content)
: content{content.c_str()}
{
}
const char *content;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const Identifier &identifier)
{
return (stream
<< colorlog::Format({colorlog::Color::Green, colorlog::FontWeight::Normal})
<< identifier.content << colorlog::ResetFormat());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Type
{
Type(const char *name)
: name{name}
{
}
Type(const std::string &name)
: name{name.c_str()}
{
}
const char *name;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const Type &type)
{
return (stream
<< colorlog::Format({colorlog::Color::Red, colorlog::FontWeight::Normal})
<< type.name << colorlog::ResetFormat());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Constant
{
Constant(const char *name)
: name{name}
{
}
Constant(const std::string &name)
: name{name.c_str()}
{
}
const char *name;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &operator<<(colorlog::ColorStream &stream, const Constant &constant)
{
return (stream
<< colorlog::Format({colorlog::Color::Yellow, colorlog::FontWeight::Normal})
<< constant.name << colorlog::ResetFormat());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline colorlog::ColorStream &printIndentedNewline(colorlog::ColorStream &stream, detail::PrintContext &printContext)
{
stream << std::endl;
for (size_t i = 0; i < printContext.indentationLevel; i++)
stream << "\t";
return stream;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,31 @@
#ifndef __PDDL__DETAIL__REQUIREMENTS_H
#define __PDDL__DETAIL__REQUIREMENTS_H
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Requirements
//
////////////////////////////////////////////////////////////////////////////////////////////////////
bool hasRequirement(const ast::Domain &domain, ast::Requirement requirement);
bool hasRequirement(const ast::Problem &problem, ast::Requirement requirement);
bool hasRequirement(const ASTContext &astContext, ast::Requirement requirement);
void checkRequirement(ast::Domain &domain, ast::Requirement requirement, Context &context);
void checkRequirement(ast::Problem &problem, ast::Requirement requirement, Context &context);
void checkRequirement(ASTContext &astContext, ast::Requirement requirement, Context &context);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,34 @@
#ifndef __PDDL__DETAIL__SIGNATURE_MATCHING_H
#define __PDDL__DETAIL__SIGNATURE_MATCHING_H
#include <string>
#include <pddl/AST.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SignatureMatching
//
////////////////////////////////////////////////////////////////////////////////////////////////////
bool matches(const ast::PrimitiveTypeDeclaration &lhs, const ast::PrimitiveTypeDeclaration &rhs);
bool matches(const ast::Either<ast::PrimitiveTypePointer> &lhs, const ast::PrimitiveTypeDeclaration &rhs);
bool matches(const ast::PrimitiveTypeDeclaration &lhs, const ast::Either<ast::PrimitiveTypePointer> &rhs);
bool matches(const ast::Either<ast::PrimitiveTypePointer> &lhs, const ast::Either<ast::PrimitiveTypePointer> &rhs);
bool matches(const ast::VariableDeclaration &lhs, const std::experimental::optional<ast::Type> &rhs);
bool matches(const ast::ConstantDeclaration &lhs, const std::experimental::optional<ast::Type> &rhs);
bool matches(const ast::Term &lhs, const std::experimental::optional<ast::Type> &rhs);
bool matches(const std::string &predicateName, const ast::Predicate::Arguments &predicateArguments, const ast::PredicateDeclaration &predicateDeclaration);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,38 @@
#ifndef __PDDL__DETAIL__VARIABLE_STACK_H
#define __PDDL__DETAIL__VARIABLE_STACK_H
#include <pddl/ASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// VariableStack
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class VariableStack
{
public:
using Layer = ast::VariableDeclarations *;
public:
void push(Layer layer);
void pop();
std::experimental::optional<ast::VariableDeclaration *> findVariableDeclaration(const std::string &variableName) const;
bool contains(const ast::VariableDeclaration &variableDeclaration) const;
private:
std::vector<Layer> m_layers;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__ACTION_H
#define __PDDL__DETAIL__NORMALIZATION__ACTION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Action
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ActionPointer normalize(ast::ActionPointer &&domain, normalizedAST::DerivedPredicateDeclarations &derivedPredicates);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__ATOMIC_FORMULA_H
#define __PDDL__DETAIL__NORMALIZATION__ATOMIC_FORMULA_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AtomicFormula
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::AtomicFormula normalize(ast::AtomicFormula &&atomicFormula);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,121 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__COLLECT_FREE_VARIABLES_H
#define __PDDL__DETAIL__NORMALIZATION__COLLECT_FREE_VARIABLES_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/Exception.h>
#include <pddl/NormalizedASTForward.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Collect Free Variables
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Variant>
void collectFreeVariables(const Variant &variant, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
variant.match([&](const auto &x){collectFreeVariables(x, freeVariables, variableStack);});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void collectFreeVariables(const ast::ConstantPointer &, std::vector<normalizedAST::VariableDeclaration *> &, VariableStack &)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void collectFreeVariables(const ast::VariablePointer &variable, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
if (variableStack.contains(*variable->declaration))
return;
const auto matchingFreeVariable = std::find(freeVariables.cbegin(), freeVariables.cend(), variable->declaration);
if (matchingFreeVariable != freeVariables.cend())
return;
freeVariables.emplace_back(variable->declaration);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline void collectFreeVariables(const ast::PredicatePointer &predicate, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
for (const auto &argument : predicate->arguments)
collectFreeVariables(argument, freeVariables, variableStack);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
void collectFreeVariables(const ast::AndPointer<Argument> &and_, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
for (const auto &argument : and_->arguments)
collectFreeVariables(argument, freeVariables, variableStack);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
void collectFreeVariables(const ast::ExistsPointer<Argument> &exists, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
variableStack.push(&exists->parameters);
collectFreeVariables(exists->argument, freeVariables, variableStack);
variableStack.pop();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
void collectFreeVariables(const ast::ForAllPointer<Argument> &forAll, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
variableStack.push(&forAll->parameters);
collectFreeVariables(forAll->argument, freeVariables, variableStack);
variableStack.pop();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
void collectFreeVariables(const ast::ImplyPointer<Argument> &imply, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
collectFreeVariables(imply->argumentLeft, freeVariables, variableStack);
collectFreeVariables(imply->argumentRight, freeVariables, variableStack);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
void collectFreeVariables(const ast::NotPointer<Argument> &not_, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
collectFreeVariables(not_->argument, freeVariables, variableStack);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
void collectFreeVariables(const ast::OrPointer<Argument> &or_, std::vector<normalizedAST::VariableDeclaration *> &freeVariables, VariableStack &variableStack)
{
for (const auto &argument : or_->arguments)
collectFreeVariables(argument, freeVariables, variableStack);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__CONDITIONAL_EFFECT_H
#define __PDDL__DETAIL__NORMALIZATION__CONDITIONAL_EFFECT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ConditionalEffect
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ConditionalEffect normalize(ast::ConditionalEffect &&conditionalEffect);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__DESCRIPTION_H
#define __PDDL__DETAIL__NORMALIZATION__DESCRIPTION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Description normalize(ast::Description &&description);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__DOMAIN_H
#define __PDDL__DETAIL__NORMALIZATION__DOMAIN_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Domain
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::DomainPointer normalize(ast::DomainPointer &&domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__EFFECT_H
#define __PDDL__DETAIL__NORMALIZATION__EFFECT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Effect
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Effect normalize(ast::Effect &&effect, normalizedAST::DerivedPredicateDeclarations &derivedPredicates);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__FACT_H
#define __PDDL__DETAIL__NORMALIZATION__FACT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fact
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Fact normalize(ast::Fact &&fact);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__INITIAL_STATE_H
#define __PDDL__DETAIL__NORMALIZATION__INITIAL_STATE_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// InitialState
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::InitialState normalize(ast::InitialState &&initialState);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__LITERAL_H
#define __PDDL__DETAIL__NORMALIZATION__LITERAL_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Literal
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Literal normalize(ast::Literal &&literal);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__PRECONDITION_H
#define __PDDL__DETAIL__NORMALIZATION__PRECONDITION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Precondition
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::Precondition normalize(ast::Precondition &&precondition, normalizedAST::DerivedPredicateDeclarations &derivedPredicates);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__PROBLEM_H
#define __PDDL__DETAIL__NORMALIZATION__PROBLEM_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Problem
//
////////////////////////////////////////////////////////////////////////////////////////////////////
normalizedAST::ProblemPointer normalize(ast::ProblemPointer &&problem, normalizedAST::Domain *domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__NORMALIZATION__REDUCTION_H
#define __PDDL__DETAIL__NORMALIZATION__REDUCTION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/NormalizedASTForward.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Reduction
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void reduce(ast::Precondition &precondition);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,50 @@
#ifndef __PDDL__DETAIL__PARSING__ACTION_H
#define __PDDL__DETAIL__PARSING__ACTION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Action
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class ActionParser
{
public:
ActionParser(Context &context, ast::Domain &domain);
ast::ActionPointer parse();
private:
void findSections(ast::Action &action);
void parseParameterSection(ast::Action &action);
void parsePreconditionSection(ast::Action &action);
void parseEffectSection(ast::Action &action);
// For compatibility with old PDDL versions
void parseVarsSection(ast::Action &action);
Context &m_context;
ast::Domain &m_domain;
tokenize::StreamPosition m_parametersPosition;
tokenize::StreamPosition m_preconditionPosition;
tokenize::StreamPosition m_effectPosition;
// For compatibility with old PDDL versions
tokenize::StreamPosition m_varsPosition;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,27 @@
#ifndef __PDDL__DETAIL__PARSING__ATOMIC_FORMULA_H
#define __PDDL__DETAIL__PARSING__ATOMIC_FORMULA_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// AtomicFormula
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::AtomicFormula> parseAtomicFormula(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,27 @@
#ifndef __PDDL__DETAIL__PARSING__CONSTANT_H
#define __PDDL__DETAIL__PARSING__CONSTANT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Constant
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::ConstantPointer> testParsingConstant(Context &context, ASTContext &astContext);
ast::ConstantPointer parseConstant(Context &context, ASTContext &astContext);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__PARSING__CONSTANT_DECLARATION_H
#define __PDDL__DETAIL__PARSING__CONSTANT_DECLARATION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ConstantDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddConstantDeclarations(Context &context, ast::Domain &domain);
void parseAndAddConstantDeclarations(Context &context, ast::Problem &problem);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,37 @@
#ifndef __PDDL__DETAIL__PARSING__DESCRIPTION_H
#define __PDDL__DETAIL__PARSING__DESCRIPTION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Description
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class DescriptionParser
{
public:
DescriptionParser(Context &context);
ast::Description parse();
private:
void findSections();
Context &m_context;
tokenize::StreamPosition m_domainPosition;
tokenize::StreamPosition m_problemPosition;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,48 @@
#ifndef __PDDL__DETAIL__PARSING__DOMAIN_H
#define __PDDL__DETAIL__PARSING__DOMAIN_H
#include <pddl/AST.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Domain
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class DomainParser
{
public:
DomainParser(Context &context);
ast::DomainPointer parse();
private:
void findSections(ast::Domain &domain);
void parseRequirementSection(ast::Domain &domain);
void computeDerivedRequirements(ast::Domain &domain);
void parseTypeSection(ast::Domain &domain);
void parseConstantSection(ast::Domain &domain);
void parsePredicateSection(ast::Domain &domain);
void parseActionSection(ast::Domain &domain);
Context &m_context;
tokenize::StreamPosition m_requirementsPosition;
tokenize::StreamPosition m_typesPosition;
tokenize::StreamPosition m_constantsPosition;
tokenize::StreamPosition m_predicatesPosition;
std::vector<tokenize::StreamPosition> m_actionPositions;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,27 @@
#ifndef __PDDL__DETAIL__PARSING__EFFECT_H
#define __PDDL__DETAIL__PARSING__EFFECT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Effect
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::Effect> parseEffect(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,252 @@
#ifndef __PDDL__DETAIL__PARSING__EXPRESSIONS_H
#define __PDDL__DETAIL__PARSING__EXPRESSIONS_H
#include <pddl/AST.h>
#include <pddl/Context.h>
#include <pddl/Exception.h>
#include <pddl/detail/ASTContext.h>
#include <pddl/detail/VariableStack.h>
#include <pddl/detail/parsing/VariableDeclaration.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::AndPointer<Argument>> parseAnd(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::AtPointer<Argument>> parseAt(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::EitherPointer<Argument>> parseEither(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::ExistsPointer<Argument>> parseExists(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::ForAllPointer<Argument>> parseForAll(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::ImplyPointer<Argument>> parseImply(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::NotPointer<Argument>> parseNot(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::OrPointer<Argument>> parseOr(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument);
template<typename ArgumentLeft, typename ArgumentRight, typename ArgumentLeftParser, typename ArgumentRightParser>
std::experimental::optional<ast::WhenPointer<ArgumentLeft, ArgumentRight>> parseWhen(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentLeftParser parseArgumentLeft, ArgumentRightParser parseArgumentRight);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions: Base Classes
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, typename ArgumentLeftParser, typename ArgumentRightParser>
std::experimental::optional<std::unique_ptr<Derived>> parseBinary(Context &context,
ASTContext &astContext, VariableStack &variableStack, ArgumentLeftParser parseArgumentLeft,
ArgumentRightParser parseArgumentRight)
{
auto &tokenizer = context.tokenizer;
const auto position = tokenizer.position();
if (!tokenizer.testAndSkip<std::string>("(")
|| !tokenizer.testIdentifierAndSkip(Derived::Identifier))
{
tokenizer.seek(position);
return std::experimental::nullopt;
}
tokenizer.skipWhiteSpace();
// Parse arguments of the expression
auto argumentLeft = parseArgumentLeft(context, astContext, variableStack);
if (!argumentLeft)
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
auto argumentRight = parseArgumentRight(context, astContext, variableStack);
if (!argumentRight)
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
tokenizer.expect<std::string>(")");
return std::make_unique<Derived>(std::move(argumentLeft.value()), std::move(argumentRight.value()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, typename ArgumentParser>
std::experimental::optional<std::unique_ptr<Derived>> parseNAry(Context &context,
ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
auto &tokenizer = context.tokenizer;
const auto position = tokenizer.position();
if (!tokenizer.testAndSkip<std::string>("(")
|| !tokenizer.testIdentifierAndSkip(Derived::Identifier))
{
tokenizer.seek(position);
return std::experimental::nullopt;
}
typename Derived::Arguments arguments;
tokenizer.skipWhiteSpace();
// Parse arguments of the expression
while (tokenizer.currentCharacter() != ')')
{
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
arguments.emplace_back(std::move(argument.value()));
tokenizer.skipWhiteSpace();
}
if (arguments.empty())
context.warningCallback(tokenizer.location(), "" + std::string(Derived::Identifier) + "” expressions should not be empty");
tokenizer.expect<std::string>(")");
return std::make_unique<Derived>(std::move(arguments));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, typename ArgumentParser>
std::experimental::optional<std::unique_ptr<Derived>> parseQuantified(Context &context,
ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
auto &tokenizer = context.tokenizer;
const auto position = tokenizer.position();
if (!tokenizer.testAndSkip<std::string>("(")
|| !tokenizer.testIdentifierAndSkip(Derived::Identifier))
{
tokenizer.seek(position);
return std::experimental::nullopt;
}
// Parse variable list
tokenizer.expect<std::string>("(");
auto parameters = parseVariableDeclarations(context, *astContext.domain);
tokenizer.expect<std::string>(")");
// Push newly parsed variables to the stack
variableStack.push(&parameters);
// Parse argument of the expression
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
// Clean up variable stack
variableStack.pop();
tokenizer.expect<std::string>(")");
return std::make_unique<Derived>(std::move(parameters), std::move(argument.value()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::AndPointer<Argument>> parseAnd(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
return parseNAry<ast::And<Argument>, ArgumentParser>(context, astContext, variableStack, parseArgument);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::EitherPointer<Argument>> parseEither(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
return parseNAry<ast::Either<Argument>, ArgumentParser>(context, astContext, variableStack, parseArgument);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::ExistsPointer<Argument>> parseExists(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
return parseQuantified<ast::Exists<Argument>, ArgumentParser>(context, astContext, variableStack, parseArgument);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::ForAllPointer<Argument>> parseForAll(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
return parseQuantified<ast::ForAll<Argument>, ArgumentParser>(context, astContext, variableStack, parseArgument);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::ImplyPointer<Argument>> parseImply(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
return parseBinary<ast::Imply<Argument>, ArgumentParser, ArgumentParser>(context, astContext, variableStack, parseArgument, parseArgument);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::NotPointer<Argument>> parseNot(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
auto &tokenizer = context.tokenizer;
const auto position = tokenizer.position();
if (!tokenizer.testAndSkip<std::string>("(")
|| !tokenizer.testIdentifierAndSkip("not"))
{
tokenizer.seek(position);
return std::experimental::nullopt;
}
tokenizer.skipWhiteSpace();
// Parse argument
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer.location(), "could not parse argument of “not” expression");
tokenizer.expect<std::string>(")");
return std::make_unique<ast::Not<Argument>>(std::move(argument.value()));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename Argument, typename ArgumentParser>
std::experimental::optional<ast::OrPointer<Argument>> parseOr(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentParser parseArgument)
{
return parseNAry<ast::Or<Argument>, ArgumentParser>(context, astContext, variableStack, parseArgument);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename ArgumentLeft, typename ArgumentRight, typename ArgumentLeftParser, typename ArgumentRightParser>
std::experimental::optional<ast::WhenPointer<ArgumentLeft, ArgumentRight>> parseWhen(Context &context, ASTContext &astContext, VariableStack &variableStack, ArgumentLeftParser parseArgumentLeft, ArgumentRightParser parseArgumentRight)
{
return parseBinary<ast::When<ArgumentLeft, ArgumentRight>, ArgumentLeftParser, ArgumentRightParser>(context, astContext, variableStack, parseArgumentLeft, parseArgumentRight);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,27 @@
#ifndef __PDDL__DETAIL__PARSING__FACT_H
#define __PDDL__DETAIL__PARSING__FACT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fact
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::Fact> parseFact(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,25 @@
#ifndef __PDDL__DETAIL__PARSING__INITIAL_STATE_H
#define __PDDL__DETAIL__PARSING__INITIAL_STATE_H
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// InitialState
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::InitialState parseInitialState(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,41 @@
#ifndef __PDDL__DETAIL__PARSER_H
#define __PDDL__DETAIL__PARSER_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Parser
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
struct Parser
{
};
template<class T, typename... ArgumentParser>
std::experimental::optional<T> parse(Context &context, ASTContext &astContext, ArgumentParser... argumentParsers)
{
return detail::Parser<T>().parse(context, astContext, argumentParsers...);
}
template<class T>
std::experimental::optional<T> parse(Context &context, ASTContext &astContext)
{
return detail::Parser<T>().parse(context, astContext);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,28 @@
#ifndef __PDDL__DETAIL__PARSING__PRECONDITION_H
#define __PDDL__DETAIL__PARSING__PRECONDITION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Precondition
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::Precondition> parsePrecondition(Context &context, ASTContext &astContext, VariableStack &variableStack);
std::experimental::optional<ast::Precondition> parsePreconditionBody(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,28 @@
#ifndef __PDDL__DETAIL__PARSING__PREDICATE_H
#define __PDDL__DETAIL__PARSING__PREDICATE_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/ASTContext.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Predicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: rename consistently
std::experimental::optional<ast::PredicatePointer> parsePredicate(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,25 @@
#ifndef __PDDL__DETAIL__PARSING__PREDICATE_DECLARATION_H
#define __PDDL__DETAIL__PARSING__PREDICATE_DECLARATION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PredicateDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddPredicateDeclarations(Context &context, ast::Domain &domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,25 @@
#ifndef __PDDL__DETAIL__PARSING__PRIMITIVE_TYPE_H
#define __PDDL__DETAIL__PARSING__PRIMITIVE_TYPE_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PrimitiveType
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::PrimitiveTypePointer parsePrimitiveType(Context &context, ast::Domain &domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,25 @@
#ifndef __PDDL__DETAIL__PARSING__PRIMITIVE_TYPE_DECLARATION_H
#define __PDDL__DETAIL__PARSING__PRIMITIVE_TYPE_DECLARATION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PrimitiveTypeDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddPrimitiveTypeDeclarations(Context &context, ast::Domain &domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,49 @@
#ifndef __PDDL__DETAIL__PARSING__PROBLEM_H
#define __PDDL__DETAIL__PARSING__PROBLEM_H
#include <pddl/AST.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Problem
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class ProblemParser
{
public:
ProblemParser(Context &context, ast::Domain &domain);
ast::ProblemPointer parse();
private:
void findSections(ast::Problem &problem);
void parseDomainSection(ast::Problem &problem);
void parseRequirementSection(ast::Problem &problem);
void computeDerivedRequirements(ast::Problem &problem);
void parseObjectSection(ast::Problem &problem);
void parseInitialStateSection(ast::Problem &problem);
void parseGoalSection(ast::Problem &problem);
Context &m_context;
ast::Domain &m_domain;
tokenize::StreamPosition m_domainPosition;
tokenize::StreamPosition m_requirementsPosition;
tokenize::StreamPosition m_objectsPosition;
tokenize::StreamPosition m_initialStatePosition;
tokenize::StreamPosition m_goalPosition;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__PARSING__REQUIREMENT_H
#define __PDDL__DETAIL__PARSING__REQUIREMENT_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Requirement
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::Requirement> parseRequirement(Context &context);
const char *toString(const ast::Requirement &requirement);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,25 @@
#ifndef __PDDL__DETAIL__PARSING__TYPE_H
#define __PDDL__DETAIL__PARSING__TYPE_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Type
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::Type parseType(Context &context, ast::Domain &domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__PARSING__UNSUPPORTED_H
#define __PDDL__DETAIL__PARSING__UNSUPPORTED_H
#include <pddl/Context.h>
#include <pddl/Exception.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Unsupported
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ParserException exceptUnsupportedExpression(tokenize::StreamPosition position, Context &context);
ParserException exceptUnsupportedSection(tokenize::StreamPosition position, Context &context);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,44 @@
#ifndef __PDDL__DETAIL__PARSE_UTILS_H
#define __PDDL__DETAIL__PARSE_UTILS_H
#include <pddl/Tokenizer.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ParseUtils
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: refactor
inline void skipSection(Tokenizer &tokenizer)
{
size_t openParentheses = 1;
while (true)
{
const auto character = tokenizer.currentCharacter();
tokenizer.advance();
if (character == '(')
openParentheses++;
else if (character == ')')
{
openParentheses--;
if (openParentheses == 0)
return;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,28 @@
#ifndef __PDDL__DETAIL__PARSING__VARIABLE_H
#define __PDDL__DETAIL__PARSING__VARIABLE_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
#include <pddl/detail/VariableStack.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Variable
//
////////////////////////////////////////////////////////////////////////////////////////////////////
// TODO: find consistent naming scheme
std::experimental::optional<ast::VariablePointer> testParsingVariable(Context &context, VariableStack &variableStack);
ast::VariablePointer parseVariable(Context &context, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -0,0 +1,26 @@
#ifndef __PDDL__DETAIL__PARSING__VARIABLE_DECLARATION_H
#define __PDDL__DETAIL__PARSING__VARIABLE_DECLARATION_H
#include <pddl/ASTForward.h>
#include <pddl/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// VariableDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddVariableDeclarations(Context &context, ast::Domain &domain, ast::VariableDeclarations &variableDeclarations);
ast::VariableDeclarations parseVariableDeclarations(Context &context, ast::Domain &domain);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif