RustPython/Parser

Question: How to walk/visit the AST?

Peter554 opened this issue · 10 comments

I'm trying to extract all the imports from a python file. To do this I'm using the rustpython_parser::parse function to get an AST and then iterating over all the statements in the body to find Stmt::Import and Stmt::ImportFrom. This works for imports defined at the root of the file, however misses any imports defined inside e.g. function/class definitions etc. So I think what I really want to do is walk the AST and visit all the nodes. Is there some way to achieve this with one of the rustpython packages?

Hi, rustpython_ast crate contains a few utilities like Visitor and Fold. They must be helpful.

bnkc commented

Hey there. I found the visitor.rs module but cannot import it. are there any updates?

Is the module public?

bnkc commented

I'm decently new to rust but from my understanding it is:

#[cfg(feature = "visitor")]
pub use visitor::Visitor;

I've also enabled the feature @fanninpm

The following line likely needs to be changed to say pub mod visitor {:

mod visitor {

bnkc commented

Ok should i open a PR?

Give it a shot!