OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library analyzer.src.dart.ast.token; | 5 library analyzer.src.dart.ast.token; |
6 | 6 |
7 import 'package:analyzer/dart/ast/token.dart'; | 7 import 'package:analyzer/dart/ast/token.dart'; |
8 import 'package:analyzer/src/generated/java_engine.dart'; | 8 import 'package:analyzer/src/generated/java_engine.dart'; |
9 | 9 |
10 /** | 10 /** |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 /** | 44 /** |
45 * Initialize a newly created token to have the given [type] at the given | 45 * Initialize a newly created token to have the given [type] at the given |
46 * [offset] and to be preceded by the comments reachable from the given | 46 * [offset] and to be preceded by the comments reachable from the given |
47 * [comment]. | 47 * [comment]. |
48 */ | 48 */ |
49 BeginTokenWithComment(TokenType type, int offset, this._precedingComment) | 49 BeginTokenWithComment(TokenType type, int offset, this._precedingComment) |
50 : super(type, offset) { | 50 : super(type, offset) { |
51 _setCommentParent(_precedingComment); | 51 _setCommentParent(_precedingComment); |
52 } | 52 } |
53 | 53 |
| 54 @override |
54 CommentToken get precedingComments => _precedingComment; | 55 CommentToken get precedingComments => _precedingComment; |
55 | 56 |
56 void set precedingComments(CommentToken comment) { | 57 void set precedingComments(CommentToken comment) { |
57 _precedingComment = comment; | 58 _precedingComment = comment; |
58 _setCommentParent(_precedingComment); | 59 _setCommentParent(_precedingComment); |
59 } | 60 } |
60 | 61 |
61 @override | 62 @override |
62 void applyDelta(int delta) { | 63 void applyDelta(int delta) { |
63 super.applyDelta(delta); | 64 super.applyDelta(delta); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 /** | 158 /** |
158 * Initialize a newly created token to to represent the given [keyword] at the | 159 * Initialize a newly created token to to represent the given [keyword] at the |
159 * given [offset] and to be preceded by the comments reachable from the given | 160 * given [offset] and to be preceded by the comments reachable from the given |
160 * [comment]. | 161 * [comment]. |
161 */ | 162 */ |
162 KeywordTokenWithComment(Keyword keyword, int offset, this._precedingComment) | 163 KeywordTokenWithComment(Keyword keyword, int offset, this._precedingComment) |
163 : super(keyword, offset) { | 164 : super(keyword, offset) { |
164 _setCommentParent(_precedingComment); | 165 _setCommentParent(_precedingComment); |
165 } | 166 } |
166 | 167 |
| 168 @override |
167 CommentToken get precedingComments => _precedingComment; | 169 CommentToken get precedingComments => _precedingComment; |
168 | 170 |
169 void set precedingComments(CommentToken comment) { | 171 void set precedingComments(CommentToken comment) { |
170 _precedingComment = comment; | 172 _precedingComment = comment; |
171 _setCommentParent(_precedingComment); | 173 _setCommentParent(_precedingComment); |
172 } | 174 } |
173 | 175 |
174 @override | 176 @override |
175 void applyDelta(int delta) { | 177 void applyDelta(int delta) { |
176 super.applyDelta(delta); | 178 super.applyDelta(delta); |
(...skipping 10 matching lines...) Expand all Loading... |
187 } | 189 } |
188 | 190 |
189 /** | 191 /** |
190 * A token that was scanned from the input. Each token knows which tokens | 192 * A token that was scanned from the input. Each token knows which tokens |
191 * precede and follow it, acting as a link in a doubly linked list of tokens. | 193 * precede and follow it, acting as a link in a doubly linked list of tokens. |
192 */ | 194 */ |
193 class SimpleToken implements Token { | 195 class SimpleToken implements Token { |
194 /** | 196 /** |
195 * The type of the token. | 197 * The type of the token. |
196 */ | 198 */ |
| 199 @override |
197 final TokenType type; | 200 final TokenType type; |
198 | 201 |
199 /** | 202 /** |
200 * The offset from the beginning of the file to the first character in the | 203 * The offset from the beginning of the file to the first character in the |
201 * token. | 204 * token. |
202 */ | 205 */ |
| 206 @override |
203 int offset = 0; | 207 int offset = 0; |
204 | 208 |
205 /** | 209 /** |
206 * The previous token in the token stream. | 210 * The previous token in the token stream. |
207 */ | 211 */ |
| 212 @override |
208 Token previous; | 213 Token previous; |
209 | 214 |
210 /** | 215 /** |
211 * The next token in the token stream. | 216 * The next token in the token stream. |
212 */ | 217 */ |
213 Token _next; | 218 Token _next; |
214 | 219 |
215 /** | 220 /** |
216 * Initialize a newly created token to have the given [type] and [offset]. | 221 * Initialize a newly created token to have the given [type] and [offset]. |
217 */ | 222 */ |
218 SimpleToken(this.type, this.offset); | 223 SimpleToken(this.type, this.offset); |
219 | 224 |
220 /** | 225 @override |
221 * Return the offset from the beginning of the file to the character after the | |
222 * last character of the token. | |
223 */ | |
224 int get end => offset + length; | 226 int get end => offset + length; |
225 | 227 |
226 /** | 228 @override |
227 * Return `true` if this token represents an operator. | |
228 */ | |
229 bool get isOperator => type.isOperator; | 229 bool get isOperator => type.isOperator; |
230 | 230 |
231 /** | 231 @override |
232 * Return `true` if this token is a synthetic token. A synthetic token is a | |
233 * token that was introduced by the parser in order to recover from an error | |
234 * in the code. | |
235 */ | |
236 bool get isSynthetic => length == 0; | 232 bool get isSynthetic => length == 0; |
237 | 233 |
238 /** | 234 @override |
239 * Return `true` if this token represents an operator that can be defined by | |
240 * users. | |
241 */ | |
242 bool get isUserDefinableOperator => type.isUserDefinableOperator; | 235 bool get isUserDefinableOperator => type.isUserDefinableOperator; |
243 | 236 |
244 /** | 237 @override |
245 * Return the number of characters in the node's source range. | |
246 */ | |
247 int get length => lexeme.length; | 238 int get length => lexeme.length; |
248 | 239 |
249 /** | 240 @override |
250 * Return the lexeme that represents this token. | |
251 */ | |
252 String get lexeme => type.lexeme; | 241 String get lexeme => type.lexeme; |
253 | 242 |
254 /** | 243 @override |
255 * Return the next token in the token stream. | |
256 */ | |
257 Token get next => _next; | 244 Token get next => _next; |
258 | 245 |
259 /** | 246 @override |
260 * Return the first comment in the list of comments that precede this token, | |
261 * or `null` if there are no comments preceding this token. Additional | |
262 * comments can be reached by following the token stream using [next] until | |
263 * `null` is returned. | |
264 * | |
265 * For example, if the original contents were `/* one */ /* two */ id`, then | |
266 * the first preceding comment token will have a lexeme of `/* one */` and | |
267 * the next comment token will have a lexeme of `/* two */`. | |
268 */ | |
269 CommentToken get precedingComments => null; | 247 CommentToken get precedingComments => null; |
270 | 248 |
271 /** | 249 @override |
272 * Apply (add) the given [delta] to this token's offset. | |
273 */ | |
274 void applyDelta(int delta) { | 250 void applyDelta(int delta) { |
275 offset += delta; | 251 offset += delta; |
276 } | 252 } |
277 | 253 |
278 /** | 254 @override |
279 * Return a newly created token that is a copy of this token but that is not a | |
280 * part of any token stream. | |
281 */ | |
282 Token copy() => new Token(type, offset); | 255 Token copy() => new Token(type, offset); |
283 | 256 |
284 /** | 257 @override |
285 * Copy a linked list of comment tokens identical to the given comment tokens. | |
286 */ | |
287 Token copyComments(Token token) { | 258 Token copyComments(Token token) { |
288 if (token == null) { | 259 if (token == null) { |
289 return null; | 260 return null; |
290 } | 261 } |
291 Token head = token.copy(); | 262 Token head = token.copy(); |
292 Token tail = head; | 263 Token tail = head; |
293 token = token.next; | 264 token = token.next; |
294 while (token != null) { | 265 while (token != null) { |
295 tail = tail.setNext(token.copy()); | 266 tail = tail.setNext(token.copy()); |
296 token = token.next; | 267 token = token.next; |
297 } | 268 } |
298 return head; | 269 return head; |
299 } | 270 } |
300 | 271 |
301 /** | 272 @override |
302 * Return `true` if this token has any one of the given [types]. | |
303 */ | |
304 bool matchesAny(List<TokenType> types) { | 273 bool matchesAny(List<TokenType> types) { |
305 for (TokenType type in types) { | 274 for (TokenType type in types) { |
306 if (this.type == type) { | 275 if (this.type == type) { |
307 return true; | 276 return true; |
308 } | 277 } |
309 } | 278 } |
310 return false; | 279 return false; |
311 } | 280 } |
312 | 281 |
313 /** | 282 @override |
314 * Set the next token in the token stream to the given [token]. This has the | |
315 * side-effect of setting this token to be the previous token for the given | |
316 * token. Return the token that was passed in. | |
317 */ | |
318 Token setNext(Token token) { | 283 Token setNext(Token token) { |
319 _next = token; | 284 _next = token; |
320 token.previous = this; | 285 token.previous = this; |
321 return token; | 286 return token; |
322 } | 287 } |
323 | 288 |
324 /** | 289 @override |
325 * Set the next token in the token stream to the given token without changing | |
326 * which token is the previous token for the given token. Return the token | |
327 * that was passed in. | |
328 */ | |
329 Token setNextWithoutSettingPrevious(Token token) { | 290 Token setNextWithoutSettingPrevious(Token token) { |
330 _next = token; | 291 _next = token; |
331 return token; | 292 return token; |
332 } | 293 } |
333 | 294 |
334 @override | 295 @override |
335 String toString() => lexeme; | 296 String toString() => lexeme; |
336 | 297 |
337 /** | 298 @override |
338 * Return the value of this token. For keyword tokens, this is the keyword | |
339 * associated with the token, for other tokens it is the lexeme associated | |
340 * with the token. | |
341 */ | |
342 Object value() => type.lexeme; | 299 Object value() => type.lexeme; |
343 | 300 |
344 /** | 301 /** |
345 * Sets the `parent` property to `this` for the given [comment] and all the | 302 * Sets the `parent` property to `this` for the given [comment] and all the |
346 * next tokens. | 303 * next tokens. |
347 */ | 304 */ |
348 void _setCommentParent(CommentToken comment) { | 305 void _setCommentParent(CommentToken comment) { |
349 while (comment != null) { | 306 while (comment != null) { |
350 comment.parent = this; | 307 comment.parent = this; |
351 comment = comment.next; | 308 comment = comment.next; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 * Initialize a newly created token to have the given [type] at the given | 350 * Initialize a newly created token to have the given [type] at the given |
394 * [offset] and to be preceded by the comments reachable from the given | 351 * [offset] and to be preceded by the comments reachable from the given |
395 * [comment]. | 352 * [comment]. |
396 */ | 353 */ |
397 StringTokenWithComment( | 354 StringTokenWithComment( |
398 TokenType type, String value, int offset, this._precedingComment) | 355 TokenType type, String value, int offset, this._precedingComment) |
399 : super(type, value, offset) { | 356 : super(type, value, offset) { |
400 _setCommentParent(_precedingComment); | 357 _setCommentParent(_precedingComment); |
401 } | 358 } |
402 | 359 |
| 360 @override |
403 CommentToken get precedingComments => _precedingComment; | 361 CommentToken get precedingComments => _precedingComment; |
404 | 362 |
405 void set precedingComments(CommentToken comment) { | 363 void set precedingComments(CommentToken comment) { |
406 _precedingComment = comment; | 364 _precedingComment = comment; |
407 _setCommentParent(_precedingComment); | 365 _setCommentParent(_precedingComment); |
408 } | 366 } |
409 | 367 |
410 @override | 368 @override |
411 void applyDelta(int delta) { | 369 void applyDelta(int delta) { |
412 super.applyDelta(delta); | 370 super.applyDelta(delta); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 /** | 534 /** |
577 * Initialize a newly created token to have the given [type] at the given | 535 * Initialize a newly created token to have the given [type] at the given |
578 * [offset] and to be preceded by the comments reachable from the given | 536 * [offset] and to be preceded by the comments reachable from the given |
579 * [comment]. | 537 * [comment]. |
580 */ | 538 */ |
581 TokenWithComment(TokenType type, int offset, this._precedingComment) | 539 TokenWithComment(TokenType type, int offset, this._precedingComment) |
582 : super(type, offset) { | 540 : super(type, offset) { |
583 _setCommentParent(_precedingComment); | 541 _setCommentParent(_precedingComment); |
584 } | 542 } |
585 | 543 |
| 544 @override |
586 CommentToken get precedingComments => _precedingComment; | 545 CommentToken get precedingComments => _precedingComment; |
587 | 546 |
588 void set precedingComments(CommentToken comment) { | 547 void set precedingComments(CommentToken comment) { |
589 _precedingComment = comment; | 548 _precedingComment = comment; |
590 _setCommentParent(_precedingComment); | 549 _setCommentParent(_precedingComment); |
591 } | 550 } |
592 | 551 |
593 @override | 552 @override |
594 Token copy() => new TokenWithComment(type, offset, precedingComments); | 553 Token copy() => new TokenWithComment(type, offset, precedingComments); |
595 } | 554 } |
OLD | NEW |