com.flagstone.translate
Class ASParser

java.lang.Object
  extended by com.flagstone.translate.ASParser
All Implemented Interfaces:
ASParserConstants

public final class ASParser
extends java.lang.Object
implements ASParserConstants

The ASParser framework is a collection of classes used to implement a compiler for Macromedia's ActionScript, Version 1.0. ActionScript statements are parsed to generate the byte-codes and actions that will be executed by the Flash Player. The parser translates ActionScript into a tree of ASNode objects which are then encoded to the byte-codes and actions that will be executed by the Flash Player.

     String script = "gotoFrame(1)";
 
     byte[] encodedActions = parser.parse(script).encode(swfVersion);
 
The version of Flash (swf) that the script is being encoded for must be passed to the root ASNode when the tree of nodes is encoded. The format of the data structures that represent clip and button events changed when version 6 and version 7 of the Flash File Format Specification was released. Search Paths
ActionScript supports #include directives which allow scripts to be nested. When the script is preprocessed the current working directory is checked for for the file specified. The parser also supports search paths which is a list of directories which should also be checked for the file. Paths may be specified as an array of path names or as a single string with paths separated by the system-dependent path separator character - ';' on Windows platforms and ':' for Unix compatible platforms. The string is split into separate paths and added to the array of paths searched. The directories are searched in the order they were added to the array of paths maintained by the parser. Error Handling
If an error occurs while parsing the script an exception is thrown. The ParseException contains detailed information on the token sequence that caused the parser to generate the error. This informatrion can be hard to decipher and should not be presented to an end-user of an application. The ASParser object generates more user/script-writer friendly information that can be used to identify the line in a script that generated the error.
    try 
    {
        String script = "on(mouseDown) { startDrag(); }";
        ...
        ASNode root = parser.parse(script);
        ...
    }
    catch (ParseException e)
    {
        String filename = parser.getFilename();
        String line = parser.getLine();
        String message = parser.getError();
        int lineNumber = parser.getLineNumber();
        ...
    }
 


Field Summary
static java.lang.String[] errorKeys
          Keys that identify the different types of error generated while #include directives are being processed and the script is being parsed.
 Token jj_nt
           
 boolean lookingAhead
           
 Token token
           
 ASParserTokenManager token_source
           
 
Fields inherited from interface com.flagstone.translate.ASParserConstants
ADD, AND, ASR, ASSIGN, ASSIGN_ADD, ASSIGN_AND, ASSIGN_ASR, ASSIGN_DIV, ASSIGN_LSL, ASSIGN_LSR, ASSIGN_MOD, ASSIGN_MUL, ASSIGN_OR, ASSIGN_SUB, ASSIGN_XOR, BIT_AND, BIT_NOT, BIT_OR, BIT_XOR, BOOLEAN_LITERAL, BREAK, CASE, CATCH, CONTINUE, DEC, DECIMAL_LITERAL, DEFAULT, DEFAULT_CASE, DELETE, DIGIT, DIVIDE, DO, DOUBLE_QUOTE, ELSE, EOF, EQ, EXPONENT, FINALLY, FLOATING_POINT_LITERAL, FOR, FUNCTION, GT, GTE, HEX_LITERAL, IDENTIFIER, IF, IN, INC, INSTANCEOF, INTEGER_LITERAL, LETTER, LOGICAL_AND, LOGICAL_NOT, LOGICAL_OR, LSL, LSR, LT, LTE, MINUS, MOD, MULTIPLY, NE, NEW, NULL_LITERAL, ON, ONCLIPEVENT, OR, PLUS, RETURN, SINGLE_QUOTE, STRICT_EQ, STRICT_NEQ, STRING_EQ, STRING_GE, STRING_GT, STRING_LE, STRING_LITERAL, STRING_NEQ, SWITCH, THROW, tokenImage, TRY, VAR, WHILE, WITH
 
Constructor Summary
ASParser()
          Constructs a new parser object.
