Exemplo n.º 1
0
 public abstract TResult Visit(FormatTextElement format);
Exemplo n.º 2
0
        private static FormatElement Parse(string format, ref int index, params char[] stopat)
        {
            FormatElement element = FormatNoContentElement.Element;

            while (index < format.Length)
            {
                switch (format[index])
                {
                case '[':     // Coloring
                    element += ParseColor(format, ref index);
                    break;

                case '?':     // Conditional
                    element += ParseCondition(format, ref index);
                    break;

                case '@':     // Listing/Function
                    element += ParseFunction(format, ref index);
                    break;

                case '$':     // Variable
                    element += ParseVariable(format, ref index);
                    break;

                case '\\':
                    if (format.Length == index + 1)
                    {
                        index++;
                    }
                    else
                    {
                        element += new FormatTextElement(format.Substring(index + 1, 1));
                        index   += 2;
                    }
                    break;

                default:     // Skip content
                    var nextIndex = format.IndexOfAny(new char[] { '[', '?', '@', '$', '\\' }, index);
                    if (nextIndex < 0)
                    {
                        nextIndex = format.Length;
                    }

                    var stopatIndex = format.IndexOfAny(stopat, index);
                    if (stopatIndex < nextIndex && stopatIndex >= 0)
                    {
                        if (stopatIndex - index > 0)
                        {
                            element += new FormatTextElement(format.Substring(index, stopatIndex - index));
                            index    = stopatIndex;
                        }
                        return(element);
                    }
                    else
                    {
                        element += new FormatTextElement(format.Substring(index, nextIndex - index));
                        index    = nextIndex;
                    }
                    break;
                }
            }

            return(element);
        }