如何更新读取文件的转换?

时间:2017-04-12 18:48:48

标签: refactoring transformation rascal

如何更新文件的内容," teste.java"作为转换结果的例子?

loc s =  |project://java-sample/teste.java|;
content = readFile(s);
CompilationUnit cUnit = parse(#CompilationUnit, content);
visit(cUnit) {
   case (Statement) `if (<Expression cond>) { return true; } else { return false; }` =>  
        (Statement) `return <Expression cond>;`
}

1 个答案:

答案 0 :(得分:1)

visit返回新树。您可以使用writeFile将该树保存到文件中,该文件将对其进行解组并保存文件:

loc s =  |project://java-sample/teste.java|;
content = readFile(s);
CompilationUnit cUnit = parse(#CompilationUnit, content);
cUnitNew = visit(cUnit) { // note the assignment!
   case (Statement) `if (<Expression cond>) { return true; } else { return false; }` =>  
        (Statement) `return <Expression cond>;`
}
writeFile(s, cUnitNew); // write the new string to disk