Chromium Code Reviews| Index: lib/compiler/implementation/unparse_validator.dart |
| diff --git a/lib/compiler/implementation/unparse_validator.dart b/lib/compiler/implementation/unparse_validator.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b2f8f0b4d6ca1eff877728699b0180d4d6c5b5c |
| --- /dev/null |
| +++ b/lib/compiler/implementation/unparse_validator.dart |
| @@ -0,0 +1,60 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * Checks result's of [Node] unparse. |
| + */ |
| +class UnparseValidator extends CompilerTask { |
| + final bool validateUnparse; |
| + |
| + UnparseValidator(Compiler compiler, this.validateUnparse) : super(compiler); |
| + |
| + void check(Element element) { |
| + if (!validateUnparse) { |
|
ahe
2012/05/22 08:49:04
Just one line:
if (!validateUnparse) return;
Anton Muhin
2012/05/22 15:34:32
Done.
|
| + return; |
| + } |
| + |
| + if (element is! PartialFunctionElement) { |
|
ahe
2012/05/22 08:49:04
Ditto.
Anton Muhin
2012/05/22 15:34:32
Done.
|
| + // TODO(antonm): consider supporting other kinds of elements. |
| + return; |
| + } |
| + |
| + PartialFunctionElement originalFunction = element; |
| + FunctionExpression originalNode = originalFunction.parseNode(compiler); |
| + String unparsed = originalNode.unparse(false); |
| + |
| + Token newTokens = new StringScanner(unparsed).tokenize(); |
| + |
| + // Find the getOrSet token. |
| + // TODO(ahe): This is to frigging complicated. Simplify it. |
|
ahe
2012/05/22 08:49:04
It's great that you copied this verbatim :-)
Anton Muhin
2012/05/22 15:34:32
:)
|
| + Parser parser = new Parser(new Listener()); |
| + Token getOrSet = |
| + parser.findGetOrSet(parser.parseModifiers(newTokens)); |
| + |
| + // TODO(ahe): This is also too frigging complicated. |
| + Script originalScript = element.getCompilationUnit().script; |
| + SourceFile synthesizedSourceFile = |
| + new SourceFile(originalScript.name, unparsed); |
| + Script synthesizedScript = |
| + new Script(originalScript.uri, synthesizedSourceFile); |
| + LibraryElement lib = new LibraryElement(synthesizedScript); |
| + NodeListener listener = new NodeListener(compiler, lib); |
| + parser = new Parser(listener); |
| + final oldHandler = compiler.handler; |
|
Anton Muhin
2012/05/21 19:14:39
this trick is sketchy, but allows nice diagnostics
ahe
2012/05/22 08:49:04
I don't understand why you have to do this. How is
Anton Muhin
2012/05/22 08:56:32
The old handler will lookup SourceFile via uri and
ahe
2012/05/22 09:04:45
The source file should be found in the "currentEle
Anton Muhin
2012/05/22 15:34:32
As far as I understand it goes like this:
we get
ahe
2012/05/22 15:50:56
Shoot. I forgot about the compiler API. It would b
Anton Muhin
2012/05/22 19:16:57
I am sorry, I've already submitted w/o it. Sendin
|
| + compiler.handler = (uri, begin, end, message, fatal) { |
| + final errorMessage = |
| + synthesizedSourceFile.getLocationMessage(message, begin, end, true); |
| + print('$errorMessage'); |
| + }; |
| + try { |
| + compiler.withCurrentElement(lib, () { |
| + parser.parseFunction(newTokens, getOrSet); |
| + }); |
| + } finally { |
| + compiler.handler = oldHandler; |
| + } |
| + FunctionExpression newNode = listener.popNode(); |
| + // TODO(antonm): add Node comparison. |
| + } |
| +} |