Skip to main content

rustpython_vm/
compiler.rs

1#[cfg(feature = "codegen")]
2pub use rustpython_codegen::CompileOpts;
3
4#[cfg(feature = "compiler")]
5pub use rustpython_compiler::*;
6
7#[cfg(not(feature = "compiler"))]
8pub use rustpython_compiler_core::Mode;
9
10#[cfg(not(feature = "compiler"))]
11pub use rustpython_compiler_core as core;
12
13#[cfg(not(feature = "compiler"))]
14pub use ruff_python_parser as parser;
15
16#[cfg(not(feature = "compiler"))]
17mod error {
18    #[cfg(all(feature = "parser", feature = "codegen"))]
19    panic!("Use --features=compiler to enable both parser and codegen");
20
21    #[derive(Debug, thiserror::Error)]
22    pub enum CompileErrorType {
23        #[cfg(feature = "codegen")]
24        #[error(transparent)]
25        Codegen(#[from] super::codegen::error::CodegenErrorType),
26        #[cfg(feature = "parser")]
27        #[error(transparent)]
28        Parse(#[from] super::parser::ParseErrorType),
29    }
30
31    #[derive(Debug, thiserror::Error)]
32    pub enum CompileError {
33        #[cfg(feature = "codegen")]
34        #[error(transparent)]
35        Codegen(#[from] super::codegen::error::CodegenError),
36        #[cfg(feature = "parser")]
37        #[error(transparent)]
38        Parse(#[from] super::parser::ParseError),
39    }
40}
41#[cfg(not(feature = "compiler"))]
42pub use error::{CompileError, CompileErrorType};
43
44#[cfg(any(feature = "parser", feature = "codegen"))]
45impl crate::convert::ToPyException for (CompileError, Option<&str>) {
46    fn to_pyexception(&self, vm: &crate::VirtualMachine) -> crate::builtins::PyBaseExceptionRef {
47        vm.new_syntax_error(&self.0, self.1)
48    }
49}
50
51#[cfg(any(feature = "parser", feature = "codegen"))]
52impl crate::convert::ToPyException for (CompileError, Option<&str>, bool) {
53    fn to_pyexception(&self, vm: &crate::VirtualMachine) -> crate::builtins::PyBaseExceptionRef {
54        vm.new_syntax_error_maybe_incomplete(&self.0, self.1, self.2)
55    }
56}