From 7e7baa1aab7204096c0c3449f0afd164ea480d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 22 Nov 2016 03:15:52 +0100 Subject: [PATCH] Wrote simple dummy parser. --- app/main.cpp | 20 ++++- include/anthem/BodyLiteralVisitor.h | 63 +++++++++++++++ include/anthem/HeadLiteralVisitor.h | 58 ++++++++++++++ include/anthem/StatementVisitor.h | 118 ++++++++++++++++++++++++++++ include/anthem/Translation.h | 23 ++++++ include/anthem/Utils.h | 30 +++++++ src/anthem/Translation.cpp | 56 +++++++++++++ 7 files changed, 367 insertions(+), 1 deletion(-) create mode 100644 include/anthem/BodyLiteralVisitor.h create mode 100644 include/anthem/HeadLiteralVisitor.h create mode 100644 include/anthem/StatementVisitor.h create mode 100644 include/anthem/Translation.h create mode 100644 include/anthem/Utils.h create mode 100644 src/anthem/Translation.cpp diff --git a/app/main.cpp b/app/main.cpp index 1b55f0e..2541cee 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -2,6 +2,8 @@ #include +#include + int main(int argc, char **argv) { namespace po = boost::program_options; @@ -37,7 +39,7 @@ int main(int argc, char **argv) } catch (const po::error &e) { - std::cerr << e.what() << std::endl; + std::cerr << "error: " << e.what() << std::endl; printHelp(); return EXIT_FAILURE; } @@ -54,5 +56,21 @@ int main(int argc, char **argv) return EXIT_SUCCESS; } + try + { + if (variablesMap.count("input")) + { + const auto &inputFiles = variablesMap["input"].as>(); + anthem::translate(inputFiles); + } + else + anthem::translate("std::cin", std::cin); + } + catch (const std::exception &e) + { + std::cerr << "error: " << e.what() << std::endl; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } diff --git a/include/anthem/BodyLiteralVisitor.h b/include/anthem/BodyLiteralVisitor.h new file mode 100644 index 0000000..0b86cb3 --- /dev/null +++ b/include/anthem/BodyLiteralVisitor.h @@ -0,0 +1,63 @@ +#ifndef __ANTHEM__BODY_LITERAL_VISITOR_H +#define __ANTHEM__BODY_LITERAL_VISITOR_H + +#include + +namespace anthem +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// BodyLiteralVisitor +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void throwErrorUnsupportedBodyLiteral(const char *statementType, const Clingo::AST::BodyLiteral &bodyLiteral) +{ + const auto errorMessage = std::string("“") + statementType + "” body literals currently not supported"; + + throwErrorAtLocation(bodyLiteral.location, errorMessage.c_str()); + + throw std::runtime_error(errorMessage); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct BodyLiteralVisitor +{ + void visit(const Clingo::AST::Literal &, const Clingo::AST::BodyLiteral &) + { + std::cout << "[literal]" << std::endl; + } + + void visit(const Clingo::AST::ConditionalLiteral &, const Clingo::AST::BodyLiteral &bodyLiteral) + { + throwErrorUnsupportedBodyLiteral("conditional literal", bodyLiteral); + } + + void visit(const Clingo::AST::Aggregate &, const Clingo::AST::BodyLiteral &bodyLiteral) + { + throwErrorUnsupportedBodyLiteral("aggregate", bodyLiteral); + } + + void visit(const Clingo::AST::BodyAggregate &, const Clingo::AST::BodyLiteral &bodyLiteral) + { + throwErrorUnsupportedBodyLiteral("body aggregate", bodyLiteral); + } + + void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::BodyLiteral &bodyLiteral) + { + throwErrorUnsupportedBodyLiteral("theory atom", bodyLiteral); + } + + void visit(const Clingo::AST::Disjoint &, const Clingo::AST::BodyLiteral &bodyLiteral) + { + throwErrorUnsupportedBodyLiteral("disjoint", bodyLiteral); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif diff --git a/include/anthem/HeadLiteralVisitor.h b/include/anthem/HeadLiteralVisitor.h new file mode 100644 index 0000000..d83d1f7 --- /dev/null +++ b/include/anthem/HeadLiteralVisitor.h @@ -0,0 +1,58 @@ +#ifndef __ANTHEM__HEAD_LITERAL_VISITOR_H +#define __ANTHEM__HEAD_LITERAL_VISITOR_H + +#include + +namespace anthem +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// HeadLiteralVisitor +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void throwErrorUnsupportedHeadLiteral(const char *statementType, const Clingo::AST::HeadLiteral &headLiteral) +{ + const auto errorMessage = std::string("“") + statementType + "” head literals currently not supported"; + + throwErrorAtLocation(headLiteral.location, errorMessage.c_str()); + + throw std::runtime_error(errorMessage); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct HeadLiteralVisitor +{ + void visit(const Clingo::AST::Literal &, const Clingo::AST::HeadLiteral &) + { + std::cout << "[literal]" << std::endl; + } + + void visit(const Clingo::AST::Disjunction &, const Clingo::AST::HeadLiteral &headLiteral) + { + throwErrorUnsupportedHeadLiteral("disjunction", headLiteral); + } + + void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral) + { + throwErrorUnsupportedHeadLiteral("aggregate", headLiteral); + } + + void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral) + { + throwErrorUnsupportedHeadLiteral("head aggregate", headLiteral); + } + + void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral) + { + throwErrorUnsupportedHeadLiteral("theory", headLiteral); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif diff --git a/include/anthem/StatementVisitor.h b/include/anthem/StatementVisitor.h new file mode 100644 index 0000000..e1bbc97 --- /dev/null +++ b/include/anthem/StatementVisitor.h @@ -0,0 +1,118 @@ +#ifndef __ANTHEM__STATEMENT_VISITOR_H +#define __ANTHEM__STATEMENT_VISITOR_H + +#include +#include +#include + +namespace anthem +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// StatementVisitor +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void throwErrorUnsupportedStatement(const char *statementType, const Clingo::AST::Statement &statement) +{ + const auto errorMessage = std::string("“") + statementType + "” statements currently not supported"; + + throwErrorAtLocation(statement.location, errorMessage.c_str()); + + throw std::runtime_error(errorMessage); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +struct StatementVisitor +{ + void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement) + { + std::cout << "[program] " << program.name << std::endl; + + if (!program.parameters.empty()) + throwErrorAtLocation(statement.location, "program parameters currently not supported"); + } + + void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &) + { + std::cout << "[rule]" << std::endl; + std::cout << "[head literal]" << std::endl; + + rule.head.data.accept(HeadLiteralVisitor(), rule.head); + + std::cout << "[body]" << std::endl; + + for (const auto &bodyLiteral : rule.body) + { + std::cout << "[body literal]" << std::endl; + + if (bodyLiteral.sign != Clingo::AST::Sign::None) + throwErrorAtLocation(bodyLiteral.location, "only positive literals currently supported"); + + bodyLiteral.data.accept(BodyLiteralVisitor(), bodyLiteral); + } + } + + void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("definition", statement); + } + + void visit(const Clingo::AST::ShowSignature &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("show signature", statement); + } + + void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("show term", statement); + } + + void visit(const Clingo::AST::Minimize &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("minimize", statement); + } + + void visit(const Clingo::AST::Script &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("script", statement); + } + + void visit(const Clingo::AST::External &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("external", statement); + } + + void visit(const Clingo::AST::Edge &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("edge", statement); + } + + void visit(const Clingo::AST::Heuristic &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("heuristic", statement); + } + + void visit(const Clingo::AST::ProjectAtom &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("project atom", statement); + } + + void visit(const Clingo::AST::ProjectSignature &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("project signature", statement); + } + + void visit(const Clingo::AST::TheoryDefinition &, const Clingo::AST::Statement &statement) + { + throwErrorUnsupportedStatement("theory definition", statement); + } +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif diff --git a/include/anthem/Translation.h b/include/anthem/Translation.h new file mode 100644 index 0000000..e2c2ad9 --- /dev/null +++ b/include/anthem/Translation.h @@ -0,0 +1,23 @@ +#ifndef __ANTHEM__TRANSLATION_H +#define __ANTHEM__TRANSLATION_H + +#include +#include + +namespace anthem +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Translation +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void translate(const std::vector &fileNames); +void translate(const char *fileName, std::istream &stream); + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif diff --git a/include/anthem/Utils.h b/include/anthem/Utils.h new file mode 100644 index 0000000..f9fc9a9 --- /dev/null +++ b/include/anthem/Utils.h @@ -0,0 +1,30 @@ +#ifndef __ANTHEM__UTILS_H +#define __ANTHEM__UTILS_H + +#include + +#include + +namespace anthem +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Utils +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline void throwErrorAtLocation(const Clingo::Location &location, const char *errorMessage) +{ + std::cerr + << location.begin_file() << ":" + << location.begin_line() << ":" << location.begin_column() + << ": error: " + << errorMessage << std::endl; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} + +#endif diff --git a/src/anthem/Translation.cpp b/src/anthem/Translation.cpp new file mode 100644 index 0000000..e902887 --- /dev/null +++ b/src/anthem/Translation.cpp @@ -0,0 +1,56 @@ +#include + +#include +#include +#include + +#include + +#include + +namespace anthem +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Translation +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void translate(const std::vector &fileNames) +{ + for (const auto &fileName : fileNames) + { + std::ifstream file(fileName, std::ios::in); + + translate(fileName.c_str(), file); + } +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void translate(const char *fileName, std::istream &stream) +{ + std::cout << "info: reading " << fileName << std::endl; + + auto fileContent = std::string(std::istreambuf_iterator(stream), {}); + + const auto translateStatement = + [](const Clingo::AST::Statement &statement) + { + statement.data.accept(StatementVisitor(), statement); + std::cout << std::endl; + }; + + const auto logger = + [](const auto warningCode, const auto *text) + { + std::cout << "warning: " << text << std::endl; + }; + + Clingo::parse_program(fileContent.c_str(), translateStatement, logger); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +}