macros

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package macros extends the compiler with a @macro engine.

A @macro directive stores a declaration; OnPreprocess turns each declaration into a PrimaryExtension dispatching on TokAtPrompt; and every expansion re-enters the parser with the caller's arguments substituted into the body.

Typical use:

@macro greet(name) { runtime.const @{ value: $name } }
@greet("World")   // expands to: runtime.const @{ value: "World" }

Without this bundle, @name is "unexpected token @name".

Index

Constants

View Source
const DeclarationKey = "macros"

DeclarationKey is the meta slot where @macro declarations land before OnPreprocess converts them to a PrimaryExtension.

View Source
const ID = "macros"

Variables

View Source
var DefaultBuiltinMacros = []Declaration{
	{
		Name:   "check_required",
		Params: []string{"field"},
		Body:   `assert(.$field != nil && .$field != "", "field '" + "$field" + "' is required")`,
	},
	{
		Name:   "check_choice",
		Params: []string{"field", "choices"},
		Body:   `assert(.$field in $choices, "field '" + "$field" + "' must be one of " + string($choices))`,
	},
	{
		Name:   "check_range",
		Params: []string{"field", "min", "max"},
		Body:   `assert(.$field >= $min && .$field <= $max, "field '" + "$field" + "' must be between " + string($min) + " and " + string($max))`,
	},
}

DefaultBuiltinMacros provides standard reusable validation and guard macros. In expr-lang, membership testing uses the 'in' operator rather than contains().

View Source
var Directive = core.Directive{
	Name:    "macro",
	Example: "@macro greet(name) {\n  runtime.const @{ value: $name }\n}",
	Handler: handleDirective,
}

Directive parses `@macro name(p1, p2) { body }` into a Declaration appended to meta[DeclarationKey]. Duplicate names are a compile error, not a silent override.

Functions

func Bundle

func Bundle(_ map[string]string) core.Bundle

Types

type Declaration

type Declaration struct {
	Name   string
	Params []string
	Body   string
}

Declaration is the parsed form of `@macro name(params) { body }`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL