diff --git a/include/plasp/sas/Description.h b/include/plasp/sas/Description.h index 076f071..9524ad1 100644 --- a/include/plasp/sas/Description.h +++ b/include/plasp/sas/Description.h @@ -42,8 +42,6 @@ class Description private: Description(); - void parseSectionIdentifier(std::istream &istream, const std::string &expectedSectionIdentifier) 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/utils/Parsing.h b/include/plasp/utils/Parsing.h index bf94f16..bf445f4 100644 --- a/include/plasp/utils/Parsing.h +++ b/include/plasp/utils/Parsing.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -38,6 +39,23 @@ T parse(std::istream &istream) //////////////////////////////////////////////////////////////////////////////////////////////////// +template +void parseExpected(std::istream &istream, const T &expectedValue) +{ + const auto value = parse(istream); + + if (value == expectedValue) + return; + + std::stringstream errorStream; + + errorStream << "Invalid format, expected " << expectedValue << ", got " + value; + + throw utils::ParserException(errorStream.str()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } } diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index 2cf84f6..a825126 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -224,16 +224,6 @@ void Description::print(std::ostream &ostream) const //////////////////////////////////////////////////////////////////////////////////////////////////// -void Description::parseSectionIdentifier(std::istream &istream, const std::string &expectedSectionIdentifier) const -{ - const auto sectionIdentifier = utils::parse(istream); - - if (sectionIdentifier != expectedSectionIdentifier) - throw utils::ParserException("Invalid format, expected " + expectedSectionIdentifier + ", got " + sectionIdentifier); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - const Variable &Description::parseVariable(std::istream &istream) const { const auto variableID = utils::parse(istream); @@ -285,25 +275,25 @@ VariableTransition Description::parseVariableTransition(std::istream &istream) c void Description::parseVersionSection(std::istream &istream) const { // Version section - parseSectionIdentifier(istream, "begin_version"); + utils::parseExpected(istream, "begin_version"); const auto formatVersion = utils::parse(istream); if (formatVersion != 3) throw utils::ParserException("Unsupported SAS format version (" + std::to_string(formatVersion) + ")"); - parseSectionIdentifier(istream, "end_version"); + utils::parseExpected(istream, "end_version"); } //////////////////////////////////////////////////////////////////////////////////////////////////// void Description::parseMetricSection(std::istream &istream) { - parseSectionIdentifier(istream, "begin_metric"); + utils::parseExpected(istream, "begin_metric"); m_usesActionCosts = utils::parse(istream); - parseSectionIdentifier(istream, "end_metric"); + utils::parseExpected(istream, "end_metric"); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -317,7 +307,7 @@ void Description::parseVariablesSection(std::istream &istream) { auto &variable = m_variables[i]; - parseSectionIdentifier(istream, "begin_variable"); + utils::parseExpected(istream, "begin_variable"); variable.name = utils::parse(istream); variable.axiomLayer = utils::parse(istream); @@ -350,7 +340,7 @@ void Description::parseVariablesSection(std::istream &istream) throw utils::ParserException("Could not parse variable " + variable.name + " (" + e.what() + ")"); } - parseSectionIdentifier(istream, "end_variable"); + utils::parseExpected(istream, "end_variable"); } } @@ -363,7 +353,7 @@ void Description::parseMutexSection(std::istream &istream) for (size_t i = 0; i < numberOfMutexGroups; i++) { - parseSectionIdentifier(istream, "begin_mutex_group"); + utils::parseExpected(istream, "begin_mutex_group"); auto &mutexGroup = m_mutexGroups[i]; @@ -376,7 +366,7 @@ void Description::parseMutexSection(std::istream &istream) mutexGroup.facts.push_back(std::move(fact)); } - parseSectionIdentifier(istream, "end_mutex_group"); + utils::parseExpected(istream, "end_mutex_group"); } } @@ -384,7 +374,7 @@ void Description::parseMutexSection(std::istream &istream) void Description::parseInitialStateSection(std::istream &istream) { - parseSectionIdentifier(istream, "begin_state"); + utils::parseExpected(istream, "begin_state"); m_initialStateFacts.reserve(m_variables.size()); @@ -396,14 +386,14 @@ void Description::parseInitialStateSection(std::istream &istream) m_initialStateFacts.push_back({variable, value}); } - parseSectionIdentifier(istream, "end_state"); + utils::parseExpected(istream, "end_state"); } //////////////////////////////////////////////////////////////////////////////////////////////////// void Description::parseGoalSection(std::istream &istream) { - parseSectionIdentifier(istream, "begin_goal"); + utils::parseExpected(istream, "begin_goal"); const auto numberOfGoalFacts = utils::parse(istream); m_goalFacts.reserve(numberOfGoalFacts); @@ -414,7 +404,7 @@ void Description::parseGoalSection(std::istream &istream) m_goalFacts.push_back(std::move(goalFact)); } - parseSectionIdentifier(istream, "end_goal"); + utils::parseExpected(istream, "end_goal"); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -426,7 +416,7 @@ void Description::parseOperatorSection(std::istream &istream) for (size_t i = 0; i < numberOfOperators; i++) { - parseSectionIdentifier(istream, "begin_operator"); + utils::parseExpected(istream, "begin_operator"); auto &operator_ = m_operators[i]; @@ -490,7 +480,7 @@ void Description::parseOperatorSection(std::istream &istream) operator_.costs = utils::parse(istream); - parseSectionIdentifier(istream, "end_operator"); + utils::parseExpected(istream, "end_operator"); } } @@ -503,7 +493,7 @@ void Description::parseAxiomSection(std::istream &istream) for (size_t i = 0; i < numberOfAxiomRules; i++) { - parseSectionIdentifier(istream, "begin_rule"); + utils::parseExpected(istream, "begin_rule"); const auto numberOfConditions = utils::parse(istream); @@ -525,7 +515,7 @@ void Description::parseAxiomSection(std::istream &istream) const AxiomRule axiomRule = {std::move(conditions), std::move(postcondition)}; m_axiomRules.push_back(std::move(axiomRule)); - parseSectionIdentifier(istream, "end_rule"); + utils::parseExpected(istream, "end_rule"); } }