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