Escaping predicates containing hyphens for ASP output.

This commit is contained in:
Patrick Lühne 2016-05-22 20:19:45 +02:00
parent 4258dfcfd0
commit d8d6998936
4 changed files with 48 additions and 5 deletions

View File

@ -1,12 +1,15 @@
#ifndef __UTILS__PARSING_H
#define __UTILS__PARSING_H
#include <algorithm>
#include <exception>
#include <iosfwd>
#include <sstream>
#include <string>
#include <typeinfo>
#include <boost/algorithm/string/replace.hpp>
#include <plasp/utils/ParserException.h>
namespace plasp
@ -56,6 +59,30 @@ void parseExpected(std::istream &istream, const T &expectedValue)
////////////////////////////////////////////////////////////////////////////////////////////////////
inline std::string escapeASP(const std::string &string)
{
auto escaped = string;
boost::replace_all(escaped, "_", "__");
boost::replace_all(escaped, "-", "_h");
return escaped;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
inline std::string unescapeASP(const std::string &string)
{
auto unescaped = string;
boost::replace_all(unescaped, "_h", "-");
boost::replace_all(unescaped, "__", "_");
return unescaped;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -85,18 +85,18 @@ void Predicate::printAsASP(std::ostream &ostream) const
{
if (m_arguments.empty())
{
ostream << m_name;
ostream << utils::escapeASP(m_name);
return;
}
ostream << m_name << "(";
ostream << utils::escapeASP(m_name) << "(";
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (i > 0)
ostream << ", ";
ostream << m_arguments[i];
ostream << utils::escapeASP(m_arguments[i]);
}
ostream << ")";

View File

@ -108,14 +108,14 @@ void Value::printAsASP(std::ostream &ostream) const
if (m_sign == Value::Sign::Negative)
ostream << "not ";
ostream << m_name;
ostream << utils::escapeASP(m_name);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Value::printAsASPCommaSeparated(std::ostream &ostream) const
{
ostream << m_name << ", " << (m_sign == Sign::Positive ? "true" : "false");
ostream << utils::escapeASP(m_name) << ", " << (m_sign == Sign::Positive ? "true" : "false");
}
////////////////////////////////////////////////////////////////////////////////////////////////////

16
tests/TestUtils.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <gtest/gtest.h>
#include <plasp/utils/Parsing.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(UtilsTests, EscapeASP)
{
const std::string predicate = "action(stack_on(block-1, block-2))";
const auto escaped = plasp::utils::escapeASP(predicate);
const auto unescaped = plasp::utils::unescapeASP(escaped);
ASSERT_EQ(escaped.find("-"), std::string::npos);
ASSERT_EQ(predicate, unescaped);
}