Chromium Code Reviews| OLD | NEW |
|---|---|
| 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.dart.compiler.common.GenerateSourceMap; | 8 import com.google.dart.compiler.common.GenerateSourceMap; |
| 9 import com.google.dart.compiler.common.HasSourceInfo; | 9 import com.google.dart.compiler.common.HasSourceInfo; |
| 10 import com.google.dart.compiler.common.SourceMapping; | 10 import com.google.dart.compiler.common.SourceMapping; |
| 11 import com.google.dart.compiler.util.TextOutput; | 11 import com.google.dart.compiler.util.TextOutput; |
| 12 import com.google.debugging.sourcemap.FilePosition; | 12 import com.google.debugging.sourcemap.FilePosition; |
| 13 | 13 |
| 14 import java.io.IOException; | 14 import java.io.IOException; |
| 15 import java.util.ArrayList; | |
| 15 import java.util.Iterator; | 16 import java.util.Iterator; |
| 16 import java.util.List; | 17 import java.util.List; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * Used by {@link DartNode} to generate Dart source from an AST subtree. | 20 * Used by {@link DartNode} to generate Dart source from an AST subtree. |
| 20 */ | 21 */ |
| 21 public class DartToSourceVisitor extends DartVisitor { | 22 public class DartToSourceVisitor extends DartVisitor { |
| 22 | 23 |
| 23 private final TextOutput out; | 24 private final TextOutput out; |
| 24 private boolean buildMappings; | 25 private boolean buildMappings; |
| 25 private List<SourceMapping> mappings = Lists.newArrayList(); | 26 private List<SourceMapping> mappings = Lists.newArrayList(); |
| 26 private final boolean isDiet; | 27 private final boolean isDiet; |
| 27 | 28 |
| 29 private final boolean calculateHash; | |
|
jbrosenberg
2011/10/11 14:58:44
Much cleaner!
| |
| 30 | |
| 28 public DartToSourceVisitor(TextOutput out) { | 31 public DartToSourceVisitor(TextOutput out) { |
| 29 this(out, false); | 32 this(out, false); |
| 30 } | 33 } |
| 31 | 34 |
| 32 public DartToSourceVisitor(TextOutput out, boolean isDiet) { | 35 public DartToSourceVisitor(TextOutput out, boolean isDiet) { |
| 33 this.out = out; | 36 this.out = out; |
| 34 this.isDiet = isDiet; | 37 this.isDiet = isDiet; |
| 38 this.calculateHash = false; | |
| 35 } | 39 } |
| 36 | 40 |
| 41 public DartToSourceVisitor(TextOutput out, boolean isDiet, boolean calculateHa sh) { | |
| 42 this.out = out; | |
| 43 this.isDiet = isDiet; | |
| 44 this.calculateHash = calculateHash; | |
| 45 } | |
| 46 | |
| 37 public void generateSourceMap(boolean generate) { | 47 public void generateSourceMap(boolean generate) { |
| 38 this.buildMappings = generate; | 48 this.buildMappings = generate; |
| 39 } | 49 } |
| 40 | 50 |
| 41 public void writeSourceMap(Appendable out, String name) throws IOException { | 51 public void writeSourceMap(Appendable out, String name) throws IOException { |
| 42 GenerateSourceMap generator = new GenerateSourceMap(); | 52 GenerateSourceMap generator = new GenerateSourceMap(); |
| 43 for (SourceMapping m : mappings) { | 53 for (SourceMapping m : mappings) { |
| 44 generator.addMapping(m.getNode(), m.getStart(), m.getEnd()); | 54 generator.addMapping(m.getNode(), m.getStart(), m.getEnd()); |
| 45 } | 55 } |
| 46 generator.appendTo(out, name); | 56 generator.appendTo(out, name); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 p(">"); | 108 p(">"); |
| 99 } | 109 } |
| 100 } | 110 } |
| 101 | 111 |
| 102 @Override | 112 @Override |
| 103 public boolean visit(DartFunctionTypeAlias x, DartContext ctx) { | 113 public boolean visit(DartFunctionTypeAlias x, DartContext ctx) { |
| 104 p("typedef "); | 114 p("typedef "); |
| 105 | 115 |
| 106 if (x.getReturnTypeNode() != null) { | 116 if (x.getReturnTypeNode() != null) { |
| 107 accept(x.getReturnTypeNode()); | 117 accept(x.getReturnTypeNode()); |
| 108 } else { | |
| 109 p("function "); | |
| 110 } | 118 } |
| 111 | 119 |
| 112 p(" "); | 120 p(" "); |
| 113 accept(x.getName()); | 121 accept(x.getName()); |
| 114 pTypeParameters(x.getTypeParameters()); | 122 pTypeParameters(x.getTypeParameters()); |
| 115 | 123 |
| 116 p("("); | 124 p("("); |
| 117 printSeparatedByComma(x.getParameters()); | 125 printSeparatedByComma(x.getParameters()); |
| 118 p(")"); | 126 p(")"); |
| 119 | 127 |
| 120 p(";"); | 128 p(";"); |
| 121 nl(); | 129 nl(); |
| 122 nl(); | 130 nl(); |
| 123 return false; | 131 return false; |
| 124 } | 132 } |
| 125 | 133 |
| 126 @Override | 134 @Override |
| 127 public boolean visit(DartClass x, DartContext ctx) { | 135 public boolean visit(DartClass x, DartContext ctx) { |
| 136 int start = 0; | |
| 137 if (calculateHash == true) { | |
| 138 start = out.getPosition(); | |
| 139 } | |
| 140 | |
| 128 if (x.isInterface()) { | 141 if (x.isInterface()) { |
| 129 p("interface "); | 142 p("interface "); |
| 130 } else { | 143 } else { |
| 131 p("class "); | 144 p("class "); |
| 132 } | 145 } |
| 133 accept(x.getName()); | 146 accept(x.getName()); |
| 134 pTypeParameters(x.getTypeParameters()); | 147 pTypeParameters(x.getTypeParameters()); |
| 135 | 148 |
| 136 if (x.getSuperclass() != null) { | 149 if (x.getSuperclass() != null) { |
| 137 p(" extends "); | 150 p(" extends "); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 166 } | 179 } |
| 167 | 180 |
| 168 p(" {"); | 181 p(" {"); |
| 169 nl(); | 182 nl(); |
| 170 indent(); | 183 indent(); |
| 171 | 184 |
| 172 acceptList(x.getMembers()); | 185 acceptList(x.getMembers()); |
| 173 | 186 |
| 174 outdent(); | 187 outdent(); |
| 175 p("}"); | 188 p("}"); |
| 189 if (calculateHash == true) { | |
|
jbrosenberg
2011/10/11 14:58:44
Should this appear after the 2 nl(); calls? Just
codefu
2011/10/11 18:43:55
This emulates the work of older code that called t
jbrosenberg
2011/10/11 21:40:13
But I think we still have calls coming into this m
codefu
2011/10/11 22:07:18
DartNode.java:
return out.toString().trim().hash
| |
| 190 x.setHash(out.toString().substring(start, out.getPosition()).hashCode()); | |
| 191 } | |
| 176 nl(); | 192 nl(); |
| 177 nl(); | 193 nl(); |
| 178 return false; | 194 return false; |
| 179 } | 195 } |
| 180 | 196 |
| 181 @Override | 197 @Override |
| 182 public boolean visit(DartTypeNode x, DartContext ctx) { | 198 public boolean visit(DartTypeNode x, DartContext ctx) { |
| 183 accept(x.getIdentifier()); | 199 accept(x.getIdentifier()); |
| 184 List<DartTypeNode> arguments = x.getTypeArguments(); | 200 List<DartTypeNode> arguments = x.getTypeArguments(); |
| 185 if (arguments != null && !arguments.isEmpty()) { | 201 if (arguments != null && !arguments.isEmpty()) { |
| (...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 757 } | 773 } |
| 758 | 774 |
| 759 @Override | 775 @Override |
| 760 public boolean visit(DartFunctionExpression x, DartContext ctx) { | 776 public boolean visit(DartFunctionExpression x, DartContext ctx) { |
| 761 DartFunction func = x.getFunction(); | 777 DartFunction func = x.getFunction(); |
| 762 if (func.getReturnTypeNode() != null) { | 778 if (func.getReturnTypeNode() != null) { |
| 763 accept(func.getReturnTypeNode()); | 779 accept(func.getReturnTypeNode()); |
| 764 p(" "); | 780 p(" "); |
| 765 } | 781 } |
| 766 DartIdentifier name = x.getName(); | 782 DartIdentifier name = x.getName(); |
| 767 if (name != null) { | |
| 768 if (func.getReturnTypeNode() == null) { | |
| 769 p("function "); | |
| 770 } | |
| 771 } else { | |
| 772 p("function"); | |
| 773 } | |
| 774 pFunctionDeclaration(name, x.getFunction()); | 783 pFunctionDeclaration(name, x.getFunction()); |
| 775 p(" "); | 784 p(" "); |
| 776 if (x.getFunction().getBody() != null) { | 785 if (x.getFunction().getBody() != null) { |
| 777 pBlock(x.getFunction().getBody(), false); | 786 pBlock(x.getFunction().getBody(), false); |
| 778 } | 787 } |
| 779 return false; | 788 return false; |
| 780 } | 789 } |
| 781 | 790 |
| 782 @Override | 791 @Override |
| 783 public boolean visit(DartIdentifier x, DartContext ctx) { | 792 public boolean visit(DartIdentifier x, DartContext ctx) { |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 983 } | 992 } |
| 984 | 993 |
| 985 private void indent() { | 994 private void indent() { |
| 986 out.indentIn(); | 995 out.indentIn(); |
| 987 } | 996 } |
| 988 | 997 |
| 989 private void outdent() { | 998 private void outdent() { |
| 990 out.indentOut(); | 999 out.indentOut(); |
| 991 } | 1000 } |
| 992 } | 1001 } |
| OLD | NEW |