Wrote simple dummy parser.

This commit is contained in:
Patrick Lühne 2016-11-22 03:15:52 +01:00
parent 14acaea28a
commit 7e7baa1aab
Signed by: patrick
GPG Key ID: 05F3611E97A70ABF
7 changed files with 367 additions and 1 deletions

View File

@ -2,6 +2,8 @@
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <anthem/Translation.h>
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
namespace po = boost::program_options; namespace po = boost::program_options;
@ -37,7 +39,7 @@ int main(int argc, char **argv)
} }
catch (const po::error &e) catch (const po::error &e)
{ {
std::cerr << e.what() << std::endl; std::cerr << "error: " << e.what() << std::endl;
printHelp(); printHelp();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -54,5 +56,21 @@ int main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
try
{
if (variablesMap.count("input"))
{
const auto &inputFiles = variablesMap["input"].as<std::vector<std::string>>();
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; return EXIT_SUCCESS;
} }

View File

@ -0,0 +1,63 @@
#ifndef __ANTHEM__BODY_LITERAL_VISITOR_H
#define __ANTHEM__BODY_LITERAL_VISITOR_H
#include <anthem/Utils.h>
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

View File

@ -0,0 +1,58 @@
#ifndef __ANTHEM__HEAD_LITERAL_VISITOR_H
#define __ANTHEM__HEAD_LITERAL_VISITOR_H
#include <anthem/Utils.h>
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

View File

@ -0,0 +1,118 @@
#ifndef __ANTHEM__STATEMENT_VISITOR_H
#define __ANTHEM__STATEMENT_VISITOR_H
#include <anthem/BodyLiteralVisitor.h>
#include <anthem/HeadLiteralVisitor.h>
#include <anthem/Utils.h>
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

View File

@ -0,0 +1,23 @@
#ifndef __ANTHEM__TRANSLATION_H
#define __ANTHEM__TRANSLATION_H
#include <string>
#include <vector>
namespace anthem
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Translation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void translate(const std::vector<std::string> &fileNames);
void translate(const char *fileName, std::istream &stream);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
#endif

30
include/anthem/Utils.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __ANTHEM__UTILS_H
#define __ANTHEM__UTILS_H
#include <iostream>
#include <clingo.hh>
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

View File

@ -0,0 +1,56 @@
#include <anthem/Translation.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <clingo.hh>
#include <anthem/StatementVisitor.h>
namespace anthem
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Translation
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void translate(const std::vector<std::string> &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<char>(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);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}