ASParser(ASParserTokenManager tm)
           
ASParser(java.io.InputStream stream)
           
ASParser(java.io.InputStream stream, java.lang.String encoding)
           
ASParser(java.io.Reader stream)
           
 
Method Summary
 void add(java.lang.String path)
          Add a path to the array of pathnames.
 ASNode AdditiveExpression()
           
 ASNode AndExpression()
           
 ASNode AnonymousArray()
           
 ASNode AnonymousObject()
           
 ASNode ArgumentList()
           
 ASNode AssignmentExpression()
           
 ASNode Attribute()
           
 ASNode BlockStatement()
           
 ASNode BreakStatement()
           
 ASNode ConditionalExpression()
           
 ASNode Constructor()
           
 ASNode ContinueStatement()
           
 void disable_tracing()
           
 ASNode DoStatement()
           
 void enable_tracing()
           
 ASNode EqualityExpression()
           
 ASNode ExclusiveOrExpression()
           
 ASNode Expression()
           
 ASNode ExpressionList()
           
 ASNode ExpressionStatement()
           
 ASNode ForStatement()
           
 ASNode Function()
           
 ASNode FunctionDefinition()
           
 ParseException generateParseException()
           
 java.lang.String getError()
          Returns the key identifying the type of error that occured while of parsing a script.
 java.lang.String getFilename()
          Returns the name of the file that contained the line of code that generated an error while parsing a script.
 java.lang.String getLine()
          Returns the line of code that generated an error while parsing a script.
 int getLineNumber()
          Returns the number of the the line of code that generated an error parsing a script.
 Token getNextToken()
           
 java.util.ArrayList getPaths()
          Returns the array of path names used when searching for a file.
 Token getToken(int index)
           
 ASNode Identifier()
           
 ASNode IfStatement()
           
 ASNode InclusiveOrExpression()
           
 ASNode InstanceExpression()
           
 ASNode Literal()
           
 ASNode LogicalAndExpression()
           
 ASNode LogicalOrExpression()
           
 ASNode MethodDefinition()
           
 ASNode MultiplicativeExpression()
           
 ASNode On()
           
 ASNode OnClipEvent()
           
 ASNode parse(java.io.File file)
          Parses the file containing ActionScript.
 ASNode parse(java.lang.String script)
          Parses the ActionScript string, script.
 ASNode PostfixExpression()
           
 ASNode PrimaryExpression()
           
 ASNode PrimaryPrefix()
           
 ASNode PrimarySuffix()
           
 void ReInit(ASParserTokenManager tm)
           
 void ReInit(java.io.InputStream stream)
           
 void ReInit(java.io.InputStream stream, java.lang.String encoding)
           
 void ReInit(java.io.Reader stream)
           
 ASNode RelationalExpression()
           
 ASNode ReturnStatement()
           
 ASNode Script()
           
 void setPaths(java.util.ArrayList paths)
          Sets the array of path names used when searching for a file.
 void setPaths(java.lang.String paths)
          Sets the array of path names used when searching for a file.
 ASNode ShiftExpression()
           
 ASNode Statement()
           
 ASNode SwitchLabel()
           
 ASNode SwitchStatement()
           
 ASNode ThrowStatement()
           
 ASNode TryStatement()
           
 ASNode UnaryExpression()
           
 ASNode WhileStatement()
           
 ASNode WithStatement()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

errorKeys

public static java.lang.String[] errorKeys
Keys that identify the different types of error generated while #include directives are being processed and the script is being parsed. The key can be used with a ResourceBundle object to generate a localized string that describes the error in detail.


token_source

public ASParserTokenManager token_source

token

public Token token

jj_nt

public Token jj_nt

lookingAhead

public boolean lookingAhead
Constructor Detail

ASParser

public ASParser()
Constructs a new parser object. The current working directory "." is added to the list of directories that will be searched when looking for a file.


ASParser

public ASParser(java.io.InputStream stream)

