parser.cc 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <ctype.h>
  14. #include "parser.h"
  15. #include "thrift/rpc_thrift_enum.h"
  16. #define LINE_LENGTH_MAX 2048
  17. static std::string gen_param_var(const std::string& type_name, size_t& cur,
  18. idl_info& info);
  19. std::string type_prefix_to_namespace(const std::string& type_name,
  20. idl_info& info);
  21. Descriptor *search_cur_file_descriptor(idl_info& info,
  22. const std::string& block_type,
  23. const std::string& block_name);
  24. Descriptor *search_include_file_descriptor(idl_info& info,
  25. const std::string include_file_name,
  26. const std::string& block_type,
  27. const std::string& block_name);
  28. idl_info *search_include_file(idl_info& info, const std::string file_name);
  29. idl_info *search_namespace(idl_info& info, const std::string name_space);
  30. void parse_thrift_type_name(const std::string& type_name,
  31. std::string& type_prefix,
  32. std::string& real_type_name);
  33. std::vector<std::string> parse_thrift_variable(const std::string& str, char sep);
  34. bool Parser::parse(const std::string& proto_file, idl_info& info)
  35. {
  36. std::string idl_file = info.input_dir + proto_file;
  37. auto pos = idl_file.find_last_of('/');
  38. if (pos == std::string::npos)
  39. info.file_name = idl_file;
  40. else
  41. info.file_name = idl_file.substr(pos + 1);
  42. pos = info.file_name.find_last_of('.');
  43. if (pos == std::string::npos)
  44. info.file_name_prefix = info.file_name + ".";
  45. else
  46. info.file_name_prefix = info.file_name.substr(0, pos + 1);
  47. info.absolute_file_path = idl_file;
  48. FILE *in = fopen(idl_file.c_str(), "r");
  49. if (!in)
  50. {
  51. fprintf(stderr, "[Parser] proto file: [%s] not exists.\n",
  52. idl_file.c_str());
  53. return false;
  54. }
  55. fprintf(stdout, "proto file: [%s]\n", idl_file.c_str());
  56. char line_buffer[LINE_LENGTH_MAX];
  57. std::string file_path;
  58. std::string block;
  59. int state = PARSER_ST_OUTSIDE_BLOCK;
  60. bool flag_append;
  61. int stack_count = 0;
  62. std::string block_name;
  63. std::string block_type;
  64. std::string extends_type;
  65. std::string old_type_name;
  66. std::string new_type_name;
  67. std::string thrift_type_prefix;
  68. std::string thrift_type_name;
  69. bool succ;
  70. while (fgets(line_buffer, LINE_LENGTH_MAX, in))
  71. {
  72. std::string line = SGenUtil::lstrip(line_buffer);
  73. if (line.empty())
  74. continue;
  75. this->check_single_comments(line);
  76. if (line.empty())
  77. continue;
  78. this->check_single_comments(line);
  79. if ((state & PARSER_ST_COMMENT_MASK) == PARSER_ST_INSIDE_COMMENT)
  80. {
  81. if (this->check_multi_comments_end(line))
  82. {
  83. state -= PARSER_ST_INSIDE_COMMENT;
  84. // state |= PARSER_ST_OUTSIDE_COMMENT_MASK;
  85. if (line.empty())
  86. continue;
  87. }
  88. else
  89. continue;
  90. }
  91. else if (this->check_multi_comments_begin(line))
  92. {
  93. state = (state & PARSER_ST_OUTSIDE_COMMENT_MASK) + PARSER_ST_INSIDE_COMMENT;
  94. if (line.empty())
  95. continue;
  96. }
  97. if (this->is_thrift == false)
  98. {
  99. int rpc_option = this->parse_pb_rpc_option(line);
  100. if (rpc_option == 1)
  101. continue;
  102. else if (rpc_option == 2)
  103. {
  104. fprintf(stderr, "[Parser ERROR] %s must not set "
  105. "\"option cc_generic_services = true\" for srpc.\n",
  106. idl_file.c_str());
  107. return false;
  108. }
  109. }
  110. if (this->parse_package_name(line, info.package_name) == true)
  111. continue;
  112. if (this->parse_include_file(line, file_path) == true)
  113. {
  114. if (!this->is_thrift)
  115. {
  116. if (file_path.rfind("google/protobuf/", 0) == 0)
  117. continue;
  118. }
  119. info.include_list.resize(info.include_list.size() + 1);
  120. info.include_list.back().input_dir = info.input_dir;
  121. succ = this->parse(file_path, info.include_list.back());
  122. if (!succ)
  123. {
  124. info.include_list.pop_back();
  125. fprintf(stderr, "[Parser ERROR] failed to parse "
  126. "\" %s \" in \" %s \"\n",
  127. file_path.c_str(), idl_file.c_str());
  128. return false;
  129. }
  130. continue;
  131. }
  132. if (this->is_thrift &&
  133. this->parse_thrift_typedef(line,
  134. old_type_name,
  135. new_type_name, info) == true)
  136. {
  137. info.typedef_list.push_back(typedef_descriptor{old_type_name,
  138. new_type_name});
  139. continue;
  140. }
  141. flag_append = false;
  142. if ((state & PARSER_ST_BLOCK_MASK) == PARSER_ST_OUTSIDE_BLOCK)
  143. {
  144. if (this->check_block_begin(in, line) == true)
  145. {
  146. state |= PARSER_ST_INSIDE_BLOCK;
  147. // stack_count++;
  148. block.clear();
  149. block = line;
  150. if (this->parse_block_name(line, block_type,
  151. block_name, extends_type) == false)
  152. {
  153. fprintf(stderr, "Invalid proto file line: %s\n",
  154. line.c_str());
  155. fprintf(stderr, "Failed to parse block name or value.\n");
  156. return false;
  157. }
  158. }
  159. }
  160. else
  161. flag_append = true;
  162. // not else, because { }; can in the same line
  163. if ((state & PARSER_ST_BLOCK_MASK) == PARSER_ST_INSIDE_BLOCK)
  164. {
  165. if (flag_append == true)
  166. block.append(line);
  167. if (this->check_block_begin(line) == true)
  168. ++stack_count;
  169. if (this->check_block_end(line) == true)
  170. {
  171. --stack_count;
  172. if (stack_count == 0)
  173. {
  174. state = PARSER_ST_OUTSIDE_BLOCK;
  175. //state &= PARSER_ST_OUTSIDE_BLOCK_MASK;
  176. Descriptor desc;
  177. if (block_type == "service")
  178. {
  179. if (this->is_thrift)
  180. succ = this->parse_service_thrift(info.file_name_prefix,
  181. block, desc, info);
  182. else
  183. succ = this->parse_service_pb(block, desc);
  184. if (!succ)
  185. return false;
  186. desc.block_type = block_type;
  187. desc.block_name = block_name;
  188. desc.extends_type = extends_type;
  189. if (desc.extends_type != "" && this->is_thrift)
  190. {
  191. Descriptor *extended_desc = NULL;
  192. parse_thrift_type_name(desc.extends_type,
  193. thrift_type_prefix,
  194. thrift_type_name);
  195. if (thrift_type_prefix == "")
  196. extended_desc = search_cur_file_descriptor(info,
  197. "service", thrift_type_name);
  198. else
  199. extended_desc = search_include_file_descriptor(info,
  200. thrift_type_prefix+".thrift",
  201. "service", thrift_type_name);
  202. if (!extended_desc)
  203. {
  204. fprintf(stderr,"service %s extends type %s not found\n",
  205. desc.block_name.c_str(),
  206. desc.extends_type.c_str());
  207. }
  208. else
  209. {
  210. desc.rpcs.insert(desc.rpcs.begin(),
  211. extended_desc->rpcs.begin(),
  212. extended_desc->rpcs.end());
  213. }
  214. }
  215. info.desc_list.emplace_back(std::move(desc));
  216. }
  217. else if ((block_type == "struct" || block_type == "union") &&
  218. this->is_thrift)
  219. {
  220. succ = this->parse_struct_thrift(info.file_name_prefix,
  221. block, desc, info);
  222. if (!succ)
  223. return false;
  224. desc.block_type = block_type;
  225. desc.block_name = block_name;
  226. info.desc_list.emplace_back(std::move(desc));
  227. }
  228. else if (block_type == "exception" && this->is_thrift)
  229. {
  230. succ = this->parse_struct_thrift(info.file_name_prefix,
  231. block, desc, info);
  232. if (!succ)
  233. return false;
  234. desc.block_type = "struct";
  235. desc.block_name = block_name;
  236. info.desc_list.emplace_back(std::move(desc));
  237. }
  238. else if (block_type == "enum" && this->is_thrift)
  239. {
  240. succ = this->parse_enum_thrift(block, desc);
  241. if (!succ)
  242. return false;
  243. SGenUtil::get_enum_set()->insert(info.file_name_prefix + block_name);
  244. desc.block_type = block_type;
  245. desc.block_name = block_name;
  246. info.desc_list.emplace_back(std::move(desc));
  247. }
  248. }
  249. }
  250. }
  251. }
  252. build_typedef_mapping(info);
  253. fclose(in);
  254. fprintf(stdout, "finish parsing proto file: [%s]\n", idl_file.c_str());
  255. return true;
  256. }
  257. std::string Parser::find_typedef_mapping_type(std::string& type_name,
  258. size_t& cur, idl_info& info)
  259. {
  260. size_t st = cur;
  261. cur = SGenUtil::find_next_nonspace(type_name,cur);
  262. for (; cur < type_name.size(); cur++)
  263. {
  264. if (type_name[cur] == ',' || type_name[cur] == '>' ||
  265. type_name[cur] == '<')
  266. break;
  267. }
  268. auto idl_type = SGenUtil::strip(type_name.substr(st, cur - st));
  269. if (info.typedef_mapping.find(idl_type) != info.typedef_mapping.end())
  270. return info.typedef_mapping[idl_type];
  271. if (idl_type == "bool" ||
  272. idl_type == "int8_t" ||
  273. idl_type == "int16_t" ||
  274. idl_type == "int32_t" ||
  275. idl_type == "int64_t" ||
  276. idl_type == "uint64_t" ||
  277. idl_type == "double" ||
  278. idl_type == "std::string")
  279. {
  280. return idl_type;
  281. }
  282. else if (idl_type == "std::map" && cur < type_name.size() &&
  283. type_name[cur] == '<')
  284. {
  285. auto key_type = find_typedef_mapping_type(type_name, ++cur, info);
  286. auto val_type = find_typedef_mapping_type(type_name, ++cur, info);
  287. ++cur;
  288. return "std::map<" + key_type + ", " + val_type + ">";
  289. }
  290. else if (idl_type == "std::set" && cur < type_name.size() &&
  291. type_name[cur] == '<')
  292. {
  293. auto val_type = find_typedef_mapping_type(type_name, ++cur, info);
  294. ++cur;
  295. return "std::set<" + val_type + ">";
  296. }
  297. else if (idl_type == "std::vector" && cur < type_name.size() &&
  298. type_name[cur] == '<')
  299. {
  300. auto val_type = find_typedef_mapping_type(type_name, ++cur, info);
  301. ++cur;
  302. return "std::vector<" + val_type + ">";
  303. }
  304. size_t pos = idl_type.find("::",0);
  305. std::string real_type_name;
  306. std::string type_namespace;
  307. if (pos == std::string::npos)
  308. real_type_name = idl_type;
  309. else
  310. {
  311. real_type_name = idl_type.substr(pos+2);
  312. type_namespace = idl_type.substr(0,pos);
  313. }
  314. for (auto& include : info.include_list)
  315. {
  316. if ( (type_namespace != "" && include.package_name.size() > 0 &&
  317. include.package_name[0] == type_namespace) ||
  318. (type_namespace == "" && include.package_name.size() == 0) )
  319. {
  320. for (auto& t : include.typedef_list)
  321. {
  322. if (real_type_name == t.new_type_name)
  323. {
  324. size_t offset = 0;
  325. include.typedef_mapping[real_type_name] =
  326. find_typedef_mapping_type(t.old_type_name, offset, include);
  327. return include.typedef_mapping[real_type_name];
  328. }
  329. }
  330. }
  331. }
  332. for (auto& t : info.typedef_list)
  333. {
  334. if (real_type_name == t.new_type_name)
  335. {
  336. size_t offset = 0;
  337. info.typedef_mapping[real_type_name] =
  338. find_typedef_mapping_type(t.old_type_name, offset, info);
  339. return info.typedef_mapping[real_type_name];
  340. }
  341. }
  342. return idl_type;
  343. }
  344. void Parser::build_typedef_mapping(idl_info& info)
  345. {
  346. for (auto &include:info.include_list)
  347. {
  348. for (auto &t:include.typedef_list)
  349. {
  350. if (include.typedef_mapping.find(t.new_type_name) != include.typedef_mapping.end())
  351. continue;
  352. size_t cur = 0;
  353. include.typedef_mapping[t.new_type_name] =
  354. find_typedef_mapping_type(t.old_type_name, cur, include);
  355. }
  356. }
  357. for (auto &t:info.typedef_list)
  358. {
  359. if (info.typedef_mapping.find(t.new_type_name) != info.typedef_mapping.end())
  360. continue;
  361. size_t cur = 0;
  362. info.typedef_mapping[t.new_type_name] =
  363. find_typedef_mapping_type(t.old_type_name, cur, info);
  364. }
  365. }
  366. // check / * and cut the first available line
  367. bool Parser::check_multi_comments_begin(std::string& line)
  368. {
  369. size_t pos = line.find("/*");
  370. if (pos == std::string::npos)
  371. return false;
  372. line = line.substr(0, pos);
  373. return true;
  374. }
  375. // check * / and cut the rest available line
  376. bool Parser::check_multi_comments_end(std::string& line)
  377. {
  378. size_t pos = line.find("*/");
  379. if (pos == std::string::npos)
  380. return false;
  381. pos += 2;
  382. while (line[pos] == ' ')
  383. pos++;
  384. line = line.substr(pos, line.length() - 1 - pos);
  385. return true;
  386. }
  387. // [ret] 0: no rpc option; 1: rpc_option = false; 2: rpc_option = true;
  388. int Parser::parse_pb_rpc_option(const std::string& line)
  389. {
  390. size_t pos = line.find("option");
  391. if (pos == std::string::npos)
  392. return 0;
  393. pos = line.find("cc_generic_services", pos);
  394. if (pos == std::string::npos)
  395. return 0;
  396. pos = line.find("true", pos);
  397. if (pos == std::string::npos)
  398. return 1;
  399. return 2;
  400. }
  401. bool Parser::parse_thrift_typedef(const std::string& line,
  402. std::string& old_type_name,
  403. std::string& new_type_name,
  404. idl_info&info)
  405. {
  406. std::vector<std::string> elems = SGenUtil::split_by_space(line);
  407. if (elems.size() >= 3 && elems[0] == "typedef")
  408. {
  409. size_t offset = 0;
  410. std::string idl_type;
  411. for (size_t i = 1; i < elems.size()-1; i++)
  412. idl_type.append(elems[i]);
  413. old_type_name = gen_param_var(idl_type,offset,info);
  414. new_type_name = elems[elems.size()-1];
  415. return true;
  416. }
  417. return false;
  418. }
  419. bool Parser::parse_include_file(const std::string& line, std::string& file_name)
  420. {
  421. std::string include_prefix = (this->is_thrift ? "include" : "import");
  422. size_t pos = line.find(include_prefix);
  423. if (pos != 0)
  424. return false;
  425. auto st = line.find_first_of('\"');
  426. auto ed = line.find_last_of('\"');
  427. if (st == std::string::npos || ed == std::string::npos || st == ed)
  428. return false;
  429. file_name = line.substr(st + 1, ed - st - 1);
  430. // fprintf(stderr, "parse_include_file(%s,%s)\n", line.c_str(), file_name.c_str());
  431. return true;
  432. }
  433. bool Parser::parse_package_name(const std::string& line,
  434. std::vector<std::string>& package_name)
  435. {
  436. std::string package_prefix = (this->is_thrift ? "namespace" : "package");
  437. size_t pos = line.find(package_prefix);
  438. if (pos != 0)
  439. return false;
  440. pos += package_prefix.length();
  441. while (pos < line.length() && isspace(line[pos]))
  442. pos++;
  443. if (this->is_thrift)
  444. {
  445. pos = line.find("cpp", pos);
  446. if (pos == std::string::npos)
  447. return false;
  448. pos += 3;
  449. while (pos < line.length() && isspace(line[pos]))
  450. pos++;
  451. }
  452. size_t begin;
  453. if (this->is_thrift)
  454. {
  455. begin = line.find_last_of('/');
  456. if (begin == std::string::npos)
  457. begin = pos;
  458. else
  459. begin++;
  460. } else {
  461. begin = pos;
  462. }
  463. while (pos < line.length() && !isspace(line[pos]) &&
  464. line[pos] != ';')
  465. {
  466. pos++;
  467. }
  468. std::string names = line.substr(begin, pos - begin);
  469. pos = names.find('.');
  470. while (pos != (size_t)-1)
  471. {
  472. package_name.push_back(names.substr(0, pos));
  473. names = names.substr(pos + 1, names.length() - pos);
  474. pos = names.find('.');
  475. }
  476. package_name.push_back(names.substr(0, pos));
  477. return true;
  478. }
  479. static std::string gen_param_var(const std::string& type_name, size_t& cur,
  480. idl_info& info)
  481. {
  482. size_t st = cur;
  483. cur = SGenUtil::find_next_nonspace(type_name,cur);
  484. for (; cur < type_name.size(); cur++)
  485. {
  486. if (type_name[cur] == ',' || type_name[cur] == '>' ||
  487. type_name[cur] == '<')
  488. break;
  489. }
  490. auto idl_type = SGenUtil::strip(type_name.substr(st, cur - st));
  491. if (idl_type == "bool")
  492. return "bool";
  493. else if (idl_type == "i8" || idl_type == "byte")
  494. return "int8_t";
  495. else if (idl_type == "i16")
  496. return "int16_t";
  497. else if (idl_type == "i32")
  498. return "int32_t";
  499. else if (idl_type == "i64")
  500. return "int64_t";
  501. else if (idl_type == "u64")
  502. return "uint64_t";
  503. else if (idl_type == "double")
  504. return "double";
  505. else if (idl_type == "string" || idl_type == "binary")
  506. return "std::string";
  507. else if (idl_type == "map" && cur < type_name.size() &&
  508. type_name[cur] == '<')
  509. {
  510. auto key_type = gen_param_var(type_name, ++cur, info);
  511. auto val_type = gen_param_var(type_name, ++cur, info);
  512. ++cur;
  513. return "std::map<" + key_type + ", " + val_type + ">";
  514. }
  515. else if (idl_type == "set" && cur < type_name.size() &&
  516. type_name[cur] == '<')
  517. {
  518. auto val_type = gen_param_var(type_name, ++cur, info);
  519. ++cur;
  520. return "std::set<" + val_type + ">";
  521. }
  522. else if (idl_type == "list" && cur < type_name.size() &&
  523. type_name[cur] == '<')
  524. {
  525. auto val_type = gen_param_var(type_name, ++cur, info);
  526. ++cur;
  527. return "std::vector<" + val_type + ">";
  528. }
  529. return type_prefix_to_namespace(idl_type, info);
  530. }
  531. static void fill_rpc_param_type(const std::string& file_name_prefix,
  532. const std::string idl_type,
  533. rpc_param& param, idl_info& info)
  534. {
  535. if (idl_type == "bool")
  536. {
  537. param.data_type = srpc::TDT_BOOL;
  538. param.type_name = "bool";
  539. }
  540. else if (idl_type == "i8" || idl_type == "byte")
  541. {
  542. param.data_type = srpc::TDT_I08;
  543. param.type_name = "int8_t";
  544. }
  545. else if (idl_type == "i16")
  546. {
  547. param.data_type = srpc::TDT_I16;
  548. param.type_name = "int16_t";
  549. }
  550. else if (idl_type == "i32")
  551. {
  552. param.data_type = srpc::TDT_I32;
  553. param.type_name = "int32_t";
  554. }
  555. else if (idl_type == "i64")
  556. {
  557. param.data_type = srpc::TDT_I64;
  558. param.type_name = "int64_t";
  559. }
  560. else if (idl_type == "u64")
  561. {
  562. param.data_type = srpc::TDT_U64;
  563. param.type_name = "uint64_t";
  564. }
  565. else if (idl_type == "double")
  566. {
  567. param.data_type = srpc::TDT_DOUBLE;
  568. param.type_name = "double";
  569. }
  570. else if (idl_type == "string" || idl_type == "binary")
  571. {
  572. param.data_type = srpc::TDT_STRING;
  573. param.type_name = "std::string";
  574. }
  575. else if (SGenUtil::start_with(idl_type, "list"))
  576. {
  577. size_t cur = 0;
  578. param.data_type = srpc::TDT_LIST;
  579. param.type_name = gen_param_var(idl_type, cur, info);
  580. }
  581. else if (SGenUtil::start_with(idl_type, "map"))
  582. {
  583. size_t cur = 0;
  584. param.data_type = srpc::TDT_MAP;
  585. param.type_name = gen_param_var(idl_type, cur, info);
  586. }
  587. else if (SGenUtil::start_with(idl_type, "set"))
  588. {
  589. size_t cur = 0;
  590. param.data_type = srpc::TDT_SET;
  591. param.type_name = gen_param_var(idl_type, cur, info);
  592. }
  593. else
  594. {
  595. auto *enum_set = SGenUtil::get_enum_set();
  596. if (enum_set->count(idl_type) > 0 ||
  597. enum_set->count(file_name_prefix + idl_type) > 0)
  598. {
  599. //enum
  600. param.data_type = srpc::TDT_I32;
  601. param.type_name = "int32_t";
  602. }
  603. else
  604. {
  605. //struct
  606. param.data_type = srpc::TDT_STRUCT;
  607. param.type_name = type_prefix_to_namespace(idl_type,info);
  608. }
  609. }
  610. }
  611. std::vector<std::string> Parser::split_thrift_rpc(const std::string& str)
  612. {
  613. std::vector<std::string> res;
  614. std::string::const_iterator start = str.begin();
  615. std::string::const_iterator parameter_end = str.end();
  616. std::string::const_iterator throws_end = str.end();
  617. while (1)
  618. {
  619. parameter_end = find(start,str.end(), ')');
  620. if (parameter_end == str.end())
  621. {
  622. res.emplace_back(start,parameter_end);
  623. break;
  624. }
  625. std::string::const_iterator offset = find_if(parameter_end + 1, str.end(),
  626. [](char c){return !std::isspace(c);});
  627. if (offset == str.end())
  628. {
  629. res.emplace_back(start,parameter_end);
  630. break;
  631. }
  632. if (str.compare(offset-str.begin(), 6, "throws") == 0)
  633. {
  634. throws_end = find(offset,str.end(), ')');
  635. res.emplace_back(start,throws_end);
  636. start = throws_end + 1;
  637. }
  638. else
  639. {
  640. res.emplace_back(start,parameter_end);
  641. start = parameter_end + 1;
  642. }
  643. }
  644. return res;
  645. }
  646. bool Parser::parse_rpc_param_thrift(const std::string& file_name_prefix,
  647. const std::string& str,
  648. std::vector<rpc_param>& params,
  649. idl_info& info)
  650. {
  651. size_t left_b = 0;
  652. rpc_param param;
  653. std::string idl_type;
  654. if (left_b + 1 < str.size())
  655. {
  656. auto bb = parse_thrift_variable(str.substr(left_b + 1), ',');
  657. for (const auto& ele : bb)
  658. {
  659. auto single_line = SGenUtil::split_skip_string(ele, '\n');
  660. if (single_line.size() != 1)
  661. continue;
  662. auto filedparam = SGenUtil::split_skip_string(single_line[0], ':');
  663. if (filedparam.size() != 2)
  664. continue;
  665. auto typevar = parse_thrift_variable(filedparam[1], ' ');
  666. if (typevar.size() != 2)
  667. continue;
  668. param.var_name = typevar[1];
  669. param.required_state = srpc::THRIFT_STRUCT_FIELD_REQUIRED;
  670. param.field_id = atoi(SGenUtil::strip(filedparam[0]).c_str());
  671. idl_type = SGenUtil::strip(typevar[0]);
  672. fill_rpc_param_type(file_name_prefix, idl_type, param, info);
  673. params.push_back(param);
  674. }
  675. }
  676. return true;
  677. }
  678. bool Parser::parse_service_thrift(const std::string& file_name_prefix,
  679. const std::string& block,
  680. Descriptor& desc,
  681. idl_info& info)
  682. {
  683. rpc_descriptor rpc_desc;
  684. auto st = block.find_first_of('{');
  685. auto ed = block.find_last_of('}');
  686. if (st == std::string::npos || ed == std::string::npos || st == ed)
  687. return false;
  688. std::string valid_block = block.substr(st + 1, ed - st - 1);
  689. auto arr = split_thrift_rpc(valid_block);
  690. for (const auto& ele : arr)
  691. {
  692. auto line = SGenUtil::strip(ele);
  693. size_t i = 0;
  694. for (; i < line.size(); i++)
  695. {
  696. if (line[i] != ';' && line[i] != ',' && !isspace(line[i]))
  697. break;
  698. }
  699. if (i == line.size())
  700. continue;
  701. line = line.substr(i);
  702. if (line.empty())
  703. continue;
  704. auto left_b = line.find('(');
  705. if (left_b == std::string::npos)
  706. continue;
  707. auto aa = SGenUtil::split_skip_string(line.substr(0, left_b), ' ');
  708. if (aa.size() != 2)
  709. continue;
  710. rpc_desc.method_name = SGenUtil::strip(aa[1]);
  711. if (rpc_desc.method_name.empty())
  712. continue;
  713. rpc_desc.request_name = rpc_desc.method_name + "Request";
  714. rpc_desc.response_name = rpc_desc.method_name + "Response";
  715. auto idl_type = SGenUtil::strip(aa[0]);
  716. rpc_param param;
  717. param.var_name = "result";
  718. param.required_state = srpc::THRIFT_STRUCT_FIELD_REQUIRED;
  719. param.field_id = 0;
  720. if (idl_type != "void")
  721. {
  722. fill_rpc_param_type(file_name_prefix, idl_type, param, info);
  723. rpc_desc.resp_params.push_back(param);
  724. }
  725. auto right_b = line.find(')',left_b);
  726. if (right_b == std::string::npos)
  727. {
  728. parse_rpc_param_thrift(file_name_prefix, line.substr(left_b),
  729. rpc_desc.req_params,info);
  730. }
  731. else
  732. {
  733. parse_rpc_param_thrift(file_name_prefix,
  734. line.substr(left_b, right_b - left_b),
  735. rpc_desc.req_params,
  736. info);
  737. auto throws_start = line.find("throws", right_b);
  738. if (throws_start != std::string::npos)
  739. {
  740. left_b = line.find('(', throws_start + 6);
  741. parse_rpc_param_thrift(file_name_prefix, line.substr(left_b),
  742. rpc_desc.resp_params, info);
  743. }
  744. }
  745. fprintf(stdout, "Successfully parse method:%s req:%s resp:%s\n",
  746. rpc_desc.method_name.c_str(),
  747. rpc_desc.request_name.c_str(),
  748. rpc_desc.response_name.c_str());
  749. desc.rpcs.emplace_back(std::move(rpc_desc));
  750. }
  751. return true;
  752. }
  753. void Parser::check_single_comments(std::string& line)
  754. {
  755. size_t pos = line.find("#");
  756. if (pos == std::string::npos)
  757. pos = line.find("//");
  758. if (pos != std::string::npos)
  759. {
  760. line.resize(pos);
  761. return;
  762. }
  763. if (pos == std::string::npos)
  764. pos = line.find("/*");
  765. size_t end;
  766. while (pos != std::string::npos)
  767. {
  768. end = line.find("*/", pos + 2);
  769. if (end == std::string::npos)
  770. return; // multi_comments can handle this, except for 'a/*\n'
  771. line.erase(pos, end - pos + 2);
  772. pos = line.find("/*", pos);
  773. }
  774. }
  775. bool Parser::parse_enum_thrift(const std::string& block, Descriptor& desc)
  776. {
  777. rpc_param param;
  778. auto st = block.find_first_of('{');
  779. auto ed = block.find_last_of('}');
  780. if (st == std::string::npos || ed == std::string::npos || st == ed)
  781. return false;
  782. std::string valid_block = block.substr(st + 1, ed - st - 1);
  783. for (size_t i = 0; i < valid_block.size(); i++)
  784. if (valid_block[i] == '\n' || valid_block[i] == '\r' || valid_block[i] == ',')
  785. valid_block[i] = ';';
  786. auto arr = SGenUtil::split_skip_string(valid_block, ';');
  787. for (const auto& ele : arr)
  788. {
  789. auto line = SGenUtil::strip(ele);
  790. if (line.empty())
  791. continue;
  792. if (line.back() == ';' || line.back() == ',')
  793. line.resize(line.size() - 1);
  794. desc.enum_lines.push_back(line);
  795. }
  796. return true;
  797. }
  798. bool Parser::parse_struct_thrift(const std::string& file_name_prefix,
  799. const std::string& block,
  800. Descriptor& desc, idl_info& info)
  801. {
  802. auto st = block.find_first_of('{');
  803. auto ed = block.find_last_of('}');
  804. if (st == std::string::npos || ed == std::string::npos || st == ed)
  805. return false;
  806. std::string valid_block = block.substr(st + 1, ed - st - 1);
  807. int deep = 0;
  808. bool in_string = false;
  809. for (size_t i = 0; i < valid_block.size(); i++)
  810. {
  811. int c = valid_block[i];
  812. if (c == '\n' || c == '\r')
  813. {
  814. valid_block[i] = ';';
  815. in_string = false;
  816. }
  817. else if (c == ',' && !in_string && deep == 0)
  818. valid_block[i] = ';';
  819. else if (c == '<' && !in_string)
  820. deep++;
  821. else if (c == '>' && !in_string)
  822. deep--;
  823. else if (c == '[' && !in_string)
  824. valid_block[i] = '{';
  825. else if (c == ']' && !in_string)
  826. valid_block[i] = '}';
  827. else if (c == '\"')
  828. in_string = !in_string;
  829. else if (in_string && c == '\\')
  830. {
  831. if (i + 1 < valid_block.size())
  832. i++;
  833. }
  834. }
  835. auto arr = SGenUtil::split_skip_string(valid_block, ';');
  836. for (const auto& ele : arr)
  837. {
  838. auto line = SGenUtil::strip(ele);
  839. if (line.empty())
  840. continue;
  841. auto aabb = SGenUtil::split_skip_string(line, ':');
  842. if (aabb.size() != 2)
  843. continue;
  844. auto aa = SGenUtil::strip(aabb[0]);
  845. if (aa.empty())
  846. continue;
  847. rpc_param param;
  848. param.field_id = atoi(aa.c_str());
  849. auto bb = SGenUtil::strip(aabb[1]);
  850. auto bbcc = SGenUtil::split_skip_string(bb, '=');
  851. if (bbcc.size() == 2)
  852. {
  853. bb = SGenUtil::strip(bbcc[0]);
  854. param.default_value = SGenUtil::strip(bbcc[1]);
  855. }
  856. auto idx1 = std::string::npos;//bb.find_first_of(' ');
  857. auto idx2 = std::string::npos;//bb.find_last_of(' ');
  858. for (size_t i = 0; i < bb.size(); i++)
  859. {
  860. if (isspace(bb[i]))
  861. {
  862. idx1 = i;
  863. break;
  864. }
  865. }
  866. for (size_t i = 0; i < bb.size(); i++)
  867. {
  868. if (isspace(bb[bb.size() - i - 1]))
  869. {
  870. idx2 = bb.size() - i - 1;
  871. break;
  872. }
  873. }
  874. if (idx1 == std::string::npos || idx2 == std::string::npos)
  875. continue;
  876. std::string b1 = SGenUtil::strip(bb.substr(0, idx1));
  877. std::string b2, b3;
  878. if (idx1 == idx2 || (b1 != "required" && b1 != "optional"))
  879. {
  880. param.required_state = srpc::THRIFT_STRUCT_FIELD_DEFAULT;
  881. b1 = "default";
  882. b2 = SGenUtil::strip(bb.substr(0, idx2));
  883. b3 = SGenUtil::strip(bb.substr(idx2 + 1));
  884. }
  885. else
  886. {
  887. param.required_state = (b1 == "required") ?
  888. srpc::THRIFT_STRUCT_FIELD_REQUIRED :
  889. srpc::THRIFT_STRUCT_FIELD_OPTIONAL;
  890. b2 = SGenUtil::strip(bb.substr(idx1 + 1, idx2 - idx1 - 1));
  891. b3 = SGenUtil::strip(bb.substr(idx2 + 1));
  892. }
  893. if (b1.empty() || b2.empty() || b3.empty())
  894. continue;
  895. param.var_name = b3;
  896. fill_rpc_param_type(file_name_prefix, b2, param, info);
  897. fprintf(stdout, "Successfully parse struct param: %s %s %s\n",
  898. param.type_name.c_str(), param.var_name.c_str(),
  899. param.default_value.c_str());
  900. desc.st.params.push_back(param);
  901. }
  902. return true;
  903. }
  904. bool Parser::parse_service_pb(const std::string& block, Descriptor& desc)
  905. {
  906. size_t pos = block.find("{");
  907. if (pos == std::string::npos)
  908. return false;
  909. while (pos < block.length())
  910. {
  911. rpc_descriptor rpc_desc;
  912. pos = block.find("rpc", pos);
  913. if (pos == std::string::npos)
  914. {
  915. if (desc.rpcs.size() == 0)
  916. {
  917. fprintf(stderr, "no \"rpc\" in service block [%s]\n",
  918. block.c_str());
  919. return false;
  920. } else {
  921. return true;
  922. }
  923. }
  924. pos += strlen("rpc");
  925. while (block[pos] == ' ' && pos < block.length())
  926. pos++;
  927. if (pos == block.length())
  928. return false;
  929. size_t method_name_pos = block.find("(", pos);
  930. if (method_name_pos == std::string::npos)
  931. {
  932. fprintf(stderr, "no method_name in service block [%s]\n",
  933. block.c_str());
  934. return false;
  935. }
  936. rpc_desc.method_name = std::string(&block[pos], &block[method_name_pos]);
  937. rpc_desc.method_name.erase(rpc_desc.method_name.find_last_not_of(" ") + 1);
  938. pos = method_name_pos;
  939. while (block[pos] == ' ' && pos < block.length())
  940. pos++;
  941. if (pos == block.length())
  942. return false;
  943. size_t request_name_pos = block.find(")", pos + 1);
  944. if (request_name_pos == std::string::npos)
  945. {
  946. fprintf(stderr, "no request_name in service block [%s]\n",
  947. block.c_str());
  948. return false;
  949. }
  950. rpc_desc.request_name = std::string(&block[pos + 1],
  951. &block[request_name_pos]);
  952. pos = block.find("returns", pos + 1);
  953. if (pos == std::string::npos)
  954. {
  955. fprintf(stderr, "no \"returns\" in service block [%s]\n",
  956. block.c_str());
  957. return false;
  958. }
  959. while (block[pos] == ' ' && pos < block.length())
  960. pos++;
  961. if (pos == block.length())
  962. return true;
  963. size_t response_name_pos = block.find("(", pos + 1);
  964. size_t response_name_end = block.find(")", pos + 1);
  965. if (response_name_pos == std::string::npos ||
  966. response_name_end == std::string::npos)
  967. {
  968. fprintf(stderr, "no response_name in service block [%s]\n",
  969. block.c_str());
  970. return false;
  971. }
  972. rpc_desc.response_name = std::string(&block[response_name_pos + 1],
  973. &block[response_name_end]);
  974. fprintf(stdout, "Successfully parse method:%s req:%s resp:%s\n",
  975. rpc_desc.method_name.c_str(),
  976. rpc_desc.request_name.c_str(),
  977. rpc_desc.response_name.c_str());
  978. desc.rpcs.emplace_back(std::move(rpc_desc));
  979. pos = response_name_end;
  980. }
  981. return true;
  982. }
  983. bool Parser::parse_block_name(const std::string& line,
  984. std::string& block_name,
  985. std::string& block_name_value,
  986. std::string& extends_type)
  987. {
  988. size_t pos = line.find("{");
  989. if (pos == std::string::npos)
  990. {
  991. fprintf(stderr, "failed to parse block name in %s\n",line.c_str());
  992. return false;
  993. }
  994. std::vector<std::string> elems = SGenUtil::split_by_space(line.substr(0,pos));
  995. if (elems.size() == 2)
  996. {
  997. block_name = elems[0];
  998. block_name_value = elems[1];
  999. extends_type = "";
  1000. }
  1001. else if (this->is_thrift && elems.size() == 4 && elems[0] == "service" &&
  1002. elems[2] == "extends")
  1003. {
  1004. block_name = elems[0];
  1005. block_name_value = elems[1];
  1006. extends_type = elems[3];
  1007. }
  1008. else
  1009. {
  1010. fprintf(stderr, "failed to parse block name in %s\n", line.c_str());
  1011. return false;
  1012. }
  1013. fprintf(stdout, "Successfully parse service block [%s] : %s\n",
  1014. block_name.c_str(), block_name_value.c_str());
  1015. return true;
  1016. }
  1017. /*
  1018. bool Parser::parse_block_name(const std::string& line,
  1019. std::string& block_name,
  1020. std::string& block_name_value)
  1021. {
  1022. size_t pos = line.find_first_of(" ", 0);
  1023. if (pos == std::string::npos)
  1024. {
  1025. fprintf(stderr, "failed to parse rpc name in %s\n", line.c_str());
  1026. return false;
  1027. }
  1028. block_name = std::string(&line[0], &line[pos]);
  1029. size_t value_pos = line.find_first_of(" ", pos + 1);
  1030. if (value_pos != std::string::npos)
  1031. {
  1032. block_name_value = std::string(&line[pos + 1], &line[value_pos]);
  1033. } else {
  1034. size_t end = line.find("{");
  1035. if (end == std::string::npos)
  1036. return false;
  1037. end--;
  1038. while (line[end] == '\n' || line[end] == '\r' || line[end] == ' ')
  1039. end --;
  1040. // block_name_value = std::string(&line[pos + 1], &line[line.length() - 1]);
  1041. block_name_value = std::string(&line[pos + 1], &line[end + 1]);
  1042. }
  1043. fprintf(stdout, "Successfully parse service block [%s] : %s\n",
  1044. block_name.c_str(), block_name_value.c_str());
  1045. return true;
  1046. }
  1047. */
  1048. bool Parser::check_block_begin(FILE *in, std::string& line)
  1049. {
  1050. if (line.find(";") != std::string::npos)
  1051. return false;
  1052. if (line.find("{") != std::string::npos)
  1053. return true;
  1054. char line_buffer[LINE_LENGTH_MAX];
  1055. if (fgets(line_buffer, LINE_LENGTH_MAX, in))
  1056. {
  1057. std::string next = SGenUtil::strip(line_buffer);
  1058. if (next.find("{") != std::string::npos)
  1059. {
  1060. line.append(next.c_str());
  1061. return true;
  1062. }
  1063. }
  1064. return false;
  1065. }
  1066. bool Parser::check_block_begin(const std::string& line)
  1067. {
  1068. if (line.find("{") == std::string::npos)
  1069. return false;
  1070. return true;
  1071. }
  1072. bool Parser::check_block_end(const std::string& line)
  1073. {
  1074. if (line.find("}") == std::string::npos
  1075. && line.find("};") == std::string::npos)
  1076. return false;
  1077. return true;
  1078. }
  1079. void parse_thrift_type_name(const std::string& type_name,
  1080. std::string& type_prefix,
  1081. std::string& real_type_name)
  1082. {
  1083. size_t pos = type_name.find('.',0);
  1084. if (pos == std::string::npos)
  1085. {
  1086. type_prefix = "";
  1087. real_type_name = SGenUtil::strip(type_name);
  1088. }
  1089. else
  1090. {
  1091. type_prefix = SGenUtil::strip(type_name.substr(0,pos));
  1092. real_type_name = SGenUtil::strip(type_name.substr(pos+1));
  1093. }
  1094. }
  1095. std::string type_prefix_to_namespace(const std::string& type_name,
  1096. idl_info& info)
  1097. {
  1098. std::string prefix;
  1099. std::string real_type;
  1100. parse_thrift_type_name(type_name, prefix, real_type);
  1101. if (prefix == "")
  1102. return type_name;
  1103. idl_info *include = search_include_file(info, prefix + ".thrift");
  1104. if (include == NULL)
  1105. {
  1106. fprintf(stderr,"cannot find type %s\n",type_name.c_str());
  1107. return type_name;
  1108. }
  1109. if (include->package_name.size() > 0)
  1110. return include->package_name[0]+"::"+real_type;
  1111. return "::"+real_type;
  1112. }
  1113. Descriptor *search_cur_file_descriptor(idl_info& info,
  1114. const std::string& block_type,
  1115. const std::string& block_name)
  1116. {
  1117. for (auto &desc : info.desc_list)
  1118. {
  1119. if (desc.block_type == block_type && desc.block_name == block_name)
  1120. return &desc;
  1121. }
  1122. return NULL;
  1123. }
  1124. Descriptor *search_include_file_descriptor(idl_info& info,
  1125. const std::string include_file_name,
  1126. const std::string& block_type,
  1127. const std::string& block_name)
  1128. {
  1129. for (auto &include : info.include_list)
  1130. {
  1131. if (include.file_name == include_file_name)
  1132. return search_cur_file_descriptor(include, block_type, block_name);
  1133. }
  1134. return NULL;
  1135. }
  1136. idl_info *search_include_file(idl_info& info, const std::string file_name)
  1137. {
  1138. for (auto &include : info.include_list)
  1139. {
  1140. if (include.file_name == file_name)
  1141. return &include;
  1142. }
  1143. return NULL;
  1144. }
  1145. idl_info *search_namespace(idl_info& info, const std::string name_space)
  1146. {
  1147. if (name_space == "")
  1148. return &info;
  1149. for (auto &include : info.include_list)
  1150. {
  1151. if (include.package_name.size() > 0 &&
  1152. include.package_name[0] == name_space)
  1153. return &include;
  1154. }
  1155. return NULL;
  1156. }
  1157. int Parser::find_valid(const std::string& line)
  1158. {
  1159. /*
  1160. char *p = line.c_str();
  1161. char *q = p;
  1162. while (q != p + line.length())
  1163. {
  1164. if(*q != ' ' && *q != '\t')
  1165. return pos;
  1166. }
  1167. */
  1168. return 0;
  1169. }
  1170. std::vector<std::string> parse_thrift_variable(const std::string& str, char sep)
  1171. {
  1172. std::vector<std::string> res;
  1173. if (sep == '\"')
  1174. return res;
  1175. const char *cursor = str.c_str();
  1176. const char *start = cursor;
  1177. bool in_map = false;
  1178. std::string param;
  1179. while (*cursor)
  1180. {
  1181. if (*cursor == '\"')
  1182. {
  1183. cursor = SGenUtil::skip_string(++cursor);
  1184. if (!*cursor)
  1185. break;
  1186. }
  1187. else if (*cursor == sep)
  1188. {
  1189. param = std::string(start, cursor);
  1190. if (in_map == false &&
  1191. param.find("map") != std::string::npos &&
  1192. param.find("<") != std::string::npos)
  1193. {
  1194. in_map = true;
  1195. cursor++;
  1196. continue;
  1197. }
  1198. if (start < cursor)
  1199. res.emplace_back(param);
  1200. start = cursor + 1;
  1201. in_map = false;
  1202. }
  1203. cursor++;
  1204. }
  1205. if (start < cursor)
  1206. res.emplace_back(start, cursor);
  1207. return res;
  1208. }