Skip to main content

Visitor

Simple Story

You want to do something at every node of a big tree, but only care about a few kinds of node.

Rather than writing the walking logic yourself and getting it wrong, you hand over an object that says "when you reach a loop, call me". The tree walks itself and calls you back at the parts you asked about. That is how the mark scheme checks a pupil used a loop without reading every twig itself.

What it is

The visitor pattern separates traversing a structure from what to do at each element. The tree knows how to walk itself; the visitor supplies one method per node type, and only the interesting ones are overridden.

It fits ASTs particularly well because a tree has many node types and any given analysis cares about few of them.

In Ares 2

The api/ast package uses JavaParser's visitors to collect the information an assertion needs, keeping the traversal generic and the question specific.

Further reading