patrick
/
plasp
Archived
1
0
Fork 0
This repository has been archived on 2023-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
plasp/lib/pddlparse/src/pddlparse/detail/parsing/VariableDeclaration.cpp

70 lines
2.0 KiB
C++

#include <pddlparse/detail/parsing/VariableDeclaration.h>
#include <pddlparse/AST.h>
#include <pddlparse/ParserException.h>
#include <pddlparse/detail/ASTCopy.h>
#include <pddlparse/detail/parsing/PrimitiveType.h>
namespace pddl
{
namespace detail
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// VariableDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void parseAndAddUntypedVariableDeclaration(Context &context, ast::VariableDeclarations &variableDeclarations)
{
auto &tokenizer = context.tokenizer;
tokenizer.expect<std::string>("?");
auto variableName = tokenizer.getIdentifier();
assert(variableName != "-");
variableDeclarations.emplace_back(std::make_unique<ast::VariableDeclaration>(std::move(variableName)));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ast::VariableDeclarations parseVariableDeclarations(Context &context, ast::Domain &domain)
{
ast::VariableDeclarations variableDeclarations;
auto &tokenizer = context.tokenizer;
tokenizer.skipWhiteSpace();
// Index on the first element of the current inheritance list
size_t inheritanceIndex = 0;
while (tokenizer.currentCharacter() != ')')
{
parseAndAddUntypedVariableDeclaration(context, variableDeclarations);
tokenizer.skipWhiteSpace();
if (!tokenizer.testAndSkip<char>('-'))
continue;
// If existing, parse and store parent type
auto parentType = parsePrimitiveType(context, domain);
for (size_t i = inheritanceIndex; i < variableDeclarations.size(); i++)
variableDeclarations[i]->type = ast::deepCopy(parentType);
// All types up to now are labeled with their parent types
inheritanceIndex = variableDeclarations.size() + 1;
tokenizer.skipWhiteSpace();
}
return variableDeclarations;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}