comparison Resources/CodeGeneration/stonegentool.py @ 519:17106b29ed6d bgo-commands-codegen

Changed the metadata system for structs. A __handler entry is now required (with "cpp", "ts" or both: ["cpp","ts"]). Changed the enumerations to string-based values. Adapted the integrated wasm test.
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 12 Mar 2019 13:11:18 +0100
parents 1dbf2d9ed1e4
children 75664eeacae5
comparison
equal deleted inserted replaced
518:40bb5eb247a5 519:17106b29ed6d
209 # defined types. Simply splitting at commas won't work 209 # defined types. Simply splitting at commas won't work
210 listOfDependentTypes = SplitListOfTypes(m.group(2)) 210 listOfDependentTypes = SplitListOfTypes(m.group(2))
211 return (True, m.group(1), listOfDependentTypes) 211 return (True, m.group(1), listOfDependentTypes)
212 212
213 def GetStructFields(struct): 213 def GetStructFields(struct):
214 """This filters out the __meta__ key from the struct fields""" 214 """This filters out the special metadata key from the struct fields"""
215 return [k for k in struct.keys() if k != '__meta__'] 215 return [k for k in struct.keys() if k != '__handler']
216 216
217 def ComputeOrderFromTypeTree( 217 def ComputeOrderFromTypeTree(
218 ancestors, 218 ancestors,
219 genOrder, 219 genOrder,
220 shortTypename, schema): 220 shortTypename, schema):
348 ancestors = [] 348 ancestors = []
349 ComputeOrderFromTypeTree(ancestors, genOrder, realName, schema) 349 ComputeOrderFromTypeTree(ancestors, genOrder, realName, schema)
350 return genOrder 350 return genOrder
351 351
352 def GetStructFields(fieldDict): 352 def GetStructFields(fieldDict):
353 """Returns the regular (non __meta__) struct fields""" 353 """Returns the regular (non __handler) struct fields"""
354 # the following happens for empty structs 354 # the following happens for empty structs
355 if fieldDict == None: 355 if fieldDict == None:
356 return fieldDict 356 return fieldDict
357 ret = {} 357 ret = {}
358 for k,v in fieldDict.items(): 358 for k,v in fieldDict.items():
359 if k != "__meta__": 359 if k != "__handler":
360 ret[k] = v 360 ret[k] = v
361 if k.startswith("__") and k != "__handler":
362 raise RuntimeError("Fields starting with __ (double underscore) are reserved names!")
361 return ret 363 return ret
362 364
363 def GetStructMetadata(fieldDict): 365 def GetStructMetadata(fieldDict):
364 """Returns the __meta__ struct fields (there are default values that 366 """Returns the __handler struct fields (there are default values that
365 can be overridden by entries in the schema 367 can be overridden by entries in the schema
366 Not tested because it's a fail-safe: if something is broken in meta, 368 Not tested because it's a fail-safe: if something is broken in this,
367 dependent projects will not build.""" 369 dependent projects will not build."""
368 metadataDict = {} 370 metadataDict = {}
369 metadataDict['handleInCpp'] = True 371 metadataDict['handleInCpp'] = False
370 metadataDict['handleInTypescript'] = True 372 metadataDict['handleInTypescript'] = False
371 373
372 # Empty types are allowed
373 if fieldDict != None: 374 if fieldDict != None:
374 for k,v in fieldDict.items(): 375 for k,v in fieldDict.items():
375 if k == "__meta__": 376 if k.startswith("__") and k != "__handler":
376 # let's examine the various metadata entries 377 raise RuntimeError("Fields starting with __ (double underscore) are reserved names")
377 for metaKey,metaValue in v.items(): 378 if k == "__handler":
378 # we only accept overriding EXISTING entries 379 if type(v) == list:
379 if metaKey in metadataDict: 380 for i in v:
380 # simple check, valid for now 381 if i == "cpp":
381 if type(metaValue) != bool: 382 metadataDict['handleInCpp'] = True
382 raise RuntimeError("Wrong value for metadata key") 383 elif i == "ts":
383 metadataDict[metaKey] = metaValue 384 metadataDict['handleInTypescript'] = True
385 else:
386 raise RuntimeError("Error in schema. Allowed values for __handler are \"cpp\" or \"ts\"")
387 elif type(v) == str:
388 if v == "cpp":
389 metadataDict['handleInCpp'] = True
390 elif v == "ts":
391 metadataDict['handleInTypescript'] = True
384 else: 392 else:
385 raise RuntimeError("Wrong key \"{metaKey}\" in metadata. Allowed keys are \"{metadataDict.keys()}\"") 393 raise RuntimeError("Error in schema. Allowed values for __handler are \"cpp\" or \"ts\" (or a list of both)")
394 else:
395 raise RuntimeError("Error in schema. Allowed values for __handler are \"cpp\" or \"ts\" (or a list of both)")
386 return metadataDict 396 return metadataDict
387 397
388 def ProcessSchema(schema, genOrder): 398 def ProcessSchema(schema, genOrder):
389 # sanity check 399 # sanity check
390 CheckSchemaSchema(schema) 400 CheckSchemaSchema(schema)
463 ch = schemaText[i] 473 ch = schemaText[i]
464 nextCh = schemaText[i+1] 474 nextCh = schemaText[i+1]
465 if ch == ':': 475 if ch == ':':
466 if not (nextCh == ' ' or nextCh == '\n'): 476 if not (nextCh == ' ' or nextCh == '\n'):
467 lineNumber = schemaText.count("\n",0,i) + 1 477 lineNumber = schemaText.count("\n",0,i) + 1
468 raise RuntimeError(f"Error at line {lineNumber} in the schema: colons must be followed by a space or a newline!") 478 raise RuntimeError("Error at line " + str(lineNumber) + " in the schema: colons must be followed by a space or a newline!")
469 schema = yaml.load(schemaText) 479 schema = yaml.load(schemaText)
470 return schema 480 return schema
471 481
472 def GetTemplatingDictFromSchemaFilename(fn): 482 def GetTemplatingDictFromSchemaFilename(fn):
473 obj = LoadSchema(fn) 483 obj = LoadSchema(fn)