Mercurial > hg > orthanc-stone
annotate Resources/CodeGeneration/stonegentool.py @ 497:d79f78971fae bgo-commands-codegen
Fixed cmake (removed missing useless file)
author | bgo-osimis |
---|---|
date | Sun, 24 Feb 2019 10:11:38 +0100 |
parents | 8b6ceae45ba0 |
children | ce49eae4c887 |
rev | line source |
---|---|
471 | 1 import json |
491 | 2 import yaml |
471 | 3 import re |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
4 import os |
473 | 5 import sys |
491 | 6 from jinja2 import Template |
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
|
7 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
|
8 import time |
469 | 9 |
472 | 10 """ |
11 1 2 3 4 5 6 7 | |
12 12345678901234567890123456789012345678901234567890123456789012345678901234567890 | |
13 """ | |
14 | |
491 | 15 # see https://stackoverflow.com/a/2504457/2927708 |
16 def trim(docstring): | |
17 if not docstring: | |
18 return '' | |
19 # Convert tabs to spaces (following the normal Python rules) | |
20 # and split into a list of lines: | |
21 lines = docstring.expandtabs().splitlines() | |
22 # Determine minimum indentation (first line doesn't count): | |
23 indent = sys.maxsize | |
24 for line in lines[1:]: | |
25 stripped = line.lstrip() | |
26 if stripped: | |
27 indent = min(indent, len(line) - len(stripped)) | |
28 # Remove indentation (first line is special): | |
29 trimmed = [lines[0].strip()] | |
30 if indent < sys.maxsize: | |
31 for line in lines[1:]: | |
32 trimmed.append(line[indent:].rstrip()) | |
33 # Strip off trailing and leading blank lines: | |
34 while trimmed and not trimmed[-1]: | |
35 trimmed.pop() | |
36 while trimmed and not trimmed[0]: | |
37 trimmed.pop(0) | |
38 # Return a single string: | |
39 return '\n'.join(trimmed) | |
40 | |
41 | |
42 class GenCode: | |
489 | 43 def __init__(self): |
44 | |
45 # file-wide preamble (#include directives, comment...) | |
46 self.cppPreamble = StringIO() | |
47 | |
48 self.cppEnums = StringIO() | |
49 self.cppStructs = StringIO() | |
50 self.cppDispatcher = StringIO() | |
51 self.cppHandler = StringIO() | |
473 | 52 |
489 | 53 # file-wide preamble (module directives, comment...) |
54 self.tsPreamble = StringIO() | |
55 | |
56 self.tsEnums = StringIO() | |
57 self.tsStructs = StringIO() | |
58 self.tsDispatcher = StringIO() | |
59 self.tsHandler = StringIO() | |
486
8e40355a172b
Unit tests OK for preambles, enums and structs in both TS and C++
bgo-osimis
parents:
485
diff
changeset
|
60 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
61 def FlattenToFiles(self, outputDir): |
489 | 62 raise NotImplementedError() |
63 | |
64 | |
473 | 65 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
|
66 """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
|
67 |
473 | 68 @staticmethod |
69 def removeCommentsFromJsonContent(string): | |
70 """ | |
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
|
71 Remove comments from a JSON file |
473 | 72 |
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
|
73 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
|
74 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
|
75 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
|
76 """ |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
77 # 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
|
78 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
|
79 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
80 # 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
|
81 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
|
82 |
473 | 83 return string |
84 | |
85 @staticmethod | |
86 def loadJsonWithComments(path): | |
87 """ | |
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
|
88 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
|
89 """ |
f58fe38c8c04
Ongoing work on codegen: ts and cpp enum and struct writing seem to be OK. No file write yet
bgo-osimis
parents:
474
diff
changeset
|
90 with open(path, "r") as fp: |
473 | 91 fileContent = fp.read() |
92 fileContent = JsonHelpers.removeCommentsFromJsonContent(fileContent) | |
93 return json.loads(fileContent) | |
94 | |
95 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
96 def LoadSchemaFromJson(filePath): |
473 | 97 return JsonHelpers.loadJsonWithComments(filePath) |
469 | 98 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
99 def CanonToCpp(canonicalTypename): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
100 # C++: prefix map vector and string with std::map, std::vector and |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
101 # std::string |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
102 # replace int32 by int32_t |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
103 # replace float32 by float |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
104 # replace float64 by double |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
105 retVal = canonicalTypename |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
106 retVal = retVal.replace("map", "std::map") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
107 retVal = retVal.replace("vector", "std::vector") |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
108 retVal = retVal.replace("string", "std::string") |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
109 retVal = retVal.replace("int32", "int32_t") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
110 retVal = retVal.replace("float32", "float") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
111 retVal = retVal.replace("float64", "double") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
112 return retVal |
470 | 113 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
114 def CanonToTs(canonicalTypename): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
115 # TS: replace vector with Array and map with Map |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
116 # string remains string |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
117 # replace int32 by number |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
118 # replace float32 by number |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
119 # replace float64 by number |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
120 retVal = canonicalTypename |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
121 retVal = retVal.replace("map", "Map") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
122 retVal = retVal.replace("vector", "Array") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
123 retVal = retVal.replace("int32", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
124 retVal = retVal.replace("float32", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
125 retVal = retVal.replace("float64", "number") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
126 retVal = retVal.replace("bool", "boolean") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
127 return retVal |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
128 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
129 def NeedsTsConstruction(enums, tsType): |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
130 if tsType == 'boolean': |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
131 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
132 elif tsType == 'number': |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
133 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
134 elif tsType == 'string': |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
135 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
136 else: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
137 enumNames = [] |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
138 for enum in enums: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
139 enumNames.append(enum['name']) |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
140 if tsType in enumNames: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
141 return False |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
142 return True |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
143 |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
144 def NeedsCppConstruction(canonTypename): |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
145 return False |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
146 |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
147 def RegisterTemplateFunction(template,func): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
148 """Makes a function callable by a jinja2 template""" |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
149 template.globals[func.__name__] = func |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
150 return func |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
151 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
152 def MakeTemplate(templateStr): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
153 template = Template(templateStr) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
154 RegisterTemplateFunction(template,CanonToCpp) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
155 RegisterTemplateFunction(template,CanonToTs) |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
156 RegisterTemplateFunction(template,NeedsTsConstruction) |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
157 RegisterTemplateFunction(template,NeedsCppConstruction) |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
158 return template |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
159 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
160 def MakeTemplateFromFile(templateFileName): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
161 templateFile = open(templateFileName, "r") |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
162 templateFileContents = templateFile.read() |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
163 return MakeTemplate(templateFileContents) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
164 templateFile.close() |
469 | 165 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
166 def EatToken(sentence): |
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
|
167 """splits "A,B,C" into "A" and "B,C" where A, B and C are type names |
471 | 168 (including templates) like "int32", "TotoTutu", or |
169 "map<map<int32,vector<string>>,map<string,int32>>" """ | |
472 | 170 |
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
|
171 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
|
172 raise Exception( |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
173 "Error in the partial template type list " + str(sentence) + "." |
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 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
|
175 ) |
471 | 176 |
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
|
177 # 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 return (sentence, "") |
471 | 187 |
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
|
188 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
189 def SplitListOfTypes(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
|
190 """Splits something like |
471 | 191 vector<string>,int32,map<string,map<string,int32>> |
192 in: | |
193 - vector<string> | |
194 - int32 | |
195 map<string,map<string,int32>> | |
196 | |
197 This is not possible with a regex so | |
198 """ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
199 stillStuffToEat = True |
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
|
200 tokenList = [] |
491 | 201 restOfString = 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 return tokenList |
471 | 208 |
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
|
209 |
490 | 210 templateRegex = \ |
211 re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>") | |
472 | 212 |
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
|
213 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
214 def ParseTemplateType(typename): |
491 | 215 """ If the type is a template like "SOMETHING<SOME<THING,EL<SE>>>", |
216 then it returns (true,"SOMETHING","SOME<THING,EL<SE>>") | |
470 | 217 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
|
218 |
f58fe38c8c04
Ongoing 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 # 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
|
220 # 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
|
221 # (space, tab, newline, return or formfeed) |
491 | 222 typename = "".join(typename.split()) |
223 matches = templateRegex.match(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
|
224 if matches == None: |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
225 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
|
226 else: |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
227 m = matches |
489 | 228 assert len(m.groups()) == 2 |
229 # we need to split with the commas that are outside of the | |
230 # 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
|
231 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
|
232 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
|
233 |
491 | 234 |
235 def ComputeOrderFromTypeTree( | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
236 ancestors, |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
237 genOrder, |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
238 shortTypename, schema): |
491 | 239 |
240 if shortTypename in ancestors: | |
489 | 241 raise Exception( |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
242 "Cyclic dependency chain found: the last of " + str(ancestors) + |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
243 + " depends on " + str(shortTypename) + " that is already in the list." |
489 | 244 ) |
245 | |
491 | 246 if not (shortTypename in genOrder): |
247 (isTemplate, _, dependentTypenames) = ParseTemplateType(shortTypename) | |
489 | 248 if isTemplate: |
491 | 249 # if it is a template, it HAS dependent types... They can be |
250 # anything (primitive, collection, enum, structs..). | |
251 # Let's process them! | |
252 for dependentTypename in dependentTypenames: | |
489 | 253 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!! |
491 | 254 # childAncestors.append(typename) |
255 ComputeOrderFromTypeTree( | |
256 ancestors, genOrder, dependentTypename, schema | |
489 | 257 ) |
258 else: | |
491 | 259 # If it is not template, we are only interested if it is a |
260 # dependency that we must take into account in the dep graph, | |
261 # i.e., a struct. | |
262 if IsShortStructType(shortTypename, schema): | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
263 struct = schema[GetLongTypename(shortTypename, schema)] |
491 | 264 # The keys in the struct dict are the member names |
265 # The values in the struct dict are the member types | |
266 for field in struct.keys(): | |
267 # we fill the chain of dependent types (starting here) | |
268 ancestors.append(shortTypename) | |
269 ComputeOrderFromTypeTree( | |
270 ancestors, genOrder, struct[field], schema) | |
271 # don't forget to restore it! | |
272 ancestors.pop() | |
273 | |
274 # now we're pretty sure our dependencies have been processed, | |
275 # we can start marking our code for generation (it might | |
276 # already have been done if someone referenced us earlier) | |
277 if not shortTypename in genOrder: | |
278 genOrder.append(shortTypename) | |
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
|
279 |
491 | 280 # +-----------------------+ |
281 # | Utility functions | | |
282 # +-----------------------+ | |
283 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
284 def IsShortStructType(typename, schema): |
491 | 285 fullStructName = "struct " + typename |
286 return (fullStructName in schema) | |
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
|
287 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
288 def GetLongTypename(shortTypename, schema): |
491 | 289 if shortTypename.startswith("enum "): |
290 raise RuntimeError('shortTypename.startswith("enum "):') | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
291 enumName = "enum " + shortTypename |
491 | 292 isEnum = enumName in schema |
490 | 293 |
491 | 294 if shortTypename.startswith("struct "): |
295 raise RuntimeError('shortTypename.startswith("struct "):') | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
296 structName = "struct " + shortTypename |
491 | 297 isStruct = ("struct " + shortTypename) in schema |
474 | 298 |
491 | 299 if isEnum and isStruct: |
300 raise RuntimeError('Enums and structs cannot have the same name') | |
474 | 301 |
491 | 302 if isEnum: |
303 return enumName | |
304 if isStruct: | |
305 return structName | |
306 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
307 def IsTypename(fullName): |
491 | 308 return (fullName.startswith("enum ") or fullName.startswith("struct ")) |
309 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
310 def IsEnumType(fullName): |
491 | 311 return fullName.startswith("enum ") |
490 | 312 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
313 def IsStructType(fullName): |
491 | 314 return fullName.startswith("struct ") |
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
|
315 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
316 def GetShortTypename(fullTypename): |
491 | 317 if fullTypename.startswith("struct "): |
318 return fullTypename[7:] | |
319 elif fullTypename.startswith("enum"): | |
320 return fullTypename[5:] | |
321 else: | |
322 raise RuntimeError \ | |
323 ('fullTypename should start with either "struct " or "enum "') | |
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
|
324 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
325 def CheckSchemaSchema(schema): |
491 | 326 if not "rootName" in schema: |
327 raise Exception("schema lacks the 'rootName' key") | |
328 for name in schema.keys(): | |
329 if (not IsEnumType(name)) and (not IsStructType(name)) and \ | |
330 (name != 'rootName'): | |
331 raise RuntimeError \ | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
332 ('Type "' + str(name) + '" should start with "enum " or "struct "') |
491 | 333 |
334 # TODO: check enum fields are unique (in whole namespace) | |
335 # TODO: check struct fields are unique (in each struct) | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
336 # TODO: check that in the source schema, there are spaces after each colon |
491 | 337 |
338 # +-----------------------+ | |
339 # | Main processing logic | | |
340 # +-----------------------+ | |
341 | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
342 def ComputeRequiredDeclarationOrder(schema): |
491 | 343 # sanity check |
344 CheckSchemaSchema(schema) | |
474 | 345 |
491 | 346 # we traverse the type dependency graph and we fill a queue with |
347 # the required struct types, in a bottom-up fashion, to compute | |
348 # the declaration order | |
349 # The genOrder list contains the struct full names in the order | |
350 # where they must be defined. | |
351 # We do not care about the enums here... They do not depend upon | |
352 # anything and we'll handle them, in their original declaration | |
353 # order, at the start | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
354 genOrder = [] |
491 | 355 for fullName in schema.keys(): |
356 if IsStructType(fullName): | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
357 realName = GetShortTypename(fullName) |
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
358 ancestors = [] |
491 | 359 ComputeOrderFromTypeTree(ancestors, genOrder, realName, schema) |
360 return genOrder | |
489 | 361 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
362 def ProcessSchema(schema, genOrder): |
491 | 363 # sanity check |
364 CheckSchemaSchema(schema) | |
472 | 365 |
491 | 366 # let's doctor the schema to clean it up a bit |
367 # order DOES NOT matter for enums, even though it's a list | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
368 enums = [] |
491 | 369 for fullName in schema.keys(): |
370 if IsEnumType(fullName): | |
371 # convert "enum Toto" to "Toto" | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
372 typename = GetShortTypename(fullName) |
491 | 373 enum = {} |
374 enum['name'] = typename | |
375 assert(type(schema[fullName]) == list) | |
376 enum['fields'] = schema[fullName] # must be a list | |
377 enums.append(enum) | |
489 | 378 |
491 | 379 # now that the order has been established, we actually store\ |
380 # the structs in the correct order | |
381 # the structs are like: | |
382 # example = [ | |
383 # { | |
384 # "name": "Message1", | |
385 # "fields": { | |
386 # "someMember":"int32", | |
387 # "someOtherMember":"vector<string>" | |
388 # } | |
389 # }, | |
390 # { | |
391 # "name": "Message2", | |
392 # "fields": { | |
393 # "someMember":"int32", | |
394 # "someOtherMember22":"vector<Message1>" | |
395 # } | |
396 # } | |
397 # ] | |
489 | 398 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
399 structs = [] |
491 | 400 for i in range(len(genOrder)): |
401 # this is already the short name | |
402 typename = genOrder[i] | |
403 fieldDict = schema["struct " + typename] | |
404 struct = {} | |
405 struct['name'] = typename | |
406 struct['fields'] = fieldDict | |
407 structs.append(struct) | |
489 | 408 |
491 | 409 templatingDict = {} |
410 templatingDict['enums'] = enums | |
411 templatingDict['structs'] = structs | |
412 templatingDict['rootName'] = schema['rootName'] | |
489 | 413 |
491 | 414 return templatingDict |
489 | 415 |
491 | 416 # +-----------------------+ |
417 # | Write to files | | |
418 # +-----------------------+ | |
474 | 419 |
491 | 420 # def WriteStreamsToFiles(rootName: str, genc: Dict[str, StringIO]) \ |
421 # -> None: | |
422 # pass | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
423 |
491 | 424 def LoadSchema(fn): |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
425 # latin-1 is a trick, when we do NOT care about NON-ascii chars but |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
426 # we wish to avoid using a decoding error handler |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
427 # (see http://python-notes.curiousefficiency.org/en/latest/python3/text_file_processing.html#files-in-an-ascii-compatible-encoding-best-effort-is-acceptable) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
428 # TL;DR: all 256 values are mapped to characters in latin-1 so the file |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
429 # contents never cause an error. |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
430 with open(fn, 'r', encoding='latin-1') as f: |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
431 schemaText = f.read() |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
432 assert(type(schemaText) == str) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
433 # ensure there is a space after each colon. Otherwise, dicts could be |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
434 # erroneously recognized as an array of strings containing ':' |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
435 for i in range(len(schemaText)-1): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
436 ch = schemaText[i] |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
437 nextCh = schemaText[i+1] |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
438 if ch == ':': |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
439 if not (nextCh == ' ' or nextCh == '\n'): |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
440 assert(False) |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
441 schema = yaml.load(schemaText) |
491 | 442 return schema |
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 |
491 | 444 def GetTemplatingDictFromSchemaFilename(fn): |
445 obj = LoadSchema(fn) | |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
446 genOrder = ComputeRequiredDeclarationOrder(obj) |
491 | 447 templatingDict = ProcessSchema(obj, genOrder) |
448 return templatingDict | |
470 | 449 |
491 | 450 # +-----------------------+ |
451 # | ENTRY POINT | | |
452 # +-----------------------+ | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
453 |
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
|
454 if __name__ == "__main__": |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
455 import argparse |
468 | 456 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
457 parser = argparse.ArgumentParser( |
491 | 458 usage="""stonegentool.py [-h] [-o OUT_DIR] [-v] input_schema |
459 EXAMPLE: python stonegentool.py -o "generated_files/" """ | |
460 + """ "mainSchema.yaml,App Specific Commands.json" """ | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
461 ) |
491 | 462 parser.add_argument("input_schema", type=str, \ |
463 help="path to the schema file") | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
464 parser.add_argument( |
489 | 465 "-o", |
466 "--out_dir", | |
467 type=str, | |
468 default=".", | |
469 help="""path of the directory where the files | |
470 will be generated. Default is current | |
471 working folder""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
472 ) |
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
473 parser.add_argument( |
489 | 474 "-v", |
475 "--verbosity", | |
476 action="count", | |
477 default=0, | |
478 help="""increase output verbosity (0 == errors | |
479 only, 1 == some verbosity, 2 == nerd | |
480 mode""", | |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
481 ) |
468 | 482 |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
483 args = parser.parse_args() |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
484 schemaFile = args.input_schema |
485
772516adcbf6
Ongoing work on code generation. Enums and structs OK in ts and cpp
bgo-osimis
parents:
482
diff
changeset
|
485 outDir = args.out_dir |
468 | 486 |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
487 tdico = GetTemplatingDictFromSchemaFilename(schemaFile) |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
488 |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
489 tsTemplateFile = \ |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
490 os.path.join(os.path.dirname(__file__), 'template.in.ts') |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
491 template = MakeTemplateFromFile(tsTemplateFile) |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
492 renderedTsCode = template.render(**tdico) |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
493 outputTsFile = os.path.join( \ |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
494 outDir,str(tdico['rootName']) + "_generated.ts") |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
495 with open(outputTsFile,"wt",encoding='utf8') as outFile: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
496 outFile.write(renderedTsCode) |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
497 |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
498 cppTemplateFile = \ |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
499 os.path.join(os.path.dirname(__file__), 'template.in.h') |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
500 template = MakeTemplateFromFile(cppTemplateFile) |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
501 renderedCppCode = template.render(**tdico) |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
502 outputCppFile = os.path.join( \ |
496
8b6ceae45ba0
Finished (untested) C++, html, typescript, tsc & browserify production.
bgo-osimis
parents:
494
diff
changeset
|
503 outDir, str(tdico['rootName']) + "_generated.hpp") |
494
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
504 with open(outputCppFile,"wt",encoding='utf8') as outFile: |
fc17251477d6
TS and CPP tests OK. Ongoing code for C++ program that reads list of serialized messages in N files. Requires conan
bgo-osimis
parents:
493
diff
changeset
|
505 outFile.write(renderedCppCode) |
491 | 506 |
507 # def GenEnumDecl(genc: GenCode, fullName: str, schema: Dict) -> None: | |
508 # """Writes the enumerations in genc""" | |
509 # enumDict:Dict=schema[fullName] | |
510 # # jinja2 template | |
511 # j2cppEnum = Template(trim( | |
512 # """ {{fullName}} | |
513 # { | |
514 # {% for key in enumDict.keys()%} | |
515 # {{key}}, | |
516 # {%endfor%} | |
517 # }; | |
518 # """)) | |
519 # j2cppEnumR = j2cppEnum.render(locals()) | |
520 # genc.cppEnums.write(j2cppEnumR) | |
521 | |
522 # j2tsEnum = Template(trim( | |
523 # """ export {{fullName}} | |
524 # { | |
525 # {% for key in enumDict.keys()%} | |
526 # {{key}}, | |
527 # {%endfor%} | |
528 # }; | |
529 # """)) | |
530 # j2cppEnumR = j2cppEnum.render(locals()) | |
531 # genc.tsEnums.write(j2cppEnumR) | |
532 | |
533 | |
534 | |
535 # def GetSerializationCode(typename: str,valueName: str, tempName: str) | |
536 # if IsPrimitiveType(typename) or IsTemplateCollection(typename): | |
537 # # no need to write code for the primitive types or collections. | |
538 # # It is handled in C++ by the template functions and in TS by | |
539 # # the JSON.stringify code. | |
540 # elif IsStructType(typename): | |
541 # pass | |
542 | |
543 # def GenStructTypeDeclAndSerialize(genc: GenCode, type, schema) -> None: | |
544 # ###### | |
545 # # CPP | |
546 # ###### | |
547 # sampleCpp = """ struct Message1 | |
548 # { | |
549 # int32_t a; | |
550 # std::string b; | |
551 # EnumMonth0 c; | |
552 # bool d; | |
553 # }; | |
554 | |
555 # Json::Value StoneSerialize(const Message1& value) | |
556 # { | |
557 # Json::Value result(Json::objectValue); | |
558 # result["a"] = StoneSerialize(value.a); | |
559 # result["b"] = StoneSerialize(value.b); | |
560 # result["c"] = StoneSerialize(value.c); | |
561 # result["d"] = StoneSerialize(value.d); | |
562 # return result; | |
563 # } | |
564 # """ | |
565 | |
566 | |
567 # ###### | |
568 # # TS | |
569 # ###### | |
570 # sampleTs = """ | |
571 # { | |
572 # export class Message1 { | |
573 # a: number; | |
574 # b: string; | |
575 # c: EnumMonth0; | |
576 # d: boolean; | |
577 # public StoneSerialize(): string { | |
578 # let container: object = {}; | |
579 # container['type'] = 'Message1'; | |
580 # container['value'] = this; | |
581 # return JSON.stringify(container); | |
582 # } | |
583 # }; | |
584 # } | |
585 # """ | |
586 | |
587 | |
588 | |
589 | |
590 # tsText: StringIO = StringIO() | |
591 # cppText: StringIO = StringIO() | |
592 | |
593 # tsText.write("class %s\n" % typeDict["name"]) | |
594 # tsText.write("{\n") | |
595 | |
596 # cppText.write("struct %s\n" % typeDict["name"]) | |
597 # cppText.write("{\n") | |
598 | |
599 # """ | |
600 | |
601 # GenerateSerializationCode(typename,valueName) | |
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
|
602 |
491 | 603 # primitives: |
604 # ----------- | |
605 # int | |
606 # jsonValue val(objectInt); | |
607 # val.setValue("$name") | |
608 # parent.add(("$name",$name) | |
609 # double | |
610 # ... | |
611 # string | |
612 # ... | |
613 | |
614 # collections: | |
615 # ----------- | |
616 # dict { } | |
617 | |
618 # serializeValue() | |
619 # """ | |
620 | |
621 # for i in range(len(typeDict["fields"])): | |
622 # field = typeDict["fields"][i] | |
623 # name = field["name"] | |
624 # tsType = GetTypeScriptTypenameFromCanonical(field["type"]) | |
625 # tsText.write(" public %s %s;\n" % (tsType, name)) | |
626 # cppType = GetCppTypenameFromCanonical(field["type"]) | |
627 # cppText.write(" %s %s;\n" % (cppType, name)) | |
628 | |
629 # tsText.write("};\n\n") | |
630 # cppText.write("};\n\n") | |
631 | |
632 # genc.tsStructs.write(tsText.getvalue()) | |
633 # genc.cppStructs.write(cppText.getvalue()) | |
634 | |
635 | |
636 # def GenerateCodeFromTsTemplate(genc) | |
637 | |
638 | |
639 # +-----------------------+ | |
640 # | CODE GENERATION | | |
641 # +-----------------------+ | |
642 | |
643 # def GenPreambles(rootName: str, genc: GenCode) -> None: | |
644 # cppPreambleT = Template(trim( | |
645 # """// autogenerated by stonegentool on {{time.ctime()}} | |
646 # // for module {{rootName}} | |
647 # #include <cstdint> | |
648 # #include <string> | |
649 # #include <vector> | |
650 # #include <map> | |
651 # namespace {{rootName}} | |
652 # { | |
653 # Json::Value StoneSerialize(int32_t value) | |
654 # { | |
655 # Json::Value result(value); | |
656 # return result; | |
657 # } | |
658 # Json::Value StoneSerialize(double value) | |
659 # { | |
660 # Json::Value result(value); | |
661 # return result; | |
662 # } | |
663 # Json::Value StoneSerialize(bool value) | |
664 # { | |
665 # Json::Value result(value); | |
666 # return result; | |
667 # } | |
668 # Json::Value StoneSerialize(const std::string& value) | |
669 # { | |
670 # // the following is better than | |
671 # Json::Value result(value.data(),value.data()+value.size()); | |
672 # return result; | |
673 # } | |
674 # template<typename T> | |
675 # Json::Value StoneSerialize(const std::map<std::string,T>& value) | |
676 # { | |
677 # Json::Value result(Json::objectValue); | |
678 | |
679 # for (std::map<std::string, T>::const_iterator it = value.cbegin(); | |
680 # it != value.cend(); ++it) | |
681 # { | |
682 # // it->first it->second | |
683 # result[it->first] = StoneSerialize(it->second); | |
684 # } | |
685 # return result; | |
686 # } | |
687 # template<typename T> | |
688 # Json::Value StoneSerialize(const std::vector<T>& value) | |
689 # { | |
690 # Json::Value result(Json::arrayValue); | |
691 # for (size_t i = 0; i < value.size(); ++i) | |
692 # { | |
693 # result.append(StoneSerialize(value[i])); | |
694 # } | |
695 # return result; | |
696 # } | |
697 # """ | |
698 # cppPreambleR = cppPreambleT.render(locals()) | |
699 # genc.cppPreamble.write(cppPreambleR) | |
700 | |
701 # tsPreambleT = Template(trim( | |
702 # """// autogenerated by stonegentool on {{time.ctime()}} | |
703 # // for module {{rootName}} | |
704 | |
705 # namespace {{rootName}} | |
706 # { | |
707 # """ | |
708 # tsPreambleR = tsPreambleT.render(locals()) | |
709 # genc.tsPreamble.write(tsPreambleR) | |
710 | |
711 # def ComputeOrder_ProcessStruct( \ | |
712 # genOrder: List[str], name:str, schema: Dict[str, str]) -> None: | |
713 # # let's generate the code according to the | |
714 # struct = schema[name] | |
715 | |
716 # if not IsStructType(name): | |
717 # raise Exception(f'{typename} should start with "struct "') | |
493
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
718 |
6fbf2eae7c88
All unit tests pass for generation, including handler and dispatcher
bgo-osimis
parents:
491
diff
changeset
|
719 |