@@ -7,13 +7,21 @@ use std::fmt;
77
88#[ derive( Debug ) ]
99pub struct CompileError {
10+ pub statement : Option < String > ,
1011 pub error : CompileErrorType ,
1112 pub location : Location ,
1213}
1314
15+ impl CompileError {
16+ pub fn update_statement_info ( & mut self , statement : String ) {
17+ self . statement = Some ( statement) ;
18+ }
19+ }
20+
1421impl From < ParseError > for CompileError {
1522 fn from ( error : ParseError ) -> Self {
1623 CompileError {
24+ statement : None ,
1725 error : CompileErrorType :: Parse ( error. error ) ,
1826 location : error. location ,
1927 }
@@ -70,21 +78,31 @@ impl CompileError {
7078
7179impl fmt:: Display for CompileError {
7280 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
73- match & self . error {
74- CompileErrorType :: Assign ( target) => write ! ( f , "can't assign to {}" , target) ,
75- CompileErrorType :: Delete ( target) => write ! ( f , "can't delete {}" , target) ,
76- CompileErrorType :: ExpectExpr => write ! ( f , "Expecting expression, got statement" ) ,
77- CompileErrorType :: Parse ( err) => write ! ( f , "{}" , err) ,
78- CompileErrorType :: SyntaxError ( err) => write ! ( f , "{}" , err) ,
79- CompileErrorType :: StarArgs => write ! ( f , "Two starred expressions in assignment" ) ,
80- CompileErrorType :: InvalidBreak => write ! ( f , "'break' outside loop" ) ,
81- CompileErrorType :: InvalidContinue => write ! ( f , "'continue' outside loop" ) ,
82- CompileErrorType :: InvalidReturn => write ! ( f , "'return' outside function" ) ,
83- CompileErrorType :: InvalidYield => write ! ( f , "'yield' outside function" ) ,
84- } ? ;
81+ let error_desc = match & self . error {
82+ CompileErrorType :: Assign ( target) => format ! ( "can't assign to {}" , target) ,
83+ CompileErrorType :: Delete ( target) => format ! ( "can't delete {}" , target) ,
84+ CompileErrorType :: ExpectExpr => "Expecting expression, got statement" . to_string ( ) ,
85+ CompileErrorType :: Parse ( err) => err. to_string ( ) ,
86+ CompileErrorType :: SyntaxError ( err) => err. to_string ( ) ,
87+ CompileErrorType :: StarArgs => "Two starred expressions in assignment" . to_string ( ) ,
88+ CompileErrorType :: InvalidBreak => "'break' outside loop" . to_string ( ) ,
89+ CompileErrorType :: InvalidContinue => "'continue' outside loop" . to_string ( ) ,
90+ CompileErrorType :: InvalidReturn => "'return' outside function" . to_string ( ) ,
91+ CompileErrorType :: InvalidYield => "'yield' outside function" . to_string ( ) ,
92+ } ;
8593
86- // Print line number:
87- write ! ( f, " at {}" , self . location)
94+ if self . statement . is_some ( ) && self . location . column ( ) > 0 {
95+ // visualize the error, when location and statement are provided
96+ write ! (
97+ f,
98+ "\n {}\n {}" ,
99+ self . statement. clone( ) . unwrap( ) ,
100+ self . location. visualize( & error_desc)
101+ )
102+ } else {
103+ // print line number
104+ write ! ( f, "{} at {}" , error_desc, self . location)
105+ }
88106 }
89107}
90108
0 commit comments