Exemplo n.º 1
0
        public TjsDict SetValue(string namepath, TjsValue value)
        {
            int split = namepath.IndexOf("/");

            if (split < 0)
            {
                // 返回最底层的dict
                this.val[namepath] = value;
                return(this);
            }
            else
            {
                string name = namepath.Substring(0, split);

                TjsValue v = null;
                if (this.val.TryGetValue(name, out v))
                {
                    TjsDict next = v as TjsDict;
                    if (next != null)
                    {
                        return(next.SetValue(namepath.Substring(split + 1), value));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public TjsValue GetValue(string namepath)
        {
            int split = namepath.IndexOf("/");

            if (split < 0)
            {
                // 读取具体值
                TjsValue v = null;
                this.val.TryGetValue(namepath, out v);
                return(v);
            }
            else
            {
                string name = namepath.Substring(0, split);

                TjsValue v = null;
                if (this.val.TryGetValue(name, out v))
                {
                    TjsDict next = v as TjsDict;
                    if (next != null)
                    {
                        return(next.GetValue(namepath.Substring(split + 1)));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        public double GetNumber(string name)
        {
            TjsValue v = null;

            this.val.TryGetValue(name, out v);

            TjsNumber ret = v as TjsNumber;

            return((ret != null) ? ret.val : double.NaN);
        }
Exemplo n.º 4
0
        public string GetString(string name)
        {
            TjsValue v = null;

            this.val.TryGetValue(name, out v);

            TjsString ret = v as TjsString;

            return((ret != null) ? ret.val : null);
        }
Exemplo n.º 5
0
        TjsArray ParseArray(TextReader r)
        {
            TjsParser.Token token = null;

            List <TjsValue> inner = new List <TjsValue>();

            do
            {
                // 读取一个值
                TjsValue val = Parse(r);

                // 直接返回错误
                if (val == null)
                {
                    return(null);
                }

                inner.Add(val);

                // 读取其后的符号
                token = GetNext(r);
                if (token.t == TokenType.Symbol)
                {
                    if (token.val == "]")
                    {
                        // 读取完毕
                        TjsArray ret = new TjsArray(inner);
                        return(ret);
                    }
                    else if (token.val != ",")
                    {
                        // 错误的符号
                        ShowError("Expect a Comma");
                        break;
                    }
                }
                else
                {
                    // 错误的符号
                    ShowError("Expect a Symbol");
                    break;
                }
            } while (token.t != TokenType.Unknow);

            ShowError("Array Parsing Failed");
            ShowError(token);
            return(null);
        }
Exemplo n.º 6
0
        TjsDict ParseDict(TextReader r)
        {
            TjsParser.Token token = null;

            Dictionary <string, TjsValue> inner = new Dictionary <string, TjsValue>();

            // 初始状态为读取key状态
            DictState s   = DictState.Key;
            string    key = null;

            do
            {
                token = GetNext(r);

                if (s == DictState.Key)
                {
                    // 读取键值
                    if (token.t == TokenType.String && key == null)
                    {
                        TjsString tmp = token.ToTjsString();
                        if (tmp == null)
                        {
                            // 无效的键值
                            ShowError("Invalid Key");
                            break;
                        }

                        // 去掉双引号
                        key = tmp.val;

                        if (inner.ContainsKey(key))
                        {
                            // 重复的键值
                            ShowError("Duplicated Key");
                            break;
                        }

                        // 切换到读取Value状态
                        s = DictState.Value;
                    }
                    else
                    {
                        // 错误的键值
                        ShowError("Expect a Key");
                        break;
                    }
                }
                else if (s == DictState.Value)
                {
                    if (token.t == TokenType.Symbol)
                    {
                        if (key != null && token.val == "=>")
                        {
                            // 读取一个值
                            TjsValue val = Parse(r);

                            // 直接返回错误
                            if (val == null)
                            {
                                return(null);
                            }

                            inner.Add(key, val);
                            key = null;
                        }
                        else if (key == null && token.val == ",")
                        {
                            // 切换为读取key状态
                            s = DictState.Key;
                        }
                        else if (key == null && token.val == "]")
                        {
                            // 读取完毕
                            TjsDict ret = new TjsDict(inner);
                            return(ret);
                        }
                        else
                        {
                            // 无效的符号
                            ShowError("Invalid Symbol");
                            break;
                        }
                    }
                    else
                    {
                        // 错误的符号
                        ShowError("Expect a Symbol");
                        break;
                    }
                }
            } while (token.t != TokenType.Unknow);

            ShowError("Dictionary Parsing Failed");
            ShowError(token);
            return(null);
        }