com.flagstone.translate
Class ASNode

java.lang.Object
  extended by com.flagstone.translate.ASNode

public final class ASNode
extends java.lang.Object

ASNode is the class used by the parser to construct a tree representation of an ActionScript file based on the parser grammar. Node trees can also be constructed 'manually' and then encoded to give the binary representation of the byte-codes and actions that will be executed by the Flash Player. For example, the node tree for the ActionScript statement:

     c = a + b;
 
Can be represented using the following code to build the tree.
     ASNode a = new ASNode(ASNode.Identifier, "a");
     ASNode b = new ASNode(ASNode.Identifier, "b");
     ASNode c = new ASNode(ASNode.Identifier, "c");
 
     ASNode add = new ASNode(ASNode.Add, a, b);
     ASNode assign = new ASNode(a, add);
 
The ASNode class defines a full range of node types ranging from specifying literals through to complex structures such as iterative and conditional constructs such as for loops and if/else blocks. The simplest method for determining the structure of the trees that represent different structure in ActionScript is to use the Interpreter class provided in the framework and dump out the structure of the parsed code.


Field Summary
static int Add
          Add operation
static int And
           
static int Array
          Array nodes are used to represent any list of ActionScript statements.
static int ASR
          Arithmetic Shift Right operation
static int Assign
          Assign, =
static int AssignAdd
          Assign add, +=
static int AssignASR
          Assign arithmetic shift right, >>=
static int AssignBitAnd
          Assign bitwise-AND, &=
static int AssignBitOr
          Assign bitwise-OR, |=
static int AssignBitXOr
          Assign bitwise-exclusive-OR, ^=
static int AssignDiv
          Assign divide, /=
static int AssignLSL
          Assign logical shift left, <<=
static int AssignLSR
          Assign logical shift right, >>>=
static int AssignMod
          Assign modulo, %=
static int AssignMul
          Assign multiply, *=
static int AssignSub
          Assign subtract, -=
static int Attribute
          Use to represent an attribute of an object
static int BitAnd
          Bitwise AND operation
static int BitNot
          Unary bit-not
static int BitOr
          Bitwise OR operation
static int BitXOr
          Bitwise Exclusive-OR operation
static int BooleanLiteral
          Use to represent a boolean value
static int Break
          Use to represent break statements
static int Button
          Button nodes are used to represent the on() block statement in ActionScript.
static int Catch
           
static int Continue
          < Use to represent continue statements
static int DefineArray
          Use to represent an anonyomus array.
static int DefineAttribute
          Use to represent an attribute on a user defined object.
static int DefineFunction
          Use to represent a user defined function.
static int DefineMethod
          Use to represent a method on a user defined object.
static int DefineObject
          Use to represent a user defined object.
static int DefineVariable
          Use to represent a var statement
static int Delete
          Object reclamation
static int Div
          Divide operation
static int Do
          Use to represent do..while loops
static int DoubleLiteral
          Use to represent an double-precision floating point value
static int EndInitClip
           
static int Equal
          Equal comparison
static int Exception
           
static int Finally
           
static int For
          Use to represent for loops
static int ForIn
          Use to represent for..in statements
static int Function
          Use to represent the name of one of ActionScript's built-in functions.
static int GreaterThan
          Greater Than comparison
static int GreaterThanEqual
          Greater Than or Equal comparison
static int Identifier
          Use to represent a variable
static int If
          Use to represent if statements
static int InitClip
           
static int InstanceOf
          Object identity
static int IntegerLiteral
          Use to represent an integer value
static int Label
           
static int LessThan
          Less Than comparison
static int LessThanEqual
          Less Than or Equal comparison
static int List
           
static int LogicalAnd
          Logical AND operation
static int LogicalOr
          Logical OR operation
static int LSL
          Logical Shift Left operation
static int LSR
          Logical Shift Right operation
static int Method
          Use to represent the name of a method
static int Minus
          Unary minus
static int Mod
          Modulo operation
