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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
471
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
1 import json
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
2 import re
473
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
5 Any,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
6 Dict,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
7 Generator,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
8 Iterable,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
9 Iterator,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
10 List,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
11 Match,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
12 Optional,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
13 Tuple,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
14 Union,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
15 cast,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
19
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
20 """
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
21 1 2 3 4 5 6 7
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
22 12345678901234567890123456789012345678901234567890123456789012345678901234567890
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
23 """
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
26 def __init__(self):
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
27
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
28 # file-wide preamble (#include directives, comment...)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
29 self.cppPreamble = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
30
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
31 self.cppEnums = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
32 self.cppStructs = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
33 self.cppDispatcher = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
34 self.cppHandler = StringIO()
473
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
35
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
36 # file-wide preamble (module directives, comment...)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
37 self.tsPreamble = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
38
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
39 self.tsEnums = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
40 self.tsStructs = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
41 self.tsDispatcher = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
44 def FlattenToFiles(self, outputDir: str):
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
45 raise NotImplementedError()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
46
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
47
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
48 raise Exception("""
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
49 $$$$TODO check field names are unique
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
50 """)
473
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
51
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
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
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
55 @staticmethod
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
56 def removeCommentsFromJsonContent(string):
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
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
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
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
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
70 return string
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
71
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
72 @staticmethod
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
73 def loadJsonWithComments(path):
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
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
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
78 fileContent = fp.read()
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
79 fileContent = JsonHelpers.removeCommentsFromJsonContent(fileContent)
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
80 return json.loads(fileContent)
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
81
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
82
490
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
83 def LoadSchemaFromJson(filePath: str):
473
628941d63b8c Ongoing work. Parsing tests work
bgo-osimis
parents: 472
diff changeset
84 return JsonHelpers.loadJsonWithComments(filePath)
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
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
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
88 # std::string
469
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
89 # replace int32 by int32_t
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
90 # replace float32 by float
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
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
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
98 return retVal
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
101 # TS: replace vector with Array and map with Map
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
102 # string remains string
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
103 # replace int32 by number
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
104 # replace float32 by number
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
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
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
113 return retVal
52549faf47ba Ongoing code generation work
bgo-osimis
parents: 468
diff changeset
114
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
115 # class Schema:
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
116 # def __init__(self, root_prefix : str, defined_types : List[Type]):
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
117 # self.rootName : str = root_prefix
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
118 # self.definedTypes : str = defined_types
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
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
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
165 (including templates) like "int32", "TotoTutu", or
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
166 "map<map<int32,vector<string>>,map<string,int32>>" """
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
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
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
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
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
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
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
188 vector<string>,int32,map<string,map<string,int32>>
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
189 in:
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
190 - vector<string>
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
191 - int32
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
192 map<string,map<string,int32>>
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
193
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
194 This is not possible with a regex so
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
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
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
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
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
207 templateRegex = \
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
208 re.compile(r"([a-zA-Z0-9_]*[a-zA-Z0-9_]*)<([a-zA-Z0-9_,:<>]+)>")
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
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
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
213 it returns (true,"SOMETHING","SOME<THING,EL<SE>>")
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
225 assert len(m.groups()) == 2
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
226 # we need to split with the commas that are outside of the
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
125c19b294e3 Ongoing codegen work
bgo-osimis
parents: 470
diff changeset
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
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
235 typeName: str) -> None:
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
236 if typeName in ancestors:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
237 raise Exception(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
238 f"Cyclic dependency chain found: the last of {ancestors} "
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
239 + f"depends on {typeName} that is already in the list."
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
240 )
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
241
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
242 if not (typeName in genOrderQueue):
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
243 # if we reach this point, it means the type is NOT a struct or an enum.
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
244 # it is another (non directly user-defined) type that we must parse and
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
245 # create. Let's do it!
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
246 (isTemplate, _, dependentTypeNames) = ParseTemplateType(typeName)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
247 if isTemplate:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
248 for dependentTypeName in dependentTypeNames:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
249 # childAncestors = ancestors.copy() NO TEMPLATE ANCESTOR!!!
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
250 # childAncestors.append(typeName)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
251 ProcessTypeTree(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
252 ancestors, genOrderQueue, structTypes, dependentTypeName
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
253 )
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
254 else:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
255 if typeName in structTypes:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
256 ProcessStructType_DepthFirstRecursive(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
260 def ProcessStructType_DepthFirstRecursive(genOrderQueue: List[str], \
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
261 structTypes: Dict[str, Dict], typeDict: Dict) -> None:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
262 # let's generate the code according to the
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
263 typeName: str = typeDict["name"]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
264 if typeDict["kind"] != "struct":
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
265 raise Exception(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
266 f"Unexpected kind '{typeDict['kind']}' for " + "type '{typeName}'"
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
267 )
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
268 typeFields: List[Dict] = typeDict["fields"]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
269 for typeField in typeFields:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
270 ancestors = [typeName]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
271 ProcessTypeTree(ancestors, genOrderQueue, structTypes, typeField["type"])
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
272 # now we're pretty sure our dependencies have been processed,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
273 # we can start marking our code for generation (it might already have
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
274 # been done if someone referenced us earlier)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
275 if not typeName in genOrderQueue:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
278 def ProcessEnumerationType(outputStreams: GeneratedCode, typeDict: Dict) -> None:
490
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
279
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
280 # enumeration declarations
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
281 tsDeclText: StringIO = StringIO()
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
282 tsDeclText.write("enum %s\n" % typeDict["name"])
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
283 tsDeclText.write("{\n")
474
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
284
490
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
285 cppDeclText: StringIO = StringIO()
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
286 cppDeclText.write("enum %s\n" % typeDict["name"])
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
287 cppDeclText.write("{\n")
474
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
288
490
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
289 cppToStringText: StringIO = StringIO()
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
290 cppToStringText.write("enum %s\n" % typeDict["name"])
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
291 cppToStringText.write("{\n")
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
292
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
293 cppFromStringText: StringIO = StringIO()
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
294 cppFromStringText.write("enum %s\n" % typeDict["name"])
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
297 for i in range(len(typeDict["fields"])):
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
298 field = typeDict["fields"][i]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
302 if i < len(typeDict["fields"]) - 1:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
307 if i < len(typeDict["fields"]) - 1:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
310
490
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
311 tsText.write("};\n\n")
6470248790db ongoing codegen work
bgo-osimis
parents: 489
diff changeset
312 cppText.write("};\n\n")
474
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
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
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
316
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
317 def GetSerializationCode(typeName: str,valueName: str, tempName: str)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
318 if IsPrimitiveType(typeName):
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
319 """
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
320 json::Value val(objectTypeInt...)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
321 val.setValue(valueName) <--- val
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
322 """
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
323 elif IsArray(typeName)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
324 """
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
325 {
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
326 json::Value val(objectTypeArray...)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
327 for(size_t i = 0; i < {fieldName}.size(); ++i)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
328 {
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
329 json::Value val(objectTypeArray...)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
330 }
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
331 val.setValue(valueName)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
332 // <--- the calling code will insert collection/field writing here,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
333 // like "parent.set("{fieldName}",val) or parent.append(val)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
334 $collectValue
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
335 }
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
336 """
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
337
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
338
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
339
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
340 def ProcessStructType(outputStreams: GeneratedCode, typeDict) -> None:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
341 tsText: StringIO = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
342 cppText: StringIO = StringIO()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
343
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
346
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
349
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
350 """
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
351
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
352 GenerateSerializationCode(typeName,valueName)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
353
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
354 primitives:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
355 -----------
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
356 int
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
357 jsonValue val(objectInt);
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
358 val.setValue("$name")
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
359 parent.add(("$name",$name)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
360 double
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
361 ...
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
362 string
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
363 ...
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
364
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
365 collections:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
366 -----------
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
367 dict { }
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
368
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
369
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
370
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
371
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
372
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
373 serializeValue()
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
374 """
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
375
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
376 for i in range(len(typeDict["fields"])):
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
377 field = typeDict["fields"][i]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
378 name = field["name"]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
392 outputStreams.cppPreamble.write(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
401 outputStreams.tsPreamble.write(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
402 """// autogenerated by stonegentool on %s for module %s
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
403 """ % (time.ctime(), rootName))
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
404
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
407 CheckSchemaSchema(schema)
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
408 rootName: str = schema["root_name"]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
409 definedTypes: list = schema["types"]
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
410
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
411 # this will be filled with the generation queue. That is, the type
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
412 # names in the order where they must be defined.
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
413 genOrderQueue: List = []
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
414
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
415 # the struct names are mapped to their JSON dictionary
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
416 structTypes: Dict[str, Dict] = {}
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
417
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
422 # the order here is the generation order
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
423 for definedType in definedTypes:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
424 if definedType["kind"] == "enum":
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
425 ProcessEnumerationType(outputStreams, definedType)
474
38997ceb9bc6 Ongoing work on message code generation
bgo-osimis
parents: 473
diff changeset
426
489
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
427 for definedType in definedTypes:
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
428 if definedType["kind"] == "struct":
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
429 structTypes[definedType["name"]] = definedType
470
db093eb6b29d Ongoing code generation tool
bgo-osimis
parents: 469
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
434 if definedType["kind"] == "struct":
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
435 ProcessStructType_DepthFirstRecursive(
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
436 genOrderQueue, structTypes, definedType
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
437 )
472
3db3289e1c25 Ongoing codegen work
bgo-osimis
parents: 471
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
440 typeName = genOrderQueue[i]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
441 typeDict = structTypes[typeName]
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
454 usage="""stonegentool.py [-h] [-o OUT_DIR] [-v] input_schemas
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
460 "-o",
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
461 "--out_dir",
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
462 type=str,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
463 default=".",
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
464 help="""path of the directory where the files
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
465 will be generated. Default is current
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
469 "-v",
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
470 "--verbosity",
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
471 action="count",
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
472 default=0,
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
473 help="""increase output verbosity (0 == errors
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
474 only, 1 == some verbosity, 2 == nerd
f6b7f113cf27 Ongoing work on code generation
bgo-osimis
parents: 486
diff changeset
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
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
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
cef55b4e6c21 stonegentool first commit
bgo-osimis
parents:
diff changeset
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