forked from M-Labs/nac3
1
0
Fork 0

nac3embedded: parse

This commit is contained in:
Sebastien Bourdeauducq 2020-12-19 00:52:06 +08:00
parent b79d8ad640
commit aa8b4e2305
3 changed files with 9 additions and 5 deletions

1
Cargo.lock generated
View File

@ -460,6 +460,7 @@ name = "nac3embedded"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"pyo3", "pyo3",
"rustpython-parser",
] ]
[[package]] [[package]]

View File

@ -8,6 +8,6 @@ edition = "2018"
name = "nac3embedded" name = "nac3embedded"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies.pyo3] [dependencies]
version = "0.12.4" pyo3 = { version = "0.12.4", features = ["extension-module"] }
features = ["extension-module"] rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" }

View File

@ -1,5 +1,7 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use pyo3::wrap_pyfunction; use pyo3::wrap_pyfunction;
use pyo3::exceptions;
use rustpython_parser::parser;
#[pyfunction] #[pyfunction]
fn add_host_object(obj: PyObject) -> PyResult<()> { 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 obj: &PyAny = obj.extract(py)?;
let inspect = PyModule::import(py, "inspect")?; let inspect = PyModule::import(py, "inspect")?;
let source = inspect.call1("getsource", (obj.get_type(), ))?; 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(())
})?; })?;
Ok(()) Ok(())
@ -16,6 +20,5 @@ fn add_host_object(obj: PyObject) -> PyResult<()> {
#[pymodule] #[pymodule]
fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> { fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(add_host_object, m)?)?; m.add_function(wrap_pyfunction!(add_host_object, m)?)?;
Ok(()) Ok(())
} }