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

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.visit(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) {
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 sb.add(' '); 502 sb.add(' ');
496 } 503 }
497 visit(node.name); 504 visit(node.name);
498 if (node.typeParameters !== null) { 505 if (node.typeParameters !== null) {
499 visit(node.typeParameters); 506 visit(node.typeParameters);
500 } 507 }
501 visit(node.formals); 508 visit(node.formals);
502 add(node.endToken.value); 509 add(node.endToken.value);
503 } 510 }
504 } 511 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698