OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * This library contains the infrastructure to parse and integrate patch files. | 6 * This library contains the infrastructure to parse and integrate patch files. |
7 * | 7 * |
8 * Three types of elements can be patched: [LibraryElement], [ClassElement], | 8 * Three types of elements can be patched: [LibraryElement], [ClassElement], |
9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded | 9 * [FunctionElement]. Patches are introduced in patch libraries which are loaded |
10 * together with the corresponding origin library. Which libraries that are | 10 * together with the corresponding origin library. Which libraries that are |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 ParserError; | 142 ParserError; |
143 import 'parser/element_listener.dart' show | 143 import 'parser/element_listener.dart' show |
144 ElementListener; | 144 ElementListener; |
145 import 'parser/member_listener.dart' show | 145 import 'parser/member_listener.dart' show |
146 MemberListener; | 146 MemberListener; |
147 import 'parser/partial_elements.dart' show | 147 import 'parser/partial_elements.dart' show |
148 PartialClassElement; | 148 PartialClassElement; |
149 import 'parser/partial_parser.dart' show | 149 import 'parser/partial_parser.dart' show |
150 PartialParser; | 150 PartialParser; |
151 import 'parser/parser.dart' show | 151 import 'parser/parser.dart' show |
152 Parser; | 152 Parser, |
| 153 ParserOptions; |
153 import 'scanner/scanner.dart' show | 154 import 'scanner/scanner.dart' show |
154 Scanner; | 155 Scanner; |
155 import 'script.dart'; | 156 import 'script.dart'; |
156 import 'tokens/token.dart' show | 157 import 'tokens/token.dart' show |
157 StringToken, | 158 StringToken, |
158 Token; | 159 Token; |
159 | 160 |
160 class PatchParserTask extends CompilerTask { | 161 class PatchParserTask extends CompilerTask { |
161 final String name = "Patching Parser"; | 162 final String name = "Patching Parser"; |
162 final bool _enableConditionalDirectives; | 163 final ParserOptions parserOptions; |
163 | 164 |
164 PatchParserTask(Compiler compiler, {bool enableConditionalDirectives}) | 165 PatchParserTask(Compiler compiler, this.parserOptions) : super(compiler); |
165 : this._enableConditionalDirectives = enableConditionalDirectives, | |
166 super(compiler); | |
167 | 166 |
168 /** | 167 /** |
169 * Scans a library patch file, applies the method patches and | 168 * Scans a library patch file, applies the method patches and |
170 * injections to the library, and returns a list of class | 169 * injections to the library, and returns a list of class |
171 * patches. | 170 * patches. |
172 */ | 171 */ |
173 Future patchLibrary(LibraryLoader loader, | 172 Future patchLibrary(LibraryLoader loader, |
174 Uri patchUri, LibraryElement originLibrary) { | 173 Uri patchUri, LibraryElement originLibrary) { |
175 return compiler.readScript(originLibrary, patchUri) | 174 return compiler.readScript(originLibrary, patchUri) |
176 .then((Script script) { | 175 .then((Script script) { |
(...skipping 14 matching lines...) Expand all Loading... |
191 void scanLibraryElements(CompilationUnitElement compilationUnit) { | 190 void scanLibraryElements(CompilationUnitElement compilationUnit) { |
192 measure(() { | 191 measure(() { |
193 // TODO(johnniwinther): Test that parts and exports are handled correctly. | 192 // TODO(johnniwinther): Test that parts and exports are handled correctly. |
194 Script script = compilationUnit.script; | 193 Script script = compilationUnit.script; |
195 Token tokens = new Scanner(script.file).tokenize(); | 194 Token tokens = new Scanner(script.file).tokenize(); |
196 Function idGenerator = compiler.getNextFreeClassId; | 195 Function idGenerator = compiler.getNextFreeClassId; |
197 Listener patchListener = new PatchElementListener(compiler, | 196 Listener patchListener = new PatchElementListener(compiler, |
198 compilationUnit, | 197 compilationUnit, |
199 idGenerator); | 198 idGenerator); |
200 try { | 199 try { |
201 new PartialParser(patchListener, | 200 new PartialParser(patchListener, parserOptions).parseUnit(tokens); |
202 enableConditionalDirectives: _enableConditionalDirectives) | |
203 .parseUnit(tokens); | |
204 } on ParserError catch (e) { | 201 } on ParserError catch (e) { |
205 // No need to recover from a parser error in platform libraries, user | 202 // No need to recover from a parser error in platform libraries, user |
206 // will never see this if the libraries are tested correctly. | 203 // will never see this if the libraries are tested correctly. |
207 reporter.internalError( | 204 reporter.internalError( |
208 compilationUnit, "Parser error in patch file: $e"); | 205 compilationUnit, "Parser error in patch file: $e"); |
209 } | 206 } |
210 }); | 207 }); |
211 } | 208 } |
212 | 209 |
213 void parsePatchClassNode(PartialClassElement cls) { | 210 void parsePatchClassNode(PartialClassElement cls) { |
214 // Parse [PartialClassElement] using a "patch"-aware parser instead | 211 // Parse [PartialClassElement] using a "patch"-aware parser instead |
215 // of calling its [parseNode] method. | 212 // of calling its [parseNode] method. |
216 if (cls.cachedNode != null) return; | 213 if (cls.cachedNode != null) return; |
217 | 214 |
218 measure(() => reporter.withCurrentElement(cls, () { | 215 measure(() => reporter.withCurrentElement(cls, () { |
219 MemberListener listener = new PatchMemberListener(compiler, cls); | 216 MemberListener listener = new PatchMemberListener(compiler, cls); |
220 Parser parser = new PatchClassElementParser( | 217 Parser parser = new PatchClassElementParser(listener, parserOptions); |
221 listener, enableConditionalDirectives: _enableConditionalDirectives); | |
222 try { | 218 try { |
223 Token token = parser.parseTopLevelDeclaration(cls.beginToken); | 219 Token token = parser.parseTopLevelDeclaration(cls.beginToken); |
224 assert(identical(token, cls.endToken.next)); | 220 assert(identical(token, cls.endToken.next)); |
225 } on ParserError catch (e) { | 221 } on ParserError catch (e) { |
226 // No need to recover from a parser error in platform libraries, user | 222 // No need to recover from a parser error in platform libraries, user |
227 // will never see this if the libraries are tested correctly. | 223 // will never see this if the libraries are tested correctly. |
228 reporter.internalError( | 224 reporter.internalError( |
229 cls, "Parser error in patch file: $e"); | 225 cls, "Parser error in patch file: $e"); |
230 } | 226 } |
231 cls.cachedNode = listener.popNode(); | 227 cls.cachedNode = listener.popNode(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 enclosingClass.addMember(patch, reporter); | 259 enclosingClass.addMember(patch, reporter); |
264 } | 260 } |
265 } | 261 } |
266 } | 262 } |
267 | 263 |
268 /** | 264 /** |
269 * Partial parser for patch files that also handles the members of class | 265 * Partial parser for patch files that also handles the members of class |
270 * declarations. | 266 * declarations. |
271 */ | 267 */ |
272 class PatchClassElementParser extends PartialParser { | 268 class PatchClassElementParser extends PartialParser { |
273 PatchClassElementParser(Listener listener, {bool enableConditionalDirectives}) | 269 PatchClassElementParser(Listener listener, ParserOptions options) |
274 : super(listener, | 270 : super(listener, options); |
275 enableConditionalDirectives: enableConditionalDirectives); | |
276 | 271 |
277 Token parseClassBody(Token token) => fullParseClassBody(token); | 272 Token parseClassBody(Token token) => fullParseClassBody(token); |
278 } | 273 } |
279 | 274 |
280 /** | 275 /** |
281 * Extension of [ElementListener] for parsing patch files. | 276 * Extension of [ElementListener] for parsing patch files. |
282 */ | 277 */ |
283 class PatchElementListener extends ElementListener implements Listener { | 278 class PatchElementListener extends ElementListener implements Listener { |
284 final Compiler compiler; | 279 final Compiler compiler; |
285 | 280 |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 | 691 |
697 class PatchVersion { | 692 class PatchVersion { |
698 final String tag; | 693 final String tag; |
699 | 694 |
700 const PatchVersion(this.tag); | 695 const PatchVersion(this.tag); |
701 | 696 |
702 bool isActive(String patchTag) => tag == null || tag == patchTag; | 697 bool isActive(String patchTag) => tag == null || tag == patchTag; |
703 | 698 |
704 String toString() => 'PatchVersion($tag)'; | 699 String toString() => 'PatchVersion($tag)'; |
705 } | 700 } |
OLD | NEW |