public static void PrintToConsoleCore(StringBuilder input, bool endWithNewline = true)
{
var originalColor = Console.ForegroundColor;
int depth = 0;
char c, next = input.TryGet(0, '\0');
for (int i = 0; i < input.Length; i++) {
c = next;
next = input.TryGet(i + 1, '\0');
if (c < ' ') {
switch(c) {
case '\n': Console.WriteLine(); break;
case (char)LesColorCode.None: Console.ForegroundColor = ConsoleColor.Gray; break;
case (char)LesColorCode.Comment: Console.ForegroundColor = ConsoleColor.Green; break;
case (char)LesColorCode.Id: Console.ForegroundColor = ConsoleColor.Gray; break;
case (char)LesColorCode.SpecialId: Console.ForegroundColor = ConsoleColor.Green; break;
case (char)LesColorCode.Number: Console.ForegroundColor = ConsoleColor.Magenta; break;
case (char)LesColorCode.String: Console.ForegroundColor = ConsoleColor.Yellow; break;
case (char)LesColorCode.CustomLiteral: Console.ForegroundColor = ConsoleColor.Magenta; break;
case (char)LesColorCode.KeywordLiteral: Console.ForegroundColor = ConsoleColor.Red; break;
case (char)LesColorCode.Operator: Console.ForegroundColor = ConsoleColor.White; break;
case (char)LesColorCode.Separator: Console.ForegroundColor = ConsoleColor.Gray; break;
case (char)LesColorCode.Attribute: Console.ForegroundColor = ConsoleColor.Blue; break;
case (char)LesColorCode.Keyword: Console.ForegroundColor = ConsoleColor.Cyan; break;
case (char)LesColorCode.Unknown: Console.ForegroundColor = ConsoleColor.DarkRed; break;
case (char)LesColorCode.Opener:
Console.ForegroundColor = ConsoleColor.Gray;
if ((next == '(' || next == '[') && (++depth & 1) == 0)
Console.ForegroundColor = ConsoleColor.White;
break;
case (char)LesColorCode.Closer:
Console.ForegroundColor = ConsoleColor.Gray;
if ((next == ')' || next == ']') && (--depth & 1) != 0)
Console.ForegroundColor = ConsoleColor.White;
break;
default:
Console.Write(c);
break;
}
} else {
Console.Write(c);
}
}
Console.ForegroundColor = originalColor;
if (endWithNewline)
Console.WriteLine();
}