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

Side by Side Diff: dart/sdk/lib/_internal/compiler/samples/constants/constants.dart

Issue 257863002: Prototype API for accessing AST nodes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 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
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'dart:io';
6
7 import 'dart:mirrors';
8
9 import '../../../libraries.dart'
10 show LIBRARIES, LibraryInfo;
11
12 import '../../implementation/mirrors/analyze.dart'
13 show analyze;
14
15 import '../../implementation/mirrors/dart2js_mirrors.dart' show
16 BackDoor,
17 ResolvedNode;
18
19 import '../../implementation/mirrors/mirrors_util.dart' show nameOf;
20
21 import '../../implementation/filenames.dart';
22 import '../../implementation/source_file.dart';
23 import '../../implementation/source_file_provider.dart';
24 import '../../implementation/util/uri_extras.dart';
25 import '../../implementation/tree/tree.dart';
26
27 const SDK_ROOT = '../../../../../';
28
29 var handler;
30 RandomAccessFile output;
31
32 main(List<String> arguments) {
33 handler = new FormattingDiagnosticHandler()
34 ..throwOnError = true;
35
36 Uri myLocation =
37 handler.provider.cwd.resolveUri(Platform.script);
38
39 List uris = arguments.map(Uri.base.resolve).toList();
40
41
42 analyze(uris, myLocation.resolve(SDK_ROOT), null, handler.provider, handler)
43 .then((mirrors) => processLibraries(uris, mirrors));
44 }
45
46 void processLibraries(List<Uri> uris, MirrorSystem mirrors) {
47 for (Uri uri in uris) {
48 processLibrary(mirrors.libraries[uri]);
49 }
50 }
51
52 void processLibrary(LibraryMirror library) {
53 library.declarations.values.forEach(processMirror);
54 }
55
56 void processMirror(Mirror mirror) {
57 if (mirror is MethodMirror) {
58 processMethod(mirror);
59 } else if (mirror is ParameterMirror) {
60 processParameter(mirror);
61 } else if (mirror is VariableMirror) {
62 processVariable(mirror);
63 } else if (mirror is DeclarationMirror) {
64 processDeclaration(mirror);
65 }
66 }
67
68 void processMethod(MethodMirror mirror) {
69 processDeclaration(mirror);
70 print('(');
71 for (var parameter in mirror.parameters) {
72 processParameter(parameter);
73 }
74 print(');');
75 }
76
77 void processParameter(ParameterMirror mirror) {
78 processDeclaration(mirror);
79 ResolvedNode defaultValue = BackDoor.defaultValueSyntaxOf(mirror);
80 if (defaultValue != null) {
81 String delimiter = mirror.isNamed ? ':' : '=';
82 print(" $delimiter ${formatResolvedNode(defaultValue)}");
83 }
84 }
85
86 void processVariable(VariableMirror mirror) {
87 processDeclaration(mirror);
88 ResolvedNode initializer = BackDoor.initializerSyntaxOf(mirror);
89 if (initializer != null) {
90 print(" = ${formatResolvedNode(initializer)}");
91 }
92 }
93
94 void processDeclaration(DeclarationMirror mirror) {
95 for (ResolvedNode node in BackDoor.metadataSyntaxOf(mirror)) {
96 print("@${formatResolvedNode(node)}");
97 }
98
99 print(MirrorSystem.getName(mirror.qualifiedName));
100 }
101
102 String formatResolvedNode(ResolvedNode node) {
103 ResolvedPrinter printer = new ResolvedPrinter(node);
104 printer.unparse(node.node);
105 return printer.result.toString();
106 }
107
108 class ResolvedPrinter extends Unparser {
109 final ResolvedNode resolvedNode;
110
111 ResolvedPrinter(this.resolvedNode);
112
113 visitSend(Send node) {
114 var m = resolvedNode.resolvedMirror(node);
115 String after = '';
116 if (m != null) {
117 add('<a href="#${MirrorSystem.getName(m.qualifiedName)}">');
118 after = '</a>';
119 }
120
121 Operator op = node.selector.asOperator();
122 String opString = op != null ? op.source : null;
123 bool spacesNeeded = identical(opString, 'is') || identical(opString, 'as');
124
125 if (node.isPrefix) visit(node.selector);
126 unparseSendReceiver(node, spacesNeeded: spacesNeeded);
127 if (!node.isPrefix && !node.isIndex) visit(node.selector);
128 if (spacesNeeded) sb.write(' ');
129 // Also add a space for sequences like x + +1 and y - -y.
130 // TODO(ahe): remove case for '+' when we drop the support for it.
131 if (node.argumentsNode != null && (identical(opString, '-')
132 || identical(opString, '+'))) {
133 Token beginToken = node.argumentsNode.getBeginToken();
134 if (beginToken != null && identical(beginToken.stringValue, opString)) {
135 sb.write(' ');
136 }
137 }
138 add(after);
139 visit(node.argumentsNode);
140 }
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698