Exemple #1
0
 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);
 }
Exemple #2
0
 private Formatter.Lexer.State EndTag(Formatter.TokenType t)
 {
     this.Next();
     return(() => {
         if (this.Current() != ']')
         {
             this.Reset();
             return new Formatter.Lexer.State(this.Str);
         }
         this.Next();
         this.Add(t, null);
         this.StartNewPattern();
         return new Formatter.Lexer.State(this.Str);
     });
 }
Exemple #3
0
 private Formatter.Lexer.State ParamTag(Formatter.TokenType t, Func <string, object> parse)
 {
     this.Next();
     this.StartNewToken();
     Formatter.Lexer.State state = null;
     state = () => {
         if (this.Current() != ']')
         {
             this.Next();
             return(state);
         }
         object obj = parse(this.Token());
         if (obj == null)
         {
             this.Reset();
             return(new Formatter.Lexer.State(this.Str));
         }
         this.Next();
         this.Add(t, obj);
         this.StartNewPattern();
         return(new Formatter.Lexer.State(this.Str));
     };
     return(state);
 }
Exemple #4
0
        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);
        }