Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

The ActionScript code generation target

Coming soon in in V3.1

The ActionScript target is targeted for inclusion in the ANTLR 3.1 release. Content in this page borrows liberally from Python Target page and is in the process of being edited. It currently contains references to Python and some Python examples which are being convereted..

Please note that the ActionScript target is (compared to most other targets) rather young. I would consider it to be in alpha state. This means that most parts are working (big exception are template output), but bugs and problems are to be expected and documentation is pretty poor. It still has to prove itself in a real world application (which is currently being done).

Both the runtime module and the code generation templates should now be feature complete and in sync with the Java target (Version 3.0.1), except for the features listed below. But large parts of the runtime are still untested.

See this Example for a working tree-walking grammer.

Please send bug reports, feedback, patches to me or the antlr-interest mailing list.

Requirements

The runtime generates ActionScript 3 code designed to work with Flex version 2 or higher from Adobe. The runtime binary libraries are all compiled with the Flex SDK 3 Beta 2.

To use generated code, you'll need to include ActionScript runtime package antlr3.swc in your library path. There are no other dependencies beyond the standard Flex libraries.

Usage

Selecting ActionScript output

Just add language=ActionScript; to the options section of your grammar:

grammar T;
options {
    language=ActionScript;
    [other options]
}

...

For a grammar T.g ANTLR3 will then create the files TLexer.as and TParser.as which contain the classes TLexer and TParser (or just one of those, if you have a pure lexer/parser). For tree parsers, ANTLR3 creates T.as containing the class T.

Using the generated classes

To use a grammar T.g:

package {

	import flash.display.Sprite;

	import org.antlr.runtime.*;

	public class AntlrActionScriptTest extends Sprite {
		public function AntlrActionScriptTest ( ):void {
			var lexer:TLexer = new TLexer ( new ANTLRStringStream ( input ) );
			var tokens:CommonTokenStream = CommonTokenStream ( lexer );

			var parser:TParser = TParser ( tokens );
			parser.entry_rule ( );
 		}
	}
}

If you want to access the tokens types in your code, you'll have to import and access them from the lexer or parser module (e.g. TLexer.EOF, TLexer.IDENTIFIER):

Using tree parsers

For grammars T.g (parser and lexer) and TWalker.g (the tree parser):

package {

	import flash.display.Sprite;

	import org.antlr.runtime.*;

	public class AntlrActionScriptTreeWalkerTest extends Sprite {
		public function AntlrActionScriptTest ( ):void {
			var lexer:TLexer = new TLexer ( new ANTLRStringStream ( input ) );
			var tokens:CommonTokenStream = CommonTokenStream ( lexer );

			var parser:TParser = TParser ( tokens );
			var r:ParserRuleReturnScope = parser.entry_rule ( );

			// This is the root of the AST.
			var root:Tree = r.tree;

			var nodes:CommonTreeNodeStream = CommonTreeNodeStream ( root );
			nodes.tokenStream = tokens;
			var walker:TWalker = new TWalker ( nodes );
			walker.entry_rule ( );

 		}
	}
}

API documentation

Reference documentation for the runtime package can be found at http://www.antlr.org/api/ActionScript/. Note: need to upload ActionScript HTML docs...

Actions

This target currently supports the action scopes @lexer, @parser and @treeparser for global actions. The following action names are known:

  • package -Wrap the generated classes with the specified package.
  • header - Will be inserted right after ANTLRs own imports at the top of the generated file. Use it for import statements or any other functions/classes which you need in the module scope.
  • init - Will be inserted at the end of the _init_ method of the lexer/parser. Here you can setup your own instance attributes.
  • members - Will be inserted in the class body of the lexer/parser right after _init_. This is the right place for custom methods and class attributes.

For rules the additional action @decorate is recognized. The contents are placed right before the rule method and can be used for decorators.

r
@decorate { @logThis }
: s ;

will create something like

@logThis
def r(self):
    ...

Caveats

TBD.

Unsupported features

  • -debug option: mostly useful for integration into ANTLRWorks.
  • No labels