Outsourced SAS variable parsing.

This commit is contained in:
Patrick Lühne 2016-05-21 15:45:01 +02:00
parent b2e4d3329a
commit bac8d5c842
3 changed files with 67 additions and 38 deletions

View File

@ -1,6 +1,7 @@
#ifndef __SAS__VARIABLE_H
#define __SAS__VARIABLE_H
#include <iosfwd>
#include <string>
#include <vector>
@ -19,6 +20,8 @@ namespace sas
struct Variable
{
static Variable fromSAS(std::istream &istream);
using Values = std::vector<Value>;
std::string name;

View File

@ -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<std::string>(istream, "begin_variable");
variable.name = utils::parse<std::string>(istream);
variable.axiomLayer = utils::parse<int>(istream);
const auto numberOfValues = utils::parse<size_t>(istream);
variable.values.resize(numberOfValues);
try
{
for (size_t j = 0; j < numberOfValues; j++)
{
auto &value = variable.values[j];
const auto sasSign = utils::parse<std::string>(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<std::string>(istream, "end_variable");
}
m_variables[i] = Variable::fromSAS(istream);
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,63 @@
#include <plasp/sas/Variable.h>
#include <iostream>
#include <plasp/utils/Parsing.h>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Variable
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Variable Variable::fromSAS(std::istream &istream)
{
Variable variable;
utils::parseExpected<std::string>(istream, "begin_variable");
variable.name = utils::parse<std::string>(istream);
variable.axiomLayer = utils::parse<int>(istream);
const auto numberOfValues = utils::parse<size_t>(istream);
variable.values.resize(numberOfValues);
try
{
for (size_t j = 0; j < numberOfValues; j++)
{
auto &value = variable.values[j];
const auto sasSign = utils::parse<std::string>(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<std::string>(istream, "end_variable");
return variable;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}