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