Continued working on reimplementing action parser.

This commit is contained in:
2017-06-16 04:45:04 +02:00
parent 9fbe0db567
commit 78b4636028
6 changed files with 244 additions and 33 deletions

View File

@@ -15,7 +15,32 @@ namespace detail
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddAction(Context &context, ast::Domain &domain);
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::Stream::Position m_parametersPosition;
tokenize::Stream::Position m_preconditionPosition;
tokenize::Stream::Position m_effectPosition;
// For compatibility with old PDDL versions
tokenize::Stream::Position m_varsPosition;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -146,7 +146,7 @@ std::experimental::optional<std::unique_ptr<Derived>> parseQuantified(Context &c
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");
// Clean up variable stack
variableStack.pop();
@@ -176,6 +176,22 @@ std::experimental::optional<ast::EitherPointer<Argument>> parseEither(Context &c
////////////////////////////////////////////////////////////////////////////////////////////////////
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)
{
@@ -184,6 +200,35 @@ std::experimental::optional<ast::ImplyPointer<Argument>> parseImply(Context &con
////////////////////////////////////////////////////////////////////////////////////////////////////
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)
{

View File

@@ -15,6 +15,7 @@ namespace detail
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddVariableDeclarations(Context &context, ast::Domain &domain, ast::VariableDeclarations &variableDeclarations);
ast::VariableDeclarations parseVariableDeclarations(Context &context, ast::Domain &domain);
////////////////////////////////////////////////////////////////////////////////////////////////////