public void TestRegionOverrideRefSuperRegion2Levels() { string g = "a() ::= \"X<@r()>Y\"\n" + "@a.r() ::= \"foo\"\n"; TemplateGroup group = new TemplateGroupString(g); string sub = "@a.r() ::= \"<@super.r()>2\"\n"; TemplateGroup subGroup = new TemplateGroupString(sub); subGroup.ImportTemplates(group); Template st = subGroup.GetInstanceOf("a"); string result = st.Render(); string expecting = "Xfoo2Y"; Assert.AreEqual(expecting, result); }
public string Parse(string templateKey, IDictionary <string, object> model) { var group = new TemplateGroupString("group", "delimiters \"$\", \"$\"\r\nt(x) ::= \" $ x $ \""); var renderer = new AdvancedRenderer(); group.RegisterRenderer(typeof(DateTimeOffset), renderer); group.RegisterRenderer(typeof(DateTime), renderer); group.RegisterRenderer(typeof(double), renderer); group.RegisterRenderer(typeof(decimal), renderer); var templateContent = _templateProvider.GetContentsFor(templateKey, model); group.DefineTemplate("template", templateContent, new[] { "Model" }); var stringTemplate = group.GetInstanceOf("template"); stringTemplate.Add("Model", model); return(stringTemplate.Render()); }
/// <summary> /// Generates the C# representation of the specified Thrift document. /// </summary> /// <param name="document">The document to generate code from.</param> /// <returns>The generated code.</returns> public string Generate(IDocument document) { var assembly = typeof(ThriftDocumentGenerator).Assembly; var rawTemplate = assembly.GetManifestResourceStream( $"{assembly.GetName().Name}.Templates.csharp.stg"); using var reader = new StreamReader(rawTemplate); var templateGroup = new TemplateGroupString( "csharp.stg", reader.ReadToEnd(), '$', '$'); var template = templateGroup.GetInstanceOf("document"); // TODO: Get the semantic version template.Add("version", assembly.GetName().Version.ToString()); template.Add("model", document); template.Add("typeMap", ThriftTypeGenerationInfo.TypeMap); return(template.Render()); }
public void TestNoNewlineTemplate() { string template = "t(x) ::= <%" + newline + "[ <if(!x)>" + "<else>" + "<x>" + newline + "<endif>" + "" + newline + "" + newline + "]" + newline + "" + newline + "%>" + newline; TemplateGroup g = new TemplateGroupString(template); Template st = g.GetInstanceOf("t"); st.Add("x", 99); string expected = "[ 99]"; string result = st.Render(); Assert.AreEqual(expected, result); }
public void TestComplicatedIndirectTemplateApplication() { string templates = "group Java;" + newline + "" + newline + "file(variables) ::= <<" + "<variables:{ v | <v.decl:(v.format)()>}; separator=\"\\n\">" + newline + ">>" + newline + "intdecl(decl) ::= \"int <decl.name> = 0;\"" + newline + "intarray(decl) ::= \"int[] <decl.name> = null;\"" + newline ; TemplateGroup group = new TemplateGroupString(templates); Template f = group.GetInstanceOf("file"); f.AddMany("variables.{ decl,format }", new Decl("i", "int"), "intdecl"); f.AddMany("variables.{decl , format}", new Decl("a", "int-array"), "intarray"); //System.out.println("f='"+f+"'"); string expecting = "int i = 0;" + newline + "int[] a = null;"; Assert.AreEqual(expecting, f.Render()); }
private void PrepareTemplate() { TemplateGroup templateGroup; switch (ParameterSetName) { case "group": var path = System.IO.Path.GetFullPath(GroupPath); if (path.EndsWith(TemplateGroup.GroupFileExtension, StringComparison.InvariantCultureIgnoreCase)) { templateGroup = new TemplateGroupFile(path, Encoding.UTF8, DelimiterStartChar, DelimiterStopChar) { Verbose = IsVerbose, Logger = Host.UI.WriteVerboseLine } } ; else { templateGroup = new TemplateGroupDirectory(path, Encoding.UTF8, DelimiterStartChar, DelimiterStopChar) { Verbose = IsVerbose, Logger = Host.UI.WriteVerboseLine } }; Template = templateGroup.GetInstanceOf(TemplateName); break; case "groupstring": templateGroup = new TemplateGroupString(TemplateName, GroupString, DelimiterStartChar, DelimiterStopChar); Template = templateGroup.GetInstanceOf(TemplateName); break; case "anonymous": Template = new Template(TemplateString, DelimiterStartChar, DelimiterStopChar); break; } }
public static void UrlSellerTemp1() { //string templates = // "test(names,phones) ::= \"<foo([names,phones])>\"" + "\n" + // "foo(items) ::= \"<items:{a | *<a>*}>\"" + "\n"; //TemplateGroup templateGroup = new TemplateGroupString(templates); //TemplateGroup templateGroup = new TemplateGroupString("[string]", templates); string templates = "test(names,phones) ::= \"$foo([names,phones])$\"" + "\n" + "foo(items) ::= \"$items:{a | *$a$*}$\"" + "\n"; TemplateGroup templateGroup = new TemplateGroupString("[string]", templates, '$', '$'); Template e = templateGroup.GetInstanceOf("test"); e.Add("names", "Ter"); e.Add("names", "Tom"); e.Add("phones", "1"); e.Add("phones", "2"); string result = e.Render(); }
create_detours ( string Output_file, List <string> Files, ref List <Api> Api_list, ref List <string> Headers ) { string file_names = string.Join(", ", Files); int line_width = 128; // // Convert the list of APIs into a form that the string template is expecting // convert_for_string_template(ref Api_list); // // Useful during debugging. Reading the templates from a text file doesn't require recompiling after every change // // string filespec = Path.GetFullPath (@"..\..\Detours.stg"); // TemplateGroup group = new TemplateGroupFile (filespec); TemplateGroup group = new TemplateGroupString(string_template_group); // Use the string template defined in this string // group.Verbose = true; // Display errors and operations to the console window // // Generate the file header // Template file_hdr = group.GetInstanceOf("file_header"); file_hdr.Add("version", "1.1.0.0"); DateTime now = DateTime.Now; file_hdr.Add("date", now); file_hdr.Add("exe_name", file_names); file_hdr.Add("apis_to_detour", Api_list); file_hdr.Add("headers", Headers); file_hdr.Add("command_line", Environment.CommandLine); File.AppendAllText(Output_file, file_hdr.Render(line_width)); // // Generate the pointers to the real APIs // Template real_api_list = group.GetInstanceOf("real_api_list"); real_api_list.Add("list", Api_list); File.AppendAllText(Output_file, real_api_list.Render(line_width)); // // Generate the support routines // Template support_routines = group.GetInstanceOf("support_routines"); support_routines.Add("exename", file_names); support_routines.Add("api_list", Api_list); File.AppendAllText(Output_file, support_routines.Render(line_width)); // // For each API, create a list of output-only parameters that are not just pointers (these will be part of the list of input parameters). This is // to get around StringTemplate bug that I found, where it doesn't handle lists with separators properly (it leaves a hanging separator at the end // of a list) // foreach (var api in Api_list) { api.output_parameters = (from p in api.parameters where p.is_output && !p.is_pointer select p).ToList(); if (api.output_parameters.Count == 0) { api.has_outputs = false; } } // // Generate the Detours routines // Template generate_routines = group.GetInstanceOf("generate_routines"); generate_routines.Add("api_list", Api_list); File.AppendAllText(Output_file, generate_routines.Render(line_width)); } // End create_detours