1 /* 2 * Copyright (c) 2025 Shenzhen Kaihong Digital. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package gen; 17 18 import grammar.*; 19 import utils.FileUtils; 20 import utils.StringUtils; 21 22 import java.io.File; 23 import java.util.List; 24 import java.util.Locale; 25 import java.util.Map; 26 27 /** 28 * <h3>类名:该类用于xxx</h3> 29 * description ${description} 30 * 31 * @author ${USER} 32 * date 2025-02-28 33 * @since 2025-02-28 34 * @version 1.0 35 */ 36 public class GenAkiCppFile extends GeneratorBase { 37 private static final String AKI_ENUM_TOKEN = "enum"; 38 private static final String AKI_CLASS_TOKEN = "class"; 39 private static final String AKI_STRUCT_TOKEN = "struct"; 40 private static final String AKI_UNION_TOKEN = "union"; 41 private static final String AKI_TEMPLATE_TOKEN = "template"; 42 private static final String AKI_TYPE_NAME_TOKEN = "typename"; 43 private static final String AKI_STAR_TOKEN = "*"; 44 private static final String AKI_CHAR_START_TOKEN = "char*"; 45 private static final String AKI_STD_STRING_TOKEN = "std::string"; 46 private static final String AKI_AUTO_TOKEN = "auto"; 47 private static final String AKI_EXPORT_TOKEN = "export"; 48 private static final String AKI_IMPLEMENAKI_TOKEN = "implements"; 49 private static final String AKI_EXTENDS_TOKEN = "extends"; 50 private static final String AKI_CONST_TOKEN = "const"; 51 private static final String AKI_PRIVATE_TOKEN = "private"; 52 private static final String AKI_PUBLIC_TOKEN = "public"; 53 private static final String AKI_INTERFACE_TOKEN = "interface"; 54 private static final String AKI_PROTECTED_TOKEN = "protected"; 55 private static final String AKI_STATIC_TOKEN = "static"; 56 private static final String AKI_ANY_TOKEN = "any"; 57 private static final String AKI_NUMBER_TOKEN = "number"; 58 private static final String AKI_NEVER_TOKEN = "never"; 59 private static final String AKI_BOOLEAN_TOKEN = "boolean"; 60 private static final String AKI_STRING_TOKEN = "string"; 61 private static final String AKI_UNIQUE_TOKEN = "unique"; 62 private static final String AKI_SYMBOL_TOKEN = "symbol"; 63 private static final String AKI_UNDEFINED_TOKEN = "undefined"; 64 private static final String AKI_OBJECT_TOKEN = "object"; 65 private static final String AKI_OF_TOKEN = "of"; 66 private static final String AKI_KEYOF_TOKEN = "keyof"; 67 private static final String AKI_TYPE_TOKEN = "type"; 68 private static final String AKI_CONSTRUCTOR_TOKEN = "constructor"; 69 private static final String AKI_NAMESPACE_TOKEN = "namespace"; 70 private static final String AKI_REQUIRE_TOKEN = "require"; 71 private static final String AKI_MODULE_TOKEN = "module"; 72 private static final String AKI_DECLARE_TOKEN = "declare"; 73 private static final String AKI_ABSTRACT_TOKEN = "abstract"; 74 private static final String AKI_DEBUGGER_TOKEN = "debugger"; 75 private static final String AKI_FUNCTION_TOKEN = "function"; 76 private static final String AKI_THIS_TOKEN = "this"; 77 private static final String AKI_WITH_TOKEN = "with"; 78 private static final String AKI_DEFAULT_TOKEN = "default"; 79 private static final String AKI_READONLY_TOKEN = "readonly"; 80 private static final String AKI_ASYNC_TOKEN = "async"; 81 private static final String AKI_AWAIT_TOKEN = "await"; 82 private static final String AKI_YIELD_TOKEN = "yield"; 83 private static final String AKI_ARROW_TOKEN = "=>"; 84 private static final String AKI_NEW_LINE = "\n"; 85 private static final String AKI_TAB_SPACE = "\t"; 86 private static final String AKI_BLANK_SPACE = " "; 87 private static final String AKI_SPLIT = " | "; 88 private static final String AKI_EQUAL = " = "; 89 private static final String AKI_COMMA = ","; 90 private static final String AKI_DOUBLE_QUOTATION = "\""; 91 private static final String AKI_UNDER_LINE = "_"; 92 private static final String AKI_SEMICOLON = ";"; 93 private static final String AKI_COLON = ":"; 94 private static final String AKI_ELLIPSIS = "..."; 95 private static final String AKI_DOT = "."; 96 private static final String AKI_LEFT_BRACE = "{"; 97 private static final String AKI_RIGHT_BRACE = "}"; 98 private static final String AKI_LEFT_PARENTHESES = "("; 99 private static final String AKI_RIGHT_PARENTHESES = ")"; 100 private static final String AKI_LEFT_SQUARE_BRACKET = "["; 101 private static final String AKI_RIGHT_SQUARE_BRACKET = "]"; 102 private static final String AKI_LEFT_ANGLE_BRACKET = "<"; 103 private static final String AKI_RIGHT_ANGLE_BRACKET = ">"; 104 105 private static final String AKI_STD_STRING = "std::string"; 106 private static final String AKI_STD_VECTOR = "std::vector"; 107 private static final String AKI_STD_LIST = "std::list"; 108 private static final String AKI_STD_ARRAY = "std::array"; 109 private static final String AKI_STD_STACK = "std::stack"; 110 private static final String AKI_STD_QUEUE = "std::queue"; 111 private static final String AKI_STD_PAIR = "std::pair"; 112 private static final String AKI_STD_MAP = "std::map"; 113 private static final String AKI_STD_SET = "std::set"; 114 private static final String AKI_STD_DEQUE = "std::deque"; 115 private static final String AKI_STD_MULTIMAP = "std::multimap"; 116 private static final String AKI_STD_MULTISET = "std::multiset"; 117 118 private static final String AKI_RET_EXPRESS = "AKI_RET_EXPRESS"; 119 private static final String AKI_ARGUMENT_EXPRESS = "AKI_ARGUMENT_EXPRESS"; 120 private static final String AKI_CALLBACK_DECLARE = "aki::SafetyCallback<AKI_RET_EXPRESS(AKI_ARGUMENT_EXPRESS)> "; 121 122 private static final String AKI_STR_SUFFIX = "STR"; 123 private static final String AKI_FILE_PREFIX = "ag_aki"; 124 private static final String AKI_FILE_H_SUFFIX = ".h"; 125 private static final String AKI_FILE_AKI_SUFFIX = ".cpp"; 126 private static final String AKI_FILE_C_SUFFIX = ".c"; 127 private static final String AKI_STRUCT_SUFFIX = "ST"; 128 129 private static final String AKI_ENUM_NAME = "AKI_ENUM_NAME"; 130 private static final String AKI_ENUM_ITEM_NANE = "AKI_ENUM_ITEM_NANE"; 131 private static final String AKI_ENUM_ITEM_DECLARE = "\n\tJSBIND_ENUM_VALUE(AKI_ENUM_ITEM_NANE);"; 132 private static final String AKI_ENUM_EXPRESSION = "JSBIND_ENUM_EXPRESSION"; 133 private static final String AKI_ENUM_DECLARE = "\nJSBIND_ENUM(AKI_ENUM_NAME) {" + 134 "JSBIND_ENUM_EXPRESSION\n" + 135 "};\n"; 136 137 private static final String AKI_CLASS_NAME = "AKI_CLASS_NAME"; 138 private static final String AKI_CONSTRUCTOR_EXPRESSION = "AKI_CONSTRUCTOR_EXPRESSION"; 139 private static final String AKI_CONSTRUCTOR_DECLARE = "\n\tJSBIND_CONSTRUCTOR<AKI_CONSTRUCTOR_PARAMS>();"; 140 private static final String AKI_CONSTRUCTOR_PARAMS = "AKI_CONSTRUCTOR_PARAMS"; 141 private static final String AKI_METHOD_DECLARE = "\n\tJSBIND_METHOD(AKI_METHOD_NAME, \"AKI_METHOD_NAME\");" + 142 "\n\tJSBIND_PMETHOD(AKI_METHOD_NAME, \"AKI_METHOD_NAMEPromise\");"; 143 private static final String AKI_METHOD_EXPRESSION = "AKI_METHOD_EXPRESSION"; 144 private static final String AKI_METHOD_NAME = "AKI_METHOD_NAME"; 145 private static final String AKI_PMETHOD_DECLARE = "\n\tJSBIND_PMETHOD(AKI_PMETHOD_NAME);"; 146 private static final String AKI_PMETHOD_EXPRESSION = "AKI_PMETHOD_EXPRESSION"; 147 private static final String AKI_PMETHOD_NAME = "AKI_PMETHOD_NAME"; 148 private static final String AKI_PROPERTY_DECLARE = "\n\tJSBIND_PROPERTY(AKI_PROPERTY_NAME);"; 149 private static final String AKI_PROPERTY_EXPRESSION = "AKI_PROPERTY_EXPRESSION"; 150 private static final String AKI_PROPERTY_NAME = "AKI_PROPERTY_NAME"; 151 private static final String AKI_CLASS_DECLARE = "\nJSBIND_CLASS(AKI_CLASS_NAME)\n" + 152 "{" + 153 "AKI_CONSTRUCTOR_EXPRESSION" + 154 "AKI_METHOD_EXPRESSION" + 155 "AKI_PMETHOD_EXPRESSION" + 156 "AKI_PROPERTY_EXPRESSION" + 157 "\n};\n"; 158 159 private static final String AKI_FUNCTION_ITEM_DECLARE = 160 "\n\tJSBIND_FUNCTION(AKI_FUNCTION_NAME, \"AKI_FUNCTION_NAME\");" + 161 "\n\tJSBIND_PFUNCTION(AKI_FUNCTION_NAME, \"AKI_FUNCTION_NAMEPromise\");"; 162 private static final String AKI_FUNCTION_EXPRESSION = "AKI_FUNCTION_EXPRESSION"; 163 private static final String AKI_FUNCTION_NAME = "AKI_FUNCTION_NAME"; 164 private static final String AKI_FUNCTION_DECLARE = "\nJSBIND_GLOBAL()\n" + 165 "{" + 166 "AKI_FUNCTION_EXPRESSION\n" + 167 "};\n"; 168 169 private static final String AKI_ADDON_NAME = "AKI_ADDON_NAME"; 170 private static final String AKI_ADDON_DECLARE = "\n#include <string>\n" + 171 "#include <aki/jsbind.h>\n" + 172 "\n" + 173 "JSBIND_ADDON(AKI_ADDON_NAME)\n"; 174 175 private String interfaceContent = ""; 176 private String enumContent = ""; 177 private String classContent = ""; 178 private String funcContent = ""; 179 private String structContent = ""; 180 private String typeContent = ""; 181 private String unionContent = ""; 182 private String constContent = ""; 183 184 private final Map<String, String> ts2cppMap = Map.ofEntries( 185 Map.entry("any", "auto"), 186 Map.entry("boolean", "bool"), 187 Map.entry("string", "std::string"), 188 Map.entry("number", "int"), 189 Map.entry("void", "void"), 190 Map.entry("[]", "*") 191 ); 192 193 private final Map<String, String> tsTokenMap = Map.ofEntries( 194 Map.entry("\"", ""), 195 Map.entry("*", ""), 196 Map.entry("&", ""), 197 Map.entry("(", ""), 198 Map.entry(")", "") 199 ); 200 201 /** 202 * 构造函数 203 */ GenAkiCppFile()204 GenAkiCppFile() { 205 206 } 207 208 /** 209 * 将 cpp key 转换成 ts key 210 * 211 * @param cppKey 枚举对象列表 212 * @return ts key 213 */ ts2CppKey(String cppKey)214 private String ts2CppKey(String cppKey) { 215 if (cppKey == null) { 216 return ""; 217 } 218 String retKey = cppKey; 219 for (Map.Entry<String, String> entry : ts2cppMap.entrySet()) { 220 String key = entry.getKey(); 221 String value = entry.getValue(); 222 int ret = cppKey.indexOf(key); 223 if (ret >= 0 && value.contains(AKI_STAR_TOKEN)) { 224 return cppKey.substring(0, ret) + value; 225 } else if (ret >= 0) { 226 return value; 227 } 228 } 229 return retKey; 230 } 231 232 /** 233 * 将cpp token 替换成对应的dts token 234 * 235 * @param cppKey 语言关键字 236 * @return 替换后字符串 237 */ replaceTsToken(String cppKey)238 private String replaceTsToken(String cppKey) { 239 String retKey = cppKey; 240 for (Map.Entry<String, String> entry : tsTokenMap.entrySet()) { 241 String key = entry.getKey(); 242 String value = entry.getValue(); 243 int ret = retKey.indexOf(key); 244 if (ret >= 0) { 245 retKey = retKey.replace(key, value); 246 } 247 } 248 return retKey; 249 } 250 251 /** 252 * 获得接口内容 253 * 254 * @return 接口内容 255 */ getInterfaceContent()256 public String getInterfaceContent() { 257 return interfaceContent; 258 } 259 260 /** 261 * 获得枚举内容 262 * 263 * @return 枚举内容 264 */ getEnumContent()265 public String getEnumContent() { 266 return enumContent; 267 } 268 269 /** 270 * 获得类内容 271 * 272 * @return 类内容 273 */ getClassContent()274 public String getClassContent() { 275 return classContent; 276 } 277 278 /** 279 * 获得方法内容 280 * 281 * @return 方法内容 282 */ getFuncContent()283 public String getFuncContent() { 284 return funcContent; 285 } 286 287 /** 288 * 获得结构体内容 289 * 290 * @return 结构体内容 291 */ getStructContent()292 public String getStructContent() { 293 return structContent; 294 } 295 296 /** 297 * 获得type内容 298 * 299 * @return type内容 300 */ getTypeContent()301 public String getTypeContent() { 302 return typeContent; 303 } 304 305 /** 306 * 获得联合体内容 307 * 308 * @return 联合体内容 309 */ getUnionContent()310 public String getUnionContent() { 311 return unionContent; 312 } 313 314 /** 315 * 获得常量内容 316 * 317 * @return 常量内容 318 */ getConstContent()319 public String getConstContent() { 320 return constContent; 321 } 322 323 /** 324 * 生成输出内容 325 * 326 * @param po 解析类 327 */ 328 @Override genContent(ParseObj po)329 public void genContent(ParseObj po) { 330 genInterfaceList(po.getInterfaceList()); 331 genEnumList(po.getEnumList()); 332 genClassList(po.getClassList()); 333 genFuncList(po.getFuncList()); 334 genStructList(po.getStructList()); 335 genTypeList(po.getTypeList()); 336 genUnionList(po.getUnionList()); 337 genVarList(po.getVarList()); 338 } 339 340 /** 341 * 生成文件 342 * 343 * @param fileName 文件名 344 * @param filePath 文件路径 345 */ 346 @Override genFile(String filePath, String fileName)347 public void genFile(String filePath, String fileName) { 348 System.out.println("genFile : " + filePath + fileName); 349 String outFileName = filePath + File.separator + AKI_FILE_PREFIX + 350 fileName.replace(".", "_") + AKI_FILE_AKI_SUFFIX; 351 this.genFileName = outFileName; 352 System.out.println("outFileName : " + outFileName); 353 354 if (this.genMode.equals(GeneratorBase.GEN_REPLACE)) { 355 FileUtils.deleteFile(outFileName); 356 } 357 FileUtils.createFile(outFileName); 358 359 FileUtils.appendText(outFileName, this.genFileHeader(filePath + File.separator + fileName)); 360 FileUtils.appendText(outFileName, 361 AKI_ADDON_DECLARE.replace(AKI_ADDON_NAME, fileName.replace(".", ""))); 362 FileUtils.appendText(outFileName, this.constContent); 363 FileUtils.appendText(outFileName, this.enumContent); 364 FileUtils.appendText(outFileName, this.typeContent); 365 FileUtils.appendText(outFileName, this.interfaceContent); 366 FileUtils.appendText(outFileName, this.unionContent); 367 FileUtils.appendText(outFileName, this.funcContent); 368 FileUtils.appendText(outFileName, this.structContent); 369 FileUtils.appendText(outFileName, this.classContent); 370 371 } 372 373 /** 374 * 生成输出内容 375 * 376 * @param iol 接口列表 377 */ 378 @Override genInterfaceList(List<InterfaceObject> iol)379 public void genInterfaceList(List<InterfaceObject> iol) { 380 System.out.println("genInterfaceList" + iol.toString()); 381 }; 382 genCppEnumContent(EnumObj eo)383 private String genCppEnumContent(EnumObj eo) { 384 String resContent = ""; 385 String enumName = eo.getName(); 386 enumName = enumName != null && !enumName.isEmpty() ? enumName : eo.getAlias(); 387 List<String> memList = eo.getMemberList(); 388 List<String> vaList = eo.getValueList(); 389 int i = 0; 390 resContent += AKI_NEW_LINE + AKI_ENUM_TOKEN + 391 AKI_BLANK_SPACE + enumName + AKI_BLANK_SPACE + AKI_LEFT_BRACE; 392 for (String memItem : memList) { 393 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + memItem; 394 if (vaList.size() > i && !vaList.get(i).isEmpty()) { 395 resContent += AKI_EQUAL + replaceTsToken(vaList.get(i)) + AKI_COMMA; 396 } else { 397 resContent += AKI_COMMA; 398 } 399 i++; 400 } 401 402 resContent = StringUtils.removeLastSpace(resContent); 403 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 404 405 i = 0; 406 if (vaList.size() > i && !vaList.get(i).isEmpty() && 407 vaList.get(i).contains("\"")) { 408 resContent += AKI_NEW_LINE + AKI_STD_STRING_TOKEN + AKI_BLANK_SPACE + 409 enumName.toLowerCase(Locale.ROOT) + AKI_UNDER_LINE + AKI_STR_SUFFIX + 410 AKI_LEFT_SQUARE_BRACKET + AKI_RIGHT_SQUARE_BRACKET + AKI_EQUAL + AKI_LEFT_BRACE; 411 for (String val : vaList) { 412 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + AKI_LEFT_SQUARE_BRACKET + 413 memList.get(i) + AKI_RIGHT_SQUARE_BRACKET + AKI_EQUAL + val + AKI_COMMA; 414 i++; 415 } 416 resContent = StringUtils.removeLastCharacter(resContent, 1); 417 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 418 419 } 420 return resContent; 421 } 422 genAkiEnumContent(EnumObj eo)423 private String genAkiEnumContent(EnumObj eo) { 424 String resContent = ""; 425 String enumName = eo.getName(); 426 enumName = !enumName.isEmpty() ? enumName : eo.getAlias(); 427 List<String> memList = eo.getMemberList(); 428 429 String enumDeclare = AKI_ENUM_DECLARE.replace(AKI_ENUM_NAME, enumName); 430 String enumItemDeclare = ""; 431 for (String memItem : memList) { 432 enumItemDeclare += AKI_ENUM_ITEM_DECLARE.replace(AKI_ENUM_ITEM_NANE, memItem); 433 } 434 435 resContent = enumDeclare.replace(AKI_ENUM_EXPRESSION, enumItemDeclare); 436 return resContent; 437 } 438 439 /** 440 * 生成输出内容 441 * 442 * @param eol 枚举列表 443 */ 444 @Override genEnumList(List<EnumObj> eol)445 public void genEnumList(List<EnumObj> eol) { 446 System.out.println("genEnumList" + eol.toString()); 447 448 String resContent = ""; 449 for (EnumObj eo : eol) { 450 resContent += genCppEnumContent(eo); 451 resContent += genAkiEnumContent(eo); 452 } 453 454 this.enumContent = resContent; 455 }; 456 setClassFunc(List<FuncObj> funcList, String content)457 private String setClassFunc(List<FuncObj> funcList, String content) { 458 String tempResContent = content; 459 for (FuncObj funcItem : funcList) { 460 String retValue = funcItem.getRetValue(); 461 retValue = retValue.isEmpty() ? "" : ts2CppKey(retValue) + AKI_BLANK_SPACE; 462 tempResContent += AKI_NEW_LINE + AKI_TAB_SPACE + retValue + 463 replaceTsToken(funcItem.getName()) + AKI_LEFT_PARENTHESES; 464 List<ParamObj> pol = funcItem.getParamList(); 465 for (ParamObj poItem : pol) { 466 String retType = ts2CppKey(poItem.getType()).isEmpty() ? 467 AKI_AUTO_TOKEN : ts2CppKey(poItem.getType()); 468 tempResContent += (poItem.getName() == null) ? retType + AKI_COMMA + AKI_BLANK_SPACE : 469 retType + AKI_BLANK_SPACE + replaceTsToken(poItem.getName()) + AKI_COMMA + AKI_BLANK_SPACE; 470 } 471 if (!pol.isEmpty()) { 472 tempResContent = StringUtils.removeLastCharacter(tempResContent, 2); 473 } 474 tempResContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON; 475 } 476 return tempResContent; 477 } 478 genCppClassContent(ClassObj co)479 private String genCppClassContent(ClassObj co) { 480 String className = co.getName(); 481 className = !className.isEmpty() ? className : co.getAlias(); 482 483 String templateStr = !co.getTempList().isEmpty() ? 484 AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; 485 for (String teStr : co.getTempList()) { 486 templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; 487 } 488 templateStr = templateStr.length() > 1 ? 489 StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; 490 491 List<String> hnList = co.getHeritageNameList(); 492 String htStr = hnList.size() > 0 ? AKI_BLANK_SPACE + AKI_COLON + AKI_BLANK_SPACE : ""; 493 for (String hName : hnList) { 494 htStr += AKI_PUBLIC_TOKEN + AKI_BLANK_SPACE + hName + AKI_COMMA + AKI_BLANK_SPACE; 495 } 496 htStr = htStr.length() > 1 ? StringUtils.removeLastCharacter(htStr, 2) : htStr; 497 498 List<String> htempList = co.getHeritageTemplateList(); 499 String htempStr = htempList.size() > 0 ? AKI_LEFT_ANGLE_BRACKET : ""; 500 for (String tempStr : htempList) { 501 htempStr += tempStr + AKI_COMMA + AKI_BLANK_SPACE; 502 } 503 htempStr = htempList.size() > 0 ? 504 StringUtils.removeLastCharacter(htempStr, 2) + AKI_RIGHT_ANGLE_BRACKET : ""; 505 String resContent = ""; 506 resContent += AKI_NEW_LINE + templateStr + AKI_CLASS_TOKEN + 507 AKI_BLANK_SPACE + className + htStr + htempStr + AKI_BLANK_SPACE + AKI_LEFT_BRACE; 508 List<ParamObj> paList = co.getParamList(); 509 for (ParamObj paItem : paList) { 510 String paType = paItem.getType(); 511 String qualifyStr = paItem.getQualifier() == null || paItem.getQualifier().isEmpty() ? 512 "" : paItem.getQualifier() + AKI_BLANK_SPACE; 513 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + qualifyStr + ts2CppKey(paType) + 514 AKI_BLANK_SPACE + replaceTsToken(paItem.getName()); 515 List<String> initVList = paItem.getvList(); 516 if (!initVList.isEmpty()) { 517 resContent += AKI_EQUAL + initVList.get(0) + AKI_SEMICOLON; 518 } else { 519 resContent += AKI_SEMICOLON; 520 } 521 } 522 523 resContent = setClassFunc(co.getFuncList(), resContent); 524 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 525 return resContent; 526 } 527 genAkiCppClassContent(ClassObj co)528 private String genAkiCppClassContent(ClassObj co) { 529 String className = co.getName(); 530 className = !className.isEmpty() ? className : co.getAlias(); 531 532 String classDeclare = AKI_CLASS_DECLARE.replace(AKI_CLASS_NAME, className); 533 String classPropertyDeclare = ""; 534 535 List<ParamObj> paList = co.getParamList(); 536 for (ParamObj paItem : paList) { 537 classPropertyDeclare += AKI_PROPERTY_DECLARE.replace(AKI_PROPERTY_NAME, paItem.getName()); 538 } 539 classDeclare = classDeclare.replace(AKI_PROPERTY_EXPRESSION, classPropertyDeclare); 540 541 List<FuncObj> foList = co.getFuncList(); 542 String classFunctionDeclare = ""; 543 String classConstructDeclare = ""; 544 for (FuncObj foItem : foList) { 545 if (foItem.getName().equals("constructor")) { 546 String paramStr = ""; 547 for (ParamObj poItem : foItem.getParamList()) { 548 paramStr += poItem.getType() + AKI_COMMA + AKI_BLANK_SPACE; 549 } 550 paramStr = StringUtils.removeLastCharacter(paramStr, 2); 551 classConstructDeclare += AKI_CONSTRUCTOR_DECLARE.replace( 552 AKI_CONSTRUCTOR_PARAMS, paramStr); 553 } else { 554 classFunctionDeclare += AKI_METHOD_DECLARE.replace(AKI_METHOD_NAME, foItem.getName()); 555 } 556 } 557 558 String classPFunctionDeclare = ""; 559 classDeclare = classDeclare.replace(AKI_METHOD_EXPRESSION, classFunctionDeclare); 560 classDeclare = classDeclare.replace(AKI_CONSTRUCTOR_EXPRESSION, classConstructDeclare); 561 classDeclare = classDeclare.replace(AKI_PMETHOD_EXPRESSION, classPFunctionDeclare); 562 563 return classDeclare; 564 } 565 566 /** 567 * 生成输出内容 568 * 569 * @param col 类列表 570 */ 571 @Override genClassList(List<ClassObj> col)572 public void genClassList(List<ClassObj> col) { 573 System.out.println("genClassList" + col.toString()); 574 575 String resContent = ""; 576 for (ClassObj co : col) { 577 resContent += genCppClassContent(co); 578 resContent += genAkiCppClassContent(co); 579 } 580 this.classContent = resContent; 581 }; 582 genCppFuncContent(FuncObj fo)583 private String genCppFuncContent(FuncObj fo) { 584 String funcName = fo.getName(); 585 funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); 586 List<String> tempList = fo.getTempList(); 587 String tempStr = tempList.isEmpty() ? "" : AKI_TEMPLATE_TOKEN + AKI_LEFT_ANGLE_BRACKET; 588 for (String teStr : tempList) { 589 tempStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; 590 } 591 tempStr = tempList.isEmpty() ? "" : 592 StringUtils.removeLastCharacter(tempStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE; 593 List<ParamObj> paList = fo.getParamList(); 594 String retValue = ts2CppKey(fo.getRetValue()).isEmpty() ? 595 "" : ts2CppKey(fo.getRetValue()) + AKI_BLANK_SPACE; 596 String resContent = ""; 597 resContent += AKI_NEW_LINE + tempStr + retValue + 598 replaceTsToken(funcName) + AKI_LEFT_PARENTHESES; 599 600 for (ParamObj poItem : paList) { 601 String rawType = poItem.getType(); 602 String paType = ""; 603 System.out.println("rawType: " + rawType); 604 if (rawType != null && rawType.contains(AKI_ARROW_TOKEN)) { 605 FuncObj cbFoItem = poItem.getFoList().get(0); 606 String cbFoRetVal = cbFoItem.getRetValue(); 607 List<ParamObj> pal = cbFoItem.getParamList(); 608 String cbFoParams = ""; 609 for (ParamObj cbFunPa : pal) { 610 cbFoParams += ts2CppKey(cbFunPa.getType()).isEmpty() ? AKI_AUTO_TOKEN : ts2CppKey(rawType); 611 cbFoParams += AKI_COMMA + AKI_BLANK_SPACE; 612 } 613 cbFoParams = StringUtils.removeLastCharacter(cbFoParams, 2); 614 paType = AKI_CALLBACK_DECLARE.replace(AKI_RET_EXPRESS, cbFoRetVal); 615 paType = paType.replace(AKI_ARGUMENT_EXPRESS, cbFoParams); 616 } else { 617 paType = ts2CppKey(rawType).isEmpty() ? 618 AKI_AUTO_TOKEN + AKI_BLANK_SPACE : ts2CppKey(rawType) + AKI_BLANK_SPACE; 619 } 620 621 String paName = poItem.getName(); 622 String defaultVal = poItem.getStrValue(0); 623 defaultVal = defaultVal.isEmpty() ? "" : AKI_EQUAL + defaultVal; 624 resContent += !paName.isEmpty() ? paType + replaceTsToken(paName) + 625 defaultVal + AKI_COMMA + AKI_BLANK_SPACE : 626 paType + AKI_COMMA + AKI_BLANK_SPACE; 627 } 628 if (!paList.isEmpty()) { 629 resContent = StringUtils.removeLastCharacter(resContent, 2); 630 } 631 resContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON + AKI_NEW_LINE; 632 return resContent; 633 } 634 genAkiCppFuncContent(List<FuncObj> fol)635 private String genAkiCppFuncContent(List<FuncObj> fol) { 636 String resContent = ""; 637 638 String funcItemDeclare = ""; 639 for (FuncObj fo : fol) { 640 String funcName = fo.getName(); 641 funcName = !funcName.isEmpty() ? funcName : fo.getAlias(); 642 funcItemDeclare += AKI_FUNCTION_ITEM_DECLARE.replace(AKI_FUNCTION_NAME, funcName); 643 } 644 String funcDeclare = AKI_FUNCTION_DECLARE.replace(AKI_FUNCTION_EXPRESSION, funcItemDeclare); 645 resContent += funcDeclare; 646 return resContent; 647 } 648 649 /** 650 * 生成输出内容 651 * 652 * @param fol 方法列表 653 */ 654 @Override genFuncList(List<FuncObj> fol)655 public void genFuncList(List<FuncObj> fol) { 656 System.out.println("genFuncList : " + fol.toString()); 657 String resContent = ""; 658 for (FuncObj fo : fol) { 659 resContent += genCppFuncContent(fo); 660 } 661 resContent += genAkiCppFuncContent(fol); 662 this.funcContent = resContent; 663 System.out.println("genFuncList : " + resContent); 664 }; 665 genCppStructContent(StructObj so)666 private String genCppStructContent(StructObj so) { 667 String structName = so.getName(); 668 structName = !structName.isEmpty() ? structName : so.getAlias(); 669 670 String templateStr = !so.getTemplateList().isEmpty() ? 671 AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; 672 for (String teStr : so.getTemplateList()) { 673 templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; 674 } 675 templateStr = templateStr.length() > 1 ? 676 StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; 677 678 List<ParamObj> paList = so.getMemberList(); 679 String resContent = ""; 680 resContent += AKI_NEW_LINE + templateStr + AKI_STRUCT_TOKEN + 681 AKI_BLANK_SPACE + structName + AKI_BLANK_SPACE + AKI_LEFT_BRACE; 682 683 for (ParamObj paItem : paList) { 684 String paType = paItem.getType().isEmpty() ? AKI_AUTO_TOKEN : paItem.getType(); 685 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + ts2CppKey(paType) + 686 AKI_BLANK_SPACE + paItem.getName(); 687 ; 688 List<String> initVList = paItem.getvList(); 689 if (initVList.size() > 0) { 690 resContent += AKI_EQUAL + initVList.get(0) + AKI_SEMICOLON; 691 } else { 692 resContent += AKI_SEMICOLON; 693 } 694 } 695 696 List<FuncObj> funcList = so.getFuncList(); 697 for (FuncObj funcItem : funcList) { 698 String retValue = ts2CppKey(funcItem.getRetValue()).isEmpty() ? "" : 699 ts2CppKey(funcItem.getRetValue()) + AKI_BLANK_SPACE; 700 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + retValue + 701 replaceTsToken(funcItem.getName()) + AKI_LEFT_PARENTHESES; 702 List<ParamObj> pol = funcItem.getParamList(); 703 for (ParamObj poItem : pol) { 704 String retType = ts2CppKey(poItem.getType()).isEmpty() ? 705 AKI_AUTO_TOKEN : ts2CppKey(poItem.getType()); 706 resContent += retType + AKI_BLANK_SPACE + replaceTsToken(poItem.getName()) + 707 AKI_COMMA + AKI_BLANK_SPACE; 708 } 709 resContent = !pol.isEmpty() ? StringUtils.removeLastCharacter(resContent, 2) : resContent; 710 resContent += AKI_RIGHT_PARENTHESES + AKI_SEMICOLON; 711 } 712 713 resContent = StringUtils.removeLastSpace(resContent); 714 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 715 return resContent; 716 } 717 genAkiCppStructContent(StructObj so)718 private String genAkiCppStructContent(StructObj so) { 719 String structName = so.getName(); 720 structName = !structName.isEmpty() ? structName : so.getAlias(); 721 722 String structDeclare = AKI_CLASS_DECLARE.replace(AKI_CLASS_NAME, structName); 723 String classPropertyDeclare = ""; 724 725 List<ParamObj> paList = so.getMemberList(); 726 for (ParamObj paItem : paList) { 727 classPropertyDeclare += AKI_PROPERTY_DECLARE.replace(AKI_PROPERTY_NAME, paItem.getName()); 728 } 729 structDeclare = structDeclare.replace(AKI_PROPERTY_EXPRESSION, classPropertyDeclare); 730 731 List<FuncObj> foList = so.getFuncList(); 732 String classFunctionDeclare = ""; 733 String classConstructDeclare = ""; 734 for (FuncObj foItem : foList) { 735 if (foItem.getName().equals("constructor")) { 736 String paramStr = ""; 737 for (ParamObj poItem : foItem.getParamList()) { 738 paramStr += poItem.getType() + AKI_COMMA + AKI_BLANK_SPACE; 739 } 740 paramStr = StringUtils.removeLastCharacter(paramStr, 2); 741 classConstructDeclare += AKI_CONSTRUCTOR_DECLARE.replace( 742 AKI_CONSTRUCTOR_PARAMS, paramStr); 743 } else { 744 classFunctionDeclare += AKI_METHOD_DECLARE.replace(AKI_METHOD_NAME, foItem.getName()); 745 } 746 } 747 748 String classPFunctionDeclare = ""; 749 structDeclare = structDeclare.replace(AKI_METHOD_EXPRESSION, classFunctionDeclare); 750 structDeclare = structDeclare.replace(AKI_CONSTRUCTOR_EXPRESSION, classConstructDeclare); 751 structDeclare = structDeclare.replace(AKI_PMETHOD_EXPRESSION, classPFunctionDeclare); 752 753 return structDeclare; 754 } 755 756 /** 757 * 生成输出内容 758 * 759 * @param sol 结构体列表 760 */ 761 @Override genStructList(List<StructObj> sol)762 public void genStructList(List<StructObj> sol) { 763 System.out.println("genStructList" + sol.toString()); 764 765 String resContent = ""; 766 for (StructObj so : sol) { 767 resContent += genCppStructContent(so); 768 resContent += genAkiCppStructContent(so); 769 } 770 this.structContent = resContent; 771 }; 772 773 /** 774 * 生成输出内容 775 * 776 * @param tol 类型列表 777 */ 778 @Override genTypeList(List<TypeObj> tol)779 public void genTypeList(List<TypeObj> tol) { 780 System.out.println("genTypeList : " + tol.toString()); 781 }; 782 783 /** 784 * 生成输出内容 785 * 786 * @param uol 联合体列表 787 */ 788 @Override genUnionList(List<UnionObj> uol)789 public void genUnionList(List<UnionObj> uol) { 790 System.out.println("genUnionList : " + uol.toString()); 791 792 String resContent = ""; 793 for (UnionObj uo : uol) { 794 String unionName = uo.getName(); 795 unionName = !unionName.isEmpty() ? unionName : uo.getAlias(); 796 797 String templateStr = !uo.getTemplateList().isEmpty() ? 798 AKI_TEMPLATE_TOKEN + AKI_BLANK_SPACE + AKI_LEFT_ANGLE_BRACKET : ""; 799 for (String teStr : uo.getTemplateList()) { 800 templateStr += AKI_TYPE_NAME_TOKEN + AKI_BLANK_SPACE + teStr + AKI_COMMA + AKI_BLANK_SPACE; 801 } 802 templateStr = templateStr.length() > 1 ? 803 StringUtils.removeLastCharacter(templateStr, 2) + AKI_RIGHT_ANGLE_BRACKET + AKI_BLANK_SPACE : ""; 804 805 resContent += AKI_NEW_LINE + templateStr + AKI_UNION_TOKEN + 806 AKI_BLANK_SPACE + unionName + AKI_LEFT_BRACE; 807 808 List<ParamObj> paList = uo.getMemList(); 809 for (ParamObj paItem : paList) { 810 String paType = paItem.getType(); 811 String paName = paItem.getName(); 812 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + ts2CppKey(paType) 813 + AKI_BLANK_SPACE + paName + AKI_SEMICOLON; 814 } 815 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE; 816 resContent += AKI_SEMICOLON + AKI_NEW_LINE; 817 } 818 this.unionContent = resContent; 819 }; 820 genVarArrayList(String tmpContent, String paName, List<ParamObj> paList)821 private String genVarArrayList(String tmpContent, String paName, List<ParamObj> paList) { 822 String resContent = tmpContent; 823 resContent += AKI_NEW_LINE + AKI_STRUCT_TOKEN + AKI_BLANK_SPACE + paName + 824 AKI_STRUCT_SUFFIX + AKI_BLANK_SPACE + AKI_LEFT_BRACE; 825 List<ParamObj> paramList = paList.get(0).getPaList(); 826 for (ParamObj paItem : paramList) { 827 String paStr = paItem.getName(); 828 String paVal = paItem.getStrValue(0); 829 String typeStr = StringUtils.isAllDigits(paVal) ? 830 AKI_NUMBER_TOKEN : AKI_STD_STRING; 831 typeStr = StringUtils.isBoolean(paVal) ? AKI_BOOLEAN_TOKEN : typeStr; 832 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + typeStr + AKI_BLANK_SPACE + paStr + AKI_SEMICOLON; 833 } 834 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 835 836 resContent += AKI_NEW_LINE + AKI_CONST_TOKEN + AKI_BLANK_SPACE + AKI_STD_VECTOR + 837 AKI_LEFT_ANGLE_BRACKET + paName + AKI_STRUCT_SUFFIX + AKI_RIGHT_ANGLE_BRACKET + 838 AKI_BLANK_SPACE + paName + AKI_EQUAL + AKI_LEFT_BRACE; 839 for (ParamObj paramListItem : paList) { 840 List<ParamObj> subParamList = paramListItem.getPaList(); 841 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + AKI_LEFT_BRACE; 842 for (ParamObj paItem : subParamList) { 843 String paVal = paItem.getStrValue(0); 844 resContent += paVal + AKI_COMMA + AKI_BLANK_SPACE; 845 } 846 resContent = StringUtils.removeLastCharacter(resContent, 2); 847 resContent += AKI_RIGHT_BRACE + AKI_COMMA; 848 } 849 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 850 return resContent; 851 }; 852 853 /** 854 * 生成输出内容 855 * 856 * @param pol 常量列表 857 */ 858 @Override genVarList(List<ParamObj> pol)859 public void genVarList(List<ParamObj> pol) { 860 System.out.println("genVarList : " + pol.toString()); 861 862 String resContent = ""; 863 for (ParamObj po : pol) { 864 String paName = po.getName(); 865 String paType = ts2CppKey(po.getType()).isEmpty() ? AKI_AUTO_TOKEN : ts2CppKey(po.getType()); 866 String paValue = po.getStrValue(0); 867 List<ParamObj> paList = po.getPaList(); 868 if (paList.isEmpty()) { 869 resContent += AKI_NEW_LINE + AKI_EXTENDS_TOKEN + AKI_BLANK_SPACE + AKI_CONST_TOKEN + 870 AKI_BLANK_SPACE + paType + AKI_BLANK_SPACE + paName + AKI_EQUAL + paValue; 871 872 resContent += AKI_SEMICOLON + AKI_NEW_LINE; 873 } else if (paList.get(0).getPaList().isEmpty()) { 874 String valType = StringUtils.isAllDigits(paList.get(0).getStrValue(0)) ? 875 AKI_NUMBER_TOKEN : AKI_STD_STRING; 876 resContent += AKI_NEW_LINE + AKI_EXTENDS_TOKEN + AKI_BLANK_SPACE + AKI_CONST_TOKEN + 877 AKI_BLANK_SPACE + AKI_STD_MAP + AKI_LEFT_ANGLE_BRACKET + AKI_STD_STRING + 878 AKI_COMMA + AKI_BLANK_SPACE + valType + AKI_RIGHT_BRACE + AKI_BLANK_SPACE + 879 paName + AKI_EQUAL + AKI_LEFT_BRACE; 880 for (ParamObj paItem : paList) { 881 String pName = paItem.getName(); 882 String pVal = paItem.getStrValue(0); 883 resContent += AKI_NEW_LINE + AKI_TAB_SPACE + AKI_LEFT_BRACE + AKI_DOUBLE_QUOTATION + 884 pName + AKI_DOUBLE_QUOTATION + AKI_COMMA + AKI_BLANK_SPACE + pVal + 885 AKI_RIGHT_BRACE + AKI_COMMA; 886 } 887 resContent = StringUtils.removeLastCharacter(resContent, 1); 888 resContent += AKI_NEW_LINE + AKI_RIGHT_BRACE + AKI_SEMICOLON + AKI_NEW_LINE; 889 } else if (!(paList.get(0).getPaList().isEmpty())) { 890 resContent = genVarArrayList(resContent, paName, paList); 891 } 892 893 } 894 this.constContent = resContent; 895 System.out.println("genVarList : " + resContent); 896 } 897 } 898