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

Side by Side Diff: compiler/java/com/google/dart/compiler/ast/DartUnit.java

Issue 9598002: Use ASTVisitor as in new AST. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 package com.google.dart.compiler.ast; 5 package com.google.dart.compiler.ast;
6 6
7 import com.google.common.collect.Lists; 7 import com.google.common.collect.Lists;
8 import com.google.common.collect.Sets; 8 import com.google.common.collect.Sets;
9 import com.google.dart.compiler.DartSource; 9 import com.google.dart.compiler.DartSource;
10 import com.google.dart.compiler.util.DefaultTextOutput;
11 10
12 import java.util.ArrayList; 11 import java.util.ArrayList;
13 import java.util.Collections; 12 import java.util.Collections;
14 import java.util.List; 13 import java.util.List;
15 import java.util.Set; 14 import java.util.Set;
16 15
17 /** 16 /**
18 * Represents a Dart compilation unit. 17 * Represents a Dart compilation unit.
19 */ 18 */
20 public class DartUnit extends DartNode { 19 public class DartUnit extends DartNode {
21 20
22 private static final long serialVersionUID = -3407637869012712127L; 21 private static final long serialVersionUID = -3407637869012712127L;
23 22
24 private LibraryUnit library; 23 private LibraryUnit library;
25 private List<DartDirective> directives; 24 private List<DartDirective> directives;
26 private final List<DartNode> topLevelNodes = Lists.newArrayList(); 25 private final List<DartNode> topLevelNodes = Lists.newArrayList();
27 private final DartSource source; 26 private final DartSource source;
28 private final boolean isDiet; 27 private final boolean isDiet;
29 /** A list of comments. May be null. */ 28 /** A list of comments. May be null. */
30 private List<DartComment> comments; 29 private List<DartComment> comments;
31 private String dietParse;
32 30
33 public DartUnit(DartSource source, boolean isDiet) { 31 public DartUnit(DartSource source, boolean isDiet) {
34 this.source = source; 32 this.source = source;
35 this.isDiet = isDiet; 33 this.isDiet = isDiet;
36 } 34 }
37 35
38 public void addTopLevelNode(DartNode node) { 36 public void addTopLevelNode(DartNode node) {
39 topLevelNodes.add(becomeParentOf(node)); 37 topLevelNodes.add(becomeParentOf(node));
40 } 38 }
41 39
(...skipping 27 matching lines...) Expand all
69 67
70 public LibraryUnit getLibrary() { 68 public LibraryUnit getLibrary() {
71 return library; 69 return library;
72 } 70 }
73 71
74 public List<DartNode> getTopLevelNodes() { 72 public List<DartNode> getTopLevelNodes() {
75 return topLevelNodes; 73 return topLevelNodes;
76 } 74 }
77 75
78 @Override 76 @Override
79 public void traverse(DartVisitor v, DartContext ctx) { 77 public void visitChildren(ASTVisitor<?> visitor) {
80 if (v.visit(this, ctx)) {
81 if (directives != null) {
82 v.acceptWithInsertRemove(this, directives);
83 }
84 v.acceptWithInsertRemove(this, topLevelNodes);
85 if (comments != null) {
86 v.acceptWithInsertRemove(this, comments);
87 }
88 }
89 v.endVisit(this, ctx);
90 }
91
92 @Override
93 public void visitChildren(DartPlainVisitor<?> visitor) {
94 if (directives != null) { 78 if (directives != null) {
95 visitor.visit(directives); 79 visitor.visit(directives);
96 } 80 }
97 visitor.visit(topLevelNodes); 81 visitor.visit(topLevelNodes);
98 if (comments != null) { 82 if (comments != null) {
99 visitor.visit(comments); 83 visitor.visit(comments);
100 } 84 }
101 } 85 }
102 86
103 @Override 87 @Override
104 public <R> R accept(DartPlainVisitor<R> visitor) { 88 public <R> R accept(ASTVisitor<R> visitor) {
105 return visitor.visitUnit(this); 89 return visitor.visitUnit(this);
106 } 90 }
107 91
108 /** 92 /**
109 * Whether this is an diet unit, meaning it contains no method bodies. 93 * Whether this is an diet unit, meaning it contains no method bodies.
110 */ 94 */
111 public boolean isDiet() { 95 public boolean isDiet() {
112 return isDiet; 96 return isDiet;
113 } 97 }
114 98
115 /** 99 /**
116 * Generates a diet version of this unit, which contains no method bodies.
117 */
118 public final String toDietSource() {
119 if (dietParse == null) {
120 DefaultTextOutput out = new DefaultTextOutput(false);
121 new DartToSourceVisitor(out, true).accept(this);
122 dietParse = out.toString();
123 }
124 return dietParse;
125 }
126
127 /**
128 * Add the specified directive to the receiver's list of directives 100 * Add the specified directive to the receiver's list of directives
129 */ 101 */
130 public void addDirective(DartDirective directive) { 102 public void addDirective(DartDirective directive) {
131 if (directives == null) { 103 if (directives == null) {
132 directives = new ArrayList<DartDirective>(); 104 directives = new ArrayList<DartDirective>();
133 } 105 }
134 directives.add(becomeParentOf(directive)); 106 directives.add(becomeParentOf(directive));
135 } 107 }
136 108
137 /** 109 /**
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 } 145 }
174 } 146 }
175 return topLevelSymbols; 147 return topLevelSymbols;
176 } 148 }
177 149
178 /** 150 /**
179 * @return the {@link Set} of names of all declarations. 151 * @return the {@link Set} of names of all declarations.
180 */ 152 */
181 public Set<String> getDeclarationNames() { 153 public Set<String> getDeclarationNames() {
182 final Set<String> symbols = Sets.newHashSet(); 154 final Set<String> symbols = Sets.newHashSet();
183 accept(new DartNodeTraverser<Void>() { 155 accept(new ASTVisitor<Void>() {
184 @Override 156 @Override
185 public Void visitFunctionTypeAlias(DartFunctionTypeAlias node) { 157 public Void visitFunctionTypeAlias(DartFunctionTypeAlias node) {
186 symbols.add(node.getName().getTargetName()); 158 symbols.add(node.getName().getTargetName());
187 return super.visitFunctionTypeAlias(node); 159 return super.visitFunctionTypeAlias(node);
188 } 160 }
189 161
190 @Override 162 @Override
191 public Void visitClass(DartClass node) { 163 public Void visitClass(DartClass node) {
192 symbols.add(node.getClassName()); 164 symbols.add(node.getClassName());
193 return super.visitClass(node); 165 return super.visitClass(node);
(...skipping 27 matching lines...) Expand all
221 193
222 @Override 194 @Override
223 public Void visitVariable(DartVariable node) { 195 public Void visitVariable(DartVariable node) {
224 symbols.add(node.getVariableName()); 196 symbols.add(node.getVariableName());
225 return super.visitVariable(node); 197 return super.visitVariable(node);
226 } 198 }
227 }); 199 });
228 return symbols; 200 return symbols;
229 } 201 }
230 } 202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698