|
1 | 1 | pub mod ast; |
2 | 2 | pub mod parser; |
3 | 3 | pub mod visitor; |
4 | | -pub mod transform; |
5 | 4 |
|
6 | | -use pest::error::{ErrorVariant, LineColLocation, InputLocation}; |
7 | | -use crate::parser::{ParsingError, ParserRule, ProgramResult}; |
8 | | - |
9 | | -// PYTHON STUFF |
10 | | -use pyo3::prelude::*; |
11 | | -use pyo3::{create_exception, PyErr}; |
12 | | -use pyo3::types::PyDict; |
13 | | -use pyo3::exceptions; |
14 | | - |
15 | | -create_exception!(unrealscriptplus, ScriptParseError, pyo3::exceptions::PyException); |
16 | | - |
17 | | -fn parsing_error_to_py_err(py: Python, error: ParsingError) -> PyErr { |
18 | | - match error { |
19 | | - ParsingError::PestError(e) => ScriptParseError::new_err(pest_error_to_py_object(py, e)), |
20 | | - ParsingError::EncodingError(e) => exceptions::PyUnicodeEncodeError::new_err(e.to_string()), |
21 | | - ParsingError::IoError(e) => exceptions::PyIOError::new_err(e.to_string()) |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -fn program_result_to_py_object(py: Python, result: &ProgramResult) -> Py<PyAny> { |
26 | | - let errors: Vec<Py<PyAny>> = result.errors.iter().map(|e| { |
27 | | - let dict = PyDict::new(py); |
28 | | - dict.set_item("line", e.span.line).unwrap(); |
29 | | - dict.set_item("message", &e.message).unwrap(); |
30 | | - dict.set_item("severity", format!("{:?}", e.severity)).unwrap(); |
31 | | - dict.into() |
32 | | - }).collect(); |
33 | | - |
34 | | - let dict = PyDict::new(py); |
35 | | - dict.set_item("errors", errors).unwrap(); |
36 | | - dict.into() |
37 | | -} |
38 | | - |
39 | | -fn pest_error_to_py_object(py: Python, e: pest_consume::Error<ParserRule>) -> Py<PyAny> { |
40 | | - let dict = PyDict::new(py); |
41 | | - |
42 | | - match e.line_col { |
43 | | - LineColLocation::Pos(pos) => { |
44 | | - dict.set_item("line_col", pos).unwrap(); |
45 | | - }, |
46 | | - LineColLocation::Span(span1, span2) => { |
47 | | - dict.set_item("line_col", (span1, span2)).unwrap(); |
48 | | - } |
49 | | - }; |
50 | | - |
51 | | - match e.location { |
52 | | - InputLocation::Pos(pos) => { |
53 | | - dict.set_item("location", pos).unwrap(); |
54 | | - }, |
55 | | - InputLocation::Span(span) => { |
56 | | - dict.set_item("location", span).unwrap(); |
57 | | - } |
58 | | - }; |
59 | | - |
60 | | - let variant: Py<PyAny> = match e.variant { |
61 | | - ErrorVariant::ParsingError { positives, negatives } => { |
62 | | - let positives: Vec<String> = positives.into_iter().map(|r| format!("{:?}", r)).collect(); |
63 | | - let negatives: Vec<String> = negatives.into_iter().map(|r| format!("{:?}", r)).collect(); |
64 | | - let variant_dict = PyDict::new(py); |
65 | | - variant_dict.set_item("type", "parsing").unwrap(); |
66 | | - variant_dict.set_item("positives", positives).unwrap(); |
67 | | - variant_dict.set_item("negatives", negatives).unwrap(); |
68 | | - variant_dict.into() |
69 | | - }, |
70 | | - ErrorVariant::CustomError { message } => { |
71 | | - let variant_dict = PyDict::new(py); |
72 | | - variant_dict.set_item("type", "custom").unwrap(); |
73 | | - variant_dict.set_item("message", message).unwrap(); |
74 | | - variant_dict.into() |
75 | | - }, |
76 | | - }; |
77 | | - dict.set_item("variant", variant).unwrap(); |
78 | | - |
79 | | - dict.into() |
80 | | -} |
81 | | - |
82 | | -#[pyfunction] |
83 | | -fn parse_expression(py: Python, path: &str) -> PyResult<String> { |
84 | | - match crate::parser::parse_expression(path) { |
85 | | - Ok(p) => { Ok(format!("{:?}", p)) } |
86 | | - Err(e) => { Err(parsing_error_to_py_err(py, e.into())) } |
87 | | - } |
88 | | -} |
89 | | - |
90 | | -#[pyfunction] |
91 | | -fn parse_file(py: Python, path: &str) -> PyResult<Py<PyAny>> { |
92 | | - match crate::parser::parse_file(path) { |
93 | | - Ok(result) => { |
94 | | - Ok(program_result_to_py_object(py, &result)) |
95 | | - } |
96 | | - Err(e) => Err(parsing_error_to_py_err(py, e)) |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -#[pymodule] |
101 | | -fn unrealscriptplus(m: &Bound<'_, PyModule>) -> PyResult<()> { |
102 | | - m.add_function(wrap_pyfunction!(parse_expression, m)?)?; |
103 | | - m.add_function(wrap_pyfunction!(parse_file, m)?)?; |
104 | | - m.add("ScriptParseError", m.py().get_type::<ScriptParseError>())?; |
105 | | - Ok(()) |
106 | | -} |
| 5 | +// Re-export main parsing functions and types for library consumers |
| 6 | +pub use parser::{parse_expression, parse_with_rule, parse_program, ParsingError, ProgramResult, Rule}; |
0 commit comments