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