diff --git a/lib/pddlparse/include/pddlparse/ASTOutput.h b/lib/pddlparse/include/pddlparse/ASTOutput.h new file mode 100644 index 0000000..f880ba9 --- /dev/null +++ b/lib/pddlparse/include/pddlparse/ASTOutput.h @@ -0,0 +1,728 @@ +#ifndef __PDDL_PARSE__AST_OUTPUT_H +#define __PDDL_PARSE__AST_OUTPUT_H + +#include +#include + +#include +#include + +namespace pddl +{ +namespace ast +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Output +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +namespace detail +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +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()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +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; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Forward declarations +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Constant &constant, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const ConstantDeclaration &constantDeclaration, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveType &primitiveType, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveTypeDeclaration &primitiveTypeDeclaration, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variable &variable, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const VariableDeclaration &variableDeclaration, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Predicate &predicate, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PredicateDeclaration &predicateDeclaration, detail::PrintContext &printContext); + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Binary &binary, detail::PrintContext &printContext); +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const NAry &nAry, detail::PrintContext &printContext); +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Quantified &quantified, detail::PrintContext &printContext); + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const At &at, detail::PrintContext &printContext); +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Not ¬_, detail::PrintContext &printContext); + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Action &action, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain &domain, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const InitialState &initialState, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Problem &problem, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Requirement &requirement, detail::PrintContext &printContext); +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Description &description, detail::PrintContext &printContext); + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::unique_ptr &variant, detail::PrintContext &printContext); +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::vector &variant, detail::PrintContext &printContext); +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variant &variant, detail::PrintContext &printContext); + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Primitives +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Constant &constant, detail::PrintContext &) +{ + return stream << detail::Constant(constant.declaration->name); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const ConstantDeclaration &constantDeclaration, detail::PrintContext &printContext) +{ + stream << 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, detail::PrintContext &) +{ + return stream << detail::Type(primitiveType.declaration->name); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PrimitiveTypeDeclaration &primitiveTypeDeclaration, detail::PrintContext &printContext) +{ + if (primitiveTypeDeclaration.parentTypes.empty()) + return (stream << detail::Type(primitiveTypeDeclaration.name)); + + if (!primitiveTypeDeclaration.parentTypes.empty()) + for (const auto &parentType : primitiveTypeDeclaration.parentTypes) + { + if (&parentType != &primitiveTypeDeclaration.parentTypes.front()) + printIndentedNewline(stream, printContext); + + stream << detail::Type(primitiveTypeDeclaration.name) << " - " << detail::Type(parentType->declaration->name); + } + + return stream; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Unsupported &unsupported, detail::PrintContext &) +{ + const auto unsupportedName = ""; + + return (stream << colorlog::Reserved(unsupportedName.c_str())); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variable &variable, 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, 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, detail::PrintContext &printContext) +{ + stream << "(" << detail::Identifier(predicate.declaration->name) << " "; + + for (const auto &argument : predicate.arguments) + { + if (&argument != &predicate.arguments.front()) + stream << " "; + + print(stream, argument, printContext); + } + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const PredicateDeclaration &predicateDeclaration, detail::PrintContext &printContext) +{ + stream << "(" << detail::Identifier(predicateDeclaration.name); + + for (const auto ¶meter : predicateDeclaration.parameters) + { + stream << " "; + print(stream, *parameter, printContext); + } + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Expressions: Base Classes +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Binary &binary, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword(Derived::Identifier); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + print(stream, binary.argumentLeft, printContext); + printIndentedNewline(stream, printContext); + print(stream, binary.argumentRight, printContext); + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const NAry &nAry, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword(Derived::Identifier); + + printContext.indentationLevel++; + + for (const auto &argument : nAry.arguments) + { + printIndentedNewline(stream, printContext); + print(stream, argument, printContext); + } + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Quantified &quantified, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword(Derived::Identifier); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + print(stream, quantified.parameters, printContext); + printIndentedNewline(stream, printContext); + print(stream, quantified.argument, printContext); + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Expressions +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const And &and_, detail::PrintContext &printContext) +{ + return print(stream, static_cast, Argument> &>(and_), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const At &at, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword("at"); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + stream << colorlog::Number(at.timePoint); + printIndentedNewline(stream, printContext); + print(stream, at.argument, printContext); + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Either &either, detail::PrintContext &printContext) +{ + return print(stream, static_cast, Argument> &>(either), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Exists &exists, detail::PrintContext &printContext) +{ + return print(stream, static_cast, Argument> &>(exists), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const ForAll &forAll, detail::PrintContext &printContext) +{ + return print(stream, static_cast, Argument> &>(forAll), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Imply &imply, detail::PrintContext &printContext) +{ + return print(stream, static_cast, Argument> &>(imply), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Not ¬_, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword("not"); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + print(stream, not_.argument, printContext); + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Or &or_, detail::PrintContext &printContext) +{ + return print(stream, static_cast, Argument> &>(or_), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const When &when, detail::PrintContext &printContext) +{ + return print(stream, static_cast, ArgumentLeft, ArgumentRight> &>(when), printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// PDDL Structure +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Action &action, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword(":action") << " " << detail::Identifier(action.name); + + printContext.indentationLevel++; + + if (!action.parameters.empty()) + { + printIndentedNewline(stream, printContext); + stream << "(" << colorlog::Keyword(":parameters"); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + 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); + stream << ")"; + + printContext.indentationLevel--; + } + + if (action.effect) + { + printIndentedNewline(stream, printContext); + stream << "(" << colorlog::Keyword(":effect"); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + print(stream, action.effect.value(), printContext); + stream << ")"; + + printContext.indentationLevel--; + } + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Domain &domain, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword("define") << " (" << colorlog::Keyword("domain") << " " << detail::Identifier(domain.name) << ")"; + + printContext.indentationLevel++; + + if (!domain.requirements.empty()) + { + printIndentedNewline(stream, printContext); + stream << "(" << colorlog::Keyword(":requirements"); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + print(stream, domain.requirements, printContext); + stream << ")"; + + 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.actions.empty()) + { + printIndentedNewline(stream, printContext); + print(stream, domain.actions, printContext); + } + + printContext.indentationLevel--; + + return (stream << ")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const InitialState &initialState, 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, detail::PrintContext &printContext) +{ + stream << "(" << colorlog::Keyword("define") << " (" << colorlog::Keyword("problem") << " " << detail::Identifier(problem.name) << ")"; + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + stream << "(" << colorlog::Keyword(":domain") << " " << detail::Identifier(problem.domain->name) << ")"; + + if (!problem.requirements.empty()) + { + printIndentedNewline(stream, printContext); + stream << "(" << colorlog::Keyword(":requirements"); + + printContext.indentationLevel++; + + printIndentedNewline(stream, printContext); + print(stream, problem.requirements, printContext); + stream << ")"; + + printContext.indentationLevel--; + } + + 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 Requirement &requirement, detail::PrintContext &) +{ + auto requirementName = std::string(":") + pddl::detail::toString(requirement); + + return (stream << detail::Identifier(requirementName.c_str())); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Description &description, 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) +{ + detail::PrintContext printContext; + + return print(stream, description, printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// Variants +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::unique_ptr &uniquePointer, detail::PrintContext &printContext) +{ + assert(uniquePointer); + return print(stream, *uniquePointer, printContext); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const std::vector &vector, detail::PrintContext &printContext) +{ + for (const auto &element : vector) + { + if (&element != &vector.front()) + printIndentedNewline(stream, printContext); + + print(stream, element, printContext); + } + + return stream; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline colorlog::ColorStream &print(colorlog::ColorStream &stream, const Variant &variant, detail::PrintContext &printContext) +{ + variant.match([&](const auto &x){return print(stream, x, printContext);}); + + return stream; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/lib/pddlparse/src/CMakeLists.txt b/lib/pddlparse/src/CMakeLists.txt index 58421ff..0eb1160 100644 --- a/lib/pddlparse/src/CMakeLists.txt +++ b/lib/pddlparse/src/CMakeLists.txt @@ -11,6 +11,7 @@ file(GLOB detail_parsing_headers "../include/pddlparse/detail/parsing/*.h") set(includes ${PROJECT_SOURCE_DIR}/include + ${PROJECT_SOURCE_DIR}/../../lib/colorlog/include ${PROJECT_SOURCE_DIR}/../../lib/tokenize/include ${PROJECT_SOURCE_DIR}/../../lib/variant/include )