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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: dart/sdk/lib/_internal/compiler/samples/constants/constants.dart
diff --git a/dart/sdk/lib/_internal/compiler/samples/constants/constants.dart b/dart/sdk/lib/_internal/compiler/samples/constants/constants.dart
new file mode 100644
index 0000000000000000000000000000000000000000..adb782a850d01169f23b283ff66006960fb5640a
--- /dev/null
+++ b/dart/sdk/lib/_internal/compiler/samples/constants/constants.dart
@@ -0,0 +1,141 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:io';
+
+import 'dart:mirrors';
+
+import '../../../libraries.dart'
+ show LIBRARIES, LibraryInfo;
+
+import '../../implementation/mirrors/analyze.dart'
+ show analyze;
+
+import '../../implementation/mirrors/dart2js_mirrors.dart' show
+ BackDoor,
+ ResolvedNode;
+
+import '../../implementation/mirrors/mirrors_util.dart' show nameOf;
+
+import '../../implementation/filenames.dart';
+import '../../implementation/source_file.dart';
+import '../../implementation/source_file_provider.dart';
+import '../../implementation/util/uri_extras.dart';
+import '../../implementation/tree/tree.dart';
+
+const SDK_ROOT = '../../../../../';
+
+var handler;
+RandomAccessFile output;
+
+main(List<String> arguments) {
+ handler = new FormattingDiagnosticHandler()
+ ..throwOnError = true;
+
+ Uri myLocation =
+ handler.provider.cwd.resolveUri(Platform.script);
+
+ List uris = arguments.map(Uri.base.resolve).toList();
+
+
+ analyze(uris, myLocation.resolve(SDK_ROOT), null, handler.provider, handler)
+ .then((mirrors) => processLibraries(uris, mirrors));
+}
+
+void processLibraries(List<Uri> uris, MirrorSystem mirrors) {
+ for (Uri uri in uris) {
+ processLibrary(mirrors.libraries[uri]);
+ }
+}
+
+void processLibrary(LibraryMirror library) {
+ library.declarations.values.forEach(processMirror);
+}
+
+void processMirror(Mirror mirror) {
+ if (mirror is MethodMirror) {
+ processMethod(mirror);
+ } else if (mirror is ParameterMirror) {
+ processParameter(mirror);
+ } else if (mirror is VariableMirror) {
+ processVariable(mirror);
+ } else if (mirror is DeclarationMirror) {
+ processDeclaration(mirror);
+ }
+}
+
+void processMethod(MethodMirror mirror) {
+ processDeclaration(mirror);
+ print('(');
+ for (var parameter in mirror.parameters) {
+ processParameter(parameter);
+ }
+ print(');');
+}
+
+void processParameter(ParameterMirror mirror) {
+ processDeclaration(mirror);
+ ResolvedNode defaultValue = BackDoor.defaultValueSyntaxOf(mirror);
+ if (defaultValue != null) {
+ String delimiter = mirror.isNamed ? ':' : '=';
+ print(" $delimiter ${formatResolvedNode(defaultValue)}");
+ }
+}
+
+void processVariable(VariableMirror mirror) {
+ processDeclaration(mirror);
+ ResolvedNode initializer = BackDoor.initializerSyntaxOf(mirror);
+ if (initializer != null) {
+ print(" = ${formatResolvedNode(initializer)}");
+ }
+}
+
+void processDeclaration(DeclarationMirror mirror) {
+ for (ResolvedNode node in BackDoor.metadataSyntaxOf(mirror)) {
+ print("@${formatResolvedNode(node)}");
+ }
+
+ print(MirrorSystem.getName(mirror.qualifiedName));
+}
+
+String formatResolvedNode(ResolvedNode node) {
+ ResolvedPrinter printer = new ResolvedPrinter(node);
+ printer.unparse(node.node);
+ return printer.result.toString();
+}
+
+class ResolvedPrinter extends Unparser {
+ final ResolvedNode resolvedNode;
+
+ ResolvedPrinter(this.resolvedNode);
+
+ visitSend(Send node) {
+ var m = resolvedNode.resolvedMirror(node);
+ String after = '';
+ if (m != null) {
+ add('<a href="#${MirrorSystem.getName(m.qualifiedName)}">');
+ after = '</a>';
+ }
+
+ Operator op = node.selector.asOperator();
+ String opString = op != null ? op.source : null;
+ bool spacesNeeded = identical(opString, 'is') || identical(opString, 'as');
+
+ if (node.isPrefix) visit(node.selector);
+ unparseSendReceiver(node, spacesNeeded: spacesNeeded);
+ if (!node.isPrefix && !node.isIndex) visit(node.selector);
+ if (spacesNeeded) sb.write(' ');
+ // Also add a space for sequences like x + +1 and y - -y.
+ // TODO(ahe): remove case for '+' when we drop the support for it.
+ if (node.argumentsNode != null && (identical(opString, '-')
+ || identical(opString, '+'))) {
+ Token beginToken = node.argumentsNode.getBeginToken();
+ if (beginToken != null && identical(beginToken.stringValue, opString)) {
+ sb.write(' ');
+ }
+ }
+ add(after);
+ visit(node.argumentsNode);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698