Added command-line option for treating warnings as errors or completely ignoring them.

This commit is contained in:
Patrick Lühne 2016-06-13 14:45:31 +02:00
parent fdbcb261df
commit 5c3ea28e48
6 changed files with 157 additions and 56 deletions

View File

@ -9,6 +9,7 @@
#include <plasp/pddl/TranslatorASP.h> #include <plasp/pddl/TranslatorASP.h>
#include <plasp/sas/Description.h> #include <plasp/sas/Description.h>
#include <plasp/sas/TranslatorASP.h> #include <plasp/sas/TranslatorASP.h>
#include <plasp/utils/ParserWarning.h>
#include <plasp/utils/TranslatorException.h> #include <plasp/utils/TranslatorException.h>
int main(int argc, char **argv) int main(int argc, char **argv)
@ -20,7 +21,8 @@ int main(int argc, char **argv)
("help,h", "Display this help message.") ("help,h", "Display this help message.")
("version,v", "Display version information.") ("version,v", "Display version information.")
("input,i", po::value<std::vector<std::string>>(), "Specify the SAS input file.") ("input,i", po::value<std::vector<std::string>>(), "Specify the SAS input file.")
("language,l", po::value<std::string>(), "Specify the input language (SAS or PDDL). Omit for automatic detection."); ("language,l", po::value<std::string>(), "Specify the input language (SAS or PDDL). Omit for automatic detection.")
("warning-level", po::value<std::string>()->default_value("normal"), "Specify whether to output warnings normally (normal), to treat them as critical errors (error), or to ignore them (ignore).");
po::positional_options_description positionalOptionsDescription; po::positional_options_description positionalOptionsDescription;
positionalOptionsDescription.add("input", -1); positionalOptionsDescription.add("input", -1);
@ -64,6 +66,14 @@ int main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
const auto handleException =
[&](const auto &messagePrefix, const auto &exception)
{
std::cerr << messagePrefix << ": " << exception.what() << std::endl << std::endl;
printHelp();
return EXIT_FAILURE;
};
try try
{ {
plasp::utils::Parser parser; plasp::utils::Parser parser;
@ -105,7 +115,16 @@ int main(int argc, char **argv)
if (language == plasp::Language::Type::PDDL) if (language == plasp::Language::Type::PDDL)
{ {
const auto description = plasp::pddl::Description::fromParser(std::move(parser)); auto context = plasp::pddl::Context(std::move(parser));
const auto warningLevel = variablesMap["warning-level"].as<std::string>();
if (warningLevel == "error")
context.logger.setWarningLevel(plasp::utils::Logger::WarningLevel::Error);
else if (warningLevel == "ignore")
context.logger.setWarningLevel(plasp::utils::Logger::WarningLevel::Ignore);
const auto description = plasp::pddl::Description::fromContext(std::move(context));
const auto translator = plasp::pddl::TranslatorASP(description, std::cout); const auto translator = plasp::pddl::TranslatorASP(description, std::cout);
translator.translate(); translator.translate();
} }
@ -118,21 +137,19 @@ int main(int argc, char **argv)
} }
catch (const plasp::utils::ParserException &e) catch (const plasp::utils::ParserException &e)
{ {
std::cerr << "Parser error: " << e.what() << std::endl << std::endl; handleException("Parser error", e);
printHelp(); }
return EXIT_FAILURE; catch (const plasp::utils::ParserWarning &e)
{
handleException("Parser warning", e);
} }
catch (const plasp::utils::TranslatorException &e) catch (const plasp::utils::TranslatorException &e)
{ {
std::cerr << "Translation error: " << e.what() << std::endl << std::endl; handleException("Translation error", e);
printHelp();
return EXIT_FAILURE;
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
std::cerr << "Error: " << e.what() << std::endl << std::endl; handleException("Error", e);
printHelp();
return EXIT_FAILURE;
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -21,12 +21,42 @@ namespace pddl
class Context class Context
{ {
public: public:
Context(utils::Parser &parser) Context() = default;
: parser(parser)
explicit Context(utils::Parser &&parser)
: parser{std::move(parser)}
{ {
} }
utils::Parser &parser; explicit Context(utils::Logger &&logger)
: logger{std::move(logger)}
{
}
explicit Context(utils::Parser &&parser, utils::Logger &&logger)
: parser{std::move(parser)},
logger{std::move(logger)}
{
}
Context(const Context &other) = delete;
Context &operator=(const Context &other) = delete;
Context(Context &&other)
: parser(std::move(other.parser)),
logger(std::move(other.logger))
{
}
Context &operator=(Context &&other)
{
parser = std::move(other.parser);
logger = std::move(other.logger);
return *this;
}
utils::Parser parser;
utils::Logger logger; utils::Logger logger;
}; };

View File

@ -21,12 +21,15 @@ namespace pddl
class Description class Description
{ {
public: public:
static Description fromParser(utils::Parser &&parser); static Description fromContext(Context &&context);
static Description fromStream(std::istream &istream); static Description fromStream(std::istream &istream);
static Description fromFile(const std::string &path); static Description fromFile(const std::string &path);
static Description fromFiles(const std::vector<std::string> &paths); static Description fromFiles(const std::vector<std::string> &paths);
public: public:
Context &context();
const Context &context() const;
const Domain &domain() const; const Domain &domain() const;
bool containsProblem() const; bool containsProblem() const;
@ -35,12 +38,11 @@ class Description
private: private:
Description(); Description();
void parseContent(); void parse();
void findSections(); void findSections();
void checkConsistency(); void checkConsistency();
utils::Parser m_parser;
Context m_context; Context m_context;
utils::Parser::Position m_domainPosition; utils::Parser::Position m_domainPosition;

View File

@ -18,15 +18,29 @@ namespace utils
class Logger class Logger
{ {
public:
enum class WarningLevel
{
Normal,
Error,
Ignore
};
public: public:
Logger(); Logger();
void setPedantic(bool isPedantic = true); Logger(const Logger &other);
Logger &operator=(const Logger &other);
Logger(Logger &&other);
Logger &operator=(Logger &&other);
void setWarningLevel(WarningLevel warningLevel);
void parserWarning(const Parser &parser, const std::string &message); void parserWarning(const Parser &parser, const std::string &message);
private: private:
bool m_isPedantic; WarningLevel m_warningLevel;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -20,8 +20,7 @@ namespace pddl
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Description::Description() Description::Description()
: m_context(m_parser), : m_domainPosition{-1},
m_domainPosition{-1},
m_domain{std::make_unique<Domain>(Domain(m_context))}, m_domain{std::make_unique<Domain>(Domain(m_context))},
m_problemPosition{-1} m_problemPosition{-1}
{ {
@ -29,17 +28,12 @@ Description::Description()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Description Description::fromParser(utils::Parser &&parser) Description Description::fromContext(Context &&context)
{ {
Description description; Description description;
parser.setCaseSensitive(false); description.m_context = std::move(context);
parser.removeComments(";", "\n", false); description.parse();
description.m_parser = std::move(parser);
description.parseContent();
description.checkConsistency();
return description; return description;
} }
@ -50,13 +44,8 @@ Description Description::fromStream(std::istream &istream)
{ {
Description description; Description description;
description.m_parser.readStream("std::cin", istream); description.m_context.parser.readStream("std::cin", istream);
description.parse();
description.m_parser.setCaseSensitive(false);
description.m_parser.removeComments(";", "\n", false);
description.parseContent();
description.checkConsistency();
return description; return description;
} }
@ -67,13 +56,8 @@ Description Description::fromFile(const std::string &path)
{ {
Description description; Description description;
description.m_parser.readFile(path); description.m_context.parser.readFile(path);
description.parse();
description.m_parser.setCaseSensitive(false);
description.m_parser.removeComments(";", "\n", false);
description.parseContent();
description.checkConsistency();
return description; return description;
} }
@ -89,20 +73,30 @@ Description Description::fromFiles(const std::vector<std::string> &paths)
std::for_each(paths.cbegin(), paths.cend(), std::for_each(paths.cbegin(), paths.cend(),
[&](const auto &path) [&](const auto &path)
{ {
description.m_parser.readFile(path); description.m_context.parser.readFile(path);
}); });
description.m_parser.setCaseSensitive(false); description.parse();
description.m_parser.removeComments(";", "\n", false);
description.parseContent();
description.checkConsistency();
return description; return description;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Context &Description::context()
{
return m_context;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Context &Description::context() const
{
return m_context;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Domain &Description::domain() const const Domain &Description::domain() const
{ {
BOOST_ASSERT(m_domain); BOOST_ASSERT(m_domain);
@ -128,22 +122,29 @@ const Problem &Description::problem() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Description::parseContent() void Description::parse()
{ {
auto &parser = m_context.parser;
parser.setCaseSensitive(false);
parser.removeComments(";", "\n", false);
// First, determine the locations of domain and problem // First, determine the locations of domain and problem
findSections(); findSections();
if (m_domainPosition == -1) if (m_domainPosition == -1)
throw ConsistencyException("No PDDL domain specified"); throw ConsistencyException("No PDDL domain specified");
m_context.parser.seek(m_domainPosition); parser.seek(m_domainPosition);
m_domain->parse(); m_domain->parse();
if (m_problemPosition != -1) if (m_problemPosition != -1)
{ {
m_context.parser.seek(m_problemPosition); parser.seek(m_problemPosition);
m_problem->parse(); m_problem->parse();
} }
checkConsistency();
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -14,22 +14,59 @@ namespace utils
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Logger::Logger() Logger::Logger()
: m_isPedantic{false} : m_warningLevel{Logger::WarningLevel::Normal}
{ {
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Logger::setPedantic(bool isPedantic) Logger::Logger(const Logger &other)
: m_warningLevel{other.m_warningLevel}
{ {
m_isPedantic = isPedantic; }
////////////////////////////////////////////////////////////////////////////////////////////////////
Logger &Logger::operator=(const Logger &other)
{
m_warningLevel = other.m_warningLevel;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Logger::Logger(Logger &&other)
: m_warningLevel{other.m_warningLevel}
{
other.m_warningLevel = WarningLevel::Normal;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Logger &Logger::operator=(Logger &&other)
{
m_warningLevel = other.m_warningLevel;
other.m_warningLevel = WarningLevel::Normal;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Logger::setWarningLevel(WarningLevel warningLevel)
{
m_warningLevel = warningLevel;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Logger::parserWarning(const Parser &parser, const std::string &message) void Logger::parserWarning(const Parser &parser, const std::string &message)
{ {
if (m_isPedantic) if (m_warningLevel == WarningLevel::Ignore)
return;
if (m_warningLevel == WarningLevel::Error)
throw ParserWarning(parser, message); throw ParserWarning(parser, message);
const auto coordinate = parser.coordinate(); const auto coordinate = parser.coordinate();