1
1
// SPDX-License-Identifier: Apache-2.0
2
2
3
+ //! Solidity parser diagnostics.
4
+
3
5
use crate :: pt;
4
6
use crate :: pt:: Loc ;
7
+ use std:: fmt;
5
8
9
+ /// The level of a diagnostic.
6
10
#[ derive( Clone , Debug , Hash , PartialOrd , Ord , PartialEq , Eq ) ]
7
11
pub enum Level {
12
+ /// Debug diagnostic level.
8
13
Debug ,
14
+ /// Info diagnostic level.
9
15
Info ,
16
+ /// Warning diagnostic level.
10
17
Warning ,
18
+ /// Error diagnostic level.
11
19
Error ,
12
20
}
13
21
22
+ impl fmt:: Display for Level {
23
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
24
+ f. write_str ( self . as_str ( ) )
25
+ }
26
+ }
27
+
14
28
impl Level {
15
- pub fn to_string ( & self ) -> & ' static str {
29
+ /// Returns this type as a static string slice.
30
+ pub fn as_str ( & self ) -> & ' static str {
16
31
match self {
17
32
Level :: Debug => "debug" ,
18
33
Level :: Info => "info" ,
@@ -22,33 +37,51 @@ impl Level {
22
37
}
23
38
}
24
39
25
- #[ derive( Clone , Debug , Hash , PartialOrd , Ord , PartialEq , Eq ) ]
40
+ /// The type of a diagnostic.
41
+ #[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
26
42
pub enum ErrorType {
43
+ /// No specific error type.
27
44
None ,
45
+ /// Parser error.
28
46
ParserError ,
47
+ /// Syntax error.
29
48
SyntaxError ,
49
+ /// Declaration error.
30
50
DeclarationError ,
51
+ /// Cast error.
31
52
CastError ,
53
+ /// Type error.
32
54
TypeError ,
55
+ /// Warning.
33
56
Warning ,
34
57
}
35
58
36
- #[ derive( Clone , Debug , Hash , PartialOrd , Ord , PartialEq , Eq ) ]
59
+ /// A diagnostic note.
60
+ #[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
37
61
pub struct Note {
62
+ /// The code location of the note.
38
63
pub loc : pt:: Loc ,
64
+ /// The message of the note.
39
65
pub message : String ,
40
66
}
41
67
42
- #[ derive( Clone , Debug , Hash , PartialOrd , Ord , PartialEq , Eq ) ]
68
+ /// A Solidity diagnostic.
69
+ #[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
43
70
pub struct Diagnostic {
71
+ /// The code location of the diagnostic.
44
72
pub loc : pt:: Loc ,
73
+ /// The level of the diagnostic.
45
74
pub level : Level ,
75
+ /// The type of diagnostic.
46
76
pub ty : ErrorType ,
77
+ /// The message of the diagnostic.
47
78
pub message : String ,
79
+ /// Extra notes about the diagnostic.
48
80
pub notes : Vec < Note > ,
49
81
}
50
82
51
83
impl Diagnostic {
84
+ /// Instantiate a new Diagnostic with the given location and message at the debug level.
52
85
pub fn debug ( loc : Loc , message : String ) -> Self {
53
86
Diagnostic {
54
87
level : Level :: Debug ,
@@ -59,6 +92,7 @@ impl Diagnostic {
59
92
}
60
93
}
61
94
95
+ /// Instantiate a new Diagnostic with the given location and message at the info level.
62
96
pub fn info ( loc : Loc , message : String ) -> Self {
63
97
Diagnostic {
64
98
level : Level :: Info ,
@@ -69,6 +103,7 @@ impl Diagnostic {
69
103
}
70
104
}
71
105
106
+ /// Instantiate a new parser error Diagnostic.
72
107
pub fn parser_error ( loc : Loc , message : String ) -> Self {
73
108
Diagnostic {
74
109
level : Level :: Error ,
@@ -79,6 +114,7 @@ impl Diagnostic {
79
114
}
80
115
}
81
116
117
+ /// Instantiate a new syntax error Diagnostic.
82
118
pub fn error ( loc : Loc , message : String ) -> Self {
83
119
Diagnostic {
84
120
level : Level :: Error ,
@@ -89,6 +125,7 @@ impl Diagnostic {
89
125
}
90
126
}
91
127
128
+ /// Instantiate a new declaration error Diagnostic.
92
129
pub fn decl_error ( loc : Loc , message : String ) -> Self {
93
130
Diagnostic {
94
131
level : Level :: Error ,
@@ -99,6 +136,7 @@ impl Diagnostic {
99
136
}
100
137
}
101
138
139
+ /// Instantiate a new cast error error Diagnostic.
102
140
pub fn cast_error ( loc : Loc , message : String ) -> Self {
103
141
Diagnostic {
104
142
level : Level :: Error ,
@@ -109,6 +147,7 @@ impl Diagnostic {
109
147
}
110
148
}
111
149
150
+ /// Instantiate a new cast error error Diagnostic, with a note.
112
151
pub fn cast_error_with_note ( loc : Loc , message : String , note_loc : Loc , note : String ) -> Self {
113
152
Diagnostic {
114
153
level : Level :: Error ,
@@ -122,6 +161,7 @@ impl Diagnostic {
122
161
}
123
162
}
124
163
164
+ /// Instantiate a new type error error Diagnostic.
125
165
pub fn type_error ( loc : Loc , message : String ) -> Self {
126
166
Diagnostic {
127
167
level : Level :: Error ,
@@ -132,6 +172,7 @@ impl Diagnostic {
132
172
}
133
173
}
134
174
175
+ /// Instantiate a new cast error Diagnostic at the warning level.
135
176
pub fn cast_warning ( loc : Loc , message : String ) -> Self {
136
177
Diagnostic {
137
178
level : Level :: Warning ,
@@ -142,6 +183,7 @@ impl Diagnostic {
142
183
}
143
184
}
144
185
186
+ /// Instantiate a new warning Diagnostic.
145
187
pub fn warning ( loc : Loc , message : String ) -> Self {
146
188
Diagnostic {
147
189
level : Level :: Warning ,
@@ -152,6 +194,7 @@ impl Diagnostic {
152
194
}
153
195
}
154
196
197
+ /// Instantiate a new warning Diagnostic, with a note.
155
198
pub fn warning_with_note ( loc : Loc , message : String , note_loc : Loc , note : String ) -> Self {
156
199
Diagnostic {
157
200
level : Level :: Warning ,
@@ -165,6 +208,7 @@ impl Diagnostic {
165
208
}
166
209
}
167
210
211
+ /// Instantiate a new warning Diagnostic, with multiple notes.
168
212
pub fn warning_with_notes ( loc : Loc , message : String , notes : Vec < Note > ) -> Self {
169
213
Diagnostic {
170
214
level : Level :: Warning ,
@@ -175,6 +219,7 @@ impl Diagnostic {
175
219
}
176
220
}
177
221
222
+ /// Instantiate a new error Diagnostic, with a note.
178
223
pub fn error_with_note ( loc : Loc , message : String , note_loc : Loc , note : String ) -> Self {
179
224
Diagnostic {
180
225
level : Level :: Error ,
@@ -188,6 +233,7 @@ impl Diagnostic {
188
233
}
189
234
}
190
235
236
+ /// Instantiate a new error Diagnostic, with multiple notes.
191
237
pub fn error_with_notes ( loc : Loc , message : String , notes : Vec < Note > ) -> Self {
192
238
Diagnostic {
193
239
level : Level :: Error ,
0 commit comments