2016-11-22 03:15:52 +01:00
|
|
|
#ifndef __ANTHEM__UTILS_H
|
|
|
|
#define __ANTHEM__UTILS_H
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <clingo.hh>
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
#include <anthem/Context.h>
|
|
|
|
#include <anthem/input/Location.h>
|
|
|
|
|
2016-11-22 03:15:52 +01:00
|
|
|
namespace anthem
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Utils
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
template<class T1, class T2>
|
|
|
|
T1 location_cast(const T2 &location);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
template<>
|
2016-11-24 13:27:22 +01:00
|
|
|
inline input::Location location_cast(const Clingo::Location &location)
|
2016-11-24 00:52:28 +01:00
|
|
|
{
|
|
|
|
return {location.begin_file(), location.end_file(), location.begin_line(), location.end_line(),
|
|
|
|
location.begin_column(), location.end_column()};
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
inline void throwErrorAtLocation(const Clingo::Location &clingoLocation, const char *errorMessage,
|
|
|
|
Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
{
|
2016-11-24 00:52:28 +01:00
|
|
|
const auto location = location_cast<input::Location>(clingoLocation);
|
|
|
|
|
2017-04-05 18:15:42 +02:00
|
|
|
// TODO: think about removing this to avoid double error messages
|
2016-11-24 00:52:28 +01:00
|
|
|
context.logger.log(output::Priority::Error, location, errorMessage);
|
2016-11-23 03:29:26 +01:00
|
|
|
|
|
|
|
throw std::runtime_error(errorMessage);
|
2016-11-22 03:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-24 13:42:36 +01:00
|
|
|
inline bool isPrefix(const char *prefix, const char *string)
|
|
|
|
{
|
|
|
|
const auto prefixLength = std::strlen(prefix);
|
|
|
|
const auto stringLength = std::strlen(string);
|
|
|
|
|
|
|
|
if (stringLength < prefixLength)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return std::strncmp(prefix, string, prefixLength) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-24 15:24:16 +01:00
|
|
|
constexpr const auto AuxiliaryHeadVariablePrefix = "V";
|
|
|
|
constexpr const auto AuxiliaryBodyVariablePrefix = "X";
|
2017-03-29 21:28:46 +02:00
|
|
|
constexpr const auto AnonymousVariablePrefix = "A";
|
2016-11-24 13:42:36 +01:00
|
|
|
constexpr const auto UserVariablePrefix = "_";
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
inline bool isReservedVariableName(const char *variableName)
|
|
|
|
{
|
|
|
|
if (!isPrefix(AuxiliaryBodyVariablePrefix, variableName)
|
2017-03-29 21:28:46 +02:00
|
|
|
&& !isPrefix(AuxiliaryHeadVariablePrefix, variableName)
|
|
|
|
&& !isPrefix(AnonymousVariablePrefix, variableName))
|
2016-11-24 13:42:36 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(std::strlen(AuxiliaryBodyVariablePrefix) == std::strlen(AuxiliaryHeadVariablePrefix));
|
2017-03-29 21:28:46 +02:00
|
|
|
assert(std::strlen(AuxiliaryBodyVariablePrefix) == std::strlen(AnonymousVariablePrefix));
|
2017-05-23 15:25:55 +02:00
|
|
|
assert(std::strlen(AuxiliaryBodyVariablePrefix) == std::strlen(UserVariablePrefix));
|
2016-11-24 13:42:36 +01:00
|
|
|
|
|
|
|
const auto prefixLength = std::strlen(AuxiliaryBodyVariablePrefix);
|
|
|
|
|
|
|
|
if (strlen(variableName) == prefixLength)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char *suffix = variableName + prefixLength;
|
|
|
|
|
|
|
|
for (const auto *i = suffix; *i != '\0'; i++)
|
|
|
|
if (!std::isdigit(*i))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-23 03:29:26 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-22 03:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|