Framework
MTL
A pure Swift implementation of the https://www.omg.org/spec/MOFM2T/ standard for template-based code generation.
Overview
MTL provides a template-based approach to code generation from models. Define templates with embedded queries that navigate your model, and MTL generates output files with the evaluated content.
This implementation follows the https://www.omg.org/spec/MOFM2T/ and is compatible with https://eclipse.dev/acceleo/, whilst providing a modern Swift API for integration with the ECore metamodelling framework.
Key Features
-
Template-based generation: Define output structure with text and queries
-
AQL integration: Use Acceleo Query Language for model navigation
-
File blocks: Control which files are generated and where
-
Protected regions: Preserve user modifications across regeneration
-
Iteration: Loop over collections with for blocks
-
Conditionals: Include content based on conditions
Quick Example
import MTL
import ECore
// Parse the template module
let parser = MTLParser()
let module = try await parser.parse(URL(fileURLWithPath: "SwiftGenerator.mtl"))
// Configure output
let strategy = MTLFileGenerationStrategy(
outputDirectory: URL(fileURLWithPath: "./Sources/Generated")
)
// Create context and register model
let context = MTLExecutionContext(module: module, generationStrategy: strategy)
try await context.registerModel(modelResource, as: "model")
// Execute
let executor = MTLExecutor(context: context)
try await executor.execute()
Topics
Essentials
Learn how to add MTL to your project and create your first code generator.
Learn the fundamental concepts of the Model-to-Text Language.
Execution
Manages the execution state for MTL template generation.
Protocol for MTL text generation output strategies.
Module Structure
Represents an MTL (Model-to-Text Language) module.
Represents an MTL template for text generation.
Represents an MTL query - a side-effect-free operation returning a value.
Template Elements
Represents a block of MTL statements.
Protected Regions
Thread-safe manager for preserving protected area content across regenerations.
Parsing
Parser for MTL (Model-to-Text Language) templates.
Errors
Errors that can occur during MTL template execution.
Errors that can occur during MTL parsing and serialization.
Classes
File system-based generation strategy that writes to disk.
Main execution engine for MTL model-to-text transformations.
In-memory generation strategy for testing and programmatic access.
Thread-safe actor for accumulating generated text with automatic indentation.
Protocols
Protocol for all MTL statements that can be executed within templates.
Structures
Represents a variable binding with an initialization expression.
A statement representing a comment that produces no output.
A thin wrapper around AQL expressions for MTL template evaluation.
A statement that evaluates an expression and outputs the result.
A file statement for directing output to a file.
A for loop statement for iterating over collections.
Statistics about MTL template execution.
A conditional statement for branching logic.
Manages indentation levels and whitespace generation for MTL text output.
A let statement for variable binding in a nested scope.
Represents an MTL macro for language extension.
A macro invocation statement.
A statement that outputs a newline with optional indentation.
A protected area statement for preserving user code across generations.
A statement that outputs literal text to the current writer.
A trace statement for recording traceability links.
Represents a trace link for model-to-text traceability.
Represents a variable or parameter declaration in MTL.
Enumerations
File operations mode enumeration.
Errors that can occur during MTL resource operations.
Visibility levels for MTL templates and queries.