Refactored interface for parsing Types.

This commit is contained in:
Patrick Lühne 2016-05-31 14:01:18 +02:00
parent 1395b62e60
commit cf1c66a085
3 changed files with 34 additions and 30 deletions

View File

@ -29,7 +29,8 @@ using TypeHashMap = std::unordered_map<std::string, Type>;
class Type class Type
{ {
public: 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: public:
const std::string &name() const; const std::string &name() const;

View File

@ -200,10 +200,10 @@ void Domain::computeDerivedRequirements()
void Domain::parseTypingSection(utils::Parser &parser) void Domain::parseTypingSection(utils::Parser &parser)
{ {
// Parses a type and potentially its parent type // Store types and their parent types
while (parser.currentCharacter() != ')') while (parser.currentCharacter() != ')')
{ {
Type::parsePDDL(parser, m_context); Type::parseWithInheritance(parser, m_context);
parser.skipWhiteSpace(); parser.skipWhiteSpace();
} }

View File

@ -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 parser.skipWhiteSpace();
const auto parseType =
[&]() -> auto &
{
parser.skipWhiteSpace();
const auto typeName = parser.parseIdentifier(isIdentifier); const auto typeName = parser.parseIdentifier(isIdentifier);
const auto match = context.types.find(typeName); const auto match = context.types.find(typeName);
const auto typeExists = (match != context.types.cend()); const auto typeExists = (match != context.types.cend());
if (typeExists) if (typeExists)
{ {
auto &type = match->second; auto &type = match->second;
type.setDirty(); type.setDirty();
return type; return type;
} }
const auto insertionResult = context.types.emplace(std::make_pair(typeName, Type(typeName))); const auto insertionResult = context.types.emplace(std::make_pair(typeName, Type(typeName)));
auto &type = insertionResult.first->second; auto &type = insertionResult.first->second;
// Flag type for potentially upcoming parent type declaration // Flag type for potentially upcoming parent type declaration
type.setDirty(); type.setDirty();
// Flag type as correctly declared in the types section // Flag type as correctly declared in the types section
type.setDeclared(); 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(); parser.skipWhiteSpace();
// Check for type inheritance // Check for type inheritance
if (!parser.advanceIf('-')) if (!parser.advanceIf('-'))
return; return type;
// If existing, parse parent type // If existing, parse and store parent type
auto &parentType = parseType(); auto &parentType = parse(parser, context);
parentType.setDirty(false); parentType.setDirty(false);
@ -81,6 +82,8 @@ void Type::parsePDDL(utils::Parser &parser, Context &context)
childType.second.addParentType(parentType); childType.second.addParentType(parentType);
childType.second.setDirty(false); childType.second.setDirty(false);
}); });
return type;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////