1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/descriptor.pb.h"
9 #include "absl/strings/str_replace.h"
10 #include "absl/strings/string_view.h"
11 #include "absl/strings/substitute.h"
12 #include "google/protobuf/compiler/code_generator.h"
13 #include "google/protobuf/compiler/plugin.h"
14 #include "google/protobuf/descriptor.h"
15 #include "google/protobuf/io/printer.h"
16
17 namespace protoc = ::google::protobuf::compiler;
18 namespace protobuf = ::google::protobuf;
19
20 class LuaGenerator : public protoc::CodeGenerator {
21 bool Generate(const protobuf::FileDescriptor* file,
22 const std::string& parameter, protoc::GeneratorContext* context,
23 std::string* error) const override;
24 };
25
StripExtension(absl::string_view fname)26 static std::string StripExtension(absl::string_view fname) {
27 size_t lastdot = fname.find_last_of('.');
28 if (lastdot == std::string::npos) {
29 return std::string(fname);
30 }
31 return std::string(fname.substr(0, lastdot));
32 }
33
Filename(const protobuf::FileDescriptor * file)34 static std::string Filename(const protobuf::FileDescriptor* file) {
35 return StripExtension(file->name()) + "_pb.lua";
36 }
37
ModuleName(const protobuf::FileDescriptor * file)38 static std::string ModuleName(const protobuf::FileDescriptor* file) {
39 std::string ret = StripExtension(file->name()) + "_pb";
40 return absl::StrReplaceAll(ret, {{"/", "."}});
41 }
42
PrintHexDigit(char digit,protobuf::io::Printer * printer)43 static void PrintHexDigit(char digit, protobuf::io::Printer* printer) {
44 char text;
45 if (digit < 10) {
46 text = '0' + digit;
47 } else {
48 text = 'A' + (digit - 10);
49 }
50 printer->WriteRaw(&text, 1);
51 }
52
PrintString(int max_cols,absl::string_view * str,protobuf::io::Printer * printer)53 static void PrintString(int max_cols, absl::string_view* str,
54 protobuf::io::Printer* printer) {
55 printer->Print("\'");
56 while (max_cols > 0 && !str->empty()) {
57 char ch = (*str)[0];
58 if (ch == '\\') {
59 printer->PrintRaw("\\\\");
60 max_cols--;
61 } else if (ch == '\'') {
62 printer->PrintRaw("\\'");
63 max_cols--;
64 } else if (isprint(ch)) {
65 printer->WriteRaw(&ch, 1);
66 max_cols--;
67 } else {
68 unsigned char byte = ch;
69 printer->PrintRaw("\\x");
70 PrintHexDigit(byte >> 4, printer);
71 PrintHexDigit(byte & 15, printer);
72 max_cols -= 4;
73 }
74 str->remove_prefix(1);
75 }
76 printer->Print("\'");
77 }
78
Generate(const protobuf::FileDescriptor * file,const std::string &,protoc::GeneratorContext * context,std::string *) const79 bool LuaGenerator::Generate(const protobuf::FileDescriptor* file,
80 const std::string& /* parameter */,
81 protoc::GeneratorContext* context,
82 std::string* /* error */) const {
83 std::string filename = Filename(file);
84 protobuf::io::ZeroCopyOutputStream* out = context->Open(filename);
85 protobuf::io::Printer printer(out, '$');
86
87 for (int i = 0; i < file->dependency_count(); i++) {
88 const protobuf::FileDescriptor* dep = file->dependency(i);
89 printer.Print("require('$name$')\n", "name", ModuleName(dep));
90 }
91
92 printer.Print("local upb = require('upb')\n");
93
94 protobuf::FileDescriptorProto file_proto;
95 file->CopyTo(&file_proto);
96 std::string file_data;
97 file_proto.SerializeToString(&file_data);
98
99 printer.Print("local descriptor = table.concat({\n");
100 absl::string_view data(file_data);
101 while (!data.empty()) {
102 printer.Print(" ");
103 PrintString(72, &data, &printer);
104 printer.Print(",\n");
105 }
106 printer.Print("})\n");
107
108 printer.Print("return upb._generated_module(descriptor)\n");
109
110 return true;
111 }
112
main(int argc,char ** argv)113 int main(int argc, char** argv) {
114 LuaGenerator generator;
115 return google::protobuf::compiler::PluginMain(argc, argv, &generator);
116 }
117