static int MovieClip
          MovieClip nodes are used to represent the onClipEvent() block statement in ActionScript.
static int Mul
          Multiply operation
static int NewObject
          Use to represent new statements for creating instances of objects.
static int NoOp
          NoOp is used as a place-holder for child nodes with resorting to using null.
static int Not
          Unary not
static int NotEqual
          Not Equal comparison
static int NullLiteral
          Use to represent a null literal
static int On
          Use to represent on statements
static int OnClipEvent
          Use to represent onClipEvent statements
static int Or
           
static int Plus
          Unary plus
static int PostDec
          Post-decrement
static int PostInc
          Post-increment
static int PreDec
          Pre-decorement
static int PreInc
          Pre-increment
static int Return
          Use to represent return statements
static int Select
          ternary operator.
static int StatementList
          List nodes are used to represent groups of one or more statements.
static int StrictEqual
          Strict Equal comparison
static int StrictNotEqual
          Strict Not Equal comparison
static int StringAdd
          Strict Not Equal comparison
static int StringEqual
           
static int StringGreaterThan
           
static int StringGreaterThanEqual
           
static int StringLessThanEqual
           
static int StringLiteral
          Use to represent a string value
static int StringNotEqual
           
static int Sub
          Subtract operation
static int Subscript
          Use to represent subscript operation when accessing the elements of an array.
static int Switch
           
static int Throw
           
static int Try
           
static int Value
          Value is an abstract node type used to group together nodes that will result in a value being generated such as subscripting an array variable or dereferencing an object's attribute.
static int While
          Use to represent while loops
static int With
          Use to represent with statements
 
Constructor Summary
ASNode(int nodeType)
          Constructs an ASNode with the specified type.
ASNode(int nodeType, ASNode node)
          Constructs an ASNode with the specified type and adds the child node.
ASNode(int nodeType, ASNode node1, ASNode node2)
          Constructs an ASNode with the specified type and adds the child nodes.
ASNode(int nodeType, double value)
          Constructs an ASNode with the specified type and floating-point value.
ASNode(int nodeType, int value)
          Constructs an ASNode with the specified type and integer value.
ASNode(int nodeType, java.lang.String value)
          Constructs an ASNode with the specified type and string value.
 
Method Summary
 void add(ASNode aNode)
          Adds a node to the array of children.
 int count()
          Return the number of child nodes contained by this node.
 void displayTree(java.lang.String prefix)
          displayTree is used to display the structure of the node tree, with the root starting at the current node.
 byte[] encode(int version)
          The encode method 'compiles' the node and all child nodes into an array of action objects which represents the sequence of actions performed by the Flash Player.
 ASNode get(int index)
          Returns the node at the specified index from the array of child nodes.
 boolean getBoolValue()
          Get the boolean value assigned to a node.
 double getDoubleValue()
          Get the floating-point value assigned to a node.
 int getIntValue()
          Get the integer value assigned to a node.
 int getNumber()
          Get the number assigned to a node.
 ASNode getParent()
          Gets the parent node of this one.
 java.lang.String getStringValue()
          Get the string value assigned to a node.
 int getType()
          Gets the type of the node.
 int indexOf(ASNode aNode)
          Returns the index position of a node in the array of child nodes.
 void insert(int index, ASNode aNode)
          Inserts a node at position i in the array of children.
 void remove(int index)
          Removes the node at position i in the array of children.
 void set(int i, ASNode aNode)
          Replaces the node at position i in the array of children.
 void setBoolValue(boolean value)
          Set the boolean value assigned to a node.
 void setDoubleValue(double value)
          Set the floating-point value assigned to a node.
 void setIntValue(int value)
          Set the integer value assigned to a node.
 void setNumber(int value)
          Set the number assigned to a node.
 void setStringValue(java.lang.String value)
          Set the string value assigned to a node.
 void setType(int type)
          Sets the type of the node.
 java.lang.String toString()
          Returns a string containing the type of node, any associated value and the number of children.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

Array

