LSL2CSConverter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System.Collections.Generic;
  29. using System.Text.RegularExpressions;
  30. namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL
  31. {
  32. public class LSL2CSConverter
  33. {
  34. //private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
  35. private Dictionary<string, string> dataTypes = new Dictionary<string, string>();
  36. private Dictionary<string, string> quotes = new Dictionary<string, string>();
  37. public LSL2CSConverter()
  38. {
  39. // Only the types we need to convert
  40. dataTypes.Add("void", "void");
  41. dataTypes.Add("integer", "int");
  42. dataTypes.Add("float", "double");
  43. dataTypes.Add("string", "string");
  44. dataTypes.Add("key", "string");
  45. dataTypes.Add("vector", "LSL_Types.Vector3");
  46. dataTypes.Add("rotation", "LSL_Types.Quaternion");
  47. dataTypes.Add("list", "list");
  48. dataTypes.Add("null", "null");
  49. }
  50. public string Convert(string Script)
  51. {
  52. string Return = "";
  53. Script = " \r\n" + Script;
  54. //
  55. // Prepare script for processing
  56. //
  57. // Clean up linebreaks
  58. Script = Regex.Replace(Script, @"\r\n", "\n");
  59. Script = Regex.Replace(Script, @"\n", "\r\n");
  60. // QUOTE REPLACEMENT
  61. // temporarily replace quotes so we can work our magic on the script without
  62. // always considering if we are inside our outside ""'s
  63. string _Script = "";
  64. string C;
  65. bool in_quote = false;
  66. bool quote_replaced = false;
  67. string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
  68. string quote = "";
  69. bool last_was_escape = false;
  70. int quote_replaced_count = 0;
  71. for (int p = 0; p < Script.Length; p++)
  72. {
  73. C = Script.Substring(p, 1);
  74. while (true)
  75. {
  76. // found " and last was not \ so this is not an escaped \"
  77. if (C == "\"" && last_was_escape == false)
  78. {
  79. // Toggle inside/outside quote
  80. in_quote = !in_quote;
  81. if (in_quote)
  82. {
  83. quote_replaced_count++;
  84. }
  85. else
  86. {
  87. if (quote == "")
  88. {
  89. // We didn't replace quote, probably because of empty string?
  90. _Script += quote_replacement_string +
  91. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
  92. }
  93. // We just left a quote
  94. quotes.Add(
  95. quote_replacement_string +
  96. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
  97. quote = "";
  98. }
  99. break;
  100. }
  101. if (!in_quote)
  102. {
  103. // We are not inside a quote
  104. quote_replaced = false;
  105. }
  106. else
  107. {
  108. // We are inside a quote
  109. if (!quote_replaced)
  110. {
  111. // Replace quote
  112. _Script += quote_replacement_string +
  113. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
  114. quote_replaced = true;
  115. }
  116. quote += C;
  117. break;
  118. }
  119. _Script += C;
  120. break;
  121. }
  122. last_was_escape = false;
  123. if (C == @"\")
  124. {
  125. last_was_escape = true;
  126. }
  127. }
  128. Script = _Script;
  129. //
  130. // END OF QUOTE REPLACEMENT
  131. //
  132. //
  133. // PROCESS STATES
  134. // Remove state definitions and add state names to start of each event within state
  135. //
  136. int ilevel = 0;
  137. int lastlevel = 0;
  138. string ret = "";
  139. string cache = "";
  140. bool in_state = false;
  141. string current_statename = "";
  142. for (int p = 0; p < Script.Length; p++)
  143. {
  144. C = Script.Substring(p, 1);
  145. while (true)
  146. {
  147. // inc / dec level
  148. if (C == @"{")
  149. ilevel++;
  150. if (C == @"}")
  151. ilevel--;
  152. if (ilevel < 0)
  153. ilevel = 0;
  154. cache += C;
  155. // if level == 0, add to return
  156. if (ilevel == 1 && lastlevel == 0)
  157. {
  158. // 0 => 1: Get last
  159. Match m =
  160. Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{",
  161. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  162. in_state = false;
  163. if (m.Success)
  164. {
  165. // Go back to level 0, this is not a state
  166. in_state = true;
  167. current_statename = m.Groups[1].Captures[0].Value;
  168. //Console.WriteLine("Current statename: " + current_statename);
  169. cache =
  170. Regex.Replace(cache,
  171. @"(?<s1>(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?<s2>[^a-zA-Z_\(\)]*){",
  172. "${s1}${s2}",
  173. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  174. }
  175. ret += cache;
  176. cache = "";
  177. }
  178. if (ilevel == 0 && lastlevel == 1)
  179. {
  180. // 1 => 0: Remove last }
  181. if (in_state == true)
  182. {
  183. cache = cache.Remove(cache.Length - 1, 1);
  184. //cache = Regex.Replace(cache, "}$", "", RegexOptions.Multiline | RegexOptions.Singleline);
  185. //Replace function names
  186. // void dataserver(key query_id, string data) {
  187. //cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  188. //Console.WriteLine("Replacing using statename: " + current_statename);
  189. cache =
  190. Regex.Replace(cache,
  191. @"^(\s*)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)",
  192. @"$1public " + current_statename + "_event_$2",
  193. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  194. }
  195. ret += cache;
  196. cache = "";
  197. in_state = true;
  198. current_statename = "";
  199. }
  200. break;
  201. }
  202. lastlevel = ilevel;
  203. }
  204. ret += cache;
  205. cache = "";
  206. Script = ret;
  207. ret = "";
  208. foreach (string key in dataTypes.Keys)
  209. {
  210. string val;
  211. dataTypes.TryGetValue(key, out val);
  212. // Replace CAST - (integer) with (int)
  213. Script =
  214. Regex.Replace(Script, @"\(" + key + @"\)", @"(" + val + ")",
  215. RegexOptions.Compiled | RegexOptions.Multiline);
  216. // Replace return types and function variables - integer a() and f(integer a, integer a)
  217. Script =
  218. Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)", @"$1$2" + val + "$3",
  219. RegexOptions.Compiled | RegexOptions.Multiline);
  220. }
  221. // Add "void" in front of functions that needs it
  222. Script =
  223. Regex.Replace(Script,
  224. @"^(\s*public\s+)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)",
  225. @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  226. // Replace <x,y,z> and <x,y,z,r>
  227. Script =
  228. Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Quaternion($1)",
  229. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  230. Script =
  231. Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Vector3($1)",
  232. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  233. // Replace List []'s
  234. Script =
  235. Regex.Replace(Script, @"\[([^\]]*)\]", @"List.Parse($1)",
  236. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  237. // Replace (string) to .ToString() //
  238. Script =
  239. Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.ToString()",
  240. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  241. Script =
  242. Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.Parse($2)",
  243. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  244. // REPLACE BACK QUOTES
  245. foreach (string key in quotes.Keys)
  246. {
  247. string val;
  248. quotes.TryGetValue(key, out val);
  249. Script = Script.Replace(key, "\"" + val + "\"");
  250. }
  251. // Add namespace, class name and inheritance
  252. Return = "" +
  253. "using OpenSim.Region.ScriptEngine.Common;";
  254. //"using System; " +
  255. //"using System.Collections.Generic; " +
  256. //"using System.Text; " +
  257. //"using OpenSim.Region.ScriptEngine.Common; " +
  258. //"using integer = System.Int32; " +
  259. //"using key = System.String; ";
  260. //// Make a Using out of DataTypes
  261. //// Using integer = System.Int32;
  262. //string _val;
  263. //foreach (string key in DataTypes.Keys)
  264. //{
  265. // DataTypes.TryGetValue(key, out _val);
  266. // if (key != _val)
  267. // {
  268. // Return += "using " + key + " = " + _val + "; ";
  269. // }
  270. //}
  271. Return += "" +
  272. "namespace SecondLife { ";
  273. Return += "" +
  274. //"[Serializable] " +
  275. "public class Script : OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass { ";
  276. Return += @"public Script() { } ";
  277. Return += Script;
  278. Return += "} }\r\n";
  279. return Return;
  280. }
  281. }
  282. }