ASTParser.stg revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/* 2 [The "BSD license"] 3 Copyright (c) 2007 Kay Roepke 2010 Alan Condit 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 1. Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 2. Redistributions in binary form must reproduce the above copyright 12 notice, this list of conditions and the following disclaimer in the 13 documentation and/or other materials provided with the distribution. 14 3. The name of the author may not be used to endorse or promote products 15 derived from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27*/ 28 29/** Templates for building ASTs during normal parsing. 30 * 31 * Deal with many combinations. Dimensions are: 32 * Auto build or rewrite 33 * no label, label, list label (label/no-label handled together) 34 * child, root 35 * token, set, rule, wildcard 36 * 37 * The situation is not too bad as rewrite (->) usage makes ^ and ! 38 * invalid. There is no huge explosion of combinations. 39 */ 40@rule.setErrorReturnValue() ::= << 41/* ASTParser rule.setErrorReturnValue */ 42retval.tree = (<ASTLabelType> *)[treeAdaptor errorNode:input From:retval.start To:[input LT:-1] Exception:re]; 43<! System.out.println("<ruleName> returns "+((CommonTree)retval.tree).toStringTree()); !> 44>> 45 46// TOKEN AST STUFF 47 48/** ID and output=AST */ 49tokenRef(token, label, elementIndex, terminalOptions) ::= << 50/* ASTParser tokenRef */ 51<super.tokenRef(...)> 52<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif> 53<label>_tree = <createNodeFromToken(...)>; 54[treeAdaptor addChild:<label>_tree toTree:root_0]; 55<if(backtracking)>}<endif> 56>> 57 58/* ID! and output=AST (same as plain tokenRef) */ 59/* ASTParser tokenRefBang */ 60tokenRefBang(token,label,elementIndex,terminalOptions) ::= "<super.tokenRef(...)>" 61 62/** ID^ and output=AST */ 63tokenRefRuleRoot(token,label,elementIndex,terminalOptions) ::= << 64<super.tokenRef(...)> 65<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif> 66<label>_tree = <createNodeFromToken(...)>; 67root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:<label>_tree old:root_0]; 68<if(backtracking)>}<endif> 69>> 70 71/** ids+=ID! and output=AST */ 72tokenRefBangAndListLabel(token,label,elementIndex,terminalOptions) ::= << 73/* ASTParser tokenRefBangAndListLabel */ 74<tokenRefBang(...)> 75<listLabel(elem=label,...)> 76>> 77 78/** label+=TOKEN when output=AST but not rewrite alt */ 79tokenRefAndListLabel(token,label,elementIndex,terminalOptions) ::= << 80/* ASTParser tokenRefAndListLabel */ 81<tokenRef(...)> 82<listLabel(elem=label,...)> 83>> 84 85/** Match label+=TOKEN^ when output=AST but not rewrite alt */ 86tokenRefRuleRootAndListLabel(token,label,terminalOptions,elementIndex) ::= << 87/* ASTParser tokenRefRuleRootAndListLabel */ 88<tokenRefRuleRoot(...)> 89<listLabel(elem=label,...)> 90>> 91 92// SET AST 93 94// the match set stuff is interesting in that it uses an argument list 95// to pass code to the default matchSet; another possible way to alter 96// inherited code. I don't use the region stuff because I need to pass 97// different chunks depending on the operator. I don't like making 98// the template name have the operator as the number of templates gets 99// large but this is the most flexible--this is as opposed to having 100// the code generator call matchSet then add root code or ruleroot code 101// plus list label plus ... The combinations might require complicated 102// rather than just added on code. Investigate that refactoring when 103// I have more time. 104 105matchSet(s,label,terminalOptions,elementIndex,postmatchCode) ::= <% 106/* ASTParser matchSet */ 107<super.matchSet(postmatchCode={<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif> 108 [treeAdaptor addChild:<createNodeFromToken(...)> toTree:root_0 ];}, ...)> 109%> 110 111matchRuleBlockSet(s,label,terminalOptions,elementIndex,postmatchCode,treeLevel="0") ::= << 112/* ASTParser matchRuleBlockSet */ 113<matchSet(...)> 114>> 115 116matchSetBang(s,label,elementIndex,terminalOptions, postmatchCode) ::= "<super.matchSet(...)>" 117 118// note there is no matchSetTrack because -> rewrites force sets to be 119// plain old blocks of alts: (A|B|...|C) 120 121matchSetRuleRoot(s,label,terminalOptions,elementIndex,debug) ::= << 122/* ASTParser matchSetRuleRoot */ 123<if(label)> 124<label>=(<labelType> *)[input LT:1]; /* matchSetRuleRoot */<\n> 125<endif> 126<super.matchSet(postmatchCode={<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif> 127root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:<createNodeFromToken(...)> old:root_0];}, ...)> 128>> 129 130// RULE REF AST 131 132/** rule when output=AST */ 133ruleRef(rule,label,elementIndex,args,scope) ::= << 134/* ASTParser ruleRef */ 135<super.ruleRef(...)> 136<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif> 137[treeAdaptor addChild:[<label> getTree] toTree:root_0]; 138>> 139 140/** rule! is same as normal rule ref */ 141ruleRefBang(rule,label,elementIndex,args,scope) ::= "<super.ruleRef(...)>" 142 143/** rule^ */ 144ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= << 145/* ASTParser ruleRefRuleRoot */ 146<super.ruleRef(...)> 147<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif> 148root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:[<label> getTree] old:root_0]; 149>> 150 151/** x+=rule when output=AST */ 152ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= << 153/* ASTParser ruleRefAndListLabel */ 154<ruleRef(...)> 155<listLabel(elem = {[<label> getTree]},...)> 156>> 157 158/** x+=rule! when output=AST is a rule ref with list addition */ 159ruleRefBangAndListLabel(rule,label,elementIndex,args,scope) ::= << 160/* ASTParser ruleRefBangAndListLabel */ 161<ruleRefBang(...)> 162<listLabel(elem = {[<label> getTree]},...)> 163>> 164 165/** x+=rule^ */ 166ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= << 167/* ASTParser ruleRefRuleRootAndListLabel */ 168<ruleRefRuleRoot(...)> 169<listLabel(elem = {[<label> getTree]},...)> 170>> 171 172// WILDCARD AST 173 174wildcard(token,label,elementIndex,terminalOptions) ::= << 175/* ASTParser wildcard */ 176<super.wildcard(...)> 177<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif> 178 [treeAdaptor addChild:[[treeAdaptor createTree:<label>] retain] toTree:root_0]; 179<if(backtracking)>}<endif> 180>> 181 182wildcardBang(token,label,elementIndex,terminalOptions) ::= "<super.wildcard(...)>" 183 184wildcardRuleRoot(token,label,elementIndex,terminalOptions) ::= << 185/* ASTParser wildcardRuleRoot */ 186<super.wildcard(...)> 187<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif> 188 <label>_tree = [[treeAdaptor createTree:<label>] retain] 189 root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:<label>_tree old:root_0]; 190<if(backtracking)>}<endif> 191>> 192 193createNodeFromToken(label,terminalOptions) ::= << 194/* ASTParser createNodeFromToken */ 195<if(terminalOptions.node)> 196[ANTLR<terminalOptions.node> newANTLR<terminalOptions.node>:<label>] <! new MethodNode(IDLabel) !> 197<else> 198(<ASTLabelType> *)[[treeAdaptor createTree:<label>] retain] 199<endif> 200>> 201 202// straight from java cleanup /// 203ruleCleanUp() ::= << 204/* ASTParser ruleCleanUp */ 205<super.ruleCleanUp()> 206<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif> 207 retval.tree = (<ASTLabelType> *)[treeAdaptor rulePostProcessing:root_0]; 208 [treeAdaptor setTokenBoundaries:retval.tree From:retval.start To:retval.stopToken]; 209<if(backtracking)>}<endif> 210>> 211