diff --git a/include/plasp/pddl/Variable.h b/include/plasp/pddl/Variable.h index 6ea62a3..0be375d 100644 --- a/include/plasp/pddl/Variable.h +++ b/include/plasp/pddl/Variable.h @@ -27,7 +27,8 @@ using Variables = std::vector; 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 &variables); public: const std::string &name() const; diff --git a/src/plasp/pddl/Predicate.cpp b/src/plasp/pddl/Predicate.cpp index e44bac0..1183b73 100644 --- a/src/plasp/pddl/Predicate.cpp +++ b/src/plasp/pddl/Predicate.cpp @@ -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(")"); diff --git a/src/plasp/pddl/Variable.cpp b/src/plasp/pddl/Variable.cpp index d916b06..69008b3 100644 --- a/src/plasp/pddl/Variable.cpp +++ b/src/plasp/pddl/Variable.cpp @@ -4,6 +4,7 @@ #include #include +#include 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 &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;