Reimplemented initial state parser.

This commit is contained in:
Patrick Lühne 2017-06-17 19:58:00 +02:00
parent fbcb790611
commit f90009b6d1
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
6 changed files with 96 additions and 51 deletions

View File

@ -241,11 +241,14 @@ using Literals = std::vector<Literal>;
////////////////////////////////////////////////////////////////////////////////////////////////////
class Fact;
namespace detail
{
using FactT = Variant<
AtomicFormula,
AtPointer<Literal>>;
AtPointer<Literal>,
NotPointer<Fact>>;
}
class Fact : public detail::FactT

View File

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

View File

@ -15,7 +15,7 @@ namespace detail
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::InitialState parseInitialState(Context &context, ASTContext &expressionContext);
ast::InitialState parseInitialState(Context &context, ASTContext &astContext, VariableStack &variableStack);
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,53 @@
#include <pddlparse/detail/parsing/Fact.h>
#include <pddlparse/AST.h>
#include <pddlparse/detail/parsing/AtomicFormula.h>
#include <pddlparse/detail/parsing/Expressions.h>
#include <pddlparse/detail/parsing/Unsupported.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Fact
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::experimental::optional<ast::Fact> parseFact(Context &context, ASTContext &astContext, VariableStack &variableStack)
{
auto &tokenizer = context.tokenizer;
// Test unsupported expressions first
const auto position = tokenizer.position();
tokenizer.expect<std::string>("(");
tokenizer.skipWhiteSpace();
if (tokenizer.testIdentifierAndReturn("=")
|| tokenizer.testIdentifierAndReturn("at"))
{
tokenizer.seek(position);
return parseUnsupported(context);
}
tokenizer.seek(position);
// Now, test supported expressions
std::experimental::optional<ast::Fact> fact;
if ((fact = parseNot<ast::Fact>(context, astContext, variableStack, parseAtomicFormula))
|| (fact = parseAtomicFormula(context, astContext, variableStack)))
{
return std::move(fact.value());
}
return parseAtomicFormula(context, astContext, variableStack);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -2,6 +2,7 @@
#include <pddlparse/AST.h>
#include <pddlparse/ParserException.h>
#include <pddlparse/detail/parsing/Fact.h>
namespace pddl
{
@ -14,62 +15,25 @@ namespace detail
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::InitialState parseInitialState(Context &context, ASTContext &)
ast::InitialState parseInitialState(Context &context, ASTContext &astContext, VariableStack &variableStack)
{
auto &tokenizer = context.tokenizer;
ast::InitialState initialState;
context.warningCallback(tokenizer.location(), "initial state parser under construction, section is currently ignored");
// TODO: reimplement
/*const auto parseInitialStateElement =
[&]() -> ExpressionPointer
{
ExpressionPointer expression;
// TODO: do not allow negative initial state literals
if ((expression = parseLiteral(context, expressionContext))
|| (expression = expressions::At::parse(context, expressionContext, parseLiteral)))
{
return expression;
}
const auto position = tokenizer.position();
tokenizer.expect<std::string>("(");
const auto expressionIdentifierPosition = tokenizer.position();
if (tokenizer.testIdentifierAndSkip("="))
{
tokenizer.seek(expressionIdentifierPosition);
const auto expressionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
return expressions::Unsupported::parse(context);
}
tokenizer.seek(expressionIdentifierPosition);
const auto expressionIdentifier = tokenizer.getIdentifier();
tokenizer.seek(position);
throw tokenize::TokenizerException(tokenizer.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in this context");
};
tokenizer.skipWhiteSpace();
while (tokenizer.currentCharacter() != ')')
{
ast::Expression expression;
auto fact = parseFact(context, astContext, variableStack);
if ((expression = parseInitialStateElement()))
initialState->m_facts.emplace_back(std::move(expression));
if (!fact)
throw ParserException(tokenizer.location(), "invalid initial state fact");
initialState.facts.emplace_back(std::move(fact.value()));
tokenizer.skipWhiteSpace();
}*/
skipSection(tokenizer);
}
return initialState;
}

View File

@ -267,12 +267,10 @@ void ProblemParser::parseInitialStateSection(ast::Problem &problem)
tokenizer.expect<std::string>("init");
ASTContext astContext(problem);
VariableStack variableStack;
m_context.warningCallback(tokenizer.location(), "initial state parser under construction, section is currently ignored");
// TODO: reimplement
//problem.initialState = parseInitialState(m_context, astContext);
//tokenizer.expect<std::string>(")");
problem.initialState = parseInitialState(m_context, astContext, variableStack);
tokenizer.expect<std::string>(")");
skipSection(tokenizer);
}