DST is a DSL to parse templates, you can use these templates to easily generate code. The DSL will be available via a library created using Deamer.
Aside from using DST to parse at runtime, DST is also able to generate C++ classes to generate the specific files. This gives the user even more flexibility, when generating files.
DST depends on the external library of Deamer CC, so it is required to install Deamer External.
Instructions on how to install Deamer External can be found here: https://github.com/Deruago/DeamerExternal.
Getting the source code
git clone https://github.com/Deruago/DeamerStringTemplate
cd DeamerStringTemplateCreating a build directory
mkdir build
cd buildBuilding and installing DST
cmake ..
sudo cmake --build . --target installCreating the file:
This is the main template file, DST will parse this and create an internal structure.
#ifndef {{header_guard}}
#define {{header_guard}}
namespace {{language_name}} { namespace ast { namespace {{ast_name}} {
class {{ast_name}} : public {{language_name}} {{base_names}}
{
private:
Value* value;
public:
{{ast_name}}(Value* value_);
~{{ast_name}}() override = default;
public:
const Value* GetValue() const;
}
}}}
#endif // {{header_guard}}
Creating the settings file:
This file is optional, it can be used to:
- Make abstractions
- Specify common operations
{{file.file_name}} = {{ast_name}}.h
{{header_guard}} = {{language_name.Upper}}_AST_NODE_{{ast_name.Upper}}_H / abstraction
Parsing the template:
This c++ code, uses DST to parse and implement the varies parameters.
#include <DST/User/ConstructionGenerator.h>
#include <string>
int main()
{
auto* const generator = user::ConstructionGenerator().GenerateConstructionFromPath(
"./template.dst",
"./template.setting.dst"
);
std::string lang_name = "Test";
std::string ast_name = "TestNode";
std::string output = generator->Output(
{
{"language_name", "", lang_name},
{"ast_name", "", ast_name}
}
);
std::cout << output << std::endl;
delete generator;
return 0;
}Example repo
For more examples visit the example repo.
DST is also capable of generating C++ files, to implement the the code generation with.
To generate such C++ file, first install the DST program using the "install" target of CMake.
After this run DST as follows:
DST ./templatepath.dst ./template_settings_path.setting.dstIf everything was correctly executed you will see an header file. You can now use this header file for code generation.