| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text; |
| | | 4 | | |
| | | 5 | | namespace NanoCLang { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a class for formatting a NanoC entity to a string representation. |
| | | 8 | | /// </summary> |
| | | 9 | | public class StringFormatter { |
| | | 10 | | private const int DEFAULT_INDENT = 4; |
| | | 11 | | private const char INDENT_CHAR = ' '; |
| | 1349 | 12 | | private readonly StringBuilder sb = new StringBuilder(); |
| | 1349 | 13 | | private int indent = 0; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Creates a string representation of the <paramref name="tokens"/> stream. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="tokens">Token stream that is converted to a string representation.</param> |
| | | 19 | | /// <returns>String representation of the <paramref name="tokens"/>.</returns> |
| | 1349 | 20 | | public string ToString(IEnumerable<StringFormatterToken> tokens) { |
| | 64848 | 21 | | foreach (var token in tokens) { |
| | 20267 | 22 | | AppendToken(token); |
| | 20267 | 23 | | } |
| | 1349 | 24 | | return sb.ToString(); |
| | 1349 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Appends the <paramref name="token"/> to the string builder. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="token">Token to append</param> |
| | | 31 | | /// <exception cref="ArgumentOutOfRangeException">Token has unexpected type.</exception> |
| | 20267 | 32 | | internal StringFormatter AppendToken(StringFormatterToken token) { |
| | 20267 | 33 | | switch (token) { |
| | 39260 | 34 | | case StringToken tk: sb.Append(tk.Value); break; |
| | 918 | 35 | | case NewLineToken _: NewLine(); break; |
| | | 36 | | case IndentChangeToken tk: |
| | 234 | 37 | | if (tk.Amount == 0) PrintIndent(); |
| | 122 | 38 | | else indent += tk.Amount; |
| | 178 | 39 | | break; |
| | 0 | 40 | | default: throw new ArgumentOutOfRangeException("Unknown token type!"); |
| | | 41 | | } |
| | 20267 | 42 | | return this; |
| | 20267 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Inserts a new line into the formatter while keeping track of the current indentation. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <returns>Formatter after the new line and indent was inserted.</returns> |
| | 459 | 49 | | public StringFormatter NewLine() { |
| | 459 | 50 | | sb.AppendLine(); |
| | 459 | 51 | | PrintIndent(); |
| | 459 | 52 | | return this; |
| | 459 | 53 | | } |
| | | 54 | | |
| | 515 | 55 | | private void PrintIndent() { |
| | 3538 | 56 | | for (int i = 0; i < indent; i++) sb.Append(INDENT_CHAR); |
| | 515 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Prints a multi-line text block containing <see cref="NewLine"/> while indenting the block further than the c |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="print">Function of the formatter that prints the contents of the block.</param> |
| | | 63 | | /// <param name="amount">Amount of indentation.</param> |
| | | 64 | | /// <returns>Formatter after the new indented block was inserted.</returns> |
| | 0 | 65 | | public StringFormatter Indented(Func<StringFormatter, StringFormatter> print, int amount = DEFAULT_INDENT) { |
| | 0 | 66 | | indent += amount; |
| | 0 | 67 | | try { |
| | 0 | 68 | | return print(this); |
| | 0 | 69 | | } finally { |
| | 0 | 70 | | indent -= amount; |
| | 0 | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |