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.generated.scanner; | 5 library analyzer.src.dart.scanner.scanner; |
6 | 6 |
7 import 'package:analyzer/dart/ast/token.dart'; | 7 import 'package:analyzer/dart/ast/token.dart'; |
8 import 'package:analyzer/src/dart/ast/token.dart'; | 8 import 'package:analyzer/src/dart/ast/token.dart'; |
| 9 import 'package:analyzer/src/dart/scanner/reader.dart'; |
9 import 'package:analyzer/src/generated/error.dart'; | 10 import 'package:analyzer/src/generated/error.dart'; |
10 import 'package:analyzer/src/generated/java_engine.dart'; | 11 import 'package:analyzer/src/generated/java_engine.dart'; |
11 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
12 | 13 |
13 export 'package:analyzer/dart/ast/token.dart'; | |
14 export 'package:analyzer/src/dart/ast/token.dart' hide SimpleToken; | |
15 | |
16 /** | |
17 * A [CharacterReader] that reads a range of characters from another character | |
18 * reader. | |
19 */ | |
20 class CharacterRangeReader extends CharacterReader { | |
21 /** | |
22 * The reader from which the characters are actually being read. | |
23 */ | |
24 final CharacterReader baseReader; | |
25 | |
26 /** | |
27 * The last character to be read. | |
28 */ | |
29 final int endIndex; | |
30 | |
31 /** | |
32 * Initialize a newly created reader to read the characters from the given | |
33 * [baseReader] between the [startIndex] inclusive to [endIndex] exclusive. | |
34 */ | |
35 CharacterRangeReader(this.baseReader, int startIndex, this.endIndex) { | |
36 baseReader.offset = startIndex - 1; | |
37 } | |
38 | |
39 @override | |
40 int get offset => baseReader.offset; | |
41 | |
42 @override | |
43 void set offset(int offset) { | |
44 baseReader.offset = offset; | |
45 } | |
46 | |
47 @override | |
48 int advance() { | |
49 if (baseReader.offset + 1 >= endIndex) { | |
50 return -1; | |
51 } | |
52 return baseReader.advance(); | |
53 } | |
54 | |
55 @override | |
56 String getString(int start, int endDelta) => | |
57 baseReader.getString(start, endDelta); | |
58 | |
59 @override | |
60 int peek() { | |
61 if (baseReader.offset + 1 >= endIndex) { | |
62 return -1; | |
63 } | |
64 return baseReader.peek(); | |
65 } | |
66 } | |
67 | |
68 /** | |
69 * An object used by the scanner to read the characters to be scanned. | |
70 */ | |
71 abstract class CharacterReader { | |
72 /** | |
73 * The current offset relative to the beginning of the source. Return the | |
74 * initial offset if the scanner has not yet scanned the source code, and one | |
75 * (1) past the end of the source code if the entire source code has been | |
76 * scanned. | |
77 */ | |
78 int get offset; | |
79 | |
80 /** | |
81 * Set the current offset relative to the beginning of the source to the given | |
82 * [offset]. The new offset must be between the initial offset and one (1) | |
83 * past the end of the source code. | |
84 */ | |
85 void set offset(int offset); | |
86 | |
87 /** | |
88 * Advance the current position and return the character at the new current | |
89 * position. | |
90 */ | |
91 int advance(); | |
92 | |
93 /** | |
94 * Return the substring of the source code between the [start] offset and the | |
95 * modified current position. The current position is modified by adding the | |
96 * [endDelta], which is the number of characters after the current location to | |
97 * be included in the string, or the number of characters before the current | |
98 * location to be excluded if the offset is negative. | |
99 */ | |
100 String getString(int start, int endDelta); | |
101 | |
102 /** | |
103 * Return the character at the current position without changing the current | |
104 * position. | |
105 */ | |
106 int peek(); | |
107 } | |
108 | |
109 /** | |
110 * A [CharacterReader] that reads characters from a character sequence. | |
111 */ | |
112 class CharSequenceReader implements CharacterReader { | |
113 /** | |
114 * The sequence from which characters will be read. | |
115 */ | |
116 final String _sequence; | |
117 | |
118 /** | |
119 * The number of characters in the string. | |
120 */ | |
121 int _stringLength = 0; | |
122 | |
123 /** | |
124 * The index, relative to the string, of the last character that was read. | |
125 */ | |
126 int _charOffset = 0; | |
127 | |
128 /** | |
129 * Initialize a newly created reader to read the characters in the given | |
130 * [_sequence]. | |
131 */ | |
132 CharSequenceReader(this._sequence) { | |
133 this._stringLength = _sequence.length; | |
134 this._charOffset = -1; | |
135 } | |
136 | |
137 @override | |
138 int get offset => _charOffset; | |
139 | |
140 @override | |
141 void set offset(int offset) { | |
142 _charOffset = offset; | |
143 } | |
144 | |
145 @override | |
146 int advance() { | |
147 if (_charOffset + 1 >= _stringLength) { | |
148 return -1; | |
149 } | |
150 return _sequence.codeUnitAt(++_charOffset); | |
151 } | |
152 | |
153 @override | |
154 String getString(int start, int endDelta) => | |
155 _sequence.substring(start, _charOffset + 1 + endDelta).toString(); | |
156 | |
157 @override | |
158 int peek() { | |
159 if (_charOffset + 1 >= _stringLength) { | |
160 return -1; | |
161 } | |
162 return _sequence.codeUnitAt(_charOffset + 1); | |
163 } | |
164 } | |
165 | |
166 /** | 14 /** |
167 * A state in a state machine used to scan keywords. | 15 * A state in a state machine used to scan keywords. |
168 */ | 16 */ |
169 class KeywordState { | 17 class KeywordState { |
170 /** | 18 /** |
171 * An empty transition table used by leaf states. | 19 * An empty transition table used by leaf states. |
172 */ | 20 */ |
173 static List<KeywordState> _EMPTY_TABLE = new List<KeywordState>(26); | 21 static List<KeywordState> _EMPTY_TABLE = new List<KeywordState>(26); |
174 | 22 |
175 /** | 23 /** |
(...skipping 1342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1518 */ | 1366 */ |
1519 const ScannerErrorCode(String name, String message, [String correction]) | 1367 const ScannerErrorCode(String name, String message, [String correction]) |
1520 : super(name, message, correction); | 1368 : super(name, message, correction); |
1521 | 1369 |
1522 @override | 1370 @override |
1523 ErrorSeverity get errorSeverity => ErrorSeverity.ERROR; | 1371 ErrorSeverity get errorSeverity => ErrorSeverity.ERROR; |
1524 | 1372 |
1525 @override | 1373 @override |
1526 ErrorType get type => ErrorType.SYNTACTIC_ERROR; | 1374 ErrorType get type => ErrorType.SYNTACTIC_ERROR; |
1527 } | 1375 } |
1528 | |
1529 /** | |
1530 * A [CharacterReader] that reads characters from a character sequence, but adds | |
1531 * a delta when reporting the current character offset so that the character | |
1532 * sequence can be a subsequence from a larger sequence. | |
1533 */ | |
1534 class SubSequenceReader extends CharSequenceReader { | |
1535 /** | |
1536 * The offset from the beginning of the file to the beginning of the source | |
1537 * being scanned. | |
1538 */ | |
1539 final int _offsetDelta; | |
1540 | |
1541 /** | |
1542 * Initialize a newly created reader to read the characters in the given | |
1543 * [sequence]. The [_offsetDelta] is the offset from the beginning of the file | |
1544 * to the beginning of the source being scanned | |
1545 */ | |
1546 SubSequenceReader(String sequence, this._offsetDelta) : super(sequence); | |
1547 | |
1548 @override | |
1549 int get offset => _offsetDelta + super.offset; | |
1550 | |
1551 @override | |
1552 void set offset(int offset) { | |
1553 super.offset = offset - _offsetDelta; | |
1554 } | |
1555 | |
1556 @override | |
1557 String getString(int start, int endDelta) => | |
1558 super.getString(start - _offsetDelta, endDelta); | |
1559 } | |
OLD | NEW |