Add beginning implementation of backends

This commit is contained in:
Robin Jadoul 2016-05-24 11:47:31 +02:00
parent 03a6964246
commit ae28ed1c65
6 changed files with 127 additions and 0 deletions

31
include/Lexesis/backend.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#ifndef LEXESIS_BACKEND_H
#define LEXESIS_BACKEND_H
#include "Lexesis/automata.h"
#include "Lexesis/template.h"
#include <functional>
#include <string>
namespace lxs {
class Backend {
public:
Backend();
virtual ~Backend();
virtual std::string getName() = 0;
virtual bool canProcessLang(std::string lang);
virtual void generateLexer(std::function<std::ostream&(std::string)> getOstreamForFileName, std::string lexerName, const DFA& dfa) = 0;
protected:
void doTemplate(std::ostream& out, std::string templateName, templ::TemplateContext context);
private:
std::string findTemplate(std::string templateName);
};
}
#endif //LEXESIS_BACKEND_H

View File

@ -0,0 +1,23 @@
#pragma once
#ifndef LEXESIS_BACKENDMANAGER_H
#define LEXESIS_BACKENDMANAGER_H
#include <memory>
#include <string>
#include <vector>
namespace lxs {
class Backend;
class BackendManager {
public:
void registerBackend(std::unique_ptr<Backend> backend);
Backend* findBackendForLang(std::string lang);
private:
std::vector<std::unique_ptr<Backend> > m_backends;
};
}
#endif //LEXESIS_BACKENDMANAGER_H

View File

@ -0,0 +1,30 @@
#pragma once
#ifndef LEXESIS_TEMPLATE_H
#define LEXESIS_TEMPLATE_H
#include <map>
#include <string>
#include <vector>
namespace lxs {
namespace templ {
using TemplateContext = void*;
TemplateContext make_string(std::string);
TemplateContext make_map(std::map<std::string, TemplateContext>);
TemplateContext make_array(std::vector<TemplateContext>);
class Template {
public:
Template(std::string filename);
~Template();
void render(std::ostream& out, TemplateContext& context);
private:
std::string m_filename;
};
} //namespace templ
} //namespace lxs
#endif //LEXESIS_TEMPLATE_H

23
src/backend.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "Lexesis/backend.h"
#include "config.h"
namespace lxs {
Backend::Backend()
{}
Backend::~Backend()
{}
bool Backend::canProcessLang(std::string /*lang*/) {
return false;
}
void Backend::doTemplate(std::ostream& out, std::string templateName, templ::TemplateContext context) {
templ::Template tpl(findTemplate(templateName));
tpl.render(out, context);
}
std::string Backend::findTemplate(std::string templateName) {
return DATADIR "templates/" + templateName;
}
}

6
src/config.h.in Normal file
View File

@ -0,0 +1,6 @@
#ifndef CONFIG_H
#define CONFIG_H
#define DATADIR "@CMAKE_INSTALL_PREFIX@/share/Lexesis/"
#endif //CONFIG_H

14
src/template.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "Lexesis/template.h"
namespace lxs {
namespace templ {
Template::Template(std::string filename) : m_filename(filename)
{}
Template::~Template()
{}
void Template::render(std::ostream&, TemplateContext&)
{}
}
}