⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ast.stg

📁 antlr最新版本V3源代码
💻 STG
📖 第 1 页 / 共 2 页
字号:
/* [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;/** Add an adaptor property that knows how to build trees */@headerFile.members() ::= <</* @headerFile.members() */pANTLR3_BASE_TREE_ADAPTOR	adaptor;pANTLR3_VECTOR_FACTORY		vectors;/* End @headerFile.members() */>>/** Install the tree adpator inteface pointer and anything else that  *  tree parsers and producers require. */@genericParser.apifuncs() ::= <<<if(PARSER)>ADAPTOR	= ANTLR3_TREE_ADAPTORNew(instream->tstream->tokenSource->strFactory);<\n><endif>ctx->vectors	= antlr3VectorFactoryNew(64);>>@genericParser.cleanup() ::= <<ctx->vectors->close(ctx->vectors);<if(PARSER)>/* We created the adaptor so we must free it */ADAPTOR->free(ADAPTOR);<endif>>>@returnScope.ruleReturnMembers() ::= <</* @returnScope.ruleReturnMembers() */pANTLR3_BASE_TREE	tree;/* End @returnScope.ruleReturnMembers() */>>/** Add a variable to track rule's return AST */ruleDeclarations() ::= <</* ruleDeclarations() */<super.ruleDeclarations()>pANTLR3_BASE_TREE root_0;<\n>/* End ruleDeclarations() */>>ruleInitializations() ::= <<<super.ruleInitializations()>root_0 = NULL;<\n>>>ruleLabelDefs() ::= <<<super.ruleLabelDefs()><ruleDescriptor.tokenLabels:{pANTLR3_BASE_TREE <it.label.text>_tree;}; separator="\n"><ruleDescriptor.tokenListLabels:{pANTLR3_BASE_TREE <it.label.text>_tree;}; separator="\n"><ruleDescriptor.allTokenRefsInAltsWithRewrites    :{pANTLR3_REWRITE_RULE_TOKEN_STREAM stream_<it>;}; separator="\n"><ruleDescriptor.allRuleRefsInAltsWithRewrites    :{pANTLR3_REWRITE_RULE_SUBTREE_STREAM stream_<it>;}; separator="\n">>>ruleLabelInitializations() ::= <<<super.ruleLabelInitializations()><ruleDescriptor.tokenLabels:{<it.label.text>_tree   = NULL;}; separator="\n"><ruleDescriptor.tokenListLabels:{<it.label.text>_tree   = NULL;}; separator="\n"><ruleDescriptor.allTokenRefsInAltsWithRewrites    :{stream_<it>   = antlr3RewriteRuleTokenStreamNewAE(ADAPTOR,(pANTLR3_UINT8)"token <it>");}; separator="\n"><ruleDescriptor.allRuleRefsInAltsWithRewrites    :{stream_<it>=antlr3RewriteRuleSubtreeStreamNewAE(ADAPTOR,(pANTLR3_UINT8)"rule <it>");}; separator="\n"><if(ruleDescriptor.hasMultipleReturnValues)>retval.tree  = NULL;<endif>>>/**  a rule label including default value */ruleLabelInitVal(label) ::= <<<super.ruleLabelInitVal(...)><label.label.text>.tree = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;<\n>>>ruleCleanUp() ::= <<<super.ruleCleanUp()><if(ruleDescriptor.hasMultipleReturnValues)><if(backtracking)>if ( BACKTRACKING==0 ) {<\n><endif><if(!ruleDescriptor.isSynPred)>	retval.stop = LT(-1);<\n><endif>	retval.tree = ADAPTOR->rulePostProcessing(ADAPTOR, root_0);	ADAPTOR->setTokenBoundaries(ADAPTOR, retval.tree, retval.start, retval.stop);<if(backtracking)>}<endif><ruleDescriptor.allTokenRefsInAltsWithRewrites    :{stream_<it>->free(stream_<it>);}; separator="\n"><ruleDescriptor.allRuleRefsInAltsWithRewrites    :{stream_<it>->free(stream_<it>);}; separator="\n"><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)><endif><endif>>>@alt.initializations() ::= <<<if(autoAST)><if(outerAlt)>root_0 = ADAPTOR->nil(ADAPTOR);<\n><endif><endif>>>// TOKEN AST STUFF/** ID and output=AST */tokenRef(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( BACKTRACKING==0 ) {<endif><label>_tree = ADAPTOR->create(ADAPTOR, <label>);ADAPTOR->addChild(ADAPTOR, root_0, <label>_tree);<if(backtracking)>}<endif>>>/** ID! and output=AST (same as plain tokenRef) */tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"/** ID^ and output=AST */tokenRefRuleRoot(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( BACKTRACKING==0 ) {<endif><label>_tree = ADAPTOR->create(ADAPTOR, <label>);root_0 = ADAPTOR->becomeRoot(ADAPTOR, <label>_tree, root_0);<if(backtracking)>}<endif>>>/** ids+=ID! and output=AST */tokenRefBangAndListLabel(token,label,elementIndex) ::= <<<tokenRefBang(...)><listLabel(elem=label,...)>>>/** label+=TOKEN when output=AST but not rewrite alt */tokenRefAndListLabel(token,label,elementIndex) ::= <<<tokenRef(...)><listLabel(elem=label,...)>>>/** Match label+=TOKEN^ when output=AST but not rewrite alt */tokenRefRuleRootAndListLabel(token,label,elementIndex) ::= <<<tokenRefRuleRoot(...)><listLabel(elem=label,...)>>>/** ID but track it for use in a rewrite rule */tokenRefTrack(token,label,elementIndex) ::= <<<super.tokenRef(...)><if(backtracking)>if ( BACKTRACKING==0 ) <endif>stream_<token>->add(stream_<token>, <label>, NULL);<\n>>>/** ids+=ID and track it for use in a rewrite rule; adds to ids *and* *  to the tracking list stream_ID for use in the rewrite. */tokenRefTrackAndListLabel(token,label,elementIndex) ::= <<<tokenRefTrack(...)><listLabel(elem=label,...)>>>// 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(ADAPTOR, root_0, ADAPTOR->create(ADAPTOR, <label>));})>>>matchSetBang(s,label,elementIndex,postmatchCode) ::= "<super.matchSet(...)>"// note there is no matchSetTrack because -> rewrites force sets to be// plain old blocks of alts: (A|B|...|C)matchSetRuleRoot(s,label,elementIndex,debug) ::= <<<super.matchSet(..., postmatchCode={<if(backtracking)>if ( BACKTRACKING==0 ) <endif>root_0 = ADAPTOR->becomeRoot(ADAPTOR, ADAPTOR->create(ADAPTOR, <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(ADAPTOR, root_0, <label>.tree);>>/** rule! is same as normal rule ref */ruleRefBang(rule,label,elementIndex,args) ::= "<super.ruleRef(...)>"/** rule^ */ruleRefRuleRoot(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><if(backtracking)>if ( BACKTRACKING==0 ) <endif>root_0 = ADAPTOR->becomeRoot(ADAPTOR, <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>stream_<rule>->add(stream_<rule>, <label>.tree, NULL);>>/* How to accumulate lists when we are doing rewrite tracking... */listLabelTrack(label) ::= <</* listLabelTrack(label) */if (list_<label> == NULL){    list_<label>=ctx->vectors->newVector(ctx->vectors);}list_<label>->add(list_<label>, <label>.tree, NULL);>>/* How to accumulate lists of rule outputs (only allowed with AST * option but if the user is going to walk the tree, they will want * all their custom elements from rule returns. * * Normally, we use inline structures (which the compiler lays down * code to copy from heap allocations. However, here we want to accumulate copies * of the returned structures because we are adding them to a list. This only makes sense if the * grammar is not rewriting the tree as a tree rewwrite onnly preserves the tree, not the object/structure * returned from the rule. The rewrite will extract the tree pointer. However, if we are not going to  * do a tree re-write, then the user may wish to iterate the structures returned by the rule in  * action code and will expect the user defined returns[] elements to be available when they do this. * Hence we cannot just preserve the tree that was returned. So, we must copy the local structure and provide  * a function that can free the allocated space. We cannot know how to free user allocated elements and * presume that the user will know to do this using their own factories for the structures they allocate. */listLabelAST(label) ::= <<if (list_<label> == NULL){    list_<label>=ctx->vectors->newVector(ctx->vectors);}{    RETURN_TYPE_<label> * tcopy;    tcopy = ANTLR3_MALLOC(sizeof(RETURN_TYPE_<label>)); /* Note no memory allocation checks! */    ANTLR3_MEMMOVE((void *)(tcopy), (const void *)&<label>, sizeof(RETURN_TYPE_<label>));    list_<label>->add(list_<label>, tcopy, freeScope);  /* Add whatever the return type is */<\n>}>>/** x+=rule when output=AST and tracking for rewrite */ruleRefTrackAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRefTrack(...)><listLabelTrack(...)>>>/** x+=rule when output=AST */ruleRefAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRef(...)><listLabelAST(...)>>>/** x+=rule! when output=AST is a rule ref with list addition */ruleRefBangAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRefBang(...)><listLabelAST(...)>>>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -