Skip to main content

Annotations

Annotations are compile-time metadata that can rewrite the IR. They are Nebra's answer to decorators (TypeScript / Python), attributes (C#), and macros (Rust): a way to attach behavior to declarations without changing the call sites.

@log("starting work")
function work()
-- ...
end

When the compiler meets @log, it looks up the annotation file named log, hands its apply function the declaration as a Lua table, and runs it in a sandboxed Lua interpreter. Whatever apply returns replaces the declaration before the rest of the compiler sees it.


Writing an annotation​

An annotation is a .neb file of its own. The file name is the annotation's name, so annotations/log.neb defines @log. It exports two things: an annotation table describing where it may be used and which arguments it takes, and an apply function.

-- annotations/log.neb
export local annotation = {
targets = { "Function", "ClassMethod" },
params = {
message = { type = "string", required = false }
}
}

export function apply(target, args)
local label = args.message or "called"
table.insert(target.body, 1, ir.exprStmt(ir.call("print", {
ir.stringLiteral("[log] " .. label)
})))
return target
end
  • targets lists the kinds of declaration the annotation may be written on (see Targets). A single kind can also be given as target = "Function".
  • params declares the arguments by name. Each one has a type ("string", "number", "boolean" or "any"), may be required, and may have a default. Only constant literals are accepted as arguments.
  • apply(target, args) receives the declaration and the validated arguments.

Annotation files are compiled with a 1-based index, whatever index_base the project uses, and imports are emitted as plain require calls.


Registering annotation files​

Tell the compiler where your annotations live:

# nebra.toml
annotations = [
"annotations", # directory
"lib/special.neb", # a single file
]

In a directory, every file that exports an annotation table or an apply function is an annotation. Any other .neb file next to them is a helper module the annotations can import.

Installed dependencies contribute their annotations automatically.


Helper modules​

An annotation file can import other .neb files, so annotations can share code:

-- annotations/util/naming.neb
export function toSnakeCase(name: string): string
return string.lower((string.gsub(name, "(%l)(%u)", "%1_%2")))
end
-- annotations/Table.neb
import { toSnakeCase } from "./util/naming"

export local annotation = { targets = { "Class" } }

export function apply(target, args)
-- ...
end

The imported modules are compiled together with the annotation and bundled into its sandbox. They run in the same annotation environment, so they can use ir as well.


Arguments​

Positional and named arguments both work:

@log("work") -- positional, bound in the order of `params`
@log(message = "work") -- named
@log -- no arguments

args is a table keyed by parameter name, with defaults filled in, so the handler reads args.message however the annotation was written.


Targets​

TargetApplies to
FunctionGlobal functions
LocalFunctionlocal function declarations
Variablelocal declarations
ParameterFunction parameters
Enumenum declarations
EnumMemberMembers of an enum
Classclass declarations
ClassMethodMethods of a class
ClassFieldFields of a class
ClassConstructorThe constructor of a class
ClassAccessorGetters and setters of a class
Interfaceinterface declarations
InterfaceMethodMethods of an interface
InterfaceFieldFields of an interface
Extendextend blocks
ExtensionMethodMethods of an extend block

Using an annotation on a kind it does not list is an error.


What apply receives​

The target is the declaration's IR as a Lua table. __kind names the node type (such as "FunctionDecl" or "ClassDecl"), and every property of the node is a camelCased field: a class has fields, methods and annotations, a field has name, typeAnnotation and defaultValue, a name is { name = "...", span = ... }.

Annotations run before names and types are resolved. A type written by name, such as Json<any> or DateTime, therefore arrives with kind = "Named": whether it is a class, an interface or an enum is not known yet.

A declaration's own annotations run before those of its members. A class annotation sees its fields and methods as they are written, including their annotations:

export function apply(target, args)
for _, field in ipairs(target.fields) do
for _, marker in ipairs(field.annotations or {}) do
if marker.name.name == "Column" then
-- ...
end
end
end
return target
end

The member annotations then run on whatever the class annotation returned.


What apply returns​

  • The target itself, changed or not: it replaces the declaration.
  • A list of statements: they replace the declaration, so an annotation can add code before or after it. When further annotations follow on the same declaration, the list has to contain a declaration of the same kind for them to run on.

The ir helpers​

The global ir builds new nodes without bookkeeping node ids:

