1/* 2 * [The "BSD license"] 3 * Copyright (c) 2007-2008 Johannes Luber 4 * Copyright (c) 2005-2007 Kunle Odutola 5 * Copyright (c) 2011 Sam Harwell 6 * Copyright (c) 2011 Terence Parr 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31/** Template subgroup to add template rewrite output 32 * If debugging, then you'll also get STDbg.stg loaded. 33 */ 34 35@outputFile.imports() ::= << 36<@super.imports()> 37using Antlr.StringTemplate; 38using Antlr.StringTemplate.Language; 39<if(!backtracking)> 40using Hashtable = System.Collections.Hashtable; 41<endif> 42 43>> 44 45/** Add this to each rule's return value struct */ 46@returnScope.ruleReturnMembers() ::= << 47private StringTemplate _st; 48public StringTemplate Template { get { return _st; } set { _st = value; } } 49public override string ToString() { return (Template==null) ? string.Empty : Template.ToString(); } 50>> 51 52@genericParser.members() ::= << 53<@super.members()> 54protected StringTemplateGroup templateLib = new StringTemplateGroup("<name>Templates", typeof(AngleBracketTemplateLexer) ); 55 56public StringTemplateGroup TemplateLib 57{ 58 get { return this.templateLib; } 59 set { this.templateLib = value; } 60} 61 62/// \<summary> Allows convenient multi-value initialization: 63/// "new STAttrMap().Add(...).Add(...)" 64/// \</summary> 65protected class STAttrMap : Hashtable 66{ 67 public STAttrMap Add(string attrName, object value) 68 { 69 base.Add(attrName, value); 70 return this; 71 } 72 public STAttrMap Add(string attrName, int value) 73 { 74 base.Add(attrName, value); 75 return this; 76 } 77} 78>> 79 80/** x+=rule when output=template */ 81ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= << 82<ruleRef(...)> 83<listLabel(elem=label+".Template",...)> 84>> 85 86rewriteTemplate(alts) ::= << 87 88// TEMPLATE REWRITE 89<if(backtracking)> 90if ( <actions.(actionScope).synpredgate> ) 91{ 92 <alts:rewriteTemplateAlt(); separator="else "> 93 <if(rewriteMode)><replaceTextInLine()><endif> 94} 95<else> 96<alts:rewriteTemplateAlt(); separator="else "> 97<if(rewriteMode)><replaceTextInLine()><endif> 98<endif> 99>> 100 101replaceTextInLine() ::= << 102<if(TREE_PARSER)> 103((TokenRewriteStream)input.TokenStream).Replace( 104 input.TreeAdaptor.GetTokenStartIndex(retval.Start), 105 input.TreeAdaptor.GetTokenStopIndex(retval.Start), 106 retval.Template); 107<else> 108((TokenRewriteStream)input).Replace( 109 ((IToken)retval.Start).TokenIndex, 110 input.LT(-1).TokenIndex, 111 retval.Template); 112<endif> 113>> 114 115rewriteTemplateAlt() ::= << 116// <it.description> 117<if(it.pred)> 118if (<it.pred>) { 119 retval.Template = <it.alt>; 120}<\n> 121<else> 122{ 123 retval.Template = <it.alt>; 124}<\n> 125<endif> 126>> 127 128rewriteEmptyTemplate(alts) ::= << 129null; 130>> 131 132/** Invoke a template with a set of attribute name/value pairs. 133 * Set the value of the rule's template *after* having set 134 * the attributes because the rule's template might be used as 135 * an attribute to build a bigger template; you get a self-embedded 136 * template. 137 */ 138rewriteExternalTemplate(name,args) ::= << 139templateLib.GetInstanceOf("<name>"<if(args)>, 140 new STAttrMap()<args:{a | .Add("<a.name>", <a.value>)}> 141 <endif>) 142>> 143 144/** expr is a string expression that says what template to load */ 145rewriteIndirectTemplate(expr,args) ::= << 146templateLib.GetInstanceOf(<expr><if(args)>, 147 new STAttrMap()<args:{a | .Add("<a.name>", <a.value>)}> 148 <endif>) 149>> 150 151/** Invoke an inline template with a set of attribute name/value pairs */ 152rewriteInlineTemplate(args, template) ::= << 153new StringTemplate(templateLib, "<template>"<if(args)>, 154 new STAttrMap()<args:{a | .Add("<a.name>", <a.value>)}> 155 <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>).SetAttribute("<attrName>",<expr>); 166>> 167 168/** Translate %{stringExpr} */ 169actionStringConstructor(stringExpr) ::= << 170new StringTemplate(templateLib,<stringExpr>) 171>> 172