ActionScript 3 import alias! Where is it?

working with Flex2/ActionScript 3 sometimes you may find that in different libraries there as classes (always in different packages) with the same name.
Ok, no problem, in fact I remember I’ve read something in the as3 documentation about the import alias directive..
A quick google search and in fact I come to this page: http://livedocs.adobe.com/…?href=as3_specification106.html

Cool, I thought. Then I applied the directive in my as3 project (because two ‘Location’ classes were giving me problems in flex) but when clicked on compile it throws me a syntax error  🙁
Strange, it’s described in the documentation, but dont work!.. It’s also described in the es4 specs.
Fortunately Colin gives me an answer: “… single name aliases were indeed dropped for AS3. even if the plan is that they will go into the ES4 standard, and if that occurs, they will be added to ActionScript eventually.”
I hope it will happen soon!

IMHO, I would prefer a syntax like the python one about the import alias: http://docs.python.org/ref/import.html

Something like: import os as somethingelse, but it’s just a stylistic issue for me…

ActionScript Parsing, the YACC revenge :)

After my first attempts with ANTLR scanners in python/java I decided to start back with Bison/Flex again to see the difference in performances.
So first I need to wrote from scratch the grammar/lexer files using only the ECMAScript 4 specifications and much patience (the elastic grammar file help me a lot too).

After finishing a first version of the parser I tested it on the same file (75Kb actionscript file) which both java and python parsed in more than 1 second.
The result was unbelievable: 0.02 seconds for that file!

Then I tested it on multiple files, and for about 320 files of the whole adobe corelib library it took 220ms

Ok, the parser it’s not yet complete and doesn’t care about regexp and xml syntax, but its performance convinced me enough…
Now, the next step is to finish and test the parser and finally create a python library using pyrex, then a benchmark test again.

If someone is interested in testing the parser, download it (use “parser –help” form the command line for usage help), but remember this is only a first test.. not really helpful right now (I just wanted to share my text/parsing experiences).

An experience with antlr, java and python

I just wanted to share a little experience with generating an AS3 parser using antlr and python.
I was trying first to create the parser using GNU Flex and Bison in C, probably the best way for a very performancing parser.
Yeah, that’s right.. but looking at the antlr syntax I realized that’s easier and easier.
Moreover I start using this very useful eclipse plugin for antlr debugging which made my life easier!

The grammar file I created is a compromise between the asdt grammar file and the ECMA-262 grammar specification.

Once finished working on my eclipse project I’ve managed to parse succesfully all the adobe corelibs files using this java test file:
package org.sepy.core.parsers.as3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import antlr.CommonAST;
import antlr.RecognitionException;
import antlr.TokenStreamException;
public class Application {
public static void main(String argv[])
{
if(argv.length > 0)
{
File file = new File(argv[0]);
if(file.exists())
{
FileInputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AS3Lexer L = new AS3Lexer(is);
AS3Parser P = new AS3Parser(L);
try {
P.compilationUnit();
} catch (RecognitionException e) {
// TODO Auto-generated catch block
System.out.println(" line=" + e.line + ", column="+ e.column);
System.out.println(e.getMessage());
e.printStackTrace(System.err);
} catch (TokenStreamException e) {
// TODO Auto-generated catch block
System.out.println(" line=" + L.getLine() + ", column="+ L.getColumn());
System.out.println(L.getGuessInfo());
System.out.println(e.getMessage());
e.printStackTrace(System.err);
}
CommonAST.setVerboseStringConversion(false, P.getTokenNames());
CommonAST ast = (CommonAST) P.getAST();
System.out.println("Tree:");
System.out.println(ast.toStringTree());
}
}
}
}

Ok, done that I decided to export the grammar file for python (thanks to antrl python export feature).
Everything works fine also for python, but I realized that the python script were so much slower than the java one!
import sys
import antlr
import AS3Parser
import AS3Lexer
L = AS3Lexer.Lexer(filename);
P = AS3Parser.Parser(L);
P.setFilename(filename)
try:
P.compilationUnit();
ast = P.getAST();
except:
pass

