feature small fix
Some checks failed
build-crpc / build (Debug, host.toolchain.cmake) (push) Failing after 4m7s
build-crpc / build (Release, host.toolchain.cmake) (push) Failing after 4m21s

This commit is contained in:
tqcq 2023-12-04 12:27:47 +08:00
parent 760b99f00d
commit 055f32c163
15 changed files with 205 additions and 16 deletions

View File

@ -34,6 +34,8 @@ add_executable(crpc
src/plugins/c/rpcs/crpc.cpp src/plugins/c/rpcs/crpc.cpp
src/plugins/c/c_service_plugin.cpp src/plugins/c/c_service_plugin.cpp
${antlr4cpp_src_files_tqcq} ${antlr4cpp_src_files_tqcq}
src/plugins/c/items/c_item_message.cpp
src/plugins/c/items/c_item_uint_32.cpp
) )
target_include_directories(crpc PRIVATE src/grammar/ src/) target_include_directories(crpc PRIVATE src/grammar/ src/)
target_include_directories(crpc PRIVATE target_include_directories(crpc PRIVATE

View File

@ -35,17 +35,30 @@ public:
message_stack.pop(); message_stack.pop();
} }
void exitItemStatement(ProtoParser::ItemStatementContext *context) override void exitMessageBaseItemStatement(ProtoParser::MessageBaseItemStatementContext *context) override
{ {
int idx = std::stoi(context->NUMBER()->getText()); int idx = std::stoi(context->NUMBER()->getText());
tqcq::Item::Ptr item = nullptr; tqcq::Item::Ptr item = nullptr;
tqcq::Item::Type item_type = tqcq::Item::Type::kInt32;
if (context->INT32()) { if (context->INT32()) {
item = std::make_shared<tqcq::Item>(tqcq::Item::Type::kInt32, context->ID()->getText(), idx); item_type = tqcq::Item::Type::kInt32;
} else if (context->STRING()){ } else if (context->STRING()){
item = std::make_shared<tqcq::Item>(tqcq::Item::Type::kString, context->ID()->getText(), idx); item_type = tqcq::Item::Type::kString;
} else if (context->UINT32()) {
item_type = tqcq::Item::Type::kUInt32;
} else { } else {
assert(false); assert(false);
} }
item = std::make_shared<tqcq::Item>(item_type, context->ID()->getText(), idx);
items.push_back(std::move(item));
}
void exitMessageMessageItemStatement(ProtoParser::MessageMessageItemStatementContext *context) override
{
int idx = std::stoi(context->NUMBER()->getText());
auto message_type = context->ID(0)->getText();
auto message_name = context->ID(1)->getText();
auto item = std::make_shared<tqcq::Item>(tqcq::Item::Type::kMessage, message_name, idx);
items.push_back(std::move(item)); items.push_back(std::move(item));
} }
@ -106,9 +119,9 @@ main(int argc, char *argv[])
Listener listener; Listener listener;
Listener listener2; Listener listener2;
antlr4::tree::ParseTreeWalker::DEFAULT.walk(&listener, parser.program()); antlr4::tree::ParseTreeWalker::DEFAULT.walk(&listener, parser.program());
// for (const auto& item : listener.message_map) { for (const auto& item : listener.message_map) {
// std::cout << item.second->ToString() << std::endl; std::cout << item.second->ToString() << std::endl;
// } }
// for (const auto& service : listener.services) { // for (const auto& service : listener.services) {
// std::cout << service->ToString() << std::endl; // std::cout << service->ToString() << std::endl;

View File

@ -1,6 +1,7 @@
lexer grammar ProtoLexer; lexer grammar ProtoLexer;
INT32: 'int32'; INT32: 'int32';
UINT32: 'uint32';
STRING: 'string'; STRING: 'string';
VERSION2: 'proto2'; VERSION2: 'proto2';

View File

