From b8bd42d361b7cdb430c2fb6f24dd3186cb73824e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sat, 14 Oct 2017 01:43:56 +0200 Subject: [PATCH] =?UTF-8?q?Implemented=20=E2=80=9Ccheck-syntax=E2=80=9D=20?= =?UTF-8?q?command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/include/plasp-app/CommandType.h | 3 +- .../plasp-app/commands/CommandCheckSyntax.h | 25 +++ .../plasp-app/commands/CommandCheckSyntax.cpp | 145 ++++++++++++++++++ app/src/plasp-app/commands/CommandHelp.cpp | 4 + .../plasp-app/commands/CommandNormalize.cpp | 2 +- .../plasp-app/commands/CommandTranslate.cpp | 4 +- app/src/plasp-app/main.cpp | 3 + 7 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 app/include/plasp-app/commands/CommandCheckSyntax.h create mode 100644 app/src/plasp-app/commands/CommandCheckSyntax.cpp diff --git a/app/include/plasp-app/CommandType.h b/app/include/plasp-app/CommandType.h index 3b49fa8..dbc2a3d 100644 --- a/app/include/plasp-app/CommandType.h +++ b/app/include/plasp-app/CommandType.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -59,7 +60,7 @@ const auto parseCommandType = //////////////////////////////////////////////////////////////////////////////////////////////////// -using AvailableCommands = std::tuple; +using AvailableCommands = std::tuple; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/app/include/plasp-app/commands/CommandCheckSyntax.h b/app/include/plasp-app/commands/CommandCheckSyntax.h new file mode 100644 index 0000000..15238f2 --- /dev/null +++ b/app/include/plasp-app/commands/CommandCheckSyntax.h @@ -0,0 +1,25 @@ +#ifndef __PLASP_APP__COMMANDS__COMMAND_CHECK_SYNTAX_H +#define __PLASP_APP__COMMANDS__COMMAND_CHECK_SYNTAX_H + +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Command Check Syntax +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class CommandCheckSyntax : public Command +{ + public: + static constexpr auto Name = "check-syntax"; + static constexpr auto Description = "Check the syntax of PDDL specifications"; + + public: + int run(int argc, char **argv); +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +#endif diff --git a/app/src/plasp-app/commands/CommandCheckSyntax.cpp b/app/src/plasp-app/commands/CommandCheckSyntax.cpp new file mode 100644 index 0000000..e5e4ef0 --- /dev/null +++ b/app/src/plasp-app/commands/CommandCheckSyntax.cpp @@ -0,0 +1,145 @@ +#include + +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Command Check Syntax +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +int CommandCheckSyntax::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.log(colorlog::Priority::Info, "no syntax errors found"); + return EXIT_SUCCESS; + } + + case plasp::Language::Type::SAS: + { + logger.log(colorlog::Priority::Error, "Syntax checking 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 b26a539..37f2c2b 100644 --- a/app/src/plasp-app/commands/CommandHelp.cpp +++ b/app/src/plasp-app/commands/CommandHelp.cpp @@ -36,6 +36,10 @@ int CommandHelp::run(int argc, char **argv) CommandBeautify().printHelp(); return EXIT_SUCCESS; + case CommandType::CheckSyntax: + CommandCheckSyntax().printHelp(); + return EXIT_SUCCESS; + default: logger.log(colorlog::Priority::Error, std::string("command “") + argv[1] + "” not yet implemented"); exit(EXIT_FAILURE); diff --git a/app/src/plasp-app/commands/CommandNormalize.cpp b/app/src/plasp-app/commands/CommandNormalize.cpp index 1255211..908bef8 100644 --- a/app/src/plasp-app/commands/CommandNormalize.cpp +++ b/app/src/plasp-app/commands/CommandNormalize.cpp @@ -109,7 +109,7 @@ int CommandNormalize::run(int argc, char **argv) auto description = pddl::parseDescription(context); auto normalizedDescription = pddl::normalize(std::move(description)); logger.outputStream() << normalizedDescription << std::endl; - break; + return EXIT_SUCCESS; } case plasp::Language::Type::SAS: diff --git a/app/src/plasp-app/commands/CommandTranslate.cpp b/app/src/plasp-app/commands/CommandTranslate.cpp index 83c52e5..95e7668 100644 --- a/app/src/plasp-app/commands/CommandTranslate.cpp +++ b/app/src/plasp-app/commands/CommandTranslate.cpp @@ -115,7 +115,7 @@ int CommandTranslate::run(int argc, char **argv) auto normalizedDescription = pddl::normalize(std::move(description)); const auto translator = plasp::pddl::TranslatorASP(std::move(normalizedDescription), logger.outputStream()); translator.translate(); - break; + return EXIT_SUCCESS; } case plasp::Language::Type::SAS: @@ -123,7 +123,7 @@ int CommandTranslate::run(int argc, char **argv) const auto description = plasp::sas::Description::fromTokenizer(std::move(tokenizer)); const auto translator = plasp::sas::TranslatorASP(description, logger.outputStream()); translator.translate(); - break; + return EXIT_SUCCESS; } } } diff --git a/app/src/plasp-app/main.cpp b/app/src/plasp-app/main.cpp index a854b58..1d6f095 100644 --- a/app/src/plasp-app/main.cpp +++ b/app/src/plasp-app/main.cpp @@ -47,6 +47,9 @@ int main(int argc, char **argv) case CommandType::Beautify: return CommandBeautify().run(argc - 1, &argv[1]); + case CommandType::CheckSyntax: + return CommandCheckSyntax().run(argc - 1, &argv[1]); + default: logger.log(colorlog::Priority::Error, std::string("command “") + argv[1] + "” not yet implemented"); exit(EXIT_FAILURE);