1
0

LSL2CSConverter.cs 15 KB

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