Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(366)

Side by Side Diff: pkg/front_end/lib/src/scanner/errors.dart

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

Powered by Google App Engine
This is Rietveld 408576698