| 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 fasta.scanner; | 5 library fasta.scanner; |
| 6 | 6 |
| 7 import 'dart:convert' show UNICODE_REPLACEMENT_CHARACTER_RUNE; | 7 import 'dart:convert' show UNICODE_REPLACEMENT_CHARACTER_RUNE; |
| 8 | 8 |
| 9 import 'scanner/token.dart' show Token; | 9 import 'scanner/token.dart' show Token; |
| 10 | 10 |
| 11 import 'scanner/string_scanner.dart' show StringScanner; |
| 12 |
| 11 import 'scanner/utf8_bytes_scanner.dart' show Utf8BytesScanner; | 13 import 'scanner/utf8_bytes_scanner.dart' show Utf8BytesScanner; |
| 12 | 14 |
| 13 import 'scanner/recover.dart' show defaultRecoveryStrategy; | 15 import 'scanner/recover.dart' show defaultRecoveryStrategy; |
| 14 | 16 |
| 15 export 'scanner/token.dart' | 17 export 'scanner/token.dart' |
| 16 show | 18 show |
| 17 BeginGroupToken, | 19 BeginGroupToken, |
| 18 KeywordToken, | 20 KeywordToken, |
| 19 StringToken, | 21 StringToken, |
| 20 SymbolToken, | 22 SymbolToken, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 45 bool get hasErrors; | 47 bool get hasErrors; |
| 46 | 48 |
| 47 List<int> get lineStarts; | 49 List<int> get lineStarts; |
| 48 | 50 |
| 49 Token tokenize(); | 51 Token tokenize(); |
| 50 } | 52 } |
| 51 | 53 |
| 52 class ScannerResult { | 54 class ScannerResult { |
| 53 final Token tokens; | 55 final Token tokens; |
| 54 final List<int> lineStarts; | 56 final List<int> lineStarts; |
| 57 final bool hasErrors; |
| 55 | 58 |
| 56 ScannerResult(this.tokens, this.lineStarts); | 59 ScannerResult(this.tokens, this.lineStarts, this.hasErrors); |
| 57 } | 60 } |
| 58 | 61 |
| 62 /// Scan/tokenize the given UTF8 [bytes]. |
| 63 /// If [recover] is null, then the [defaultRecoveryStrategy] is used. |
| 59 ScannerResult scan(List<int> bytes, | 64 ScannerResult scan(List<int> bytes, |
| 60 {bool includeComments: false, Recover recover}) { | 65 {bool includeComments: false, Recover recover}) { |
| 61 if (bytes.last != 0) { | 66 if (bytes.last != 0) { |
| 62 throw new ArgumentError("[bytes]: the last byte must be null."); | 67 throw new ArgumentError("[bytes]: the last byte must be null."); |
| 63 } | 68 } |
| 64 Scanner scanner = | 69 Scanner scanner = |
| 65 new Utf8BytesScanner(bytes, includeComments: includeComments); | 70 new Utf8BytesScanner(bytes, includeComments: includeComments); |
| 71 return _tokenizeAndRecover(scanner, recover, bytes); |
| 72 } |
| 73 |
| 74 /// Scan/tokenize the given [source]. |
| 75 /// If [recover] is null, then the [defaultRecoveryStrategy] is used. |
| 76 ScannerResult scanString(String source, |
| 77 {bool includeComments: false, Recover recover}) { |
| 78 assert(source != null, 'source must not be null'); |
| 79 StringScanner scanner = |
| 80 new StringScanner(source, includeComments: includeComments); |
| 81 // TODO(danrubel): what should be passed for the "bytes" argument? |
| 82 // does the Recover API need to be adjusted for a String source? |
| 83 return _tokenizeAndRecover(scanner, recover, null); |
| 84 } |
| 85 |
| 86 ScannerResult _tokenizeAndRecover( |
| 87 Scanner scanner, Recover recover, List<int> bytes) { |
| 66 Token tokens = scanner.tokenize(); | 88 Token tokens = scanner.tokenize(); |
| 67 if (scanner.hasErrors) { | 89 if (scanner.hasErrors) { |
| 68 recover ??= defaultRecoveryStrategy; | 90 recover ??= defaultRecoveryStrategy; |
| 69 tokens = recover(bytes, tokens, scanner.lineStarts); | 91 tokens = recover(bytes, tokens, scanner.lineStarts); |
| 70 } | 92 } |
| 71 return new ScannerResult(tokens, scanner.lineStarts); | 93 return new ScannerResult(tokens, scanner.lineStarts, scanner.hasErrors); |
| 72 } | 94 } |
| OLD | NEW |