Reimplemented major parts of parsing preconditions.

This commit is contained in:
2017-06-16 03:34:32 +02:00
parent 62b9da844a
commit a6babf3e90
19 changed files with 540 additions and 38 deletions

View File

@@ -114,7 +114,11 @@ struct PrimitiveTypeDeclaration
struct Unsupported
{
explicit Unsupported() = default;
explicit Unsupported(std::string &&type)
: type{std::move(type)}
{
}
Unsupported(const Unsupported &other) = delete;
Unsupported &operator=(const Unsupported &&other) = delete;
Unsupported(Unsupported &&other) = default;
@@ -165,8 +169,11 @@ struct VariableDeclaration
struct Predicate
{
explicit Predicate(PredicateDeclaration *declaration)
: declaration{declaration}
using Arguments = Terms;
explicit Predicate(Arguments &&arguments, PredicateDeclaration *declaration)
: arguments{std::move(arguments)},
declaration{declaration}
{
}
@@ -175,7 +182,7 @@ struct Predicate
Predicate(Predicate &&other) = default;
Predicate &operator=(Predicate &&other) = default;
Terms arguments;
Arguments arguments;
PredicateDeclaration *declaration;
};

View File

@@ -2,6 +2,7 @@
#define __PDDL_PARSE__DETAIL__AST_COPY_H
#include <pddlparse/AST.h>
#include <pddlparse/Variant.h>
namespace pddl
{
@@ -27,11 +28,11 @@ inline Variable deepCopy(Variable &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class ArgumentLeft, class ArgumentRight = ArgumentLeft>
inline Binary<Derived, ArgumentLeft, ArgumentRight> deepCopy(Binary<Derived, ArgumentLeft, ArgumentRight> &other);
inline Derived deepCopy(Binary<Derived, ArgumentLeft, ArgumentRight> &other);
template<class Derived, class Argument>
inline NAry<Derived, Argument> deepCopy(NAry<Derived, Argument> &other);
inline Derived deepCopy(NAry<Derived, Argument> &other);
template<class Derived, class Argument>
inline Quantified<Derived, Argument> deepCopy(Quantified<Derived, Argument> &other);
inline Derived deepCopy(Quantified<Derived, Argument> &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Expressions
@@ -46,7 +47,14 @@ inline Not<Argument> deepCopy(Not<Argument> &other);
// Variants
////////////////////////////////////////////////////////////////////////////////////////////////////
inline ast::Term deepCopy(ast::Term &other);
inline ast::Type deepCopy(ast::Type &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Unique Pointers
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T>
std::unique_ptr<T> deepCopy(std::unique_ptr<T> &other);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Primitives
@@ -76,10 +84,10 @@ Variable deepCopy(Variable &other)
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class ArgumentLeft, class ArgumentRight>
Binary<Derived, ArgumentLeft, ArgumentRight> deepCopy(Binary<Derived, ArgumentLeft, ArgumentRight> &other)
Derived deepCopy(Binary<Derived, ArgumentLeft, ArgumentRight> &other)
{
auto argumentLeft = deepCopy(other.argumentLeft);
auto argumentRight = deepCopy(other.argumentRight);
auto argumentLeft{deepCopy(other.argumentLeft)};
auto argumentRight{deepCopy(other.argumentRight)};
return Binary<Derived, ArgumentLeft, ArgumentRight>(std::move(argumentLeft), std::move(argumentRight));
}
@@ -87,23 +95,23 @@ Binary<Derived, ArgumentLeft, ArgumentRight> deepCopy(Binary<Derived, ArgumentLe
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
NAry<Derived, Argument> deepCopy(NAry<Derived, Argument> &other)
Derived deepCopy(NAry<Derived, Argument> &other)
{
typename NAry<Derived, Argument>::Arguments arguments;
typename Derived::Arguments arguments;
arguments.reserve(other.arguments.size());
for (auto &argument : other.arguments)
arguments.emplace_back(deepCopy(argument));
return NAry<Derived, Argument>(std::move(arguments));
return Derived(std::move(arguments));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived, class Argument>
Quantified<Derived, Argument> deepCopy(Quantified<Derived, Argument> &other)
Derived deepCopy(Quantified<Derived, Argument> &other)
{
auto argument = deepCopy(other.argument);
auto argument{deepCopy(other.argument)};
return Quantified<Derived, Argument>(std::move(argument));
}
@@ -115,17 +123,27 @@ Quantified<Derived, Argument> deepCopy(Quantified<Derived, Argument> &other)
template<class Argument>
At<Argument> deepCopy(At<Argument> &other)
{
auto argument = deepCopy(other.argument);
auto argument{deepCopy(other.argument)};
return At<Argument>(other.timePoint, std::move(argument));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/*
template<class Argument>
Either<Argument> deepCopy(Either<Argument> &other)
{
auto argument{deepCopy(other.argument)};
return Not<Argument>(std::move(argument));
}*/
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Argument>
Not<Argument> deepCopy(Not<Argument> &other)
{
auto argument = deepCopy(other.argument);
auto argument{deepCopy(other.argument)};
return Not<Argument>(std::move(argument));
}
@@ -143,6 +161,13 @@ struct DeepCopyVisitor
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::Type deepCopy(ast::Type &other)
{
return other.match([](auto &x){deepCopy(x); return std::make_unique<ast::PrimitiveType>(nullptr);});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Unique Pointers
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -17,11 +17,16 @@ namespace detail
class VariableStack
{
public:
void push(ast::VariableDeclarations *variables);
using Layer = ast::VariableDeclarations *;
public:
void push(Layer layer);
void pop();
std::experimental::optional<ast::VariableDeclaration *> findVariableDeclaration(const std::string &variableName);
private:
std::vector<ast::VariableDeclarations *> m_variableStack;
std::vector<Layer> m_layers;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -3,6 +3,7 @@
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/detail/ASTContext.h>
namespace pddl
{
@@ -15,8 +16,7 @@ namespace detail
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::ConstantPointer parseConstant(Context &context, ast::Domain &domain);
ast::ConstantPointer parseConstant(Context &context, ast::Problem &problem);
ast::ConstantPointer parseConstant(Context &context, ASTContext &astContext);
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -63,12 +63,12 @@ std::experimental::optional<std::unique_ptr<Derived>> parseBinary(Context &conte
auto argumentLeft = parseArgument(context, astContext, variableStack);
if (!argumentLeft)
throw ParserException(tokenizer.location(), "could not parse argument of “" + Derived::Identifier + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
auto argumentRight = parseArgument(context, astContext, variableStack);
if (!argumentRight)
throw ParserException(tokenizer.location(), "could not parse argument of “" + Derived::Identifier + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
tokenizer.expect<std::string>(")");
@@ -99,18 +99,18 @@ std::experimental::optional<std::unique_ptr<Derived>> parseNAry(Context &context
// Parse arguments of the expression
while (tokenizer.currentCharacter() != ')')
{
auto &argument = parseArgument(context, astContext, variableStack);
auto argument = parseArgument(context, astContext, variableStack);
if (!argument)
throw ParserException(tokenizer.location(), "could not parse argument of “" + Derived::Identifier + "” expression");
throw ParserException(tokenizer.location(), "could not parse argument of “" + std::string(Derived::Identifier) + "” expression");
arguments.emplace_back(std::move(argument.value));
arguments.emplace_back(std::move(argument.value()));
tokenizer.skipWhiteSpace();
}
if (arguments.empty())
context.warningCallback(tokenizer.location(), "" + Derived::Identifier + "” expressions should not be empty");
context.warningCallback(tokenizer.location(), "" + std::string(Derived::Identifier) + "” expressions should not be empty");
tokenizer.expect<std::string>(")");

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,25 @@
#ifndef __PDDL_PARSE__DETAIL__PARSING__UNSUPPORTED_H
#define __PDDL_PARSE__DETAIL__PARSING__UNSUPPORTED_H
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Unsupported
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::UnsupportedPointer parseUnsupported(Context &context);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@@ -3,6 +3,7 @@
#include <pddlparse/ASTForward.h>
#include <pddlparse/Context.h>
#include <pddlparse/detail/VariableStack.h>
namespace pddl
{
@@ -15,7 +16,7 @@ namespace detail
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::Variable parseVariable(Context &context, ast::Domain &domain);
ast::VariablePointer parseVariable(Context &context, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////