1 module IDL.IdlParseStruct; 2 3 import std.array; 4 import std.regex; 5 import std.conv; 6 import std.stdio; 7 import std.array : appender; 8 import std.format; 9 import std.uni; 10 11 import IDL.IdlBaseInterface; 12 import IDL.IdlUnit; 13 import IDL.IdlSymbol; 14 import IDL.IdlStructCreateCode; 15 import IDL.IdlParseInterface; 16 17 class MemberAttr{ 18 19 this(string type, string member) 20 { 21 22 auto type_split = split(type,"[]"); 23 24 if(type_split.length > 1) 25 { 26 typeName = type_split[0]; 27 isArray = true; 28 }else 29 { 30 typeName = type; 31 isArray = false; 32 } 33 34 if(typeName == "string") 35 { 36 isString = true; 37 }else 38 { 39 isString = false; 40 } 41 42 if(idlStructList.get(typeName, null) !is null) 43 { 44 isClass = true; 45 }else 46 { 47 if(idlDlangVariable.get(typeName, null) is null) 48 { 49 throw new Exception("Idl Incorrect type, type name:" ~ typeName); 50 } 51 52 isClass = false; 53 } 54 55 memberName = member; 56 57 writefln("message member: %s: %s", memberName, type); 58 59 } 60 61 string getTypeName() 62 { 63 return typeName; 64 } 65 66 string getVarName() 67 { 68 return memberName; 69 } 70 71 72 public: 73 string typeName; 74 string memberName; 75 bool isArray; 76 bool isString; 77 bool isClass; 78 } 79 80 81 class IdlParseStruct: IdlBaseInterface 82 { 83 bool parse(string filePath, string name, string structBodys) 84 { 85 if(name[0].isUpper == false) 86 { 87 throw new Exception("parse meesgae name is failed, The first character of the name is capitalized, message name:" ~ name); 88 } 89 90 91 this.structName = name; 92 93 auto MemberAttrList = split(structBodys, ";"); 94 95 if(MemberAttrList.length < 1) 96 { 97 throw new Exception("parse meesgae member attr is failed, " ~ structBodys); 98 } 99 100 writeln("----------------------------"); 101 writefln("message name:%s ", name); 102 103 foreach(attr; MemberAttrList) 104 { 105 if(attr.empty == false) 106 { 107 auto member = split(attr, ":"); 108 109 if(member.length > 1) 110 { 111 int index = to!(int)(member[1]); 112 113 auto memberFlag = split(member[0], " "); 114 115 if(memberFlag.length < 2) 116 { 117 throw new Exception("parse message member flag is failed, " ~ member[0]); 118 } 119 120 memberAttrInfo[index] = new MemberAttr(memberFlag[0], memberFlag[1]); 121 }else 122 { 123 throw new Exception("parse message name:" ~ name ~ ", member:" ~ attr ~", please set the number!!!"); 124 } 125 } 126 } 127 128 writeln("----------------------------\n\n"); 129 130 return true; 131 } 132 133 134 string getName() 135 { 136 return this.structName; 137 } 138 139 140 141 string createCodeForLanguage(CODE_LANGUAGE language) 142 { 143 string codeText; 144 145 switch(language) 146 { 147 case CODE_LANGUAGE.CL_CPP:break; 148 case CODE_LANGUAGE.CL_DLANG: codeText = IdlStructDlangCode.createServerCode(this); break; 149 case CODE_LANGUAGE.CL_GOLANG:break; 150 case CODE_LANGUAGE.CL_JAVA:break; 151 152 default: 153 throw new Exception("language is not exits!!"); 154 } 155 156 return codeText; 157 } 158 159 160 string createServerCodeForInterface(CODE_LANGUAGE language) 161 { 162 return ""; 163 } 164 165 string createServerCodeForService(CODE_LANGUAGE language) 166 { 167 return ""; 168 } 169 170 string createClientCodeForService(CODE_LANGUAGE language) 171 { 172 return ""; 173 } 174 175 string createClientCodeForInterface(CODE_LANGUAGE language) 176 { 177 return ""; 178 } 179 180 static uint DeserializeRecursive = -1; 181 static string createDeserializeCodeForFlatbuffer(IdlParseStruct structInfo, string varName, string fbName) 182 { 183 auto strings = appender!string(); 184 185 auto memberAttrInfo = structInfo.memberAttrInfo; 186 auto structName = structInfo.structName; 187 DeserializeRecursive++; 188 189 for(int i=1; i<=memberAttrInfo.length; i++) 190 { 191 string iterName = memberAttrInfo[i].getVarName; 192 string iterType = memberAttrInfo[i].getTypeName; 193 194 foreach(j; 0..DeserializeRecursive) formattedWrite(strings, "\t"); 195 196 if(memberAttrInfo[i].isArray) 197 { 198 if(memberAttrInfo[i].isClass) 199 { 200 auto tmpName = stringToLower(iterType, 0)~"Tmp"; 201 202 formattedWrite(strings, "\t\tforeach(%s; %s.%s){\n\n", iterName, fbName, iterName); 203 formattedWrite(strings, "\t\t\t%s %s;\n", iterType, tmpName); 204 formattedWrite(strings, "%s", IdlParseStruct.createDeserializeCodeForFlatbuffer(idlStructList[iterType], tmpName, iterName)); 205 formattedWrite(strings, "\t\t\t%s.%s ~= %s;\n", varName, iterName, tmpName); 206 }else 207 { 208 formattedWrite(strings, "\t\tforeach(%s; %s.%s){\n\n", iterName, fbName, iterName); 209 formattedWrite(strings, "\t\t\t%s.%s ~= %s;\n", varName, iterName, iterName); 210 } 211 212 formattedWrite(strings, "\t\t}\n\n"); 213 214 }else if(memberAttrInfo[i].isClass) 215 { 216 formattedWrite(strings, "\t\t%s %s;\n", iterType, iterName); 217 formattedWrite(strings, "%s", IdlParseStruct.createDeserializeCodeForFlatbuffer(idlStructList[iterType], iterName, iterName)); 218 }else 219 { 220 formattedWrite(strings, "\t\t%s.%s = %s.%s;\n", varName, iterName, fbName, iterName); 221 } 222 } 223 224 DeserializeRecursive--; 225 return strings.data; 226 } 227 228 229 230 static uint serializeRecursive = -1; 231 static string createSerializeCodeForFlatbuffer(IdlParseStruct structInfo, string varName) 232 { 233 auto strings = appender!string(); 234 auto argsStrings = appender!string(); 235 236 auto memberAttrInfo = structInfo.memberAttrInfo; 237 auto structName = structInfo.structName; 238 239 serializeRecursive++; 240 241 242 for(int i = 1; i <= memberAttrInfo.length; i++) 243 { 244 string iterName = memberAttrInfo[i].getVarName; 245 string iterType = memberAttrInfo[i].getTypeName; 246 247 if(memberAttrInfo[i].isArray) 248 { 249 if(memberAttrInfo[i].isClass) 250 { 251 formattedWrite(strings, "uint[] %sPosArray;\n", iterName); 252 formattedWrite(strings, "\t\tforeach(%s; %s.%s){\n\n", iterName, varName, iterName); 253 254 formattedWrite(strings, "\t\t%s", IdlParseStruct.createSerializeCodeForFlatbuffer(idlStructList[iterType], iterName)); 255 256 formattedWrite(strings, "\t\t\t%sPosArray ~= %sPos;\n", iterName, iterName); 257 258 }else if(memberAttrInfo[i].isString) 259 { 260 formattedWrite(strings, "uint[] %sPosArray;\n", iterName); 261 formattedWrite(strings, "\t\tforeach(%s; %s.%s){\n\n", iterName, varName, iterName); 262 263 formattedWrite(strings, "\t\t\tauto %sPos = builder.createString(%s);\n", iterName, iterName); 264 formattedWrite(strings, "\t\t\t%sPosArray ~= %sPos;\n", iterName, iterName); 265 266 }else 267 { 268 formattedWrite(strings, "%s[] %sPosArray;\n", iterType, iterName); 269 formattedWrite(strings, "\t\tforeach(%s; %s.%s){\n\n", iterName, varName, iterName); 270 271 formattedWrite(strings, "\t\t\t%sPosArray ~= %s;\n", iterName, iterName); 272 } 273 274 formattedWrite(strings, "\t\t}\n\n"); 275 276 formattedWrite(argsStrings, "%sFB.create%sVector(builder, %sPosArray), ", structName, stringToUpper(iterName, 0), iterName); 277 278 foreach(j; 0..serializeRecursive) formattedWrite(strings, "\t"); 279 280 281 }else if(memberAttrInfo[i].isClass) 282 { 283 formattedWrite(strings, "\t\tauto %s = %s.%s;\n", iterName, varName, iterName); 284 formattedWrite(strings, IdlParseStruct.createSerializeCodeForFlatbuffer(idlStructList[iterType], iterName)); 285 formattedWrite(argsStrings, "%sPos, ", iterName); 286 287 }else if(memberAttrInfo[i].isString) 288 { 289 formattedWrite(argsStrings, "builder.createString(%s.%s), ", varName, iterName); 290 } 291 else 292 { 293 formattedWrite(argsStrings, "%s.%s, ", varName, iterName); 294 } 295 } 296 297 formattedWrite(strings, "\t\tauto %sPos = %sFB.create%sFB(builder, %s);\n", varName, structName, structName, argsStrings.data); 298 299 serializeRecursive--; 300 return strings.data; 301 } 302 303 304 305 public: 306 string structName; 307 MemberAttr[int] memberAttrInfo; 308 } 309