From 2c564f47d344c563e391dda7df0382c0cf2cc464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 28 May 2016 14:21:05 +0200 Subject: [PATCH] Refactored predicate argument parsing. --- include/plasp/utils/Parser.h | 36 ++++++++++++++++++++++++++++++++++++ src/plasp/sas/Predicate.cpp | 15 +++++---------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/include/plasp/utils/Parser.h b/include/plasp/utils/Parser.h index ae30750..c650d1e 100644 --- a/include/plasp/utils/Parser.h +++ b/include/plasp/utils/Parser.h @@ -3,6 +3,7 @@ #include #include +#include namespace plasp { @@ -29,6 +30,9 @@ class Parser template Type parse(); + template + void parseUntil(std::vector &container, CharacterCondition characterCondition); + template void expect(const Type &expectedValue); @@ -59,6 +63,38 @@ class Parser //////////////////////////////////////////////////////////////////////////////////////////////////// +template +void Parser::parseUntil(std::vector &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)); + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } } diff --git a/src/plasp/sas/Predicate.cpp b/src/plasp/sas/Predicate.cpp index 4f1b541..9f4d0c7 100644 --- a/src/plasp/sas/Predicate.cpp +++ b/src/plasp/sas/Predicate.cpp @@ -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::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) {