Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/stonegentool.py @ 489:f6b7f113cf27 bgo-commands-codegen
Ongoing work on code generation
author | bgo-osimis |
---|---|
date | Mon, 18 Feb 2019 07:46:59 +0100 |
parents | 8e40355a172b |
children | 6470248790db |
rev | line source |
---|---|
471 | 1 import json |
2 import re | |
473 | 3 import sys |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
4 from typing import ( |
489 | 5 Any, |
6 Dict, | |
7 Generator, | |
8 Iterable, | |
9 Iterator, | |
10 List, | |
11 Match, | |
12 Optional, | |
13 Tuple, | |
14 Union, | |
15 cast, | |
16 ) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
17 from io import StringIO |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
18 import time |
469 | 19 |
472 | 20 """ |
21 1 2 3 4 5 6 7 | |
22 12345678901234567890123456789012345678901234567890123456789012345678901234567890 | |
23 """ | |
24 | |
489 | 25 |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
26 class GeneratedCode: |
489 | 27 def __init__(self): |
28 | |
29 # file-wide preamble (#include directives, comment...) | |
30 self.cppPreamble = StringIO() | |
31 | |
32 self.cppEnums = StringIO() | |
33 self.cppStructs = StringIO() | |
34 self.cppDispatcher = StringIO() | |
35 self.cppHandler = StringIO() | |
473 | 36 |
489 | 37 # file-wide preamble (module directives, comment...) |
38 self.tsPreamble = StringIO() | |
39 | |
40 self.tsEnums = StringIO() | |
41 self.tsStructs = StringIO() | |
42 self.tsDispatcher = StringIO() | |
43 self.tsHandler = StringIO() | |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
44 |
489 | 45 def FlattenToFiles(self, outputDir: str): |
46 raise NotImplementedError() | |
47 | |
48 | |
49 raise Exception(""" | |
50 $$$$TODO check field names are unique | |
51 """) | |
473 | 52 |
53 class JsonHelpers: | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
54 """A set of utilities to perform JSON operations""" |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
55 |
473 | 56 @staticmethod |
57 def removeCommentsFromJsonContent(string): | |
58 """ | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
59 Remove comments from a JSON file |
473 | 60 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
61 Comments are not allowed in JSON but, i.e., Orthanc configuration files |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
62 contains C++ like comments that we need to remove before python can |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
63 parse the file |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
64 """ |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
65 # remove all occurrence streamed comments (/*COMMENT */) from string |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
66 string = re.sub(re.compile("/\*.*?\*/", re.DOTALL), "", string) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
67 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
68 # remove all occurrence singleline comments (//COMMENT\n ) from string |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
69 string = re.sub(re.compile("//.*?\n"), "", string) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
70 |
473 | 71 return string |
72 | |
73 @staticmethod | |
74 def loadJsonWithComments(path): | |
75 """ | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
76 Reads a JSON file that may contain C++ like comments |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
77 """ |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
78 with open(path, "r") as fp: |
473 | 79 fileContent = fp.read() |
80 fileContent = JsonHelpers.removeCommentsFromJsonContent(fileContent) | |
81 return json.loads(fileContent) | |
82 | |
83 | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
84 def LoadSchema(filePath: str): |
473 | 85 return JsonHelpers.loadJsonWithComments(filePath) |
469 | 86 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
87 |
472 | 88 # class Type: |
89 # def __init__(self, canonicalTypeName:str, kind:str): | |
90 # allowedTypeKinds = ["primitive","enum","struct","collection"] | |
91 # """dependent type is the list of canonical types this type depends on. | |
92 # For instance, vector<map<string,int32>> depends on map<string,int32> | |
93 # that, in turn, depends on string and int32 that, in turn, depend on | |
94 # nothing""" | |
95 # self.canonicalTypeName = canonicalTypeName | |
96 # assert(kind in allowedTypeKinds) | |
470 | 97 |
472 | 98 # def setDependentTypes(self, dependentTypes:List[Type]) -> None: |
99 # self.dependentTypes = dependentTypes | |
470 | 100 |
472 | 101 # def getDependentTypes(self) -> List[Type]: |
102 # return self.dependentTypes | |
470 | 103 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
104 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
105 def GetCppTypeNameFromCanonical(canonicalTypeName: str) -> str: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
106 # C++: prefix map vector and string with std::map, std::vector and |
472 | 107 # std::string |
469 | 108 # replace int32 by int32_t |
109 # replace float32 by float | |
110 # replace float64 by double | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
111 retVal: str = canonicalTypeName |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
112 retVal = retVal.replace("map", "std::map") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
113 retVal = retVal.replace("vector", "std::vector") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
114 retVal = retVal.replace("int32", "int32_t") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
115 retVal = retVal.replace("float32", "float") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
116 retVal = retVal.replace("float64", "double") |
469 | 117 return retVal |
470 | 118 |
489 | 119 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
120 def GetTypeScriptTypeNameFromCanonical(canonicalTypeName: str) -> str: |
469 | 121 # TS: replace vector with Array and map with Map |
122 # string remains string | |
123 # replace int32 by number | |
124 # replace float32 by number | |
125 # replace float64 by number | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
126 retVal: str = canonicalTypeName |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
127 retVal = retVal.replace("map", "Map") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
128 retVal = retVal.replace("vector", "Array") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
129 retVal = retVal.replace("int32", "number") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
130 retVal = retVal.replace("float32", "number") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
131 retVal = retVal.replace("float64", "number") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
132 retVal = retVal.replace("bool", "boolean") |
469 | 133 return retVal |
134 | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
135 |
472 | 136 # class Schema: |
137 # def __init__(self, root_prefix : str, defined_types : List[Type]): | |
138 # self.rootName : str = root_prefix | |
139 # self.definedTypes : str = defined_types | |
470 | 140 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
141 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
142 def CheckTypeSchema(definedType: Dict) -> None: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
143 allowedDefinedTypeKinds = ["enum", "struct"] |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
144 if not "name" in definedType: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
145 raise Exception("type lacks the 'name' key") |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
146 name = definedType["name"] |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
147 if not "kind" in definedType: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
148 raise Exception(f"type {name} lacks the 'kind' key") |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
149 kind = definedType["kind"] |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
150 if not (kind in allowedDefinedTypeKinds): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
151 raise Exception( |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
152 f"type {name} : kind {kind} is not allowed. " |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
153 + f"It must be one of {allowedDefinedTypeKinds}" |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
154 ) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
155 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
156 if not "fields" in definedType: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
157 raise Exception("type {name} lacks the 'fields' key") |
470 | 158 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
159 # generic check on all kinds of types |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
160 fields = definedType["fields"] |
470 | 161 for field in fields: |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
162 fieldName = field["name"] |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
163 if not "name" in field: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
164 raise Exception("field in type {name} lacks the 'name' key") |
470 | 165 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
166 # fields in struct must have types |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
167 if kind == "struct": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
168 for field in fields: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
169 fieldName = field["name"] |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
170 if not "type" in field: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
171 raise Exception( |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
172 f"field {fieldName} in type {name} " + "lacks the 'type' key" |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
173 ) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
174 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
175 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
176 def CheckSchemaSchema(schema: Dict) -> None: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
177 if not "root_name" in schema: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
178 raise Exception("schema lacks the 'root_name' key") |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
179 if not "types" in schema: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
180 raise Exception("schema lacks the 'types' key") |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
181 for definedType in schema["types"]: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
182 CheckTypeSchema(definedType) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
183 |
470 | 184 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
185 def EatToken(sentence: str) -> Tuple[str, str]: |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
186 """splits "A,B,C" into "A" and "B,C" where A, B and C are type names |
471 | 187 (including templates) like "int32", "TotoTutu", or |
188 "map<map<int32,vector<string>>,map<string,int32>>" """ | |
472 | 189 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
190 if sentence.count("<") != sentence.count(">"): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
191 raise Exception( |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
192 f"Error in the partial template type list {sentence}." |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
193 + " The number of < and > do not match!" |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
194 ) |
471 | 195 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
196 # the template level we're currently in |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
197 templateLevel = 0 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
198 for i in range(len(sentence)): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
199 if (sentence[i] == ",") and (templateLevel == 0): |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
200 return (sentence[0:i], sentence[i + 1 :]) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
201 elif sentence[i] == "<": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
202 templateLevel += 1 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
203 elif sentence[i] == ">": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
204 templateLevel -= 1 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
205 return (sentence, "") |
471 | 206 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
207 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
208 def SplitListOfTypes(typeName: str) -> List[str]: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
209 """Splits something like |
471 | 210 vector<string>,int32,map<string,map<string,int32>> |
211 in: | |
212 - vector<string> | |
213 - int32 | |
214 map<string,map<string,int32>> | |
215 | |
216 This is not possible with a regex so | |
217 """ | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
218 stillStuffToEat: bool = True |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
219 tokenList = [] |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
220 restOfString = typeName |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
221 while stillStuffToEat: |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
222 firstToken, restOfString = EatToken(restOfString) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
223 tokenList.append(firstToken) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
224 if restOfString == "": |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
225 stillStuffToEat = False |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
226 return tokenList |
471 | 227 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
228 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
229 templateRegex = re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>") |
472 | 230 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
231 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
232 def ParseTemplateType(typeName) -> Tuple[bool, str, List[str]]: |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
233 """ If the type is a template like "SOMETHING<SOME<THING,EL<SE>>>", then |
470 | 234 it returns (true,"SOMETHING","SOME<THING,EL<SE>>") |
235 otherwise it returns (false,"","")""" | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
236 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
237 # let's remove all whitespace from the type |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
238 # split without argument uses any whitespace string as separator |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
239 # (space, tab, newline, return or formfeed) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
240 typeName = "".join(typeName.split()) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
241 matches = templateRegex.match(typeName) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
242 if matches == None: |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
243 return (False, "", []) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
244 else: |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
245 m = cast(Match[str], matches) |
489 | 246 assert len(m.groups()) == 2 |
247 # we need to split with the commas that are outside of the | |
248 # defined types. Simply splitting at commas won't work | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
249 listOfDependentTypes = SplitListOfTypes(m.group(2)) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
250 return (True, m.group(1), listOfDependentTypes) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
251 |
470 | 252 |
472 | 253 # def GetPrimitiveType(typeName : str) -> Type: |
473 | 254 # if typeName in allTypes: |
472 | 255 # return allTypes[typeName] |
256 # else: | |
257 # primitiveTypes = ['int32', 'float32', 'float64', 'string'] | |
258 # if not (typeName in primitiveTypes): | |
259 # raise Exception(f"Type {typeName} is unknown.") | |
260 # typeObject = Type(typeName,'primitive') | |
261 # # there are no dependent types in a primitive type --> Type object | |
262 # # constrution is finished at this point | |
263 # allTypes[typeName] = typeObject | |
264 # return typeObject | |
470 | 265 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
266 |
471 | 267 def ProcessTypeTree( |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
268 ancestors: List[str], |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
269 genOrderQueue: List[str], |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
270 structTypes: Dict[str, Dict], |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
271 typeName: str, |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
272 ) -> None: |
489 | 273 if typeName in ancestors: |
274 raise Exception( | |
275 f"Cyclic dependency chain found: the last of {ancestors} " | |
276 + f"depends on {typeName} that is already in the list." | |
277 ) | |
278 | |
279 if not (typeName in genOrderQueue): | |
280 # if we reach this point, it means the type is NOT a struct or an enum. | |
281 # it is another (non directly user-defined) type that we must parse and | |
282 # create. Let's do it! | |
283 (isTemplate, _, dependentTypeNames) = ParseTemplateType(typeName) | |
284 if isTemplate: | |
285 for dependentTypeName in dependentTypeNames: | |
286 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!! | |
287 # childAncestors.append(typeName) | |
288 ProcessTypeTree( | |
289 ancestors, genOrderQueue, structTypes, dependentTypeName | |
290 ) | |
291 else: | |
292 if typeName in structTypes: | |
293 ProcessStructType_DepthFirstRecursive( | |
294 genOrderQueue, structTypes, structTypes[typeName] | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
295 ) |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
296 |
489 | 297 def ProcessStructType_DepthFirstRecursive(genOrderQueue: List[str], \ |
298 structTypes: Dict[str, Dict], typeDict: Dict) -> None: | |
299 # let's generate the code according to the | |
300 typeName: str = typeDict["name"] | |
301 if typeDict["kind"] != "struct": | |
302 raise Exception( | |
303 f"Unexpected kind '{typeDict['kind']}' for " + "type '{typeName}'" | |
304 ) | |
305 typeFields: List[Dict] = typeDict["fields"] | |
306 for typeField in typeFields: | |
307 ancestors = [typeName] | |
308 ProcessTypeTree(ancestors, genOrderQueue, structTypes, typeField["type"]) | |
309 # now we're pretty sure our dependencies have been processed, | |
310 # we can start marking our code for generation (it might already have | |
311 # been done if someone referenced us earlier) | |
312 if not typeName in genOrderQueue: | |
313 genOrderQueue.append(typeName) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
314 |
489 | 315 def ProcessEnumerationType(outputStreams: GeneratedCode, typeDict: Dict) -> None: |
316 tsText: StringIO = StringIO() | |
317 cppText: StringIO = StringIO() | |
474 | 318 |
489 | 319 tsText.write("enum %s\n" % typeDict["name"]) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
320 tsText.write("{\n") |
474 | 321 |
489 | 322 cppText.write("enum %s\n" % typeDict["name"]) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
323 cppText.write("{\n") |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
324 |
489 | 325 for i in range(len(typeDict["fields"])): |
326 field = typeDict["fields"][i] | |
327 name = field["name"] | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
328 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
329 tsText.write(" %s" % name) |
489 | 330 if i < len(typeDict["fields"]) - 1: |
331 tsText.write(",") | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
332 tsText.write("\n") |
474 | 333 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
334 cppText.write(" %s" % name) |
489 | 335 if i < len(typeDict["fields"]) - 1: |
336 cppText.write(",") | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
337 cppText.write("\n") |
489 | 338 |
339 tsText.write("};\n\n") | |
340 cppText.write("};\n\n") | |
474 | 341 |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
342 outputStreams.tsEnums.write(tsText.getvalue()) |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
343 outputStreams.cppEnums.write(cppText.getvalue()) |
472 | 344 |
489 | 345 def GetSerializationCode(typeName: str,valueName: str, tempName: str) |
346 if IsPrimitiveType(typeName): | |
347 """ | |
348 json::Value val(objectTypeInt...) | |
349 val.setValue(valueName) <--- val | |
350 """ | |
351 elif IsArray(typeName) | |
352 """ | |
353 { | |
354 json::Value val(objectTypeArray...) | |
355 for(size_t i = 0; i < {fieldName}.size(); ++i) | |
356 { | |
357 json::Value val(objectTypeArray...) | |
358 } | |
359 val.setValue(valueName) | |
360 // <--- the calling code will insert collection/field writing here, | |
361 // like "parent.set("{fieldName}",val) or parent.append(val) | |
362 $collectValue | |
363 } | |
364 """ | |
365 | |
366 | |
367 | |
368 | |
369 def ProcessStructType(outputStreams: GeneratedCode, typeDict) -> None: | |
370 tsText: StringIO = StringIO() | |
371 cppText: StringIO = StringIO() | |
372 | |
373 tsText.write("class %s\n" % typeDict["name"]) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
374 tsText.write("{\n") |
474 | 375 |
489 | 376 cppText.write("struct %s\n" % typeDict["name"]) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
377 cppText.write("{\n") |
474 | 378 |
489 | 379 """ |
380 | |
381 GenerateSerializationCode(typeName,valueName) | |
382 | |
383 primitives: | |
384 ----------- | |
385 int | |
386 jsonValue val(objectInt); | |
387 val.setValue("$name") | |
388 parent.add(("$name",$name) | |
389 double | |
390 ... | |
391 string | |
392 ... | |
393 | |
394 collections: | |
395 ----------- | |
396 dict { } | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 serializeValue() | |
403 """ | |
404 | |
405 for i in range(len(typeDict["fields"])): | |
406 field = typeDict["fields"][i] | |
407 name = field["name"] | |
408 tsType = GetTypeScriptTypeNameFromCanonical(field["type"]) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
409 tsText.write(" public %s %s;\n" % (tsType, name)) |
489 | 410 cppType = GetCppTypeNameFromCanonical(field["type"]) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
411 cppText.write(" %s %s;\n" % (cppType, name)) |
489 | 412 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
413 tsText.write("};\n\n") |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
414 cppText.write("};\n\n") |
474 | 415 |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
416 outputStreams.tsStructs.write(tsText.getvalue()) |
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
417 outputStreams.cppStructs.write(cppText.getvalue()) |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
418 |
489 | 419 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
420 def WritePreambles(rootName: str, outputStreams: GeneratedCode) -> None: |
489 | 421 outputStreams.cppPreamble.write( |
422 """// autogenerated by stonegentool on %s for module %s | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
423 #include <cstdint> |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
424 #include <string> |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
425 #include <vector> |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
426 #include <map> |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
427 |
489 | 428 """ % (time.ctime(), rootName)) |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
429 |
489 | 430 outputStreams.tsPreamble.write( |
431 """// autogenerated by stonegentool on %s for module %s | |
432 """ % (time.ctime(), rootName)) | |
433 | |
470 | 434 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
435 def ProcessSchema(schema: dict) -> Tuple[str, GeneratedCode, List[str]]: |
489 | 436 CheckSchemaSchema(schema) |
437 rootName: str = schema["root_name"] | |
438 definedTypes: list = schema["types"] | |
470 | 439 |
489 | 440 # this will be filled with the generation queue. That is, the type |
441 # names in the order where they must be defined. | |
442 genOrderQueue: List = [] | |
470 | 443 |
489 | 444 # the struct names are mapped to their JSON dictionary |
445 structTypes: Dict[str, Dict] = {} | |
470 | 446 |
489 | 447 outputStreams: GeneratedCode = GeneratedCode() |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
448 |
489 | 449 WritePreambles(rootName, outputStreams) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
450 |
489 | 451 # the order here is the generation order |
452 for definedType in definedTypes: | |
453 if definedType["kind"] == "enum": | |
454 ProcessEnumerationType(outputStreams, definedType) | |
474 | 455 |
489 | 456 for definedType in definedTypes: |
457 if definedType["kind"] == "struct": | |
458 structTypes[definedType["name"]] = definedType | |
470 | 459 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
460 # the order here is NOT the generation order: the types |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
461 # will be processed according to their dependency graph |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
462 for definedType in definedTypes: |
489 | 463 if definedType["kind"] == "struct": |
464 ProcessStructType_DepthFirstRecursive( | |
465 genOrderQueue, structTypes, definedType | |
466 ) | |
472 | 467 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
468 for i in range(len(genOrderQueue)): |
489 | 469 typeName = genOrderQueue[i] |
470 typeDict = structTypes[typeName] | |
471 ProcessStructType(outputStreams, typeDict) | |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
472 |
489 | 473 return (rootName, outputStreams, genOrderQueue) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
474 |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
475 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
476 def WriteStreamsToFiles(rootName: str, outputStreams: Dict[str, StringIO]) -> None: |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
477 pass |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
478 |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
479 if __name__ == "__main__": |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
480 import argparse |
468 | 481 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
482 parser = argparse.ArgumentParser( |
489 | 483 usage="""stonegentool.py [-h] [-o OUT_DIR] [-v] input_schemas |
484 EXAMPLE: python command_gen.py -o "generated_files/" """ | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
485 + """ "mainSchema.json,App Specific Commands.json" """ |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
486 ) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
487 parser.add_argument("input_schema", type=str, help="path to the schema file") |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
488 parser.add_argument( |
489 | 489 "-o", |
490 "--out_dir", | |
491 type=str, | |
492 default=".", | |
493 help="""path of the directory where the files | |
494 will be generated. Default is current | |
495 working folder""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
496 ) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
497 parser.add_argument( |
489 | 498 "-v", |
499 "--verbosity", | |
500 action="count", | |
501 default=0, | |
502 help="""increase output verbosity (0 == errors | |
503 only, 1 == some verbosity, 2 == nerd | |
504 mode""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
505 ) |
468 | 506 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
507 args = parser.parse_args() |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
508 inputSchemaFilename = args.input_schema |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
509 outDir = args.out_dir |
468 | 510 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
511 (rootName, outputStreams, _) = ProcessSchema(LoadSchema(inputSchemaFilename)) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
512 WriteStreamsToFiles(rootName, outputStreams) |
482
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
513 |