Refactored predicate argument parsing.

This commit is contained in:
Patrick Lühne 2016-05-28 14:21:05 +02:00
parent b4bc347006
commit 2c564f47d3
2 changed files with 41 additions and 10 deletions

View File

@ -3,6 +3,7 @@
#include <iostream>
#include <iterator>
#include <vector>
namespace plasp
{
@ -29,6 +30,9 @@ class Parser
template<typename Type>
Type parse();
template<class CharacterCondition>
void parseUntil(std::vector<std::string> &container, CharacterCondition characterCondition);
template<typename Type>
void expect(const Type &expectedValue);
@ -59,6 +63,38 @@ class Parser
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class CharacterCondition>
void Parser::parseUntil(std::vector<std::string> &container, CharacterCondition characterCondition)
{
while (true)
{
skipWhiteSpace();
std::string value;
while (true)
{
const auto character = *m_position;
if (characterCondition(character))
{
container.emplace_back(std::move(value));
return;
}
if (std::isspace(character))
break;
value.push_back(character);
advance();
}
container.emplace_back(std::move(value));
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -26,17 +26,12 @@ Predicate Predicate::fromSAS(utils::Parser &parser)
{
parser.skipLine();
// TODO: Inefficient, reimplement in one pass
const std::string line = parser.getLine();
predicate.m_name = parser.parse<std::string>();
std::stringstream lineStream(line);
lineStream >> predicate.m_name;
while (lineStream.peek() == ' ')
lineStream.ignore(1);
for (std::string argument; std::getline(lineStream, argument, ' ');)
predicate.m_arguments.push_back(std::move(argument));
parser.parseUntil(predicate.m_arguments, [](const auto character)
{
return character == '\n';
});
}
catch (const std::exception &e)
{