diff options
Diffstat (limited to 'build/tools/bindingGen/main.cs')
-rw-r--r-- | build/tools/bindingGen/main.cs | 87 |
1 files changed, 67 insertions, 20 deletions
diff --git a/build/tools/bindingGen/main.cs b/build/tools/bindingGen/main.cs index 7f99da4..f58b846 100644 --- a/build/tools/bindingGen/main.cs +++ b/build/tools/bindingGen/main.cs @@ -20,6 +20,7 @@ namespace bindingGen static string output = @"#include ""../{0}.h"" using namespace std; +using namespace Luax; namespace {1} {{ @@ -78,14 +79,14 @@ namespace {1} return ""; string register_methods = ""; register_methods += "\t\t\tLUAX_REGISTER_METHODS(state,\n"; - int maxlen = 0; - foreach(var m in methods) + int maxlen = 0; + foreach (var m in methods) { string method = m.ToString(); if (method.Count() > maxlen) - maxlen = method.Count(); + maxlen = method.Count(); } - for(int i = 0; i < methods.Count; ++i) + for (int i = 0; i < methods.Count; ++i) { Match m = methods[i]; string method = m.ToString(); @@ -107,12 +108,12 @@ namespace {1} if (mc.Count == 0) return ""; - string methods = ""; + string methods = ""; - foreach(var m in mc) + foreach (var m in mc) { string name = m.ToString(); - if(name != "_New") + if (name != "_New") { methods += String.Format(method, cname, name.Substring(1, name.Count() - 1), cname.ToLower()); } @@ -125,16 +126,36 @@ namespace {1} return methods; } + static int IndexOfChar(string str, char c, int n) + { + int j = 0; + for (int i = 0; i < str.Count(); ++i) + { + char ch = str[i]; + if (ch == c) + { + if (j == n) + return i; + ++j; + } + } + return -1; + } + static string make_register_enum(MatchCollection mc, string src) { if (mc.Count == 0) return ""; string reg_enum_l = @"(?<=enum\s*"; - string reg_enum_r = @"[\s\n]*\{((?!\})[\s\S])*)[A-Z_]+(?=\s*)"; + string reg_enum_r = @"[\s\n]*\{((?!\})[\s\S])*\s)[A-Z_0-9]+(?=[\s\=,]+)"; string enums = ""; - foreach(var m in mc) + // 尝试查找下划线索引号,如果没有,代表这个枚举字符串全部都是key + string reg_under_line_l = @"(?<=LUAX_DECL_ENUM\s*\(\s*"; + string reg_under_line_r = @"\s*,\s*)[0-9]+(?=\s*\))"; + + foreach (var m in mc) { string name = m.ToString(); enums += "\t\t\tLUAX_REGISTER_ENUM(state, "; @@ -142,15 +163,30 @@ namespace {1} MatchCollection values = Regex.Matches(src, reg_enum_l + name + reg_enum_r); string[] enames = new string[values.Count]; string[] keys = new string[values.Count]; + int underline_index = -1; + Match underline = Regex.Match(src, reg_under_line_l + name + reg_under_line_r); + if (underline.Success) + { + underline_index = int.Parse(underline.ToString()); + } int maxEname = 0, maxKey = 0; for (int i = 0; i < values.Count; ++i) { - enames[i] = values[i].ToString(); - keys[i] = enames[i].Substring(enames[i].LastIndexOf('_') + 1, enames[i].Count() - enames[i].LastIndexOf('_') -1); + enames[i] = values[i].ToString(); + if(underline_index != -1) + { + int start = IndexOfChar(enames[i], '_', underline_index) + 1; + keys[i] = enames[i].Substring(start, enames[i].Count() - start); + } + else + { + // 表明全部都是key + keys[i] = enames[i]; + } if (enames[i].Count() > maxEname) maxEname = enames[i].Count(); if (keys[i].Count() > maxKey) maxKey = keys[i].Count(); } - for(int i = 0; i < values.Count; ++i) + for (int i = 0; i < values.Count; ++i) { enums += "\t\t\t\t{ "; enums += ('"' + keys[i] + "\",").PadRight(maxKey + 4); @@ -183,17 +219,18 @@ namespace {1} return; string reg_class = @"(?<=Portable\<)[0-9a-zA-Z]+(?=\>)"; + string reg_abclass = @"(?<=LUAX_DECL_ABSTRACT_FACTORY\s*\(\s*)[0-9a-zA-Z]+(?=\s*\))"; string reg_methods = @"(?<=LUAX_DECL_METHOD\()[0-9a-zA-Z_]+(?=\))"; - string reg_enums = @"(?<=LUAX_DECL_ENUM\()[0-9a-zA-Z_]+(?=\))"; + string reg_enums = @"(?<=LUAX_DECL_ENUM\()[0-9a-zA-Z_]+(?=\s*[\),]+)"; string reg_namespace = @"(?<=namespace\s)[0-9a-zA-Z]+(?=[\s\n]*\{)"; - if(!Directory.Exists(dir + "/binding")) + if (!Directory.Exists(dir + "/binding")) { Directory.CreateDirectory(dir + "/binding"); } string[] files = Directory.GetFiles(dir); - for(int i = 0; i < files.Count(); ++i) + for (int i = 0; i < files.Count(); ++i) { string file = files[i]; if (!File.Exists(file)) @@ -204,14 +241,24 @@ namespace {1} if (File.Exists(bindingFile)) continue; string code = File.ReadAllText(file); - // + // 工厂和单例 Match m = Regex.Match(code, reg_class); + string className = ""; + if (m.Success) + { + className = m.ToString(); + } if (!m.Success) - continue; - string className = m.ToString(); + { + // 抽象类,不会继承Portable模板 + Match abstract_m = Regex.Match(code, reg_abclass); + if (!abstract_m.Success) + continue; + className = abstract_m.ToString(); + } MatchCollection mc = Regex.Matches(code, reg_namespace); // 应该两个名称空间 - if(mc.Count != 2) + if (mc.Count != 2) { Console.WriteLine("Error: 源文件没有两个名称空间 " + file); continue; @@ -227,7 +274,7 @@ namespace {1} mc = Regex.Matches(code, reg_methods); content += make_impl_methods(mc, className); string binding = String.Format(output, name, namespace1, namespace2, content); - Console.WriteLine("输出: "+bindingFile); + Console.WriteLine("输出: " + bindingFile); File.WriteAllText(bindingFile, binding); } } |