1 module IDL.IdlParseInterface; 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 core.internal.hash; 10 11 import IDL.IdlBaseInterface; 12 import IDL.IdlUnit; 13 import IDL.IdlInerfaceCreateCode; 14 import IDL.IdlParseStruct; 15 16 class FunctionArg 17 { 18 this(string type, string var) 19 { 20 typeName = type; 21 varName = var; 22 23 if(idlStructList.get(typeName, null) is null) 24 { 25 throw new Exception("parse type error, is not exist type:" ~ typeName); 26 } 27 28 writefln("function argument: %s:%s", varName, typeName); 29 } 30 31 string getVarName() 32 { 33 return varName; 34 } 35 36 string getTypeName() 37 { 38 return typeName; 39 } 40 41 public: 42 string typeName; 43 string varName; 44 } 45 46 47 class FunctionAttr 48 { 49 this(string fileRoutePath,string funcTlp) 50 { 51 auto formatFuncTlp = replaceAll(funcTlp, regex(`\(|\)`), " "); 52 53 formatFuncTlp = replaceAll(formatFuncTlp, regex(`\,`),""); 54 formatFuncTlp = replaceAll(formatFuncTlp, regex(`^\s*`), ""); 55 formatFuncTlp = replaceAll(formatFuncTlp, regex(`\s*$`), ""); 56 57 auto funcTlpList = split(formatFuncTlp, " "); 58 59 if(funcTlpList.length < 4 && funcTlpList.length % 2 == 0) 60 { 61 throw new Exception("parse function arguments is failed, " ~ funcTlp); 62 } 63 64 writeln("********************************"); 65 66 retValue = new FunctionArg(funcTlpList[0], "ret_" ~ funcTlpList[0]); 67 68 funcName = funcTlpList[1]; 69 funcHash = bytesHash((fileRoutePath~this.funcName).ptr, (fileRoutePath~this.funcName).length, 0); 70 71 int funcArgIndex = 0; 72 writefln("function name:%s, return value:%s", funcName, retValue.getTypeName); 73 74 for(int i = 2; i<funcTlpList.length; i+=2) 75 { 76 funcArgMap = new FunctionArg(funcTlpList[i], funcTlpList[i+1]); 77 break; 78 } 79 80 writeln("********************************\n"); 81 } 82 83 string getFuncName() 84 { 85 return this.funcName; 86 } 87 88 89 public: 90 FunctionArg retValue; 91 string funcName; 92 size_t funcHash; 93 FunctionArg funcArgMap; 94 } 95 96 class IdlParseInterface : IdlBaseInterface 97 { 98 bool parse(string filePath, string name, string structBodys) 99 { 100 this.interfaceName = name; 101 this.filePath = filePath; 102 103 auto MemberAttrList = split(structBodys, ";"); 104 105 if(MemberAttrList.length < 1) 106 { 107 throw new Exception("parse service member attr is failed, " ~ structBodys); 108 } 109 110 writeln("----------------------------"); 111 writefln("serivce name:%s", name); 112 113 foreach(attr; MemberAttrList) 114 { 115 if(attr.length > 1) 116 { 117 auto funcAttr = new FunctionAttr(this.filePath ~ this.interfaceName, attr); 118 functionList[funcIndex++] = funcAttr; 119 } 120 } 121 122 writeln("----------------------------\n\n"); 123 return true; 124 } 125 126 string getName() 127 { 128 return this.interfaceName; 129 } 130 131 132 string createServerCodeForInterface(CODE_LANGUAGE language) 133 { 134 string codeText; 135 136 switch(language) 137 { 138 case CODE_LANGUAGE.CL_CPP:break; 139 case CODE_LANGUAGE.CL_DLANG: codeText = idl_inerface_dlang_code.createServerCodeForInterface(this); break; 140 case CODE_LANGUAGE.CL_GOLANG:break; 141 case CODE_LANGUAGE.CL_JAVA:break; 142 143 default: 144 throw new Exception("language is not exits!!"); 145 } 146 147 return codeText; 148 } 149 150 string createServerCodeForService(CODE_LANGUAGE language) 151 { 152 string codeText; 153 154 switch(language) 155 { 156 case CODE_LANGUAGE.CL_CPP:break; 157 case CODE_LANGUAGE.CL_DLANG: codeText = idl_inerface_dlang_code.createServerCodeForService(this); break; 158 case CODE_LANGUAGE.CL_GOLANG:break; 159 case CODE_LANGUAGE.CL_JAVA:break; 160 161 default: 162 throw new Exception("language is not exits!!"); 163 } 164 165 return codeText; 166 } 167 168 169 string createClientCodeForInterface(CODE_LANGUAGE language) 170 { 171 string codeText; 172 173 switch(language) 174 { 175 case CODE_LANGUAGE.CL_CPP:break; 176 case CODE_LANGUAGE.CL_DLANG:codeText = idl_inerface_dlang_code.createClientCodeForInterface(this); break; 177 case CODE_LANGUAGE.CL_GOLANG:break; 178 case CODE_LANGUAGE.CL_JAVA:break; 179 180 default: 181 throw new Exception("language is not exits!!"); 182 } 183 184 return codeText; 185 } 186 187 string createClientCodeForService(CODE_LANGUAGE language) 188 { 189 string codeText; 190 191 switch(language) 192 { 193 case CODE_LANGUAGE.CL_CPP:break; 194 case CODE_LANGUAGE.CL_DLANG:codeText = idl_inerface_dlang_code.createClientCodeForService(this); break; 195 case CODE_LANGUAGE.CL_GOLANG:break; 196 case CODE_LANGUAGE.CL_JAVA:break; 197 198 default: 199 throw new Exception("language is not exits!!"); 200 } 201 202 return codeText; 203 } 204 205 206 public: 207 int funcIndex; 208 string interfaceName; 209 string filePath; 210 FunctionAttr[int] functionList; 211 }