| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 class ValidatorListener implements DiagnosticListener { | |
| 6 final SourceFile sourceFile; | |
| 7 | |
| 8 ValidatorListener(this.sourceFile); | |
| 9 | |
| 10 void cancel([String reason, node, token, instruction, element]) { | |
| 11 assert(token !== null); | |
| 12 SourceSpan.withCharacterOffsets(token, token, (beginOffset, endOffset) { | |
| 13 String errorMessage = | |
| 14 sourceFile.getLocationMessage(reason, beginOffset, endOffset, true, | |
| 15 (s) => s /* no color */); | |
| 16 print(errorMessage); | |
| 17 }); | |
| 18 throw new CompilerCancelledException(reason); | |
| 19 } | |
| 20 | |
| 21 void log(message) {} | |
| 22 } | |
| 23 | |
| 24 /** | |
| 25 * Checks result's of [Node] unparse. | |
| 26 */ | |
| 27 class UnparseValidator extends CompilerTask { | |
| 28 final bool validateUnparse; | |
| 29 | |
| 30 String get name() => "Unparse validator"; | |
| 31 | |
| 32 UnparseValidator(Compiler compiler, this.validateUnparse) : super(compiler); | |
| 33 | |
| 34 void checkFunction(PartialFunctionElement originalFunction) { | |
| 35 FunctionExpression originalNode = originalFunction.parseNode(compiler); | |
| 36 String unparsed = originalNode.unparse(); | |
| 37 | |
| 38 Token newTokens = new StringScanner(unparsed).tokenize(); | |
| 39 | |
| 40 // Find the getOrSet token. | |
| 41 // TODO(ahe): This is to frigging complicated. Simplify it. | |
| 42 Parser parser = new Parser(new Listener()); | |
| 43 Token getOrSet = | |
| 44 parser.findGetOrSet(parser.parseModifiers(newTokens)); | |
| 45 | |
| 46 // TODO(ahe): This is also too frigging complicated. | |
| 47 Script originalScript = originalFunction.getCompilationUnit().script; | |
| 48 String name = SourceSpan.withCharacterOffsets( | |
| 49 originalFunction.beginToken, originalFunction.endToken, | |
| 50 (beginOffset, endOffset) => | |
| 51 'synthesized:${originalScript.name}#$beginOffset:$endOffset'); | |
| 52 SourceFile synthesizedSourceFile = new SourceFile(name, unparsed); | |
| 53 Script synthesizedScript = | |
| 54 new Script(originalScript.uri, synthesizedSourceFile); | |
| 55 LibraryElement lib = new LibraryElement(synthesizedScript); | |
| 56 lib.canUseNative = originalFunction.getLibrary().canUseNative; | |
| 57 NodeListener listener = | |
| 58 new NodeListener(new ValidatorListener(synthesizedSourceFile), | |
| 59 lib.entryCompilationUnit); | |
| 60 parser = new Parser(listener); | |
| 61 parser.parseFunction(newTokens, getOrSet); | |
| 62 FunctionExpression newNode = listener.popNode(); | |
| 63 // TODO(antonm): add Node comparison. | |
| 64 } | |
| 65 | |
| 66 void check(Element element) { | |
| 67 if (!validateUnparse) return; | |
| 68 | |
| 69 if (element is PartialFunctionElement) { | |
| 70 checkFunction(element); | |
| 71 } else if (element.isGenerativeConstructor()) { | |
| 72 assert(element is FunctionElement); | |
| 73 // Generative constructors parse to very special function expressions. | |
| 74 // Handle them when classes are properly handled. | |
| 75 } else if (element.isField() || element is AbstractFieldElement) { | |
| 76 assert(element is VariableElement || element is AbstractFieldElement); | |
| 77 // Fields are just names with possible initialization expressions. | |
| 78 // Nothing to care about for now. | |
| 79 } else if (element is VoidElement) { | |
| 80 // Nothing to do here. | |
| 81 } else { | |
| 82 compiler.cancel('Cannot handle $element'); | |
| 83 } | |
| 84 } | |
| 85 } | |
| OLD | NEW |