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/** Template subgroup to add template rewrite output
29 *  If debugging, then you'll also get STDbg.stg loaded.
30 */
31
32@outputFile.imports() ::= <<
33<@super.imports()>
34import stringtemplate3
35>>
36
37/** Add this to each rule's return value struct */
38@returnScope.ruleReturnInit() ::= <<
39self.st = None
40>>
41
42@returnScope.ruleReturnMembers() ::= <<
43def getTemplate(self):
44    return self.st
45
46def toString(self):
47    if self.st is not None:
48        return self.st.toString()
49    return None
50__str__ = toString
51
52>>
53
54@genericParser.init() ::= <<
55<@super.init()>
56self.templateLib = stringtemplate3.StringTemplateGroup(
57    '<name>Templates', lexer='angle-bracket'
58    )
59
60>>
61
62@genericParser.members() ::= <<
63<@super.members()>
64def setTemplateLib(self, templateLib):
65    self.templateLib = templateLib
66
67def getTemplateLib(self):
68    return self.templateLib
69
70>>
71
72/** x+=rule when output=template */
73ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
74<ruleRef(...)>
75<listLabel(label, {<label>.st})>
76>>
77
78rewriteTemplate(alts) ::= <<
79# TEMPLATE REWRITE
80<if(backtracking)>
81if <actions.(actionScope).synpredgate>:
82    <first(alts):rewriteTemplateAltFirst()>
83    <rest(alts):{it | el<rewriteTemplateAlt(it)>}>
84    <if(rewriteMode)><replaceTextInLine()><endif>
85
86<else>
87<first(alts):rewriteTemplateAltFirst()>
88<rest(alts):{it | el<rewriteTemplateAlt(it)>}>
89<if(rewriteMode)><replaceTextInLine()><endif>
90<endif>
91>>
92
93replaceTextInLine() ::= <<
94<if(TREE_PARSER)>
95self.input.getTokenStream().replace(
96    self.input.getTreeAdaptor().getTokenStartIndex(retval.start),
97    self.input.getTreeAdaptor().getTokenStopIndex(retval.start),
98    retval.st
99    )
100<else>
101self.input.replace(
102    retval.start.getTokenIndex(),
103    self.input.LT(-1).getTokenIndex(),
104    retval.st
105    )
106<endif>
107>>
108
109rewriteTemplateAltFirst(alt) ::= <<
110<if(alt.pred)>
111if <alt.pred>:
112    # <alt.description>
113    retval.st = <alt.alt>
114<\n>
115<else>
116# <alt.description>
117retval.st = <alt.alt>
118<\n>
119<endif>
120>>
121
122rewriteTemplateAlt(alt) ::= <<
123<if(alt.pred)>if <alt.pred>:
124    # <alt.description>
125    retval.st = <alt.alt>
126<\n>
127<else>se:
128    # <alt.description>
129    retval.st = <alt.alt>
130<\n>
131<endif>
132>>
133
134rewriteEmptyTemplate(alts) ::= <<
135None
136>>
137
138/** Invoke a template with a set of attribute name/value pairs.
139 *  Set the value of the rule's template *after* having set
140 *  the attributes because the rule's template might be used as
141 *  an attribute to build a bigger template; you get a self-embedded
142 *  template.
143 */
144rewriteExternalTemplate(name,args) ::= <%
145self.templateLib.getInstanceOf("<name>"<if(args)>, attributes={<args:{a | "<a.name>": <a.value>}; separator=", ">}<endif>)
146%>
147
148/** expr is a string expression that says what template to load */
149rewriteIndirectTemplate(expr,args) ::= <%
150self.templateLib.getInstanceOf(<expr><if(args)>, attributes={<args:{a | "<a.name>": <a.value>}; separator=", ">}<endif>)
151%>
152
153/** Invoke an inline template with a set of attribute name/value pairs */
154rewriteInlineTemplate(args, template) ::= <%
155stringtemplate3.StringTemplate("<template>", group=self.templateLib<if(args)>, attributes={<args:{a | "<a.name>": <a.value>}; separator=", ">}<endif>)
156%>
157
158/** plain -> {foo} action */
159rewriteAction(action) ::= <<
160<action>
161>>
162
163/** An action has %st.attrName=expr; or %{st}.attrName=expr; */
164actionSetAttribute(st,attrName,expr) ::= <<
165(<st>)["<attrName>"] = <expr>
166>>
167
168/** Translate %{stringExpr} */
169actionStringConstructor(stringExpr) ::= <<
170stringtemplate3.StringTemplate(<stringExpr>, group=self.templateLib)
171>>
172