@ -6,9 +6,15 @@ program: syntaxStatement baseStatement* EOF;
syntaxStatement: SYNTAX_TYPE '=' '"' (VERSION2 | VERSION3) '"' ';'; syntaxStatement: SYNTAX_TYPE '=' '"' (VERSION2 | VERSION3) '"' ';';
baseStatement: messageStatement | serviceStatement | ';' ; baseStatement: messageStatement | serviceStatement | ';' ;
messageStatement: MESSAGE_TYPE ID '{' (itemStatement | ';')* '}'; messageStatement:
MESSAGE_TYPE ID '{' (
messageBaseItemStatement
| ';'
| messageMessageItemStatement
)* '}';
serviceStatement: SERVICE_TYPE ID '{' (rpcStatement | ';')* '}'; serviceStatement: SERVICE_TYPE ID '{' (rpcStatement | ';')* '}';
itemStatement: (INT32 | STRING) ID '=' NUMBER ';'; messageBaseItemStatement: (INT32 | UINT32 | STRING) ID '=' NUMBER ';';
messageMessageItemStatement: ID ID '=' NUMBER ';';
rpcStatement: RPC_TYPE ID '(' ID ')' 'returns' '(' ID ')' '{' '}'; rpcStatement: RPC_TYPE ID '(' ID ')' 'returns' '(' ID ')' '{' '}';

View File

@ -15,6 +15,7 @@ public:
enum class Type { enum class Type {
kMessage, kMessage,
kInt32, kInt32,
kUInt32,
kString kString
}; };
static std::string ToString(Type type); static std::string ToString(Type type);

View File