public static final int Array
Array nodes are used to represent any list of ActionScript statements. Use this type of node when constructing trees to represent sequences of actions for FSDoAction, FSClipEvent or FSButtonEvent objects.

See Also:
Constant Field Values

Button

public static final int Button
Button nodes are used to represent the on() block statement in ActionScript. Use this type of node when constructing trees that will be encoded and added to FSDefineButton2 objects.

See Also:
Constant Field Values

MovieClip

public static final int MovieClip
MovieClip nodes are used to represent the onClipEvent() block statement in ActionScript. Use this type of node when constructing trees that will be encoded and added to FSPlaceObject2 objects.

See Also:
Constant Field Values

StatementList

public static final int StatementList
List nodes are used to represent groups of one or more statements. They are used to represent statements included in any block structure such as an if statement or for loop. Lists are also used to simplify the construction of complex statements such as for loops. Using Lists, a for loop contains a maximum of four child nodes with lists used to group the statements forming the initialisation and iteration part of the for statement and body of the loop.

See Also:
Constant Field Values

List

public static final int List
See Also:
Constant Field Values

NoOp

public static final int NoOp
NoOp is used as a place-holder for child nodes with resorting to using null. No actions will be generated when the node is translated.

See Also:
Constant Field Values

If

public static final int If
Use to represent if statements

See Also:
Constant Field Values

For

public static final int For
Use to represent for loops

See Also:
Constant Field Values

ForIn

public static final int ForIn
Use to represent for..in statements

See Also:
Constant Field Values

While

public static final int While
Use to represent while loops

See Also:
Constant Field Values

Do

public static final int Do
Use to represent do..while loops

See Also:
Constant Field Values

With

public static final int With
Use to represent with statements

See Also:
Constant Field Values

OnClipEvent

public static final int OnClipEvent
Use to represent onClipEvent statements

See Also:
Constant Field Values

On

public static final int On
Use to represent on statements

See Also:
Constant Field Values

Break

public static final int Break
Use to represent break statements

See Also:
Constant Field Values

Return

public static final int Return
Use to represent return statements

See Also:
Constant Field Values

Continue

public static final int Continue
< Use to represent continue statements

See Also:
Constant Field Values

Value

public static final int Value
Value is an abstract node type used to group together nodes that will result in a value being generated such as subscripting an array variable or dereferencing an object's attribute.

See Also:
Constant Field Values

BooleanLiteral

public static final int BooleanLiteral
Use to represent a boolean value

See Also:
Constant Field Values

IntegerLiteral

public static final int IntegerLiteral
Use to represent an integer value

See Also:
Constant Field Values

DoubleLiteral

public static final int DoubleLiteral
Use to represent an double-precision floating point value

See Also:
Constant Field Values

StringLiteral

public static final int StringLiteral
Use to represent a string value

See Also:
Constant Field Values

NullLiteral

public static final int NullLiteral
Use to represent a null literal

See Also:
Constant Field Values

Identifier

public static final int Identifier
Use to represent a variable

See Also:
Constant Field Values

Attribute

public static final int Attribute
Use to represent an attribute of an object

See Also:
Constant Field Values

Method

public static final int Method
Use to represent the name of a method

See Also:
Constant Field Values

Function

public static final int Function
Use to represent the name of one of ActionScript's built-in functions.

See Also:
Constant Field Values

NewObject

public static final int NewObject
Use to represent new statements for creating instances of objects.

See Also:
Constant Field Values

Subscript

public static final int Subscript
Use to represent subscript operation when accessing the elements of an array.

See Also:
Constant Field Values

DefineFunction

public static final int DefineFunction
Use to represent a user defined function.

See Also:
Constant Field Values

DefineArray

public static final int DefineArray
Use to represent an anonyomus array.

See Also:
Constant Field Values

DefineObject

public static final int DefineObject
Use to represent a user defined object.

See Also:
Constant Field Values

DefineMethod

public static final int DefineMethod
Use to represent a method on a user defined object.

See Also:
Constant Field Values

