// you’re reading...

ActionScript 3

Listen to me rant about ActionScript, part 1

So I’ve been working on a new feature for Flex Builder (Gumbo), and like always I can’t really tell you what it is yet.  But, I can say it’s support for a new language feature in ActionScript that rhymes with “Schmector.”

Reading over the ActionScript grammar reminds me how much I dislike so many syntactic elements in the language.  So, I’m going to rant.  I don’t do this often, but phrases like “context-sensitive reserved words” make be a bit grumpy.  And as the guy working on the parsers in Flex Builder, it makes me exceptionally grumpy.

Thing that makes me grumpy #1- Syntactic ambiguities

Regular Expressions:
Regex’s are great, and we wouldn’t have a lot of parsers and compilers without them.  In ActionScript 3, they’re a literal on the same level as Strings, Objects, Booleans, XML and Numbers.  So, I can write:

var string:String = "Hi Mom!";
var bool:Boolean = true.
var regex:Regexp = /[a-zA-z0-9]*/;

ActionScript (and EcmaScript-based languages) use the / delimiter to start and end a regular expression. This deviates from languages like Java, where regex’s are wrapped in quotes. The problem with the Java approach is that programmers need to escape anything that has special meaning to a String literal, like escape sequences, etc. ActionScript solves that problem with /, but introduces a totally different problem for those of us writing tooling: it’s a syntax rule that is ambiguous!  

So what does that mean?  Well, let’s say I have the following snippet: /hello
Is this a division operator, or is this an unterminated regular expression? Just looking at the / doesn’t tell us anything, and we have to start back-tracking when we tokenize and potentially change the meanings of already processed tokens. If this is a regex, then / is no longer a division operator, it’s actually a regex delimiter. But how do we decide if it’s a regex?  Flex Builder does it, but trust me, it’s complicated.

E4X
One of the really cool features in ActionScript 3 is E4X: it let’s you embed XML directly as a literal in your code.  So, you can write:
var foo:XML = <ArbXML>MyValue</ArbXML>

Again, this is great for AS3 programmers but you may have noticed that this is also ambiguous.  What do the <, > and / mean in this context?  Well, it depends what comes before them just like with regex syntax.  But this adds an additional level of fun, because in addition to ambiguities with less than and greater than operators, we all get the added bonus of figuring out if we’re a division operator or a regex delimiter.

Don’t get me wrong, ActionScript provides great features to the user, but in a lot of cases they do so by sacrificing the tool-ability of the language.  Just look at hoisting, or context-sensitive keywords.  But I’ll talk about those tomorrow…

Discussion

7 comments for “Listen to me rant about ActionScript, part 1”

  1. For years I have argued that one of the most significant failings in computing is the lack of an actual delimeter.

    I’ve argued for the adoption of þ as a universal delimiter or the creation of some said unused character. That would not be included in actual common text.

    Posted by Jason The Saj | December 8, 2008, 2:46 pm
  2. [...] developer’s lives easier. That sugar, however, gives me, umm, toothaches, some of which I talked about already. So now, I’ll rant about some other idiosyncrasies of AS3 that make it an interesting [...]

    Posted by Marking Occurrences » Listen to me rant about ActionScript, part 2 | December 8, 2008, 10:22 pm
  3. In my recent project, I had to do quite some String parsing (in Actionscript). It’s mainly parsing CSS, an extended version of JSON and some simple action format. While my main challenge was speed, I can now much more appreciate when I read about your insides. It’s very interesting and I’m looking forward to read part 2 (now) :)

    On a side note – RegExp: It’s kind of funny that it gives you difficulties to parse, as it gave me difficulties to *use* for parsing, for the simple fact that it’s much slower than string operations.

    Posted by Sev | December 17, 2008, 5:16 am
  4. ASCII (and presumably Unicode, since it supposedly incorporates everything) already has four delimiters built in:

    28 – File Separator
    29 – Group Separator
    30 – Record Separator
    31 – Unit Separator

    I have never understood why nobody uses them (other than most editors not displaying them well)

    Posted by Rob Freundlich | January 20, 2009, 6:42 am
  5. Editors and keyboards not supporting them well is a bit of a hurdle :D

    Posted by David Zuckerman | January 20, 2009, 10:37 am
  6. Hi David,

    I need the ActionScript grammar to build a parser upon it. Is it possible to get it !!?

    Best regards,

    Posted by Awan | February 18, 2009, 6:38 am
  7. If you google ActionScript grammar, there’s probably something out there. Check out antlr.org, they have a great list of grammars

    Posted by David Zuckerman | February 18, 2009, 4:04 pm

Post a comment