Skip to main content

Module visit

Module visit 

Source
Expand description

Generic AST visitor used by the linter, formatter, and any other crate that needs to walk every SNode in a parsed program.

Centralizing this here keeps a single source of truth for which children each Node variant has — adding a new variant requires one edit (in collect_children) and every consumer benefits.

§Usage

use harn_parser::visit::walk_program;
let mut count = 0;
walk_program(&program, &mut |node| {
    if matches!(&node.node, harn_parser::Node::FunctionCall { .. }) {
        count += 1;
    }
});

The visitor invokes the closure on each node before recursing into its children (pre-order). To stop recursion at a particular node, prefer using walk_children directly.

Functions§

immediate_children
Collect node’s immediate children without recursing. Lets callers walk selectively (e.g. stop descending at nested loops) while still relying on this module’s single source of truth for each variant’s children.
walk_children
Recurse into node’s children without re-visiting node itself. Useful when a caller wants to handle the parent specially and then continue the default traversal.
walk_node
Visit node, then recurse into its children.
walk_program
Walk every node in program in pre-order, invoking visitor on each.