LSL2CSConverter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // c Style
  40. private Regex cstylecomments = new Regex(@"/\*(.|[\r\n])*?\*/", RegexOptions.Compiled | RegexOptions.Multiline);
  41. // c# one liners
  42. private Regex nonCommentFwsl = new Regex("\"[a-zA-Z0-9.,:/\\n ]+//[^\"+]+([\\\\\\\"+]+)?(\\s+)?[\"+](\\s+)?(;)?", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  43. private Regex conelinecomments = new Regex(@"[^:].?([\/]{2}[^\n]*)|([\n]{1,}[\/]{2}[^\n]*)", RegexOptions.Compiled | RegexOptions.Multiline);
  44. // ([^\"])((?:[a-zA-Z])\.[a-zA-Z].?)([^\"])
  45. // value we're looking for: (?:[a-zA-Z])\.[a-zA-Z]
  46. public LSL2CSConverter()
  47. {
  48. // Only the types we need to convert
  49. dataTypes.Add("void", "void");
  50. dataTypes.Add("integer", "LSL_Types.LSLInteger");
  51. dataTypes.Add("float", "double");
  52. dataTypes.Add("string", "LSL_Types.LSLString");
  53. dataTypes.Add("key", "LSL_Types.LSLString");
  54. dataTypes.Add("vector", "LSL_Types.Vector3");
  55. dataTypes.Add("rotation", "LSL_Types.Quaternion");
  56. dataTypes.Add("list", "LSL_Types.list");
  57. dataTypes.Add("null", "null");
  58. }
  59. public string Convert(string Script)
  60. {
  61. quotes.Clear();
  62. string Return = String.Empty;
  63. Script = " \r\n" + Script;
  64. //
  65. // Prepare script for processing
  66. //
  67. // Clean up linebreaks
  68. Script = Regex.Replace(Script, @"\r\n", "\n");
  69. Script = Regex.Replace(Script, @"\n", "\r\n");
  70. // QUOTE REPLACEMENT
  71. // temporarily replace quotes so we can work our magic on the script without
  72. // always considering if we are inside our outside quotes's
  73. // TODO: Does this work on half-quotes in strings? ;)
  74. string _Script = String.Empty;
  75. string C;
  76. bool in_quote = false;
  77. bool quote_replaced = false;
  78. string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
  79. string quote = String.Empty;
  80. bool last_was_escape = false;
  81. int quote_replaced_count = 0;
  82. string removefwnoncomments = nonCommentFwsl.Replace(Script, "\"\";");
  83. string removecomments = conelinecomments.Replace(removefwnoncomments, "");
  84. removecomments = cstylecomments.Replace(removecomments, "");
  85. string[] localscript = removecomments.Split('"');
  86. string checkscript = String.Empty;
  87. bool flip = true;
  88. for (int p = 0; p < localscript.Length; p++)
  89. {
  90. //if (localscript[p].Length >= 1)
  91. //{
  92. if (!localscript[p].EndsWith(@"\"))
  93. {
  94. flip = !flip;
  95. //System.Console.WriteLine("Flip:" + flip.ToString() + " - " + localscript[p] + " ! " + localscript[p].EndsWith(@"\").ToString());
  96. }
  97. //}
  98. //else
  99. //{
  100. // flip = !flip;
  101. // System.Console.WriteLine("Flip:" + flip.ToString() + " - " + localscript[p]);
  102. //}
  103. if (!flip)
  104. checkscript += localscript[p];
  105. }
  106. //System.Console.WriteLine("SCRIPT:" + checkscript);
  107. // checks for alpha.alpha way of referring to objects in C#
  108. // ignores alpha.x alpha.y, alpha.z for refering to vector components
  109. Match SecurityM;
  110. SecurityM = Regex.Match(checkscript, @"([a-zA-Z])\.(?:[a-wA-Z]|[a-zA-Z][a-zA-Z])", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  111. if (SecurityM.Success)
  112. throw new Exception("CS0103: 'The . symbol cannot be used in LSL except in float values or vector components'. Detected around: " + SecurityM.Captures[0].Value);
  113. SecurityM = Regex.Match(checkscript, @"typeof\s", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  114. if (SecurityM.Success)
  115. throw new Exception("CS0103: 'The object.typeof method isn't allowed in LSL'");
  116. SecurityM = Regex.Match(checkscript, @"GetType\(", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  117. if (SecurityM.Success)
  118. throw new Exception("CS0103: 'The object.GetType method isn't allowed in LSL'");
  119. for (int p = 0; p < Script.Length; p++)
  120. {
  121. C = Script.Substring(p, 1);
  122. while (true)
  123. {
  124. // found " and last was not \ so this is not an escaped \"
  125. if (C == "\"" && last_was_escape == false)
  126. {
  127. // Toggle inside/outside quote
  128. in_quote = !in_quote;
  129. if (in_quote)
  130. {
  131. quote_replaced_count++;
  132. }
  133. else
  134. {
  135. if (quote == String.Empty)
  136. {
  137. // We didn't replace quote, probably because of empty string?
  138. _Script += quote_replacement_string +
  139. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
  140. }
  141. // We just left a quote
  142. quotes.Add(
  143. quote_replacement_string +
  144. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
  145. quote = String.Empty;
  146. }
  147. break;
  148. }
  149. if (!in_quote)
  150. {
  151. // We are not inside a quote
  152. quote_replaced = false;
  153. }
  154. else
  155. {
  156. // We are inside a quote
  157. if (!quote_replaced)
  158. {
  159. // Replace quote
  160. _Script += quote_replacement_string +
  161. quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
  162. quote_replaced = true;
  163. }
  164. quote += C;
  165. break;
  166. }
  167. _Script += C;
  168. break;
  169. }
  170. last_was_escape = false;
  171. if (C == @"\")
  172. {
  173. last_was_escape = true;
  174. }
  175. }
  176. Script = _Script;
  177. //
  178. // END OF QUOTE REPLACEMENT
  179. //
  180. //
  181. // PROCESS STATES
  182. // Remove state definitions and add state names to start of each event within state
  183. //
  184. int ilevel = 0;
  185. int lastlevel = 0;
  186. string ret = String.Empty;
  187. string cache = String.Empty;
  188. bool in_state = false;
  189. string current_statename = String.Empty;
  190. for (int p = 0; p < Script.Length; p++)
  191. {
  192. C = Script.Substring(p, 1);
  193. while (true)
  194. {
  195. // inc / dec level
  196. if (C == @"{")
  197. ilevel++;
  198. if (C == @"}")
  199. ilevel--;
  200. if (ilevel < 0)
  201. ilevel = 0;
  202. cache += C;
  203. // if level == 0, add to return
  204. if (ilevel == 1 && lastlevel == 0)
  205. {
  206. // 0 => 1: Get last
  207. Match m =
  208. //Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{",
  209. Regex.Match(cache, @"(?![a-zA-Z_]+)\s*(state\s+)?(?<statename>[a-zA-Z_][a-zA-Z_0-9]*)[^a-zA-Z_0-9\(\)]*{",
  210. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  211. in_state = false;
  212. if (m.Success)
  213. {
  214. // Go back to level 0, this is not a state
  215. in_state = true;
  216. current_statename = m.Groups["statename"].Captures[0].Value;
  217. //Console.WriteLine("Current statename: " + current_statename);
  218. cache =
  219. //@"(?<s1>(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?<s2>[^a-zA-Z_\(\)]*){",
  220. Regex.Replace(cache,
  221. @"(?<s1>(?![a-zA-Z_]+)\s*)" + @"(state\s+)?([a-zA-Z_][a-zA-Z_0-9]*)(?<s2>[^a-zA-Z_0-9\(\)]*){",
  222. "${s1}${s2}",
  223. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  224. }
  225. ret += cache;
  226. cache = String.Empty;
  227. }
  228. if (ilevel == 0 && lastlevel == 1)
  229. {
  230. // 1 => 0: Remove last }
  231. if (in_state == true)
  232. {
  233. cache = cache.Remove(cache.Length - 1, 1);
  234. //cache = Regex.Replace(cache, "}$", String.Empty, RegexOptions.Multiline | RegexOptions.Singleline);
  235. //Replace function names
  236. // void dataserver(key query_id, string data) {
  237. //cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  238. //Console.WriteLine("Replacing using statename: " + current_statename);
  239. cache =
  240. Regex.Replace(cache,
  241. @"^(\s*)((?!(if|switch|for|while)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)",
  242. @"$1public " + current_statename + "_event_$2",
  243. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  244. }
  245. ret += cache;
  246. cache = String.Empty;
  247. in_state = true;
  248. current_statename = String.Empty;
  249. }
  250. break;
  251. }
  252. lastlevel = ilevel;
  253. }
  254. ret += cache;
  255. cache = String.Empty;
  256. Script = ret;
  257. ret = String.Empty;
  258. foreach (string key in dataTypes.Keys)
  259. {
  260. string val;
  261. dataTypes.TryGetValue(key, out val);
  262. // Replace CAST - (integer) with (int)
  263. Script =
  264. Regex.Replace(Script, @"\(" + key + @"\)", @"(" + val + ")",
  265. RegexOptions.Compiled | RegexOptions.Multiline);
  266. // Replace return types and function variables - integer a() and f(integer a, integer a)
  267. Script =
  268. Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s+)", @"$1$2" + val + "$3",
  269. RegexOptions.Compiled | RegexOptions.Multiline);
  270. Script =
  271. Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)[,]", @"$1$2" + val + "$3,",
  272. RegexOptions.Compiled | RegexOptions.Multiline);
  273. }
  274. // Add "void" in front of functions that needs it
  275. Script =
  276. Regex.Replace(Script,
  277. @"^(\s*public\s+)?((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)",
  278. @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  279. // Replace <x,y,z> and <x,y,z,r>
  280. Script =
  281. Regex.Replace(Script, @"<([^,>;]*,[^,>;]*,[^,>;]*,[^,>;]*)>", @"new LSL_Types.Quaternion($1)",
  282. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  283. Script =
  284. Regex.Replace(Script, @"<([^,>;)]*,[^,>;]*,[^,>;]*)>", @"new LSL_Types.Vector3($1)",
  285. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  286. // Replace List []'s
  287. Script =
  288. Regex.Replace(Script, @"\[([^\]]*)\]", @"new LSL_Types.list($1)",
  289. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  290. // Replace (string) to .ToString() //
  291. Script =
  292. Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_.]+(\s*\([^\)]*\))?)", @"$1.ToString()",
  293. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  294. Script =
  295. Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_.]+(\s*\([^\)]*\))?)", @"$1.Parse($2)",
  296. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
  297. // Replace "state STATENAME" with "state("statename")"
  298. Script =
  299. Regex.Replace(Script, @"(state)\s+([^;\n\r]+)(;[\r\n\s])", "$1(\"$2\")$3",
  300. RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);
  301. // REPLACE BACK QUOTES
  302. foreach (string key in quotes.Keys)
  303. {
  304. string val;
  305. quotes.TryGetValue(key, out val);
  306. Script = Script.Replace(key, "\"" + val + "\"");
  307. }
  308. //System.Console.WriteLine(Script);
  309. Return = String.Empty;// +
  310. //"using OpenSim.Region.ScriptEngine.Common; using System.Collections.Generic;";
  311. //Return += String.Empty +
  312. // "namespace SecondLife { ";
  313. //Return += String.Empty +
  314. // //"[Serializable] " +
  315. // "public class Script : OpenSim.Region.ScriptEngine.Common.LSL_BaseClass { ";
  316. //Return += @"public Script() { } ";
  317. Return += Script;
  318. //Return += "} }\r\n";
  319. quotes.Clear();
  320. return Return;
  321. }
  322. }
  323. }