From cf1c66a085c4b6ba67d96207992773522b2b939c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 31 May 2016 14:01:18 +0200 Subject: [PATCH] Refactored interface for parsing Types. --- include/plasp/pddl/Type.h | 3 ++- src/plasp/pddl/Domain.cpp | 4 +-- src/plasp/pddl/Type.cpp | 57 ++++++++++++++++++++------------------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/include/plasp/pddl/Type.h b/include/plasp/pddl/Type.h index 001f702..f59377b 100644 --- a/include/plasp/pddl/Type.h +++ b/include/plasp/pddl/Type.h @@ -29,7 +29,8 @@ using TypeHashMap = std::unordered_map; class Type { public: - static void parsePDDL(utils::Parser &parser, Context &context); + static Type &parse(utils::Parser &parser, Context &context); + static Type &parseWithInheritance(utils::Parser &parser, Context &context); public: const std::string &name() const; diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index a025490..09fb06c 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -200,10 +200,10 @@ void Domain::computeDerivedRequirements() void Domain::parseTypingSection(utils::Parser &parser) { - // Parses a type and potentially its parent type + // Store types and their parent types while (parser.currentCharacter() != ')') { - Type::parsePDDL(parser, m_context); + Type::parseWithInheritance(parser, m_context); parser.skipWhiteSpace(); } diff --git a/src/plasp/pddl/Type.cpp b/src/plasp/pddl/Type.cpp index 9c22942..e5adadb 100644 --- a/src/plasp/pddl/Type.cpp +++ b/src/plasp/pddl/Type.cpp @@ -25,49 +25,50 @@ Type::Type(std::string name) //////////////////////////////////////////////////////////////////////////////////////////////////// -void Type::parsePDDL(utils::Parser &parser, Context &context) +Type &Type::parse(utils::Parser &parser, Context &context) { - // Parses a single type identifier - const auto parseType = - [&]() -> auto & - { - parser.skipWhiteSpace(); + parser.skipWhiteSpace(); - const auto typeName = parser.parseIdentifier(isIdentifier); - const auto match = context.types.find(typeName); - const auto typeExists = (match != context.types.cend()); + const auto typeName = parser.parseIdentifier(isIdentifier); + const auto match = context.types.find(typeName); + const auto typeExists = (match != context.types.cend()); - if (typeExists) - { - auto &type = match->second; + if (typeExists) + { + auto &type = match->second; - type.setDirty(); + type.setDirty(); - return type; - } + return type; + } - const auto insertionResult = context.types.emplace(std::make_pair(typeName, Type(typeName))); - auto &type = insertionResult.first->second; + const auto insertionResult = context.types.emplace(std::make_pair(typeName, Type(typeName))); + auto &type = insertionResult.first->second; - // Flag type for potentially upcoming parent type declaration - type.setDirty(); + // Flag type for potentially upcoming parent type declaration + type.setDirty(); - // Flag type as correctly declared in the types section - type.setDeclared(); + // Flag type as correctly declared in the types section + type.setDeclared(); - return type; - }; + return type; +} - parseType(); +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Type &Type::parseWithInheritance(utils::Parser &parser, Context &context) +{ + // Parse and store type + auto &type = parse(parser, context); parser.skipWhiteSpace(); // Check for type inheritance if (!parser.advanceIf('-')) - return; + return type; - // If existing, parse parent type - auto &parentType = parseType(); + // If existing, parse and store parent type + auto &parentType = parse(parser, context); parentType.setDirty(false); @@ -81,6 +82,8 @@ void Type::parsePDDL(utils::Parser &parser, Context &context) childType.second.addParentType(parentType); childType.second.setDirty(false); }); + + return type; } ////////////////////////////////////////////////////////////////////////////////////////////////////