Put parsing of typed variables into Variables class.

This commit is contained in:
Patrick Lühne 2016-06-01 17:20:34 +02:00
parent 79773ba634
commit 0a4541a401
3 changed files with 33 additions and 27 deletions

View File

@ -27,7 +27,8 @@ using Variables = std::vector<Variable>;
class Variable
{
public:
static Variable parse(utils::Parser &parser, Context &context);
static Variable parse(utils::Parser &parser);
static void parseTyped(utils::Parser &parser, Context &context, std::vector<Variable> &variables);
public:
const std::string &name() const;

View File

@ -39,31 +39,7 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
// Parse arguments
while (parser.currentCharacter() != ')')
{
predicate->m_arguments.emplace_back(Variable::parse(parser, context));
parser.skipWhiteSpace();
// Check if the variable has a type declaration
if (!parser.advanceIf('-'))
continue;
// Parse argument type
const auto type = parseType(parser, context);
// Set the argument type for all previously flagged arguments
std::for_each(predicate->m_arguments.begin(), predicate->m_arguments.end(),
[&](auto &argument)
{
if (!argument.isDirty())
return;
argument.setType(type);
argument.setDirty(false);
});
parser.skipWhiteSpace();
}
Variable::parseTyped(parser, context, predicate->m_arguments);
parser.expect<std::string>(")");

View File

@ -4,6 +4,7 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/Type.h>
namespace plasp
{
@ -24,7 +25,7 @@ Variable::Variable(std::string name)
////////////////////////////////////////////////////////////////////////////////////////////////////
Variable Variable::parse(utils::Parser &parser, Context &context)
Variable Variable::parse(utils::Parser &parser)
{
parser.skipWhiteSpace();
@ -40,6 +41,34 @@ Variable Variable::parse(utils::Parser &parser, Context &context)
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::parseTyped(utils::Parser &parser, Context &context, std::vector<Variable> &variables)
{
// Parse and store variable itself
variables.emplace_back(parse(parser));
parser.skipWhiteSpace();
// Check if the variable has a type declaration
if (!parser.advanceIf('-'))
return;
// Parse argument type
const auto type = parseType(parser, context);
// Set the argument type for all previously flagged arguments
std::for_each(variables.begin(), variables.end(),
[&](auto &variable)
{
if (!variable.isDirty())
return;
variable.setType(type);
variable.setDirty(false);
});
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::setDirty(bool isDirty)
{
m_isDirty = isDirty;