Skip to content

Commit a53af1e

Browse files
authored
srpc_generator : fix parsing thrift (#352)
* cmake : set Protobuf_LIB_DIR and Protobuf_LIBRARY * wiki : update the picture of wiki.md * srpc_generator : fix parsing thrift * srpc_generator : add parsing_thrift_variable() for map * srpc_generator : support context before multi_commnet
1 parent 7737e42 commit a53af1e

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

src/generator/descriptor.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct idl_info
8484

8585
class SGenUtil
8686
{
87-
private:
87+
public:
8888
static const char *skip_string(const char *cursor)
8989
{
9090
while (*cursor && *cursor != '\"')
@@ -102,7 +102,6 @@ class SGenUtil
102102
return cursor;
103103
}
104104

105-
public:
106105
static std::vector<std::string> split_skip_string(const std::string& str,
107106
char sep)
108107
{

src/generator/parser.cc

+76-5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ idl_info *search_namespace(idl_info& info, const std::string name_space);
3636
void parse_thrift_type_name(const std::string& type_name,
3737
std::string& type_prefix,
3838
std::string& real_type_name);
39+
std::vector<std::string> parse_thrift_variable(const std::string& str, char sep);
3940

4041
bool Parser::parse(const std::string& proto_file, idl_info& info)
4142
{
@@ -108,7 +109,8 @@ bool Parser::parse(const std::string& proto_file, idl_info& info)
108109
else if (this->check_multi_comments_begin(line))
109110
{
110111
state = (state & PARSER_ST_OUTSIDE_COMMENT_MASK) + PARSER_ST_INSIDE_COMMENT;
111-
continue;
112+
if (line.empty())
113+
continue;
112114
}
113115

114116
if (this->is_thrift == false)
@@ -743,14 +745,18 @@ bool Parser::parse_rpc_param_thrift(const std::string& file_name_prefix,
743745
std::string idl_type;
744746
if (left_b + 1 < str.size())
745747
{
746-
auto bb = SGenUtil::split_skip_string(str.substr(left_b + 1), ',');
748+
auto bb = parse_thrift_variable(str.substr(left_b + 1), ',');
747749
for (const auto& ele : bb)
748750
{
749-
auto filedparam = SGenUtil::split_skip_string(ele, ':');
751+
auto single_line = SGenUtil::split_skip_string(ele, '\n');
752+
if (single_line.size() != 1)
753+
continue;
754+
755+
auto filedparam = SGenUtil::split_skip_string(single_line[0], ':');
750756
if (filedparam.size() != 2)
751757
continue;
752758

753-
auto typevar = SGenUtil::split_skip_string(filedparam[1], ' ');
759+
auto typevar = parse_thrift_variable(filedparam[1], ' ');
754760
if (typevar.size() != 2)
755761
continue;
756762

@@ -777,6 +783,7 @@ bool Parser::parse_service_thrift(const std::string& file_name_prefix,
777783
return false;
778784

779785
std::string valid_block = block.substr(st + 1, ed - st - 1);
786+
780787
auto arr = split_thrift_rpc(valid_block);
781788

782789
for (const auto& ele : arr)
@@ -861,9 +868,25 @@ void Parser::check_single_comments(std::string& line)
861868
size_t pos = line.find("#");
862869
if (pos == std::string::npos)
863870
pos = line.find("//");
864-
865871
if (pos != std::string::npos)
872+
{
866873
line.resize(pos);
874+
return;
875+
}
876+
877+
if (pos == std::string::npos)
878+
pos = line.find("/*");
879+
880+
size_t end;
881+
while (pos != std::string::npos)
882+
{
883+
end = line.find("*/", pos + 2);
884+
if (end == std::string::npos)
885+
return; // multi_comments can handle this, except for 'a/*\n'
886+
887+
line.erase(pos, end - pos + 2);
888+
pos = line.find("/*", pos);
889+
}
867890
}
868891

869892
bool Parser::parse_enum_thrift(const std::string& block, Descriptor& desc)
@@ -1321,3 +1344,51 @@ int Parser::find_valid(const std::string& line)
13211344
return 0;
13221345
}
13231346

1347+
std::vector<std::string> parse_thrift_variable(const std::string& str, char sep)
1348+
{
1349+
std::vector<std::string> res;
1350+
if (sep == '\"')
1351+
return res;
1352+
1353+
const char *cursor = str.c_str();
1354+
const char *start = cursor;
1355+
1356+
bool in_map = false;
1357+
std::string param;
1358+
1359+
while (*cursor)
1360+
{
1361+
if (*cursor == '\"')
1362+
{
1363+
cursor = SGenUtil::skip_string(++cursor);
1364+
if (!*cursor)
1365+
break;
1366+
}
1367+
else if (*cursor == sep)
1368+
{
1369+
param = std::string(start, cursor);
1370+
1371+
if (in_map == false &&
1372+
param.find("map") != std::string::npos &&
1373+
param.find("<") != std::string::npos)
1374+
{
1375+
in_map = true;
1376+
cursor++;
1377+
continue;
1378+
}
1379+
1380+
if (start < cursor)
1381+
res.emplace_back(param);
1382+
1383+
start = cursor + 1;
1384+
in_map = false;
1385+
}
1386+
1387+
cursor++;
1388+
}
1389+
1390+
if (start < cursor)
1391+
res.emplace_back(start, cursor);
1392+
1393+
return res;
1394+
}

src/generator/printer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ inline %s %sClient::%s(%s)
14801480
)";
14811481

14821482
std::string client_method_thrift_end_format = R"(
1483-
%s(&__thrift__sync__req, &__thrift__sync__resp, %sClient::get_thread_sync_ctx());
1483+
this->%s(&__thrift__sync__req, &__thrift__sync__resp, %sClient::get_thread_sync_ctx());
14841484
%s;
14851485
}
14861486

0 commit comments

Comments
 (0)