Exemplo n.º 1
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count > 0 && ParserHelpers.IsEqual(Field.KEY_FOREACH, tc.First.Text))
            {
                if (tc.Count > 5 &&
                    tc[1].TokenKind == TokenKind.LeftParentheses &&
                    tc[2].TokenKind == TokenKind.TextData &&
                    ParserHelpers.IsEqual(tc[3].Text, Field.KEY_IN) &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    ForeachTag tag = new ForeachTag();
                    tag.Name = tc[2].Text;
                    TokenCollection coll = new TokenCollection();
                    coll.Add(tc, 4, tc.Count - 2);
                    tag.Source = parser.Read(coll);

                    while (parser.MoveNext())
                    {
                        tag.Children.Add(parser.Current);
                        if (parser.Current is EndTag)
                        {
                            return(tag);
                        }
                    }

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

            return(null);
        }
Exemplo n.º 2
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count == 1 && tc.First.TokenKind == TokenKind.TextData && ParserHelpers.IsEqual(tc.First.Text, Field.KEY_NULL))
            {
                return(new NullTag());
            }

            return(null);
        }
Exemplo n.º 3
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count == 1 &&
                ParserHelpers.IsEqual(tc.First.Text, Field.KEY_END))
            {
                return(new EndTag());
            }

            return(null);
        }