HelperBuilds
ir.stringLiteral(value), ir.numberLiteral(raw), ir.boolLiteral(value), ir.nilLiteral()Literals
ir.nameExpr(name)A reference to a name
ir.call(callee, args)A call; callee is a node or a name
ir.methodCall(object, method, args)object:method(args)
ir.dotAccess(object, field)object.field
ir.newExpr(className, args)new ClassName(args)
ir.table(fields)A table constructor; each entry is a bare node (positional), { key = "name", value = node } or { key = node, value = node }
ir.localDecl(name, value)local name = value
ir.exprStmt(expr), ir.returnStmt(values)Statements
ir.param(name, opts), ir.functionDef(params, body, returnStmt)Parameters and anonymous functions
ir.varargExpr(), ir.varargMethodCallClosure(object, method)... and a closure forwarding it
ir.annotation(name, args)An annotation, such as a marker for another annotation or for reflection

The sandbox​

An annotation runs with standard Lua (string, table, math, pcall, ...), the ir helpers, and require for the helper modules it imports. It has no io, os, package, load, dofile or debug: annotations cannot read your filesystem, fetch URLs, or escape the compile.


Compiler builtins​

A small set of annotations is recognised directly by the compiler. They don't go through annotation files, can't be redefined, and don't need an annotations entry in nebra.toml. They change compile-time behaviour rather than IR.

  • @side(client | server | shared) - multiplayer-sandbox execution-side scoping for declarations. See Sides for the full mental model.

  • @reflectable - records a declaration for reflection under mode = "annotated".

  • @overrideCtor("template") - overrides how new ClassName(args) lowers to Lua. Applied to a class (or declare class) decl. The template is a format string with two placeholders:

    • $class - the resolved class identifier
    • $args - the comma-separated rendered arguments

    Example: nanos-world exposes every class as a global call (Player(args), not Player.new(args)). The nanos-world-types package emits @overrideCtor("$class($args)") on every class so:

    -- source
    local p = new Player()
    -- generated Lua
    local p = Player()

    When the template has no $args placeholder, the compiler appends a default (args) call so the arguments are never silently dropped.

  • @overrideInherit("template") - for a host that creates subclasses of its classes itself. Written on a declare class, it applies to every class extending that class, directly or further down. The template is the Lua expression that creates the class table:

    • $base - the resolved name of the class being extended
    • $name - the name of the new class as a quoted string

    Example: nanos world creates a subclass with Prop.Inherit("Name"). The nanos-world-types package writes @overrideInherit("$base.Inherit($name)") on Entity, so a plain extends is all it takes:

    -- source
    class Crate extends Prop
    constructor(location: Vector)
    super(location, new Rotator(), "nanos-world::SM_Cube")
    end

    override function SetScale(scale: Vector): nil
    super:SetScale(scale)
    end
    end

    local crate = new Crate(new Vector())

    -- generated Lua
    Crate = Prop.Inherit("Crate")
    function Crate:Constructor(location)
    self.Super:Constructor(location, Rotator(), "nanos-world::SM_Cube")
    end
    function Crate:SetScale(scale)
    self.Super:SetScale(scale)
    end

    local crate = Crate(Vector())

    super reaches a method or constructor of the host through self.Super, and one of a base class compiled from Nebra through that class. new follows the @overrideCtor of the nearest base class that has one. Written on a class compiled from Nebra, the template applies to that class alone, which is how a single class gets a different registered name.

    The host keeps the metatable of its objects, so such a class cannot declare accessors or operator (). operator [] and operator []= need a method of the host to be called through, named by the arguments index and newindex:

    @overrideInherit("$base.Inherit($name)", index = "index", newindex = "newindex")
    declare class Entity

    Reads and writes then reach them under the rules of the host, and a field the class declares is a key like any other. With operator []= calling self:SetValue(key, value) and operator [] calling self:GetValue(key, nil), a declared field lives in the entity's values. A class declaring only one of the two should keep that in mind: a field written through operator []= is read back through the host.

  • @overrideIs("template") - overrides how value is ClassName, instanceof and a type pattern in match test against a class and the classes extending it, for objects whose class Lua's metatables say nothing about. The template is a Lua condition with two placeholders:

    • $value - the value being tested, which can be anything, nil included
    • $class - the resolved name of the class tested against

    The value is evaluated once, even when the template uses it several times. The nanos-world-types package uses it to test through IsA:

    @overrideIs('(type($value) == "table" or type($value) == "userdata") and $value.IsA ~= nil and $value:IsA($class)')
    declare class Entity

When NOT to use an annotation​

Annotations are powerful and run at compile time, which makes them tempting. Don't reach for them when:

  • A plain function would do (e.g. validate(myValue) instead of @validate myValue).
  • The behavior depends on runtime state - annotations only see compile-time IR.
  • You need it just to suppress a warning - use the relevant [rules] knob instead.

Annotations are best for: registering symbols (routes, event handlers, entities), generating boilerplate, and runtime type guards.