I am looking to do some automated analysis on packages, e.g. automatically check for common mistakes. Mathematica makes it relatively easy to manipulate code as data, and Import
can get the expressions from a package, so I was hoping that this would be possible.
The biggest obstacle so far is that having the proper context for each symbol is often critical, and Import
doesn't handle this.
Is there an existing solution that can infer the correct context of each symbol when importing a package?
Example:
code = "
BeginPackage[\"Foo`\"]
MyFoo::usage = \"MyFoo[] is a function\";
Begin[\"`Private`\"]
foo[] := 3 (* helper function *)
MyFoo[] := foo[]^2 (* my function *)
End[]
EndPackage[]
";
ImportString[code, "HeldExpressions"]
{HoldComplete[BeginPackage["Foo`"]], HoldComplete[MyFoo::usage = "MyFoo[] is a function";], HoldComplete[Begin["`Private`"]], HoldComplete[foo[] := 3], HoldComplete[MyFoo[] := foo[]^2], HoldComplete[End[]], HoldComplete[EndPackage[]]}
I want HoldComplete[Foo`MyFoo::usage = "MyFoo[] is a function";]
, HoldComplete[Foo`Private`foo[] := 3]
, etc.
I know that creating something that always works is plainly impossible without rewriting the evaluator itself. What I am hoping for is something that works well enough to be practically useful and handles most common package structures. And something that hopefully already exists.