import { ADFNode } from './adfNode'; import type { ADFNodeGroup } from './types/ADFNodeGroup'; import type { ADFNodeContentRangeSpec } from './types/ADFNodeSpec'; export type ADFVisitor = { $onePlus?: (content: C) => C; $or?: (content: Array) => C; $range?: (item: ADFNodeContentRangeSpec, content: C) => C; $zeroPlus?: (content: C) => C; group?: (group: ADFNodeGroup, nodes: Array) => G; node?: (node: ADFNode, children: Array, cycle?: true) => N; }; /** * Implements post-order traversal of an ADF DSL tree. * * Traverse accepts a root node of the ADF DSL tree and a visitor object. * Visitor is a pattern that is commonly used in tree traversal algorithms. * It allows to separate the traversal logic from the actual processing logic. * * The visitor object should have the following methods: * - node(node, children, cycle) - called for each node in the tree * - node - the node being visited * - children - an array of processed children of the node * - cycle - a flag indicating that the node is being visited again due to a cycle * - group(group, nodes) - called for each group in the tree * - group - the group being visited * - nodes - an array of processed nodes in the group * - $or(content) - called for each $or content in the tree * - content - an array of processed nodes or groups * - $onePlus(content) - called for each $one+ content in the tree * - content - the processed content * - $zeroPlus(content) - called for each $zero+ content in the tree * - content - the processed content * - $range(item, content) - called for each $range content in the tree * - item - the range content item, includes the range metadata – min and max * - content - the processed content * * How does it deal with cycles? * * In order to deal with cyclic structure we stop processing children if the node was seen before. * That allows us to still return something meaningful to a parent node without falling into a cycle. * * Example usage: * const doc = adfNode('doc').definine({root: true}); * * traverse(doc, { * node(node, children) {}, * group(group, nodes) {}, * $or(content) {}, * $onePlus(content) {}, * $zeroPlus(content) {}, * $range(item, content) {}, * }) */ export declare function traverse(node: ADFNode, visitor: ADFVisitor): void;