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

📄 ast.stg

📁 antlr最新版本V3源代码
💻 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.*//* in sync with Java/AST.stg revision 36 */group AST;finishedBacktracking(block) ::= <<<if(backtracking)>if self.backtracking == 0:    <block><else><block><endif>>>@outputFile.imports() ::= <<<@super.imports()><if(!TREE_PARSER)><! tree parser would already have imported !>from antlr3.tree import *<\n><endif>>>@genericParser.members() ::= <<<@super.members()><parserMembers()>>>/** Add an adaptor property that knows how to build trees */parserMembers() ::= <<self.adaptor = CommonTreeAdaptor()<\n>>>@returnScope.ruleReturnMembers() ::= <<self.tree = None>>/** Add a variable to track rule's return AST */ruleDeclarations() ::= <<<super.ruleDeclarations()>root_0 = None<\n>>>ruleLabelDefs() ::= <<<super.ruleLabelDefs()><ruleDescriptor.tokenLabels:{<it.label.text>_tree = None}; separator="\n"><ruleDescriptor.tokenListLabels:{<it.label.text>_tree = None}; separator="\n"><ruleDescriptor.allTokenRefsInAltsWithRewrites    :{stream_<it> = RewriteRuleTokenStream(self.adaptor, "token <it>")}; separator="\n"><ruleDescriptor.allRuleRefsInAltsWithRewrites    :{stream_<it> = RewriteRuleSubtreeStream(self.adaptor, "rule <it>")}; separator="\n">>>ruleCleanUp() ::= <<<super.ruleCleanUp()><if(ruleDescriptor.hasMultipleReturnValues)><finishedBacktracking({retval.tree = self.adaptor.rulePostProcessing(root_0)self.adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop)})><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 = self.adaptor.nil()<\n><else><endif><endif>>>// TOKEN AST STUFF/** ID and output=AST */tokenRef(token,label,elementIndex) ::= <<<super.tokenRef(...)><label>_tree = self.adaptor.createWithPayload(<label>)self.adaptor.addChild(root_0, <label>_tree)<\n>>>/** ID! and output=AST (same as plain tokenRef) */tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"/** ID^ and output=AST */tokenRefRuleRoot(token,label,elementIndex) ::= <<<super.tokenRef(...)><finishedBacktracking({<label>_tree = self.adaptor.createWithPayload(<label>)root_0 = self.adaptor.becomeRoot(<label>_tree, root_0)})>>>/** 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 */tokenRefRuleRootAndListLabel(token,label,elementIndex) ::= <<<tokenRefRuleRoot(...)><listLabel(...)>>>/** ID but track it for use in a rewrite rule */tokenRefTrack(token,label,elementIndex) ::= <<<super.tokenRef(...)><finishedBacktracking({stream_<token>.add(<label>)})>>>/** 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(...)>>>// 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={<finishedBacktracking({self.adaptor.addChild(root_0, self.adaptor.createWithPayload(<label>))})>})>>>matchSetBang(s,label,elementIndex,postmatchCode) ::= "<super.matchSet(...)>"matchSetRuleRoot(s,label,elementIndex,debug) ::= <<<super.matchSet(..., postmatchCode={<finishedBacktracking({<!FIXME(40,untested)!>root_0 = self.adaptor.becomeRoot(self.adaptor.createWithPayload(<label>), root_0)})>})>>>// RULE REF AST/** rule when output=AST */ruleRef(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><finishedBacktracking({self.adaptor.addChild(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(...)><finishedBacktracking({root_0 = self.adaptor.becomeRoot(<label>.tree, root_0)})>>>/** rule when output=AST and tracking for rewrite */ruleRefTrack(rule,label,elementIndex,args) ::= <<<super.ruleRef(...)><finishedBacktracking({stream_<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^ */ruleRefRuleRootAndListLabel(rule,label,elementIndex,args) ::= <<<ruleRefRuleRoot(...)><listLabel(...)>>>// WILDCARD ASTwildcard(label,elementIndex) ::= <<<super.wildcard(...)><finishedBacktracking({<label>_tree = self.adaptor.createWithPayload(<label>)self.adaptor.addChild(root_0, <label>_tree)})>>>wildcardBang(label,elementIndex) ::= "<super.wildcard(...)>"wildcardRuleRoot(label,elementIndex) ::= <<<super.wildcard(...)><finishedBacktracking({<label>_tree = self.adaptor.createWithPayload(<label>)root_0 = self.adaptor.becomeRoot(<label>_tree, root_0)})>>>// TODO: ugh, am i really missing the combinations for Track and ListLabel?// there's got to be a better way// R e w r i t erewriteCode(	alts, description,	referencedElementsDeep, // ALL referenced elements to right of ->	referencedTokenLabels,	referencedTokenListLabels,	referencedRuleLabels,	referencedRuleListLabels,	rewriteBlockLevel, enclosingTreeLevel, treeLevel) ::=<<# AST Rewrite# elements: <referencedElementsDeep; separator=", "># token labels: <referencedTokenLabels; separator=", "># rule labels: <referencedRuleLabels; separator=", "># token list labels: <referencedTokenListLabels; separator=", "># rule list labels: <referencedRuleListLabels; separator=", "><finishedBacktracking({<prevRuleRootRef()>.tree = root_0<rewriteCodeLabels()>root_0 = self.adaptor.nil()<first(alts):rewriteAltFirst(); anchor><rest(alts):{a | el<rewriteAltRest(a)>}; anchor, separator="\n\n">})>>>rewriteCodeLabels() ::= <<<referencedTokenLabels    :{stream_<it> = RewriteRuleTokenStream(self.adaptor, "token <it>", <it>)};    separator="\n"><referencedTokenListLabels    :{stream_<it> = RewriteRuleTokenStream(self.adaptor, "token <it>", list_<it>)};    separator="\n"><referencedRuleLabels    :{if <it> is not None:    stream_<it> = RewriteRuleSubtreeStream(self.adaptor, "token <it>", <it>.tree)else:    stream_<it> = RewriteRuleSubtreeStream(self.adaptor, "token <it>", None)};    separator="\n"><referencedRuleListLabels    :{stream_<it> = RewriteRuleSubtreeStream(self.adaptor, "token <it>", list_<it>)};    separator="\n">>>/** Generate code for an optional rewrite block; note it uses the deep ref'd element  *  list rather shallow like other blocks.  */rewriteOptionalBlock(	alt,rewriteBlockLevel,	referencedElementsDeep, // all nested refs	referencedElements, // elements in immediately block; no nested blocks	description) ::=<<# <fileName>:<description>if <referencedElementsDeep:{el | stream_<el>.hasNext()}; separator=" or ">:    <alt><referencedElementsDeep:{el | stream_<el>.reset();<\n>}>>>rewriteClosureBlock(	alt,rewriteBlockLevel,	referencedElementsDeep, // all nested refs	referencedElements, // elements in immediately block; no nested blocks	description) ::=<<# <fileName>:<description>while <referencedElements:{el | stream_<el>.hasNext()}; separator=" or ">:    <alt><referencedElements:{el | stream_<el>.reset();<\n>}>>>rewritePositiveClosureBlock(	alt,rewriteBlockLevel,	referencedElementsDeep, // all nested refs	referencedElements, // elements in immediately block; no nested blocks	description) ::=<<# <fileName>:<description>if not (<referencedElements:{el | stream_<el>.hasNext()}; separator=" or ">):    raise RewriteEarlyExitException()while <referencedElements:{el | stream_<el>.hasNext()}; separator=" or ">:    <alt><referencedElements:{el | stream_<el>.reset()<\n>}>>>rewriteAltRest(a) ::= <<<if(a.pred)>if <a.pred>:    # <a.description>    <a.alt><else>se: <! little hack to get if .. elif .. else block right !>    # <a.description>    <a.alt><endif>>>rewriteAltFirst(a) ::= <<<if(a.pred)>if <a.pred>:    # <a.description>    <a.alt><else># <a.description><a.alt><endif>>>/** For empty rewrites: "r : ... -> ;" */rewriteEmptyAlt() ::= "root_0 = self.adaptor.nil()"rewriteTree(root,children,description,enclosingTreeLevel,treeLevel) ::= <<# <fileName>:<description>root_<treeLevel> = self.adaptor.nil()<root:rewriteElement()><children:rewriteElement()>self.adaptor.addChild(root_<enclosingTreeLevel>, root_<treeLevel>)<\n>>>rewriteElementList(elements) ::= "<elements:rewriteElement()>"rewriteElement(e) ::= <<<@pregen()><e.el>>>/** Gen ID or ID[args] */rewriteTokenRef(token,elementIndex,args) ::= <<self.adaptor.addChild(root_<treeLevel>, <if(args)>self.adaptor.createFromToken(<token>,<args; separator=", ">)<else>stream_<token>.next()<endif>)<\n>>>/** Gen $label ... where defined via label=ID */rewriteTokenLabelRef(label,elementIndex) ::= <<self.adaptor.addChild(root_<treeLevel>, stream_<label>.next())<\n>>>/** Gen $label ... where defined via label+=ID */rewriteTokenListLabelRef(label,elementIndex) ::= <<self.adaptor.addChild(root_<treeLevel>, stream_<label>.next())<\n>>>/** Gen ^($label ...) */rewriteTokenLabelRefRoot(label,elementIndex) ::= <<root_<treeLevel> = self.adaptor.becomeRoot(stream_<label>.next(), root_<treeLevel>)<\n>>>/** Gen ^(ID ...) or ^(ID[args] ...) */rewriteTokenRefRoot(token,elementIndex,args) ::= <<root_<treeLevel> = self.adaptor.becomeRoot(<if(args)>self.adaptor.createFromToken(<token>,<args; separator=", ">)<else>stream_<token>.next()<endif>, root_<treeLevel>)<\n>>>rewriteImaginaryTokenRef(args,token,elementIndex) ::= <<<! need to call different adaptor.create*() methods depending of argument count !>self.adaptor.addChild(root_<treeLevel>, <if (!args)>self.adaptor.createFromType(<token>, "<token>")<else><if (!rest(args))>self.adaptor.createFromToken(<token>, <first(args)>)<else><if (!rest(rest(args)))>self.adaptor.createFromToken(<token>, <first(args)>, <first(rest(args))>)<endif><endif><endif>)<\n>>>rewriteImaginaryTokenRefRoot(args,token,elementIndex) ::= <<<! need to call different adaptor.create*() methods depending of argument count !>root_<treeLevel> = self.adaptor.becomeRoot(<if (!args)>self.adaptor.createFromType(<token>, "<token>")<else><if (!rest(args))>self.adaptor.createFromToken(<token>, <first(args)>)<else><if (!rest(rest(args)))>self.adaptor.createFromToken(<token>, <first(args)>, <first(rest(args))>)<endif><endif><endif>, root_<treeLevel>)<\n>>>/** plain -> {foo} action */rewriteAction(action) ::= <<<!FIXME(96,untested)!>root_0 = <action><\n>>>/** What is the name of the previous value of this rule's root tree?  This *  let's us refer to $rule to mean previous value.  I am reusing the *  variable 'tree' sitting in retval struct to hold the value of root_0 right *  before I set it during rewrites.  The assign will be to retval.tree. */prevRuleRootRef() ::= "retval"rewriteRuleRef(rule) ::= <<self.adaptor.addChild(root_<treeLevel>, stream_<rule>.next())<\n>>>rewriteRuleRefRoot(rule) ::= <<root_<treeLevel> = self.adaptor.becomeRoot(stream_<rule>.nextNode(), root_<treeLevel>)<\n>>>rewriteNodeAction(action) ::= <<self.adaptor.addChild(root_<treeLevel>, <action>)<\n>>>rewriteNodeActionRoot(action) ::= <<root_<treeLevel> = self.adaptor.becomeRoot(<action>, root_<treeLevel>)<\n>>>/** Gen $ruleLabel ... where defined via ruleLabel=rule */rewriteRuleLabelRef(label) ::= <<self.adaptor.addChild(root_<treeLevel>, stream_<label>.next())<\n>>>/** Gen $ruleLabel ... where defined via ruleLabel+=rule */rewriteRuleListLabelRef(label) ::= <<self.adaptor.addChild(root_<treeLevel>, stream_<label>.next().tree)<\n>>>/** Gen ^($ruleLabel ...) where ruleLabel=rule */rewriteRuleLabelRefRoot(label) ::= <<root_<treeLevel> = self.adaptor.becomeRoot(stream_<label>.nextNode(), root_<treeLevel>)<\n>>>/** Gen ^($ruleLabel ...) where ruleLabel+=rule */rewriteRuleListLabelRefRoot(label) ::= <<root_<treeLevel> = self.adaptor.becomeRoot(stream_<label>.nextNode().tree, root_<treeLevel>)<\n>>>

⌨️ 快捷键说明

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