On a 75Kb actionscript file the python script took about 7 seconds to run, while the java application only 2 seconds. I know python interpreter caould be slower than many other languages, but I never thought so much slower.
So I run the python hotshot profiler to see which could be the bottleneck in the python script and I found most of the problems were due to unuseless antlr (the python module) method’s calls.
After making corrections to the antlr.py file the same script took exactly half of the time. Now 3 seconds. Wow 🙂
But not fast enough.
So I enabled for the antlr python script psyco module and this time the same script took only 1.6 seconds.
Now the python script is fast enough, even if I’m sure I can make more optimizations in the antlr module…

E4X, ActionScript 3 new “sublanguage”

Since I’ve heard the voices about the upcoming E4x in actionscript 3 I awaited this moment…
Finally ActionScript supports E4X and we can kick away the old XML object (always supported for backward compatibility)! we can stop using all the various xml2object classes created in these years, we finally have a validating parser, so we will see finally well formatted XML files from flash users 🙂
But finally we have a powerful “language” with a very strong syntax.

I’ve written a small tutorial with a quick guide  about actionscript3 and e4x basic syntax (without including Namespaces and QNames for the moment)

http://www.sephiroth.it/tutorials/flashPHP/E4X/index.php

P.s. I forgot to say that E4X is also available in the Geko1.8 browser (such as Firefox 1.5). For more info see this article: http://developer.mozilla.org/en/docs/E4X. Unfortunately E4X ins’t supported by python, neither there’s a module for it 🙁

ActionScript 3, … (rest) parameter, finally!

Yesterday I was reading the latest actionscript 3 documentation on adobe labs and I found that they finally introduced the … (rest) function’s argument, as also described in the ECMA Script 4 specifications.
I say “finally” because it’s since I discovered python *args and **kwds function arguments that I pray for them also in actionscript.
In actionscript3 rest parameter is used for example:

[cc lang=”actionscript3″]
public function log(message:String, … rest):void
{
logging.text += message + “\n”;
for(var i:uint = 0; i < rest.length; i++)
{
logging.text += rest[i] + “\n”;
}
}
log(“hello”, “spammed”, “world”);

[/cc]

The reference says about the rest parameter: specifies that a function will accept any number of comma delimited arguments. The list of arguments becomes an array that is available throughout the function body.
Now I just will wait till they will intoduce also the **kwds python equivalent parameter and I will an happy man! 🙂
Read more here

Actionscript 3 ?

Recently Macromedia release some news about Flex 2…and what we can find in one page (you can see it here) ?
“With ActionScript 3.0, we have achieved more than simple compliance with the ECMAScript standard; Macromedia now chairs the ECMAScript committee and helps drive its evolution. ActionScript 3.0 features a compilation mode for stronger compile-time type checking that provides the benefits of languages such as Java or C#. It supports new features to streamline data manipulation, including the E4X (ECMAScript for XML) standard, which extends the language and adds XML as a native data type allowing developers to more naturally interact with and manipulate XML. It adds regular expressions support for better text parsing and processing. It sheds some of the ad hoc event handling schemes in the old virtual machine in favor of a unified model based on the W3C DOM Events standard. And it has significantly updated APIs aimed at the application developer audience.
For a complete overview of the new capabilities, stay tuned as we release an overview of ActionScript 3.0 and the Flash Player 8.5 alpha.”

It seems that on 17th October we’ll have a public preview of Flash Player 8.5…I’m curious a lot!
http://www.macromedia.com/devnet/…/flex2_intro.html

Will Flash 8 support E4X?

An unintentionally mail posted in the Flex mailing list talks about one of the new Flex 2.0 features, the support of E4X.
This means that probably the new flash player will support it also… This should be a great step over the old “DOM” flash support.. in this case XML entities will be threated as builtins objects Read more about ECMAScript for XML (E4X) Specification: http://www.ecma-international.org/publications/standards/Ecma-357.htm

see the original post