diff --git a/include/plasp/utils/Parsing.h b/include/plasp/utils/Parsing.h index bf445f4..2641541 100644 --- a/include/plasp/utils/Parsing.h +++ b/include/plasp/utils/Parsing.h @@ -1,12 +1,15 @@ #ifndef __UTILS__PARSING_H #define __UTILS__PARSING_H +#include #include #include #include #include #include +#include + #include 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; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } } diff --git a/src/plasp/sas/Predicate.cpp b/src/plasp/sas/Predicate.cpp index d506276..7536a89 100644 --- a/src/plasp/sas/Predicate.cpp +++ b/src/plasp/sas/Predicate.cpp @@ -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 << ")"; diff --git a/src/plasp/sas/Value.cpp b/src/plasp/sas/Value.cpp index 80f52bc..2cf91c7 100644 --- a/src/plasp/sas/Value.cpp +++ b/src/plasp/sas/Value.cpp @@ -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"); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/tests/TestUtils.cpp b/tests/TestUtils.cpp new file mode 100644 index 0000000..d96b84a --- /dev/null +++ b/tests/TestUtils.cpp @@ -0,0 +1,16 @@ +#include + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +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); +}