| 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 // Returns null if no need to rename a node. | 5 // Returns null if no need to rename a node. |
| 6 typedef String Renamer(Node node); | 6 typedef String Renamer(Node node); |
| 7 | 7 |
| 8 String unparse(Node node) { | 8 String unparse(Node node) { |
| 9 Unparser unparser = new Unparser(); | 9 Unparser unparser = new Unparser(); |
| 10 unparser.unparse(node); | 10 unparser.unparse(node); |
| 11 return unparser.result; | 11 return unparser.result; |
| 12 } | 12 } |
| 13 | 13 |
| 14 class Unparser implements Visitor { | 14 class Unparser implements Visitor { |
| 15 Renamer rename; | 15 Renamer rename; |
| 16 final StringBuffer sb; | 16 final StringBuffer sb; |
| 17 | 17 |
| 18 String get result => sb.toString(); | 18 String get result => sb.toString(); |
| 19 | 19 |
| 20 Unparser() : sb = new StringBuffer() { | 20 Unparser() : sb = new StringBuffer() { |
| 21 // TODO(smok): Move this to initializer once dart2js stops complaining | 21 // TODO(smok): Move this to initializer once dart2js stops complaining |
| 22 // about closures in initializers. | 22 // about closures in initializers. |
| 23 rename = (Node node) => null; | 23 rename = (Node node) => null; |
| 24 } | 24 } |
| 25 Unparser.withRenamer(this.rename) : sb = new StringBuffer(); | 25 Unparser.withRenamer(this.rename) : sb = new StringBuffer(); |
| 26 | 26 |
| 27 // TODO(antonm): will go away soon. | |
| 28 void addString(String s) { | |
| 29 sb.add(s); | |
| 30 } | |
| 31 | |
| 32 void add(SourceString string) { | 27 void add(SourceString string) { |
| 33 string.printOn(sb); | 28 string.printOn(sb); |
| 34 } | 29 } |
| 35 | 30 |
| 36 void addToken(Token token) { | 31 void addToken(Token token) { |
| 37 if (token === null) return; | 32 if (token === null) return; |
| 38 add(token.value); | 33 add(token.value); |
| 39 if (token.kind === KEYWORD_TOKEN || token.kind === IDENTIFIER_TOKEN) { | 34 if (token.kind === KEYWORD_TOKEN || token.kind === IDENTIFIER_TOKEN) { |
| 40 sb.add(' '); | 35 sb.add(' '); |
| 41 } | 36 } |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 } | 456 } |
| 462 | 457 |
| 463 visitSwitchCase(SwitchCase node) { | 458 visitSwitchCase(SwitchCase node) { |
| 464 visit(node.labelsAndCases); | 459 visit(node.labelsAndCases); |
| 465 if (node.isDefaultCase) { | 460 if (node.isDefaultCase) { |
| 466 sb.add('default:'); | 461 sb.add('default:'); |
| 467 } | 462 } |
| 468 visit(node.statements); | 463 visit(node.statements); |
| 469 } | 464 } |
| 470 | 465 |
| 466 unparseImportTag(String uri, [String prefix]) { |
| 467 final suffix = prefix === null ? '' : ',prefix:"$prefix"'; |
| 468 sb.add('#import("$uri"$suffix);'); |
| 469 } |
| 470 |
| 471 visitScriptTag(ScriptTag node) { | 471 visitScriptTag(ScriptTag node) { |
| 472 add(node.beginToken.value); | 472 add(node.beginToken.value); |
| 473 visit(node.tag); | 473 visit(node.tag); |
| 474 sb.add('('); | 474 sb.add('('); |
| 475 visit(node.argument); | 475 visit(node.argument); |
| 476 if (node.prefixIdentifier !== null) { | 476 if (node.prefixIdentifier !== null) { |
| 477 visit(node.prefixIdentifier); | 477 visit(node.prefixIdentifier); |
| 478 sb.add(':'); | 478 sb.add(':'); |
| 479 visit(node.prefix); | 479 visit(node.prefix); |
| 480 } | 480 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 sb.add(' '); | 517 sb.add(' '); |
| 518 } | 518 } |
| 519 visit(node.name); | 519 visit(node.name); |
| 520 if (node.typeParameters !== null) { | 520 if (node.typeParameters !== null) { |
| 521 visit(node.typeParameters); | 521 visit(node.typeParameters); |
| 522 } | 522 } |
| 523 visit(node.formals); | 523 visit(node.formals); |
| 524 add(node.endToken.value); | 524 add(node.endToken.value); |
| 525 } | 525 } |
| 526 } | 526 } |
| OLD | NEW |