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

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

Issue 2848493003: remove TokenType.KEYWORD (Closed)
Patch Set: rebase Created 3 years, 7 months 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 /**
6 * Defines the tokens that are produced by the scanner, used by the parser, and 6 * Defines the tokens that are produced by the scanner, used by the parser, and
7 * referenced from the [AST structure](ast.dart). 7 * referenced from the [AST structure](ast.dart).
8 */ 8 */
9 import 'dart:collection'; 9 import 'dart:collection';
10 10
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 references.forEach((ref) => copy.references.add(ref.copy())); 159 references.forEach((ref) => copy.references.add(ref.copy()));
160 return copy; 160 return copy;
161 } 161 }
162 } 162 }
163 163
164 /** 164 /**
165 * The keywords in the Dart programming language. 165 * The keywords in the Dart programming language.
166 * 166 *
167 * Clients may not extend, implement or mix-in this class. 167 * Clients may not extend, implement or mix-in this class.
168 */ 168 */
169 class Keyword { 169 class Keyword extends TokenType {
170 static const Keyword ABSTRACT = const Keyword("abstract", isBuiltIn: true); 170 static const Keyword ABSTRACT =
171 const Keyword("abstract", "ABSTRACT", isBuiltIn: true);
171 172
172 static const Keyword AS = 173 static const Keyword AS = const Keyword("as", "AS",
173 const Keyword("as", info: TokenType.AS, isBuiltIn: true); 174 precedence: RELATIONAL_PRECEDENCE, isBuiltIn: true);
174 175
175 static const Keyword ASSERT = const Keyword("assert"); 176 static const Keyword ASSERT = const Keyword("assert", "ASSERT");
176 177
177 static const Keyword ASYNC = const Keyword("async", isPseudo: true); 178 static const Keyword ASYNC = const Keyword("async", "ASYNC", isPseudo: true);
178 179
179 static const Keyword AWAIT = const Keyword("await", isPseudo: true); 180 static const Keyword AWAIT = const Keyword("await", "AWAIT", isPseudo: true);
180 181
181 static const Keyword BREAK = const Keyword("break"); 182 static const Keyword BREAK = const Keyword("break", "BREAK");
182 183
183 static const Keyword CASE = const Keyword("case"); 184 static const Keyword CASE = const Keyword("case", "CASE");
184 185
185 static const Keyword CATCH = const Keyword("catch"); 186 static const Keyword CATCH = const Keyword("catch", "CATCH");
186 187
187 static const Keyword CLASS = const Keyword("class"); 188 static const Keyword CLASS = const Keyword("class", "CLASS");
188 189
189 static const Keyword CONST = const Keyword("const"); 190 static const Keyword CONST = const Keyword("const", "CONST");
190 191
191 static const Keyword CONTINUE = const Keyword("continue"); 192 static const Keyword CONTINUE = const Keyword("continue", "CONTINUE");
192 193
193 static const Keyword COVARIANT = const Keyword("covariant", isBuiltIn: true); 194 static const Keyword COVARIANT =
195 const Keyword("covariant", "COVARIANT", isBuiltIn: true);
194 196
195 static const Keyword DEFAULT = const Keyword("default"); 197 static const Keyword DEFAULT = const Keyword("default", "DEFAULT");
196 198
197 static const Keyword DEFERRED = const Keyword("deferred", isBuiltIn: true); 199 static const Keyword DEFERRED =
200 const Keyword("deferred", "DEFERRED", isBuiltIn: true);
198 201
199 static const Keyword DO = const Keyword("do"); 202 static const Keyword DO = const Keyword("do", "DO");
200 203
201 static const Keyword DYNAMIC = const Keyword("dynamic", isBuiltIn: true); 204 static const Keyword DYNAMIC =
205 const Keyword("dynamic", "DYNAMIC", isBuiltIn: true);
202 206
203 static const Keyword ELSE = const Keyword("else"); 207 static const Keyword ELSE = const Keyword("else", "ELSE");
204 208
205 static const Keyword ENUM = const Keyword("enum"); 209 static const Keyword ENUM = const Keyword("enum", "ENUM");
206 210
207 static const Keyword EXPORT = const Keyword("export", isBuiltIn: true); 211 static const Keyword EXPORT =
212 const Keyword("export", "EXPORT", isBuiltIn: true);
208 213
209 static const Keyword EXTENDS = const Keyword("extends"); 214 static const Keyword EXTENDS = const Keyword("extends", "EXTENDS");
210 215
211 static const Keyword EXTERNAL = const Keyword("external", isBuiltIn: true); 216 static const Keyword EXTERNAL =
217 const Keyword("external", "EXTERNAL", isBuiltIn: true);
212 218
213 static const Keyword FACTORY = const Keyword("factory", isBuiltIn: true); 219 static const Keyword FACTORY =
220 const Keyword("factory", "FACTORY", isBuiltIn: true);
214 221
215 static const Keyword FALSE = const Keyword("false"); 222 static const Keyword FALSE = const Keyword("false", "FALSE");
216 223
217 static const Keyword FINAL = const Keyword("final"); 224 static const Keyword FINAL = const Keyword("final", "FINAL");
218 225
219 static const Keyword FINALLY = const Keyword("finally"); 226 static const Keyword FINALLY = const Keyword("finally", "FINALLY");
220 227
221 static const Keyword FOR = const Keyword("for"); 228 static const Keyword FOR = const Keyword("for", "FOR");
222 229
223 static const Keyword FUNCTION = const Keyword("Function", isPseudo: true); 230 static const Keyword FUNCTION =
231 const Keyword("Function", "FUNCTION", isPseudo: true);
224 232
225 static const Keyword GET = const Keyword("get", isBuiltIn: true); 233 static const Keyword GET = const Keyword("get", "GET", isBuiltIn: true);
226 234
227 static const Keyword HIDE = const Keyword("hide", isPseudo: true); 235 static const Keyword HIDE = const Keyword("hide", "HIDE", isPseudo: true);
228 236
229 static const Keyword IF = const Keyword("if"); 237 static const Keyword IF = const Keyword("if", "IF");
230 238
231 static const Keyword IMPLEMENTS = 239 static const Keyword IMPLEMENTS =
232 const Keyword("implements", isBuiltIn: true); 240 const Keyword("implements", "IMPLEMENTS", isBuiltIn: true);
233 241
234 static const Keyword IMPORT = const Keyword("import", isBuiltIn: true); 242 static const Keyword IMPORT =
243 const Keyword("import", "IMPORT", isBuiltIn: true);
235 244
236 static const Keyword IN = const Keyword("in"); 245 static const Keyword IN = const Keyword("in", "IN");
237 246
238 static const Keyword IS = const Keyword("is", info: TokenType.IS); 247 static const Keyword IS =
248 const Keyword("is", "IS", precedence: RELATIONAL_PRECEDENCE);
239 249
240 static const Keyword LIBRARY = const Keyword("library", isBuiltIn: true); 250 static const Keyword LIBRARY =
251 const Keyword("library", "LIBRARY", isBuiltIn: true);
241 252
242 static const Keyword NATIVE = const Keyword("native", isPseudo: true); 253 static const Keyword NATIVE =
254 const Keyword("native", "NATIVE", isPseudo: true);
243 255
244 static const Keyword NEW = const Keyword("new"); 256 static const Keyword NEW = const Keyword("new", "NEW");
245 257
246 static const Keyword NULL = const Keyword("null"); 258 static const Keyword NULL = const Keyword("null", "NULL");
247 259
248 static const Keyword OF = const Keyword("of", isPseudo: true); 260 static const Keyword OF = const Keyword("of", "OF", isPseudo: true);
249 261
250 static const Keyword ON = const Keyword("on", isPseudo: true); 262 static const Keyword ON = const Keyword("on", "ON", isPseudo: true);
251 263
252 static const Keyword OPERATOR = const Keyword("operator", isBuiltIn: true); 264 static const Keyword OPERATOR =
265 const Keyword("operator", "OPERATOR", isBuiltIn: true);
253 266
254 static const Keyword PART = const Keyword("part", isBuiltIn: true); 267 static const Keyword PART = const Keyword("part", "PART", isBuiltIn: true);
255 268
256 static const Keyword PATCH = const Keyword("patch", isPseudo: true); 269 static const Keyword PATCH = const Keyword("patch", "PATCH", isPseudo: true);
257 270
258 static const Keyword RETHROW = const Keyword("rethrow"); 271 static const Keyword RETHROW = const Keyword("rethrow", "RETHROW");
259 272
260 static const Keyword RETURN = const Keyword("return"); 273 static const Keyword RETURN = const Keyword("return", "RETURN");
261 274
262 static const Keyword SET = const Keyword("set", isBuiltIn: true); 275 static const Keyword SET = const Keyword("set", "SET", isBuiltIn: true);
263 276
264 static const Keyword SHOW = const Keyword("show", isPseudo: true); 277 static const Keyword SHOW = const Keyword("show", "SHOW", isPseudo: true);
265 278
266 static const Keyword SOURCE = const Keyword("source", isPseudo: true); 279 static const Keyword SOURCE =
280 const Keyword("source", "SOURCE", isPseudo: true);
267 281
268 static const Keyword STATIC = const Keyword("static", isBuiltIn: true); 282 static const Keyword STATIC =
283 const Keyword("static", "STATIC", isBuiltIn: true);
269 284
270 static const Keyword SUPER = const Keyword("super"); 285 static const Keyword SUPER = const Keyword("super", "SUPER");
271 286
272 static const Keyword SWITCH = const Keyword("switch"); 287 static const Keyword SWITCH = const Keyword("switch", "SWITCH");
273 288
274 static const Keyword SYNC = const Keyword("sync", isPseudo: true); 289 static const Keyword SYNC = const Keyword("sync", "SYNC", isPseudo: true);
275 290
276 static const Keyword THIS = const Keyword("this"); 291 static const Keyword THIS = const Keyword("this", "THIS");
277 292
278 static const Keyword THROW = const Keyword("throw"); 293 static const Keyword THROW = const Keyword("throw", "THROW");
279 294
280 static const Keyword TRUE = const Keyword("true"); 295 static const Keyword TRUE = const Keyword("true", "TRUE");
281 296
282 static const Keyword TRY = const Keyword("try"); 297 static const Keyword TRY = const Keyword("try", "TRY");
283 298
284 static const Keyword TYPEDEF = const Keyword("typedef", isBuiltIn: true); 299 static const Keyword TYPEDEF =
300 const Keyword("typedef", "TYPEDEF", isBuiltIn: true);
285 301
286 static const Keyword VAR = const Keyword("var"); 302 static const Keyword VAR = const Keyword("var", "VAR");
287 303
288 static const Keyword VOID = const Keyword("void"); 304 static const Keyword VOID = const Keyword("void", "VOID");
289 305
290 static const Keyword WHILE = const Keyword("while"); 306 static const Keyword WHILE = const Keyword("while", "WHILE");
291 307
292 static const Keyword WITH = const Keyword("with"); 308 static const Keyword WITH = const Keyword("with", "WITH");
293 309
294 static const Keyword YIELD = const Keyword("yield", isPseudo: true); 310 static const Keyword YIELD = const Keyword("yield", "YIELD", isPseudo: true);
295 311
296 static const List<Keyword> values = const <Keyword>[ 312 static const List<Keyword> values = const <Keyword>[
297 ABSTRACT, 313 ABSTRACT,
298 AS, 314 AS,
299 ASSERT, 315 ASSERT,
300 ASYNC, 316 ASYNC,
301 AWAIT, 317 AWAIT,
302 BREAK, 318 BREAK,
303 CASE, 319 CASE,
304 CATCH, 320 CATCH,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 WHILE, 372 WHILE,
357 WITH, 373 WITH,
358 YIELD, 374 YIELD,
359 ]; 375 ];
360 376
361 /** 377 /**
362 * A table mapping the lexemes of keywords to the corresponding keyword. 378 * A table mapping the lexemes of keywords to the corresponding keyword.
363 */ 379 */
364 static final Map<String, Keyword> keywords = _createKeywordMap(); 380 static final Map<String, Keyword> keywords = _createKeywordMap();
365 381
366 final TokenType info;
367
368 /** 382 /**
369 * A flag indicating whether the keyword is "built-in" identifier. 383 * A flag indicating whether the keyword is "built-in" identifier.
370 */ 384 */
371 final bool isBuiltIn; 385 final bool isBuiltIn;
372 386
373 /** 387 /**
374 * A flag indicating whether the keyword can be used as an identifier 388 * A flag indicating whether the keyword can be used as an identifier
375 * in some situations. 389 * in some situations.
376 */ 390 */
377 final bool isPseudo; 391 final bool isPseudo;
378 392
379 /** 393 /**
380 * The lexeme for the keyword.
381 */
382 final String lexeme;
383
384 /**
385 * Initialize a newly created keyword. 394 * Initialize a newly created keyword.
386 */ 395 */
387 const Keyword(this.lexeme, 396 const Keyword(String lexeme, String name,
388 {this.isBuiltIn: false, 397 {this.isBuiltIn: false,
389 this.isPseudo: false, 398 this.isPseudo: false,
390 this.info: TokenType.KEYWORD}); 399 int precedence: NO_PRECEDENCE})
400 : super(lexeme, name, precedence, KEYWORD_TOKEN);
391 401
392 bool get isBuiltInOrPseudo => isBuiltIn || isPseudo; 402 bool get isBuiltInOrPseudo => isBuiltIn || isPseudo;
393 403
394 /** 404 /**
395 * A flag indicating whether the keyword is "built-in" identifier. 405 * A flag indicating whether the keyword is "built-in" identifier.
396 * This method exists for backward compatibility and will be removed. 406 * This method exists for backward compatibility and will be removed.
397 * Use [isBuiltIn] instead. 407 * Use [isBuiltIn] instead.
398 */ 408 */
399 @deprecated 409 @deprecated
400 bool get isPseudoKeyword => isBuiltIn; // TODO (danrubel): remove this 410 bool get isPseudoKeyword => isBuiltIn; // TODO (danrubel): remove this
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 * A token representing a keyword in the language. 443 * A token representing a keyword in the language.
434 */ 444 */
435 class KeywordToken extends SimpleToken { 445 class KeywordToken extends SimpleToken {
436 @override 446 @override
437 final Keyword keyword; 447 final Keyword keyword;
438 448
439 /** 449 /**
440 * Initialize a newly created token to represent the given [keyword] at the 450 * Initialize a newly created token to represent the given [keyword] at the
441 * given [offset]. 451 * given [offset].
442 */ 452 */
443 KeywordToken(this.keyword, int offset) : super(TokenType.KEYWORD, offset); 453 KeywordToken(this.keyword, int offset) : super(keyword, offset);
444
445 @override
446 String get lexeme => keyword.lexeme;
447 454
448 @override 455 @override
449 Token copy() => new KeywordToken(keyword, offset); 456 Token copy() => new KeywordToken(keyword, offset);
450 457
451 @override 458 @override
452 // Changed return type from Keyword to Object because 459 // Changed return type from Keyword to Object because
453 // fasta considers pseudo-keywords to be keywords rather than identifiers 460 // fasta considers pseudo-keywords to be keywords rather than identifiers
454 Object value() => keyword; 461 Object value() => keyword;
455 } 462 }
456 463
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 1027
1021 static const TokenType HEXADECIMAL = const TokenType( 1028 static const TokenType HEXADECIMAL = const TokenType(
1022 'hexadecimal', 'HEXADECIMAL', NO_PRECEDENCE, HEXADECIMAL_TOKEN); 1029 'hexadecimal', 'HEXADECIMAL', NO_PRECEDENCE, HEXADECIMAL_TOKEN);
1023 1030
1024 static const TokenType IDENTIFIER = const TokenType( 1031 static const TokenType IDENTIFIER = const TokenType(
1025 'identifier', 'STRING_INT', NO_PRECEDENCE, IDENTIFIER_TOKEN); 1032 'identifier', 'STRING_INT', NO_PRECEDENCE, IDENTIFIER_TOKEN);
1026 1033
1027 static const TokenType INT = 1034 static const TokenType INT =
1028 const TokenType('int', 'INT', NO_PRECEDENCE, INT_TOKEN); 1035 const TokenType('int', 'INT', NO_PRECEDENCE, INT_TOKEN);
1029 1036
1030 static const TokenType KEYWORD =
1031 const TokenType('keyword', 'KEYWORD', NO_PRECEDENCE, KEYWORD_TOKEN);
1032
1033 static const TokenType MULTI_LINE_COMMENT = const TokenType( 1037 static const TokenType MULTI_LINE_COMMENT = const TokenType(
1034 'comment', 'MULTI_LINE_COMMENT', NO_PRECEDENCE, COMMENT_TOKEN); 1038 'comment', 'MULTI_LINE_COMMENT', NO_PRECEDENCE, COMMENT_TOKEN);
1035 1039
1036 static const TokenType SCRIPT_TAG = 1040 static const TokenType SCRIPT_TAG =
1037 const TokenType('script', 'SCRIPT_TAG', NO_PRECEDENCE, SCRIPT_TOKEN); 1041 const TokenType('script', 'SCRIPT_TAG', NO_PRECEDENCE, SCRIPT_TOKEN);
1038 1042
1039 static const TokenType SINGLE_LINE_COMMENT = const TokenType( 1043 static const TokenType SINGLE_LINE_COMMENT = const TokenType(
1040 'comment', 'SINGLE_LINE_COMMENT', NO_PRECEDENCE, COMMENT_TOKEN); 1044 'comment', 'SINGLE_LINE_COMMENT', NO_PRECEDENCE, COMMENT_TOKEN);
1041 1045
1042 static const TokenType STRING = 1046 static const TokenType STRING =
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
1289 'GENERIC_METHOD_TYPE_LIST', 1293 'GENERIC_METHOD_TYPE_LIST',
1290 NO_PRECEDENCE, 1294 NO_PRECEDENCE,
1291 GENERIC_METHOD_TYPE_LIST_TOKEN); 1295 GENERIC_METHOD_TYPE_LIST_TOKEN);
1292 1296
1293 static const TokenType GENERIC_METHOD_TYPE_ASSIGN = const TokenType( 1297 static const TokenType GENERIC_METHOD_TYPE_ASSIGN = const TokenType(
1294 'generic_comment_assign', 1298 'generic_comment_assign',
1295 'GENERIC_METHOD_TYPE_ASSIGN', 1299 'GENERIC_METHOD_TYPE_ASSIGN',
1296 NO_PRECEDENCE, 1300 NO_PRECEDENCE,
1297 GENERIC_METHOD_TYPE_ASSIGN_TOKEN); 1301 GENERIC_METHOD_TYPE_ASSIGN_TOKEN);
1298 1302
1299 static const TokenType AS = 1303 static const TokenType AS = Keyword.AS;
1300 const TokenType('as', 'AS', RELATIONAL_PRECEDENCE, KEYWORD_TOKEN);
1301 1304
1302 static const TokenType IS = 1305 static const TokenType IS = Keyword.IS;
1303 const TokenType('is', 'IS', RELATIONAL_PRECEDENCE, KEYWORD_TOKEN);
1304 1306
1305 /** 1307 /**
1306 * Token type used by error tokens. 1308 * Token type used by error tokens.
1307 */ 1309 */
1308 static const TokenType BAD_INPUT = const TokenType( 1310 static const TokenType BAD_INPUT = const TokenType(
1309 'malformed input', 'BAD_INPUT', NO_PRECEDENCE, BAD_INPUT_TOKEN); 1311 'malformed input', 'BAD_INPUT', NO_PRECEDENCE, BAD_INPUT_TOKEN);
1310 1312
1311 /** 1313 /**
1312 * Token type used by synthetic tokens that are created during parser 1314 * Token type used by synthetic tokens that are created during parser
1313 * recovery (non-analyzer use case). 1315 * recovery (non-analyzer use case).
1314 */ 1316 */
1315 static const TokenType RECOVERY = 1317 static const TokenType RECOVERY =
1316 const TokenType('recovery', 'RECOVERY', NO_PRECEDENCE, RECOVERY_TOKEN); 1318 const TokenType('recovery', 'RECOVERY', NO_PRECEDENCE, RECOVERY_TOKEN);
1317 1319
1318 // TODO(danrubel): "all" is misleading 1320 // TODO(danrubel): "all" is misleading
1319 // because this list does not include all TokenType instances. 1321 // because this list does not include all TokenType instances.
1320 static const List<TokenType> all = const <TokenType>[ 1322 static const List<TokenType> all = const <TokenType>[
1321 TokenType.EOF, 1323 TokenType.EOF,
1322 TokenType.DOUBLE, 1324 TokenType.DOUBLE,
1323 TokenType.HEXADECIMAL, 1325 TokenType.HEXADECIMAL,
1324 TokenType.IDENTIFIER, 1326 TokenType.IDENTIFIER,
1325 TokenType.INT, 1327 TokenType.INT,
1326 TokenType.KEYWORD,
1327 TokenType.MULTI_LINE_COMMENT, 1328 TokenType.MULTI_LINE_COMMENT,
1328 TokenType.SCRIPT_TAG, 1329 TokenType.SCRIPT_TAG,
1329 TokenType.SINGLE_LINE_COMMENT, 1330 TokenType.SINGLE_LINE_COMMENT,
1330 TokenType.STRING, 1331 TokenType.STRING,
1331 TokenType.AMPERSAND, 1332 TokenType.AMPERSAND,
1332 TokenType.AMPERSAND_AMPERSAND, 1333 TokenType.AMPERSAND_AMPERSAND,
1333 TokenType.AMPERSAND_EQ, 1334 TokenType.AMPERSAND_EQ,
1334 TokenType.AT, 1335 TokenType.AT,
1335 TokenType.BANG, 1336 TokenType.BANG,
1336 TokenType.BANG_EQ, 1337 TokenType.BANG_EQ,
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
1555 1556
1556 void set precedingComments(CommentToken comment) { 1557 void set precedingComments(CommentToken comment) {
1557 _precedingComment = comment; 1558 _precedingComment = comment;
1558 _setCommentParent(_precedingComment); 1559 _setCommentParent(_precedingComment);
1559 } 1560 }
1560 1561
1561 @override 1562 @override
1562 Token copy() => 1563 Token copy() =>
1563 new TokenWithComment(type, offset, copyComments(precedingComments)); 1564 new TokenWithComment(type, offset, copyComments(precedingComments));
1564 } 1565 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/fasta/scanner/token.dart ('k') | pkg/front_end/test/precedence_info_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698