Wrote simple dummy parser.

This commit is contained in:
2016-11-22 03:15:52 +01:00
parent 14acaea28a
commit 7e7baa1aab
7 changed files with 367 additions and 1 deletions

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