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

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

Issue 473533003: Initial code to generate the Java types from the spec. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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 /** 5 /**
6 * Tools for Java code generation. 6 * Tools for Java code generation.
7 */ 7 */
8 library CodegenJava; 8 library CodegenJava;
9 9
10 import 'api.dart'; 10 import 'api.dart';
(...skipping 16 matching lines...) Expand all
27 }; 27 };
28 28
29 /** 29 /**
30 * Type references in the spec that are named something else in Java. 30 * Type references in the spec that are named something else in Java.
31 */ 31 */
32 static const Map<String, String> _typeRenames = const { 32 static const Map<String, String> _typeRenames = const {
33 'bool': 'boolean', 33 'bool': 'boolean',
34 'FilePath': 'String', 34 'FilePath': 'String',
35 'DebugContextId': 'String', 35 'DebugContextId': 'String',
36 'object': 'Object', 36 'object': 'Object',
37 'Override': 'OverrideMember',
37 }; 38 };
38 39
39 /** 40 /**
40 * Visitor used to produce doc comments. 41 * Visitor used to produce doc comments.
41 */ 42 */
42 final ToHtmlVisitor toHtmlVisitor; 43 final ToHtmlVisitor toHtmlVisitor;
43 44
44 CodegenJavaVisitor(Api api) 45 CodegenJavaVisitor(Api api)
45 : super(api), 46 : super(api),
46 toHtmlVisitor = new ToHtmlVisitor(api); 47 toHtmlVisitor = new ToHtmlVisitor(api);
47 48
48 /** 49 /**
50 * Create a private field, using [callback] to create its contents.
51 */
52 void privateField(String fieldName, void callback()) {
53 _state.privateFields[fieldName] = collectCode(callback);
54 }
55
56 /**
57 * Create a constructor, using [callback] to create its contents.
58 */
59 void constructor(String name, void callback()) {
60 _state.constructors[name] = collectCode(callback);
61 }
62
63 /**
49 * Create a private method, using [callback] to create its contents. 64 * Create a private method, using [callback] to create its contents.
50 */ 65 */
51 void privateMethod(String methodName, void callback()) { 66 void privateMethod(String methodName, void callback()) {
52 _state.privateMethods[methodName] = collectCode(callback); 67 _state.privateMethods[methodName] = collectCode(callback);
53 } 68 }
54 69
55 /** 70 /**
56 * Create a public method, using [callback] to create its contents. 71 * Create a public method, using [callback] to create its contents.
57 */ 72 */
58 void publicMethod(String methodName, void callback()) { 73 void publicMethod(String methodName, void callback()) {
59 _state.publicMethods[methodName] = collectCode(callback); 74 _state.publicMethods[methodName] = collectCode(callback);
60 } 75 }
61 76
62 /** 77 /**
63 * Execute [callback], collecting any methods that are output using 78 * Execute [callback], collecting any methods that are output using
64 * [privateMethod] or [publicMethod], and insert the class (with methods 79 * [privateMethod] or [publicMethod], and insert the class (with methods
65 * sorted). [header] is the part of the class declaration before the 80 * sorted). [header] is the part of the class declaration before the
66 * opening brace. 81 * opening brace.
67 */ 82 */
68 void makeClass(String header, void callback()) { 83 void makeClass(String header, void callback()) {
69 _CodegenJavaState oldState = _state; 84 _CodegenJavaState oldState = _state;
70 try { 85 try {
71 _state = new _CodegenJavaState(); 86 _state = new _CodegenJavaState();
72 callback(); 87 callback();
73 writeln('$header {'); 88 writeln('$header {');
74 indent(() { 89 indent(() {
90 // fields
91 List<String> allFields = _valuesSortedByKey(_state.privateFields).toList ();
92 for (String field in allFields) {
93 writeln();
94 write(field);
95 }
96
97 // constructors
98 List<String> allConstructors = _valuesSortedByKey(_state.constructors).t oList();
99 for (String constructor in allConstructors) {
100 writeln();
101 write(constructor);
102 }
103
104 // methods
75 List<String> allMethods = _valuesSortedByKey(_state.publicMethods).toLis t(); 105 List<String> allMethods = _valuesSortedByKey(_state.publicMethods).toLis t();
76 allMethods.addAll(_valuesSortedByKey(_state.privateMethods)); 106 allMethods.addAll(_valuesSortedByKey(_state.privateMethods));
77 for (String method in allMethods) { 107 for (String method in allMethods) {
78 writeln(); 108 writeln();
79 write(method); 109 write(method);
80 } 110 }
81 }); 111 });
82 writeln('}'); 112 writeln('}');
83 } finally { 113 } finally {
84 _state = oldState; 114 _state = oldState;
85 } 115 }
86 } 116 }
87 117
88 /** 118 /**
89 * Convert the given [TypeDecl] to a Java type. 119 * Convert the given [TypeDecl] to a Java type.
90 */ 120 */
91 String javaType(TypeDecl type) { 121 String javaType(TypeDecl type) {
92 if (type is TypeReference) { 122 if (type is TypeReference) {
93 TypeReference resolvedType = resolveTypeReferenceChain(type); 123 TypeReference resolvedType = resolveTypeReferenceChain(type);
94 String typeName = resolvedType.typeName; 124 String typeName = resolvedType.typeName;
95 if (_typeRenames.containsKey(typeName)) { 125 if (_typeRenames.containsKey(typeName)) {
96 return _typeRenames[typeName]; 126 return _typeRenames[typeName];
97 } else { 127 } else {
98 return typeName; 128 return typeName;
99 } 129 }
100 } else if (type is TypeList) { 130 } else if (type is TypeList) {
101 return 'List<${javaType(type.itemType)}>'; 131 if (isPrimitive(type.itemType)) {
132 return '${javaType(type.itemType)}[]';
133 } else {
134 return 'List<${javaType(type.itemType)}>';
135 }
102 } else if (type is TypeMap) { 136 } else if (type is TypeMap) {
103 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>'; 137 return 'Map<${javaType(type.keyType)}, ${javaType(type.valueType)}>';
104 } else if (type is TypeUnion) { 138 } else if (type is TypeUnion) {
105 return 'Object'; 139 return 'Object';
106 } else { 140 } else {
107 throw new Exception("Can't make type buildable"); 141 throw new Exception("Can't make type buildable");
108 } 142 }
109 } 143 }
110 144
111 /** 145 /**
146 * Return true iff the passed [TypeDecl] will represent a primitive Java type.
147 */
148 bool isPrimitive(TypeDecl type) {
149 if (type is TypeReference) {
150 String typeStr = javaType(type);
151 return typeStr == 'int' || typeStr == 'boolean';
152 }
153 return false;
154 }
155
156 /**
157 * Return true iff the passed [TypeDecl] will represent an array in Java.
Paul Berry 2014/08/13 21:39:06 Funky indentation here.
jwren 2014/08/13 22:29:15 Fixed. This is me fussing with the Dart Editor :)
158 */
159 bool isList(TypeDecl type) {
160 return type is TypeList && !isPrimitive(type.itemType);
161 }
162
163 /**
164 * Return true iff the passed [TypeDecl] will represent an array in Java.
165 */
166 bool isArray(TypeDecl type) {
167 return type is TypeList && isPrimitive(type.itemType);
168 }
169
170 /**
171 * Return true iff the passed [TypeDecl] will represent a Map in type.
172 */
173 bool isMap(TypeDecl type) {
174 return type is TypeMap;
175 }
176
177 /**
112 * Return a suitable representation of [name] as the name of a Java variable. 178 * Return a suitable representation of [name] as the name of a Java variable.
113 */ 179 */
114 String javaName(String name) { 180 String javaName(String name) {
115 if (_variableRenames.containsKey(name)) { 181 if (_variableRenames.containsKey(name)) {
116 return _variableRenames[name]; 182 return _variableRenames[name];
117 } 183 }
118 return name; 184 return name;
119 } 185 }
120 186
121 @override 187 @override
(...skipping 21 matching lines...) Expand all
143 class _CodegenJavaState { 209 class _CodegenJavaState {
144 /** 210 /**
145 * Temporary storage for public methods. 211 * Temporary storage for public methods.
146 */ 212 */
147 Map<String, String> publicMethods = <String, String>{}; 213 Map<String, String> publicMethods = <String, String>{};
148 214
149 /** 215 /**
150 * Temporary storage for private methods. 216 * Temporary storage for private methods.
151 */ 217 */
152 Map<String, String> privateMethods = <String, String>{}; 218 Map<String, String> privateMethods = <String, String>{};
219
220 /**
221 * Temporary storage for private fields.
Paul Berry 2014/08/13 21:39:06 And here
jwren 2014/08/13 22:29:15 Done.
222 */
223 Map<String, String> privateFields = <String, String>{};
224
225 /**
226 * Temporary storage for constructors.
Paul Berry 2014/08/13 21:39:06 And here
jwren 2014/08/13 22:29:15 Done.
227 */
228 Map<String, String> constructors = <String, String>{};
153 } 229 }
154 230
155 /** 231 /**
156 * Create a [GeneratedFile] that creates Java code and outputs it to [path]. 232 * Create a [GeneratedFile] that creates Java code and outputs it to [path].
157 * [path] uses Posix-style path separators regardless of the OS. 233 * [path] uses Posix-style path separators regardless of the OS.
158 */ 234 */
159 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor createVisitor(Ap i api)) { 235 GeneratedFile javaGeneratedFile(String path, CodegenJavaVisitor createVisitor(Ap i api)) {
160 return new GeneratedFile(path, () { 236 return new GeneratedFile(path, () {
161 CodegenJavaVisitor visitor = createVisitor(readApi()); 237 CodegenJavaVisitor visitor = createVisitor(readApi());
162 return visitor.collectCode(visitor.visitApi); 238 return visitor.collectCode(visitor.visitApi);
163 }); 239 });
164 } 240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698