1+ use core:: fmt;
2+
13use crate :: errors:: api:: Location ;
24
5+ /// Struct to store the error information
6+ ///
7+ /// # Creation
8+ ///
9+ /// To create an error, you need to have the [`Location`] of the error. Then,
10+ /// use the methods on that location, for example:
11+ ///
12+ /// ```ignore
13+ /// let location = Location::from("filename.c");
14+ /// let error = location.to_error("Something bad happened here.".to_owned());
15+ /// ```
16+ ///
17+ /// To see the others methods to create errors see [`Location`].
18+ ///
19+ /// # Usage
20+ ///
21+ /// The [`CompileError`] is mainly used as part of a
22+ /// [`Res`](super::result::Res).
323#[ derive( Debug ) ]
424pub struct CompileError {
25+ /// Severity of the error
526 err_lvl : ErrorLevel ,
27+ /// Length of the erroneous token or expression
628 length : usize ,
29+ /// Location of the error in the C source file
730 location : Location ,
31+ /// Error message to be displayed to the user
832 message : String ,
933}
1034
1135impl CompileError {
12- pub fn get ( self ) -> ( Location , String , & ' static str , usize ) {
36+ /// Returns the owned data of a `CompileError`.
37+ pub ( super ) fn into_values ( self ) -> ( Location , String , String , usize ) {
1338 (
1439 self . location ,
1540 self . message ,
16- self . err_lvl . repr ( ) ,
41+ self . err_lvl . to_string ( ) ,
1742 self . length ,
1843 )
1944 }
2045
21- pub fn is_error ( & self ) -> bool {
46+ /// Checks if the error is of severity [`ErrorLevel::Error`].
47+ pub ( crate ) fn is_error ( & self ) -> bool {
2248 self . err_lvl == ErrorLevel :: Error
2349 }
2450
25- pub fn specify_length ( & mut self , length : usize ) {
51+ // Replaces length of the token or expression concerned by the `CompileError`.
52+ pub ( crate ) fn specify_length ( & mut self , length : usize ) {
2653 self . length = length;
2754 }
2855}
2956
3057impl From < ( Location , String , ErrorLevel , usize ) > for CompileError {
58+ #[ inline]
3159 fn from ( ( location, message, err_lvl, length) : ( Location , String , ErrorLevel , usize ) ) -> Self {
3260 Self {
3361 err_lvl,
@@ -39,6 +67,7 @@ impl From<(Location, String, ErrorLevel, usize)> for CompileError {
3967}
4068
4169impl From < ( Location , String , ErrorLevel ) > for CompileError {
70+ #[ inline]
4271 fn from ( ( location, message, err_lvl) : ( Location , String , ErrorLevel ) ) -> Self {
4372 Self {
4473 message,
@@ -49,19 +78,44 @@ impl From<(Location, String, ErrorLevel)> for CompileError {
4978 }
5079}
5180
81+ /// Different levels of errors
5282#[ derive( Debug , PartialEq , Eq ) ]
5383pub enum ErrorLevel {
84+ /// The compiler stops compiling the current block and fails.
85+ ///
86+ /// The level is only `Error` when the compiler can't fix the error and
87+ /// panics.
88+ ///
89+ /// The compiler will continue if it manages to do so safely on parts that
90+ /// are independent from the original location of the error. Not all of the
91+ /// independent parts are compiled though.
5492 Error ,
93+ /// Found a bad practice.
94+ ///
95+ /// # Examples
96+ ///
97+ /// - a leading space after `\` at end of line
5598 Suggestion ,
99+ /// The compiler manages to fix the code and continue.
100+ ///
101+ /// A warning is displayed to the user, but the compiler continues as
102+ /// nothing happened.
103+ ///
104+ /// # Examples
105+ ///
106+ /// - an overflow on a integer constant: the value is crapped and the
107+ /// compiler continues
108+ /// - deprecated behaviours (e.g. using `_Bool` instead of `bool` in C23).
56109 Warning ,
57110}
58111
59- impl ErrorLevel {
60- const fn repr ( & self ) -> & ' static str {
112+ #[ expect( clippy:: min_ident_chars) ]
113+ impl fmt:: Display for ErrorLevel {
114+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
61115 match self {
62- Self :: Warning => "warning" ,
63- Self :: Error => "error" ,
64- Self :: Suggestion => "suggestion" ,
116+ Self :: Error => "error" . fmt ( f ) ,
117+ Self :: Suggestion => "suggestion" . fmt ( f ) ,
118+ Self :: Warning => "warning" . fmt ( f ) ,
65119 }
66120 }
67121}
0 commit comments