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"
dependencies = [
"pyo3",
"rustpython-parser",
]
[[package]]

View File

@ -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" }

View File

@ -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(())
}