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