Expand description
CEL macro system for compile-time expression expansion. CEL macro system for extending the expression language.
This module provides functionality for defining and registering custom macros that can expand CEL expressions at compile time. Macros enable syntactic sugar and domain-specific language extensions without modifying the core parser.
§Overview
CEL macros transform parsed expressions into new expression trees before type checking and evaluation. This allows you to:
- Add new syntactic constructs (e.g.,
has(foo.bar)for presence testing) - Implement domain-specific operators (e.g.,
all(list, predicate)) - Optimize common patterns by rewriting expressions
- Extend CEL with custom comprehension forms
§Macro Types
There are two types of macros:
- Global macros: Called as functions, e.g.,
all([1, 2, 3], x, x > 0) - Receiver macros: Called as methods, e.g.,
list.filter(x, x > 0)
Both types can accept fixed or variable numbers of arguments.
§Examples
§Defining a global macro
use cel_cxx::macros::{Macro, MacroExprFactory, Expr};
// Macro that creates a constant expression with doubled value
let double_macro = Macro::new_global("double", 1, |factory, args| {
if let Some(expr) = args.first() {
if let Some(val) = expr.kind().and_then(|k| k.as_constant()) {
if let Some(int_val) = val.as_int() {
return Some(factory.new_const(int_val * 2));
}
}
}
None
});§Defining a receiver macro
// Macro for optional element access: list.maybe_get(index)
let maybe_get_macro = Macro::new_receiver("maybe_get", 1, |factory, target, args| {
// Implementation would handle optional list indexing
Some(factory.new_call("_?.[]", &[target, args[0].clone()]))
});Structs§
- Call
Expr - A function or method call expression.
- Comprehension
Expr - A comprehension expression for list/map processing.
- Expr
- A CEL expression node in the abstract syntax tree.
- Ident
Expr - An identifier expression referencing a variable or constant.
- List
Expr - A list literal expression.
- List
Expr Element - An element in a list expression.
- Macro
- A CEL macro that expands expressions at compile time.
- Macro
Expr Factory - Factory for constructing CEL expressions during macro expansion.
- MapExpr
- A map literal expression.
- MapExpr
Entry - An entry in a map expression.
- Select
Expr - A field selection expression.
- Struct
Expr - A struct literal expression.
- Struct
Expr Field - A field in a struct expression.
Enums§
- Expr
Kind - The kind/type of a CEL expression.
Traits§
- Global
Macro Expander - Trait for global macro expansion functions.
- Receiver
Macro Expander - Trait for receiver macro expansion functions.