Directives

$ introduces a registered compiler directive. Template-head directives already use this marker. The accepted source-modifier family adds next-construct modifiers such as $config and later compiler-owned forms. HTML document metadata uses direct const inserts in @entry.moth through builder slots.

Template directives are implemented. The $config source modifier is delivered for typed build inputs; later compiler-owned source forms remain accepted design. No root-purpose directive exists: @entry.moth owns entry eligibility, and $entry and $page are not source syntax.

Choose the explanation level: Directives

Directives

A directive modifies an existing construct. Template-head directives modify their template. Source directives attach to the next declaration or statement when their own contract allows it.

Two locations

message #= [$md: Hello from Moth]

$export $doc [: Public greeting.]
greeting #= "Hello"

$todo [: Add a better error message.]
assert(false, "Not implemented")

$export publishes declarations or direct-selection dependency clauses from @mod.moth and @package.moth. $config marks an explicitly typed top-level compile-time binding as a build input.

$doc documents a declaration. $todo annotates a declaration or statement. Both take a compile-time String value, often written as a template. $doc records declaration documentation. $todo has no runtime or builder effect. Use $doc [$md: ...] to request Markdown explicitly.

Stacking and operands

Source directives may share one line or use consecutive lines. No commas separate them. Each distinct directive attaches independently to the same following-line construct and order never changes its meaning. Any repeated directive name on one target is an error.

Comment-only lines can intervene, but a blank line breaks attachment. Another source directive ends a preceding operand only at the outer expression boundary, not inside a nested template. A directive cannot skip an invalid target.

Directives use $name, $name value or $name key = value, according to their contract. Parenthesized calls like $name(...) are invalid. $export and $config take no operand.

Template-head directives include $before, $after, $fresh, $slot, $in, $note, $raw, $md, $html, $css, $escape_html and $code. Template heads use commas between items. Only $note discards comment content, and only as a child of a template body.

See Directives, Template directives and Build inputs.

Directives modify existing constructs. Template-head directives modify the containing template. Source directives annotate the following declaration or statement when their own target contract permits it. The compiler owns all directives.

Template-head directives

A template-head directive belongs to the template that contains it:

message #= [$md: Hello from Moth]

Template heads separate meaningful items with commas, including directives and expressions. Each head directive has its own operand and compatibility rules. A directive name may occur only once per template head. See Template directives.

Source directives

Source directives attach to one following construct:

$export $doc [$md: Public greeting.]
greeting #= "Hello"

$doc [: Parses input text.]
parse_text |source String| -> String:
    $todo [: Add Unicode escapes.]
    return source
;

$export publishes a declaration or strict direct-selection dependency clause in @mod.moth or @package.moth. It cannot export an entry, ordinary runtime statement, whole namespace or $config input. See Public API and Public re-exports.

$config marks one explicitly typed top-level compile-time # binding in @config.moth or a selected source module as a build input:

$config
api_url #String = "https://example.com"

Its name is the input key, its type is the contract and its initializer is the fallback. Only a marked input can omit its initializer. See Build configuration values.

$doc value documents a declaration, including an eligible local declaration. Its operand must evaluate at compile time to a String. The compiler attaches the resolved text to the declaration's semantic identity as documentation metadata. It does not publish a template fragment or emit runtime work. $doc does not format its argument automatically. Use $doc [$md: ...] for Markdown or $doc [: ...] for ordinary template content.

$todo value annotates a declaration or a complete executable statement, including a branch, loop or return. It also requires a compile-time String operand. It is validated and discarded for now, without a runtime effect, builder output or automatic diagnostic. A future consumer can add a use without changing its source syntax.

$doc and $todo are source directives, not template-head directives. $note is the template-only comment directive and works only as a child of a template body. Neither source annotations nor a discarded note contribute an entry page fragment.

Attachment and stacking

A contiguous run of source directives attaches to the immediately following declaration or statement. All directives in the run attach to that same construct independently. Their order does not change the target or meaning.

Different directives may occupy separate consecutive lines or share a line, separated by whitespace. Commas are not used:

$doc [: Returns a greeting.] $todo [: Consider localisation.]
greeting || -> String:
    return "Hello"
;

The target construct begins on a later line, never after the final directive on the same line. A comment-only line may intervene. A blank physical line, scope boundary or end of file breaks attachment, and an intervening unrelated construct cannot be skipped. LF and CRLF follow the same rules.

A directive consumes at most one permitted operand. Another source directive terminates that operand only at its outer expression boundary. A nested template directive, such as $md in $doc [$md: text], remains part of that template. No source directive can be the operand of another.

Each directive name may occur at most once in one attachment run. Every duplicate is an error, even if the operands match or repetition would otherwise be harmless. Diagnostics identify both authored occurrences. The same directive may annotate a different following construct. There is no blanket pairwise compatibility hierarchy: each distinct directive validates its own eligible target and semantic requirements. Constraint directives therefore evaluate the resulting program semantics independently of source order.

Operands and diagnostics

The general directive surface is:

$name
$name value
$name key = value

The directive contract selects which of these forms is legal. A named operand has exactly one key and value, without a type annotation. Operands use the ordinary compile-time expression rules and may contain balanced nested templates. $export and $config accept no operand. Parenthesized calls such as $name(...) are not directive syntax.

The compiler diagnoses unknown names, wrong locations, invalid operands, nonconstant required values, duplicate names, missing targets and unsuitable targets at their authored sites. It never searches past an invalid target to find a later eligible one. A known directive in the wrong context is a placement error, not an unknown name.

Module roles come from @mod.moth, @package.moth and @entry.moth filenames. Project and builder settings are ordinary records in @config.moth, a reserved build-only filename with no module role. Source files and builders cannot register directives or use them as macros.

Compiler references