Wednesday 11 September 2013

Get list of calls to a method and arguments passed with the call from source code

Get list of calls to a method and arguments passed with the call from
source code

Question
Given the following code, I want to extract muffinize and all the
arguments passed with it:
$ask = false
muffinize("Shoop")
muffinize("Woop") if($ask == true)
if($ask == false)
muffinize("Pants")
end
This is the output that I would expect:
Call#:Args
1:"Shoop"
2:"Woop"
3:"Pants"
What I'm doing so far
I'm parsing the source code with Ripper to find the locations where the
method is called. Then I'm finding the range of characters in the line of
code where the method is called. After extracting the method from the
line, I'm then analyzing it with the Parser gem. I will post the code as
an answer once I've complete it, but I'm interested in hearing if there's
a better way to do this. I don't know code parsing that well so I'm kind
of flailing around blindly at this point.
Why not just use a Regex
A regex would still capture commented code blocks. There's probably other
corner cases that I haven't considered yet.

No comments:

Post a Comment