use std::fmt::Debug;
pub mod python_options;
pub use python_options::*;
pub use to_tokenstream::CodeGen;
#[derive(Clone, Debug)]
pub enum CodeGenContext {
Module(String),
Class(String),
Function,
Async(Box<CodeGenContext>),
Loop {
has_else: bool,
parent: Box<CodeGenContext>,
},
TryBlock {
parent: Box<CodeGenContext>,
},
ExceptHandler {
parent: Box<CodeGenContext>,
},
}
impl CodeGenContext {
pub fn in_try_block(&self) -> bool {
match self {
CodeGenContext::TryBlock { .. } => true,
CodeGenContext::ExceptHandler { parent } | CodeGenContext::Loop { parent, .. } => parent.in_try_block(),
CodeGenContext::Async(inner) => inner.in_try_block(),
_ => false,
}
}
pub fn break_crosses_try_closure(&self) -> bool {
match self {
CodeGenContext::Loop { .. } => false,
CodeGenContext::TryBlock { .. } => true,
CodeGenContext::ExceptHandler { parent } => parent.break_crosses_try_closure(),
CodeGenContext::Async(inner) => inner.break_crosses_try_closure(),
_ => false,
}
}
pub fn break_target_has_else(&self) -> bool {
match self {
CodeGenContext::Loop { has_else, .. } => *has_else,
CodeGenContext::TryBlock { parent } | CodeGenContext::ExceptHandler { parent } => parent.break_target_has_else(),
CodeGenContext::Async(inner) => inner.break_target_has_else(),
_ => false,
}
}
pub fn in_except_handler(&self) -> bool {
match self {
CodeGenContext::ExceptHandler { .. } => true,
CodeGenContext::TryBlock { parent } | CodeGenContext::Loop { parent, .. } => parent.in_except_handler(),
CodeGenContext::Async(inner) => inner.in_except_handler(),
_ => false,
}
}
pub fn strip_exception_scopes(self) -> CodeGenContext {
match self {
CodeGenContext::TryBlock { parent } | CodeGenContext::ExceptHandler { parent } => parent.strip_exception_scopes(),
other => other,
}
}
pub fn enclosing_class_name(&self) -> Option<&str> {
match self {
CodeGenContext::Class(name) => Some(name),
CodeGenContext::Async(inner) => inner.enclosing_class_name(),
CodeGenContext::Loop { parent, .. } | CodeGenContext::TryBlock { parent } | CodeGenContext::ExceptHandler { parent } => {
parent.enclosing_class_name()
}
_ => None,
}
}
}