/// <summary>
        ///     Optimizes the specified tokens.
        /// </summary>
        /// <param name="tokens">The tokens to be optimized.</param>
        /// <returns>A Enumerable of tokens.</returns>
        public static IEnumerable <Token.Token> Optimize(this IEnumerable <Token.Token> tokens)
        {
            RawTextToken prevToken = null;

            foreach (var current in tokens)
            {
                if (current is RawTextToken)
                {
                    prevToken = new RawTextToken(prevToken, current);
                }
                else
                {
                    if (prevToken != null)
                    {
                        yield return(prevToken);
                    }

                    prevToken = null;
                    yield return(current);
                }
            }

            if (prevToken != null)
            {
                yield return(prevToken);
            }
        }
예제 #2
0
            /// <summary>
            /// fail return null
            /// </summary>
            /// <returns></returns>
            public static TextDataBindExpress Parse(string input)
            {
                if (input == null || "".Equals(input))
                {
                    return(null);
                }

                List <RawTextToken> rawList = new List <RawTextToken>();

                for (int i = 0; i < input.Length; i++)
                {
                    var curr = input[i];
                    if (curr == '{' && i + 1 < input.Length && input[i + 1] == '{')
                    {
                        rawList.Add(new RawTextToken(-1, ' '));
                        i++;
                    }
                    else if (curr == '}' && i + 1 < input.Length && input[i + 1] == '}')
                    {
                        rawList.Add(new RawTextToken(1, ' '));
                        i++;
                    }
                    else
                    {
                        rawList.Add(new RawTextToken(0, curr));
                    }
                }

                // RawTextToken to TextToken
                List <TextToken> tokenList = new List <TextToken>();

                for (int i = 0; i < rawList.Count;)
                {
                    if (rawList[i].type == -1)
                    {
                        int startIndex = i + 1;
                        i++;
                        while (i < rawList.Count && rawList[i].type != 1)
                        {
                            i++;
                        }

                        var str = RawTextToken.ToString(rawList, startIndex, i - startIndex);
                        if (i == rawList.Count) // EOF
                        {
                            // normal text
                            tokenList.Add(new TextToken(str));
                            break;
                        }

                        var express = ObjectDataBindExpress.Parse(str);
                        if (express != null)
                        {
                            tokenList.Add(new TextToken(express));
                        }

                        i++;
                    }
                    else
                    {
                        int startIndex = i;
                        while (i < rawList.Count && rawList[i].type != -1)
                        {
                            i++;
                        }
                        int length = i - startIndex;
                        if (length > 0)
                        {
                            var str = RawTextToken.ToString(rawList, startIndex, length);
                            tokenList.Add(new TextToken(str));
                        }
                    }
                }

                return(tokenList.Count > 0 ? new TextDataBindExpress(tokenList) : null);
            }