📄 ast.stg
字号:
/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/group AST;@outputFile.imports() ::= <<<@super.imports()><if(!TREE_PARSER)><! tree parser would already have imported !>import org.antlr.runtime.tree.*;<\n><endif>>>@genericParser.members() ::= <<<@super.members()><parserMembers()>>>/** Add an adaptor property that knows how to build trees */parserMembers() ::= <<protected TreeAdaptor adaptor = new CommonTreeAdaptor();<\n>public void setTreeAdaptor(TreeAdaptor adaptor) { this.adaptor = adaptor;}public TreeAdaptor getTreeAdaptor() { return adaptor;}>>@returnScope.ruleReturnMembers() ::= <<<ASTLabelType> tree;public Object getTree() { return tree; }>>/** Add a variable to track rule's return AST */ruleDeclarations() ::= <<<super.ruleDeclarations()><ASTLabelType> root_0 = null;<\n>>>ruleLabelDefs() ::= <<<super.ruleLabelDefs()><ruleDescriptor.tokenLabels:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n"><ruleDescriptor.tokenListLabels:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n">>>ruleCleanUp() ::= <<<super.ruleCleanUp()><if(ruleDescriptor.hasMultipleReturnValues)><if(backtracking)>if ( backtracking==0 ) {<\n><endif> retval.tree = (<ASTLabelType>)adaptor.rulePostProcessing(root_0); adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);<if(backtracking)>}<endif><endif>>>/** When doing auto AST construction, we must define some variables; * These should be turned off if doing rewrites. This must be a "mode" * as a rule could have both rewrite and AST within the same alternative * block. */@alt.declarations() ::= <<<if(autoAST)><if(outerAlt)>root_0 = (<ASTLabelType>)adaptor.nil();<\n><else><ASTLabelType> root_<blockLevel> = (<ASTLabelType>)adaptor.nil();<\n><endif><endif>>>@alt.cleanup() ::= <<<if(autoAST)><if(!outerAlt)><if(backtracking)>if ( backtracking==0 ) <endif>adaptor.addChild(root_<enclosingBlockLevel>, root_<blockLevel>);<\n><endif><endif>>>// TOKEN AST STUFF/** ID and output=AST */tokenRef(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( backtracking==0 ) {<endif><label>_tree = (<ASTLabelType>)adaptor.create(<label>);adaptor.addChild(root_<blockLevel>, <label>_tree);<if(backtracking)>}<endif>>>/** ID! and output=AST (same as plain tokenRef) */tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"/** ID^ and output=AST */tokenRefRoot(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( backtracking==0 ) {<endif><label>_tree = (<ASTLabelType>)adaptor.create(<label>);root_<blockLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>_tree, root_<blockLevel>);<if(backtracking)>}<endif>>>/** ID^^ and output=AST */tokenRefRuleRoot(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( backtracking==0 ) {<endif><label>_tree = (<ASTLabelType>)adaptor.create(<label>);root_0 = (<ASTLabelType>)adaptor.becomeRoot(<label>_tree, root_0);<if(backtracking)>}<endif>>>/** ids+=ID! and output=AST */tokenRefBangAndListLabel(token,label,elementIndex) ::= <<<tokenRefBang(...)><listLabel(...)>>>/** label+=TOKEN when output=AST but not rewrite alt */tokenRefAndListLabel(token,label,elementIndex) ::= <<<tokenRef(...)><listLabel(...)>>>/** Match label+=TOKEN^ when output=AST but not rewrite alt */tokenRefRootAndListLabel(token,label,elementIndex) ::= <<<tokenRefRoot(...)><listLabel(...)>>>/** Match label+=TOKEN^^ when output=AST but not rewrite alt */tokenRefRuleRootAndListLabel(token,label,elementIndex) ::= <<<tokenRefRuleRoot(...)><listLabel(...)>>>/** ID but track it for use in a rewrite rule */tokenRefTrack(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( backtracking==0 ) <endif>list_<token>.add(<label>);<\n>>>/** ids+=ID and track it for use in a rewrite rule; adds to ids *and* * to the tracking list list_ID for use in the rewrite. */tokenRefTrackAndListLabel(token,label,elementIndex) ::= <<<tokenRefTrack(...)><listLabel(...)>>>// SET AST// the match set stuff is interesting in that it uses an argument list// to pass code to the default matchSet; another possible way to alter// inherited code. I don't use the region stuff because I need to pass// different chunks depending on the operator. I don't like making// the template name have the operator as the number of templates gets// large but this is the most flexible--this is as opposed to having// the code generator call matchSet then add root code or ruleroot code// plus list label plus ... The combinations might require complicated// rather than just added on code. Investigate that refactoring when// I have more time.matchSet(s,label,elementIndex,postmatchCode) ::= <<<super.matchSet(..., postmatchCode={<if(backtracking)>if ( backtracking==0 ) <endif>adaptor.addChild(root_<blockLevel>, adaptor.create(<label>));})>>>matchSetRoot(s,label,elementIndex,debug) ::= <<<super.matchSet(..., postmatchCode={<if(backtracking)>if ( backtracking==0 ) <endif>root_<blockLevel> = (<ASTLabelType>)adaptor.becomeRoot(adaptor.create(<label>), root_<blockLevel>);})>>>matchSetRuleRoot(s,label,elementIndex,debug) ::= <<<super.matchSet(..., postmatchCode={<if(backtracking)>if ( backtracking==0 ) <endif>root_0 = (<ASTLabelType>)adaptor.becomeRoot(adaptor.create(<label>), root_0);})>>>// RULE REF AST/** rule when output=AST */ruleRef(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><if(backtracking)>if ( backtracking==0 ) <endif>adaptor.addChild(root_<blockLevel>, <label>.tree);>>/** rule! is same as normal rule ref */ruleRefBang(rule,label,elementIndex,args) ::= "<super.ruleRef(...)>"/** rule^ */ruleRefRoot(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><if(backtracking)>if ( backtracking==0 ) <endif>root_<blockLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>.tree, root_<blockLevel>);>>/** rule^^ */ruleRefRuleRoot(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><if(backtracking)>if ( backtracking==0 ) <endif>root_0 = (<ASTLabelType>)adaptor.becomeRoot(<label>.tree, root_0);>>/** rule when output=AST and tracking for rewrite */ruleRefTrack(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><if(backtracking)>if ( backtracking==0 ) <endif>list_<rule>.add(<label>.tree);>>/** x+=rule when output=AST and tracking for rewrite */ruleRefTrackAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRefTrack(...)><listLabel(...)>>>/** x+=rule when output=AST */ruleRefAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRef(...)><listLabel(...)>>>/** x+=rule! when output=AST is a rule ref with list addition */ruleRefBangAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRefBang(...)><listLabel(...)>>>/** x+=rule^ */ruleRefRootAndListLabel(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)>root_<blockLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>.tree, root_<blockLevel>);<\n><listLabel(...)>>>/** x+=rule^^ */ruleRefRuleRootAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRefRuleRoot(...)><listLabel(...)>>>// WILDCARD ASTwildcard(label,elementIndex) ::= <<<super.wildcard(...)>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -