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

Side by Side Diff: pkg/analysis_server/tool/spec/codegen_java.dart

Issue 463163002: Initial cut at generating the AnalysisServer.java interface. Currently this generates a commented o… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: nit Created 6 years, 4 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 /**
6 * Tools for Java code generation.
7 */
8 library CodegenJava;
9
10 import 'dart:io';
11
12 import 'api.dart';
13 import 'codegen_tools.dart';
14 import 'to_html.dart';
15
16 /**
17 * Common code for all Java code generation.
18 */
19 class CodegenJavaVisitor extends HierarchicalApiVisitor with CodeGenerator {
20 _CodegenJavaState _state;
21
22 /**
23 * Variable names which must be changed in order to avoid conflict with
24 * reserved words in Java.
25 */
26 static const Map<String, String> _variableRenames = const {
27 'default': 'defaultSdk'
28 };
29
30 /**
31 * Visitor used to produce doc comments.
32 */
33 final ToHtmlVisitor toHtmlVisitor;
34
35 CodegenJavaVisitor(Api api)
36 : super(api),
37 toHtmlVisitor = new ToHtmlVisitor(api);
38
39 /**
40 * Create a private method, using [callback] to create its contents.
41 */
42 void privateMethod(String methodName, void callback()) {
43 _state.privateMethods[methodName] = collectCode(callback);
44 }
45
46 /**
47 * Create a public method, using [callback] to create its contents.
48 */
49 void publicMethod(String methodName, void callback()) {
50 _state.publicMethods[methodName] = collectCode(callback);
51 }
52
53 /**
54 * Execute [callback], collecting any methods that are output using
55 * [privateMethod] or [publicMethod], and insert the class (with methods
56 * sorted). [header] is the part of the class declaration before the
57 * opening brace.
58 */
59 void makeClass(String header, void callback()) {
60 _CodegenJavaState oldState = _state;
61 try {
62 _state = new _CodegenJavaState();
63 callback();
64 writeln('$header {');
65 indent(() {
66 List<String> allMethods = _valuesSortedByKey(_state.publicMethods).toLis t();
67 allMethods.addAll(_valuesSortedByKey(_state.privateMethods));
68 for (String method in allMethods) {
69 writeln();
70 write(method);
71 }
72 });
73 writeln('}');
74 } finally {
75 _state = oldState;
76 }
77 }
78
79 /**
80 * Convert the given [TypeDecl] to a Java type.
81 */
82 String javaType(TypeDecl type) {
83 if (type is TypeReference) {
84 TypeReference resolvedType = resolveTypeReferenceChain(type);
85 switch (resolvedType.typeName) {
86 // TODO(jwren) make this into a type type renames map, like _variableRen ames
87 case 'bool':
88 return 'boolean';
89 case 'FilePath':
90 return 'String';
91 case 'DebugContextId':
92 return 'String';
93 case 'object':
94 return 'Object';
95 default:
96 return resolvedType.typeName;
97 }
98 } else if (type is TypeList) {
99 return 'List<${javaType(type.itemType)}>';
100 } else if (type is TypeMap) {
101 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>';
102 } else {
103 throw new Exception("Can't make type buildable");
104 }
105 }
106
107 /**
108 * Return a suitable representation of [name] as the name of a Java variable.
109 */
110 String javaName(String name) {
111 if (_variableRenames.containsKey(name)) {
112 return _variableRenames[name];
113 }
114 return name;
115 }
116
117 @override
118 TypeReference resolveTypeReferenceChain(TypeReference type) {
119 TypeDecl typeDecl = super.resolveTypeReferenceChain(type);
120 if (typeDecl is TypeEnum) {
121 return new TypeReference('String', null);
122 }
123 return type;
124 }
125 }
126
127 /**
128 * Iterate through the values in [map] in the order of increasing keys.
129 */
130 Iterable<String> _valuesSortedByKey(Map<String, String> map) {
131 List<String> keys = map.keys.toList();
132 keys.sort();
133 return keys.map((String key) => map[key]);
134 }
135
136 /**
137 * State used by [CodegenJavaVisitor].
138 */
139 class _CodegenJavaState {
140 /**
141 * Temporary storage for public methods.
142 */
143 Map<String, String> publicMethods = <String, String>{};
144
145 /**
146 * Temporary storage for private methods.
147 */
148 Map<String, String> privateMethods = <String, String>{};
149 }
150
151 /**
152 * Use [visitor] to create Java code and output it to [path].
153 */
154 void createJavaCode(String path, CodegenJavaVisitor visitor) {
155 String code = visitor.collectCode(visitor.visitApi);
156 File outputFile = new File(path);
157 outputFile.writeAsStringSync(code);
158 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/tool/spec/codegen_inttest_methods.dart ('k') | pkg/analysis_server/tool/spec/codegen_matchers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698