1 /* 2 [The "BSD license"] 3 Copyright (c) 2005-2006 Terence Parr 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 41 finishedBacktracking(block) ::= << 42 <if(backtracking)> 43 if <actions.(actionScope).synpredgate>: 44 <block> 45 <else> 46 <block> 47 <endif> 48 >> 49 50 @ruleBody.setErrorReturnValue() ::= << 51 retval.tree = self._adaptor.errorNode(self.input, retval.start, self.input.LT(-1), re) 52 >> 53 54 // TOKEN AST STUFF 55 56 /** ID and output=AST */ 57 tokenRef(token,label,elementIndex,terminalOptions) ::= << 58 <super.tokenRef(...)> 59 <finishedBacktracking({ 60 <label>_tree = <createNodeFromToken(...)> 61 self._adaptor.addChild(root_0, <label>_tree) 62 })> 63 >> 64 65 /** ID! and output=AST (same as plain tokenRef) */ 66 tokenRefBang(token,label,elementIndex,terminalOptions) ::= "<super.tokenRef(...)>" 67 68 /** ID^ and output=AST */ 69 tokenRefRuleRoot(token,label,elementIndex,terminalOptions) ::= << 70 <super.tokenRef(...)> 71 <finishedBacktracking({ 72 <label>_tree = <createNodeFromToken(...)> 73 root_0 = self._adaptor.becomeRoot(<label>_tree, root_0) 74 })> 75 >> 76 77 /** ids+=ID! and output=AST */ 78 tokenRefBangAndListLabel(token,label,elementIndex,terminalOptions) ::= << 79 <tokenRefBang(...)> 80 <listLabel(elem=label,...)> 81 >> 82 83 /** label+=TOKEN when output=AST but not rewrite alt */ 84 tokenRefAndListLabel(token,label,elementIndex,terminalOptions) ::= << 85 <tokenRef(...)> 86 <listLabel(elem=label,...)> 87 >> 88 89 /** Match label+=TOKEN^ when output=AST but not rewrite alt */ 90 tokenRefRuleRootAndListLabel(token,label,terminalOptions,elementIndex) ::= << 91 <tokenRefRuleRoot(...)> 92 <listLabel(elem=label,...)> 93 >> 94 95 // SET AST 96 97 // the match set stuff is interesting in that it uses an argument list 98 // to pass code to the default matchSet; another possible way to alter 99 // inherited code. I don't use the region stuff because I need to pass 100 // different chunks depending on the operator. I don't like making 101 // the template name have the operator as the number of templates gets 102 // large but this is the most flexible--this is as opposed to having 103 // the code generator call matchSet then add root code or ruleroot code 104 // plus list label plus ... The combinations might require complicated 105 // rather than just added on code. Investigate that refactoring when 106 // I have more time. 107 108 matchSet(s,label,terminalOptions,elementIndex,postmatchCode) ::= <% 109 <super.matchSet(postmatchCode={<finishedBacktracking({self._adaptor.addChild(root_0, <createNodeFromToken(...)>)})>}, ...)> 110 %> 111 112 matchRuleBlockSet(s,label,terminalOptions,elementIndex,postmatchCode,treeLevel="0") ::= << 113 <matchSet(...)> 114 >> 115 116 matchSetBang(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 121 matchSetRuleRoot(s,label,terminalOptions,elementIndex,debug) ::= << 122 <if(label)> 123 <label> = self.input.LT(1)<\n> 124 <endif> 125 <super.matchSet(postmatchCode={<finishedBacktracking({root_0 = self._adaptor.becomeRoot(<createNodeFromToken(...)>, root_0)})>}, ...)> 126 >> 127 128 // RULE REF AST 129 130 /** rule when output=AST */ 131 ruleRef(rule,label,elementIndex,args,scope) ::= << 132 <super.ruleRef(...)> 133 <finishedBacktracking({self._adaptor.addChild(root_0, <label>.tree)})> 134 >> 135 136 /** rule! is same as normal rule ref */ 137 ruleRefBang(rule,label,elementIndex,args,scope) ::= "<super.ruleRef(...)>" 138 139 /** rule^ */ 140 ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= << 141 <super.ruleRef(...)> 142 <finishedBacktracking({root_0 = self._adaptor.becomeRoot(<label>.tree, root_0)})> 143 >> 144 145 /** x+=rule when output=AST */ 146 ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= << 147 <ruleRef(...)> 148 <listLabel(label, {<label>.tree})> 149 >> 150 151 /** x+=rule! when output=AST is a rule ref with list addition */ 152 ruleRefBangAndListLabel(rule,label,elementIndex,args,scope) ::= << 153 <ruleRefBang(...)> 154 <listLabel(label, {<label>.tree})> 155 >> 156 157 /** x+=rule^ */ 158 ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= << 159 <ruleRefRuleRoot(...)> 160 <listLabel(label, {<label>.tree})> 161 >> 162 163 // WILDCARD AST 164 165 wildcard(token,label,elementIndex,terminalOptions) ::= << 166 <super.wildcard(...)> 167 <finishedBacktracking({ 168 <label>_tree = self._adaptor.createWithPayload(<label>) 169 self._adaptor.addChild(root_0, <label>_tree) 170 })> 171 >> 172 173 wildcardBang(label,elementIndex) ::= "<super.wildcard(...)>" 174 175 wildcardRuleRoot(token,label,elementIndex,terminalOptions) ::= << 176 <super.wildcard(...)> 177 <finishedBacktracking({ 178 <label>_tree = self._adaptor.createWithPayload(<label>) 179 root_0 = self._adaptor.becomeRoot(<label>_tree, root_0) 180 })> 181 >> 182 183 createNodeFromToken(label,terminalOptions) ::= <% 184 <if(terminalOptions.node)> 185 <terminalOptions.node>(<label>) <! new MethodNode(IDLabel) !> 186 <else> 187 self._adaptor.createWithPayload(<label>) 188 <endif> 189 %> 190 191 ruleCleanUp() ::= << 192 <super.ruleCleanUp()> 193 <finishedBacktracking({ 194 retval.tree = self._adaptor.rulePostProcessing(root_0) 195 self._adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop) 196 })> 197 >> 198