mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
* stage1 * compiler pass build * introduce rustpython-compiler-source * stage2 * fixup * pass compile * Fix hello world compiler test * Fix code generation for if-elif-else statement * Fix code generation for lambda expression * Fix code generation for integers * Fix code generation for fstrings * Fix code generation for if statement * Fix code generation for if statement * Fix code generation for if statement * Fix code generation for fstring * Fix code generation for class definition * Replace feature flags * Initialize frozen core modules * Allow __future__ import after module doc comment * Disable ast module * Commit remaining fixes for compile errors in examples * Fix some warnings * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Split ast stdlib module into files * Fix codegen for positional arguments with defaults * Update ast stdlib module * Update ast stdlib module * Extract string and constant handling from expression.rs * Always add required fields to AST nodes * Compile doc strings correctly again * Enable "ast" Cargo feature by default * Refactor compilation of big integer literal * Update ast stdlib module * Update ast stdlib module * Update ast stdlib module * Reset barebones example * Fix some left-over warnings * Undo accidential change * Adapt shell to ruff parser * Pin parser to v0.4.10 * fix clippy * Add TODO about interactive mode * Fix compilation of complex number expression * Remove moved code * Update test case to ruff v0.4.10 * Apply suggestion Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> * Apply suggestion Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> * Fix compilation of fstring expression * Fix compilation of fstring expression * Fix wasm compile errors * Attach correct source locations to ast objects * Fix some more wasm compile errors * Consider compile mode and enable AST stdlib module again * Fix incorrect AST source location end column * Fix compile error if "compiler" feature is not enabled * Fix regrtests * Fix some test_ast tests * Add source range to type ignore * Fix incompatibility with Rust 2024 edition * Fix todos by implementing missing ast conversions and deleting unused code * Appease clippy * Fix remaining ast tests * Fix remaining ast tests * Mark/fix remaining tests * Fix more * Hacky windows fix --------- Co-authored-by: Kangzhi Shi <shikangzhi@gmail.com> Co-authored-by: Jeong YunWon <jeong@youknowone.org> Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com> Co-authored-by: Noa <coolreader18@gmail.com>
144 lines
4.0 KiB
Rust
144 lines
4.0 KiB
Rust
/// This an example usage of the rustpython_parser crate.
|
|
/// This program crawls over a directory of python files and
|
|
/// tries to parse them into an abstract syntax tree (AST)
|
|
///
|
|
/// example usage:
|
|
/// $ RUST_LOG=info cargo run --release parse_folder /usr/lib/python3.7
|
|
extern crate env_logger;
|
|
#[macro_use]
|
|
extern crate log;
|
|
|
|
use ruff_python_parser::parse_module;
|
|
use rustpython_compiler::ast;
|
|
use std::{
|
|
path::{Path, PathBuf},
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
fn main() {
|
|
env_logger::init();
|
|
|
|
let folder: PathBuf = std::env::args_os()
|
|
.nth(1)
|
|
.expect("please pass a path argument")
|
|
.into();
|
|
|
|
if folder.exists() && folder.is_dir() {
|
|
println!("Parsing folder of python code: {folder:?}");
|
|
let t1 = Instant::now();
|
|
let parsed_files = parse_folder(&folder).unwrap();
|
|
let t2 = Instant::now();
|
|
let results = ScanResult {
|
|
t1,
|
|
t2,
|
|
parsed_files,
|
|
};
|
|
statistics(results);
|
|
} else {
|
|
println!("{folder:?} is not a folder.");
|
|
}
|
|
}
|
|
|
|
fn parse_folder(path: &Path) -> std::io::Result<Vec<ParsedFile>> {
|
|
let mut res = vec![];
|
|
info!("Parsing folder of python code: {:?}", path);
|
|
for entry in path.read_dir()? {
|
|
debug!("Entry: {:?}", entry);
|
|
let entry = entry?;
|
|
let metadata = entry.metadata()?;
|
|
|
|
let path = entry.path();
|
|
if metadata.is_dir() {
|
|
res.extend(parse_folder(&path)?);
|
|
}
|
|
|
|
if metadata.is_file() && path.extension().and_then(|s| s.to_str()) == Some("py") {
|
|
let parsed_file = parse_python_file(&path);
|
|
match &parsed_file.result {
|
|
Ok(_) => {}
|
|
Err(y) => error!("Erreur in file {:?} {:?}", path, y),
|
|
}
|
|
|
|
res.push(parsed_file);
|
|
}
|
|
}
|
|
Ok(res)
|
|
}
|
|
|
|
fn parse_python_file(filename: &Path) -> ParsedFile {
|
|
info!("Parsing file {:?}", filename);
|
|
match std::fs::read_to_string(filename) {
|
|
Err(e) => ParsedFile {
|
|
filename: Box::new(filename.to_path_buf()),
|
|
code: "".to_owned(),
|
|
num_lines: 0,
|
|
result: Err(e.to_string()),
|
|
},
|
|
Ok(source) => {
|
|
let num_lines = source.lines().count();
|
|
let result = parse_module(&source)
|
|
.map(|x| x.into_suite())
|
|
.map_err(|e| e.to_string());
|
|
ParsedFile {
|
|
filename: Box::new(filename.to_path_buf()),
|
|
code: source.to_string(),
|
|
num_lines,
|
|
result,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn statistics(results: ScanResult) {
|
|
// println!("Processed {:?} files", res.len());
|
|
println!("Scanned a total of {} files", results.parsed_files.len());
|
|
let total: usize = results.parsed_files.len();
|
|
let total_lines: usize = results.parsed_files.iter().map(|p| p.num_lines).sum();
|
|
let failed = results
|
|
.parsed_files
|
|
.iter()
|
|
.filter(|p| p.result.is_err())
|
|
.count();
|
|
let passed = results
|
|
.parsed_files
|
|
.iter()
|
|
.filter(|p| p.result.is_ok())
|
|
.count();
|
|
println!("Passed: {passed} Failed: {failed} Total: {total}");
|
|
println!(
|
|
"That is {} % success rate.",
|
|
(passed as f64 * 100.0) / total as f64
|
|
);
|
|
let duration = results.t2 - results.t1;
|
|
println!("Total time spend: {duration:?}");
|
|
println!(
|
|
"Processed {} files. That's {} files/second",
|
|
total,
|
|
rate(total, duration)
|
|
);
|
|
println!(
|
|
"Processed {} lines of python code. That's {} lines/second",
|
|
total_lines,
|
|
rate(total_lines, duration)
|
|
);
|
|
}
|
|
|
|
fn rate(counter: usize, duration: Duration) -> f64 {
|
|
(counter * 1_000_000) as f64 / duration.as_micros() as f64
|
|
}
|
|
|
|
struct ScanResult {
|
|
t1: Instant,
|
|
t2: Instant,
|
|
parsed_files: Vec<ParsedFile>,
|
|
}
|
|
|
|
struct ParsedFile {
|
|
filename: Box<PathBuf>,
|
|
code: String,
|
|
num_lines: usize,
|
|
result: ParseResult,
|
|
}
|
|
|
|
type ParseResult = Result<Vec<ast::Stmt>, String>;
|