use alloc::fmt;
use core::fmt::Display;
use rustpython_compiler_core::SourceLocation;
use thiserror::Error;
// pub type CodegenError = rustpython_parser_core::source_code::LocatedError;
#[derive(Error, Debug)]
pub struct CodegenError {
pub location: Option,
pub end_location: Option,
#[source]
pub error: CodegenErrorType,
pub source_path: String,
}
impl fmt::Display for CodegenError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::StackUnderflow => write!(f, "Invalid CFG, stack underflow"),
Self::InconsistentStackDepth => write!(f, "Invalid CFG, inconsistent stackdepth"),
Self::InvalidStackEffect => write!(f, "Invalid stack effect"),
Self::MalformedControlFlowGraph => write!(f, "malformed control flow graph."),
Self::MissingSymbol(s) => write!(
f,
"The symbol '{s}' must be present in the symbol table, even when it is undefined in python."
),
Self::ConstIndexOutOfRange { index, len } => {
write!(
f,
"LOAD_CONST index {index} is out of range for consts (len={len})"
)
}
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum CodegenErrorType {
/// Invalid assignment, cannot store value in target.
Assign(&'static str),
/// Invalid delete
Delete(&'static str),
SyntaxError(String),
/// Multiple `*` detected
MultipleStarArgs,
MultipleStarredExpressionsInSequencePattern,
MultipleStarredNamesInSequencePattern,
/// Misplaced `*` expression
InvalidStarExpr,
/// Break statement outside of loop.
InvalidBreak,
/// Continue statement outside of loop.
InvalidContinue,
InvalidReturn,
InvalidYield,
InvalidYieldFrom,
InvalidAwait,
InvalidAsyncFor,
InvalidAsyncWith,
InvalidAsyncComprehension,
AsyncYieldFrom,
AsyncReturnValue,
InvalidFuturePlacement,
InvalidFutureFeature(String),
InvalidFutureBraces,
RecursionError,
TooManyStarUnpack,
TooManyExpressionsInStarUnpackingSequencePattern,
EmptyWithItems,
EmptyWithBody,
ForbiddenName,
DuplicateStore(String),
UnreachableWildcardPattern,
UnreachableNameCapturePattern(String),
RepeatedAttributePattern(String),
ConflictingNameBindPattern,
/// break/continue/return inside except* block
BreakContinueReturnInExceptStar,
}
impl core::error::Error for CodegenErrorType {}
impl fmt::Display for CodegenErrorType {
fn fmt(&self, f: &mut fmt::Formatter