The ActionScript code generation target

The ActionScript code generation target

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 George Scott (preferable) 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 latest Flex 3.2 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. Note, that the library contains some Adobe AIR class references, to use the library in a pure Flex project use the -library-path compiler option rather than the -include-library option when compiling from the command line.

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.

Specifying an ActionScript package for your recognizer

You can specify that your generated recognizer should be declared within a specific package as shown below. By default all recognizers are generated as top-level types with no enclosing package.

grammar MyGrammar;

options
{
    language=ActionScript;
}

@parser::package { My.Custom.NameSpace.For.Parser.In.Combined.Grammar } // Or just @package { ... }

@lexer::package { My.Custom.NameSpace.For.Lexer.In.Combined.Grammar }

// rest of grammar follows
....
lexer grammar MyGrammar;

options
{
    language=ActionScript;
}

@package { My.Custom.NameSpace.For.Lexer }

// rest of grammar follows
....
parser grammar MyGrammar;

options
{
    language=ActionScript;
}

@package { My.Custom.NameSpace.For.Parser }

// rest of grammar follows
....
tree grammar MyGrammar;

options
{
    language=ActionScript;
}

@package { My.Custom.NameSpace.For.TreeParser }

// rest of grammar follows
....

Using the generated classes

To use a grammar T.g:

package {

	import org.antlr.runtime.*;

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

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

For complex parsers, you may want to avoid creating the lexers and parsers each time you want to parse input. In this case you can do the following, which will reset the lexer and parser by setting the charStream or tokenStream property (as appropriate):

package {

	import org.antlr.runtime.*;

	public class AntlrActionScriptTest {

		private static var lexer:TLexer = new TLexer(null);
		private static var parser:TParser = new TParser(null);

		public function parseInput(input:String):void {
			lexer.charStream = new ANTLRStringStream(input);
			parser.tokenStream = new CommonTokenStream(lexer);
			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 org.antlr.runtime.*;
	import org.antlr.runtime.tree.*;

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

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

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

			var nodes:CommonTreeNodeStream = new 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/.

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 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.

Unsupported features

  • -debug option: mostly useful for integration into ANTLRWorks.
  • output=template: StringTemplate has not been ported to ActionScript, so template-based recognizers are not supported.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值