2016-05-29 16:27:11 +02:00
|
|
|
#include <algorithm>
|
2016-05-20 15:29:24 +02:00
|
|
|
#include <iostream>
|
2016-05-29 16:27:11 +02:00
|
|
|
#include <string>
|
2016-05-20 15:29:24 +02:00
|
|
|
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
2017-06-22 20:58:31 +02:00
|
|
|
#include <colorlog/ColorStream.h>
|
|
|
|
#include <colorlog/Logger.h>
|
|
|
|
#include <colorlog/Priority.h>
|
|
|
|
|
2017-06-20 01:53:55 +02:00
|
|
|
#include <pddlparse/AST.h>
|
2017-06-21 03:07:09 +02:00
|
|
|
#include <pddlparse/Exception.h>
|
2017-06-20 03:23:19 +02:00
|
|
|
#include <pddlparse/Mode.h>
|
2017-06-23 04:18:07 +02:00
|
|
|
#include <pddlparse/Normalize.h>
|
|
|
|
#include <pddlparse/NormalizedASTOutput.h>
|
2017-06-20 01:53:55 +02:00
|
|
|
#include <pddlparse/Parse.h>
|
|
|
|
|
2016-06-10 01:23:41 +02:00
|
|
|
#include <plasp/LanguageDetection.h>
|
2017-06-22 20:58:31 +02:00
|
|
|
#include <plasp/TranslatorException.h>
|
|
|
|
|
2016-06-10 17:40:32 +02:00
|
|
|
#include <plasp/pddl/TranslatorASP.h>
|
2016-05-20 15:29:24 +02:00
|
|
|
#include <plasp/sas/Description.h>
|
2016-05-21 02:43:07 +02:00
|
|
|
#include <plasp/sas/TranslatorASP.h>
|
2016-05-20 15:29:24 +02:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
2016-12-01 17:18:58 +01:00
|
|
|
bool warningsAsErrors;
|
|
|
|
|
2016-05-20 15:29:24 +02:00
|
|
|
po::options_description description("Allowed options");
|
|
|
|
description.add_options()
|
2016-11-29 02:01:23 +01:00
|
|
|
("help,h", "Display this help message")
|
|
|
|
("version,v", "Display version information")
|
|
|
|
("input,i", po::value<std::vector<std::string>>(), "Input files (in PDDL or SAS format)")
|
2017-06-20 03:23:19 +02:00
|
|
|
("parsing-mode", po::value<std::string>()->default_value("strict"), "Parsing mode (strict, compatibility)")
|
2016-11-29 02:01:23 +01:00
|
|
|
("language,l", po::value<std::string>()->default_value("auto"), "Input language (pddl, sas, auto)")
|
2016-12-01 17:18:58 +01:00
|
|
|
("color", po::value<std::string>()->default_value("auto"), "Colorize output (always, never, auto)")
|
2017-06-21 03:05:37 +02:00
|
|
|
("log-priority,p", po::value<std::string>()->default_value("info"), "Log messages starting from this priority (debug, info, warning, error)")
|
2016-12-01 17:18:58 +01:00
|
|
|
("warnings-as-errors", po::bool_switch(&warningsAsErrors), "Treat warnings as errors");
|
2016-05-20 15:29:24 +02:00
|
|
|
|
|
|
|
po::positional_options_description positionalOptionsDescription;
|
|
|
|
positionalOptionsDescription.add("input", -1);
|
|
|
|
|
|
|
|
po::variables_map variablesMap;
|
2016-05-24 16:24:37 +02:00
|
|
|
|
|
|
|
const auto printHelp =
|
|
|
|
[&]()
|
|
|
|
{
|
2016-11-29 02:01:23 +01:00
|
|
|
std::cout << "Usage: plasp [options] file..." << std::endl;
|
|
|
|
std::cout << "Translate PDDL to ASP." << std::endl << std::endl;
|
2016-05-24 16:24:37 +02:00
|
|
|
|
|
|
|
std::cout << description;
|
|
|
|
};
|
|
|
|
|
2017-06-22 20:58:31 +02:00
|
|
|
colorlog::Logger logger;
|
2016-06-14 18:02:59 +02:00
|
|
|
|
2016-05-24 16:24:37 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
po::store(po::command_line_parser(argc, argv)
|
|
|
|
.options(description)
|
|
|
|
.positional(positionalOptionsDescription)
|
|
|
|
.run(),
|
|
|
|
variablesMap);
|
|
|
|
po::notify(variablesMap);
|
|
|
|
}
|
|
|
|
catch (const po::error &e)
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, e.what());
|
2016-06-14 18:02:59 +02:00
|
|
|
std::cout << std::endl;
|
2016-05-24 16:24:37 +02:00
|
|
|
printHelp();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-05-20 15:29:24 +02:00
|
|
|
|
|
|
|
if (variablesMap.count("help"))
|
|
|
|
{
|
2016-05-24 16:24:37 +02:00
|
|
|
printHelp();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (variablesMap.count("version"))
|
|
|
|
{
|
2016-09-02 15:46:58 +02:00
|
|
|
std::cout << "plasp version 3.0.3-git" << std::endl;
|
2016-05-20 15:29:24 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-12-01 17:18:58 +01:00
|
|
|
if (warningsAsErrors)
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.setAbortPriority(colorlog::Priority::Warning);
|
2016-06-14 18:02:59 +02:00
|
|
|
|
2017-06-20 03:23:19 +02:00
|
|
|
auto parsingMode = pddl::Mode::Strict;
|
|
|
|
|
|
|
|
const auto parsingModeString = variablesMap["parsing-mode"].as<std::string>();
|
|
|
|
|
|
|
|
if (parsingModeString == "compatibility")
|
|
|
|
parsingMode = pddl::Mode::Compatibility;
|
|
|
|
else if (parsingModeString != "strict")
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, "unknown parsing mode “" + parsingModeString + "”");
|
2017-06-20 03:23:19 +02:00
|
|
|
std::cout << std::endl;
|
|
|
|
printHelp();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-06-21 03:07:09 +02:00
|
|
|
const auto printCompatibilityInfo =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
if (parsingMode != pddl::Mode::Compatibility)
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Info, "try using --parsing-mode=compatibility for extended legacy feature support");
|
2017-06-21 03:07:09 +02:00
|
|
|
};
|
|
|
|
|
2016-06-14 18:02:59 +02:00
|
|
|
const auto colorPolicy = variablesMap["color"].as<std::string>();
|
|
|
|
|
|
|
|
if (colorPolicy == "auto")
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.setColorPolicy(colorlog::ColorStream::ColorPolicy::Auto);
|
2016-06-14 18:02:59 +02:00
|
|
|
else if (colorPolicy == "never")
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.setColorPolicy(colorlog::ColorStream::ColorPolicy::Never);
|
2016-06-14 18:02:59 +02:00
|
|
|
else if (colorPolicy == "always")
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.setColorPolicy(colorlog::ColorStream::ColorPolicy::Always);
|
2016-06-14 18:47:02 +02:00
|
|
|
else
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, "unknown color policy “" + colorPolicy + "”");
|
2016-06-14 18:47:02 +02:00
|
|
|
std::cout << std::endl;
|
|
|
|
printHelp();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-06-14 18:02:59 +02:00
|
|
|
|
2016-12-01 17:18:58 +01:00
|
|
|
const auto logPriorityString = variablesMap["log-priority"].as<std::string>();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
const auto logPriority = colorlog::priorityFromName(logPriorityString.c_str());
|
2016-12-01 17:18:58 +01:00
|
|
|
logger.setLogPriority(logPriority);
|
|
|
|
}
|
|
|
|
catch (const std::exception &e)
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, ("unknown log priorty “" + logPriorityString + "”").c_str());
|
2016-12-01 17:18:58 +01:00
|
|
|
logger.errorStream() << std::endl;
|
|
|
|
printHelp();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-06-10 17:12:23 +02:00
|
|
|
try
|
2016-06-10 00:59:44 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenize::Tokenizer<tokenize::CaseInsensitiveTokenizerPolicy> tokenizer;
|
2016-06-10 00:59:44 +02:00
|
|
|
|
2016-06-10 17:12:23 +02:00
|
|
|
if (variablesMap.count("input"))
|
2016-06-10 01:23:41 +02:00
|
|
|
{
|
2016-06-10 17:12:23 +02:00
|
|
|
const auto &inputFiles = variablesMap["input"].as<std::vector<std::string>>();
|
|
|
|
|
|
|
|
std::for_each(inputFiles.cbegin(), inputFiles.cend(),
|
|
|
|
[&](const auto &inputFile)
|
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.read(inputFile);
|
2016-06-10 17:12:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.read("std::cin", std::cin);
|
2016-06-10 17:12:23 +02:00
|
|
|
|
|
|
|
const auto detectLanguage =
|
|
|
|
[&]()
|
|
|
|
{
|
|
|
|
const auto languageName = variablesMap["language"].as<std::string>();
|
2016-11-29 02:01:23 +01:00
|
|
|
const auto language = plasp::Language::fromString(languageName);
|
|
|
|
|
|
|
|
if (language == plasp::Language::Type::Automatic)
|
2017-05-12 14:17:57 +02:00
|
|
|
return plasp::detectLanguage(tokenizer);
|
2016-06-10 01:23:41 +02:00
|
|
|
|
2016-11-29 02:01:23 +01:00
|
|
|
return language;
|
2016-06-10 17:12:23 +02:00
|
|
|
};
|
2016-06-10 01:23:41 +02:00
|
|
|
|
2016-06-10 17:12:23 +02:00
|
|
|
const auto language = detectLanguage();
|
2016-06-10 01:23:41 +02:00
|
|
|
|
2016-06-10 17:12:23 +02:00
|
|
|
if (language == plasp::Language::Type::Unknown)
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, "unknown input language");
|
2016-06-14 12:47:39 +02:00
|
|
|
std::cout << std::endl;
|
2016-06-10 17:12:23 +02:00
|
|
|
printHelp();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (language == plasp::Language::Type::PDDL)
|
2016-06-10 17:40:32 +02:00
|
|
|
{
|
2017-06-20 01:53:55 +02:00
|
|
|
const auto logWarning =
|
|
|
|
[&](const auto &location, const auto &warning)
|
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Warning, location, warning);
|
2017-06-20 01:53:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
auto context = pddl::Context(std::move(tokenizer), logWarning);
|
2017-06-20 03:23:19 +02:00
|
|
|
context.mode = parsingMode;
|
2017-06-20 01:53:55 +02:00
|
|
|
auto description = pddl::parseDescription(context);
|
2017-06-23 04:18:07 +02:00
|
|
|
auto normalizedDescription = pddl::normalize(std::move(description));
|
|
|
|
const auto translator = plasp::pddl::TranslatorASP(std::move(normalizedDescription), logger.outputStream());
|
2016-06-12 22:31:18 +02:00
|
|
|
translator.translate();
|
2016-06-10 17:40:32 +02:00
|
|
|
}
|
2016-06-10 17:12:23 +02:00
|
|
|
else if (language == plasp::Language::Type::SAS)
|
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto description = plasp::sas::Description::fromTokenizer(std::move(tokenizer));
|
2016-06-14 18:02:59 +02:00
|
|
|
const auto translator = plasp::sas::TranslatorASP(description, logger.outputStream());
|
2016-06-12 22:35:31 +02:00
|
|
|
translator.translate();
|
2016-06-10 17:12:23 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-12 14:17:57 +02:00
|
|
|
catch (const tokenize::TokenizerException &e)
|
2016-06-12 22:09:47 +02:00
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, e.location(), e.message().c_str());
|
2017-06-21 03:07:09 +02:00
|
|
|
|
|
|
|
printCompatibilityInfo();
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
catch (const pddl::ParserException &e)
|
|
|
|
{
|
|
|
|
if (e.location())
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, e.location().value(), e.message().c_str());
|
2017-06-21 03:07:09 +02:00
|
|
|
else
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, e.message().c_str());
|
2017-06-21 03:07:09 +02:00
|
|
|
|
|
|
|
printCompatibilityInfo();
|
|
|
|
|
2016-06-14 12:47:39 +02:00
|
|
|
return EXIT_FAILURE;
|
2016-06-13 14:45:31 +02:00
|
|
|
}
|
2017-06-22 20:58:31 +02:00
|
|
|
catch (const plasp::TranslatorException &e)
|
2016-06-12 22:09:47 +02:00
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, e.what());
|
2016-06-14 12:47:39 +02:00
|
|
|
return EXIT_FAILURE;
|
2016-06-12 22:09:47 +02:00
|
|
|
}
|
2016-06-10 17:12:23 +02:00
|
|
|
catch (const std::exception &e)
|
2016-06-10 01:23:41 +02:00
|
|
|
{
|
2017-06-22 20:58:31 +02:00
|
|
|
logger.log(colorlog::Priority::Error, e.what());
|
2016-06-14 12:47:39 +02:00
|
|
|
return EXIT_FAILURE;
|
2016-06-10 01:23:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-20 15:29:24 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|