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.
Type Parameters
Section titled “Type Parameters”TName extends string
TPattern
Section titled “TPattern”TPattern extends readonly PatternSchema[]
TPrecedence
Section titled “TPrecedence”TPrecedence extends number
TResultType
Section titled “TResultType”TResultType extends ResultSpec | undefined = undefined
Parameters
Section titled “Parameters”config
Section titled “config”TName
precedence
Section titled “precedence”TPrecedence
pattern
Section titled “pattern”(p) => BuiltPattern<TPattern, TResultType>
Returns
Section titled “Returns”NodeSchema<TName, TPattern, TPrecedence, TResultType>
Example
Section titled “Example”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())),});