From aa8b4e2305d7048de8b392dfe7131d801a7418fd Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 19 Dec 2020 00:52:06 +0800 Subject: [PATCH] nac3embedded: parse --- Cargo.lock | 1 + nac3embedded/Cargo.toml | 6 +++--- nac3embedded/src/lib.rs | 7 +++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f2d08fd..857ba230 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -460,6 +460,7 @@ name = "nac3embedded" version = "0.1.0" dependencies = [ "pyo3", + "rustpython-parser", ] [[package]] diff --git a/nac3embedded/Cargo.toml b/nac3embedded/Cargo.toml index 230d0d9e..e2b466f0 100644 --- a/nac3embedded/Cargo.toml +++ b/nac3embedded/Cargo.toml @@ -8,6 +8,6 @@ edition = "2018" name = "nac3embedded" crate-type = ["cdylib"] -[dependencies.pyo3] -version = "0.12.4" -features = ["extension-module"] +[dependencies] +pyo3 = { version = "0.12.4", features = ["extension-module"] } +rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" } diff --git a/nac3embedded/src/lib.rs b/nac3embedded/src/lib.rs index 213f15e6..bffe2374 100644 --- a/nac3embedded/src/lib.rs +++ b/nac3embedded/src/lib.rs @@ -1,5 +1,7 @@ use pyo3::prelude::*; use pyo3::wrap_pyfunction; +use pyo3::exceptions; +use rustpython_parser::parser; #[pyfunction] fn add_host_object(obj: PyObject) -> PyResult<()> { @@ -7,7 +9,9 @@ fn add_host_object(obj: PyObject) -> PyResult<()> { let obj: &PyAny = obj.extract(py)?; let inspect = PyModule::import(py, "inspect")?; let source = inspect.call1("getsource", (obj.get_type(), ))?; - println!("source:\n{}", source); + let ast = parser::parse_program(source.extract()?).map_err(|e| + exceptions::PySyntaxError::new_err(format!("failed to parse host object source: {}", e)))?; + println!("{:?}", ast); Ok(()) })?; Ok(()) @@ -16,6 +20,5 @@ fn add_host_object(obj: PyObject) -> PyResult<()> { #[pymodule] fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(add_host_object, m)?)?; - Ok(()) }