| 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.ScriptTag> imports = new LinkBuilder<tree.ScriptTag>(); | 29 LinkBuilder<tree.LibraryTag> imports = new LinkBuilder<tree.LibraryTag>(); |
| 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.ScriptTag tag in imports.toLink()) { | 39 for (tree.LibraryTag 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.ScriptTag> imports) { | 46 LinkBuilder<tree.LibraryTag> 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); |
| 57 new PatchParser(patchListener).parseUnit(tokens); | 57 new PatchParser(patchListener).parseUnit(tokens); |
| 58 }); | 58 }); |
| 59 } | 59 } |
| 60 | 60 |
| 61 tree.ClassNode parsePatchClassNode(PartialClassElement element) { | 61 tree.ClassNode parsePatchClassNode(PartialClassElement element) { |
| 62 // Parse [PartialClassElement] using a "patch"-aware parser instead | 62 return measure(() => compiler.withCurrentElement(element, () { |
| 63 // of calling its [parseNode] method. | 63 // Parse [PartialClassElement] using a "patch"-aware parser instead |
| 64 if (element.cachedNode != null) return element.cachedNode; | 64 // of calling its [parseNode] method. |
| 65 PatchMemberListener listener = | 65 if (element.cachedNode != null) return element.cachedNode; |
| 66 new PatchMemberListener(compiler, element); | 66 PatchMemberListener listener = |
| 67 Parser parser = new PatchClassElementParser(listener); | 67 new PatchMemberListener(compiler, element); |
| 68 Token token = parser.parseTopLevelDeclaration(element.beginToken); | 68 Parser parser = new PatchClassElementParser(listener); |
| 69 assert(token === element.endToken.next); | 69 Token token = parser.parseTopLevelDeclaration(element.beginToken); |
| 70 element.cachedNode = listener.popNode(); | 70 assert(token === element.endToken.next); |
| 71 assert(listener.nodes.isEmpty()); | 71 element.cachedNode = listener.popNode(); |
| 72 return element.cachedNode; | 72 assert(listener.nodes.isEmpty()); |
| 73 return element.cachedNode; |
| 74 })); |
| 73 } | 75 } |
| 74 } | 76 } |
| 75 | 77 |
| 76 /** | 78 /** |
| 77 * Extension of the [Listener] interface to handle the extra "patch" pseudo- | 79 * Extension of the [Listener] interface to handle the extra "patch" pseudo- |
| 78 * keyword in patch files. | 80 * keyword in patch files. |
| 79 * Patch files shouldn't have a type named "patch". | 81 * Patch files shouldn't have a type named "patch". |
| 80 */ | 82 */ |
| 81 abstract class PatchListener extends Listener { | 83 abstract class PatchListener extends Listener { |
| 82 void beginPatch(Token patch); | 84 void beginPatch(Token patch); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 class PatchClassElementParser extends PatchParser { | 150 class PatchClassElementParser extends PatchParser { |
| 149 PatchClassElementParser(PatchListener listener) : super(listener); | 151 PatchClassElementParser(PatchListener listener) : super(listener); |
| 150 | 152 |
| 151 Token parseClassBody(Token token) => fullParseClassBody(token); | 153 Token parseClassBody(Token token) => fullParseClassBody(token); |
| 152 } | 154 } |
| 153 | 155 |
| 154 /** | 156 /** |
| 155 * Extension of [ElementListener] for parsing patch files. | 157 * Extension of [ElementListener] for parsing patch files. |
| 156 */ | 158 */ |
| 157 class PatchElementListener extends ElementListener implements PatchListener { | 159 class PatchElementListener extends ElementListener implements PatchListener { |
| 158 final LinkBuilder<tree.ScriptTag> imports; | 160 final LinkBuilder<tree.LibraryTag> imports; |
| 159 bool isMemberPatch = false; | 161 bool isMemberPatch = false; |
| 160 bool isClassPatch = false; | 162 bool isClassPatch = false; |
| 161 | 163 |
| 162 PatchElementListener(leg.DiagnosticListener listener, | 164 PatchElementListener(leg.DiagnosticListener listener, |
| 163 CompilationUnitElement patchElement, | 165 CompilationUnitElement patchElement, |
| 164 int idGenerator(), | 166 int idGenerator(), |
| 165 this.imports) | 167 this.imports) |
| 166 : super(listener, patchElement, idGenerator); | 168 : super(listener, patchElement, idGenerator); |
| 167 | 169 |
| 168 MetadataAnnotation popMetadata() { | 170 MetadataAnnotation popMetadata() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 185 isClassPatch = false; | 187 isClassPatch = false; |
| 186 } else { | 188 } else { |
| 187 isMemberPatch = false; | 189 isMemberPatch = false; |
| 188 } | 190 } |
| 189 } | 191 } |
| 190 | 192 |
| 191 /** | 193 /** |
| 192 * Allow script tags (import only, the parser rejects the rest for now) in | 194 * 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. | 195 * patch files. The import tags will be added to the library. |
| 194 */ | 196 */ |
| 195 bool allowScriptTags() => true; | 197 bool allowLibraryTags() => true; |
| 196 | 198 |
| 197 void addScriptTag(tree.ScriptTag tag) { | 199 void addLibraryTag(tree.LibraryTag tag) { |
| 198 super.addScriptTag(tag); | 200 super.addLibraryTag(tag); |
| 199 imports.addLast(tag); | 201 imports.addLast(tag); |
| 200 } | 202 } |
| 201 | 203 |
| 202 void pushElement(Element element) { | 204 void pushElement(Element element) { |
| 203 if (isMemberPatch || (isClassPatch && element is ClassElement)) { | 205 if (isMemberPatch || (isClassPatch && element is ClassElement)) { |
| 204 // Apply patch. | 206 // Apply patch. |
| 205 element.addMetadata(popMetadata()); | 207 element.addMetadata(popMetadata()); |
| 206 LibraryElement library = compilationUnitElement.getLibrary(); | 208 LibraryElement library = compilationUnitElement.getLibrary(); |
| 207 Element existing = library.localLookup(element.name); | 209 Element existing = library.localLookup(element.name); |
| 208 if (isMemberPatch) { | 210 if (isMemberPatch) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 super.addMember(element); | 292 super.addMember(element); |
| 291 } | 293 } |
| 292 } | 294 } |
| 293 | 295 |
| 294 // TODO(ahe): Get rid of this class. | 296 // TODO(ahe): Get rid of this class. |
| 295 class PatchMetadataAnnotation extends MetadataAnnotation { | 297 class PatchMetadataAnnotation extends MetadataAnnotation { |
| 296 final leg.Constant value = null; | 298 final leg.Constant value = null; |
| 297 | 299 |
| 298 PatchMetadataAnnotation() : super(STATE_DONE); | 300 PatchMetadataAnnotation() : super(STATE_DONE); |
| 299 } | 301 } |
| OLD | NEW |