2023-12-02 00:32:54 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <ostream>
|
|
|
|
#include "antlr4-runtime.h"
|
|
|
|
#include "ProtoParserBaseListener.h"
|
|
|
|
#include "ProtoLexer.h"
|
|
|
|
#include "ProtoParser.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include "service.h"
|
|
|
|
#include "plugins/c/c_message_plugin.h"
|
2023-12-02 08:45:42 +08:00
|
|
|
#include "plugins/c/c_service_server_plugin.h"
|
|
|
|
#include "plugins/c/c_service_client_plugin.h"
|
2023-12-02 00:32:54 +08:00
|
|
|
#include <stack>
|
|
|
|
#include <map>
|
2023-12-02 01:56:16 +08:00
|
|
|
#include <unistd.h>
|
2023-12-02 00:32:54 +08:00
|
|
|
|
2023-12-03 11:57:23 +08:00
|
|
|
using namespace tqcq;
|
|
|
|
|
2023-12-02 00:32:54 +08:00
|
|
|
class Listener : public ProtoParserBaseListener {
|
|
|
|
public:
|
|
|
|
void enterMessageStatement(ProtoParser::MessageStatementContext *context) override
|
|
|
|
{
|
|
|
|
message_stack.push(std::make_shared<tqcq::Message>(context->ID()->getText()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void exitMessageStatement(ProtoParser::MessageStatementContext *context) override
|
|
|
|
{
|
|
|
|
message_stack.top()->SetItems(std::move(items));
|
|
|
|
assert(items.empty());
|
|
|
|
if (message_map.find(message_stack.top()->id()) != message_map.end()) {
|
|
|
|
std::cout << "message " << message_stack.top()->id() << " already exists" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
message_map.insert(std::make_pair(message_stack.top()->id(), message_stack.top()));
|
|
|
|
message_stack.pop();
|
|
|
|
}
|
|
|
|
|
2023-12-04 12:27:47 +08:00
|
|
|
void exitMessageBaseItemStatement(ProtoParser::MessageBaseItemStatementContext *context) override
|
2023-12-02 00:32:54 +08:00
|
|
|
{
|
|
|
|
int idx = std::stoi(context->NUMBER()->getText());
|
|
|
|
tqcq::Item::Ptr item = nullptr;
|
2023-12-04 12:27:47 +08:00
|
|
|
tqcq::Item::Type item_type = tqcq::Item::Type::kInt32;
|
2023-12-02 00:32:54 +08:00
|
|
|
if (context->INT32()) {
|
2023-12-04 12:27:47 +08:00
|
|
|
item_type = tqcq::Item::Type::kInt32;
|
2023-12-02 00:32:54 +08:00
|
|
|
} else if (context->STRING()){
|
2023-12-04 12:27:47 +08:00
|
|
|
item_type = tqcq::Item::Type::kString;
|
|
|
|
} else if (context->UINT32()) {
|
|
|
|
item_type = tqcq::Item::Type::kUInt32;
|
2023-12-02 00:32:54 +08:00
|
|
|
} else {
|
|
|
|
assert(false);
|
|
|
|
}
|
2023-12-04 12:27:47 +08:00
|
|
|
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);
|
2023-12-02 00:32:54 +08:00
|
|
|
items.push_back(std::move(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
void exitServiceStatement(ProtoParser::ServiceStatementContext *context) override
|
|
|
|
{
|
|
|
|
std::string id = context->ID()->getText();
|
|
|
|
auto service = std::make_shared<tqcq::Service>(id);
|
|
|
|
service->SetRPCs(std::move(rpcs));
|
|
|
|
assert(rpcs.empty());
|
|
|
|
services.push_back(std::move(service));
|
|
|
|
}
|
|
|
|
|
|
|
|
void exitRpcStatement(ProtoParser::RpcStatementContext *context) override {
|
|
|
|
std::string id = context->ID(0)->getText();
|
|
|
|
std::string request_id = context->ID(1)->getText();
|
|
|
|
std::string response_id = context->ID(2)->getText();
|
|
|
|
auto rpc = std::make_shared<tqcq::RPC>(id, request_id, response_id);
|
|
|
|
rpcs.push_back(std::move(rpc));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string, tqcq::Message::Ptr> message_map;
|
|
|
|
std::vector<tqcq::Item::Ptr> items;
|
|
|
|
std::stack<tqcq::Message::Ptr> message_stack;
|
|
|
|
|
|
|
|
std::vector<tqcq::RPC::Ptr> rpcs;
|
|
|
|
std::vector<tqcq::Service::Ptr> services;
|
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2023-12-03 08:04:44 +08:00
|
|
|
if (argc < 3) {
|
|
|
|
std::cout << "usage: " << argv[0] << " <proto file> <output dir>" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::string proto_file = argv[1];
|
|
|
|
std::string output_dir = argv[2];
|
|
|
|
|
2023-12-02 01:56:16 +08:00
|
|
|
// show current working directory
|
|
|
|
char cwd[1024];
|
|
|
|
if (getcwd(cwd, sizeof(cwd)) != NULL) {
|
|
|
|
std::cout << "Current working dir: " << cwd << std::endl;
|
|
|
|
} else {
|
|
|
|
perror("getcwd() error");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-12-02 00:32:54 +08:00
|
|
|
std::ifstream ifs;
|
2023-12-03 08:04:44 +08:00
|
|
|
ifs.open(proto_file);
|
2023-12-02 00:32:54 +08:00
|
|
|
|
|
|
|
antlr4::ANTLRInputStream input(ifs);
|
|
|
|
ProtoLexer lexer(&input);
|
|
|
|
antlr4::CommonTokenStream tokens(&lexer);
|
|
|
|
ProtoParser parser(&tokens);
|
|
|
|
// parser.removeErrorListeners();
|
|
|
|
// Visitor visitor;
|
|
|
|
// visitor.visit(parser.program());
|
|
|
|
Listener listener;
|
|
|
|
Listener listener2;
|
|
|
|
antlr4::tree::ParseTreeWalker::DEFAULT.walk(&listener, parser.program());
|
2023-12-04 12:27:47 +08:00
|
|
|
for (const auto& item : listener.message_map) {
|
|
|
|
std::cout << item.second->ToString() << std::endl;
|
|
|
|
}
|
2023-12-02 00:32:54 +08:00
|
|
|
|
2023-12-03 08:04:44 +08:00
|
|
|
// for (const auto& service : listener.services) {
|
|
|
|
// std::cout << service->ToString() << std::endl;
|
|
|
|
// }
|
2023-12-02 00:32:54 +08:00
|
|
|
|
2023-12-03 08:04:44 +08:00
|
|
|
tqcq::CMessagePlugin message_plugin(output_dir);
|
2023-12-02 00:32:54 +08:00
|
|
|
message_plugin.Generate(listener.message_map);
|
|
|
|
|
2023-12-03 08:04:44 +08:00
|
|
|
tqcq::CServiceServerPlugin service_server_plugin(output_dir);
|
2023-12-02 08:45:42 +08:00
|
|
|
service_server_plugin.Generate(listener.services);
|
2023-12-03 11:57:23 +08:00
|
|
|
tqcq::CServiceClientPlugin service_client_plugin(output_dir);
|
|
|
|
service_client_plugin.Generate(listener.services);
|
2023-12-02 08:45:42 +08:00
|
|
|
|
2023-12-02 00:32:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|