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

Side by Side Diff: tools/json_schema_compiler/h_generator.py

Issue 10701012: JSON Schema Compiler: Added event compilation. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Added more testing for event compilation. Created 8 years, 5 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
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import cpp_util 7 import cpp_util
8 import model 8 import model
9 import os 9 import os
10 import schema_util 10 import schema_util
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 if self._namespace.functions: 85 if self._namespace.functions:
86 (c.Append('//') 86 (c.Append('//')
87 .Append('// Functions') 87 .Append('// Functions')
88 .Append('//') 88 .Append('//')
89 .Append() 89 .Append()
90 ) 90 )
91 for function in self._namespace.functions.values(): 91 for function in self._namespace.functions.values():
92 (c.Concat(self._GenerateFunction(function)) 92 (c.Concat(self._GenerateFunction(function))
93 .Append() 93 .Append()
94 ) 94 )
95 if self._namespace.events:
96 (c.Append('//')
97 .Append('// Events')
98 .Append('//')
99 .Append()
100 )
101 for event in self._namespace.events.values():
102 (c.Concat(self._GenerateEvent(event))
103 .Append()
104 )
95 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) 105 (c.Concat(self._cpp_type_generator.GetNamespaceEnd())
96 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) 106 .Concat(self._cpp_type_generator.GetRootNamespaceEnd())
97 .Append() 107 .Append()
98 .Append('#endif // %s' % ifndef_name) 108 .Append('#endif // %s' % ifndef_name)
99 .Append() 109 .Append()
100 ) 110 )
101 return c 111 return c
102 112
103 def _FieldDependencyOrder(self): 113 def _FieldDependencyOrder(self):
104 """Generates the list of types in the current namespace in an order in which 114 """Generates the list of types in the current namespace in an order in which
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 ) 215 )
206 216
207 (c.Eblock() 217 (c.Eblock()
208 .Sblock(' private:') 218 .Sblock(' private:')
209 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 219 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
210 .Eblock('};') 220 .Eblock('};')
211 ) 221 )
212 c.Substitute({'classname': classname}) 222 c.Substitute({'classname': classname})
213 return c 223 return c
214 224
225 def _GenerateEvent(self, event):
226 """Generates the namespaces for an event.
227 """
228 c = Code()
229 c.Sblock('namespace %s {' % cpp_util.Classname(event.name))
230 c.Concat(self._GenerateFromClientFunction(event))
231 c.Eblock('};')
232 return c
233
215 def _GenerateFunction(self, function): 234 def _GenerateFunction(self, function):
216 """Generates the structs for a function. 235 """Generates the namespaces and structs for a function.
217 """ 236 """
218 c = Code() 237 c = Code()
219 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) 238 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name))
220 .Concat(self._GenerateFunctionParams(function)) 239 .Concat(self._GenerateFunctionParams(function))
221 .Append() 240 .Append()
222 ) 241 )
223 if function.callback: 242 if function.callback:
224 (c.Concat(self._GenerateFunctionResult(function)) 243 (c.Concat(self._GenerateFunctionResult(function.callback))
225 .Append() 244 .Append()
226 ) 245 )
227 c.Eblock('};') 246 c.Eblock('};')
228 247
229 return c 248 return c
230 249
231 def _GenerateFunctionParams(self, function): 250 def _GenerateFunctionParams(self, function):
232 """Generates the struct for passing parameters into a function. 251 """Generates the struct for passing parameters from JSON to a function.
233 """ 252 """
234 c = Code() 253 c = Code()
235 254
236 if function.params: 255 if function.params:
237 (c.Sblock('struct Params {') 256 (c.Sblock('struct Params {')
238 .Concat(self._GeneratePropertyStructures(function.params)) 257 .Concat(self._GeneratePropertyStructures(function.params))
239 .Concat(self._GenerateFields(function.params)) 258 .Concat(self._GenerateFields(function.params))
240 .Append('~Params();') 259 .Append('~Params();')
241 .Append() 260 .Append()
242 .Append('static scoped_ptr<Params> Create(const ListValue& args);') 261 .Append('static scoped_ptr<Params> Create(const ListValue& args);')
(...skipping 24 matching lines...) Expand all
267 self._cpp_type_generator.GetChoicesEnumType(prop), 286 self._cpp_type_generator.GetChoicesEnumType(prop),
268 prop, 287 prop,
269 [choice.type_.name for choice in prop.choices.values()])) 288 [choice.type_.name for choice in prop.choices.values()]))
270 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 289 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
271 elif prop.type_ == PropertyType.ENUM: 290 elif prop.type_ == PropertyType.ENUM:
272 enum_name = self._cpp_type_generator.GetType(prop) 291 enum_name = self._cpp_type_generator.GetType(prop)
273 c.Concat(self._GenerateEnumDeclaration( 292 c.Concat(self._GenerateEnumDeclaration(
274 enum_name, 293 enum_name,
275 prop, 294 prop,
276 prop.enum_values)) 295 prop.enum_values))
277 c.Append('static scoped_ptr<Value> CreateEnumValue(%s %s);' % 296 # If the property is from the UI then we're in a struct so this function
278 (enum_name, prop.unix_name)) 297 # should be static. If it's from the client, then we're just in a
298 # namespace so we can't have the static keyword.
299 if prop.from_json:
300 c.Append('static scoped_ptr<Value> CreateEnumValue(%s %s);' %
301 (enum_name, prop.unix_name))
302 else:
303 c.Append('scoped_ptr<Value> CreateEnumValue(%s %s);' %
304 (enum_name, prop.unix_name))
279 return c 305 return c
280 306
281 def _GenerateFunctionResult(self, function): 307 def _GenerateFromClientFunction(self, function):
282 """Generates functions for passing a function's result back. 308 """Generates functions for passing paramaters to a callback.
283 """ 309 """
284 c = Code() 310 c = Code()
285 311 params = function.params
286 c.Sblock('namespace Result {')
287 params = function.callback.params
288 if not params: 312 if not params:
289 c.Append('Value* Create();') 313 c.Append('Value* Create();')
290 else: 314 else:
291 c.Concat(self._GeneratePropertyStructures(params)) 315 c.Concat(self._GeneratePropertyStructures(params))
292 316
293 # If there is a single parameter, this is straightforward. However, if 317 # If there is a single parameter, this is straightforward. However, if
294 # the callback parameter is of 'choices', this generates a Create method 318 # the callback parameter is of 'choices', this generates a Create method
295 # for each choice. This works because only 1 choice can be returned at a 319 # for each choice. This works because only 1 choice can be returned at a
296 # time. 320 # time.
297 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): 321 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
298 if param.description: 322 if param.description:
299 c.Comment(param.description) 323 c.Comment(param.description)
300 if param.type_ == PropertyType.ANY: 324 if param.type_ == PropertyType.ANY:
301 c.Comment("Value* Result::Create(Value*) not generated " 325 c.Comment("Value* Create(Value*) not generated "
302 "because it's redundant.") 326 "because it's redundant.")
303 continue 327 continue
304 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( 328 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration(
305 param, self._cpp_type_generator.GetType(param))) 329 param, self._cpp_type_generator.GetType(param)))
330
331 return c
332
333 def _GenerateFunctionResult(self, callback):
334 """Generates namespace for passing a function's result back.
335 """
336 c = Code()
337 c.Sblock('namespace Result {')
338 c.Concat(self._GenerateFromClientFunction(callback))
306 c.Eblock('};') 339 c.Eblock('};')
307 340
308 return c 341 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698