DefineAttribute

public static final int DefineAttribute
Use to represent an attribute on a user defined object.

See Also:
Constant Field Values

DefineVariable

public static final int DefineVariable
Use to represent a var statement

See Also:
Constant Field Values

Add

public static final int Add
Add operation

See Also:
Constant Field Values

Sub

public static final int Sub
Subtract operation

See Also:
Constant Field Values

Mul

public static final int Mul
Multiply operation

See Also:
Constant Field Values

Div

public static final int Div
Divide operation

See Also:
Constant Field Values

Mod

public static final int Mod
Modulo operation

See Also:
Constant Field Values

LSL

public static final int LSL
Logical Shift Left operation

See Also:
Constant Field Values

ASR

public static final int ASR
Arithmetic Shift Right operation

See Also:
Constant Field Values

LSR

public static final int LSR
Logical Shift Right operation

See Also:
Constant Field Values

BitAnd

public static final int BitAnd
Bitwise AND operation

See Also:
Constant Field Values

BitOr

public static final int BitOr
Bitwise OR operation

See Also:
Constant Field Values

BitXOr

public static final int BitXOr
Bitwise Exclusive-OR operation

See Also:
Constant Field Values

LogicalAnd

public static final int LogicalAnd
Logical AND operation

See Also:
Constant Field Values

LogicalOr

public static final int LogicalOr
Logical OR operation

See Also:
Constant Field Values

Equal

public static final int Equal
Equal comparison

See Also:
Constant Field Values

NotEqual

public static final int NotEqual
Not Equal comparison

See Also:
Constant Field Values

GreaterThan

public static final int GreaterThan
Greater Than comparison

See Also:
Constant Field Values

LessThan

public static final int LessThan
Less Than comparison

See Also:
Constant Field Values

GreaterThanEqual

public static final int GreaterThanEqual
Greater Than or Equal comparison

See Also:
Constant Field Values

LessThanEqual

public static final int LessThanEqual
Less Than or Equal comparison

See Also:
Constant Field Values

Select

public static final int Select
ternary operator.

See Also:
Constant Field Values

Not

public static final int Not
Unary not

See Also:
Constant Field Values

BitNot

public static final int BitNot
Unary bit-not

See Also:
Constant Field Values

Plus

public static final int Plus
Unary plus

See Also:
Constant Field Values

Minus

public static final int Minus
Unary minus

See Also:
Constant Field Values

PreInc

public static final int PreInc
Pre-increment

See Also:
Constant Field Values

PreDec

public static final int PreDec
Pre-decorement

See Also:
Constant Field Values

PostInc

public static final int PostInc
Post-increment

See Also:
Constant Field Values

PostDec

public static final int PostDec
Post-decrement

See Also:
Constant Field Values

Assign

public static final int Assign
Assign, =

See Also:
Constant Field Values

AssignAdd

public static final int AssignAdd
Assign add, +=

See Also:
Constant Field Values

AssignSub

public static final int AssignSub
Assign subtract, -=

See Also:
Constant Field Values

AssignMul

public static final int AssignMul
Assign multiply, *=

See Also:
Constant Field Values

AssignDiv

public static final int AssignDiv
Assign divide, /=

See Also:
Constant Field Values

AssignMod

public static final int AssignMod
Assign modulo, %=

See Also:
Constant Field Values

AssignLSL

public static final int AssignLSL
Assign logical shift left, <<=

See Also:
Constant Field Values

AssignASR

public static final int AssignASR
Assign arithmetic shift right, >>=

See Also:
Constant Field Values

AssignLSR

public static final int AssignLSR
Assign logical shift right, >>>=

See Also:
Constant Field Values

AssignBitAnd

public static final int AssignBitAnd
Assign bitwise-AND, &=

See Also:
Constant Field Values

AssignBitOr

public static final int AssignBitOr
Assign bitwise-OR, |=

See Also:
Constant Field Values

AssignBitXOr

public static final int AssignBitXOr
Assign bitwise-exclusive-OR, ^=

