I'm trying to find all method calls and the classes that contain them. If I understand correctly, pattern matches perform backtracking to match in all possible ways.
Take the following java code.
package main;
public class Main {
public static void main(String[] args) {
System.out.println("hello world");
System.out.println("hello again");
}
}
I'm loading the code with createAstsFromDirectory.
rascal>ast = createAstsFromDirectory(|home:///multiple-method-calls|, false);
I'm trying to find both calls to println. The following code matches once:
void findCalls(set[Declaration] ast)
{
visit(ast)
{
case \class(_,_,_,/\methodCall(_,_,str methodName,_)):
println("<methodName>");
}
}
rascal>findCalls(ast);
println
ok
This code matches four times:
void findCalls(set[Declaration] ast)
{
visit(ast)
{
case /\class(_,_,_,/\methodCall(_,_,str methodName,_)):
println("<methodName>");
}
}
rascal>findCalls(ast);
println
println
println
println
ok
How must the pattern look like to match exactly twice?
Related question, how to access the class name? When trying to access the class name I get an error message.
void findCalls(set[Declaration] ast)
{
visit(ast)
{
case /\class(str className,_,_,/\methodCall(_,_,str methodName,_)):
println("<className> <methodName>");
}
}
findCalls(ast);
Main println
|project://personal-prof/src/Assignment13Rules.rsc|(3177,9,<141,16>,<141,25>): Undeclared variable: className
It looks like the first match has className bound correctly to "Main", but the second one does not.
class
andmethodCall
the escape is not needed and this is nice if you also have the deep match/
in front ;-) – Jurgen Vinju Dec 10 '19 at 12:19