From f90009b6d1b4c022f8e7bd6441ffe65479c4d275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 17 Jun 2017 19:58:00 +0200 Subject: [PATCH] Reimplemented initial state parser. --- lib/pddlparse/include/pddlparse/ASTForward.h | 5 +- .../include/pddlparse/detail/parsing/Fact.h | 27 ++++++++++ .../pddlparse/detail/parsing/InitialState.h | 2 +- .../src/pddlparse/detail/parsing/Fact.cpp | 53 +++++++++++++++++++ .../pddlparse/detail/parsing/InitialState.cpp | 52 +++--------------- .../src/pddlparse/detail/parsing/Problem.cpp | 8 ++- 6 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 lib/pddlparse/include/pddlparse/detail/parsing/Fact.h create mode 100644 lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp diff --git a/lib/pddlparse/include/pddlparse/ASTForward.h b/lib/pddlparse/include/pddlparse/ASTForward.h index 6a520b5..56b98d0 100644 --- a/lib/pddlparse/include/pddlparse/ASTForward.h +++ b/lib/pddlparse/include/pddlparse/ASTForward.h @@ -241,11 +241,14 @@ using Literals = std::vector; //////////////////////////////////////////////////////////////////////////////////////////////////// +class Fact; + namespace detail { using FactT = Variant< AtomicFormula, - AtPointer>; + AtPointer, + NotPointer>; } class Fact : public detail::FactT diff --git a/lib/pddlparse/include/pddlparse/detail/parsing/Fact.h b/lib/pddlparse/include/pddlparse/detail/parsing/Fact.h new file mode 100644 index 0000000..e55b076 --- /dev/null +++ b/lib/pddlparse/include/pddlparse/detail/parsing/Fact.h @@ -0,0 +1,27 @@ +#ifndef __PDDL_PARSE__DETAIL__PARSING__FACT_H +#define __PDDL_PARSE__DETAIL__PARSING__FACT_H + +#include +#include +#include +#include + +namespace pddl +{ +namespace detail +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Fact +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +std::experimental::optional parseFact(Context &context, ASTContext &astContext, VariableStack &variableStack); + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/lib/pddlparse/include/pddlparse/detail/parsing/InitialState.h b/lib/pddlparse/include/pddlparse/detail/parsing/InitialState.h index 10afbe2..b4c41a9 100644 --- a/lib/pddlparse/include/pddlparse/detail/parsing/InitialState.h +++ b/lib/pddlparse/include/pddlparse/detail/parsing/InitialState.h @@ -15,7 +15,7 @@ namespace detail // //////////////////////////////////////////////////////////////////////////////////////////////////// -ast::InitialState parseInitialState(Context &context, ASTContext &expressionContext); +ast::InitialState parseInitialState(Context &context, ASTContext &astContext, VariableStack &variableStack); //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp b/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp new file mode 100644 index 0000000..02806a4 --- /dev/null +++ b/lib/pddlparse/src/pddlparse/detail/parsing/Fact.cpp @@ -0,0 +1,53 @@ +#include + +#include +#include +#include +#include + +namespace pddl +{ +namespace detail +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Fact +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +std::experimental::optional parseFact(Context &context, ASTContext &astContext, VariableStack &variableStack) +{ + auto &tokenizer = context.tokenizer; + + // Test unsupported expressions first + const auto position = tokenizer.position(); + + tokenizer.expect("("); + tokenizer.skipWhiteSpace(); + + if (tokenizer.testIdentifierAndReturn("=") + || tokenizer.testIdentifierAndReturn("at")) + { + tokenizer.seek(position); + return parseUnsupported(context); + } + + tokenizer.seek(position); + + // Now, test supported expressions + std::experimental::optional fact; + + if ((fact = parseNot(context, astContext, variableStack, parseAtomicFormula)) + || (fact = parseAtomicFormula(context, astContext, variableStack))) + { + return std::move(fact.value()); + } + + return parseAtomicFormula(context, astContext, variableStack); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/lib/pddlparse/src/pddlparse/detail/parsing/InitialState.cpp b/lib/pddlparse/src/pddlparse/detail/parsing/InitialState.cpp index 362969e..fdfa7c3 100644 --- a/lib/pddlparse/src/pddlparse/detail/parsing/InitialState.cpp +++ b/lib/pddlparse/src/pddlparse/detail/parsing/InitialState.cpp @@ -2,6 +2,7 @@ #include #include +#include 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("("); - - 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; } diff --git a/lib/pddlparse/src/pddlparse/detail/parsing/Problem.cpp b/lib/pddlparse/src/pddlparse/detail/parsing/Problem.cpp index 21709df..b307cdb 100644 --- a/lib/pddlparse/src/pddlparse/detail/parsing/Problem.cpp +++ b/lib/pddlparse/src/pddlparse/detail/parsing/Problem.cpp @@ -267,12 +267,10 @@ void ProblemParser::parseInitialStateSection(ast::Problem &problem) tokenizer.expect("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(")"); + problem.initialState = parseInitialState(m_context, astContext, variableStack); + tokenizer.expect(")"); skipSection(tokenizer); }