private void Add(Formatter.TokenType type, object val = null) { Formatter.Token token = new Formatter.Token() { Type = type, Val = val, Pattern = this.text.Substring(this.patternStart, this.position - this.patternStart) }; this.tokens.Add(token); }
private static List <Element> Parse(List <Formatter.Token> tokens) { int num = 0; Stack <Formatter.Entry> entries = new Stack <Formatter.Entry>(); entries.Push(new Formatter.Entry(null, Element.Tag(ElementType.String))); while (num < tokens.Count) { int num1 = num; num = num1 + 1; Formatter.Token item = tokens[num1]; Action <Element> action = (Element el) => entries.Push(new Formatter.Entry(item.Pattern, el)); Element element = entries.Peek().Element; Formatter.TokenType type = item.Type; Formatter.TokenType?nullable = Formatter.closeTags[element.Type]; if (!(type == nullable.GetValueOrDefault() & nullable.HasValue)) { switch (item.Type) { case Formatter.TokenType.String: { element.Body.Add(Element.String(item.Val)); continue; } case Formatter.TokenType.Bold: { action(Element.Tag(ElementType.Bold)); continue; } case Formatter.TokenType.Italic: { action(Element.Tag(ElementType.Italic)); continue; } case Formatter.TokenType.Color: { action(Element.ParamTag(ElementType.Color, item.Val)); continue; } case Formatter.TokenType.Size: { action(Element.ParamTag(ElementType.Size, item.Val)); continue; } } element.Body.Add(Element.String(item.Pattern)); } else { entries.Pop(); entries.Peek().Element.Body.Add(element); } } while (entries.Count > 1) { Formatter.Entry entry = entries.Pop(); List <Element> body = entries.Peek().Element.Body; body.Add(Element.String(entry.Pattern)); body.AddRange(entry.Element.Body); } return(entries.Pop().Element.Body); }