public void Les3PrettyPrinterDemo() { // Pretty printer demo var code = Les3LanguageService.Value.Parse( @".memory 1 .fn _sumIntegersDemo(input: i32, length: i32): i32 { sum: i32 sum = 0 .block { .br stop if input s< 1 sum = 0 .loop loop { // I picked := for set_local and = for tee_local; // feel free to vote your own preference. $sum := i32[$input] + $sum $input := $input + 4 .br loop if $length = $length + -1 } stop: } sum // return value }", msgs: ConsoleMessageSink.Value); var pp = new Les3PrettyPrinter(null, new Les3PrinterOptions { IndentString = " " }); pp.PrintToConsole(code.Cast<ILNode>()); }
private void TestPrettyPrint(string pretty, LNode node, bool addPreCode = false, bool rawTest = true, bool htmlTest = true) { if (rawTest) { // Raw output test: must add {Opener}, {Closer} and {Separator} markers, // change {EndId} to {/} and remove excess {/}s var pretty2 = pretty.Replace("(", "{Opener}({/}").Replace("[", "{Opener}[{/}").Replace("{{", "{Opener}{{{/}") .Replace(")", "{Closer}){/}").Replace("]", "{Closer}]{/}").Replace("}}", "{Closer}}}{/}") .Replace("{/Id}", "{/}").Replace(",", "{Separator},{/}").Replace(";", "{Separator};{/}"); var pretty3 = (pretty2.EndsWith("{/}") ? pretty2.Substring(0, pretty2.Length - 3) : pretty2).Replace("{/}{", "{"); var expected = pretty3.FormatCore(ControlCodeTable); var pp = new Les3PrettyPrinter(); StringBuilder result = pp.Print(node); AreEqual(expected, result.ToString()); } if (htmlTest) { // HTML test: no spans are created for Ids so eliminate {Id} and {/Id}. var pretty2 = pretty.Replace("{Id}", "").Replace("{/Id}", ""); var expected = pretty2.FormatCore(HtmlCodeTable); if (addPreCode) { expected = "<pre class='highlight'><code>" + expected + "</code></pre>"; } var result = new Les3PrettyPrinter().PrintToHtml(node, addPreCode: addPreCode); AreEqual(expected, result.ToString()); } }
/// <summary>Prints an LNode as LESv3 with HTML syntax highlighting elements.</summary> /// <param name="nodes">Syntax trees to print.</param> /// <param name="output">Output StringBuilder for HTML code.</param> /// <param name="addPreCode">Whether to wrap the output in "<pre class='highlight'><code>" tags.</param> /// <param name="options">Options to control the style for code printing.</param> /// <returns>The output StringBuilder</returns> public static StringBuilder PrintToHtml( IEnumerable <ILNode> nodes, StringBuilder output = null, bool addPreCode = true, IMessageSink sink = null, ILNodePrinterOptions options = null) { var pp = new Les3PrettyPrinter(sink, options); return(pp.PrintToHtml(nodes, output, addPreCode)); }
private void TestPrettyPrint(string pretty, LNode node, bool addPreCode = false, bool rawTest = true, bool htmlTest = true) { if (rawTest) { // Raw output test: must add {Opener}, {Closer} and {Separator} markers, // change {EndId} to {0} and remove excess {0}s var pretty2 = pretty.Replace("(", "{Opener}({0}").Replace("[", "{Opener}[{0}").Replace("{{", "{Opener}{{{0}") .Replace(")", "{Closer}){0}").Replace("]", "{Closer}]{0}").Replace("}}", "{Closer}}}{0}") .Replace("{/Id}", "{0}").Replace(",", "{Separator},{0}").Replace(";", "{Separator};{0}"); var pretty3 = (pretty2.EndsWith("{0}") ? pretty2.Substring(0, pretty2.Length - 3) : pretty2).Replace("{0}{", "{"); var expected = pretty3.FormatCore(ControlCodeTable); var pp = new Les3PrettyPrinter(); StringBuilder result = pp.Print(node); AreEqual(expected, result.ToString()); } if (htmlTest) { // HTML test: no spans are created for Ids so eliminate {Id} and {/Id}. var pretty2 = pretty.Replace("{Id}", "").Replace("{/Id}", ""); var expected = pretty2.FormatCore(HtmlCodeTable); if (addPreCode) expected = "<pre class='highlight'><code>" + expected + "</code></pre>"; var result = new Les3PrettyPrinter().PrintToHtml(node, addPreCode: addPreCode); AreEqual(expected, result.ToString()); } }
/// <summary>Prints an LNode as LESv3 with HTML syntax highlighting elements.</summary> /// <param name="nodes">Syntax trees to print.</param> /// <param name="output">Output StringBuilder for HTML code.</param> /// <param name="addPreCode">Whether to wrap the output in "<pre class='highlight'><code>" tags.</param> /// <param name="options">Options to control the style for code printing.</param> /// <returns>The output StringBuilder</returns> public static StringBuilder PrintToHtml( IEnumerable<ILNode> nodes, StringBuilder output = null, bool addPreCode = true, IMessageSink sink = null, ILNodePrinterOptions options = null) { var pp = new Les3PrettyPrinter(sink, options); return pp.PrintToHtml(nodes, output, addPreCode); }