After watching this talk :
http://www.infoq.com/presentations/Design-Your-Own-DSL-with-Groovy
I remembered that I wanted to write about internal DSL in ActionScript.
So first of all what is DSL (for those who are to lazy to follow the wikipedia link)
DSL (Domain Specific Language) is a language that you use for some specific type of problems.
For example you want to draw stuff. You know that if you want to draw something you have to declare a shape that is described by points. The language you create for this one and only problem, should be very specific and clear, so that it is fun to read and write.
There are two ways of creating a Domain Specific Language and also two types of DSLs.
External DSL is a language with natural language syntax that you have to parse by RegEx or a parser.
Internal DSL created by appliance of the language you write this DSL with.
In the talk I have mentioned above, the speaker describes what are the possibilities to create an internal DSL with Groovy, and it is pretty cool
.
In my opinion ActionScript provides you with three posibilities of creating a DSL:
First: XML
If you think about it, MXML is also an internal DSL written in XML and read by MXMLC compiler. You could write a framework that could read XML and process something out of this. For example painted something on the stage.
<shape>
<point x='10', y= '20'/>
<point x='30' y='40'/>
<point x='25 y='25'/>
</shape> |
Second: Plane Text
Write your domain specific stuff in plain text and parse it with RegEx. Natural language is alway easier to read and write if you describe some simple rules or state.
Third: Object initializer
In my opinion it is the powerful one, because you can add behavior to your DSL
var shape = {
points: [
{x:10, y:20},
{x:30, y:40},
{x:25, y:25}
],
onClick: function(){
trace(this.points)
}
} |
As you can see you could create a framework as powerful as Flex that works on DSL created with object intializer syntax, that would look JavaFx like
.