From 081f0eb70fd05205bbd139722fe2162e8fcae096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 21 May 2016 14:14:14 +0200 Subject: [PATCH] Moved parsing utilities to separate namespace. --- include/plasp/sas/Description.h | 2 - .../plasp/{sas => utils}/ParserException.h | 6 +- include/plasp/utils/Parsing.h | 42 ++++++++++ src/CMakeLists.txt | 6 ++ src/plasp/sas/Description.cpp | 78 +++++++------------ 5 files changed, 81 insertions(+), 53 deletions(-) rename include/plasp/{sas => utils}/ParserException.h (91%) create mode 100644 include/plasp/utils/Parsing.h diff --git a/include/plasp/sas/Description.h b/include/plasp/sas/Description.h index 8814365..076f071 100644 --- a/include/plasp/sas/Description.h +++ b/include/plasp/sas/Description.h @@ -44,8 +44,6 @@ class Description void parseSectionIdentifier(std::istream &istream, const std::string &expectedSectionIdentifier) const; - template - T parse(std::istream &istream) const; const Variable &parseVariable(std::istream &istream) const; const Value &parseValue(std::istream &istream, const Variable &variable) const; AssignedVariable parseAssignedVariable(std::istream &istream) const; diff --git a/include/plasp/sas/ParserException.h b/include/plasp/utils/ParserException.h similarity index 91% rename from include/plasp/sas/ParserException.h rename to include/plasp/utils/ParserException.h index 6ba04a6..1b5054d 100644 --- a/include/plasp/sas/ParserException.h +++ b/include/plasp/utils/ParserException.h @@ -1,12 +1,12 @@ -#ifndef __SAS__PARSER_EXCEPTION_H -#define __SAS__PARSER_EXCEPTION_H +#ifndef __UTILS__PARSER_EXCEPTION_H +#define __UTILS__PARSER_EXCEPTION_H #include #include namespace plasp { -namespace sas +namespace utils { //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/utils/Parsing.h b/include/plasp/utils/Parsing.h new file mode 100644 index 0000000..3a3a34f --- /dev/null +++ b/include/plasp/utils/Parsing.h @@ -0,0 +1,42 @@ +#ifndef __UTILS__PARSING_H +#define __UTILS__PARSING_H + +#include +#include +#include +#include + +namespace plasp +{ +namespace utils +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Parsing +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +T parse(std::istream &istream) +{ + T value; + + try + { + istream >> value; + } + catch (const std::exception &e) + { + throw ParserException(std::string("Could not parse value of type ") + typeid(T).name() + " (" + e.what() + ")"); + } + + return value; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index be5d1e2..3f90d47 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,9 @@ file(GLOB core_headers "../include/plasp/*.h") file(GLOB sas_sources "plasp/sas/*.cpp") file(GLOB sas_headers "../include/plasp/sas/*.h") +file(GLOB utils_sources "plasp/utils/*.cpp") +file(GLOB utils_headers "../include/plasp/utils/*.h") + include_directories( ${Boost_INCLUDE_DIRS} ) @@ -20,6 +23,9 @@ set(sources ${sas_sources} ${sas_headers} + + ${utils_sources} + ${utils_headers} ) set(libraries diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index 56c7287..2cf84f6 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -6,7 +6,8 @@ #include -#include +#include +#include namespace plasp { @@ -225,39 +226,20 @@ void Description::print(std::ostream &ostream) const void Description::parseSectionIdentifier(std::istream &istream, const std::string &expectedSectionIdentifier) const { - const auto sectionIdentifier = parse(istream); + const auto sectionIdentifier = utils::parse(istream); if (sectionIdentifier != expectedSectionIdentifier) - throw ParserException("Invalid format, expected " + expectedSectionIdentifier + ", got " + sectionIdentifier); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -T Description::parse(std::istream &istream) const -{ - T value; - - try - { - istream >> value; - } - catch (const std::exception &e) - { - throw ParserException(std::string("Could not parse value of type ") + typeid(T).name() + " (" + e.what() + ")"); - } - - return value; + throw utils::ParserException("Invalid format, expected " + expectedSectionIdentifier + ", got " + sectionIdentifier); } //////////////////////////////////////////////////////////////////////////////////////////////////// const Variable &Description::parseVariable(std::istream &istream) const { - const auto variableID = parse(istream); + const auto variableID = utils::parse(istream); if (variableID >= m_variables.size()) - throw ParserException("Variable index out of range (index " + std::to_string(variableID) + ")"); + throw utils::ParserException("Variable index out of range (index " + std::to_string(variableID) + ")"); return m_variables[variableID]; } @@ -266,13 +248,13 @@ const Variable &Description::parseVariable(std::istream &istream) const const Value &Description::parseValue(std::istream &istream, const Variable &variable) const { - const auto valueID = parse(istream); + const auto valueID = utils::parse(istream); if (valueID == -1) return Value::Any; if (valueID < 0 || static_cast(valueID) >= variable.values.size()) - throw ParserException("Value index out of range (variable " + variable.name + ", index " + std::to_string(valueID) + ")"); + throw utils::ParserException("Value index out of range (variable " + variable.name + ", index " + std::to_string(valueID) + ")"); return variable.values[valueID]; } @@ -305,10 +287,10 @@ void Description::parseVersionSection(std::istream &istream) const // Version section parseSectionIdentifier(istream, "begin_version"); - const auto formatVersion = parse(istream); + const auto formatVersion = utils::parse(istream); if (formatVersion != 3) - throw ParserException("Unsupported SAS format version (" + std::to_string(formatVersion) + ")"); + throw utils::ParserException("Unsupported SAS format version (" + std::to_string(formatVersion) + ")"); parseSectionIdentifier(istream, "end_version"); } @@ -319,7 +301,7 @@ void Description::parseMetricSection(std::istream &istream) { parseSectionIdentifier(istream, "begin_metric"); - m_usesActionCosts = parse(istream); + m_usesActionCosts = utils::parse(istream); parseSectionIdentifier(istream, "end_metric"); } @@ -328,7 +310,7 @@ void Description::parseMetricSection(std::istream &istream) void Description::parseVariablesSection(std::istream &istream) { - const auto numberOfVariables = parse(istream); + const auto numberOfVariables = utils::parse(istream); m_variables.resize(numberOfVariables); for (size_t i = 0; i < numberOfVariables; i++) @@ -337,10 +319,10 @@ void Description::parseVariablesSection(std::istream &istream) parseSectionIdentifier(istream, "begin_variable"); - variable.name = parse(istream); - variable.axiomLayer = parse(istream); + variable.name = utils::parse(istream); + variable.axiomLayer = utils::parse(istream); - const auto numberOfValues = parse(istream); + const auto numberOfValues = utils::parse(istream); variable.values.resize(numberOfValues); try @@ -349,14 +331,14 @@ void Description::parseVariablesSection(std::istream &istream) { auto &value = variable.values[j]; - const auto sasSign = parse(istream); + const auto sasSign = utils::parse(istream); if (sasSign == "Atom") value.sign = Value::Sign::Positive; else if (sasSign == "NegatedAtom") value.sign = Value::Sign::Negative; else - throw ParserException("Invalid value sign \"" + sasSign + "\""); + throw utils::ParserException("Invalid value sign \"" + sasSign + "\""); istream.ignore(1); @@ -365,7 +347,7 @@ void Description::parseVariablesSection(std::istream &istream) } catch (const std::exception &e) { - throw ParserException("Could not parse variable " + variable.name + " (" + e.what() + ")"); + throw utils::ParserException("Could not parse variable " + variable.name + " (" + e.what() + ")"); } parseSectionIdentifier(istream, "end_variable"); @@ -376,7 +358,7 @@ void Description::parseVariablesSection(std::istream &istream) void Description::parseMutexSection(std::istream &istream) { - const auto numberOfMutexGroups = parse(istream); + const auto numberOfMutexGroups = utils::parse(istream); m_mutexGroups.resize(numberOfMutexGroups); for (size_t i = 0; i < numberOfMutexGroups; i++) @@ -385,7 +367,7 @@ void Description::parseMutexSection(std::istream &istream) auto &mutexGroup = m_mutexGroups[i]; - const auto numberOfFacts = parse(istream); + const auto numberOfFacts = utils::parse(istream); mutexGroup.facts.reserve(numberOfFacts); for (size_t j = 0; j < numberOfFacts; j++) @@ -423,7 +405,7 @@ void Description::parseGoalSection(std::istream &istream) { parseSectionIdentifier(istream, "begin_goal"); - const auto numberOfGoalFacts = parse(istream); + const auto numberOfGoalFacts = utils::parse(istream); m_goalFacts.reserve(numberOfGoalFacts); for (size_t i = 0; i < numberOfGoalFacts; i++) @@ -439,7 +421,7 @@ void Description::parseGoalSection(std::istream &istream) void Description::parseOperatorSection(std::istream &istream) { - const auto numberOfOperators = parse(istream); + const auto numberOfOperators = utils::parse(istream); m_operators.resize(numberOfOperators); for (size_t i = 0; i < numberOfOperators; i++) @@ -458,7 +440,7 @@ void Description::parseOperatorSection(std::istream &istream) std::stringstream lineStream(line); - operator_.predicate.name = parse(lineStream); + operator_.predicate.name = utils::parse(lineStream); while (lineStream.peek() == ' ') lineStream.ignore(1); @@ -468,10 +450,10 @@ void Description::parseOperatorSection(std::istream &istream) } catch (const std::exception &e) { - throw ParserException("Could not parse operator predicate"); + throw utils::ParserException("Could not parse operator predicate"); } - const auto numberOfPrevailConditions = parse(istream); + const auto numberOfPrevailConditions = utils::parse(istream); operator_.preconditions.reserve(numberOfPrevailConditions); for (size_t j = 0; j < numberOfPrevailConditions; j++) @@ -480,14 +462,14 @@ void Description::parseOperatorSection(std::istream &istream) operator_.preconditions.push_back(std::move(precondition)); } - const auto numberOfEffects = parse(istream); + const auto numberOfEffects = utils::parse(istream); operator_.effects.reserve(numberOfEffects); for (size_t j = 0; j < numberOfEffects; j++) { Effect::Conditions conditions; - const auto numberOfEffectConditions = parse(istream); + const auto numberOfEffectConditions = utils::parse(istream); conditions.reserve(numberOfEffectConditions); for (size_t k = 0; k < numberOfEffectConditions; k++) @@ -506,7 +488,7 @@ void Description::parseOperatorSection(std::istream &istream) operator_.effects.push_back(std::move(effect)); } - operator_.costs = parse(istream); + operator_.costs = utils::parse(istream); parseSectionIdentifier(istream, "end_operator"); } @@ -516,14 +498,14 @@ void Description::parseOperatorSection(std::istream &istream) void Description::parseAxiomSection(std::istream &istream) { - const auto numberOfAxiomRules = parse(istream); + const auto numberOfAxiomRules = utils::parse(istream); m_axiomRules.reserve(numberOfAxiomRules); for (size_t i = 0; i < numberOfAxiomRules; i++) { parseSectionIdentifier(istream, "begin_rule"); - const auto numberOfConditions = parse(istream); + const auto numberOfConditions = utils::parse(istream); AxiomRule::Conditions conditions; conditions.reserve(numberOfConditions);