Search code examples
rascal

Comparing AST Nodes


For calculating duplication, I am parsing a java project into the AST.

But when trying to compare the AST nodes, Declaration attributes such as src are compared too, which leads to the comparison failing.

This change was introduced < 1 year ago where Declaration annotations were removed.

To negate this I have to do the following:

myAst = visit(myAst) {
    case node n : {
        n.src = |unknown:///|;
    }
}

And then compare the nodes.

Is there any other way to make comparing nodes easier?


Solution

  • Pattern matching ignores keyword parameters if they arent part of the pattern.

    rascal>data  X = z(int y = 0);
    ok
    rascal>x1 = z(y=3);
    X: z(y=3)
    rascal>x2 = z(y=4);
    X: z(y=4)
    rascal>x1 == x2
    bool: false
    rascal>x1 := x2
    bool: true
    rascal>z() := x1
    bool: true
    rascal>z(y=5) := x1
    bool: false
    rascal>
    

    So if you want to test for equality, use pattern matching. If you are putting them in a map or a set, you will indeed have to "unset" them. There is a rascal function that does this in the most efficient way possible. The function unset in Node does it for the current node, and the unsetRec function does it recursively using a similar visitor as in your code.