| 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('native_handler.dart', prefix: 'native'); | 13 #import('native_handler.dart', prefix: 'native'); |
| 14 | 14 |
| 15 |
| 15 class PatchParserTask extends leg.CompilerTask { | 16 class PatchParserTask extends leg.CompilerTask { |
| 16 PatchParserTask(leg.Compiler compiler): super(compiler); | 17 PatchParserTask(leg.Compiler compiler): super(compiler); |
| 17 final String name = "Patching Parser"; | 18 final String name = "Patching Parser"; |
| 18 | 19 |
| 19 LibraryElement loadPatchLibrary(Uri uri) { | 20 LibraryElement loadPatchLibrary(Uri uri) { |
| 20 bool newLibrary = false; | 21 bool newLibrary = false; |
| 21 LibraryElement library = | 22 LibraryElement library = |
| 22 compiler.libraries.putIfAbsent(uri.toString(), () { | 23 compiler.libraries.putIfAbsent(uri.toString(), () { |
| 23 newLibrary = true; | 24 newLibrary = true; |
| 24 leg.Script script = compiler.readScript(uri, null); | 25 leg.Script script = compiler.readScript(uri, null); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 measure(() { | 41 measure(() { |
| 41 // TODO(lrn): Possibly recursively handle #source directives in patch. | 42 // TODO(lrn): Possibly recursively handle #source directives in patch. |
| 42 leg.Script script = library.script; | 43 leg.Script script = library.script; |
| 43 Token tokens = new StringScanner(script.text).tokenize(); | 44 Token tokens = new StringScanner(script.text).tokenize(); |
| 44 Function idGenerator = compiler.getNextFreeClassId; | 45 Function idGenerator = compiler.getNextFreeClassId; |
| 45 PatchListener patchListener = | 46 PatchListener patchListener = |
| 46 new PatchElementListener(compiler, library, idGenerator); | 47 new PatchElementListener(compiler, library, idGenerator); |
| 47 new PatchParser(patchListener).parseUnit(tokens); | 48 new PatchParser(patchListener).parseUnit(tokens); |
| 48 }); | 49 }); |
| 49 } | 50 } |
| 51 |
| 52 tree.ClassNode parsePatchClassNode(PartialClassElement element) { |
| 53 // Parse [PartialClassElement] using a "patch"-aware parser instead |
| 54 // of calling its [parseNode] method. |
| 55 if (element.cachedNode != null) return element.cachedNode; |
| 56 PatchMemberListener listener = |
| 57 new PatchMemberListener(compiler, element); |
| 58 Parser parser = new PatchClassElementParser(listener); |
| 59 Token token = parser.parseTopLevelDeclaration(element.beginToken); |
| 60 assert(token === element.endToken.next); |
| 61 element.cachedNode = listener.popNode(); |
| 62 assert(listener.nodes.isEmpty()); |
| 63 return element.cachedNode; |
| 64 } |
| 50 } | 65 } |
| 51 | 66 |
| 52 /** | 67 /** |
| 53 * Extension of the [Listener] interface to handle the extra "patch" pseudo- | 68 * Extension of the [Listener] interface to handle the extra "patch" pseudo- |
| 54 * keyword in patch files. | 69 * keyword in patch files. |
| 55 * Patch files shouldn't have a type named "patch". | 70 * Patch files shouldn't have a type named "patch". |
| 56 */ | 71 */ |
| 57 interface PatchListener extends Listener { | 72 interface PatchListener extends Listener { |
| 58 void beginPatch(Token patch); | 73 void beginPatch(Token patch); |
| 59 void endPatch(Token patch); | 74 void endPatch(Token patch); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 86 String value = token.stringValue; | 101 String value = token.stringValue; |
| 87 if (value === 'interface' | 102 if (value === 'interface' |
| 88 || value === 'typedef' | 103 || value === 'typedef' |
| 89 || value === '#' | 104 || value === '#' |
| 90 || value === 'abstract') { | 105 || value === 'abstract') { |
| 91 // At the top level, you can only patch functions and classes. | 106 // At the top level, you can only patch functions and classes. |
| 92 // Patch classes and functions can't be marked abstract. | 107 // Patch classes and functions can't be marked abstract. |
| 93 return listener.unexpected(patch); | 108 return listener.unexpected(patch); |
| 94 } | 109 } |
| 95 patchListener.beginPatch(patch); | 110 patchListener.beginPatch(patch); |
| 96 token = super.parseTopLevelDeclaration(token.next); | 111 token = super.parseTopLevelDeclaration(token); |
| 97 patchListener.endPatch(patch); | 112 patchListener.endPatch(patch); |
| 98 return token; | 113 return token; |
| 99 } | 114 } |
| 100 | 115 |
| 101 /** | 116 /** |
| 102 * Parse a class member. | 117 * Parse a class member. |
| 103 * If the member starts with "patch", it's a member override. | 118 * If the member starts with "patch", it's a member override. |
| 104 * Only methods can be overridden, including constructors, getters and | 119 * Only methods can be overridden, including constructors, getters and |
| 105 * setters, but not fields. If "patch" occurs in front of a field, the error | 120 * setters, but not fields. If "patch" occurs in front of a field, the error |
| 106 * is caught elsewhere. | 121 * is caught elsewhere. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 172 } |
| 158 } | 173 } |
| 159 | 174 |
| 160 void pushElement(Element element) { | 175 void pushElement(Element element) { |
| 161 if (isMemberPatch || (isClassPatch && element is ClassElement)) { | 176 if (isMemberPatch || (isClassPatch && element is ClassElement)) { |
| 162 element.addMetadata(popNode()); | 177 element.addMetadata(popNode()); |
| 163 } | 178 } |
| 164 super.pushElement(element); | 179 super.pushElement(element); |
| 165 } | 180 } |
| 166 } | 181 } |
| 182 |
| 183 |
| 184 /** |
| 185 * Extension of [MemberListener] for parsing patch class bodies. |
| 186 */ |
| 187 class PatchMemberListener extends MemberListener implements PatchListener { |
| 188 bool isMemberPatch = false; |
| 189 bool isClassPatch = false; |
| 190 PatchMemberListener(leg.DiagnosticListener listener, |
| 191 Element enclosingElement) |
| 192 : super(listener, enclosingElement); |
| 193 |
| 194 void beginPatch(Token token) { |
| 195 if (token.next.stringValue === "class") { |
| 196 isClassPatch = true; |
| 197 } else { |
| 198 isMemberPatch = true; |
| 199 } |
| 200 handleIdentifier(token); |
| 201 } |
| 202 |
| 203 void endPatch(Token token) { |
| 204 if (token.next.stringValue === "class") { |
| 205 isClassPatch = false; |
| 206 } else { |
| 207 isMemberPatch = false; |
| 208 } |
| 209 } |
| 210 |
| 211 void addMember(Element element) { |
| 212 if (isMemberPatch || (isClassPatch && element is ClassElement)) { |
| 213 element.addMetadata(popNode()); |
| 214 } |
| 215 super.addMember(element); |
| 216 } |
| 217 } |
| OLD | NEW |