Пример #1
0
        private LSObject ParseObject()
        {
            Consume();
            var obj  = new LSObject();
            var name = new StringBuilder();

            while (true)
            {
                switch (LookAhead())
                {
                case LSToken.Comma:
                    Consume();
                    //分隔符,清除name
                    name.Clear();
                    break;

                case LSToken.RBracket:    //]} 对象终止
                case LSToken.RBrace:
                    Consume();
                    //处理未能识别的表达式
                    foreach (var t in dictionary.Keys.ToArray())
                    {
                        var t2 = obj.Find(dictionary[t]);
                        if (t2 != null)
                        {
                            if (t.ValueType == LSType.Object)
                            {
                                t2.CopyTo(t);
                            }
                            else if (t.ValueType == LSType.String && t2.ValueType != LSType.Object)
                            {
                                var s = t.Value as string;
                                t.Value = s.Replace("${" + dictionary[t] + "}", t2.Value?.ToString());
                            }
                            dictionary.Remove(t);
                        }
                    }
                    return(obj);

                case LSToken.Colon:     //:
                    Consume();
                    if (LookAhead() != LSToken.Expression)
                    {
                        var value = ParseValue();
                        var t     = obj.Add(name.ToString(), value);
                        if (value is string s)
                        {
                            var match = Regex.Match(s, @"\$\{.*\}");
                            var exp   = match.Value;
                            if (exp != string.Empty && (match.Index < 1 || s[match.Index - 1] != '\\'))
                            {
                                dictionary.Add(t, exp.Substring(2, exp.Length - 3));
                            }
                        }
                        name.Clear();
                    }
                    break;

                case LSToken.Dot:
                    Consume();
                    //refrence
                    name.Append(".");
                    break;

                case LSToken.Expression:
                    var key     = ParseExpression();
                    var current = obj.Find(key);
                    var tp      = obj.Add(name.ToString(), current);
                    if (current == null)
                    {
                        dictionary.Add(tp, key);
                    }
                    current = null;
                    name.Clear();
                    break;

                case LSToken.Identifier:
                    name.Append(ParseIdentifier());
                    break;

                case LSToken.Charcter:    // 字符/字符串
                case LSToken.String:
                    var val = ParseValue();
                    if (LookAhead() == LSToken.Colon)       //兼容字符串命名
                    {
                        name.Clear();
                        name.Append(val + "");
                    }
                    else
                    {
                        obj.Add(val);
                    }
                    break;

                case LSToken.LBrace:
                case LSToken.LBracket:
                case LSToken.True:
                case LSToken.False:
                case LSToken.Number:
                case LSToken.Null:
                    obj.Add(ParseValue());
                    break;

                default:
                    throw new Exception($"无法识别的字符!'{source[index]}'");
                }
            }
        }