mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-30 23:27:39 +00:00
Add parser deps to rustpython_ast_pyo3 (#75)
This commit is contained in:
parent
531aeb3511
commit
ae3a477c97
5 changed files with 192 additions and 0 deletions
|
@ -3,3 +3,47 @@ mod py_ast;
|
|||
pub mod wrapper;
|
||||
|
||||
pub use py_ast::{init, PyNode, ToPyAst};
|
||||
use pyo3::prelude::*;
|
||||
use rustpython_parser::ast::{source_code::SourceLocator, Fold};
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (source, filename="<unknown>", *, type_comments=false, locate=true))]
|
||||
pub fn parse<'py>(
|
||||
source: &str,
|
||||
filename: &str,
|
||||
type_comments: bool,
|
||||
locate: bool,
|
||||
py: Python<'py>,
|
||||
) -> PyResult<&'py PyAny> {
|
||||
if type_comments {
|
||||
todo!("'type_comments' is not implemented yet");
|
||||
}
|
||||
let parsed = rustpython_parser::parse(source, rustpython_parser::Mode::Module, filename)
|
||||
.map_err(|e| PyErr::new::<pyo3::exceptions::PySyntaxError, _>(e.to_string()))?;
|
||||
if locate {
|
||||
let parsed = SourceLocator::new(source).fold(parsed).unwrap();
|
||||
parsed.module().unwrap().to_py_ast(py)
|
||||
} else {
|
||||
parsed.module().unwrap().to_py_ast(py)
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
fn rustpython_ast(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
py_ast::init(py)?;
|
||||
|
||||
#[cfg(feature = "wrapper")]
|
||||
{
|
||||
let ast = PyModule::new(py, "ast")?;
|
||||
wrapper::located::add_to_module(py, ast)?;
|
||||
m.add_submodule(ast)?;
|
||||
|
||||
let ast = PyModule::new(py, "unlocated_ast")?;
|
||||
wrapper::ranged::add_to_module(py, ast)?;
|
||||
m.add_submodule(ast)?;
|
||||
}
|
||||
|
||||
m.add_function(wrap_pyfunction!(parse, m)?)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue