1

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.

1 Answer 1

1
  • 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.
Sign up to request clarification or add additional context in comments.

6 Comments

Ah the Node approach makes the most sense to me. Question: which parts of an AST as defined above are classifiable as a Node and which are not? The documentation is lacking such examples in my opinion.
Another question: getChildren(n) returns list[value], not list[Node]. How to retrieve all direct children nodes of a Node?
some children might not be nodes. So if you want to filter only the nodes that's possible in many ways. One is using a list comprehension: [ m | node m <- getChildren(n)]. But beware this will skip over any child which does not match the node m pattern.
To your other question: everything in an AST is node if it looks like name( ..., ...), so prefix terms with positional parameters and optional keyword parameters. In Rascal the type system is WYSIWYG, so nodes have different syntax from lists, sets, maps, integers, tuples, and so if something looks like a node, it certainly is a node.
I think (0 | it + 1 | node _ <- t) does not get the right result for the total number of nodes. When I do the same count using visit(), I get different answers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.