OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 library kernel.ast_to_text; | 4 library kernel.ast_to_text; |
5 | 5 |
6 import '../ast.dart'; | 6 import '../ast.dart'; |
7 import '../import_table.dart'; | 7 import '../import_table.dart'; |
8 | 8 |
9 class Namer<T> { | 9 class Namer<T> { |
10 int index = 0; | 10 int index = 0; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 return node.name ?? syntheticNames.nameClass(node); | 203 return node.name ?? syntheticNames.nameClass(node); |
204 } | 204 } |
205 | 205 |
206 String getClassReference(Class node) { | 206 String getClassReference(Class node) { |
207 if (node == null) return '<No Class>'; | 207 if (node == null) return '<No Class>'; |
208 String name = getClassName(node); | 208 String name = getClassName(node); |
209 String library = getLibraryReference(node.enclosingLibrary); | 209 String library = getLibraryReference(node.enclosingLibrary); |
210 return '$library::$name'; | 210 return '$library::$name'; |
211 } | 211 } |
212 | 212 |
| 213 String getTypedefReference(Typedef node) { |
| 214 if (node == null) return '<No Typedef>'; |
| 215 String library = getLibraryReference(node.enclosingLibrary); |
| 216 return '$library::${node.name}'; |
| 217 } |
| 218 |
213 static final String emptyNameString = '•'; | 219 static final String emptyNameString = '•'; |
214 static final Name emptyName = new Name(emptyNameString); | 220 static final Name emptyName = new Name(emptyNameString); |
215 | 221 |
216 Name getMemberName(Member node) { | 222 Name getMemberName(Member node) { |
217 if (node.name?.name == '') return emptyName; | 223 if (node.name?.name == '') return emptyName; |
218 if (node.name != null) return node.name; | 224 if (node.name != null) return node.name; |
219 return new Name(syntheticNames.nameMember(node)); | 225 return new Name(syntheticNames.nameMember(node)); |
220 } | 226 } |
221 | 227 |
222 String getMemberReference(Member node) { | 228 String getMemberReference(Member node) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 } else { | 280 } else { |
275 var prefix = syntheticNames.nameLibraryPrefix(library); | 281 var prefix = syntheticNames.nameLibraryPrefix(library); |
276 endLine('import "$importPath" as $prefix;'); | 282 endLine('import "$importPath" as $prefix;'); |
277 } | 283 } |
278 } | 284 } |
279 for (var import in library.deferredImports) { | 285 for (var import in library.deferredImports) { |
280 import.accept(this); | 286 import.accept(this); |
281 } | 287 } |
282 endLine(); | 288 endLine(); |
283 var inner = new Printer._inner(this, imports); | 289 var inner = new Printer._inner(this, imports); |
| 290 library.typedefs.forEach(inner.writeNode); |
284 library.classes.forEach(inner.writeNode); | 291 library.classes.forEach(inner.writeNode); |
285 library.fields.forEach(inner.writeNode); | 292 library.fields.forEach(inner.writeNode); |
286 library.procedures.forEach(inner.writeNode); | 293 library.procedures.forEach(inner.writeNode); |
287 } | 294 } |
288 | 295 |
289 void writeProgramFile(Program program) { | 296 void writeProgramFile(Program program) { |
290 ImportTable imports = new ProgramImportTable(program); | 297 ImportTable imports = new ProgramImportTable(program); |
291 var inner = new Printer._inner(this, imports); | 298 var inner = new Printer._inner(this, imports); |
292 writeWord('main'); | 299 writeWord('main'); |
293 writeSpaced('='); | 300 writeSpaced('='); |
(...skipping 12 matching lines...) Expand all Loading... |
306 } | 313 } |
307 if (library.importUri != null) { | 314 if (library.importUri != null) { |
308 writeSpaced('from'); | 315 writeSpaced('from'); |
309 writeWord('"${library.importUri}"'); | 316 writeWord('"${library.importUri}"'); |
310 } | 317 } |
311 var prefix = syntheticNames.nameLibraryPrefix(library); | 318 var prefix = syntheticNames.nameLibraryPrefix(library); |
312 writeSpaced('as'); | 319 writeSpaced('as'); |
313 writeWord(prefix); | 320 writeWord(prefix); |
314 endLine(' {'); | 321 endLine(' {'); |
315 ++inner.indentation; | 322 ++inner.indentation; |
| 323 library.typedefs.forEach(inner.writeNode); |
316 library.classes.forEach(inner.writeNode); | 324 library.classes.forEach(inner.writeNode); |
317 library.fields.forEach(inner.writeNode); | 325 library.fields.forEach(inner.writeNode); |
318 library.procedures.forEach(inner.writeNode); | 326 library.procedures.forEach(inner.writeNode); |
319 --inner.indentation; | 327 --inner.indentation; |
320 endLine('}'); | 328 endLine('}'); |
321 } | 329 } |
322 } | 330 } |
323 | 331 |
324 int getPrecedence(TreeNode node) { | 332 int getPrecedence(TreeNode node) { |
325 return Precedence.of(node); | 333 return Precedence.of(node); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 writeList(type.typeArguments, writeType); | 430 writeList(type.typeArguments, writeType); |
423 writeSymbol('>'); | 431 writeSymbol('>'); |
424 } | 432 } |
425 } | 433 } |
426 } | 434 } |
427 | 435 |
428 visitVectorType(VectorType type) { | 436 visitVectorType(VectorType type) { |
429 writeWord('Vector'); | 437 writeWord('Vector'); |
430 } | 438 } |
431 | 439 |
| 440 visitTypedefType(TypedefType type) { |
| 441 writeTypedefReference(type.typedefNode); |
| 442 if (type.typeArguments.isNotEmpty) { |
| 443 writeSymbol('<'); |
| 444 writeList(type.typeArguments, writeType); |
| 445 writeSymbol('>'); |
| 446 } |
| 447 } |
| 448 |
432 void writeModifier(bool isThere, String name) { | 449 void writeModifier(bool isThere, String name) { |
433 if (isThere) { | 450 if (isThere) { |
434 writeWord(name); | 451 writeWord(name); |
435 } | 452 } |
436 } | 453 } |
437 | 454 |
438 void writeName(Name name) { | 455 void writeName(Name name) { |
439 if (name?.name == '') { | 456 if (name?.name == '') { |
440 writeWord(emptyNameString); | 457 writeWord(emptyNameString); |
441 } else { | 458 } else { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 } | 620 } |
604 | 621 |
605 void writeMemberReference(Member member) { | 622 void writeMemberReference(Member member) { |
606 writeWord(getMemberReference(member)); | 623 writeWord(getMemberReference(member)); |
607 } | 624 } |
608 | 625 |
609 void writeClassReference(Class classNode) { | 626 void writeClassReference(Class classNode) { |
610 writeWord(getClassReference(classNode)); | 627 writeWord(getClassReference(classNode)); |
611 } | 628 } |
612 | 629 |
| 630 void writeTypedefReference(Typedef typedefNode) { |
| 631 writeWord(getTypedefReference(typedefNode)); |
| 632 } |
| 633 |
613 void writeLibraryReference(Library library) { | 634 void writeLibraryReference(Library library) { |
614 writeWord(getLibraryReference(library)); | 635 writeWord(getLibraryReference(library)); |
615 } | 636 } |
616 | 637 |
617 void writeVariableReference(VariableDeclaration variable) { | 638 void writeVariableReference(VariableDeclaration variable) { |
618 writeWord(getVariableReference(variable)); | 639 writeWord(getVariableReference(variable)); |
619 } | 640 } |
620 | 641 |
621 void writeTypeParameterReference(TypeParameter node) { | 642 void writeTypeParameterReference(TypeParameter node) { |
622 writeWord(getTypeParameterReference(node)); | 643 writeWord(getTypeParameterReference(node)); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 endLine(endLineString); | 765 endLine(endLineString); |
745 ++indentation; | 766 ++indentation; |
746 node.fields.forEach(writeNode); | 767 node.fields.forEach(writeNode); |
747 node.constructors.forEach(writeNode); | 768 node.constructors.forEach(writeNode); |
748 node.procedures.forEach(writeNode); | 769 node.procedures.forEach(writeNode); |
749 --indentation; | 770 --indentation; |
750 writeIndentation(); | 771 writeIndentation(); |
751 endLine('}'); | 772 endLine('}'); |
752 } | 773 } |
753 | 774 |
| 775 visitTypedef(Typedef node) { |
| 776 writeIndentation(); |
| 777 writeWord('typedef'); |
| 778 writeWord(node.name); |
| 779 writeTypeParameterList(node.typeParameters); |
| 780 writeSpaced('='); |
| 781 writeNode(node.type); |
| 782 endLine(';'); |
| 783 } |
| 784 |
754 visitInvalidExpression(InvalidExpression node) { | 785 visitInvalidExpression(InvalidExpression node) { |
755 writeWord('invalid-expression'); | 786 writeWord('invalid-expression'); |
756 } | 787 } |
757 | 788 |
758 visitMethodInvocation(MethodInvocation node) { | 789 visitMethodInvocation(MethodInvocation node) { |
759 writeExpression(node.receiver, Precedence.PRIMARY); | 790 writeExpression(node.receiver, Precedence.PRIMARY); |
760 writeSymbol('.'); | 791 writeSymbol('.'); |
761 writeInterfaceTarget(node.name, node.interfaceTarget); | 792 writeInterfaceTarget(node.name, node.interfaceTarget); |
762 writeNode(node.arguments); | 793 writeNode(node.arguments); |
763 } | 794 } |
(...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1612 } | 1643 } |
1613 throw 'illegal ProcedureKind: $kind'; | 1644 throw 'illegal ProcedureKind: $kind'; |
1614 } | 1645 } |
1615 | 1646 |
1616 class ExpressionPrinter { | 1647 class ExpressionPrinter { |
1617 final Printer writeer; | 1648 final Printer writeer; |
1618 final int minimumPrecedence; | 1649 final int minimumPrecedence; |
1619 | 1650 |
1620 ExpressionPrinter(this.writeer, this.minimumPrecedence); | 1651 ExpressionPrinter(this.writeer, this.minimumPrecedence); |
1621 } | 1652 } |
OLD | NEW |