public static string RemoveEmptyEntries(string input) { Lexer lexer = new Lexer(input); Lexem lexem = lexer.Next(); StringBuilder newText = new StringBuilder(); while (lexem != null) { if (lexem.Token == JToken.String) newText.Append("\"" + lexem.Text + "\""); else newText.Append(lexem.Text); lexem = lexer.Next(); } return newText.ToString(); }
public static string Format(string input) { int numOfSpaces = 0; StringBuilder newStr = new StringBuilder(); Lexer lexer = new Lexer(input); Lexem lexem = lexer.Next(); while (lexem != null) { StringBuilder subStr = new StringBuilder(); if (lexem.Token == JToken.OpenArrayBrace || lexem.Token == JToken.OpenObjectBrace) { numOfSpaces += 2; subStr.Append(lexem.Text + "\n"); for (int i = 0; i < numOfSpaces; i++) subStr.Append(" "); } else if (lexem.Token == JToken.Colon) subStr.Append(lexem.Text + " "); else if (lexem.Token == JToken.Comma) { subStr.Append(lexem.Text + "\n"); for (int i = 0; i < numOfSpaces; i++) subStr.Append(" "); } else if (lexem.Token == JToken.CloseArrayBrace || lexem.Token == JToken.CloseObjectBrace) { numOfSpaces -= 2; subStr.Append("\n"); for (int i = 0; i < numOfSpaces; i++) subStr.Append(" "); subStr.Append(lexem.Text); } else if (lexem.Token == JToken.String) subStr.Append("\"" + lexem.Text + "\""); else subStr.Append(lexem.Text); newStr.Append(subStr); lexem = lexer.Next(); } return newStr.ToString(); }
public static FlowDocument Colorize(string input) { Lexer lexer = new Lexer(input); Lexem lastLexem = new Lexem(JToken.Null, "", 0); Lexem lexem = lexer.Next(); FlowDocument document = new FlowDocument(); Paragraph paragraph = new Paragraph(); Run tempStringRun = null; while (lexem != null) { StringBuilder subStr = new StringBuilder(); Run run = new Run(); subStr.Append(input.Substring(lastLexem.Offset + lastLexem.Length, lexem.Offset - lastLexem.Offset - lastLexem.Length)); if (lexem.Token == JToken.String) { subStr.Append("\"" + lexem.Text + "\""); tempStringRun = new Run(subStr.ToString()); lastLexem = lexem; lexem = lexer.Next(); continue; } else { AddString(paragraph, ref tempStringRun, lexem.Token); subStr.Append(lexem.Text); if (lexem.Token == JToken.True || lexem.Token == JToken.False || lexem.Token == JToken.Null) run.Foreground = GetColor(lexem.Token); } run.Text = subStr.ToString(); paragraph.Inlines.Add(run); lastLexem = lexem; lexem = lexer.Next(); } paragraph.Inlines.Add(new Run(input.Substring(lastLexem.Offset + lastLexem.Length))); document.Blocks.Add(paragraph); return document; }