rascal icon indicating copy to clipboard operation
rascal copied to clipboard

Source locations lost during transforming visit traversal

Open tvdstorm opened this issue 1 year ago • 0 comments

When doing a transforming visit on a parse tree, rewriting a nested element, the enclosing tree (trees?) don't get their source locations back. Strangely enough, adding a dummy case for the enclosing nonterminal fixes the problem.

NB: the exception says "Undeclared" but it is physically not there. NB: there is no difference between insert and =>

Reproduce:

module Bug

import IO;
import Node;

extend lang::std::Layout;
extend lang::std::Id;

syntax Stmt 
  = "if" Expr cond "{" Stmt body "}"
  | Id x "=" Id y
  ;

lexical Expr = [a-z];


void bug() {
    Stmt stmt = (Stmt)`if a { x = y }`;

    stmt2 = visit (stmt) {
        case (Expr)`a`: insert (Expr)`b`;
    }

    // original statement has a source location
    println(stmt.src);

    // prints empty map
    println(getKeywordParameters(stmt2));

    // throws exception Undeclared field: src for Tree = appl(Production prod,list[Tree] args)
    println(stmt2.src); 

    // However: the following does work... (commenting out previous statement)

    stmt3 = visit (stmt) {
        case (Expr)`a`: insert (Expr)`b`;
        case Stmt _: ;
    }

    // prints map with src entry
    println(getKeywordParameters(stmt3));

    // prints correct loc
    println(stmt3.src); 

}

tvdstorm avatar Aug 15 '24 09:08 tvdstorm