1
0

LSL2CSConverter.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. using System.Collections.Generic;
  28. using System.Text.RegularExpressions;
  29. namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL
  30. {
  31. public class LSL2CSConverter
  32. {
  33. //private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
  34. private Dictionary<string, string> dataTypes = new Dictionary<string, string>();
  35. private Dictionary<string, string> quotes = new Dictionary<string, string>();
  36. public LSL2CSConverter()
  37. {
  38. // Only the types we need to convert
  39. dataTypes.Add("void", "void");
  40. dataTypes.Add("integer", "int");
  41. dataTypes.Add("float", "double");
  42. dataTypes.Add("string", "string");
  43. dataTypes.Add("key", "string");
  44. dataTypes.Add("vector", "LSL_Types.Vector3");
  45. dataTypes.Add("rotation", "LSL_Types.Quaternion");
  46. dataTypes.Add("list", "list");
  47. dataTypes.Add("null", "null");
  48. }
  49. public string Convert(string Script)
  50. {
  51. string Return = "";
  52. Script = " \r\n" + Script;
  53. //
  54. // Prepare script for processing
  55. //
  56. // Clean up linebreaks
  57. Script = Regex.Replace(Script, @"\r\n", "\n");
  58. Script = Regex.Replace(Script, @"\n", "\r\n");
  59. // QUOTE REPLACEMENT
  60. // temporarily replace quotes so we can work our magic on the script without
  61. // always considering if we are inside our outside ""'s
  62. string _Script = "";
  63. string C;
  64. bool in_quote = false;
  65. bool quote_replaced = false;
  66. string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
  67. string quote = "";
  68. bool last_was_escape = false;
  69. int quote_replaced_count = 0;
  70. for (int p = 0; p < Script.Length; p++)
  71. {
  72. C = Script.Substring(p, 1);
  73. while (true)
  74. {
  75. // found " and last was not \ so this is not an escaped \"
  76. if (C == "\"" && last_was_escape == false)
  77. {
  78. // Toggle inside/outside quote
  79. in_quote = !in_quote;
  80. if (in_quote)
  81. {
  82. quote_replaced_count++;
  83. }
  84. else
  85. {
  86. if (quote == "")
  87. {
  88. // We didn't replace quote, probably because of empty string?
  89. _Script += quote_replacement_string +
  90. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
  91. }
  92. // We just left a quote
  93. quotes.Add(
  94. quote_replacement_string +
  95. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
  96. quote = "";
  97. }
  98. break;
  99. }
  100. if (!in_quote)
  101. {
  102. // We are not inside a quote
  103. quote_replaced = false;
  104. }
  105. else
  106. {
  107. // We are inside a quote
  108. if (!quote_replaced)
  109. {
  110. // Replace quote
  111. _Script += quote_replacement_string +
  112. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
  113. quote_replaced = true;
  114. }
  115. quote += C;
  116. break;
  117. }
  118. _Script += C;
  119. break;
  120. }
  121. last_was_escape = false;
  122. if (C == @"\")
  123. {
  124. last_was_escape = true;
  125. }
  126. }
  127. Script = _Script;
  128. //
  129. // END OF QUOTE REPLACEMENT
  130. //
  131. //
  132. // PROCESS STATES
  133. // Remove state definitions and add state names to start of each event within state
  134. //
  135. int ilevel = 0;
  136. int lastlevel = 0;
  137. string ret = "";
  138. string cache = "";
  139. bool in_state = false;
  140. string current_statename = "";
  141. for (int p = 0; p < Script.Length; p++)
  142. {
  143. C = Script.Substring(p, 1);
  144. while (true)
  145. {
  146. // inc / dec level
  147. if (C == @"{")
  148. ilevel++;
  149. if (C == @"}")
  150. ilevel--;
  151. if (ilevel < 0)
  152. ilevel = 0;
  153. cache += C;
  154. // if level == 0, add to return
  155. if (ilevel == 1 && lastlevel == 0)
  156. {
  157. // 0 => 1: Get last
  158. Match m =
  159. Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{",
  160. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  161. in_state = false;
  162. if (m.Success)
  163. {
  164. // Go back to level 0, this is not a state
  165. in_state = true;
  166. current_statename = m.Groups[1].Captures[0].Value;
  167. //Console.WriteLine("Current statename: " + current_statename);
  168. cache =
  169. Regex.Replace(cache,
  170. @"(?<s1>(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?<s2>[^a-zA-Z_\(\)]*){",
  171. "${s1}${s2}",
  172. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  173. }
  174. ret += cache;
  175. cache = "";
  176. }
  177. if (ilevel == 0 && lastlevel == 1)
  178. {
  179. // 1 => 0: Remove last }
  180. if (in_state == true)
  181. {
  182. cache = cache.Remove(cache.Length - 1, 1);
  183. //cache = Regex.Replace(cache, "}$", "", RegexOptions.Multiline | RegexOptions.Singleline);
  184. //Replace function names
  185. // void dataserver(key query_id, string data) {
  186. //cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  187. //Console.WriteLine("Replacing using statename: " + current_statename);
  188. cache =
  189. Regex.Replace(cache,
  190. @"^(\s*)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)",
  191. @"$1public " + current_statename + "_event_$2",
  192. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  193. }
  194. ret += cache;
  195. cache = "";
  196. in_state = true;
  197. current_statename = "";
  198. }
  199. break;
  200. }
  201. lastlevel = ilevel;
  202. }
  203. ret += cache;
  204. cache = "";
  205. Script = ret;
  206. ret = "";
  207. foreach (string key in dataTypes.Keys)
  208. {
  209. string val;
  210. dataTypes.TryGetValue(key, out val);
  211. // Replace CAST - (integer) with (int)
  212. Script =
  213. Regex.Replace(Script, @"\(" + key + @"\)", @"(" + val + ")",
  214. RegexOptions.Compiled | RegexOptions.Multiline);
  215. // Replace return types and function variables - integer a() and f(integer a, integer a)
  216. Script =
  217. Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)", @"$1$2" + val + "$3",
  218. RegexOptions.Compiled | RegexOptions.Multiline);
  219. }
  220. // Add "void" in front of functions that needs it
  221. Script =
  222. Regex.Replace(Script,
  223. @"^(\s*public\s+)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)",
  224. @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  225. // Replace <x,y,z> and <x,y,z,r>
  226. Script =
  227. Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Quaternion($1)",
  228. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  229. Script =
  230. Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Vector3($1)",
  231. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  232. // Replace List []'s
  233. Script =
  234. Regex.Replace(Script, @"\[([^\]]*)\]", @"List.Parse($1)",
  235. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  236. // Replace (string) to .ToString() //
  237. Script =
  238. Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.ToString()",
  239. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  240. Script =
  241. Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.Parse($2)",
  242. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  243. // REPLACE BACK QUOTES
  244. foreach (string key in quotes.Keys)
  245. {
  246. string val;
  247. quotes.TryGetValue(key, out val);
  248. Script = Script.Replace(key, "\"" + val + "\"");
  249. }
  250. // Add namespace, class name and inheritance
  251. Return = "" +
  252. "using OpenSim.Region.ScriptEngine.Common;";
  253. //"using System; " +
  254. //"using System.Collections.Generic; " +
  255. //"using System.Text; " +
  256. //"using OpenSim.Region.ScriptEngine.Common; " +
  257. //"using integer = System.Int32; " +
  258. //"using key = System.String; ";
  259. //// Make a Using out of DataTypes
  260. //// Using integer = System.Int32;
  261. //string _val;
  262. //foreach (string key in DataTypes.Keys)
  263. //{
  264. // DataTypes.TryGetValue(key, out _val);
  265. // if (key != _val)
  266. // {
  267. // Return += "using " + key + " = " + _val + "; ";
  268. // }
  269. //}
  270. Return += "" +
  271. "namespace SecondLife { ";
  272. Return += "" +
  273. //"[Serializable] " +
  274. "public class Script : OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass { ";
  275. Return += @"public Script() { } ";
  276. Return += Script;
  277. Return += "} }\r\n";
  278. return Return;
  279. }
  280. }
  281. }