OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library front_end.src.scanner.errors; | |
Brian Wilkerson
2016/11/08 20:53:23
Is there any value to having library directives (i
Paul Berry
2016/11/08 21:47:06
IMHO, not really. I've removed them.
| |
6 | |
7 import 'package:analyzer/error/error.dart' | |
kevmoo
2016/11/08 22:26:03
We try to avoid circular dependencies between pack
Paul Berry
2016/11/08 22:28:14
Yes. This will be removed when the "@deprecated"
| |
8 show errorCodeValues, errorCodeByUniqueName; | |
9 | |
10 /** | |
11 * An error code associated with an [AnalysisError]. | |
12 * | |
13 * Generally, we want to provide messages that consist of three sentences. From | |
14 * the user's perspective these sentences should explain: | |
15 * | |
16 * 1. what is wrong, | |
17 * 2. why is it wrong, and | |
18 * 3. how do I fix it. | |
19 * | |
20 * However, we combine the first two in the [message] and the last in the | |
21 * [correction]. | |
22 * | |
23 * When composing messages (including correction messages) keep the following | |
24 * guidelines in mind. | |
25 * | |
26 * 1. The message should be a complete sentence starting with an uppercase | |
27 * letter, and ending with a period. | |
28 * | |
29 * 2. Reserved words and embedded identifiers should be in single quotes, so | |
30 * prefer double quotes for the complete message. For example, | |
31 * ``` | |
32 * "The class '{0}' can't use 'super'." | |
33 * ``` | |
34 * Notice that the word 'class' in the preceding message is not quoted as it | |
35 * refers to the concept 'class', not the reserved word. On the other hand, | |
36 * 'super' refers to the reserved word. Do not quote 'null' and numeric literals . | |
37 * | |
38 * 3. Do not try to compose messages, as it can make translating them hard. | |
39 * | |
40 * 4. Try to keep the error messages short, but informative. | |
41 * | |
42 * 5. Use simple words and terminology, assume the reader of the message doesn't | |
43 * have an advanced degree in math, and that English is not the reader's native | |
44 * language. Do not assume any formal computer science training. For example, do | |
45 * not use Latin abbreviations (prefer "that is" over "i.e.", and "for example" | |
46 * over "e.g."). Also avoid phrases such as "if and only if" and "iff"; that | |
47 * level of precision is unnecessary. | |
48 * | |
49 * 6. Prefer contractions when they are in common use, for example, prefer | |
50 * "can't" over "cannot". Using "cannot", "must not", "shall not", etc. is | |
51 * off-putting to people new to programming. | |
52 * | |
53 * 7. Use common terminology, preferably from the Dart Language Specification. | |
54 * This increases the user's chance of finding a good explanation on the web. | |
55 * | |
56 * 8. Do not try to be cute or funny. It is extremely frustrating to work on a | |
57 * product that crashes with a "tongue-in-cheek" message, especially if you did | |
58 * not want to use this product to begin with. | |
59 * | |
60 * 9. Do not lie, that is, do not write error messages containing phrases like | |
61 * "can't happen". If the user ever saw this message, it would be a lie. Prefer | |
62 * messages like: "Internal error: This function should not be called when 'x' | |
63 * is null.". | |
64 * | |
65 * 10. Prefer to not use the imperative tone. That is, the message should not | |
66 * sound accusing or like it is ordering the user around. The computer should | |
67 * describe the problem, not criticize the user for violating the specification. | |
68 */ | |
69 abstract class ErrorCode { | |
Brian Wilkerson
2016/11/08 20:53:23
I would recommend putting the abstract classes in
Paul Berry
2016/11/08 21:47:06
Acknowledged. I will address this in later CLs.
| |
70 /** | |
71 * Engine error code values. | |
72 */ | |
73 @deprecated | |
74 static const List<ErrorCode> values = errorCodeValues; | |
75 | |
76 /** | |
77 * An empty list of error codes. | |
78 */ | |
79 @deprecated | |
80 static const List<ErrorCode> EMPTY_LIST = const <ErrorCode>[]; | |
81 | |
82 /** | |
83 * The name of the error code. | |
84 */ | |
85 final String name; | |
86 | |
87 /** | |
88 * The template used to create the message to be displayed for this error. The | |
89 * message should indicate what is wrong and why it is wrong. | |
90 */ | |
91 final String message; | |
92 | |
93 /** | |
94 * The template used to create the correction to be displayed for this error, | |
95 * or `null` if there is no correction information for this error. The | |
96 * correction should indicate how the user can fix the error. | |
97 */ | |
98 final String correction; | |
99 | |
100 /** | |
101 * Initialize a newly created error code to have the given [name]. The message | |
102 * associated with the error will be created from the given [message] | |
103 * template. The correction associated with the error will be created from the | |
104 * given [correction] template. | |
105 */ | |
106 const ErrorCode(this.name, this.message, [this.correction]); | |
107 | |
108 /** | |
109 * The severity of the error. | |
110 */ | |
111 ErrorSeverity get errorSeverity; | |
112 | |
113 /** | |
114 * The type of the error. | |
115 */ | |
116 ErrorType get type; | |
117 | |
118 /** | |
119 * The unique name of this error code. | |
120 */ | |
121 String get uniqueName => "$runtimeType.$name"; | |
122 | |
123 @override | |
124 String toString() => uniqueName; | |
125 | |
126 /** | |
127 * Return the [ErrorCode] with the given [uniqueName], or `null` if not | |
128 * found. | |
129 */ | |
130 @deprecated | |
131 static ErrorCode byUniqueName(String uniqueName) => | |
132 errorCodeByUniqueName(uniqueName); | |
133 } | |
134 | |
135 /** | |
136 * The severity of an [ErrorCode]. | |
137 */ | |
138 class ErrorSeverity implements Comparable<ErrorSeverity> { | |
139 /** | |
140 * The severity representing a non-error. This is never used for any error | |
141 * code, but is useful for clients. | |
142 */ | |
143 static const ErrorSeverity NONE = const ErrorSeverity('NONE', 0, " ", "none"); | |
144 | |
145 /** | |
146 * The severity representing an informational level analysis issue. | |
147 */ | |
148 static const ErrorSeverity INFO = const ErrorSeverity('INFO', 1, "I", "info"); | |
149 | |
150 /** | |
151 * The severity representing a warning. Warnings can become errors if the `-We rror` command | |
152 * line flag is specified. | |
153 */ | |
154 static const ErrorSeverity WARNING = | |
155 const ErrorSeverity('WARNING', 2, "W", "warning"); | |
156 | |
157 /** | |
158 * The severity representing an error. | |
159 */ | |
160 static const ErrorSeverity ERROR = | |
161 const ErrorSeverity('ERROR', 3, "E", "error"); | |
162 | |
163 static const List<ErrorSeverity> values = const [NONE, INFO, WARNING, ERROR]; | |
164 | |
165 /** | |
166 * The name of this error code. | |
167 */ | |
168 final String name; | |
169 | |
170 /** | |
171 * The ordinal value of the error code. | |
172 */ | |
173 final int ordinal; | |
174 | |
175 /** | |
176 * The name of the severity used when producing machine output. | |
177 */ | |
178 final String machineCode; | |
179 | |
180 /** | |
181 * The name of the severity used when producing readable output. | |
182 */ | |
183 final String displayName; | |
184 | |
185 /** | |
186 * Initialize a newly created severity with the given names. | |
187 */ | |
188 const ErrorSeverity( | |
189 this.name, this.ordinal, this.machineCode, this.displayName); | |
190 | |
191 @override | |
192 int get hashCode => ordinal; | |
193 | |
194 @override | |
195 int compareTo(ErrorSeverity other) => ordinal - other.ordinal; | |
196 | |
197 /** | |
198 * Return the severity constant that represents the greatest severity. | |
199 */ | |
200 ErrorSeverity max(ErrorSeverity severity) => | |
201 this.ordinal >= severity.ordinal ? this : severity; | |
202 | |
203 @override | |
204 String toString() => name; | |
205 } | |
206 | |
207 /** | |
208 * The type of an [ErrorCode]. | |
209 */ | |
210 class ErrorType implements Comparable<ErrorType> { | |
211 /** | |
212 * Task (todo) comments in user code. | |
213 */ | |
214 static const ErrorType TODO = const ErrorType('TODO', 0, ErrorSeverity.INFO); | |
215 | |
216 /** | |
217 * Extra analysis run over the code to follow best practices, which are not in | |
218 * the Dart Language Specification. | |
219 */ | |
220 static const ErrorType HINT = const ErrorType('HINT', 1, ErrorSeverity.INFO); | |
221 | |
222 /** | |
223 * Compile-time errors are errors that preclude execution. A compile time | |
224 * error must be reported by a Dart compiler before the erroneous code is | |
225 * executed. | |
226 */ | |
227 static const ErrorType COMPILE_TIME_ERROR = | |
228 const ErrorType('COMPILE_TIME_ERROR', 2, ErrorSeverity.ERROR); | |
229 | |
230 /** | |
231 * Checked mode compile-time errors are errors that preclude execution in | |
232 * checked mode. | |
233 */ | |
234 static const ErrorType CHECKED_MODE_COMPILE_TIME_ERROR = const ErrorType( | |
235 'CHECKED_MODE_COMPILE_TIME_ERROR', 3, ErrorSeverity.ERROR); | |
236 | |
237 /** | |
238 * Static warnings are those warnings reported by the static checker. They | |
239 * have no effect on execution. Static warnings must be provided by Dart | |
240 * compilers used during development. | |
241 */ | |
242 static const ErrorType STATIC_WARNING = | |
243 const ErrorType('STATIC_WARNING', 4, ErrorSeverity.WARNING); | |
244 | |
245 /** | |
246 * Many, but not all, static warnings relate to types, in which case they are | |
247 * known as static type warnings. | |
248 */ | |
249 static const ErrorType STATIC_TYPE_WARNING = | |
250 const ErrorType('STATIC_TYPE_WARNING', 5, ErrorSeverity.WARNING); | |
251 | |
252 /** | |
253 * Syntactic errors are errors produced as a result of input that does not | |
254 * conform to the grammar. | |
255 */ | |
256 static const ErrorType SYNTACTIC_ERROR = | |
257 const ErrorType('SYNTACTIC_ERROR', 6, ErrorSeverity.ERROR); | |
258 | |
259 /** | |
260 * Lint warnings describe style and best practice recommendations that can be | |
261 * used to formalize a project's style guidelines. | |
262 */ | |
263 static const ErrorType LINT = const ErrorType('LINT', 7, ErrorSeverity.INFO); | |
264 | |
265 static const List<ErrorType> values = const [ | |
266 TODO, | |
267 HINT, | |
268 COMPILE_TIME_ERROR, | |
269 CHECKED_MODE_COMPILE_TIME_ERROR, | |
270 STATIC_WARNING, | |
271 STATIC_TYPE_WARNING, | |
272 SYNTACTIC_ERROR, | |
273 LINT | |
274 ]; | |
275 | |
276 /** | |
277 * The name of this error type. | |
278 */ | |
279 final String name; | |
280 | |
281 /** | |
282 * The ordinal value of the error type. | |
283 */ | |
284 final int ordinal; | |
285 | |
286 /** | |
287 * The severity of this type of error. | |
288 */ | |
289 final ErrorSeverity severity; | |
290 | |
291 /** | |
292 * Initialize a newly created error type to have the given [name] and | |
293 * [severity]. | |
294 */ | |
295 const ErrorType(this.name, this.ordinal, this.severity); | |
296 | |
297 String get displayName => name.toLowerCase().replaceAll('_', ' '); | |
298 | |
299 @override | |
300 int get hashCode => ordinal; | |
301 | |
302 @override | |
303 int compareTo(ErrorType other) => ordinal - other.ordinal; | |
304 | |
305 @override | |
306 String toString() => name; | |
307 } | |
308 | |
309 /** | |
310 * The error codes used for errors detected by the scanner. | |
311 */ | |
312 class ScannerErrorCode extends ErrorCode { | |
313 /** | |
314 * Parameters: | |
315 * 0: the illegal character | |
316 */ | |
317 static const ScannerErrorCode ILLEGAL_CHARACTER = | |
318 const ScannerErrorCode('ILLEGAL_CHARACTER', "Illegal character '{0}'."); | |
319 | |
320 static const ScannerErrorCode MISSING_DIGIT = | |
321 const ScannerErrorCode('MISSING_DIGIT', "Decimal digit expected."); | |
322 | |
323 static const ScannerErrorCode MISSING_HEX_DIGIT = const ScannerErrorCode( | |
324 'MISSING_HEX_DIGIT', "Hexidecimal digit expected."); | |
325 | |
326 static const ScannerErrorCode MISSING_QUOTE = | |
327 const ScannerErrorCode('MISSING_QUOTE', "Expected quote (' or \")."); | |
328 | |
329 /** | |
330 * Parameters: | |
331 * 0: the path of the file that cannot be read | |
332 */ | |
333 static const ScannerErrorCode UNABLE_GET_CONTENT = const ScannerErrorCode( | |
334 'UNABLE_GET_CONTENT', "Unable to get content of '{0}'."); | |
335 | |
336 static const ScannerErrorCode UNTERMINATED_MULTI_LINE_COMMENT = | |
337 const ScannerErrorCode( | |
338 'UNTERMINATED_MULTI_LINE_COMMENT', | |
339 "Unterminated multi-line comment.", | |
340 "Try terminating the comment with '*/', or " | |
341 "removing any unbalanced occurances of '/*' (because comments nest in Dart)."); | |
342 | |
343 static const ScannerErrorCode UNTERMINATED_STRING_LITERAL = | |
344 const ScannerErrorCode( | |
345 'UNTERMINATED_STRING_LITERAL', "Unterminated string literal."); | |
346 | |
347 /** | |
348 * Initialize a newly created error code to have the given [name]. The message | |
349 * associated with the error will be created from the given [message] | |
350 * template. The correction associated with the error will be created from the | |
351 * given [correction] template. | |
352 */ | |
353 const ScannerErrorCode(String name, String message, [String correction]) | |
354 : super(name, message, correction); | |
355 | |
356 @override | |
357 ErrorSeverity get errorSeverity => ErrorSeverity.ERROR; | |
358 | |
359 @override | |
360 ErrorType get type => ErrorType.SYNTACTIC_ERROR; | |
361 } | |
OLD | NEW |