annotate Resources/CodeGeneration/stonegentool.py @ 472:3db3289e1c25 bgo-commands-codegen

Ongoing codegen work
author bgo-osimis
date Wed, 13 Feb 2019 06:46:36 +0100
parents 125c19b294e3
children 628941d63b8c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
1 from typing import List,Dict,Set
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
2 import sys
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
3 import json
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
4 import re
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
5
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
6 """
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
7 1 2 3 4 5 6 7
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
8 12345678901234567890123456789012345678901234567890123456789012345678901234567890
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
9 """
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
10
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
11 def LoadSchema(file_path : str):
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
12 with open(file_path, 'r') as fp:
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
13 obj = json.load(fp)
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
14 return obj
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
15
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
16 # class Type:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
17 # def __init__(self, canonicalTypeName:str, kind:str):
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
18 # allowedTypeKinds = ["primitive","enum","struct","collection"]
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
19 # """dependent type is the list of canonical types this type depends on.
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
20 # For instance, vector<map<string,int32>> depends on map<string,int32>
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
21 # that, in turn, depends on string and int32 that, in turn, depend on
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
22 # nothing"""
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
23 # self.canonicalTypeName = canonicalTypeName
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
24 # assert(kind in allowedTypeKinds)
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
25
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
26 # def setDependentTypes(self, dependentTypes:List[Type]) -> None:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
27 # self.dependentTypes = dependentTypes
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
28
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
29 # def getDependentTypes(self) -> List[Type]:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
30 # return self.dependentTypes
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
31
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
32 def GetCppTypeNameFromCanonical(canonicalTypeName : str) -> str:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
33 # C++: prefix map vector and string with std::map, std::vector and
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
34 # std::string
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
35 # replace int32 by int32_t
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
36 # replace float32 by float
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
37 # replace float64 by double
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
38 retVal : str = canonicalTypeName.replace("map","std::map")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
39 retVal : str = canonicalTypeName.replace("vector","std::vector")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
40 retVal : str = canonicalTypeName.replace("int32","int32_t")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
41 retVal : str = canonicalTypeName.replace("float32","float")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
42 retVal : str = canonicalTypeName.replace("float64","double")
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
43 return retVal
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
44
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
45 def GetTypeScriptTypeNameFromCanonical(canonicalTypeName : str) -> str:
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
46 # TS: replace vector with Array and map with Map
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
47 # string remains string
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
48 # replace int32 by number
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
49 # replace float32 by number
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
50 # replace float64 by number
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
51 retVal : str = canonicalTypeName.replace("map","Map")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
52 retVal : str = canonicalTypeName.replace("vector","Array")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
53 retVal : str = canonicalTypeName.replace("int32","number")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
54 retVal : str = canonicalTypeName.replace("float32","number")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
55 retVal : str = canonicalTypeName.replace("float64","number")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
56 retVal : str = canonicalTypeName.replace("bool","boolean")
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
57 return retVal
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
58
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
59 # class Schema:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
60 # def __init__(self, root_prefix : str, defined_types : List[Type]):
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
61 # self.rootName : str = root_prefix
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
62 # self.definedTypes : str = defined_types
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
63
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
64 def CheckTypeSchema(definedType : Dict) -> None:
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
65 allowedDefinedTypeKinds = ["enum","struct"]
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
66 if not definedType.has_key('name'):
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
67 raise Exception("type lacks the 'name' key")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
68 name = definedType['name']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
69 if not definedType.has_key('kind'):
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
70 raise Exception(f"type {name} lacks the 'kind' key")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
71 kind = definedType['kind']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
72 if not (kind in allowedDefinedTypeKinds):
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
73 raise Exception(f"type {name} : kind {kind} is not allowed. " +
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
74 f"It must be one of {allowedDefinedTypeKinds}")
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
75
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
76 if not definedType.has_key('fields'):
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
77 raise Exception("type {name} lacks the 'fields' key")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
78
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
79 # generic check on all kinds of types
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
80 fields = definedType['fields']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
81 for field in fields:
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
82 fieldName = field['name']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
83 if not field.has_key('name'):
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
84 raise Exception("field in type {name} lacks the 'name' key")
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
85
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
86 # fields in struct must have types
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
87 if kind == 'struct':
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
88 for field in fields:
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
89 fieldName = field['name']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
90 if not field.has_key('type'):
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
91 raise Exception(f"field {fieldName} in type {name} "
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
92 + "lacks the 'type' key")
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
93
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
94 def CheckSchemaSchema(schema : Dict) -> None:
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
95 if not schema.has_key('root_name'):
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
96 raise Exception("schema lacks the 'root_name' key")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
97 if not schema.has_key('types'):
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
98 raise Exception("schema lacks the 'types' key")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
99 for definedType in schema['types']:
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
100 CheckTypeSchema(definedType)
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
101
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
102 # def CreateAndCacheTypeObject(allTypes : Dict[str,Type], typeDict : Dict) -> None:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
103 # """This does not set the dependentTypes field"""
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
104 # typeName : str = typeDict['name']
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
105 # if allTypes.has_key(typeName):
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
106 # raise Exception(f'Type {typeName} is defined more than once!')
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
107 # else:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
108 # typeObject = Type(typeName, typeDict['kind'])
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
109 # allTypes[typeName] = typeObject
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
110
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
111 def EatToken(sentence : str) -> (str,str):
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
112 """splits "A,B,C" into "A" and "B,C" where A, B and C are type names
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
113 (including templates) like "int32", "TotoTutu", or
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
114 "map<map<int32,vector<string>>,map<string,int32>>" """
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
115
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
116 if sentence.count('<') != sentence.count('>'):
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
117 raise Exception(f"Error in the partial template type list {sentence}."
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
118 + " The number of < and > do not match!")
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
119
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
120 # the template level we're currently in
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
121 templateLevel = 0
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
122 for i in len(sentence):
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
123 if (sentence[i] == ",") and (templateLevel == 0):
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
124 return (sentence[0:i],sentence[i+1:])
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
125 elif (sentence[i] == "<"):
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
126 templateLevel += 1
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
127 elif (sentence[i] == ">"):
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
128 templateLevel -= 1
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
129 return (sentence,"")
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
130
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
131 def SplitListOfTypes(typeName : str) -> List[str]:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
132 """Splits something like
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
133 vector<string>,int32,map<string,map<string,int32>>
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
134 in:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
135 - vector<string>
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
136 - int32
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
137 map<string,map<string,int32>>
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
138
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
139 This is not possible with a regex so
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
140 """
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
141 stillStuffToEat : bool = True
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
142 tokenList = []
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
143 restOfString = typeName
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
144 while stillStuffToEat:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
145 firstToken,restOfString = EatToken(restOfString)
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
146 tokenList.append(firstToken)
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
147 return tokenList
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
148
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
149 templateRegex = \
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
150 re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
151
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
152 def ParseTemplateType(typeName) -> (bool,str,List[str]):
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
153 """ If the type is a template like "SOMETHING<SOME<THING,EL<SE>>>", then
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
154 it returns (true,"SOMETHING","SOME<THING,EL<SE>>")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
155 otherwise it returns (false,"","")"""
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
156
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
157 # let's remove all whitespace from the type
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
158 # split without argument uses any whitespace string as separator
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
159 # (space, tab, newline, return or formfeed)
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
160 typeName = "".join(typeName.split())
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
161 matches = templateRegex.match(typeName)
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
162 if matches == None:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
163 return (False,"","")
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
164 else:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
165 # we need to split with the commas that are outside of the defined types
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
166 # simply splitting at commas won't work
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
167 listOfDependentTypes = SplitListOfTypes(matches.group(2))
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
168 return (True,matches.group(1),listOfDependentTypes)
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
169
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
170
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
171 # def GetPrimitiveType(typeName : str) -> Type:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
172 # if allTypes.has_key(typeName):
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
173 # return allTypes[typeName]
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
174 # else:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
175 # primitiveTypes = ['int32', 'float32', 'float64', 'string']
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
176 # if not (typeName in primitiveTypes):
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
177 # raise Exception(f"Type {typeName} is unknown.")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
178 # typeObject = Type(typeName,'primitive')
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
179 # # there are no dependent types in a primitive type --> Type object
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
180 # # constrution is finished at this point
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
181 # allTypes[typeName] = typeObject
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
182 # return typeObject
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
183
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
184 def ProcessTypeTree(
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
185 ancestors : List[str]
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
186 , genOrderQueue : List[str]
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
187 , structTypes : Dict[str,Dict], typeName : str) -> None:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
188 if typeName in ancestors:
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
189 raise Exception(f"Cyclic dependency chain found: the last of {ancestors} "
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
190 + f"depends on {typeName} that is already in the list.")
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
191
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
192 if not (typeName in genOrderQueue):
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
193 # if we reach this point, it means the type is NOT a struct or an enum.
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
194 # it is another (non directly user-defined) type that we must parse and
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
195 # create. Let's do it!
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
196 (isTemplate,_,parameters) = ParseTemplateType(typeName)
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
197 if isTemplate:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
198 dependentTypeNames : List[str] = SplitListOfTypes(parameters)
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
199 for dependentTypeName in dependentTypeNames:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
200 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!!
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
201 # childAncestors.append(typeName)
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
202 ProcessTypeTree(ancestors, genOrderQueue,
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
203 structTypes, dependentTypeName)
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
204 else:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
205 if structTypes.has_key(typeName):
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
206 ProcessStructType_DepthFirstRecursive(genOrderQueue, structTypes,
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
207 structTypes[typeName])
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
208
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
209 def ProcessStructType_DepthFirstRecursive(
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
210 genOrderQueue : List[str], structTypes : Dict[str,Dict]
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
211 , typeDict : Dict) -> None:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
212 # let's generate the code according to the
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
213 typeName : str = typeDict['name']
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
214 if typeDict['kind'] != 'struct':
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
215 raise Exception(f"Unexpected kind '{typeDict['kind']}' for " +
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
216 "type '{typeName}'")
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
217 typeFields : List[Dict] = typeDict['fields']
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
218 for typeField in typeFields:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
219 ancestors = [typeName]
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
220 ProcessTypeTree(ancestors, genOrderQueue
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
221 , structTypes, typeField['name'])
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
222 # now we're pretty sure our dependencies have been processed,
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
223 # we can start marking our code for generation
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
224 genOrderQueue.append(typeName)
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
225
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
226 def ProcessEnumerationType(processedTypes, definedType) -> None:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
227 print(f"About to process enumeration: {definedType['name']}")
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
228
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
229 def ProcessSchema(schema : dict) -> None:
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
230 CheckSchemaSchema(schema)
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
231 rootName : str = schema['root_name']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
232 definedTypes : list = schema['types']
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
233
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
234 print(f"Processing schema. rootName = f{rootName}")
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
235 # this will be filled with the generation queue. That is, the type
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
236 # names in the order where they must be defined.
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
237 genOrderQueue : Set = set()
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
238
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
239 # the struct names are mapped to their JSON dictionary
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
240 structTypes : Dict[str,Dict] = {}
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
241
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
242 # the order here is the generation order
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
243 for definedType in definedTypes:
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
244 if definedType['kind'] == 'enum':
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
245 ProcessEnumerationType(genOrderQueue, definedType)
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
246
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
247 # the order here is NOT the generation order: the types
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
248 # will be processed according to their dependency graph
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
249 for definedType in definedTypes:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
250 if definedType['kind'] == 'struct':
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
251 structTypes[definedType['name']] = definedType
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
252 ProcessStructType_DepthFirstRecursive(genOrderQueue,structTypes,
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
253 definedType)
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
254
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
255 print(f"genOrderQueue = {genOrderQueue}")
468
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
256
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
257 if __name__ == '__main__':
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
258 import argparse
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
259 parser = argparse.ArgumentParser(
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
260 usage = """stonegentool.py [-h] [-o OUT_DIR] [-v] input_schemas
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
261 EXAMPLE: python command_gen.py -o "generated_files/" """
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
262 + """ "mainSchema.json,App Specific Commands.json" """)
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
263 parser.add_argument("input_schema", type=str,
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
264 help = "path to the schema file")
468
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
265 parser.add_argument("-o", "--out_dir", type=str, default=".",
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
266 help = """path of the directory where the files
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
267 will be generated. Default is current
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
268 working folder""")
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
269 parser.add_argument("-v", "--verbosity", action="count", default=0,
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
270 help = """increase output verbosity (0 == errors
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
271 only, 1 == some verbosity, 2 == nerd
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
272 mode""")
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
273
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
274 args = parser.parse_args()
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
275 inputSchemaFilename = args.input_schema
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
276 outDir = args.out_dir
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
277
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
278 print("input schema = " + str(inputSchemaFilename))
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
279 print("out dir = " + str(outDir))
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
280
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
281 ProcessSchema(LoadSchema(inputSchemaFilename))
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
282
468
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
283
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
284 ###################
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
285 ## ATTIC ##
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
286 ###################
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
287
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
288 # this works
468
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
289
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
290 if False:
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
291 obj = json.loads("""{
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
292 "firstName": "Alice",
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
293 "lastName": "Hall",
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
294 "age": 35
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
295 }""")
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
296 print(obj)