OLD | NEW |
1 // Copyright (c) 2014, 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 library analyzer.src.dart.scanner.scanner; | 5 library front_end.src.scanner.scanner; |
6 | 6 |
7 import 'package:analyzer/dart/ast/token.dart'; | |
8 import 'package:analyzer/error/error.dart'; | |
9 import 'package:analyzer/error/listener.dart'; | |
10 import 'package:analyzer/src/dart/ast/token.dart'; | |
11 import 'package:analyzer/src/dart/error/syntactic_errors.dart'; | |
12 import 'package:analyzer/src/dart/scanner/reader.dart'; | |
13 import 'package:analyzer/src/generated/java_engine.dart'; | |
14 import 'package:analyzer/src/generated/source.dart'; | |
15 import 'package:charcode/ascii.dart'; | 7 import 'package:charcode/ascii.dart'; |
16 | 8 import 'package:front_end/src/scanner/errors.dart'; |
17 export 'package:analyzer/src/dart/error/syntactic_errors.dart'; | 9 import 'package:front_end/src/scanner/reader.dart'; |
| 10 import 'package:front_end/src/scanner/string_utilities.dart'; |
| 11 import 'package:front_end/src/scanner/token.dart'; |
18 | 12 |
19 /** | 13 /** |
20 * A state in a state machine used to scan keywords. | 14 * A state in a state machine used to scan keywords. |
21 */ | 15 */ |
22 class KeywordState { | 16 class KeywordState { |
23 /** | 17 /** |
24 * An empty transition table used by leaf states. | 18 * An empty transition table used by leaf states. |
25 */ | 19 */ |
26 static List<KeywordState> _EMPTY_TABLE = new List<KeywordState>(26); | 20 static List<KeywordState> _EMPTY_TABLE = new List<KeywordState>(26); |
27 | 21 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 /** | 119 /** |
126 * The class `Scanner` implements a scanner for Dart code. | 120 * The class `Scanner` implements a scanner for Dart code. |
127 * | 121 * |
128 * The lexical structure of Dart is ambiguous without knowledge of the context | 122 * The lexical structure of Dart is ambiguous without knowledge of the context |
129 * in which a token is being scanned. For example, without context we cannot | 123 * in which a token is being scanned. For example, without context we cannot |
130 * determine whether source of the form "<<" should be scanned as a single | 124 * determine whether source of the form "<<" should be scanned as a single |
131 * left-shift operator or as two left angle brackets. This scanner does not have | 125 * left-shift operator or as two left angle brackets. This scanner does not have |
132 * any context, so it always resolves such conflicts by scanning the longest | 126 * any context, so it always resolves such conflicts by scanning the longest |
133 * possible token. | 127 * possible token. |
134 */ | 128 */ |
135 class Scanner { | 129 abstract class Scanner { |
136 /** | |
137 * The source being scanned. | |
138 */ | |
139 final Source source; | |
140 | |
141 /** | 130 /** |
142 * The reader used to access the characters in the source. | 131 * The reader used to access the characters in the source. |
143 */ | 132 */ |
144 final CharacterReader _reader; | 133 final CharacterReader _reader; |
145 | 134 |
146 /** | 135 /** |
147 * The error listener that will be informed of any errors that are found | |
148 * during the scan. | |
149 */ | |
150 final AnalysisErrorListener _errorListener; | |
151 | |
152 /** | |
153 * The flag specifying whether documentation comments should be parsed. | 136 * The flag specifying whether documentation comments should be parsed. |
154 */ | 137 */ |
155 bool _preserveComments = true; | 138 bool _preserveComments = true; |
156 | 139 |
157 /** | 140 /** |
158 * The token pointing to the head of the linked list of tokens. | 141 * The token pointing to the head of the linked list of tokens. |
159 */ | 142 */ |
160 Token _tokens; | 143 Token _tokens; |
161 | 144 |
162 /** | 145 /** |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 bool scanGenericMethodComments = false; | 195 bool scanGenericMethodComments = false; |
213 | 196 |
214 /** | 197 /** |
215 * A flag indicating whether the lazy compound assignment operators '&&=' and | 198 * A flag indicating whether the lazy compound assignment operators '&&=' and |
216 * '||=' are enabled. | 199 * '||=' are enabled. |
217 */ | 200 */ |
218 bool scanLazyAssignmentOperators = false; | 201 bool scanLazyAssignmentOperators = false; |
219 | 202 |
220 /** | 203 /** |
221 * Initialize a newly created scanner to scan characters from the given | 204 * Initialize a newly created scanner to scan characters from the given |
222 * [source]. The given character [_reader] will be used to read the characters | 205 * character [_reader]. |
223 * in the source. The given [_errorListener] will be informed of any errors | |
224 * that are found. | |
225 */ | 206 */ |
226 Scanner(this.source, this._reader, this._errorListener) { | 207 Scanner(this._reader) { |
227 _tokens = new Token(TokenType.EOF, -1); | 208 _tokens = new Token(TokenType.EOF, -1); |
228 _tokens.setNext(_tokens); | 209 _tokens.setNext(_tokens); |
229 _tail = _tokens; | 210 _tail = _tokens; |
230 _tokenStart = -1; | 211 _tokenStart = -1; |
231 _lineStarts.add(0); | 212 _lineStarts.add(0); |
232 } | 213 } |
233 | 214 |
234 /** | 215 /** |
235 * Return the first token in the token stream that was scanned. | 216 * Return the first token in the token stream that was scanned. |
236 */ | 217 */ |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 } | 433 } |
453 | 434 |
454 /** | 435 /** |
455 * Record the fact that we are at the beginning of a new line in the source. | 436 * Record the fact that we are at the beginning of a new line in the source. |
456 */ | 437 */ |
457 void recordStartOfLine() { | 438 void recordStartOfLine() { |
458 _lineStarts.add(_reader.offset); | 439 _lineStarts.add(_reader.offset); |
459 } | 440 } |
460 | 441 |
461 /** | 442 /** |
| 443 * Report an error at the given offset. The [errorCode] is the error code |
| 444 * indicating the nature of the error. The [arguments] are any arguments |
| 445 * needed to complete the error message |
| 446 */ |
| 447 void reportError( |
| 448 ScannerErrorCode errorCode, int offset, List<Object> arguments); |
| 449 |
| 450 /** |
462 * Record that the source begins on the given [line] and [column] at the | 451 * Record that the source begins on the given [line] and [column] at the |
463 * current offset as given by the reader. Both the line and the column are | 452 * current offset as given by the reader. Both the line and the column are |
464 * one-based indexes. The line starts for lines before the given line will not | 453 * one-based indexes. The line starts for lines before the given line will not |
465 * be correct. | 454 * be correct. |
466 * | 455 * |
467 * This method must be invoked at most one time and must be invoked before | 456 * This method must be invoked at most one time and must be invoked before |
468 * scanning begins. The values provided must be sensible. The results are | 457 * scanning begins. The values provided must be sensible. The results are |
469 * undefined if these conditions are violated. | 458 * undefined if these conditions are violated. |
470 */ | 459 */ |
471 void setSourceStart(int line, int column) { | 460 void setSourceStart(int line, int column) { |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
670 } | 659 } |
671 return null; | 660 return null; |
672 } | 661 } |
673 | 662 |
674 /** | 663 /** |
675 * Report an error at the current offset. The [errorCode] is the error code | 664 * Report an error at the current offset. The [errorCode] is the error code |
676 * indicating the nature of the error. The [arguments] are any arguments | 665 * indicating the nature of the error. The [arguments] are any arguments |
677 * needed to complete the error message | 666 * needed to complete the error message |
678 */ | 667 */ |
679 void _reportError(ScannerErrorCode errorCode, [List<Object> arguments]) { | 668 void _reportError(ScannerErrorCode errorCode, [List<Object> arguments]) { |
680 _errorListener.onError( | 669 reportError(errorCode, _reader.offset, arguments); |
681 new AnalysisError(source, _reader.offset, 1, errorCode, arguments)); | |
682 } | 670 } |
683 | 671 |
684 int _select(int choice, TokenType yesType, TokenType noType) { | 672 int _select(int choice, TokenType yesType, TokenType noType) { |
685 int next = _reader.advance(); | 673 int next = _reader.advance(); |
686 if (next == choice) { | 674 if (next == choice) { |
687 _appendTokenOfType(yesType); | 675 _appendTokenOfType(yesType); |
688 return _reader.advance(); | 676 return _reader.advance(); |
689 } else { | 677 } else { |
690 _appendTokenOfType(noType); | 678 _appendTokenOfType(noType); |
691 return next; | 679 return next; |
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1346 } | 1334 } |
1347 | 1335 |
1348 /** | 1336 /** |
1349 * Checks if [value] is a single-line or multi-line comment. | 1337 * Checks if [value] is a single-line or multi-line comment. |
1350 */ | 1338 */ |
1351 static bool _isDocumentationComment(String value) { | 1339 static bool _isDocumentationComment(String value) { |
1352 return StringUtilities.startsWith3(value, 0, $slash, $slash, $slash) || | 1340 return StringUtilities.startsWith3(value, 0, $slash, $slash, $slash) || |
1353 StringUtilities.startsWith3(value, 0, $slash, $asterisk, $asterisk); | 1341 StringUtilities.startsWith3(value, 0, $slash, $asterisk, $asterisk); |
1354 } | 1342 } |
1355 } | 1343 } |
OLD | NEW |