A variant of Les3Printer that adds syntax highlighting in one of three ways: as console output, as HTML output, or as LesColorCode control codes.
Create an instance by invoking the constructor, then call PrintToConsole for console output, PrintToHtml for HTML output, or Les3Printer.Print(IEnumerable{ILNode}) for just control codes.
Inheritance: Les3Printer
示例#1
0
		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>());
		}
示例#2
0
 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());
     }
 }
示例#3
0
        /// <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 "&lt;pre class='highlight'>&lt;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));
        }
示例#4
0
		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());
			}
		}
示例#5
0
		/// <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 "&lt;pre class='highlight'>&lt;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);
		}