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.stringtemplate.v4.*; 35import java.util.HashMap; 36>> 37 38/** Add this to each rule's return value struct */ 39@returnScope.ruleReturnMembers() ::= << 40/* ST returnScope.ruleReturnMembers -- empty */ 41>> 42 43/** Add this to each rule's return value struct */ 44@returnScope.ruleReturn.memvars() ::= << 45ST *st; 46>> 47 48/** Add this to each rule's return value struct */ 49@returnScope.ruleReturn.properties() ::= << 50@property (retain) ST *st; 51>> 52 53/** Add this to each rule's return value struct */ 54@returnScope.ruleReturn.methodsDecl() ::= << 55- (ST *)getTemplate; 56- (NSString *)toString; 57>> 58 59/** Add this to each rule's return value struct */ 60@returnScope.ruleReturn.synthesize() ::= << 61@synthesize st; 62>> 63 64/** Add this to each rule's return value struct */ 65@returnScope.ruleReturn.methods() ::= << 66- (ST *)getTemplate { return st; } 67- (NSString *)toString { return st==nil?nil:[st render]; } 68>> 69 70@genericParser.members() ::= << 71<@super.members()> 72STGroup *templateLib = [STGroup newSTGroup]; 73 74- (void)setTemplateLib:(STGroup *)aTemplateLib 75{ 76 self.templateLib = aTemplateLib; 77} 78 79- (STGroup *)getTemplateLib 80{ 81 return templateLib; 82} 83>> 84 85@genericParserHeaderFile.memVars() ::= << 86<@super.memVars()> 87/* ST genericParserHeaderFile.memVars -- empty now */ 88STGroup *templateLib; /* ST -- really a part of STAttrMap */ 89>> 90 91@genericParserHeaderFile.properties() ::= << 92<@super.properties()> 93/* ST genericParser.properties */ 94@property (retain, getter=getTemplateLib, setter=setTemplateLib:) STGroup *templateLib; 95>> 96 97@genericParserHeaderFile.methodsDecl() ::= << 98<@super.methodsDecl()> 99/* ST genericParser.methodsDecl */ 100- init; 101- (STGroup *) getTemplateLib; 102- (void) setTemplateLib:(STGroup *)aTemplateLib; 103@end 104>> 105 106@genericParser.synthesize() ::= << 107<@super.synthesize()> 108/* ST genericParserImplementation.synthesize */ 109@synthesize templateLib; 110>> 111 112@genericParser.methods() ::= << 113<@super.methods()> 114/* ST genericParser.methods */ 115 116- (STGroup *)getTemplateLib 117{ 118 return templateLib; 119} 120 121- (void) setTemplateLib:(STGroup *)aTemplateLib 122{ 123 templateLib = aTemplateLib; 124} 125 126>> 127 128@genericParser.members() ::= << 129<@super.members()> 130STGroup *templateLib = [STGroup newSTGroup]; 131 132- (STGroup *) getTemplateLib 133{ 134 return templateLib; 135} 136 137- (void) setTemplateLib:(STGroup *) templateLib 138{ 139 this.templateLib = templateLib; 140} 141 142/** x+=rule when output=template */ 143ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= << 144<ruleRef(rule,label,elementIndex,args,scope)> 145<listLabel(label, { [<label> getTemplate]; })> 146>> 147 148rewriteTemplate(alts) ::= << 149 150// TEMPLATE REWRITE 151<if(backtracking)> 152if ( <actions.(actionScope).synpredgate> ) { 153 <alts:rewriteTemplateAlt(); separator="else "> 154 <if(rewriteMode)><replaceTextInLine()><endif> 155} 156<else> 157<alts:rewriteTemplateAlt(); separator="else "> 158<if(rewriteMode)><replaceTextInLine()><endif> 159<endif> 160>> 161 162replaceTextInLine() ::= << 163<if(TREE_PARSER)> 164 [([(TokenRewriteStream)input getTokenStream]) 165 replaceFromIndex:[[input getTreeAdaptor] getTokenStartIndex:retval.start] 166 ToIndex:[[input getTreeAdaptor] getTokenStopIndex:retval.start] 167 Text:[retval.st render]]; 168<else> 169 [((TokenRewriteStream)input) 170 replaceFromIndex:[((Token)retval.start) getTokenIndex] 171 ToIndex:[input LT:-1] getTokenIndex] 172 Text:[retval.st render]]; 173<endif> 174>> 175 176rewriteTemplateAlt(alt) ::= << 177// <alt.description> 178<if(alt.pred)> 179if (<alt.pred>) { 180 retval.st = <alt.alt>; 181}<\n> 182<else> 183{ 184 retval.st = <alt.alt>; 185}<\n> 186<endif> 187>> 188 189rewriteEmptyTemplate(alts) ::= << 190nil; 191>> 192 193/** Invoke a template with a set of attribute name/value pairs. 194 * Set the value of the rule's template *after* having set 195 * the attributes because the rule's template might be used as 196 * an attribute to build a bigger template; you get a self-embedded 197 * template. 198 */ 199rewriteExternalTemplate(name,args) ::= <% 200<if(args)><args:{a | [}><endif> 201[templateLib getInstanceOf:@"<name>"] 202 <if(args)><args:{a | add:@"<a.name>" value:<a.value>]}><endif>; 203%> 204 205/** expr is a string expression that says what template to load */ 206rewriteIndirectTemplate(expr,args) ::= <% 207<if(args)><args:{a | [}><endif> 208[templateLib getInstanceOf:<expr>]; 209<if(args)><args:{a | add:@"<a.name>" value:<a.value>]}><endif>; 210%> 211 212/** Invoke an inline template with a set of attribute name/value pairs */ 213rewriteInlineTemplate(args, template) ::= <% 214<if(args)><args:{a | [}><endif> 215[ST newST:templateLib template:"<template>"] 216 <if(args)><args:{a | add:@"<a.name>" <a.value>]}><endif>; 217%> 218 219/** plain -> {foo} action */ 220rewriteAction(action) ::= << 221<action> 222>> 223 224/** An action has %st.attrName=expr; or %{st}.attrName=expr; */ 225actionSetAttribute(st,attrName,expr) ::= << 226[(<st>) setAttribute:@"<attrName>" value:<expr>]; 227>> 228 229/** Translate %{stringExpr} */ 230actionStringConstructor(stringExpr) ::= << 231 [ST newST:templateLib template:<stringExpr>]; 232>> 233