LSL2CSConverter.cs 13 KB

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