| 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 #library("patchparser"); | 5 #library("patchparser"); |
| 6 #import("dart:uri"); | 6 #import("dart:uri"); |
| 7 | 7 |
| 8 #import("tree/tree.dart", prefix: "tree"); | 8 #import("tree/tree.dart", prefix: "tree"); |
| 9 #import("leg.dart", prefix: 'leg'); // CompilerTask, Compiler. | 9 #import("leg.dart", prefix: 'leg'); // CompilerTask, Compiler. |
| 10 #import("apiimpl.dart"); | 10 #import("apiimpl.dart"); |
| 11 #import("scanner/scannerlib.dart"); // Scanner, Parsers, Listeners | 11 #import("scanner/scannerlib.dart"); // Scanner, Parsers, Listeners |
| 12 #import("elements/elements.dart"); | 12 #import("elements/elements.dart"); |
| 13 #import('util/util.dart'); | 13 #import('util/util.dart'); |
| 14 | 14 |
| 15 class PatchParserTask extends leg.CompilerTask { | 15 class PatchParserTask extends leg.CompilerTask { |
| 16 PatchParserTask(leg.Compiler compiler): super(compiler); | 16 PatchParserTask(leg.Compiler compiler): super(compiler); |
| 17 final String name = "Patching Parser"; | 17 final String name = "Patching Parser"; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Scans a library patch file, applies the method patches and | 20 * Scans a library patch file, applies the method patches and |
| 21 * injections to the library, and returns a list of class | 21 * injections to the library, and returns a list of class |
| 22 * patches. | 22 * patches. |
| 23 */ | 23 */ |
| 24 void patchLibrary(Uri patchUri, LibraryElement library) { | 24 void patchLibrary(Uri patchUri, LibraryElement library) { |
| 25 leg.Script script = compiler.readScript(patchUri, null); | 25 leg.Script script = compiler.readScript(patchUri, null); |
| 26 CompilationUnitElement compilationUnit = | 26 CompilationUnitElement compilationUnit = |
| 27 new CompilationUnitElement(script, library); | 27 new CompilationUnitElement(script, library); |
| 28 library.addCompilationUnit(compilationUnit); | 28 library.addCompilationUnit(compilationUnit); |
| 29 LinkBuilder<tree.LibraryTag> imports = new LinkBuilder<tree.LibraryTag>(); | 29 LinkBuilder<tree.ScriptTag> imports = new LinkBuilder<tree.ScriptTag>(); |
| 30 compiler.withCurrentElement(compilationUnit, () { | 30 compiler.withCurrentElement(compilationUnit, () { |
| 31 // This patches the elements of the patch library into [library]. | 31 // This patches the elements of the patch library into [library]. |
| 32 // Injected elements are added directly under the compilation unit. | 32 // Injected elements are added directly under the compilation unit. |
| 33 // Patch elements are stored on the patched functions or classes. | 33 // Patch elements are stored on the patched functions or classes. |
| 34 scanLibraryElements(compilationUnit, imports); | 34 scanLibraryElements(compilationUnit, imports); |
| 35 }); | 35 }); |
| 36 // After scanning declarations, we handle the import tags in the patch. | 36 // After scanning declarations, we handle the import tags in the patch. |
| 37 // TODO(lrn): These imports end up in the original library and are in | 37 // TODO(lrn): These imports end up in the original library and are in |
| 38 // scope for the original methods too. This should be fixed. | 38 // scope for the original methods too. This should be fixed. |
| 39 for (tree.LibraryTag tag in imports.toLink()) { | 39 for (tree.ScriptTag tag in imports.toLink()) { |
| 40 compiler.scanner.importLibraryFromTag(tag, compilationUnit); | 40 compiler.scanner.importLibraryFromTag(tag, compilationUnit); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 void scanLibraryElements( | 44 void scanLibraryElements( |
| 45 CompilationUnitElement compilationUnit, | 45 CompilationUnitElement compilationUnit, |
| 46 LinkBuilder<tree.LibraryTag> imports) { | 46 LinkBuilder<tree.ScriptTag> imports) { |
| 47 measure(() { | 47 measure(() { |
| 48 // TODO(lrn): Possibly recursively handle #source directives in patch. | 48 // TODO(lrn): Possibly recursively handle #source directives in patch. |
| 49 leg.Script script = compilationUnit.script; | 49 leg.Script script = compilationUnit.script; |
| 50 Token tokens = new StringScanner(script.text).tokenize(); | 50 Token tokens = new StringScanner(script.text).tokenize(); |
| 51 Function idGenerator = compiler.getNextFreeClassId; | 51 Function idGenerator = compiler.getNextFreeClassId; |
| 52 PatchListener patchListener = | 52 PatchListener patchListener = |
| 53 new PatchElementListener(compiler, | 53 new PatchElementListener(compiler, |
| 54 compilationUnit, | 54 compilationUnit, |
| 55 idGenerator, | 55 idGenerator, |
| 56 imports); | 56 imports); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 class PatchClassElementParser extends PatchParser { | 148 class PatchClassElementParser extends PatchParser { |
| 149 PatchClassElementParser(PatchListener listener) : super(listener); | 149 PatchClassElementParser(PatchListener listener) : super(listener); |
| 150 | 150 |
| 151 Token parseClassBody(Token token) => fullParseClassBody(token); | 151 Token parseClassBody(Token token) => fullParseClassBody(token); |
| 152 } | 152 } |
| 153 | 153 |
| 154 /** | 154 /** |
| 155 * Extension of [ElementListener] for parsing patch files. | 155 * Extension of [ElementListener] for parsing patch files. |
| 156 */ | 156 */ |
| 157 class PatchElementListener extends ElementListener implements PatchListener { | 157 class PatchElementListener extends ElementListener implements PatchListener { |
| 158 final LinkBuilder<tree.LibraryTag> imports; | 158 final LinkBuilder<tree.ScriptTag> imports; |
| 159 bool isMemberPatch = false; | 159 bool isMemberPatch = false; |
| 160 bool isClassPatch = false; | 160 bool isClassPatch = false; |
| 161 | 161 |
| 162 PatchElementListener(leg.DiagnosticListener listener, | 162 PatchElementListener(leg.DiagnosticListener listener, |
| 163 CompilationUnitElement patchElement, | 163 CompilationUnitElement patchElement, |
| 164 int idGenerator(), | 164 int idGenerator(), |
| 165 this.imports) | 165 this.imports) |
| 166 : super(listener, patchElement, idGenerator); | 166 : super(listener, patchElement, idGenerator); |
| 167 | 167 |
| 168 MetadataAnnotation popMetadata() { | 168 MetadataAnnotation popMetadata() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 185 isClassPatch = false; | 185 isClassPatch = false; |
| 186 } else { | 186 } else { |
| 187 isMemberPatch = false; | 187 isMemberPatch = false; |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 | 190 |
| 191 /** | 191 /** |
| 192 * Allow script tags (import only, the parser rejects the rest for now) in | 192 * Allow script tags (import only, the parser rejects the rest for now) in |
| 193 * patch files. The import tags will be added to the library. | 193 * patch files. The import tags will be added to the library. |
| 194 */ | 194 */ |
| 195 bool allowLibraryTags() => true; | 195 bool allowScriptTags() => true; |
| 196 | 196 |
| 197 void addLibraryTag(tree.LibraryTag tag) { | 197 void addScriptTag(tree.ScriptTag tag) { |
| 198 super.addLibraryTag(tag); | 198 super.addScriptTag(tag); |
| 199 imports.addLast(tag); | 199 imports.addLast(tag); |
| 200 } | 200 } |
| 201 | 201 |
| 202 void pushElement(Element element) { | 202 void pushElement(Element element) { |
| 203 if (isMemberPatch || (isClassPatch && element is ClassElement)) { | 203 if (isMemberPatch || (isClassPatch && element is ClassElement)) { |
| 204 // Apply patch. | 204 // Apply patch. |
| 205 element.addMetadata(popMetadata()); | 205 element.addMetadata(popMetadata()); |
| 206 LibraryElement library = compilationUnitElement.getLibrary(); | 206 LibraryElement library = compilationUnitElement.getLibrary(); |
| 207 Element existing = library.localLookup(element.name); | 207 Element existing = library.localLookup(element.name); |
| 208 if (isMemberPatch) { | 208 if (isMemberPatch) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 super.addMember(element); | 290 super.addMember(element); |
| 291 } | 291 } |
| 292 } | 292 } |
| 293 | 293 |
| 294 // TODO(ahe): Get rid of this class. | 294 // TODO(ahe): Get rid of this class. |
| 295 class PatchMetadataAnnotation extends MetadataAnnotation { | 295 class PatchMetadataAnnotation extends MetadataAnnotation { |
| 296 final leg.Constant value = null; | 296 final leg.Constant value = null; |
| 297 | 297 |
| 298 PatchMetadataAnnotation() : super(STATE_DONE); | 298 PatchMetadataAnnotation() : super(STATE_DONE); |
| 299 } | 299 } |
| OLD | NEW |