ASParser

public ASParser(java.io.InputStream stream,
                java.lang.String encoding)

ASParser

public ASParser(java.io.Reader stream)

ASParser

public ASParser(ASParserTokenManager tm)
Method Detail

getPaths

public java.util.ArrayList getPaths()
Returns the array of path names used when searching for a file.

Returns:
an array of strings containing the names of directories to search.

setPaths

public void setPaths(java.util.ArrayList paths)
Sets the array of path names used when searching for a file.

Parameters:
paths - an array of strings containing the names of directories to search.

setPaths

public void setPaths(java.lang.String paths)
Sets the array of path names used when searching for a file. The string should contains paths which contain the system-dependent separator and pathSeparator characters.

Parameters:
paths - a string containing the names of directories to search.

add

public void add(java.lang.String path)
Add a path to the array of pathnames. The path should contain the system-dependent separator.

Parameters:
path - a string containing the path to a directory.

getFilename

public java.lang.String getFilename()
Returns the name of the file that contained the line of code that generated an error while parsing a script.

Returns:
the name of the file which contained the line of code or an empty string if the line was in the 'root' script.

getLineNumber

public int getLineNumber()
Returns the number of the the line of code that generated an error parsing a script.

Returns:
the number of the line which that triggered the error.

getLine

public java.lang.String getLine()
Returns the line of code that generated an error while parsing a script.

Returns:
the line which that triggered the error.

getError

public java.lang.String getError()
Returns the key identifying the type of error that occured while of parsing a script.

Returns:
the line which that triggered the error.

parse

public ASNode parse(java.lang.String script)
             throws ParseException
Parses the ActionScript string, script. Any nested files specified using #include directives are loaded before the complete script is parsed. The filenames and line numbers of #include'd scripts are tracked so any syntax errors are reported accurately. The character used used in the script is assumed to be UTF-8.

Parameters:
script - a String containing the ActionScript code to parse.
Throws:
ParseException - if a parsing error occurs.

parse

public ASNode parse(java.io.File file)
             throws ParseException
Parses the file containing ActionScript. Any nested files specified using #include directives are loaded before the complete script is parsed. The filenames and line numbers of #include'd scripts are tracked so any syntax errors are reported accurately. The character used used in the script is assumed to be UTF-8.

Parameters:
file - a File containing the ActionScript statements to parse.
Throws:
ParseException - if a parsing error occurs.

Literal

public final ASNode Literal()
                     throws ParseException
Throws:
ParseException

Identifier

public final ASNode Identifier()
                        throws ParseException
Throws:
ParseException

Script

public final ASNode Script()
                    throws ParseException
Throws:
ParseException

Statement

public final ASNode Statement()
                       throws ParseException
Throws:
ParseException

FunctionDefinition

public final ASNode FunctionDefinition()
                                throws ParseException
Throws:
ParseException

BlockStatement

public final ASNode BlockStatement()
                            throws ParseException
Throws:
ParseException

ExpressionStatement

public final ASNode ExpressionStatement()
                                 throws ParseException
Throws:
ParseException

IfStatement

public final ASNode IfStatement()
                         throws ParseException
Throws:
ParseException

WithStatement

public final ASNode WithStatement()
                           throws ParseException
Throws:
ParseException

WhileStatement

public final ASNode WhileStatement()
                            throws ParseException
Throws:
ParseException

DoStatement

public final ASNode DoStatement()
                         throws ParseException
Throws:
ParseException

ForStatement

public final ASNode ForStatement()
                          throws ParseException
Throws:
ParseException

BreakStatement

public final ASNode BreakStatement()
                            throws ParseException
Throws:
ParseException

ContinueStatement

public final ASNode ContinueStatement()
                               throws ParseException
Throws:
ParseException

ReturnStatement

public final ASNode ReturnStatement()
                             throws ParseException
Throws:
ParseException

OnClipEvent

public final ASNode OnClipEvent()
                         throws ParseException
