1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Google LLC nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #include "google/protobuf/descriptor.pb.h"
27 #include "absl/strings/str_replace.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/strings/substitute.h"
30 #include "google/protobuf/compiler/code_generator.h"
31 #include "google/protobuf/compiler/plugin.h"
32 #include "google/protobuf/descriptor.h"
33 #include "google/protobuf/io/printer.h"
34
35 namespace protoc = ::google::protobuf::compiler;
36 namespace protobuf = ::google::protobuf;
37
38 class LuaGenerator : public protoc::CodeGenerator {
39 bool Generate(const protobuf::FileDescriptor* file,
40 const std::string& parameter, protoc::GeneratorContext* context,
41 std::string* error) const override;
42 };
43
StripExtension(absl::string_view fname)44 static std::string StripExtension(absl::string_view fname) {
45 size_t lastdot = fname.find_last_of('.');
46 if (lastdot == std::string::npos) {
47 return std::string(fname);
48 }
49 return std::string(fname.substr(0, lastdot));
50 }
51
Filename(const protobuf::FileDescriptor * file)52 static std::string Filename(const protobuf::FileDescriptor* file) {
53 return StripExtension(file->name()) + "_pb.lua";
54 }
55
ModuleName(const protobuf::FileDescriptor * file)56 static std::string ModuleName(const protobuf::FileDescriptor* file) {
57 std::string ret = StripExtension(file->name()) + "_pb";
58 return absl::StrReplaceAll(ret, {{"/", "."}});
59 }
60
PrintHexDigit(char digit,protobuf::io::Printer * printer)61 static void PrintHexDigit(char digit, protobuf::io::Printer* printer) {
62 char text;
63 if (digit < 10) {
64 text = '0' + digit;
65 } else {
66 text = 'A' + (digit - 10);
67 }
68 printer->WriteRaw(&text, 1);
69 }
70
PrintString(int max_cols,absl::string_view * str,protobuf::io::Printer * printer)71 static void PrintString(int max_cols, absl::string_view* str,
72 protobuf::io::Printer* printer) {
73 printer->Print("\'");
74 while (max_cols > 0 && !str->empty()) {
75 char ch = (*str)[0];
76 if (ch == '\\') {
77 printer->PrintRaw("\\\\");
78 max_cols--;
79 } else if (ch == '\'') {
80 printer->PrintRaw("\\'");
81 max_cols--;
82 } else if (isprint(ch)) {
83 printer->WriteRaw(&ch, 1);
84 max_cols--;
85 } else {
86 unsigned char byte = ch;
87 printer->PrintRaw("\\x");
88 PrintHexDigit(byte >> 4, printer);
89 PrintHexDigit(byte & 15, printer);
90 max_cols -= 4;
91 }
92 str->remove_prefix(1);
93 }
94 printer->Print("\'");
95 }
96
Generate(const protobuf::FileDescriptor * file,const std::string &,protoc::GeneratorContext * context,std::string *) const97 bool LuaGenerator::Generate(const protobuf::FileDescriptor* file,
98 const std::string& /* parameter */,
99 protoc::GeneratorContext* context,
100 std::string* /* error */) const {
101 std::string filename = Filename(file);
102 protobuf::io::ZeroCopyOutputStream* out = context->Open(filename);
103 protobuf::io::Printer printer(out, '$');
104
105 for (int i = 0; i < file->dependency_count(); i++) {
106 const protobuf::FileDescriptor* dep = file->dependency(i);
107 printer.Print("require('$name$')\n", "name", ModuleName(dep));
108 }
109
110 printer.Print("local upb = require('upb')\n");
111
112 protobuf::FileDescriptorProto file_proto;
113 file->CopyTo(&file_proto);
114 std::string file_data;
115 file_proto.SerializeToString(&file_data);
116
117 printer.Print("local descriptor = table.concat({\n");
118 absl::string_view data(file_data);
119 while (!data.empty()) {
120 printer.Print(" ");
121 PrintString(72, &data, &printer);
122 printer.Print(",\n");
123 }
124 printer.Print("})\n");
125
126 printer.Print("return upb._generated_module(descriptor)\n");
127
128 return true;
129 }
130
main(int argc,char ** argv)131 int main(int argc, char** argv) {
132 LuaGenerator generator;
133 return google::protobuf::compiler::PluginMain(argc, argv, &generator);
134 }
135