From 89edafb586750c5251bda937edc9fa17977ca953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 14 Oct 2017 01:37:40 +0200 Subject: [PATCH] =?UTF-8?q?Implemented=20=E2=80=9Cbeautify=E2=80=9D=20comm?= =?UTF-8?q?and.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/include/plasp-app/CommandType.h | 7 +- .../plasp-app/commands/CommandBeautify.h | 25 +++ .../plasp-app/commands/CommandBeautify.cpp | 145 ++++++++++++++++++ app/src/plasp-app/commands/CommandHelp.cpp | 8 +- .../plasp-app/commands/CommandNormalize.cpp | 2 - app/src/plasp-app/main.cpp | 7 +- 6 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 app/include/plasp-app/commands/CommandBeautify.h create mode 100644 app/src/plasp-app/commands/CommandBeautify.cpp diff --git a/app/include/plasp-app/CommandType.h b/app/include/plasp-app/CommandType.h index 0bdf22b..3b49fa8 100644 --- a/app/include/plasp-app/CommandType.h +++ b/app/include/plasp-app/CommandType.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -21,7 +22,7 @@ enum class CommandType Version, CheckSyntax, Requirements, - PrettyPrint, + Beautify, Normalize, Translate }; @@ -38,7 +39,7 @@ static const std::map commandNames = {"--version", CommandType::Version}, {"check-syntax", CommandType::CheckSyntax}, {"requirements", CommandType::Requirements}, - {"pretty-print", CommandType::PrettyPrint}, + {"beautify", CommandType::Beautify}, {"normalize", CommandType::Normalize}, {"translate", CommandType::Translate}, }; @@ -58,7 +59,7 @@ const auto parseCommandType = //////////////////////////////////////////////////////////////////////////////////////////////////// -using AvailableCommands = std::tuple; +using AvailableCommands = std::tuple; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/app/include/plasp-app/commands/CommandBeautify.h b/app/include/plasp-app/commands/CommandBeautify.h new file mode 100644 index 0000000..69f67bf --- /dev/null +++ b/app/include/plasp-app/commands/CommandBeautify.h @@ -0,0 +1,25 @@ +#ifndef __PLASP_APP__COMMANDS__COMMAND_BEAUTIFY_H +#define __PLASP_APP__COMMANDS__COMMAND_BEAUTIFY_H + +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Command Beautify +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class CommandBeautify : public Command +{ + public: + static constexpr auto Name = "beautify"; + static constexpr auto Description = "Cleanly format PDDL specifications"; + + public: + int run(int argc, char **argv); +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif diff --git a/app/src/plasp-app/commands/CommandBeautify.cpp b/app/src/plasp-app/commands/CommandBeautify.cpp new file mode 100644 index 0000000..bf98c54 --- /dev/null +++ b/app/src/plasp-app/commands/CommandBeautify.cpp @@ -0,0 +1,145 @@ +#include + +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Command Beautify +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +int CommandBeautify::run(int argc, char **argv) +{ + parseOptions(argc, argv); + + const auto &basicOptions = std::get(m_optionGroups); + const auto &outputOptions = std::get(m_optionGroups); + const auto &parserOptions = std::get(m_optionGroups); + + if (basicOptions.help) + { + printHelp(); + return EXIT_SUCCESS; + } + + if (basicOptions.version) + { + printVersion(); + return EXIT_SUCCESS; + } + + colorlog::Logger logger; + logger.setColorPolicy(outputOptions.colorPolicy); + logger.setLogPriority(outputOptions.logPriority); + + if (basicOptions.warningsAsErrors) + logger.setAbortPriority(colorlog::Priority::Warning); + + const auto printCompatibilityInfo = + [&]() + { + if (parserOptions.parsingMode != pddl::Mode::Compatibility) + logger.log(colorlog::Priority::Info, "try using --parsing-mode=compatibility for extended legacy feature support"); + }; + + try + { + tokenize::Tokenizer tokenizer; + + if (!parserOptions.inputFiles.empty()) + std::for_each(parserOptions.inputFiles.cbegin(), parserOptions.inputFiles.cend(), + [&](const auto &inputFile) + { + tokenizer.read(inputFile); + }); + else + { + logger.log(colorlog::Priority::Info, "reading from stdin"); + tokenizer.read("std::cin", std::cin); + } + + const auto detectLanguage = + [&]() + { + if (parserOptions.language == plasp::Language::Type::Automatic) + return plasp::detectLanguage(tokenizer); + + return parserOptions.language; + }; + + switch (detectLanguage()) + { + case plasp::Language::Type::Automatic: + case plasp::Language::Type::Unknown: + { + logger.log(colorlog::Priority::Error, "unknown input language"); + std::cout << std::endl; + printHelp(); + return EXIT_FAILURE; + } + + // TODO: get rid of unknown language type, use exception instead + case plasp::Language::Type::PDDL: + { + const auto logWarning = + [&](const auto &location, const auto &warning) + { + logger.log(colorlog::Priority::Warning, location, warning); + }; + + auto context = pddl::Context(std::move(tokenizer), logWarning); + context.mode = parserOptions.parsingMode; + auto description = pddl::parseDescription(context); + logger.outputStream() << description << std::endl; + break; + } + + case plasp::Language::Type::SAS: + { + logger.log(colorlog::Priority::Error, "Beautification is only supported for PDDL specifications"); + return EXIT_FAILURE; + } + } + } + catch (const tokenize::TokenizerException &e) + { + logger.log(colorlog::Priority::Error, e.location(), e.message().c_str()); + + printCompatibilityInfo(); + + return EXIT_FAILURE; + } + catch (const pddl::ParserException &e) + { + if (e.location()) + logger.log(colorlog::Priority::Error, e.location().value(), e.message().c_str()); + else + logger.log(colorlog::Priority::Error, e.message().c_str()); + + printCompatibilityInfo(); + + return EXIT_FAILURE; + } + catch (const std::exception &e) + { + logger.log(colorlog::Priority::Error, e.what()); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/app/src/plasp-app/commands/CommandHelp.cpp b/app/src/plasp-app/commands/CommandHelp.cpp index 2e5674b..b26a539 100644 --- a/app/src/plasp-app/commands/CommandHelp.cpp +++ b/app/src/plasp-app/commands/CommandHelp.cpp @@ -24,12 +24,16 @@ int CommandHelp::run(int argc, char **argv) case CommandType::Version: break; + case CommandType::Translate: + CommandTranslate().printHelp(); + return EXIT_SUCCESS; + case CommandType::Normalize: CommandNormalize().printHelp(); return EXIT_SUCCESS; - case CommandType::Translate: - CommandTranslate().printHelp(); + case CommandType::Beautify: + CommandBeautify().printHelp(); return EXIT_SUCCESS; default: diff --git a/app/src/plasp-app/commands/CommandNormalize.cpp b/app/src/plasp-app/commands/CommandNormalize.cpp index 6f316ad..1255211 100644 --- a/app/src/plasp-app/commands/CommandNormalize.cpp +++ b/app/src/plasp-app/commands/CommandNormalize.cpp @@ -19,8 +19,6 @@ #include -#include - //////////////////////////////////////////////////////////////////////////////////////////////////// // // Command Normalize diff --git a/app/src/plasp-app/main.cpp b/app/src/plasp-app/main.cpp index c4cb6ad..a854b58 100644 --- a/app/src/plasp-app/main.cpp +++ b/app/src/plasp-app/main.cpp @@ -38,11 +38,14 @@ int main(int argc, char **argv) case CommandType::Version: return CommandVersion().run(argc - 1, &argv[1]); + case CommandType::Translate: + return CommandTranslate().run(argc - 1, &argv[1]); + case CommandType::Normalize: return CommandNormalize().run(argc - 1, &argv[1]); - case CommandType::Translate: - return CommandTranslate().run(argc - 1, &argv[1]); + case CommandType::Beautify: + return CommandBeautify().run(argc - 1, &argv[1]); default: logger.log(colorlog::Priority::Error, std::string("command “") + argv[1] + "” not yet implemented");