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
targetslists the kinds of declaration the annotation may be written on (see Targets). A single kind can also be given astarget = "Function".paramsdeclares the arguments by name. Each one has atype("string","number","boolean"or"any"), may berequired, and may have adefault. 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
| Target | Applies to |
|---|---|
Function | Global functions |
LocalFunction | local function declarations |
Variable | local declarations |
Parameter | Function parameters |
Enum | enum declarations |
EnumMember | Members of an enum |
Class | class declarations |
ClassMethod | Methods of a class |
ClassField | Fields of a class |
ClassConstructor | The constructor of a class |
ClassAccessor | Getters and setters of a class |
Interface | interface declarations |
InterfaceMethod | Methods of an interface |
InterfaceField | Fields of an interface |
Extend | extend blocks |
ExtensionMethod | Methods 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:
| Helper | Builds |
|---|---|
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 undermode = "annotated". -
@overrideCtor("template")- overrides hownew ClassName(args)lowers to Lua. Applied to aclass(ordeclare 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), notPlayer.new(args)). Thenanos-world-typespackage emits@overrideCtor("$class($args)")on every class so:-- sourcelocal p = new Player()-- generated Lualocal p = Player()When the template has no
$argsplaceholder, 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 adeclare 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"). Thenanos-world-typespackage writes@overrideInherit("$base.Inherit($name)")onEntity, so a plainextendsis all it takes:-- sourceclass Crate extends Propconstructor(location: Vector)super(location, new Rotator(), "nanos-world::SM_Cube")endoverride function SetScale(scale: Vector): nilsuper:SetScale(scale)endendlocal crate = new Crate(new Vector())-- generated LuaCrate = Prop.Inherit("Crate")function Crate:Constructor(location)self.Super:Constructor(location, Rotator(), "nanos-world::SM_Cube")endfunction Crate:SetScale(scale)self.Super:SetScale(scale)endlocal crate = Crate(Vector())superreaches a method or constructor of the host throughself.Super, and one of a base class compiled from Nebra through that class.newfollows the@overrideCtorof 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 []andoperator []=need a method of the host to be called through, named by the argumentsindexandnewindex:@overrideInherit("$base.Inherit($name)", index = "index", newindex = "newindex")declare class EntityReads 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 []=callingself:SetValue(key, value)andoperator []callingself: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 throughoperator []=is read back through the host. -
@overrideIs("template")- overrides howvalue is ClassName,instanceofand a type pattern inmatchtest 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,nilincluded$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-typespackage uses it to test throughIsA:@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.