Added initial support for rendering composites

This is far from complete, but it's a start
and it will enable us to create things like tables
and other complex objects in the long run.
This commit is contained in:
Patrik Svensson
2020-07-29 17:34:54 +02:00
committed by Patrik Svensson
parent e596e6eb4f
commit 8e4f33bba4
41 changed files with 1164 additions and 24 deletions

View File

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
namespace Spectre.Console.Internal
{
internal static class MarkupParser
{
public static IMarkupNode Parse(string text)
{
using var tokenizer = new MarkupTokenizer(text);
var root = new MarkupBlockNode();
var stack = new Stack<MarkupBlockNode>();
var current = root;
while (true)
{
var token = tokenizer.GetNext();
if (token == null)
{
break;
}
if (token.Kind == MarkupTokenKind.Text)
{
current.Append(new MarkupTextNode(token.Value));
continue;
}
else if (token.Kind == MarkupTokenKind.Open)
{
var (style, foreground, background) = MarkupStyleParser.Parse(token.Value);
var content = new MarkupBlockNode();
current.Append(new MarkupStyleNode(style, foreground, background, content));
current = content;
stack.Push(current);
continue;
}
else if (token.Kind == MarkupTokenKind.Close)
{
if (stack.Count == 0)
{
throw new InvalidOperationException($"Encountered closing tag when none was expected near position {token.Position}.");
}
stack.Pop();
if (stack.Count == 0)
{
current = root;
}
else
{
current = stack.Peek();
}
continue;
}
throw new InvalidOperationException("Encountered unkown markup token.");
}
if (stack.Count > 0)
{
throw new InvalidOperationException("Unbalanced markup stack. Did you forget to close a tag?");
}
return root;
}
}
}