Documentation Language: Swift

Class

MTLExecutionContext

Manages the execution state for MTL template generation.

@MainActor final class MTLExecutionContext

Overview

The execution context maintains all runtime state during template execution, including variable bindings, writer stack, indentation, protected areas, and model access. It coordinates between MTL templates and the underlying AQL expression evaluator.

Overview

The execution context provides:

Variable Scoping

Variables are managed in a stack of scopes:

context.setVariable("x", value: 10)     // Global scope
context.pushScope()
context.setVariable("x", value: 20)     // Local scope (shadows global)
await context.getVariable("x")          // Returns 20
context.popScope()
await context.getVariable("x")          // Returns 10

Writer Stack

File blocks create new writers that are pushed onto a stack:

// Main output (stdout)
context.write("Top level")

// Open file block - pushes new writer
try await context.openFile(url: "output.txt", mode: .create, charset: "UTF-8")
context.write("File content")  // Goes to output.txt
try await context.closeFile()  // Pops writer, finalizes file

context.write("Top level again")  // Back to stdout

Example Usage

let module = MTLModule(...)
let strategy = MTLInMemoryStrategy()
let context = MTLExecutionContext(
    module: module,
    generationStrategy: strategy
)

// Register models
await context.registerModel("input", resource: inputModel)

// Execute template
context.setVariable("model", value: modelRoot)
let template = module.templates["generateClass"]!
// ... execute template body ...

// Finalize
try await context.finalize()

Topics

Initializers

Instance Properties

V
currentIndentation

Returns the current indentation level.

V
debug

Whether debug mode is enabled.

V
module

The MTL module being executed.

V
protectedAreas

Returns the protected area manager for advanced operations.

Instance Methods

F
addTraceLink(source:target:)

Adds a traceability link from a source model element to generated text.

F
closeFile()

Closes the current file, finalizing its content and popping its writer.

F
debugSummary()

Returns a summary of the current execution state for debugging.

F
evaluateExpression(_:)

Evaluates an MTL expression using the AQL evaluator.

F
finalize()

Finalizes the execution context after generation completes.

F
getGeneratedText()

Returns the currently generated text from the main writer.

F
getModel(_:)

Retrieves a registered model by alias.

F
getProtectedAreaContent(_:)

Retrieves the preserved content for a protected area.

F
getTraceLinks()

Returns all recorded trace links.

F
getVariable(_:)

Retrieves a variable from the current or enclosing scopes.

F
openFile(url:mode:charset:)

Opens a new file for output, pushing a new writer onto the stack.

F
popIndentation()

Pops the current indentation level from the stack.

F
popScope()

Pops the current variable scope from the stack.

F
pushIndentation()

Pushes a new indentation level onto the stack.

F
pushScope()

Pushes a new variable scope onto the stack.

F
registerModel(_:resource:)

Registers an input model for template access.

F
scanFileForProtectedAreas(_:)

Scans a file for protected areas before regeneration.

F
setProtectedAreaContent(_:content:markers:)

Sets the preserved content for a protected area.

F
setVariable(_:value:)

Sets a variable in the current scope.

F
write(_:indent:)

Writes text to the current writer.

F
writeLine(_:indent:)

Writes a line of text followed by a newline to the current writer.

Relationships

Conforms To

See Also

Execution

P
MTLGenerationStrategy

Protocol for MTL text generation output strategies.