Skip to content

defineNode

defineNode<TName, TPattern, TPrecedence, TResultType>(config): NodeSchema<TName, TPattern, TPrecedence, TResultType>

Defined in: src/schema/index.ts:605

Define a node type for the grammar.

The pattern is authored through a fluent builder (see PatternBuilder): elements chain left to right, .as(name) names the element just appended, .result(def) declares the node’s result type (a binding name, or an arktype def — validated against the chain’s bindings), and .eval(fn) attaches evaluation (bindings arrive as memoized thunks; the return type is checked against the declared result). Every def is validated by arktype WHERE IT IS WRITTEN — a typo like operand(“nmbr”) is a compile error at that call.

TName extends string

TPattern extends readonly PatternSchema[]

TPrecedence extends number

TResultType extends ResultSpec | undefined = undefined

TName

TPrecedence

(p) => BuiltPattern<TPattern, TResultType>

NodeSchema<TName, TPattern, TPrecedence, TResultType>

A polymorphic, short-circuiting ternary:
const ternary = defineNode({
name: "ternary",
precedence: 0,
pattern: (p) => p
.operand("boolean").as("cond")
.constVal("?")
.expr().as("then")
.constVal(":")
.rest("then").as("else")
.result("then")
.eval(({ cond, then, else: alt }) => (cond() ? then() : alt())),
});