コード例 #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);
        }
コード例 #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);
        }
コード例 #3
0
        public TjsValue Parse(TextReader r)
        {
            TjsParser.Token token = null;
            do
            {
                token = GetNext(r);
                if (token.t == TokenType.Number)
                {
                    // 解析出数字
                    TjsNumber ret = token.ToTjsNumber();
                    if (ret == null)
                    {
                        // 数字格式错误
                        ShowError("Invalid Number");
                        break;
                    }
                    return(ret);
                }
                else if (token.t == TokenType.String)
                {
                    // 解析出字符串
                    TjsString ret = token.ToTjsString();
                    if (ret == null)
                    {
                        // 字符串格式错误
                        ShowError("Invalid String");
                        break;
                    }
                    return(ret);
                }
                else if (token.t == TokenType.Symbol)
                {
                    // 兼容2.28
                    if (token.val == "(const)" || token.val == "int" || token.val == "string")
                    {
                        // 啥也不干
                    }
                    else if (token.val == "void")
                    {
                        // 解析出空值
                        return(new TjsVoid());
                    }
                    else if (token.val == "%[")
                    {
                        // 返回字典
                        TjsDict ret = ParseDict(r);
                        return(ret);
                    }
                    else if (token.val == "[")
                    {
                        // 返回数组
                        TjsArray ret = ParseArray(r);
                        return(ret);
                    }
                    else
                    {
                        // 无效的符号
                        ShowError("Invalid Symbol");
                        break;
                    }
                }
            } while (token.t != TokenType.Unknow);

            if (token.t != TokenType.Unknow)
            {
                ShowError("Value Parsing Failed");
                ShowError(token);
            }

            return(null);
        }
コード例 #4
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);
        }