Throws:
ParseException

On

public final ASNode On()
                throws ParseException
Throws:
ParseException

TryStatement

public final ASNode TryStatement()
                          throws ParseException
Throws:
ParseException

ThrowStatement

public final ASNode ThrowStatement()
                            throws ParseException
Throws:
ParseException

SwitchStatement

public final ASNode SwitchStatement()
                             throws ParseException
Throws:
ParseException

SwitchLabel

public final ASNode SwitchLabel()
                         throws ParseException
Throws:
ParseException

ExpressionList

public final ASNode ExpressionList()
                            throws ParseException
Throws:
ParseException

ArgumentList

public final ASNode ArgumentList()
                          throws ParseException
Throws:
ParseException

Expression

public final ASNode Expression()
                        throws ParseException
Throws:
ParseException

AssignmentExpression

public final ASNode AssignmentExpression()
                                  throws ParseException
Throws:
ParseException

ConditionalExpression

public final ASNode ConditionalExpression()
                                   throws ParseException
Throws:
ParseException

LogicalOrExpression

public final ASNode LogicalOrExpression()
                                 throws ParseException
Throws:
ParseException

LogicalAndExpression

public final ASNode LogicalAndExpression()
                                  throws ParseException
Throws:
ParseException

InclusiveOrExpression

public final ASNode InclusiveOrExpression()
                                   throws ParseException
Throws:
ParseException

ExclusiveOrExpression

public final ASNode ExclusiveOrExpression()
                                   throws ParseException
Throws:
ParseException

AndExpression

public final ASNode AndExpression()
                           throws ParseException
Throws:
ParseException

EqualityExpression

public final ASNode EqualityExpression()
                                throws ParseException
Throws:
ParseException

RelationalExpression

public final ASNode RelationalExpression()
                                  throws ParseException
Throws:
ParseException

InstanceExpression

public final ASNode InstanceExpression()
                                throws ParseException
Throws:
ParseException

ShiftExpression

public final ASNode ShiftExpression()
                             throws ParseException
Throws:
ParseException

AdditiveExpression

public final ASNode AdditiveExpression()
                                throws ParseException
Throws:
ParseException

MultiplicativeExpression

public final ASNode MultiplicativeExpression()
                                      throws ParseException
Throws:
ParseException

UnaryExpression

public final ASNode UnaryExpression()
                             throws ParseException
Throws:
ParseException

PostfixExpression

public final ASNode PostfixExpression()
                               throws ParseException
Throws:
ParseException

PrimaryExpression

public final ASNode PrimaryExpression()
                               throws ParseException
Throws:
ParseException

PrimaryPrefix

public final ASNode PrimaryPrefix()
                           throws ParseException
Throws:
ParseException

PrimarySuffix

public final ASNode PrimarySuffix()
                           throws ParseException
Throws:
ParseException

Function

public final ASNode Function()
                      throws ParseException
Throws:
ParseException

Constructor

public final ASNode Constructor()
                         throws ParseException
Throws:
ParseException

AnonymousArray

public final ASNode AnonymousArray()
                            throws ParseException
Throws:
ParseException

MethodDefinition

public final ASNode MethodDefinition()
                              throws ParseException
Throws:
ParseException

AnonymousObject

public final ASNode AnonymousObject()
                             throws ParseException
Throws:
ParseException

Attribute

public final ASNode Attribute()
                       throws ParseException
Throws:
ParseException

ReInit

public void ReInit(java.io.InputStream stream)

ReInit

public void ReInit(java.io.InputStream stream,
                   java.lang.String encoding)

ReInit

public void ReInit(java.io.Reader stream)

ReInit

public void ReInit(ASParserTokenManager tm)

getNextToken

public final Token getNextToken()

getToken

public final Token getToken(int index)

generateParseException

public ParseException generateParseException()

enable_tracing

public final void enable_tracing()

disable_tracing

public final void disable_tracing()