Archive

Archive for August 25th, 2010

as3parser :: Expression building, sneak peak

August 25th, 2010 Michael Schmalle No comments

Hi,

I have been working the last 2 days completely refactoring, actually rewriting the AS3Parser, all 2500 lines.

The old parser implementation used recursive decent but it had no idea about token stream. I have re-written the parser to use a LinkedListToken token implementation with next and previous, start and stop tokens.

This is all possible since the metaas Java DOM was so awesome, thanks David, I learned a lot! Well, I am using his DOM api to about 80%, it’s really intuitive and tight, check the example below.

Yes, we are at the lowest level, every aspect of actionscript3 will be able to be created now. Not to mention code formatters can be easily written in an AIR application.

The below three tests are just some eye candy right now, this dev is on the as3block branch, I havn’t committed this yet

[Test]
public function testAssignmentExpressionNode():void
{
	var left:IExpressionNode = factory.newExpression("myAnswer");
	var right:IExpressionNode = factory.newExpression("4");
 
	var expression:IAssignmentExpressionNode = 
		factory.newAssignmentExpression(left, right);
 
	assertPrintExpression("myAnswer = 4", expression);
 
	// change right expression
	expression.rightExpression = factory.newExpression("otherAnswer = 4");
 
	assertPrintExpression("myAnswer = otherAnswer = 4", expression);
 
	// change left expression to an array access
	var target:IExpressionNode = factory.newExpression("myObject[42]");
	var subscript:IExpressionNode = factory.newExpression("2");
 
	var arrayAccessExpression:IArrayAccessExpressionNode = 
		factory.newArrayAccessExpression(target, subscript);
 
	expression.leftExpression = arrayAccessExpression;
 
	assertPrintExpression("myObject[42][2] = otherAnswer = 4", expression);
}
 
[Test]
public function testArrayLiteralNode():void
{
	var expression:IArrayLiteralNode = factory.newArrayLiteral();
 
	expression.add(factory.newSimpleNameExpressionNode("a"));
	expression.add(factory.newSimpleNameExpressionNode("b"));
	expression.add(factory.newSimpleNameExpressionNode("c"));
 
	assertPrintExpression("[a, b, c]", expression);
 
	expression.remove(2);
 
	var expression2:IArrayLiteralNode = factory.newArrayLiteral();
 
	expression2.add(factory.newNullLiteral());
	expression2.add(factory.newSimpleNameExpressionNode("foo"));
	expression2.add(factory.newStringLiteral("Hello World"));
	expression2.add(factory.newBooleanLiteral(true));
 
	expression.add(factory.newArrayAccessExpression(
		factory.newSimpleNameExpressionNode("abc"),
		factory.newNumberLiteral(0)));
 
	expression.add(expression2);
 
	assertPrintExpression("[a, b, abc[0], [null, foo, \"Hello World\", true]]", expression);
}
 
[Test]
public function testArrayAccessExpressionNode():void
{
	var target:IExpressionNode = factory.newExpression("myObject[42]");
	var subscript:IExpressionNode = factory.newExpression("0");
 
	var expression:IArrayAccessExpressionNode = 
		factory.newArrayAccessExpression(target, subscript);
 
	assertPrintExpression("myObject[42][0]", expression);
 
	// test changing the 'target'
	target = factory.newExpression("myObject");
	expression.target = target;
 
	assertPrintExpression("myObject[0]", expression);
 
	// test changing the 'subscript'
	subscript = factory.newExpression("42");
	expression.subscript = subscript;
 
	assertPrintExpression("myObject[42]", expression);
 
	// test changing the 'subscript' to a string literal
	subscript = factory.newExpression("'myProp'");
	// or you could use
	// subscript = factory.newStringLiteral("'myProp'");
	expression.subscript = subscript;
 
	assertPrintExpression("myObject['myProp']", expression);
}

Mike

Categories: Uncategorized Tags: