Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(833)

Side by Side Diff: lib/compiler/implementation/tree/unparser.dart

Issue 10919033: Rework Unparser/unparse StringBuffer management. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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) {
9 Unparser unparser = new Unparser();
10 unparser.unparse(node);
11 return unparser.result;
12 }
13
8 class Unparser implements Visitor { 14 class Unparser implements Visitor {
9 Renamer rename; 15 Renamer rename;
10 StringBuffer sb; 16 final StringBuffer sb;
11 17
12 Unparser() { 18 String get result => sb.toString();
19
20 Unparser() : sb = new StringBuffer() {
13 // TODO(smok): Move this to initializer once dart2js stops complaining 21 // TODO(smok): Move this to initializer once dart2js stops complaining
14 // about closures in initializers. 22 // about closures in initializers.
15 rename = (Node node) => null; 23 rename = (Node node) => null;
16 } 24 }
17 Unparser.withRenamer(this.rename); 25 Unparser.withRenamer(this.rename) : sb = new StringBuffer();
18 26
19 String unparse(Node node) { 27 // TODO(antonm): will go away soon.
20 sb = new StringBuffer(); 28 void addString(String s) {
21 visit(node); 29 sb.add(s);
22 return sb.toString();
23 } 30 }
24 31
25 void add(SourceString string) { 32 void add(SourceString string) {
26 string.printOn(sb); 33 string.printOn(sb);
27 } 34 }
28 35
29 void addToken(Token token) { 36 void addToken(Token token) {
30 if (token === null) return; 37 if (token === null) return;
31 add(token.value); 38 add(token.value);
32 if (token.kind === KEYWORD_TOKEN || token.kind === IDENTIFIER_TOKEN) { 39 if (token.kind === KEYWORD_TOKEN || token.kind === IDENTIFIER_TOKEN) {
33 sb.add(' '); 40 sb.add(' ');
34 } 41 }
35 } 42 }
36 43
44 unparse(Node node) { visit(node); }
45
37 visit(Node node) { 46 visit(Node node) {
38 if (node === null) return; 47 if (node === null) return;
39 String renamed = rename(node); 48 String renamed = rename(node);
40 if (renamed !== null) { 49 if (renamed !== null) {
41 sb.add(renamed); 50 sb.add(renamed);
42 } else { 51 } else {
43 // Fallback. 52 // Fallback.
44 node.accept(this); 53 node.accept(this);
45 } 54 }
46 } 55 }
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 sb.add(' '); 504 sb.add(' ');
496 } 505 }
497 visit(node.name); 506 visit(node.name);
498 if (node.typeParameters !== null) { 507 if (node.typeParameters !== null) {
499 visit(node.typeParameters); 508 visit(node.typeParameters);
500 } 509 }
501 visit(node.formals); 510 visit(node.formals);
502 add(node.endToken.value); 511 add(node.endToken.value);
503 } 512 }
504 } 513 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698