| 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"); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 return super.parseMember(token); | 139 return super.parseMember(token); |
| 140 } | 140 } |
| 141 Token patch = token; | 141 Token patch = token; |
| 142 patchListener.beginPatch(patch); | 142 patchListener.beginPatch(patch); |
| 143 token = super.parseMember(token.next); | 143 token = super.parseMember(token.next); |
| 144 patchListener.endPatch(patch); | 144 patchListener.endPatch(patch); |
| 145 return token; | 145 return token; |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 | 148 |
| 149 | |
| 150 /** | 149 /** |
| 151 * Partial parser for patch files that also handles the members of class | 150 * Partial parser for patch files that also handles the members of class |
| 152 * declarations. | 151 * declarations. |
| 153 */ | 152 */ |
| 154 class PatchClassElementParser extends PatchParser { | 153 class PatchClassElementParser extends PatchParser { |
| 155 PatchClassElementParser(PatchListener listener) : super(listener); | 154 PatchClassElementParser(PatchListener listener) : super(listener); |
| 156 | 155 |
| 157 Token parseClassBody(Token token) => fullParseClassBody(token); | 156 Token parseClassBody(Token token) => fullParseClassBody(token); |
| 158 } | 157 } |
| 159 | 158 |
| 160 | |
| 161 /** | 159 /** |
| 162 * Extension of [ElementListener] for parsing patch files. | 160 * Extension of [ElementListener] for parsing patch files. |
| 163 */ | 161 */ |
| 164 class PatchElementListener extends ElementListener implements PatchListener { | 162 class PatchElementListener extends ElementListener implements PatchListener { |
| 165 final LinkBuilder<tree.ScriptTag> imports; | 163 final LinkBuilder<tree.ScriptTag> imports; |
| 166 bool isMemberPatch = false; | 164 bool isMemberPatch = false; |
| 167 bool isClassPatch = false; | 165 bool isClassPatch = false; |
| 168 | 166 |
| 169 PatchElementListener(leg.DiagnosticListener listener, | 167 PatchElementListener(leg.DiagnosticListener listener, |
| 170 CompilationUnitElement patchElement, | 168 CompilationUnitElement patchElement, |
| 171 int idGenerator(), | 169 int idGenerator(), |
| 172 this.imports) | 170 this.imports) |
| 173 : super(listener, patchElement, idGenerator); | 171 : super(listener, patchElement, idGenerator); |
| 174 | 172 |
| 173 MetadataAnnotation popMetadata() { |
| 174 // TODO(ahe): Remove this method. |
| 175 popNode(); // Discard null. |
| 176 return new PatchMetadataAnnotation(); |
| 177 } |
| 178 |
| 175 void beginPatch(Token token) { | 179 void beginPatch(Token token) { |
| 176 if (token.next.stringValue === "class") { | 180 if (token.next.stringValue === "class") { |
| 177 isClassPatch = true; | 181 isClassPatch = true; |
| 178 } else { | 182 } else { |
| 179 isMemberPatch = true; | 183 isMemberPatch = true; |
| 180 } | 184 } |
| 181 handleIdentifier(token); | 185 handleIdentifier(token); |
| 182 } | 186 } |
| 183 | 187 |
| 184 void endPatch(Token token) { | 188 void endPatch(Token token) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 196 bool allowScriptTags() => true; | 200 bool allowScriptTags() => true; |
| 197 | 201 |
| 198 void addScriptTag(tree.ScriptTag tag) { | 202 void addScriptTag(tree.ScriptTag tag) { |
| 199 super.addScriptTag(tag); | 203 super.addScriptTag(tag); |
| 200 imports.addLast(tag); | 204 imports.addLast(tag); |
| 201 } | 205 } |
| 202 | 206 |
| 203 void pushElement(Element element) { | 207 void pushElement(Element element) { |
| 204 if (isMemberPatch || (isClassPatch && element is ClassElement)) { | 208 if (isMemberPatch || (isClassPatch && element is ClassElement)) { |
| 205 // Apply patch. | 209 // Apply patch. |
| 206 element.addMetadata(popNode()); | 210 element.addMetadata(popMetadata()); |
| 207 LibraryElement library = compilationUnitElement.getLibrary(); | 211 LibraryElement library = compilationUnitElement.getLibrary(); |
| 208 Element existing = library.localLookup(element.name); | 212 Element existing = library.localLookup(element.name); |
| 209 if (isMemberPatch) { | 213 if (isMemberPatch) { |
| 210 if (element is! FunctionElement) { | 214 if (element is! FunctionElement) { |
| 211 listener.internalErrorOnElement(element, | 215 listener.internalErrorOnElement(element, |
| 212 "Member patch is not a function."); | 216 "Member patch is not a function."); |
| 213 } | 217 } |
| 214 if (existing.kind === ElementKind.ABSTRACT_FIELD) { | 218 if (existing.kind === ElementKind.ABSTRACT_FIELD) { |
| 215 if (!element.isAccessor()) { | 219 if (!element.isAccessor()) { |
| 216 listener.internalErrorOnElement( | 220 listener.internalErrorOnElement( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 244 element, "Patching the same class more than once."); | 248 element, "Patching the same class more than once."); |
| 245 } | 249 } |
| 246 classElement.patch = element; | 250 classElement.patch = element; |
| 247 } | 251 } |
| 248 return; | 252 return; |
| 249 } | 253 } |
| 250 super.pushElement(element); | 254 super.pushElement(element); |
| 251 } | 255 } |
| 252 } | 256 } |
| 253 | 257 |
| 254 | |
| 255 /** | 258 /** |
| 256 * Extension of [MemberListener] for parsing patch class bodies. | 259 * Extension of [MemberListener] for parsing patch class bodies. |
| 257 */ | 260 */ |
| 258 class PatchMemberListener extends MemberListener implements PatchListener { | 261 class PatchMemberListener extends MemberListener implements PatchListener { |
| 259 bool isMemberPatch = false; | 262 bool isMemberPatch = false; |
| 260 bool isClassPatch = false; | 263 bool isClassPatch = false; |
| 261 PatchMemberListener(leg.DiagnosticListener listener, | 264 PatchMemberListener(leg.DiagnosticListener listener, |
| 262 Element enclosingElement) | 265 Element enclosingElement) |
| 263 : super(listener, enclosingElement); | 266 : super(listener, enclosingElement); |
| 264 | 267 |
| 268 MetadataAnnotation popMetadata() { |
| 269 // TODO(ahe): Remove this method. |
| 270 popNode(); // Discard null. |
| 271 return new PatchMetadataAnnotation(); |
| 272 } |
| 273 |
| 265 void beginPatch(Token token) { | 274 void beginPatch(Token token) { |
| 266 if (token.next.stringValue === "class") { | 275 if (token.next.stringValue === "class") { |
| 267 isClassPatch = true; | 276 isClassPatch = true; |
| 268 } else { | 277 } else { |
| 269 isMemberPatch = true; | 278 isMemberPatch = true; |
| 270 } | 279 } |
| 271 handleIdentifier(token); | 280 handleIdentifier(token); |
| 272 } | 281 } |
| 273 | 282 |
| 274 void endPatch(Token token) { | 283 void endPatch(Token token) { |
| 275 if (token.next.stringValue === "class") { | 284 if (token.next.stringValue === "class") { |
| 276 isClassPatch = false; | 285 isClassPatch = false; |
| 277 } else { | 286 } else { |
| 278 isMemberPatch = false; | 287 isMemberPatch = false; |
| 279 } | 288 } |
| 280 } | 289 } |
| 281 | 290 |
| 282 void addMember(Element element) { | 291 void addMember(Element element) { |
| 283 if (isMemberPatch || (isClassPatch && element is ClassElement)) { | 292 if (isMemberPatch || (isClassPatch && element is ClassElement)) { |
| 284 element.addMetadata(popNode()); | 293 element.addMetadata(popMetadata()); |
| 285 } | 294 } |
| 286 super.addMember(element); | 295 super.addMember(element); |
| 287 } | 296 } |
| 288 } | 297 } |
| 298 |
| 299 // TODO(ahe): Get rid of this class. |
| 300 class PatchMetadataAnnotation extends MetadataAnnotation { |
| 301 final Constant value = null; |
| 302 } |
| OLD | NEW |