The ActionScript target closely follows the Java target. The API and runtime behavior are consistent to the extent possible.
Please send bug reports, feedback, patches to me or the antlr-interest mailing list.
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 latest Flex 3 SDK.
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.
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
.
To use a grammar T.g
:
package { import org.antlr.runtime.*; public class AntlrActionScriptTest { 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):
For grammars T.g
(parser and lexer) and TWalker.g
(the tree parser):
package { import org.antlr.runtime.*; public class AntlrActionScriptTreeWalkerTest { 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(); } } } |
Reference documentation for the runtime package can be found at http://www.antlr.org/api/ActionScript/.
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 package scope.init
- Will be inserted at the end of the constructor of the lexer/parser. Here you can setup your own instance attributes.members
- Will be inserted in the class body of the lexer/parser. This is the right place for custom methods and class attributes.-debug option
: mostly useful for integration into ANTLRWorks.