| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 /// Listener used in combination with `TopLevelParser` to extract the URIs of | 5 /// Listener used in combination with `TopLevelParser` to extract the URIs of |
| 6 /// import, part, and export directives. | 6 /// import, part, and export directives. |
| 7 library front_end.src.fasta.source.directive_listener; | 7 library front_end.src.fasta.source.directive_listener; |
| 8 | 8 |
| 9 import '../fasta_codes.dart' show FastaMessage, codeExpectedBlockToSkip; | 9 import '../fasta_codes.dart' show FastaMessage, codeExpectedBlockToSkip; |
| 10 import '../parser/dart_vm_native.dart' show skipNativeClause; | |
| 11 import '../parser/listener.dart'; | 10 import '../parser/listener.dart'; |
| 12 import '../quote.dart'; | 11 import '../quote.dart'; |
| 13 import '../scanner/token.dart'; | 12 import '../scanner/token.dart'; |
| 14 | 13 |
| 15 /// Listener that records the URIs from imports, exports, and part directives. | 14 /// Listener that records the URIs from imports, exports, and part directives. |
| 16 /// | 15 /// |
| 17 /// This is normally used in combination with the `TopLevelParser`, which skips | 16 /// This is normally used in combination with the `TopLevelParser`, which skips |
| 18 /// over the body of declarations like classes and function that are irrelevant | 17 /// over the body of declarations like classes and function that are irrelevant |
| 19 /// for directives. Note that on correct programs directives cannot occur after | 18 /// for directives. Note that on correct programs directives cannot occur after |
| 20 /// any top-level declaration, but we recommend to continue parsing the entire | 19 /// any top-level declaration, but we recommend to continue parsing the entire |
| 21 /// file in order to gracefully handle input errors. | 20 /// file in order to gracefully handle input errors. |
| 22 class DirectiveListener extends Listener { | 21 class DirectiveListener extends Listener { |
| 23 /// Whether we accept the native-syntax used by the VM patch files. | |
| 24 final bool acceptsNativeClause; | |
| 25 | |
| 26 /// Collects URIs that occur on any import directive. | 22 /// Collects URIs that occur on any import directive. |
| 27 final Set<String> imports = new Set<String>(); | 23 final Set<String> imports = new Set<String>(); |
| 28 | 24 |
| 29 /// Collects URIs that occur on any export directive. | 25 /// Collects URIs that occur on any export directive. |
| 30 final Set<String> exports = new Set<String>(); | 26 final Set<String> exports = new Set<String>(); |
| 31 | 27 |
| 32 /// Collects URIs that occur on any part directive. | 28 /// Collects URIs that occur on any part directive. |
| 33 final Set<String> parts = new Set<String>(); | 29 final Set<String> parts = new Set<String>(); |
| 34 | 30 |
| 35 DirectiveListener({this.acceptsNativeClause: false}); | 31 DirectiveListener(); |
| 36 | 32 |
| 37 /// Set when entering the context of a directive, null when the parser is not | 33 /// Set when entering the context of a directive, null when the parser is not |
| 38 /// looking at a directive. | 34 /// looking at a directive. |
| 39 Set<String> _current = null; | 35 Set<String> _current = null; |
| 40 | 36 |
| 41 bool get _inDirective => _current != null; | 37 bool get _inDirective => _current != null; |
| 42 | 38 |
| 43 @override | 39 @override |
| 44 beginImport(_) { | 40 beginImport(_) { |
| 45 _current = imports; | 41 _current = imports; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 72 | 68 |
| 73 @override | 69 @override |
| 74 void beginLiteralString(Token token) { | 70 void beginLiteralString(Token token) { |
| 75 if (_inDirective) { | 71 if (_inDirective) { |
| 76 _current.add(unescapeString(token.lexeme)); | 72 _current.add(unescapeString(token.lexeme)); |
| 77 } | 73 } |
| 78 } | 74 } |
| 79 | 75 |
| 80 @override | 76 @override |
| 81 Token handleUnrecoverableError(Token token, FastaMessage message) { | 77 Token handleUnrecoverableError(Token token, FastaMessage message) { |
| 82 if (acceptsNativeClause && message.code == codeExpectedBlockToSkip) { | 78 if (message.code == codeExpectedBlockToSkip) { |
| 83 Token recover = skipNativeClause(token); | 79 Token recover = handleNativeClause(token); |
| 84 if (recover != null) return recover; | 80 if (recover != null) return recover; |
| 85 } | 81 } |
| 86 return super.handleUnrecoverableError(token, message); | 82 return super.handleUnrecoverableError(token, message); |
| 87 } | 83 } |
| 84 |
| 85 /// Defines how native clauses are handled. By default, they are not handled |
| 86 /// and an error is thrown; |
| 87 Token handleNativeClause(Token token) => null; |
| 88 } | 88 } |
| OLD | NEW |