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 tree 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 *  Each combination has its own template except that label/no label
38 *  is combined into tokenRef, ruleRef, ...
39 */
40
41/** Add a variable to track last element matched */
42ruleDeclarations() ::= <<
43<super.ruleDeclarations()>
44<ASTLabelType> _first_0 = null;
45<ASTLabelType> _last = null;<\n>
46>>
47
48/** What to emit when there is no rewrite rule.  For auto build
49 *  mode, does nothing.
50 */
51noRewrite(rewriteBlockLevel, treeLevel) ::= <<
52<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
53<if(rewriteMode)>
54retval.tree = (<ASTLabelType>)_first_0;
55if ( adaptor.getParent(retval.tree)!=null && adaptor.isNil( adaptor.getParent(retval.tree) ) )
56    retval.tree = (<ASTLabelType>)adaptor.getParent(retval.tree);
57<endif>
58<if(backtracking)>}<endif>
59>>
60
61/** match ^(root children) in tree parser; override here to
62 *  add tree construction actions.
63 */
64tree(root, actionsAfterRoot, children, nullableChildList,
65     enclosingTreeLevel, treeLevel) ::= <<
66_last = (<ASTLabelType>)input.LT(1);
67{
68<ASTLabelType> _save_last_<treeLevel> = _last;
69<ASTLabelType> _first_<treeLevel> = null;
70<if(!rewriteMode)>
71<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.nil();
72<endif>
73<root:element()>
74<if(rewriteMode)>
75<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
76<if(root.el.rule)>
77if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>.tree;
78<else>
79if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>;
80<endif>
81<endif>
82<actionsAfterRoot:element()>
83<if(nullableChildList)>
84if ( input.LA(1)==Token.DOWN ) {
85    match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
86    <children:element()>
87    match(input, Token.UP, null); <checkRuleBacktrackFailure()>
88}
89<else>
90match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
91<children:element()>
92match(input, Token.UP, null); <checkRuleBacktrackFailure()>
93<endif>
94<if(!rewriteMode)>
95adaptor.addChild(root_<enclosingTreeLevel>, root_<treeLevel>);
96<endif>
97_last = _save_last_<treeLevel>;
98}<\n>
99>>
100
101// TOKEN AST STUFF
102
103/** ID! and output=AST (same as plain tokenRef) 'cept add
104 *  setting of _last
105 */
106tokenRefBang(token,label,elementIndex,terminalOptions) ::= <<
107_last = (<ASTLabelType>)input.LT(1);
108<super.tokenRef(...)>
109>>
110
111/** ID auto construct */
112tokenRef(token,label,elementIndex,terminalOptions) ::= <<
113_last = (<ASTLabelType>)input.LT(1);
114<super.tokenRef(...)>
115<if(!rewriteMode)>
116<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
117<if(terminalOptions.node)>
118<label>_tree = new <terminalOptions.node>(<label>);
119<else>
120<label>_tree = (<ASTLabelType>)adaptor.dupNode(<label>);
121<endif><\n>
122adaptor.addChild(root_<treeLevel>, <label>_tree);
123<if(backtracking)>}<endif>
124<else> <! rewrite mode !>
125<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
126if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>;
127<endif>
128>>
129
130/** label+=TOKEN auto construct */
131tokenRefAndListLabel(token,label,elementIndex,terminalOptions) ::= <<
132<tokenRef(...)>
133<listLabel(elem=label,...)>
134>>
135
136/** ^(ID ...) auto construct */
137tokenRefRuleRoot(token,label,elementIndex,terminalOptions) ::= <<
138_last = (<ASTLabelType>)input.LT(1);
139<super.tokenRef(...)>
140<if(!rewriteMode)>
141<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
142<if(terminalOptions.node)>
143<label>_tree = new <terminalOptions.node>(<label>);
144<else>
145<label>_tree = (<ASTLabelType>)adaptor.dupNode(<label>);
146<endif><\n>
147root_<treeLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>_tree, root_<treeLevel>);
148<if(backtracking)>}<endif>
149<endif>
150>>
151
152/** Match ^(label+=TOKEN ...) auto construct */
153tokenRefRuleRootAndListLabel(token,label,elementIndex,terminalOptions) ::= <<
154<tokenRefRuleRoot(...)>
155<listLabel(elem=label,...)>
156>>
157
158/** Match . wildcard and auto dup the node/subtree */
159wildcard(token,label,elementIndex,terminalOptions) ::= <<
160_last = (<ASTLabelType>)input.LT(1);
161<super.wildcard(...)>
162<if(!rewriteMode)>
163<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
164<label>_tree = (<ASTLabelType>)adaptor.dupTree(<label>);
165adaptor.addChild(root_<treeLevel>, <label>_tree);
166<if(backtracking)>}<endif>
167<else> <! rewrite mode !>
168<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
169if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>;
170<endif>
171>>
172
173// SET AST
174
175matchSet(s,label,terminalOptions,elementIndex,postmatchCode) ::= <<
176_last = (<ASTLabelType>)input.LT(1);
177<super.matchSet(postmatchCode={
178<if(!rewriteMode)>
179<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
180<if(terminalOptions.node)>
181<label>_tree = new <terminalOptions.node>(<label>);
182<else>
183<label>_tree = (<ASTLabelType>)adaptor.dupNode(<label>);
184<endif><\n>
185adaptor.addChild(root_<treeLevel>, <label>_tree);
186<if(backtracking)>\}<endif>
187<endif>
188}, ...
189)>
190>>
191
192matchRuleBlockSet(s,label,terminalOptions,elementIndex,postmatchCode,treeLevel="0") ::= <<
193<matchSet(...)>
194<noRewrite(...)> <! set return tree !>
195>>
196
197matchSetBang(s,label,terminalOptions,elementIndex,postmatchCode) ::= <<
198_last = (<ASTLabelType>)input.LT(1);
199<super.matchSet(...)>
200>>
201
202matchSetRuleRoot(s,label,terminalOptions,elementIndex,debug) ::= <<
203<super.matchSet(postmatchCode={
204<if(!rewriteMode)>
205<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
206<if(terminalOptions.node)>
207<label>_tree = new <terminalOptions.node>(<label>);
208<else>
209<label>_tree = (<ASTLabelType>)adaptor.dupNode(<label>);
210<endif><\n>
211root_<treeLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>_tree, root_<treeLevel>);
212<if(backtracking)>\}<endif>
213<endif>
214}, ...
215)>
216>>
217
218// RULE REF AST
219
220/** rule auto construct */
221ruleRef(rule,label,elementIndex,args,scope) ::= <<
222_last = (<ASTLabelType>)input.LT(1);
223<super.ruleRef(...)>
224<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>
225<if(!rewriteMode)>
226adaptor.addChild(root_<treeLevel>, <label>.getTree());
227<else> <! rewrite mode !>
228if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>.tree;
229<endif>
230>>
231
232/** x+=rule auto construct */
233ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
234<ruleRef(...)>
235<listLabel(label, {<label>.getTree()})>
236>>
237
238/** ^(rule ...) auto construct */
239ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
240_last = (<ASTLabelType>)input.LT(1);
241<super.ruleRef(...)>
242<if(!rewriteMode)>
243<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>root_<treeLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>.getTree(), root_<treeLevel>);
244<endif>
245>>
246
247/** ^(x+=rule ...) auto construct */
248ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
249<ruleRefRuleRoot(...)>
250<listLabel(label, {<label>.getTree()})>
251>>
252
253/** rule when output=AST and tracking for rewrite */
254ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
255_last = (<ASTLabelType>)input.LT(1);
256<super.ruleRefTrack(...)>
257>>
258
259/** x+=rule when output=AST and tracking for rewrite */
260ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
261_last = (<ASTLabelType>)input.LT(1);
262<super.ruleRefTrackAndListLabel(...)>
263>>
264
265/** ^(rule ...) rewrite */
266ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
267_last = (<ASTLabelType>)input.LT(1);
268<super.ruleRefRootTrack(...)>
269>>
270
271/** ^(x+=rule ...) rewrite */
272ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
273_last = (<ASTLabelType>)input.LT(1);
274<super.ruleRefRuleRootTrackAndListLabel(...)>
275>>
276
277/** Streams for token refs are tree nodes now; override to
278 *  change nextToken to nextNode.
279 */
280createRewriteNodeFromElement(token,terminalOptions,scope) ::= <<
281<if(terminalOptions.node)>
282new <terminalOptions.node>(stream_<token>.nextNode())
283<else>
284stream_<token>.nextNode()
285<endif>
286>>
287
288ruleCleanUp() ::= <<
289<super.ruleCleanUp()>
290<if(!rewriteMode)>
291<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
292retval.tree = (<ASTLabelType>)adaptor.rulePostProcessing(root_0);
293<if(backtracking)>}<endif>
294<endif>
295>>
296