-
Notifications
You must be signed in to change notification settings - Fork 0
/
as3tohaxe.txt
417 lines (350 loc) · 17.9 KB
/
as3tohaxe.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright (c) 2011, TouchMyPixel & contributors
* Original author : Tarwin Stroh-Spijer <tarwin@touchmypixel.com>
* Contributers: Tony Polinelli <tonyp@touchmypixel.com>
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE TOUCH MY PIXEL & CONTRIBUTERS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE TOUCH MY PIXEL & CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package;
import neko.FileSystem;
import neko.Lib;
import neko.Sys;
using StringTools;
using As3ToHaxe;
/**
* Simple Program which iterates -from folder, finds .mtt templates and compiles them to the -to folder
*/
class As3ToHaxe
{
public static inline var keys = ["-from", "-to", "-remove"];
var to:String;
var from:String;
var remove:String;
var sysargs:Array<String>;
var items:Array<String>;
public static var basePackage:String = "away3d";
private var nameSpaces:Hash<Ns>;
private var maxLoop:Int;
static function main()
{
new As3ToHaxe();
}
public function new()
{
maxLoop = 1000;
if (parseArgs())
{
// make sure that the to directory exists
if (!FileSystem.exists(to)) FileSystem.createDirectory(to);
// delete old files
if (remove == "true")
removeDirectory(to);
items = [];
// fill items
recurse(from);
// to remember namespaces
nameSpaces = new Hash();
for (item in items)
{
// make sure we only work wtih AS fiels
var ext = getExt(item);
switch(ext)
{
case "as":
doConversion(item);
}
}
// build namespace files
buildNameSpaces();
}
}
private function doConversion(file:String):Void
{
var fromFile = file;
var toFile = to + "/" + file.substr(from.length + 1, file.lastIndexOf(".") - (from.length)) + "hx";
var rF = "";
var rC = "";
var b = 0;
// create the folder if it doesn''t exist
var dir = toFile.substr(0, toFile.lastIndexOf("/"));
createFolder(dir);
var s = neko.io.File.getContent(fromFile);
// spacse to tabs
s = quickRegR(s, " ", "\t");
// undent
s = quickRegR(s, "\t\t", "\t");
// some quick setup, finding what we''ve got
var className = quickRegM(s, "public class([ ]*)([A-Z][a-zA-Z0-9_]*)", 2)[1];
var hasVectors = (quickRegM(s, "Vector([ ]*)\\.([ ]*)<([ ]*)([^>]*)([ ]*)>").length != 0);
// package
s = quickRegR(s, "package ([a-zA-Z\\.0-9-_]*)([ \n\r]*){", "package $1;\n", "gs");
// remove last
s = quickRegR(s, "\\}([\n\r\t ]*)\\}([\n\r\t ]*)$", "}", "gs");
// extra indentation
s = quickRegR(s, "\n\t", "\n");
// class
s = quickRegR(s, "public class", "class");
// constructor
s = quickRegR(s, "function " + className, "function new");
// simple typing
s = quickRegR(s, ":([ ]*)void", ":$1Void");
s = quickRegR(s, ":([ ]*)Boolean", ":$1Bool");
s = quickRegR(s, ":([ ]*)int", ":$1Int");
s = quickRegR(s, ":([ ]*)uint", ":$1UInt");
s = quickRegR(s, ":([ ]*)Number", ":$1Float");
s = quickRegR(s, ":([ ]*)\\*", ":$1Dynamic");
s = quickRegR(s, "<Number>", "<Float>");
s = quickRegR(s, "<int>", "<Int>");
s = quickRegR(s, "<uint>", "<UInt>");
s = quickRegR(s, "<Boolean>", "<Bool>");
// vector
// definition
s = quickRegR(s, "Vector([ ]*)\\.([ ]*)<([ ]*)([^>]*)([ ]*)>", "Vector<$3$4$5>");
// new (including removing stupid spaces)
s = quickRegR(s, "new Vector([ ]*)([ ]*)<([ ]*)([^>]*)([ ]*)>([ ]*)\\(([ ]*)\\)([ ]*)", "new Vector()");
// and import if we have to
if (hasVectors) {
s = quickRegR(s, "class([ ]*)(" + className + ")", "import flash.Vector;\n\nclass$1$2");
}
// array
s = quickRegR(s, " Array([ ]*);", " Array<Dynamic>;");
// remap protected -> private & internal -> private
s = quickRegR(s, "protected var", "private var");
s = quickRegR(s, "internal var", "private var");
s = quickRegR(s, "protected function", "private function");
s = quickRegR(s, "internal function", "private function");
/* -----------------------------------------------------------*/
// namespaces
// find which namespaces are used in this class
var r = new EReg("([^#])use([ ]+)namespace([ ]+)([a-zA-Z-]+)([ ]*);", "g");
b = 0;
while (true) {
b++; if (b > maxLoop) { logLoopError("namespaces find", file); break; }
if (r.match(s)) {
var ns:Ns = {
name : r.matched(4),
classDefs : new Hash()
};
nameSpaces.set(ns.name, ns);
s = r.replace(s, "//" + r.matched(0).replace("use", "#use") + "\nusing " + basePackage + ".namespace." + ns.name.fUpper() + ";");
}else {
break;
}
}
// collect all namespace definitions
// replace them with private
for (k in nameSpaces.keys()) {
var n = nameSpaces.get(k);
b = 0;
while (true) {
b++; if (b > maxLoop) { logLoopError("namespaces collect/replace var", file); break; }
// vars
var r = new EReg(n.name + "([ ]+)var([ ]+)", "g");
s = r.replace(s, "private$1var$2");
if (!r.match(s)) break;
}
b = 0;
while (true) {
b++; if (b > maxLoop) { logLoopError("namespaces collect/replace func", file); break; }
// funcs
var matched:Bool = false;
var r = new EReg(n.name + "([ ]+)function([ ]+)", "g");
if (r.match(s)) matched = true;
s = r.replace(s, "private$1function$2");
r = new EReg(n.name + "([ ]+)function([ ]+)get([ ]+)", "g");
if (r.match(s)) matched = true;
s = r.replace(s, "private$1function$2get$3");
r = new EReg(n.name + "([ ]+)function([ ]+)set([ ]+)", "g");
if (r.match(s)) matched = true;
s = r.replace(s, "private$1function$2$3set");
if (!matched) break;
}
}
/* -----------------------------------------------------------*/
// change const to inline statics
s = quickRegR(s, "([\n\t ]+)(public|private)([ ]*)const([ ]+)([a-zA-Z0-9_]+)([ ]*):", "$1$2$3static inline var$4$5$6:");
s = quickRegR(s, "([\n\t ]+)(public|private)([ ]*)(static)*([ ]+)const([ ]+)([a-zA-Z0-9_]+)([ ]*):", "$1$2$3$4$5inline var$6$7$8:");
/* -----------------------------------------------------------*/
// move variables being set from var def to top of constructor
// do NOT do this for const
// if they're static, leave them there
// TODO!
/* -----------------------------------------------------------*/
// Error > flash.Error
// if " Error (" then add "import flash.Error" to head
var r = new EReg("([ ]+)new([ ]+)Error([ ]*)\\(", "");
if (r.match(s))
s = quickRegR(s, "class([ ]*)(" + className + ")", "import flash.Error;\n\nclass$1$2");
/* -----------------------------------------------------------*/
// create getters and setters
b = 0;
while (true) {
b++;
var d = { get: null, set: null, type: null, ppg: null, pps: null, name: null };
// get
var r = new EReg("([\n\t ]+)([a-z]+)([ ]*)function([ ]*)get([ ]+)([a-zA-Z_][a-zA-Z0-9_]+)([ ]*)\\(([ ]*)\\)([ ]*):([ ]*)([A-Z][a-zA-Z0-9_]*)", "");
var m = r.match(s);
if (m) {
d.ppg = r.matched(2);
if (d.ppg == "") d.ppg = "public";
d.name = r.matched(6);
d.get = "get" + d.name.substr(0, 1).toUpperCase() + d.name.substr(1);
d.type = r.matched(11);
}
// set
var r = new EReg("([\n\t ]+)([a-z]+)([ ]*)function([ ]*)set([ ]+)([a-zA-Z_][a-zA-Z0-9_]*)([ ]*)\\(([ ]*)([a-zA-Z][a-zA-Z0-9_]*)([ ]*):([ ]*)([a-zA-Z][a-zA-Z0-9_]*)", "");
var m = r.match(s);
if (m) {
if (r.matched(6) == d.get || d.get == null)
if (d.name == null) d.name = r.matched(6);
d.pps = r.matched(2);
if (d.pps == "") d.pps = "public";
d.set = "set" + d.name.substr(0, 1).toUpperCase() + d.name.substr(1);
if (d.type == null) d.type = r.matched(12);
}
// ERROR
if (b > maxLoop) { logLoopError("getter/setter: " + d, file); break; }
// replace get
if (d.get != null)
s = quickRegR(s, d.ppg + "([ ]+)function([ ]+)get([ ]+)" + d.name, "private function " + d.get);
// replace set
if (d.set != null)
s = quickRegR(s, d.pps + "([ ]+)function([ ]+)set([ ]+)" + d.name, "private function " + d.set);
// make haxe getter/setter OR finish
if (d.get != null || d.set != null) {
var gs = (d.ppg != null ? d.ppg : d.pps) + " var " + d.name + "(" + d.get + ", " + d.set + "):" + d.type + ";";
s = quickRegR(s, "private function " + (d.get != null ? d.get : d.set), gs + "\n \tprivate function " + (d.get != null ? d.get : d.set));
}else {
break;
}
}
/* -----------------------------------------------------------*/
// for loops (?)
// TODO!
//s = quickRegR(s, "for([ ]*)\\(([ ]*)var([ ]*)([A-Z][a-zA-Z0-9_]*)([.^;]*);([.^;]*);([.^\\)]*)\\)", "");
//var t = quickRegM(s, "for([ ]*)\\(([ ]*)var([ ]*)([a-zA-Z][a-zA-Z0-9_]*)([.^;]*)", 5);
//trace(t);
//for (var i : Int = 0; i < len; ++i)
/* -----------------------------------------------------------*/
var o = neko.io.File.write(toFile, true);
o.writeString(s);
o.close();
// use for testing on a single file
//Sys.exit(1);
}
private function logLoopError(type:String, file:String)
{
trace("ERROR: " + type + " - " + file);
}
private function buildNameSpaces()
{
// build friend namespaces!
trace(nameSpaces);
}
public static function quickRegR(str:String, reg:String, rep:String, ?regOpt:String = "g"):String
{
return new EReg(reg, regOpt).replace(str, rep);
}
public static function quickRegM(str:String, reg:String, ?numMatches:Int = 1, ?regOpt:String = "g"):Array<String>
{
var r = new EReg(reg, regOpt);
var m = r.match(str);
if (m) {
var a = [];
var i = 1;
while (i <= numMatches) {
a.push(r.matched(i));
i++;
}
return a;
}
return [];
}
private function createFolder(path:String):Void
{
var parts = path.split("/");
var folder = "";
for (part in parts)
{
if (folder == "") folder += part;
else folder += "/" + part;
if (!FileSystem.exists(folder)) FileSystem.createDirectory(folder);
}
}
private function parseArgs():Bool
{
// Parse args
var args = Sys.args();
for (i in 0...args.length)
if (Lambda.has(keys, args[i]))
Reflect.setField(this, args[i].substr(1), args[i + 1]);
// Check to see if argument is missing
if (to == null) { Lib.println("Missing argument '-to'"); return false; }
if (from == null) { Lib.println("Missing argument '-from'"); return false; }
return true;
}
public function recurse(path:String)
{
var dir = FileSystem.readDirectory(path);
for (item in dir)
{
var s = path + "/" + item;
if (FileSystem.isDirectory(s))
{
recurse(s);
}
else
{
var exts = ["as"];
if(Lambda.has(exts, getExt(item)))
items.push(s);
}
}
}
public function getExt(s:String)
{
return s.substr(s.lastIndexOf(".") + 1).toLowerCase();
}
public function removeDirectory(d, p = null)
{
if (p == null) p = d;
var dir = FileSystem.readDirectory(d);
for (item in dir)
{
item = p + "/" + item;
if (FileSystem.isDirectory(item)) {
removeDirectory(item);
}else{
FileSystem.deleteFile(item);
}
}
FileSystem.deleteDirectory(d);
}
public static function fUpper(s:String)
{
return s.charAt(0).toUpperCase() + s.substr(1);
}
}
typedef Ns = {
var name:String;
var classDefs:Hash<String>;
}