示例#1
0
            private ArrayTag ReadArray(TemplateContext context)
            {
                Debug.Assert(Peek() == '[');
                Read(); // Skip '['
                SkipWhitespace();
                ArrayTag value = new ArrayTag();

                if (Peek() == ']')
                {
                    Read(); // Skip ']'
                    return(value);
                }
                for (;;)
                {
                    value.Set(value.Count, ReadValue(context), context);
                    SkipWhitespace();
                    switch (Read())
                    {
                    case ',': SkipWhitespace(); break;

                    case ']': return(value);

                    default: throw new ParseException(string.Concat("Must be a comma or ']' after an array element. ", ReadToEnd()));
                    }
                }
            }
示例#2
0
            private ArrayTag ReadObject(TemplateContext context)
            {
                Debug.Assert(Peek() == '{');
                Read(); // Skip '{'
                SkipWhitespace();
                ArrayTag value = new ArrayTag();

                if (Peek() == '}')
                {
                    Read();
                    return(value);
                }
                StringTag key;

                for (int memberCount = 0; ;)
                {
                    if (Peek() != '"')
                    {
                        throw new ParseException(string.Concat("Name of an object member must be a string. ", ReadToEnd()));
                    }
                    key = ReadString();
                    SkipWhitespace();
                    if (Peek() != ':')
                    {
                        throw new ParseException(string.Concat("There must be a colon after the name of object member. ", ReadToEnd()));
                    }
                    Read(); // Skip ':'
                    SkipWhitespace();
                    value.Set(key.ToString(), ReadValue(context), context);
                    SkipWhitespace();
                    ++memberCount;
                    switch (Read())
                    {
                    case ',': SkipWhitespace(); break;

                    case '}': return(value);

                    default: throw new ParseException(string.Concat("Must be a comma or '}' after an object member. ", ReadToEnd()));
                    }
                }
            }
示例#3
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);
        }