11<?php
2- /*
3- TODO: For general auto-convertor, need to handle user-defined type annotations
4- Need to rewrite annotation removing system completely, as the user could have
5- called their class anything and this could destroy all sorts of unexpected bits of code
6- Perhaps insert spacing into ternary operator statements so that we can safely delete any
7- words after a colon
8- Caveats:
9- Has no respect for string literals. Will munch words inside strings that look like code
10- */
11- $ start = microtime (true );
12-
2+ /*
3+ DESCRIPTION:
4+ Converts Flixel's AS3 classes to Mootools-friendly Javascript classes
5+ Reads all the .as files in the subdirectories org/flixel and org/flixel/data
6+
7+ TODO: For general auto-convertor, need to handle user-defined type annotations
8+ Need to rewrite annotation removing system completely, as the user could have
9+ called their class anything and this could destroy all sorts of unexpected bits of code
10+ Perhaps insert spacing into ternary operator statements so that we can safely delete any
11+ words after a colon
12+
13+ Caveats:
14+ Has no respect for string literals. Will munch words inside strings that look like code
15+ */
1316 $ as3_class_names = Array (
1417 "Rectangle " , "Point " , "BitmapData " , "SoundChannel " , "SoundTransform " ,
1518 "Event " , "Class " , "Matrix " , "ColorTransform " , "Sprite " , "KeyboardEvent " , "TextField " ,
7982 //step through each class...
8083 foreach ($ all_code as $ c ) {
8184
85+ //Figure out what the current class it called and what it inherits from (if anything)
8286 preg_match ('@(?:dynamic )?(?:public|internal) class (\w+)(?: extends (\w+))?@ ' , $ c , $ matches );
8387 $ name = $ matches [1 ];
8488
8892
8993 //step through each line of the current class
9094 foreach ($ code_lines as $ line ) {
95+
96+ //Count braces to figure out where we are
9197 $ brace_count += substr_count ($ line , "{ " );
9298 $ brace_count -= substr_count ($ line , "} " );
9399 if ($ brace_count == 1 ) {
96102 $ in_class = false ;
97103 }
98104
105+ //Detect braces that close the method (as opposed to other statements) and jump us out
99106 if ($ brace_count <= 1 ) {
100107 if ($ in_method ) {
101108 $ code_out [] = "} " ;
104111 }
105112
106113 if ($ in_method ) {
107- $ line = preg_replace ("@([^.])((?: $ tokens))@ " , "$1this.$2 " , $ line );
114+ //Match tokens properly delimited (using lookbehind + lookahead regex)
115+ //Token must be preceded by something that isn't a period,
116+ // to avoid messing with nested propert refs, and (?=[^\w]) prevents cutting into the
117+ // middle of words
118+ //Also ignore anything after "new " so we don't get this: new this.FlxPoint()
119+ $ line = preg_replace ("@(?<=[^.\w])(?<!new )((?: $ tokens))(?=[^\w])@ " , "this.$1 " , $ line );
108120 $ code_out [] = $ line ;
109121 }
110122
123+ //When we hit the beginning of a method, pull out relevant parameters and build
124+ // a method definition in MooTools style
111125 if ($ brace_count == 2 && preg_match ('@function (?:(get|set) )?(\w+)\s*\(.*\)@ ' , $ line , $ m )) {
112126 $ in_method = true ;
113127 $ get_set = $ m [1 ];
132146
133147
134148 //echo $str;
135- // var_dump($code_out);
149+ var_dump ($ code_out );
136150 //foreach($code_out as $line) { echo "$line\n"; }
137151
138152
146160
147161/*****************************************************************/
148162
163+ /** Does some pre-processing to normalize the code into an easy-to-parse format **/
149164 function class_format ($ str ) {
150165
151166 global $ class_regex ;
@@ -187,7 +202,7 @@ function class_format($str) {
187202
188203 function class_extract ($ str ) {
189204
190- ///Extracts into into:
205+ ///Extracts info into:
191206 /// $class_name, $extends, $class_constants, $class_props, $class_static, $found_methods
192207
193208 //Pull out the class name and the class it extends (if any)
@@ -211,7 +226,7 @@ function class_extract($str) {
211226 preg_match_all ('@static (?:public|protected|private|internal) var (\w+);@ ' , $ str , $ class_static );
212227 //Class methods. Includes getters and setters
213228 //NOTE: Had to update parameter capturing to be non-greedy - (.*?) and (.+?) - in case a function is
214- //defined on one, line as with flicker() and flickering() in FlxObject
229+ //defined on one line, as with flicker() and flickering() in FlxObject
215230 preg_match_all ('@(?:override )?(static)?\s*(?:public|protected|private|internal) function (?:(get|set) )?(.+?)\((.*?)\)@ ' , $ str , $ found_methods );
216231
217232 ///Loop through and separate out getters/setters from other methods
@@ -296,11 +311,11 @@ function list_files() {
296311 return $ files ;
297312 }
298313
299- // List only files with certain extensions
300- // exclude hidden files (starting with . or ending with ~)
301- // For now, this is not optional, they're just hidden
302- // * to list files with any extension
303- // Returns: A 2-dimensional array, [full filename] = [name, ext]
314+ /** List only files with certain extensions
315+ exclude hidden files (starting with . or ending with ~)
316+ For now, this is not optional, they're just hidden
317+ * to list files with any extension
318+ Returns: A 2-dimensional array, [full filename] = [name, ext] **/
304319 function list_some_files ($ allowed_ext , $ show_hidden = false ) {
305320 $ files = array ();
306321 $ list = list_files ();
0 commit comments