Search code examples
visitor-patternrascal

Rascal MPL print id of all traversed nodes in a visit


Is it possible to print all node ids / values that are traversed in a visit? Take the following code sample:

top-down visit(asts) {
    case x: println(typeOf(x));
}

I now get all types of the nodes that were visited. I would instead like to know the ids / values of all x that were encountered (without printing the entire tree). In other words, I am curious as to how Rascal traverses the list[Declaration] asts containing lang::java::m3::ASTs.

As a corrolary, is it possible to print direct ascendants / descendants of a Declaration regardless of their type? Or print the total number of children of an ast?

In the documentation (https://www.rascal-mpl.org/docs/rascal/expressions/visit/) there is no mention of this.


Solution

    • in principle a Rascal value does not have an "id". It's structure is its "id" and its id is its structure. You can print a hash if you want, using md5sum for example from the IO function: case value x : println(md5sum(x));
    • Rascal traverses lists from left to right. In top-down mode it first visits the entire list and in bottom-up mode it goes first to the children.
    • printing the total number of nodes in a tree: (0 | it + 1 | /_ <- t)
    • printing all children of a node: import Node;, case node n: println(getChildren(n));
    • ascendants are not accessible unless you match deeper and include two levels of nodes in a pattern.