@ -38,6 +38,12 @@ std::string
CServiceServerPlugin::GenerateHeaderStructAPIStart(Service::Ptr service) CServiceServerPlugin::GenerateHeaderStructAPIStart(Service::Ptr service)
{ {
std::stringstream ss; std::stringstream ss;
for (const auto& rpc : service->rpcs()) {
auto crpc = CRPCServer::Create(rpc);
ss << crpc->GenerateAPITypedef(service->id());
}
ss << std::endl;
ss << "typedef struct " ss << "typedef struct "
<< "{" << std::endl; << "{" << std::endl;
return ss.str(); return ss.str();

View File

@ -5,6 +5,7 @@
#include "c_item.h" #include "c_item.h"
#include "assert.h" #include "assert.h"
#include "c_item_int_32.h" #include "c_item_int_32.h"
#include "c_item_uint_32.h"
#include "c_item_string.h" #include "c_item_string.h"
namespace tqcq { namespace tqcq {
@ -15,6 +16,8 @@ CItem::Create(Item::Ptr item)
switch (item->type()) { switch (item->type()) {
case Item::Type::kInt32: case Item::Type::kInt32:
return std::make_shared<CItemInt32>(item); return std::make_shared<CItemInt32>(item);
case Item::Type::kUInt32:
return std::make_shared<CItemUInt32>(item);
case Item::Type::kString: case Item::Type::kString:
return std::make_shared<CItemString>(item); return std::make_shared<CItemString>(item);
default: default:

View File

@ -0,0 +1,7 @@
//
// Created by tqcq on 2023/12/4.
//
#include "c_item_message.h"
namespace tqcq {}// namespace tqcq

View File

@ -0,0 +1,14 @@
//
// Created by tqcq on 2023/12/4.
//
#ifndef CRPC_SRC_PLUGINS_C_ITEMS_C_ITEM_MESSAGE_H_
#define CRPC_SRC_PLUGINS_C_ITEMS_C_ITEM_MESSAGE_H_
namespace tqcq {
class CItemMessage {};
}// namespace tqcq
#endif//CRPC_SRC_PLUGINS_C_ITEMS_C_ITEM_MESSAGE_H_

View File

@ -41,9 +41,9 @@ std::string
CItemString::GenerateSetterDeclaration(std::string message_id) const CItemString::GenerateSetterDeclaration(std::string message_id) const
{ {
std::stringstream ss; std::stringstream ss;
ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const char* " << id() << ");"; ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const void* " << id() << ");";
ss << std::endl; ss << std::endl;
ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const char* " << id() << ", int32_t " << id() << "_len);"; ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const void* " << id() << ", int32_t " << id() << "_len);";
ss << std::endl; ss << std::endl;
return ss.str(); return ss.str();
} }
@ -53,7 +53,7 @@ CItemString::GenerateSetterDefinition(std::string message_id) const
{ {
std::stringstream ss; std::stringstream ss;
// set_xxx_data // set_xxx_data
ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const char* " << id() << ", int32_t " << id() << "_len) {" << std::endl; ss << "void " << message_id << "_set_" << id() << "_data(" << message_id << "* message, const void* " << id() << ", int32_t " << id() << "_len) {" << std::endl;
ss << " char* new_" << id() << " = (char*)malloc(" << id() << "_len + 1);" << std::endl; ss << " char* new_" << id() << " = (char*)malloc(" << id() << "_len + 1);" << std::endl;
ss << " if (new_" << id() << " == NULL) { return; }" << std::endl; ss << " if (new_" << id() << " == NULL) { return; }" << std::endl;
ss << " memcpy(new_" << id() << ", " << id() << ", " << id() << "_len);" << std::endl; ss << " memcpy(new_" << id() << ", " << id() << ", " << id() << "_len);" << std::endl;
@ -67,7 +67,7 @@ CItemString::GenerateSetterDefinition(std::string message_id) const
ss << std::endl; ss << std::endl;
// set_xxx_string // set_xxx_string
ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const char* " << id() << ") {" << std::endl; ss << "void " << message_id << "_set_" << id() << "_string(" << message_id << "* message, const void* " << id() << ") {" << std::endl;
ss << " int32_t " << id() << "_len = strlen(" << id() << ");" << std::endl; ss << " int32_t " << id() << "_len = strlen(" << id() << ");" << std::endl;
ss << " " << message_id << "_set_" << id() << "_data(message, " << id() << ", " << id() << "_len);" << std::endl; ss << " " << message_id << "_set_" << id() << "_data(message, " << id() << ", " << id() << "_len);" << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
@ -80,8 +80,18 @@ std::string
CItemString::GenerateGetterDefinition(std::string message_id) const CItemString::GenerateGetterDefinition(std::string message_id) const
{ {
std::stringstream ss; std::stringstream ss;
ss << "const char* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message) {" << std::endl; ss << "void* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message) {" << std::endl;
ss << " return message->" << id() << ";" << std::endl; ss << " return (void*)message->" << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
ss << "const char* " << message_id << "_get_" << id() << "_string(const " << message_id << "* message) {" << std::endl;
ss << " return (const char*)message->" << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
ss << "int32_t " << message_id << "_get_" << id() << "_len(const " << message_id << "* message) {" << std::endl;
ss << " return message->" << id() << "_len;" << std::endl;
ss << "}" << std::endl; ss << "}" << std::endl;
return ss.str(); return ss.str();
@ -91,9 +101,11 @@ std::string
CItemString::GenerateGetterDeclaration(std::string message_id) const CItemString::GenerateGetterDeclaration(std::string message_id) const
{ {
std::stringstream ss; std::stringstream ss;
ss << "const char* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message);"; ss << "void* " << message_id << "_get_" << id() << "_data(const " << message_id << "* message);";
ss << std::endl; ss << std::endl;
ss << "const char* " << message_id << "_get_" << id() << "_len(const " << message_id << "* message);"; ss << "const char* " << message_id << "_get_" << id() << "_string(const " << message_id << "* message);";
ss << std::endl;
ss << "int32_t " << message_id << "_get_" << id() << "_len(const " << message_id << "* message);";
return ss.str(); return ss.str();
} }

View File

@ -0,0 +1,78 @@
//
// Created by tqcq on 2023/12/2.
//
#include "c_item_uint_32.h"
#include <sstream>
namespace tqcq {
CItemUInt32::CItemUInt32(Item::Ptr item) : CItem(item) {}
std::string
CItemUInt32::GenerateStructDeclareCodeBlock(std::string message_id) const
{
return " uint32_t " + id() + ";";
}
std::string
CItemUInt32::GenerateInitCodeBlock(std::string message_id) const
{
std::stringstream ss;
ss << " " << "message->" << id() << " = 0;";
return ss.str();
}
std::string
CItemUInt32::GenerateDestroyCodeBlock(std::string message_id) const
{
return " // do nothing for " + id() + " destroy";
}
std::string
CItemUInt32::GenerateSetterDeclaration(std::string message_id) const
{
return "void " + message_id + "_set_" + id() + "(" + message_id + "* message, uint32_t " + id() + ");";
}
std::string
CItemUInt32::GenerateSetterDefinition(std::string message_id) const
{
std::stringstream ss;
ss << "void " << message_id << "_set_" << id() << "(" << message_id << "* message, uint32_t " << id() << ") {" << std::endl;
ss << " message->" << id() << " = " << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
return ss.str();
}
std::string
CItemUInt32::GenerateGetterDefinition(std::string message_id) const
{
std::stringstream ss;
ss << "uint32_t " << message_id << "_get_" << id() << "(const " << message_id << "* message) {" << std::endl;
ss << " return message->" << id() << ";" << std::endl;
ss << "}" << std::endl;
ss << std::endl;
return ss.str();
}
std::string
CItemUInt32::GenerateGetterDeclaration(std::string message_id) const
{
std::stringstream ss;
ss << "uint32_t " << message_id << "_get_" << id() << "(const " << message_id << "* message);";
return ss.str();
}
std::string
CItemUInt32::GenerateSerializeCodeBlock(std::string message_id) const
{
return " // implement me for " + message_id + "_" + id() + " serialize";
}
std::string
CItemUInt32::GenerateDeserializeCodeBlock(std::string message_id) const
{
return " // implement me for " + id() + " deserialize";
}
}// namespace tqcq

View File

@ -0,0 +1,35 @@
//
// Created by tqcq on 2023/12/2.
//
#ifndef CRPC_SRC_PLUGINS_C_GENERATORS_C_ITEM_UINT_32_H_
#define CRPC_SRC_PLUGINS_C_GENERATORS_C_ITEM_UINT_32_H_
#include "c_item.h"
namespace tqcq {
class CItemUInt32 : public CItem {
public:
using Ptr = std::shared_ptr<CItemUInt32>;
CItemUInt32(Item::Ptr item);
~CItemUInt32() override = default;
std::string GenerateStructDeclareCodeBlock(std::string message_id) const override;
std::string GenerateInitCodeBlock(std::string message_id) const override;
std::string GenerateDestroyCodeBlock(std::string message_id) const override;
std::string GenerateSetterDeclaration(std::string message_id) const override;
std::string GenerateSetterDefinition(std::string message_id) const override;
std::string GenerateGetterDefinition(std::string message_id) const override;
std::string GenerateGetterDeclaration(std::string message_id) const override;
std::string GenerateSerializeCodeBlock(std::string message_id) const override;
std::string GenerateDeserializeCodeBlock(std::string message_id) const override;
};
}// namespace tqcq
#endif//CRPC_SRC_PLUGINS_C_GENERATORS_C_ITEM_UINT_32_H_

View File

@ -110,4 +110,13 @@ CRPCServer::GenerateStructAPIWrapper(std::string service_id) const
return ss.str(); return ss.str();
} }
std::string
CRPCServer::GenerateAPITypedef(std::string service_id) const
{
std::stringstream ss;
// ss << "typedef int32_t (*" << service_id << "_API_"<< id() << "_Type)(const " << request_id() << " *request, " << response_id() << " *response);\n";
ss << "int32_t RPCSM_" << id() << "_API_Impl" << "(const " << request_id() << " *request, " << response_id() << " *response);\n";;
return ss.str();
}
}// namespace tqcq }// namespace tqcq

View File

@ -20,6 +20,7 @@ public:
std::string GenerateStructAPIWrapper(std::string service_id) const override; std::string GenerateStructAPIWrapper(std::string service_id) const override;
std::string GenerateStructDeclareCodeBlock(std::string service_id) const override; std::string GenerateStructDeclareCodeBlock(std::string service_id) const override;
std::string GenerateStructAPIDeclareCodeBlock(std::string service_id) const override; std::string GenerateStructAPIDeclareCodeBlock(std::string service_id) const override;
std::string GenerateAPITypedef(std::string service_id) const;
}; };
}// namespace tqcq }// namespace tqcq

View File

@ -2,6 +2,7 @@ syntax = "proto3";
message EmptyRequest { message EmptyRequest {
} }
message EmptyResponse { message EmptyResponse {
int32 code = 1; int32 code = 1;
string msg = 2; string msg = 2;