See Also:
Constant Field Values

InstanceOf

public static final int InstanceOf
Object identity

See Also:
Constant Field Values

Delete

public static final int Delete
Object reclamation

See Also:
Constant Field Values

StrictEqual

public static final int StrictEqual
Strict Equal comparison

See Also:
Constant Field Values

StrictNotEqual

public static final int StrictNotEqual
Strict Not Equal comparison

See Also:
Constant Field Values

StringAdd

public static final int StringAdd
Strict Not Equal comparison

See Also:
Constant Field Values

StringEqual

public static final int StringEqual
See Also:
Constant Field Values

StringNotEqual

public static final int StringNotEqual
See Also:
Constant Field Values

StringLessThanEqual

public static final int StringLessThanEqual
See Also:
Constant Field Values

StringGreaterThan

public static final int StringGreaterThan
See Also:
Constant Field Values

StringGreaterThanEqual

public static final int StringGreaterThanEqual
See Also:
Constant Field Values

Exception

public static final int Exception
See Also:
Constant Field Values

Try

public static final int Try
See Also:
Constant Field Values

Catch

public static final int Catch
See Also:
Constant Field Values

Finally

public static final int Finally
See Also:
Constant Field Values

Switch

public static final int Switch
See Also:
Constant Field Values

Throw

public static final int Throw
See Also:
Constant Field Values

Label

public static final int Label
See Also:
Constant Field Values

InitClip

public static final int InitClip
See Also:
Constant Field Values

EndInitClip

public static final int EndInitClip
See Also:
Constant Field Values

And

public static final int And
See Also:
Constant Field Values

Or

public static final int Or
See Also:
Constant Field Values
Constructor Detail

ASNode

public ASNode(int nodeType)
Constructs an ASNode with the specified type.

Parameters:
nodeType - the type of node being constructed.

ASNode

public ASNode(int nodeType,
              int value)
Constructs an ASNode with the specified type and integer value. This constructor is primarily used to create nodes representing integer literals.

Parameters:
nodeType - the type of node being constructed.
value - the integer value assigned to the node.

ASNode

public ASNode(int nodeType,
              double value)
Constructs an ASNode with the specified type and floating-point value. This constructor is primarily used to create nodes representing literals.

Parameters:
nodeType - the type of node being constructed.
value - the floating-point value assigned to the node.

ASNode

public ASNode(int nodeType,
              java.lang.String value)
Constructs an ASNode with the specified type and string value. This constructor is primarily used to create string literals and identifiers.

Parameters:
nodeType - the type of node being constructed.
value - the string assigned to the node.

ASNode

public ASNode(int nodeType,
              ASNode node)
Constructs an ASNode with the specified type and adds the child node.

Parameters:
nodeType - the type of node being constructed.
node - a child node which will be added to the new node.

ASNode

public ASNode(int nodeType,
              ASNode node1,
              ASNode node2)
Constructs an ASNode with the specified type and adds the child nodes.

Parameters:
nodeType - the type of node being constructed.
node1 - a child node which will be added to the new node.
node2 - a child node which will be added to the new node.
Method Detail

getType

public int getType()
Gets the type of the node.

Returns:
the type assigned to the node.

setType

public void setType(int type)
Sets the type of the node.

Parameters:
type - the type assigned to the node.

getBoolValue

public boolean getBoolValue()
Get the boolean value assigned to a node.

Returns:
the boolean value assigned to a node.

setBoolValue

public void setBoolValue(boolean value)
Set the boolean value assigned to a node.

Parameters:
value - a value that will be assigned to the node.

getIntValue

public int getIntValue()
Get the integer value assigned to a node.

Returns:
the integer value assigned to a node.

setIntValue

public void setIntValue(int value)
Set the integer value assigned to a node.

Parameters:
value - a value that will be assigned to the node.

getDoubleValue

public double getDoubleValue()
Get the floating-point value assigned to a node.

Returns:
the floating-point value assigned to a node.

setDoubleValue

