From bac8d5c84231c57f7806f4e951d6a6d833359a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 21 May 2016 15:45:01 +0200 Subject: [PATCH] Outsourced SAS variable parsing. --- include/plasp/sas/Variable.h | 3 ++ src/plasp/sas/Description.cpp | 39 +--------------------- src/plasp/sas/Variable.cpp | 63 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 src/plasp/sas/Variable.cpp diff --git a/include/plasp/sas/Variable.h b/include/plasp/sas/Variable.h index 09049ce..18ccd15 100644 --- a/include/plasp/sas/Variable.h +++ b/include/plasp/sas/Variable.h @@ -1,6 +1,7 @@ #ifndef __SAS__VARIABLE_H #define __SAS__VARIABLE_H +#include #include #include @@ -19,6 +20,8 @@ namespace sas struct Variable { + static Variable fromSAS(std::istream &istream); + using Values = std::vector; std::string name; diff --git a/src/plasp/sas/Description.cpp b/src/plasp/sas/Description.cpp index a825126..a15b9c3 100644 --- a/src/plasp/sas/Description.cpp +++ b/src/plasp/sas/Description.cpp @@ -304,44 +304,7 @@ void Description::parseVariablesSection(std::istream &istream) m_variables.resize(numberOfVariables); for (size_t i = 0; i < numberOfVariables; i++) - { - auto &variable = m_variables[i]; - - utils::parseExpected(istream, "begin_variable"); - - variable.name = utils::parse(istream); - variable.axiomLayer = utils::parse(istream); - - const auto numberOfValues = utils::parse(istream); - variable.values.resize(numberOfValues); - - try - { - for (size_t j = 0; j < numberOfValues; j++) - { - auto &value = variable.values[j]; - - 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 utils::ParserException("Invalid value sign \"" + sasSign + "\""); - - istream.ignore(1); - - std::getline(istream, value.name); - } - } - catch (const std::exception &e) - { - throw utils::ParserException("Could not parse variable " + variable.name + " (" + e.what() + ")"); - } - - utils::parseExpected(istream, "end_variable"); - } + m_variables[i] = Variable::fromSAS(istream); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/sas/Variable.cpp b/src/plasp/sas/Variable.cpp new file mode 100644 index 0000000..97a95f8 --- /dev/null +++ b/src/plasp/sas/Variable.cpp @@ -0,0 +1,63 @@ +#include + +#include + +#include + +namespace plasp +{ +namespace sas +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Variable +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Variable Variable::fromSAS(std::istream &istream) +{ + Variable variable; + + utils::parseExpected(istream, "begin_variable"); + + variable.name = utils::parse(istream); + variable.axiomLayer = utils::parse(istream); + + const auto numberOfValues = utils::parse(istream); + variable.values.resize(numberOfValues); + + try + { + for (size_t j = 0; j < numberOfValues; j++) + { + auto &value = variable.values[j]; + + 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 utils::ParserException("Invalid value sign \"" + sasSign + "\""); + + istream.ignore(1); + + std::getline(istream, value.name); + } + } + catch (const std::exception &e) + { + throw utils::ParserException("Could not parse variable " + variable.name + " (" + e.what() + ")"); + } + + utils::parseExpected(istream, "end_variable"); + + return variable; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +}