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