11use rustpython_codegen:: { compile, symboltable} ;
2- use rustpython_compiler_core:: { BaseError , CodeObject } ;
2+ use rustpython_compiler_core:: CodeObject ;
33use rustpython_parser:: {
44 ast:: { fold:: Fold , ConstantOptimizer , Location } ,
55 error:: ParseErrorType ,
66 parser,
77} ;
8- use std:: fmt;
98
109pub 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 ) ]
1413pub 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
9966pub 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
11885fn get_statement ( source : & str , loc : Location ) -> Option < String > {
0 commit comments