public void setDoubleValue(double value)
Set the floating-point value assigned to a node.

Parameters:
value - a floating-point value that will be assigned to the node.

getStringValue

public java.lang.String getStringValue()
Get the string value assigned to a node.

Returns:
the string value assigned to a node.

setNumber

public void setNumber(int value)
Set the number assigned to a node.

Parameters:
value - a unique number that will be assigned to the node.

getNumber

public int getNumber()
Get the number assigned to a node.

Returns:
the number assigned to a node.

setStringValue

public void setStringValue(java.lang.String value)
Set the string value assigned to a node.

Parameters:
value - a string that will be assigned to the node.

get

public ASNode get(int index)
Returns the node at the specified index from the array of child nodes. If the index is outside the range of the array then an ArrayIndexOutOfBounds exception is thrown.

Parameters:
index - the index of the child node to return.
Returns:
the ith node in the array of children.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if (index < 0 || index >= length).

set

public void set(int i,
                ASNode aNode)
Replaces the node at position i in the array of children. If the position is outside the range of the array (i< 0 || i >= length) then an ArrayIndexOutOfBoundsException is thrown.

Parameters:
i - the index of the child node to replace.
aNode - the node to replace the ith node.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if (index < 0 || index >= length).

add

public void add(ASNode aNode)
Adds a node to the array of children. If the node is null then it is ignored.

Parameters:
aNode - the node to be added.

insert

public void insert(int index,
                   ASNode aNode)
Inserts a node at position i in the array of children. The size of the array is increased by one and the nodes from the insertion point onwards are moved to the right. If the position is outside the range of the array (i< 0 || i >= length) then an ArrayIndexOutOfBoundsException is thrown.

Parameters:
index - the index of the child node to replace.
aNode - the node to replace the ith node.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if (index < 0 || index >= length).

remove

public void remove(int index)
Removes the node at position i in the array of children. The size of the array is decreased by one and the nodes from the insertion point onwards are moved to the left. If the position is outside the range of the array (i< 0 || i >= length) then an ArrayIndexOutOfBoundsException is thrown.

Parameters:
index - the index of the child node to remove.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if (index < 0 || index >= length).

indexOf

public int indexOf(ASNode aNode)
Returns the index position of a node in the array of child nodes. If the node is not one of the current nodes children then -1 is returned.

Parameters:
aNode - the node to search the array of children for.
Returns:
the index of the node in the array of children, -1 if the node is not a child of this node.

getParent

public ASNode getParent()
Gets the parent node of this one. If no parent is define then null is returned.

Returns:
the parent node of this one.

count

public int count()
Return the number of child nodes contained by this node.

Returns:
the number of child nodes.

toString

public java.lang.String toString()
Returns a string containing the type of node, any associated value and the number of children.

Overrides:
toString in class java.lang.Object
Returns:
the string representation of the node.

displayTree

public void displayTree(java.lang.String prefix)
displayTree is used to display the structure of the node tree, with the root starting at the current node. The prefix argument is used to indent the text displayed. The level of indent is increased by appending the string " " before calling the displayTree method on each child node. This illustrates the tree structure with nodes at the same level in the tree displayed with the same level of indent.

Parameters:
prefix - the string prepended to the text representation for this node.

encode

public byte[] encode(int version)
The encode method 'compiles' the node and all child nodes into an array of action objects which represents the sequence of actions performed by the Flash Player. The actions are then encoded to generate the binary data that can be added to an encoded Flash file. The version of Flash for which the actions are generated is specified to ensure compatibility with future release of Flash. IMPORTANT: The programming model changed with Flash version 5 to support stack-based actions. Earlier versions of Flash are not support. An IllegalArgumentException will be thrown if the version is earlier than 5. Identifiers and string literals are assumed to have an encoding of UTF-8.

Parameters:
version - the version of Flash that control the actions that are generated.
Returns:
an array of bytes containing encoded action objects.
Throws:
java.lang.IllegalArgumentException - is the version is less than 5.