Skip to content

Commit d876673

Browse files
committed
Move out CompileError to core as generic form
1 parent 9aacd14 commit d876673

6 files changed

Lines changed: 54 additions & 56 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ num-bigint = { version = "0.4.3", features = ["serde"] }
1717
num-complex = { version = "0.4.0", features = ["serde"] }
1818
serde = { version = "1.0.136", features = ["derive"] }
1919
static_assertions = "1.1.0"
20+
thiserror = "1.0"

compiler/core/src/error.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,32 @@ impl<T> BaseError<T> {
5858
BaseError::from(self)
5959
}
6060
}
61+
62+
#[derive(Debug, thiserror::Error)]
63+
pub struct CompileError<T> {
64+
pub body: BaseError<T>,
65+
pub statement: Option<String>,
66+
}
67+
68+
impl<T> std::ops::Deref for CompileError<T> {
69+
type Target = BaseError<T>;
70+
fn deref(&self) -> &Self::Target {
71+
&self.body
72+
}
73+
}
74+
75+
impl<T> std::fmt::Display for CompileError<T>
76+
where
77+
T: std::fmt::Display,
78+
{
79+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
80+
let loc = self.location;
81+
if let Some(ref stmt) = self.statement {
82+
// visualize the error when location and statement are provided
83+
loc.fmt_with(f, &self.error)?;
84+
write!(f, "\n{stmt}{arrow:>pad$}", pad = loc.column(), arrow = "^")
85+
} else {
86+
loc.fmt_with(f, &self.error)
87+
}
88+
}
89+
}

compiler/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ mod location;
77
mod mode;
88

99
pub use bytecode::*;
10-
pub use error::BaseError;
10+
pub use error::{BaseError, CompileError};
1111
pub use location::Location;
1212
pub use mode::Mode;

compiler/src/lib.rs

Lines changed: 21 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use rustpython_codegen::{compile, symboltable};
2-
use rustpython_compiler_core::{BaseError, CodeObject};
2+
use rustpython_compiler_core::CodeObject;
33
use rustpython_parser::{
44
ast::{fold::Fold, ConstantOptimizer, Location},
55
error::ParseErrorType,
66
parser,
77
};
8-
use std::fmt;
98

109
pub use rustpython_codegen::compile::CompileOpts;
11-
pub use rustpython_compiler_core::Mode;
10+
pub use rustpython_compiler_core::{BaseError as CompileErrorBody, Mode};
1211

1312
#[derive(Debug, thiserror::Error)]
1413
pub enum CompileErrorType {
@@ -18,56 +17,25 @@ pub enum CompileErrorType {
1817
Parse(#[from] rustpython_parser::error::ParseErrorType),
1918
}
2019

21-
pub type CompileErrorBody = BaseError<CompileErrorType>;
20+
pub type CompileError = rustpython_compiler_core::CompileError<CompileErrorType>;
2221

23-
#[derive(Debug, thiserror::Error)]
24-
pub struct CompileError {
25-
pub body: CompileErrorBody,
26-
pub statement: Option<String>,
27-
}
28-
29-
impl std::ops::Deref for CompileError {
30-
type Target = CompileErrorBody;
31-
fn deref(&self) -> &Self::Target {
32-
&self.body
33-
}
34-
}
35-
36-
impl fmt::Display for CompileError {
37-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38-
let loc = self.location;
39-
if let Some(ref stmt) = self.statement {
40-
// visualize the error when location and statement are provided
41-
loc.fmt_with(f, &self.error)?;
42-
write!(f, "\n{stmt}{arrow:>pad$}", pad = loc.column(), arrow = "^")
43-
} else {
44-
loc.fmt_with(f, &self.error)
45-
}
22+
fn error_from_codegen(
23+
error: rustpython_codegen::error::CodegenError,
24+
source: &str,
25+
) -> CompileError {
26+
let statement = get_statement(source, error.location);
27+
CompileError {
28+
body: error.into(),
29+
statement,
4630
}
4731
}
4832

49-
impl CompileError {
50-
fn from_codegen(error: rustpython_codegen::error::CodegenError, source: &str) -> Self {
51-
let statement = get_statement(source, error.location);
52-
Self {
53-
body: error.into(),
54-
statement,
55-
}
56-
}
57-
fn from_parse(error: rustpython_parser::error::ParseError, source: &str) -> Self {
58-
let error: rustpython_compiler_core::BaseError<ParseErrorType> = error.into();
59-
let statement = get_statement(source, error.location);
60-
Self {
61-
body: error.into(),
62-
statement,
63-
}
64-
}
65-
fn from_symtable(
66-
error: symboltable::SymbolTableError,
67-
source: &str,
68-
source_path: String,
69-
) -> Self {
70-
Self::from_codegen(error.into_codegen_error(source_path), source)
33+
fn error_from_parse(error: rustpython_parser::error::ParseError, source: &str) -> CompileError {
34+
let error: CompileErrorBody<ParseErrorType> = error.into();
35+
let statement = get_statement(source, error.location);
36+
CompileError {
37+
body: error.into(),
38+
statement,
7139
}
7240
}
7341

@@ -85,23 +53,22 @@ pub fn compile(
8553
};
8654
let mut ast = match parser::parse(source, parser_mode, &source_path) {
8755
Ok(x) => x,
88-
Err(e) => return Err(CompileError::from_parse(e, source)),
56+
Err(e) => return Err(error_from_parse(e, source)),
8957
};
9058
if opts.optimize > 0 {
9159
ast = ConstantOptimizer::new()
9260
.fold_mod(ast)
9361
.unwrap_or_else(|e| match e {});
9462
}
95-
compile::compile_top(&ast, source_path, mode, opts)
96-
.map_err(|e| CompileError::from_codegen(e, source))
63+
compile::compile_top(&ast, source_path, mode, opts).map_err(|e| error_from_codegen(e, source))
9764
}
9865

9966
pub fn compile_symtable(
10067
source: &str,
10168
mode: compile::Mode,
10269
source_path: &str,
10370
) -> Result<symboltable::SymbolTable, CompileError> {
104-
let parse_err = |e| CompileError::from_parse(e, source);
71+
let parse_err = |e| error_from_parse(e, source);
10572
let res = match mode {
10673
compile::Mode::Exec | compile::Mode::Single | compile::Mode::BlockExpr => {
10774
let ast = parser::parse_program(source, source_path).map_err(parse_err)?;
@@ -112,7 +79,7 @@ pub fn compile_symtable(
11279
symboltable::SymbolTable::scan_expr(&expr)
11380
}
11481
};
115-
res.map_err(|e| CompileError::from_symtable(e, source, source_path.to_owned()))
82+
res.map_err(|e| error_from_codegen(e.into_codegen_error(source_path.to_owned()), source))
11683
}
11784

11885
fn get_statement(source: &str, loc: Location) -> Option<String> {

src/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
mod helper;
22

33
use rustpython_parser::error::{LexicalErrorType, ParseErrorType};
4-
use rustpython_vm::readline::{Readline, ReadlineResult};
54
use rustpython_vm::{
65
builtins::PyBaseExceptionRef,
76
compile::{self, CompileError, CompileErrorBody, CompileErrorType},
7+
readline::{Readline, ReadlineResult},
88
scope::Scope,
99
AsObject, PyResult, VirtualMachine,
1010
};

0 commit comments

Comments
 (0)