-
Notifications
You must be signed in to change notification settings - Fork 6
/
ffi_python.rs
91 lines (79 loc) · 2.39 KB
/
ffi_python.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
use pyo3::prelude::*;
#[macro_use]
extern crate rust_to_ocaml_attr;
use cst_to_ast::Parser as CSTToASTParser;
use crate::printers::parse_module_print_ast_code;
use crate::printers::parse_module_print_ast_pretty_and_errors;
use crate::printers::PrintingMode;
pub mod ast;
pub mod constants;
pub mod cst_to_ast;
pub mod errors;
pub mod node_wrapper;
pub mod parser_post_process;
pub mod parser_pre_process;
pub mod printers;
pub mod sitter;
pub mod string_helpers;
#[pyfunction]
fn py_parse_module_print_ast(input_code: String) -> PyResult<(String, String)> {
Ok(parse_module_print_ast_code(
input_code,
PrintingMode::ASTOnly,
))
}
#[pyfunction]
fn py_parse_module_print_ast_and_pretty_print(input_code: String) -> PyResult<(String, String)> {
Ok(parse_module_print_ast_code(
input_code,
PrintingMode::ASTAndPrettyPrintAST,
))
}
#[pyfunction]
fn py_parse_module_print_ast_pretty_print_only(input_code: String) -> PyResult<(String, String)> {
Ok(parse_module_print_ast_code(
input_code,
PrintingMode::PrettyPrintASTOnly,
))
}
#[pyfunction]
fn py_parse_module_print_ast_pretty_and_errors(
input_code: String,
) -> PyResult<(String, String, String)> {
Ok(parse_module_print_ast_pretty_and_errors(input_code))
}
///
/// run errpy but ignore result - useful for benchmarking
#[pyfunction]
fn py_parse_module(input_code: String) -> PyResult<()> {
let mut cst_to_ast = CSTToASTParser::new(input_code);
_ = cst_to_ast.parse();
Ok(())
}
/// A Python module implemented in Rust. The name of this function must match
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
fn ffi_python(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(py_parse_module_print_ast, m)?)?;
m.add_function(wrap_pyfunction!(
py_parse_module_print_ast_and_pretty_print,
m
)?)?;
m.add_function(wrap_pyfunction!(
py_parse_module_print_ast_pretty_print_only,
m
)?)?;
m.add_function(wrap_pyfunction!(
py_parse_module_print_ast_pretty_and_errors,
m
)?)?;
m.add_function(wrap_pyfunction!(py_parse_module, m)?)?;
Ok(())
}