I am new to rascal and want to extract conditional statements (if,while,etc) from a java project.
The best method seems to be at http://tutor.rascal-mpl.org/Rascal/Libraries/analysis/m3/Core/containment/containment.html#/Rascal/Libraries/analysis/m3/AST/AST.html
My code so far is
void statements(loc location) {
ast = createAstFromFile(location,true,javaVersion="1.7");
for(/Statement s := ast) println(readFile(s@src));
}
But this returns all statements including comments. How do i filter the statements to only return conditional statements if,while,for,etc.?
Rascal implements the Visitor pattern for this. You could do something like this on your ast
variable:
visit(ast){
case \if(icond,ithen,ielse): {
println(" if-then-else statement with condition <icond> found"); }
case \if(icond,ithen): {
println(" if-then statement with condition <icond> found"); }
};
This example returns the if
statements from the code.
You can find the definitions of patterns to use as case patterns in the package lang::java::m3::AST.