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 org.antlr.stringtemplate.*;
35import org.antlr.stringtemplate.language.*;
36import java.util.HashMap;
37>>
38
39/** Add this to each rule's return value struct */
40@returnScope.ruleReturnMembers() ::= <<
41public StringTemplate st;
42public Object getTemplate() { return st; }
43public String toString() { return st==null?null:st.toString(); }
44>>
45
46@genericParser.members() ::= <<
47<@super.members()>
48protected StringTemplateGroup templateLib =
49  new StringTemplateGroup("<name>Templates", AngleBracketTemplateLexer.class);
50
51public void setTemplateLib(StringTemplateGroup templateLib) {
52  this.templateLib = templateLib;
53}
54public StringTemplateGroup getTemplateLib() {
55  return templateLib;
56}
57/** allows convenient multi-value initialization:
58 *  "new STAttrMap().put(...).put(...)"
59 */
60public static class STAttrMap extends HashMap {
61  public STAttrMap put(String attrName, Object value) {
62    super.put(attrName, value);
63    return this;
64  }
65  public STAttrMap put(String attrName, int value) {
66    super.put(attrName, new Integer(value));
67    return this;
68  }
69}
70>>
71
72/** x+=rule when output=template */
73ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
74<ruleRef(rule,label,elementIndex,args,scope)>
75<listLabel(label, {<label>.getTemplate()})>
76>>
77
78rewriteTemplate(alts) ::= <<
79
80// TEMPLATE REWRITE
81<if(backtracking)>
82if ( <actions.(actionScope).synpredgate> ) {
83  <alts:rewriteTemplateAlt(); separator="else ">
84  <if(rewriteMode)><replaceTextInLine()><endif>
85}
86<else>
87<alts:rewriteTemplateAlt(); separator="else ">
88<if(rewriteMode)><replaceTextInLine()><endif>
89<endif>
90>>
91
92replaceTextInLine() ::= <<
93<if(TREE_PARSER)>
94((TokenRewriteStream)input.getTokenStream()).replace(
95  input.getTreeAdaptor().getTokenStartIndex(retval.start),
96  input.getTreeAdaptor().getTokenStopIndex(retval.start),
97  retval.st);
98<else>
99((TokenRewriteStream)input).replace(
100  ((Token)retval.start).getTokenIndex(),
101  input.LT(-1).getTokenIndex(),
102  retval.st);
103<endif>
104>>
105
106rewriteTemplateAlt(alt) ::= <<
107// <alt.description>
108<if(alt.pred)>
109if (<alt.pred>) {
110    retval.st = <alt.alt>;
111}<\n>
112<else>
113{
114    retval.st = <alt.alt>;
115}<\n>
116<endif>
117>>
118
119rewriteEmptyTemplate(alts) ::= <<
120null;
121>>
122
123/** Invoke a template with a set of attribute name/value pairs.
124 *  Set the value of the rule's template *after* having set
125 *  the attributes because the rule's template might be used as
126 *  an attribute to build a bigger template; you get a self-embedded
127 *  template.
128 */
129rewriteExternalTemplate(name,args) ::= <%
130templateLib.getInstanceOf("<name>"<if(args)>,
131  new STAttrMap()<args:{a | .put("<a.name>", <a.value>)}>
132  <endif>)
133%>
134
135/** expr is a string expression that says what template to load */
136rewriteIndirectTemplate(expr,args) ::= <%
137templateLib.getInstanceOf(<expr><if(args)>,
138  new STAttrMap()<args:{a | .put("<a.name>", <a.value>)}>
139  <endif>)
140%>
141
142/** Invoke an inline template with a set of attribute name/value pairs */
143rewriteInlineTemplate(args, template) ::= <%
144new StringTemplate(templateLib, "<template>"<if(args)>,
145  new STAttrMap()<args:{a | .put("<a.name>", <a.value>)}>
146  <endif>)
147%>
148
149/** plain -> {foo} action */
150rewriteAction(action) ::= <<
151<action>
152>>
153
154/** An action has %st.attrName=expr; or %{st}.attrName=expr; */
155actionSetAttribute(st,attrName,expr) ::= <<
156(<st>).setAttribute("<attrName>",<expr>);
157>>
158
159/** Translate %{stringExpr} */
160actionStringConstructor(stringExpr) ::= <<
161new StringTemplate(templateLib,<stringExpr>)
162>>
163