Skip to content

Commit

Permalink
Flag to print AST in Jupyter Mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 24, 2023
1 parent 828603a commit e711a9f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions crates/ruff_dev/src/print_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use rustpython_parser::ast::Suite;
use rustpython_parser::Parse;
use rustpython_parser::{parse, Mode};

#[derive(clap::Args)]
pub(crate) struct Args {
/// Python file for which to generate the AST.
#[arg(required = true)]
file: PathBuf,
/// Run in Jupyter mode i.e., allow line magics.
#[arg(long)]
jupyter: bool,
}

pub(crate) fn main(args: &Args) -> Result<()> {
let contents = fs::read_to_string(&args.file)?;
let python_ast = Suite::parse(&contents, &args.file.to_string_lossy())?;
let mode = if args.jupyter {
Mode::Jupyter
} else {
Mode::Module
};
let python_ast = parse(&contents, mode, &args.file.to_string_lossy())?;
println!("{python_ast:#?}");
Ok(())
}

0 comments on commit e711a9f

Please sign in to comment.