1- use rustpython_parser:: error:: { LexicalErrorType , ParseError , ParseErrorType } ;
2- use rustpython_parser:: location:: Location ;
3- use rustpython_parser:: token:: Tok ;
1+ use rustpython_ast:: Location ;
42
53use std:: error:: Error ;
64use std:: fmt;
75
86#[ derive( Debug ) ]
97pub struct CompileError {
10- pub statement : Option < String > ,
118 pub error : CompileErrorType ,
129 pub location : Location ,
1310 pub source_path : String ,
1411}
1512
16- impl CompileError {
17- pub fn from_parse_error ( parse_error : ParseError , source_path : String ) -> Self {
18- Self {
19- statement : None ,
20- error : CompileErrorType :: Parse ( parse_error. error ) ,
21- location : parse_error. location ,
22- source_path,
23- }
24- }
25-
26- pub fn update_statement_info ( & mut self , statement : String ) {
27- self . statement = Some ( statement) ;
28- }
29- }
30-
3113#[ derive( Debug ) ]
14+ #[ non_exhaustive]
3215pub enum CompileErrorType {
3316 /// Invalid assignment, cannot store value in target.
3417 Assign ( & ' static str ) ,
3518 /// Invalid delete
3619 Delete ( & ' static str ) ,
3720 /// Expected an expression got a statement
3821 ExpectExpr ,
39- /// Parser error
40- Parse ( ParseErrorType ) ,
4122 SyntaxError ( String ) ,
4223 /// Multiple `*` detected
4324 MultipleStarArgs ,
@@ -57,74 +38,43 @@ pub enum CompileErrorType {
5738 InvalidFutureFeature ( String ) ,
5839}
5940
60- impl CompileError {
61- pub fn is_indentation_error ( & self ) -> bool {
62- if let CompileErrorType :: Parse ( parse) = & self . error {
63- match parse {
64- ParseErrorType :: Lexical ( LexicalErrorType :: IndentationError ) => true ,
65- ParseErrorType :: UnrecognizedToken ( token, expected) => {
66- * token == Tok :: Indent || expected. clone ( ) == Some ( "Indent" . to_owned ( ) )
67- }
68- _ => false ,
69- }
70- } else {
71- false
72- }
73- }
74-
75- pub fn is_tab_error ( & self ) -> bool {
76- if let CompileErrorType :: Parse ( parse) = & self . error {
77- if let ParseErrorType :: Lexical ( lex) = parse {
78- if let LexicalErrorType :: TabError = lex {
79- return true ;
80- }
81- }
82- }
83- false
84- }
85- }
86-
87- impl fmt:: Display for CompileError {
41+ impl fmt:: Display for CompileErrorType {
8842 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
89- let error_desc = match & self . error {
90- CompileErrorType :: Assign ( target) => format ! ( "can't assign to {}" , target) ,
91- CompileErrorType :: Delete ( target) => format ! ( "can't delete {}" , target) ,
92- CompileErrorType :: ExpectExpr => "Expecting expression, got statement" . to_owned ( ) ,
93- CompileErrorType :: Parse ( err) => err. to_string ( ) ,
94- CompileErrorType :: SyntaxError ( err) => err. to_string ( ) ,
43+ match self {
44+ CompileErrorType :: Assign ( target) => write ! ( f, "can't assign to {}" , target) ,
45+ CompileErrorType :: Delete ( target) => write ! ( f, "can't delete {}" , target) ,
46+ CompileErrorType :: ExpectExpr => write ! ( f, "Expecting expression, got statement" ) ,
47+ CompileErrorType :: SyntaxError ( err) => write ! ( f, "{}" , err. as_str( ) ) ,
9548 CompileErrorType :: MultipleStarArgs => {
96- "two starred expressions in assignment" . to_owned ( )
49+ write ! ( f , "two starred expressions in assignment" )
9750 }
98- CompileErrorType :: InvalidStarExpr => "can't use starred expression here" . to_owned ( ) ,
99- CompileErrorType :: InvalidBreak => "'break' outside loop" . to_owned ( ) ,
100- CompileErrorType :: InvalidContinue => "'continue' outside loop" . to_owned ( ) ,
101- CompileErrorType :: InvalidReturn => "'return' outside function" . to_owned ( ) ,
102- CompileErrorType :: InvalidYield => "'yield' outside function" . to_owned ( ) ,
103- CompileErrorType :: InvalidYieldFrom => "'yield from' outside function" . to_owned ( ) ,
104- CompileErrorType :: InvalidAwait => "'await' outside async function" . to_owned ( ) ,
105- CompileErrorType :: AsyncYieldFrom => "'yield from' inside async function" . to_owned ( ) ,
51+ CompileErrorType :: InvalidStarExpr => write ! ( f , "can't use starred expression here" ) ,
52+ CompileErrorType :: InvalidBreak => write ! ( f , "'break' outside loop" ) ,
53+ CompileErrorType :: InvalidContinue => write ! ( f , "'continue' outside loop" ) ,
54+ CompileErrorType :: InvalidReturn => write ! ( f , "'return' outside function" ) ,
55+ CompileErrorType :: InvalidYield => write ! ( f , "'yield' outside function" ) ,
56+ CompileErrorType :: InvalidYieldFrom => write ! ( f , "'yield from' outside function" ) ,
57+ CompileErrorType :: InvalidAwait => write ! ( f , "'await' outside async function" ) ,
58+ CompileErrorType :: AsyncYieldFrom => write ! ( f , "'yield from' inside async function" ) ,
10659 CompileErrorType :: AsyncReturnValue => {
107- "'return' with value inside async generator" . to_owned ( )
108- }
109- CompileErrorType :: InvalidFuturePlacement => {
110- "from __future__ imports must occur at the beginning of the file" . to_owned ( )
60+ write ! ( f, "'return' with value inside async generator" )
11161 }
62+ CompileErrorType :: InvalidFuturePlacement => write ! (
63+ f,
64+ "from __future__ imports must occur at the beginning of the file"
65+ ) ,
11266 CompileErrorType :: InvalidFutureFeature ( feat) => {
113- format ! ( "future feature {} is not defined" , feat)
114- }
115- } ;
116-
117- if let Some ( statement) = & self . statement {
118- if self . location . column ( ) > 0 {
119- if let Some ( line) = statement. lines ( ) . nth ( self . location . row ( ) - 1 ) {
120- // visualize the error, when location and statement are provided
121- return write ! ( f, "{}" , self . location. visualize( line, & error_desc) ) ;
122- }
67+ write ! ( f, "future feature {} is not defined" , feat)
12368 }
12469 }
70+ }
71+ }
12572
126- // print line number
127- write ! ( f, "{} at {}" , error_desc, self . location)
73+ impl Error for CompileErrorType { }
74+
75+ impl fmt:: Display for CompileError {
76+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
77+ write ! ( f, "{} at {}" , self . error, self . location)
12878 }
12979}
13080
0 commit comments