patrick
/
plasp
Archived
1
0
Fork 0
This repository has been archived on 2023-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
plasp/src/plasp/sas/Predicate.cpp

107 lines
2.2 KiB
C++
Raw Normal View History

#include <plasp/sas/Predicate.h>
#include <iostream>
2016-05-22 16:00:58 +02:00
#include <limits>
#include <sstream>
#include <plasp/output/Formatting.h>
2017-05-12 14:17:57 +02:00
#include <tokenize/TokenizerException.h>
namespace plasp
{
namespace sas
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Predicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
2017-05-12 14:17:57 +02:00
Predicate Predicate::fromSAS(tokenize::Tokenizer<> &tokenizer)
2016-05-22 16:00:58 +02:00
{
Predicate predicate;
try
{
2017-05-12 14:17:57 +02:00
tokenizer.skipLine();
2016-05-22 16:00:58 +02:00
2017-05-12 14:17:57 +02:00
predicate.m_name = tokenizer.get<std::string>();
2016-05-22 16:00:58 +02:00
2016-05-29 15:08:10 +02:00
while (true)
{
// Skip whitespace but not newlines
2017-05-12 14:17:57 +02:00
tokenizer.skipBlankSpace();
2016-05-29 15:08:10 +02:00
// TODO: check \r handling
2017-05-12 14:17:57 +02:00
if (tokenizer.currentCharacter() == '\n')
2016-05-29 15:08:10 +02:00
break;
2017-05-12 14:17:57 +02:00
const auto value = tokenizer.get<std::string>();
2016-05-29 15:08:10 +02:00
predicate.m_arguments.emplace_back(std::move(value));
}
2016-05-22 16:00:58 +02:00
}
catch (const std::exception &e)
{
2017-05-12 14:17:57 +02:00
throw tokenize::TokenizerException(tokenizer.location(), "could not parse operator predicate");
2016-05-22 16:00:58 +02:00
}
return predicate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &Predicate::name() const
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Predicate::Arguments &Predicate::arguments() const
{
return m_arguments;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::printAsSAS(output::ColorStream &stream) const
2016-05-22 16:00:58 +02:00
{
if (m_arguments.empty())
{
stream << m_name;
2016-05-22 16:00:58 +02:00
return;
}
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (i > 0)
stream << " ";
2016-05-22 16:00:58 +02:00
stream << m_arguments[i];
2016-05-22 16:00:58 +02:00
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::printAsASP(output::ColorStream &stream) const
{
2016-05-22 16:00:58 +02:00
if (m_arguments.empty())
{
stream << output::String(m_name.c_str());
2016-05-22 16:00:58 +02:00
return;
}
2016-05-21 02:31:47 +02:00
stream << "(" << output::String(m_name.c_str());
2016-05-22 16:00:58 +02:00
for (size_t i = 0; i < m_arguments.size(); i++)
stream << ", " << output::String(m_arguments[i].c_str());
stream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}