Esempio n. 1
0
        /// <summary>
        /// 分析标签
        /// </summary>
        /// <param name="parser">TemplateParser</param>
        /// <param name="tc">Token集合</param>
        /// <returns></returns>
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc != null &&
                parser != null &&
                tc.Count > 3 &&
                Common.Utility.IsEqual(tc.First.Text, Field.KEY_ELSEIF))
            {
                if (tc[1].TokenKind == TokenKind.LeftParentheses &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    ElseifTag tag = new ElseifTag();

                    TokenCollection coll = new TokenCollection();
                    coll.Add(tc, 2, tc.Count - 2);
                    tag.Test = parser.Read(coll);

                    return(tag);
                }
                else
                {
                    throw new Exception.ParseException(String.Concat("syntax error near if:", tc), tc.First.BeginLine, tc.First.BeginColumn);
                }
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        internal static Type GuessIfType(this CompileContext ctx, ElseifTag tag)
        {
            var types   = new List <Type>();
            var hasVoid = false;

            for (var i = 0; i < tag.Children.Count; i++)
            {
                var type = ctx.GuessType(tag.Children[i]);
                if (type.FullName == "System.Void")
                {
                    hasVoid = true;
                }
                else
                {
                    types.Add(type);
                }
            }
            if (types.Count == 1)
            {
                return(types[0]);
            }
            if (types.Count == 0 && hasVoid)
            {
                return(typeof(void));
            }
            return(typeof(string));
        }
Esempio n. 3
0
        /// <inheritdoc />
        public override Func <TemplateParser, TokenCollection, ITag> BuildParseMethod()
        {
            return((parser, tc) =>
            {
                if (tc != null &&
                    parser != null &&
                    tc.Count > 3 &&
                    Utility.IsEqual(tc.First.Text, Field.KEY_IF))
                {
                    if (tc[1].TokenKind == TokenKind.LeftParentheses &&
                        tc.Last.TokenKind == TokenKind.RightParentheses)
                    {
                        var tag = new IfTag();

                        var t = new ElseifTag();
                        TokenCollection coll = tc[2, -1];
                        t.Condition = parser.Read(coll);
                        t.FirstToken = coll.First;
                        //t.LastToken = coll.Last;
                        tag.AddChild(t);

                        while (parser.MoveNext())
                        {
                            if (parser.Current is EndTag)
                            {
                                tag.AddChild(parser.Current);
                                return tag;
                            }
                            else if (parser.Current is ElseifTag ||
                                     parser.Current is ElseTag)
                            {
                                tag.AddChild(parser.Current);
                            }
                            else
                            {
                                tag.Children[tag.Children.Count - 1].AddChild(parser.Current);
                            }
                        }

                        throw new Exception.ParseException(string.Concat("if is not properly closed by a end tag:", tc), tc.First.BeginLine, tc.First.BeginColumn);
                    }
                    else
                    {
                        throw new Exception.ParseException(string.Concat("syntax error near if:", tc), tc.First.BeginLine, tc.First.BeginColumn);
                    }
                }

                return null;
            });
        }
Esempio n. 4
0
        /// <summary>
        /// 分析标签
        /// </summary>
        /// <param name="parser">TemplateParser</param>
        /// <param name="tc">Token集合</param>
        /// <returns></returns>
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc != null &&
                parser != null &&
                tc.Count > 3 &&
                Common.Utility.IsEqual(tc.First.Text, Field.KEY_IF))
            {
                if (tc[1].TokenKind == TokenKind.LeftParentheses &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    IfTag tag = new IfTag();

                    ElseifTag       t    = new ElseifTag();
                    TokenCollection coll = new TokenCollection();
                    coll.Add(tc, 2, tc.Count - 2);
                    t.Test       = parser.Read(coll);
                    t.FirstToken = coll.First;
                    //t.LastToken = coll.Last;
                    tag.AddChild(t);

                    while (parser.MoveNext())
                    {
                        if (parser.Current is EndTag)
                        {
                            tag.AddChild(parser.Current);
                            return(tag);
                        }
                        else if (parser.Current is ElseifTag ||
                                 parser.Current is ElseTag)
                        {
                            tag.AddChild(parser.Current);
                        }
                        else
                        {
                            tag.Children[tag.Children.Count - 1].AddChild(parser.Current);
                        }
                    }

                    throw new Exception.ParseException(String.Concat("if is not properly closed by a end tag:", tc), tc.First.BeginLine, tc.First.BeginColumn);
                }
                else
                {
                    throw new Exception.ParseException(String.Concat("syntax error near if:", tc), tc.First.BeginLine, tc.First.BeginColumn);
                }
            }

            return(null);
        }
Esempio n. 5
0
        /// <summary>
        /// 分析标签
        /// </summary>
        /// <param name="parser">TemplateParser</param>
        /// <param name="tc">Token集合</param>
        /// <returns></returns>
        public ITag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc != null &&
                parser != null &&
                tc.Count > 3 &&
                (Utility.IsEqual(tc.First.Text, Field.KEY_ELSEIF) || Utility.IsEqual(tc.First.Text, Field.KEY_ELIF)) &&
                tc[1].TokenKind == TokenKind.LeftParentheses &&
                tc.Last.TokenKind == TokenKind.RightParentheses)
            {
                ElseifTag tag = new ElseifTag();

                TokenCollection coll = new TokenCollection();
                tag.Test = parser.Read(tc[2, -1]);

                return(tag);
                //}
                //else
                //{
                //    throw new Exception.ParseException(string.Concat("syntax error near if:", tc), tc.First.BeginLine, tc.First.BeginColumn);
                //}
            }

            return(null);
        }