Exemplo n.º 4
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (ParserHelpers.IsEqual(tc.First.Text, Field.KEY_INCLUDE))
            {
                if (tc.Count > 2 &&
                    (tc[1].TokenKind == TokenKind.LeftParentheses) &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    IncludeTag tag = new IncludeTag();
                    tag.Path = parser.Read(new TokenCollection(tc, 2, tc.Count - 2));
                    return(tag);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (ParserHelpers.IsEqual(tc.First.Text, Field.KEY_JSON))
            {
                if (tc.Count > 2 &&
                    (tc[1].TokenKind == TokenKind.LeftParentheses) &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    Tag     vtag = parser.Read(new TokenCollection(tc, 2, tc.Count - 2));
                    JsonTag tag  = new JsonTag();
                    tag.Json = vtag;
                    return(tag);
                }
            }

            return(null);
        }
Exemplo n.º 6
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count > 3 &&
                ParserHelpers.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 ParseException(string.Concat("if is not properly closed by a end tag:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                }
                else
                {
                    throw new ParseException(string.Concat("syntax error near if:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count > 3 &&
                ParserHelpers.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 ParseException(string.Concat("syntax error near if:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                }
            }

            return(null);
        }
Exemplo n.º 8
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (ParserHelpers.IsEqual(tc.First.Text, Field.KEY_ARRAY))
            {
                if (tc.Count > 2 &&
                    (tc[1].TokenKind == TokenKind.LeftParentheses) &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    int               pos   = 0;
                    int               begin = 2;
                    int               end;
                    Tag               tmp;
                    ArrayTag          tag  = new ArrayTag();
                    Stack <TokenKind> last = new Stack <TokenKind>();
                    for (int i = 2; i < tc.Count; ++i)
                    {
                        if ((tc[i].TokenKind == TokenKind.Comma && last.Count == 0) || i == (tc.Count - 1))
                        {
                            end = i - 1;
                            tmp = parser.Read(new TokenCollection(tc, begin, end));
                            if (tmp is ExpressionTag || tmp is SetTag)
                            {
                                if (tmp is SetTag)
                                {
                                    SetTag      set = (SetTag)tmp;
                                    VariableTag var = new VariableTag();
                                    var.Name = set.Name;
                                    object val = var.Parse(parser.Context);
                                    if (val != null)
                                    {
                                        if (val is string)
                                        {
                                            tag.Set((string)val, set.Value, parser.Context);
                                        }
                                        else if (VariableTag.IsNumber(val))
                                        {
                                            tag.Set((int)val, set.Value, parser.Context);
                                        }
                                        else
                                        {
                                            throw new ParseException(string.Concat("array key type is error:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                                        }
                                    }
                                    else
                                    {
                                        throw new ParseException(string.Concat("array key is null referer:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                                    }
                                }
                                else if (tmp.Children[0] is StringTag)
                                {
                                    tag.Set(((StringTag)tmp.Children[0]).Value, tmp.Children[2], parser.Context);
                                }
                                else if (tmp.Children[0] is NumberTag)
                                {
                                    tag.Set((int)((NumberTag)tmp.Children[0]).Value, tmp.Children[2], parser.Context);
                                }
                                else
                                {
                                    throw new ParseException(string.Concat("array key type is error:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                                }
                            }
                            else
                            {
                                tag.Set(pos, tmp, parser.Context);
                                ++pos;
                            }
                            begin = i + 1;
                        }
                        else if (tc[i].TokenKind == TokenKind.StringStart)
                        {
                            last.Push(TokenKind.StringStart);
                        }
                        else if (tc[i].TokenKind == TokenKind.LeftParentheses)
                        {
                            last.Push(TokenKind.LeftParentheses);
                        }
                        else if (tc[i].TokenKind == TokenKind.StringEnd && last.Peek() == TokenKind.StringStart)
                        {
                            last.Pop();
                        }
                        else if (tc[i].TokenKind == TokenKind.RightParentheses && last.Peek() == TokenKind.LeftParentheses)
                        {
                            last.Pop();
                        }
                    }
                    return(tag);
                }
            }

            return(null);
        }
Exemplo n.º 9
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            //支持写法:简写格式:
            //常规格式:
            if (tc.Count > 5 &&
                ParserHelpers.IsEqual(tc.First.Text, Field.KEY_SET) &&
                tc[1].TokenKind == TokenKind.LeftParentheses &&
                tc[3].Text == "=" &&
                tc.Last.TokenKind == TokenKind.RightParentheses)
            {
                SetTag tag = new SetTag();
                tag.Name = tc[2].Text;

                TokenCollection coll = new TokenCollection();
                coll.Add(tc, 4, tc.Count - 2);

                tag.Value = parser.Read(coll);
                return(tag);
            }
            else if (tc.Count == 2 &&
                     tc.First.TokenKind == TokenKind.TextData &&
                     tc.Last.TokenKind == TokenKind.Operator &&
                     (tc.Last.Text == "++" || tc.Last.Text == "--"))
            {
                SetTag tag = new SetTag();
                tag.Name = tc.First.Text;

                ExpressionTag c = new ExpressionTag();
                c.AddChild(new VariableTag()
                {
                    FirstToken = tc.First,
                    Name       = tc.First.Text
                });
                c.AddChild(new TextTag()
                {
                    FirstToken = new Token(TokenKind.Operator, tc.Last.Text[0].ToString())
                });
                c.AddChild(new NumberTag()
                {
                    Value      = 1,
                    FirstToken = new Token(TokenKind.Number, "1")
                });

                tag.Value = c;
                return(tag);
            }
            else if (tc.Count > 2 &&
                     tc.First.TokenKind == TokenKind.TextData &&
                     tc[1].Text == "=")
            {
                SetTag tag = new SetTag();
                tag.Name = tc.First.Text;

                TokenCollection coll = new TokenCollection();
                coll.Add(tc, 2, tc.Count - 1);

                tag.Value = parser.Read(coll);
                return(tag);
            }

            return(null);
        }
Exemplo n.º 10
0
        public Tag Parse(TemplateParser parser, TokenCollection tc)
        {
            if (tc.Count > 3 && ParserHelpers.IsEqual(Field.KEY_FOR, tc.First.Text))
            {
                if (tc[1].TokenKind == TokenKind.LeftParentheses &&
                    tc.Last.TokenKind == TokenKind.RightParentheses)
                {
                    int pos        = 0,
                             start = 2,
                             end;

                    List <Tag> ts = new List <Tag>(3);

                    ForTag tag = new ForTag();
                    for (int i = 2; i < tc.Count - 1; ++i)
                    {
                        end = i;
                        if (tc[i].TokenKind == TokenKind.Punctuation && tc[i].Text == ";")
                        {
                            if (pos == 0)
                            {
                                TokenCollection coll = new TokenCollection();
                                coll.Add(tc, start, end - 1);
                                if (coll.Count > 0)
                                {
                                    ts.Add(parser.Read(coll));
                                }
                                else
                                {
                                    ts.Add(null);
                                }
                                start = i + 1;
                                continue;
                            }
                        }

                        if (tc[i].TokenKind == TokenKind.LeftParentheses)
                        {
                            ++pos;
                        }
                        else if (tc[i].TokenKind == TokenKind.RightParentheses)
                        {
                            --pos;
                        }
                        if (i == tc.Count - 2)
                        {
                            TokenCollection coll = new TokenCollection();
                            coll.Add(tc, start, end);
                            if (coll.Count > 0)
                            {
                                ts.Add(parser.Read(coll));
                            }
                            else
                            {
                                ts.Add(null);
                            }
                        }
                    }

                    if (ts.Count != 3)
                    {
                        throw new ParseException(string.Concat("syntax error near for:", tc), parser.Context.CurrentPath, tc.First.BeginLine, tc.First.BeginColumn);
                    }

                    tag.Initial = ts[0];
                    tag.Test    = ts[1];
                    tag.Do      = ts[2];

                    while (parser.MoveNext())
                    {
                        tag.Children.Add(parser.Current);
                        if (parser.Current is EndTag)
                        {
                            return(tag);
                        }
                    }

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

            return(null);
        }