OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of csslib.parser; |
| 6 |
5 /** | 7 /** |
6 * A single token in the Dart language. | 8 * A single token in the Dart language. |
7 */ | 9 */ |
8 class Token { | 10 class Token { |
9 /** A member of [TokenKind] specifying what kind of token this is. */ | 11 /** A member of [TokenKind] specifying what kind of token this is. */ |
10 final int kind; | 12 final int kind; |
11 | 13 |
12 /** The [SourceFile] this token came from. */ | 14 /** The location where this token was parsed from. */ |
13 final SourceFile source; | 15 final Span span; |
14 | 16 |
15 /** The start and end indexes into the [SourceFile] of this [Token]. */ | 17 /** The start offset of this token. */ |
16 final int start, end; | 18 int get start => span.start.offset; |
17 | 19 |
18 Token(this.kind, this.source, this.start, this.end) {} | 20 /** The end offset of this token. */ |
19 | 21 int get end => span.end.offset; |
20 Token.fake(this.kind, span) | |
21 : this.source = span.file, this.start = span.start, this.end = span.end; | |
22 | 22 |
23 /** Returns the source text corresponding to this [Token]. */ | 23 /** Returns the source text corresponding to this [Token]. */ |
24 String get text { | 24 String get text => span.text; |
25 return source.text.substring(start, end); | 25 |
26 } | 26 Token(this.kind, this.span); |
27 | 27 |
28 /** Returns a pretty representation of this token for error messages. **/ | 28 /** Returns a pretty representation of this token for error messages. **/ |
29 String toString() { | 29 String toString() { |
30 var kindText = TokenKind.kindToString(kind); | 30 var kindText = TokenKind.kindToString(kind); |
31 var actualText = text; | 31 var actualText = text; |
32 if (kindText != actualText) { | 32 if (kindText != actualText) { |
33 if (actualText.length > 10) { | 33 if (actualText.length > 10) { |
34 actualText = actualText.substring(0, 8) + '...'; | 34 actualText = '${actualText.substring(0, 8)}...'; |
35 } | 35 } |
36 return '$kindText($actualText)'; | 36 return '$kindText($actualText)'; |
37 } else { | 37 } else { |
38 return kindText; | 38 return kindText; |
39 } | 39 } |
40 } | 40 } |
41 | |
42 /** Returns a [SourceSpan] representing the source location. */ | |
43 SourceSpan get span { | |
44 return new SourceSpan(source, start, end); | |
45 } | |
46 } | 41 } |
47 | 42 |
48 /** A token containing a parsed literal value. */ | 43 /** A token containing a parsed literal value. */ |
49 class LiteralToken extends Token { | 44 class LiteralToken extends Token { |
50 var value; | 45 var value; |
51 LiteralToken(kind, source, start, end, this.value) | 46 LiteralToken(int kind, Span span, this.value) : super(kind, span); |
52 : super(kind, source, start, end); | |
53 } | 47 } |
54 | 48 |
55 /** A token containing error information. */ | 49 /** A token containing error information. */ |
56 class ErrorToken extends Token { | 50 class ErrorToken extends Token { |
57 String message; | 51 String message; |
58 ErrorToken(kind, source, start, end, this.message) | 52 ErrorToken(int kind, Span span, this.message) : super(kind, span); |
59 : super(kind, source, start, end); | |
60 } | 53 } |
OLD | NEW |