[
https://issues.apache.org/jira/browse/THRIFT-1681?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14009706#comment-14009706
]
ASF GitHub Bot commented on THRIFT-1681:
----------------------------------------
Github user daurnimator commented on a diff in the pull request:
https://github.com/apache/thrift/pull/92#discussion_r13079843
--- Diff: compiler/cpp/src/generate/t_lua_generator.cc ---
@@ -0,0 +1,1226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <sstream>
+#include "t_oop_generator.h"
+#include "platform.h"
+
+using std::ofstream;
+using std::string;
+using std::vector;
+using std::map;
+
+static const string endl = "\n"; // avoid ostream << std::endl flushes
+
+/**
+ * LUA code generator.
+ *
+ */
+class t_lua_generator : public t_oop_generator {
+ public:
+ t_lua_generator(
+ t_program* program,
+ const std::map<std::string, std::string>& parsed_options,
+ const std::string& option_string)
+ : t_oop_generator(program)
+ {
+ std::map<std::string, std::string>::const_iterator iter;
+
+ iter = parsed_options.find("omit_requires");
+ gen_requires_ = (iter == parsed_options.end());
+
+ out_dir_base_ = "gen-lua";
+ }
+
+ /**
+ * Init and close methods
+ */
+ void init_generator();
+ void close_generator();
+
+ /**
+ * Program-level generation functions
+ */
+ void generate_typedef (t_typedef* ttypedef);
+ void generate_enum (t_enum* tenum);
+ void generate_const (t_const* tconst);
+ void generate_struct (t_struct* tstruct);
+ void generate_xception (t_struct* txception);
+ void generate_service (t_service* tservice);
+
+ std::string render_const_value(t_type* type, t_const_value* value);
+
+ private:
+
+ /**
+ * True iff we should generate lua require statements.
+ */
+ bool gen_requires_;
+
+ /**
+ * Struct-level generation functions
+ */
+ void generate_lua_struct_definition(
+ std::ofstream& out, t_struct* tstruct, bool is_xception=false);
+ void generate_lua_struct_reader(std::ofstream& out, t_struct* tstruct);
+ void generate_lua_struct_writer(std::ofstream& out, t_struct* tstruct);
+
+ /**
+ * Service-level generation functions
+ */
+ void generate_service_client (std::ofstream& out, t_service*
tservice);
+ void generate_service_interface (std::ofstream& out, t_service*
tservice);
+ void generate_service_processor (std::ofstream& out, t_service*
tservice);
+ void generate_process_function (std::ofstream& out, t_service* tservice,
+ t_function* tfunction);
+ void generate_service_helpers (ofstream &out, t_service* tservice);
+ void generate_function_helpers (ofstream &out, t_function* tfunction);
+
+ /**
+ * Deserialization (Read)
+ */
+ void generate_deserialize_field(
+ std::ofstream &out, t_field *tfield, std::string prefix="");
+
+ void generate_deserialize_struct(
+ std::ofstream &out, t_struct *tstruct, std::string prefix="");
+
+ void generate_deserialize_container(
+ std::ofstream &out, t_type *ttype, std::string prefix="");
+
+ void generate_deserialize_set_element(
+ std::ofstream &out, t_set *tset, std::string prefix="");
+
+ void generate_deserialize_map_element(
+ std::ofstream &out, t_map *tmap, std::string prefix="");
+
+ void generate_deserialize_list_element(
+ std::ofstream &out, t_list *tlist, std::string prefix="");
+
+ /**
+ * Serialization (Write)
+ */
+ void generate_serialize_field(
+ std::ofstream &out, t_field *tfield, std::string prefix="");
+
+ void generate_serialize_struct(
+ std::ofstream &out, t_struct *tstruct, std::string prefix="");
+
+ void generate_serialize_container(
+ std::ofstream &out, t_type *ttype, std::string prefix="");
+
+ void generate_serialize_map_element(
+ std::ofstream &out, t_map *tmap, std::string kiter, std::string viter);
+
+ void generate_serialize_set_element(
+ std::ofstream &out, t_set *tmap, std::string iter);
+
+ void generate_serialize_list_element(
+ std::ofstream &out, t_list *tlist, std::string iter);
+
+ /**
+ * Helper rendering functions
+ */
+ std::string lua_includes();
+ std::string function_signature(t_function* tfunction, std::string
prefix="");
+ std::string argument_list(t_struct* tstruct, std::string prefix="");
+ std::string type_to_enum(t_type* ttype);
+ static std::string get_namespace(const t_program* program);
+
+ std::string autogen_comment() {
+ return
+ std::string("--\n") +
+ "-- Autogenerated by Thrift\n" +
+ "--\n" +
+ "-- DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE
DOING\n" +
+ "-- @""generated\n" +
+ "--\n";
+ }
+
+ /**
+ * File streams
+ */
+ std::ofstream f_types_;
+ std::ofstream f_consts_;
+ std::ofstream f_service_;
+};
+
+
+/**
+ * Init and close methods
+ */
+void t_lua_generator::init_generator() {
+ // Make output directory
+ string outdir = get_out_dir();
+ MKDIR(outdir.c_str());
+
+ // Make output files
+ string cur_namespace = get_namespace(program_);
+ string f_consts_name = outdir + cur_namespace + "constants.lua";
+ f_consts_.open(f_consts_name.c_str());
+ string f_types_name = outdir + cur_namespace + "ttypes.lua";
+ f_types_.open(f_types_name.c_str());
+
+ // Add headers
+ f_consts_ << autogen_comment() << lua_includes();
+ f_types_ << autogen_comment() << lua_includes();
+ if (gen_requires_) {
+ f_types_ << endl << "require '" << cur_namespace << "constants'";
+ }
+}
+
+void t_lua_generator::close_generator() {
+ // Close types file
+ f_types_.close();
+ f_consts_.close();
+}
+
+/**
+ * Generate a typedef (essentially a constant)
+ */
+void t_lua_generator::generate_typedef(t_typedef* ttypedef) {
+ f_types_
+ << endl << endl << indent()
+ << ttypedef->get_symbolic() << " = "
+ << ttypedef->get_type()->get_name();
+}
+
+/**
+ * Generates code for an enumerated type (table)
+ */
+void t_lua_generator::generate_enum(t_enum* tenum) {
+ f_types_ << endl << endl << tenum->get_name() << " = {" << endl;
+
+ vector<t_enum_value*> constants = tenum->get_constants();
+ vector<t_enum_value*>::iterator c_iter;
+ for (c_iter = constants.begin(); c_iter != constants.end();) {
+ int32_t value = (*c_iter)->get_value();
+
+ f_types_ << " " << (*c_iter)->get_name() << " = " << value;
+ ++c_iter;
+ if (c_iter != constants.end()) {
+ f_types_ << ",";
+ }
+ f_types_ << endl;
+ }
+ f_types_ << "}";
+}
+
+/**
+ * Generate a constant (non-local) value
+ */
+void t_lua_generator::generate_const(t_const* tconst) {
+ t_type* type = tconst->get_type();
+ string name = tconst->get_name();
+ t_const_value* value = tconst->get_value();
+
+ f_consts_ << endl << endl << name << " = ";
+ f_consts_ << render_const_value(type, value);
+}
+
+/**
+ * Prints the value of a constant with the given type.
+ */
+string t_lua_generator::render_const_value(
+ t_type* type, t_const_value* value) {
+ std::ostringstream out;
+
+ type = get_true_type(type);
+ if (type->is_base_type()) {
+ t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
+ switch (tbase) {
+ case t_base_type::TYPE_STRING:
+ out << "'" << value->get_string() << "'";
--- End diff --
escaping?
> Add Lua Support
> ---------------
>
> Key: THRIFT-1681
> URL: https://issues.apache.org/jira/browse/THRIFT-1681
> Project: Thrift
> Issue Type: Sub-task
> Components: Compiler (General)
> Reporter: Daurn Imator
> Assignee: Roger Meier
> Labels: language, lua
> Fix For: 0.9.2
>
>
> I'd love to see a lua library for Thift.
--
This message was sent by Atlassian JIRA
(v6.2#6252)