OLD | NEW |
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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library dart_scanner; | 5 library dart_scanner; |
6 | 6 |
7 import 'src/token.dart' show | 7 import 'src/token.dart' show |
8 Token; | 8 Token; |
9 | 9 |
10 import 'src/utf8_bytes_scanner.dart' show | 10 import 'src/utf8_bytes_scanner.dart' show |
11 Utf8BytesScanner; | 11 Utf8BytesScanner; |
12 | 12 |
| 13 import 'src/recover.dart' show |
| 14 defaultRecoveryStrategy; |
| 15 |
13 export 'src/token.dart' show | 16 export 'src/token.dart' show |
14 BeginGroupToken, | 17 BeginGroupToken, |
15 ErrorToken, | 18 ErrorToken, |
16 KeywordToken, | 19 KeywordToken, |
17 StringToken, | 20 StringToken, |
18 SymbolToken, | 21 SymbolToken, |
19 Token, | 22 Token, |
20 isBinaryOperator, | 23 isBinaryOperator, |
21 isMinusOperator, | 24 isMinusOperator, |
22 isTernaryOperator, | 25 isTernaryOperator, |
23 isUnaryOperator, | 26 isUnaryOperator, |
24 isUserDefinableOperator; | 27 isUserDefinableOperator; |
25 | 28 |
26 export 'src/token_constants.dart' show | 29 export 'src/token_constants.dart' show |
27 EOF_TOKEN; | 30 EOF_TOKEN; |
28 | 31 |
29 export 'src/utf8_bytes_scanner.dart' show | 32 export 'src/utf8_bytes_scanner.dart' show |
30 Utf8BytesScanner; | 33 Utf8BytesScanner; |
31 | 34 |
32 export 'src/string_scanner.dart' show | 35 export 'src/string_scanner.dart' show |
33 StringScanner; | 36 StringScanner; |
34 | 37 |
35 export 'src/keyword.dart' show | 38 export 'src/keyword.dart' show |
36 Keyword; | 39 Keyword; |
37 | 40 |
| 41 typedef Token Recover(List<int> bytes, Token tokens, List<int> lineStarts); |
| 42 |
38 abstract class Scanner { | 43 abstract class Scanner { |
| 44 /// Returns true if an error occured during [tokenize]. |
| 45 bool get hasErrors; |
| 46 |
39 List<int> get lineStarts; | 47 List<int> get lineStarts; |
| 48 |
40 Token tokenize(); | 49 Token tokenize(); |
41 } | 50 } |
42 | 51 |
43 class ScannerResult { | 52 class ScannerResult { |
44 final Token tokens; | 53 final Token tokens; |
45 final List<int> lineStarts; | 54 final List<int> lineStarts; |
46 | 55 |
47 ScannerResult(this.tokens, this.lineStarts); | 56 ScannerResult(this.tokens, this.lineStarts); |
48 } | 57 } |
49 | 58 |
50 ScannerResult scan(List<int> bytes, {bool includeComments: false}) { | 59 ScannerResult scan(List<int> bytes, |
| 60 {bool includeComments: false, Recover recover}) { |
51 if (bytes.last != 0) { | 61 if (bytes.last != 0) { |
52 throw new ArgumentError("[bytes]: the last byte must be null."); | 62 throw new ArgumentError("[bytes]: the last byte must be null."); |
53 } | 63 } |
54 Scanner scanner = | 64 Scanner scanner = |
55 new Utf8BytesScanner(bytes, includeComments: includeComments); | 65 new Utf8BytesScanner(bytes, includeComments: includeComments); |
56 return new ScannerResult(scanner.tokenize(), scanner.lineStarts); | 66 Token tokens = scanner.tokenize(); |
| 67 if (scanner.hasErrors) { |
| 68 recover ??= defaultRecoveryStrategy; |
| 69 tokens = recover(bytes, tokens, scanner.lineStarts); |
| 70 } |
| 71 return new ScannerResult(tokens, scanner.lineStarts); |
57 } | 72 } |
OLD | NEW |