Crates.io | python-ast |
lib.rs | python-ast |
version | 0.3.1 |
source | src |
created_at | 2023-10-27 08:00:00.259165 |
updated_at | 2024-05-16 01:33:57.241088 |
description | A library for compiling Python to Rust |
homepage | |
repository | https://github.com/rexlunae/python-ast-rs.git |
max_upload_size | |
id | 1015718 |
size | 140,382 |
A Rust library for accessing a Python AST using the Python 3 ast library. Because it relies on Python itself to parse Python, it should be very close to the reference implementation. However, it is possible that changes in the underlying Python language could prevent it from working at all if there are dramatic changes to the syntax tree.
This project is at a very early state, and should be considered completely unstable.
Reading a Python file into an ast works like this:
use python_ast::parse;
fn read_python_file(input: std::path::Path) {
let py = read_to_string(input).unwrap();
let ast = parse(&py, "mod_name.py").unwrap();
println!("{:?}", ast);
}
You can also use the CodeGen trait to convert the Python code into Rust code. Please note that this feature is extremely unstable and experimental.
use python_ast::{parse, CodeGen, CodeGenContext, PythonOptions, SymbolTableScopes};
fn read_python_file(input: std::path::Path) {
let py = read_to_string(input).unwrap();
let ast = parse(&py, "mod_name.py").unwrap();
let rust = ast.to_rust(Context::Module("mod_name".to_string(), PythonOptions::default(), SymbolTableScopes::new())).unwrap();
println!("{}", rust);
}
The goal of this project is to create a fully-compiled Python-like language that is as close to the reference language as possible, but with the advantages of Rust's static types and fearless concurrency. At this point, it's probably best to view it as a proof of concept, rather than a workable tool, but I am hoping that this will change.